symon.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* $Id: symon.c,v 1.49 2007/11/29 13:55:30 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2007 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <sys/param.h>
  32. #include <sys/time.h>
  33. #include <errno.h>
  34. #include <fcntl.h>
  35. #include <limits.h>
  36. #include <pwd.h>
  37. #include <signal.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <sysexits.h>
  42. #include <syslog.h>
  43. #include <time.h>
  44. #include <unistd.h>
  45. #include "conf.h"
  46. #include "data.h"
  47. #include "error.h"
  48. #include "net.h"
  49. #include "readconf.h"
  50. #include "symon.h"
  51. #include "symonnet.h"
  52. #include "xmalloc.h"
  53. __BEGIN_DECLS
  54. void alarmhandler(int);
  55. void drop_privileges();
  56. void exithandler(int);
  57. void huphandler(int);
  58. void set_stream_use(struct muxlist *);
  59. __END_DECLS
  60. int flag_unsecure = 0;
  61. int flag_hup = 0;
  62. int flag_testconf = 0;
  63. int symon_interval = SYMON_DEFAULT_INTERVAL;
  64. /* map stream types to inits and getters */
  65. struct funcmap streamfunc[] = {
  66. {MT_IO1, 0, NULL, init_io, gets_io, get_io},
  67. {MT_CPU, 0, NULL, init_cpu, gets_cpu, get_cpu},
  68. {MT_MEM, 0, NULL, init_mem, gets_mem, get_mem},
  69. {MT_IF, 0, NULL, init_if, gets_if, get_if},
  70. {MT_PF, 0, privinit_pf, init_pf, gets_pf, get_pf},
  71. {MT_DEBUG, 0, NULL, init_debug, NULL, get_debug},
  72. {MT_PROC, 0, privinit_proc, init_proc, gets_proc, get_proc},
  73. {MT_MBUF, 0, NULL, init_mbuf, NULL, get_mbuf},
  74. {MT_SENSOR, 0, privinit_sensor, init_sensor, NULL, get_sensor},
  75. {MT_IO2, 0, NULL, init_io, gets_io, get_io},
  76. {MT_PFQ, 0, privinit_pfq, init_pfq, gets_pfq, get_pfq},
  77. {MT_DF, 0, NULL, init_df, gets_df, get_df},
  78. {MT_EOT, 0, NULL, NULL, NULL}
  79. };
  80. void
  81. set_stream_use(struct muxlist *mul)
  82. {
  83. struct mux *mux;
  84. struct stream *stream;
  85. int i;
  86. for (i = 0; i < MT_EOT; i++)
  87. streamfunc[i].used = 0;
  88. SLIST_FOREACH(mux, mul, muxes) {
  89. SLIST_FOREACH(stream, &mux->sl, streams)
  90. streamfunc[stream->type].used = 1;
  91. }
  92. }
  93. void
  94. drop_privileges(int unsecure)
  95. {
  96. struct passwd *pw;
  97. if (unsecure) {
  98. if (setegid(getgid()) || setgid(getgid()) ||
  99. seteuid(getuid()) || setuid(getuid()))
  100. fatal("can't drop privileges: %.200s", strerror(errno));
  101. } else {
  102. if ((pw = getpwnam(SYMON_USER)) == NULL)
  103. fatal("could not get user information for user '%.200s': %.200s",
  104. SYMON_USER, strerror(errno));
  105. if (chroot(pw->pw_dir) < 0)
  106. fatal("chroot failed: %.200s", strerror(errno));
  107. if (chdir("/") < 0)
  108. fatal("chdir / failed: %.200s", strerror(errno));
  109. if (setgroups(1, &pw->pw_gid))
  110. fatal("can't setgroups: %.200s", strerror(errno));
  111. if (setgid(pw->pw_gid))
  112. fatal("can't set group id: %.200s", strerror(errno));
  113. if (setegid(pw->pw_gid))
  114. fatal("can't set effective group id: %.200s", strerror(errno));
  115. if (setuid(pw->pw_uid))
  116. fatal("can't set user id: %.200s", strerror(errno));
  117. if (seteuid(pw->pw_uid))
  118. fatal("can't set effective user id: %.200s", strerror(errno));
  119. }
  120. }
  121. /* alarmhandler that gets called every symon_interval */
  122. void
  123. alarmhandler(int s)
  124. {
  125. /* EMPTY */
  126. }
  127. void
  128. exithandler(int s)
  129. {
  130. info("received signal %d - quitting", s);
  131. exit(1);
  132. }
  133. void
  134. huphandler(int s)
  135. {
  136. info("hup received");
  137. flag_hup = 1;
  138. }
  139. /*
  140. * Symon is a system measurement utility.
  141. *
  142. * The main goals symon hopes to accomplish are:
  143. * - to take fine grained measurements of system parameters
  144. * - with minimal performance impact
  145. * - in a secure way.
  146. *
  147. * Measurements are processed by a second program called symux. symon and symux
  148. * communicate via udp.
  149. */
  150. int
  151. main(int argc, char *argv[])
  152. {
  153. struct muxlist mul, newmul;
  154. struct itimerval alarminterval;
  155. struct stream *stream;
  156. struct mux *mux;
  157. time_t now, last_update;
  158. FILE *pidfile;
  159. char *cfgpath;
  160. int ch;
  161. int i;
  162. SLIST_INIT(&mul);
  163. /* reset flags */
  164. flag_debug = 0;
  165. flag_daemon = 0;
  166. flag_unsecure = 0;
  167. cfgpath = SYMON_CONFIG_FILE;
  168. while ((ch = getopt(argc, argv, "df:tuv")) != -1) {
  169. switch (ch) {
  170. case 'd':
  171. flag_debug = 1;
  172. break;
  173. case 'f':
  174. cfgpath = xstrdup(optarg);
  175. break;
  176. case 't':
  177. flag_testconf = 1;
  178. break;
  179. case 'u':
  180. flag_unsecure = 1;
  181. break;
  182. case 'v':
  183. info("symon version %s", SYMON_VERSION);
  184. default:
  185. info("usage: %s [-d] [-t] [-u] [-v] [-f cfgfile]", __progname);
  186. exit(EX_USAGE);
  187. }
  188. }
  189. if (!read_config_file(&mul, cfgpath))
  190. fatal("configuration file contained errors - aborting");
  191. if (flag_testconf) {
  192. info("%s: ok", cfgpath);
  193. exit(EX_OK);
  194. }
  195. set_stream_use(&mul);
  196. /* open resources that might not be available after privilege drop */
  197. for (i = 0; i < MT_EOT; i++)
  198. if (streamfunc[i].used && (streamfunc[i].privinit != NULL))
  199. (streamfunc[i].privinit) ();
  200. if ((pidfile = fopen(SYMON_PID_FILE, "w")) == NULL)
  201. warning("could not open \"%.200s\", %.200s", SYMON_PID_FILE,
  202. strerror(errno));
  203. if (flag_debug != 1) {
  204. if (daemon(0, 0) != 0)
  205. fatal("daemonize failed: %.200s", strerror(errno));
  206. flag_daemon = 1;
  207. if (pidfile) {
  208. fprintf(pidfile, "%u\n", (u_int) getpid());
  209. fclose(pidfile);
  210. }
  211. }
  212. drop_privileges(flag_unsecure);
  213. info("symon version %s", SYMON_VERSION);
  214. if (flag_debug == 1)
  215. info("program id=%d", (u_int) getpid());
  216. /* setup signal handlers */
  217. signal(SIGALRM, alarmhandler);
  218. signal(SIGHUP, huphandler);
  219. signal(SIGINT, exithandler);
  220. signal(SIGQUIT, exithandler);
  221. signal(SIGTERM, exithandler);
  222. /* prepare crc32 */
  223. init_crc32();
  224. /* init modules */
  225. SLIST_FOREACH(mux, &mul, muxes) {
  226. init_symon_packet(mux);
  227. connect2mux(mux);
  228. SLIST_FOREACH(stream, &mux->sl, streams) {
  229. (streamfunc[stream->type].init) (stream);
  230. }
  231. }
  232. /* setup alarm */
  233. timerclear(&alarminterval.it_interval);
  234. timerclear(&alarminterval.it_value);
  235. alarminterval.it_interval.tv_sec =
  236. alarminterval.it_value.tv_sec = symon_interval;
  237. if (setitimer(ITIMER_REAL, &alarminterval, NULL) != 0) {
  238. fatal("alarm setup failed: %.200s", strerror(errno));
  239. }
  240. last_update = time(NULL);
  241. for (;;) { /* FOREVER */
  242. sleep(symon_interval * 2); /* alarm will interrupt sleep */
  243. now = time(NULL);
  244. if (flag_hup == 1) {
  245. flag_hup = 0;
  246. SLIST_INIT(&newmul);
  247. if (flag_unsecure) {
  248. if (!read_config_file(&newmul, cfgpath)) {
  249. info("new configuration contains errors; keeping old configuration");
  250. free_muxlist(&newmul);
  251. } else {
  252. free_muxlist(&mul);
  253. mul = newmul;
  254. info("read configuration file '%.200s' successfully", cfgpath);
  255. /* init modules */
  256. SLIST_FOREACH(mux, &mul, muxes) {
  257. init_symon_packet(mux);
  258. connect2mux(mux);
  259. SLIST_FOREACH(stream, &mux->sl, streams) {
  260. (streamfunc[stream->type].init) (stream);
  261. }
  262. }
  263. set_stream_use(&mul);
  264. }
  265. } else {
  266. info("configuration unreachable because of privsep; keeping old configuration");
  267. }
  268. } else {
  269. /* check timing to catch ntp drifts */
  270. if (now < last_update ||
  271. now > last_update + symon_interval + symon_interval) {
  272. info("last update seems long ago - assuming system time change");
  273. last_update = now;
  274. } else if (now < last_update + symon_interval) {
  275. debug("did not sleep %d seconds - skipping a measurement", symon_interval);
  276. continue;
  277. }
  278. last_update = now;
  279. /* populate for modules that get all their measurements in one go */
  280. for (i = 0; i < MT_EOT; i++)
  281. if (streamfunc[i].used && (streamfunc[i].gets != NULL))
  282. (streamfunc[i].gets) ();
  283. SLIST_FOREACH(mux, &mul, muxes) {
  284. prepare_packet(mux);
  285. SLIST_FOREACH(stream, &mux->sl, streams)
  286. stream_in_packet(stream, mux);
  287. finish_packet(mux);
  288. send_packet(mux);
  289. }
  290. }
  291. }
  292. return (EX_SOFTWARE); /* NOTREACHED */
  293. }