symon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* $Id: symon.c,v 1.50 2007/12/11 14:17:59 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_MEM1, 0, NULL, init_mem, gets_mem, get_mem},
  69. {MT_IF1, 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_MEM2, 0, NULL, init_mem, gets_mem, get_mem},
  79. {MT_IF2, 0, NULL, init_if, gets_if, get_if},
  80. {MT_EOT, 0, NULL, NULL, NULL}
  81. };
  82. void
  83. set_stream_use(struct muxlist *mul)
  84. {
  85. struct mux *mux;
  86. struct stream *stream;
  87. int i;
  88. for (i = 0; i < MT_EOT; i++)
  89. streamfunc[i].used = 0;
  90. SLIST_FOREACH(mux, mul, muxes) {
  91. SLIST_FOREACH(stream, &mux->sl, streams)
  92. streamfunc[stream->type].used = 1;
  93. }
  94. }
  95. void
  96. drop_privileges(int unsecure)
  97. {
  98. struct passwd *pw;
  99. if (unsecure) {
  100. if (setegid(getgid()) || setgid(getgid()) ||
  101. seteuid(getuid()) || setuid(getuid()))
  102. fatal("can't drop privileges: %.200s", strerror(errno));
  103. } else {
  104. if ((pw = getpwnam(SYMON_USER)) == NULL)
  105. fatal("could not get user information for user '%.200s': %.200s",
  106. SYMON_USER, strerror(errno));
  107. if (chroot(pw->pw_dir) < 0)
  108. fatal("chroot failed: %.200s", strerror(errno));
  109. if (chdir("/") < 0)
  110. fatal("chdir / failed: %.200s", strerror(errno));
  111. if (setgroups(1, &pw->pw_gid))
  112. fatal("can't setgroups: %.200s", strerror(errno));
  113. if (setgid(pw->pw_gid))
  114. fatal("can't set group id: %.200s", strerror(errno));
  115. if (setegid(pw->pw_gid))
  116. fatal("can't set effective group id: %.200s", strerror(errno));
  117. if (setuid(pw->pw_uid))
  118. fatal("can't set user id: %.200s", strerror(errno));
  119. if (seteuid(pw->pw_uid))
  120. fatal("can't set effective user id: %.200s", strerror(errno));
  121. }
  122. }
  123. /* alarmhandler that gets called every symon_interval */
  124. void
  125. alarmhandler(int s)
  126. {
  127. /* EMPTY */
  128. }
  129. void
  130. exithandler(int s)
  131. {
  132. info("received signal %d - quitting", s);
  133. exit(1);
  134. }
  135. void
  136. huphandler(int s)
  137. {
  138. info("hup received");
  139. flag_hup = 1;
  140. }
  141. /*
  142. * Symon is a system measurement utility.
  143. *
  144. * The main goals symon hopes to accomplish are:
  145. * - to take fine grained measurements of system parameters
  146. * - with minimal performance impact
  147. * - in a secure way.
  148. *
  149. * Measurements are processed by a second program called symux. symon and symux
  150. * communicate via udp.
  151. */
  152. int
  153. main(int argc, char *argv[])
  154. {
  155. struct muxlist mul, newmul;
  156. struct itimerval alarminterval;
  157. struct stream *stream;
  158. struct mux *mux;
  159. time_t now, last_update;
  160. FILE *pidfile;
  161. char *cfgpath;
  162. int ch;
  163. int i;
  164. SLIST_INIT(&mul);
  165. /* reset flags */
  166. flag_debug = 0;
  167. flag_daemon = 0;
  168. flag_unsecure = 0;
  169. cfgpath = SYMON_CONFIG_FILE;
  170. while ((ch = getopt(argc, argv, "df:tuv")) != -1) {
  171. switch (ch) {
  172. case 'd':
  173. flag_debug = 1;
  174. break;
  175. case 'f':
  176. cfgpath = xstrdup(optarg);
  177. break;
  178. case 't':
  179. flag_testconf = 1;
  180. break;
  181. case 'u':
  182. flag_unsecure = 1;
  183. break;
  184. case 'v':
  185. info("symon version %s", SYMON_VERSION);
  186. default:
  187. info("usage: %s [-d] [-t] [-u] [-v] [-f cfgfile]", __progname);
  188. exit(EX_USAGE);
  189. }
  190. }
  191. if (!read_config_file(&mul, cfgpath))
  192. fatal("configuration file contained errors - aborting");
  193. if (flag_testconf) {
  194. info("%s: ok", cfgpath);
  195. exit(EX_OK);
  196. }
  197. set_stream_use(&mul);
  198. /* open resources that might not be available after privilege drop */
  199. for (i = 0; i < MT_EOT; i++)
  200. if (streamfunc[i].used && (streamfunc[i].privinit != NULL))
  201. (streamfunc[i].privinit) ();
  202. if ((pidfile = fopen(SYMON_PID_FILE, "w")) == NULL)
  203. warning("could not open \"%.200s\", %.200s", SYMON_PID_FILE,
  204. strerror(errno));
  205. if (flag_debug != 1) {
  206. if (daemon(0, 0) != 0)
  207. fatal("daemonize failed: %.200s", strerror(errno));
  208. flag_daemon = 1;
  209. if (pidfile) {
  210. fprintf(pidfile, "%u\n", (u_int) getpid());
  211. fclose(pidfile);
  212. }
  213. }
  214. drop_privileges(flag_unsecure);
  215. info("symon version %s", SYMON_VERSION);
  216. if (flag_debug == 1)
  217. info("program id=%d", (u_int) getpid());
  218. /* setup signal handlers */
  219. signal(SIGALRM, alarmhandler);
  220. signal(SIGHUP, huphandler);
  221. signal(SIGINT, exithandler);
  222. signal(SIGQUIT, exithandler);
  223. signal(SIGTERM, exithandler);
  224. /* prepare crc32 */
  225. init_crc32();
  226. /* init modules */
  227. SLIST_FOREACH(mux, &mul, muxes) {
  228. init_symon_packet(mux);
  229. connect2mux(mux);
  230. SLIST_FOREACH(stream, &mux->sl, streams) {
  231. (streamfunc[stream->type].init) (stream);
  232. }
  233. }
  234. /* setup alarm */
  235. timerclear(&alarminterval.it_interval);
  236. timerclear(&alarminterval.it_value);
  237. alarminterval.it_interval.tv_sec =
  238. alarminterval.it_value.tv_sec = symon_interval;
  239. if (setitimer(ITIMER_REAL, &alarminterval, NULL) != 0) {
  240. fatal("alarm setup failed: %.200s", strerror(errno));
  241. }
  242. last_update = time(NULL);
  243. for (;;) { /* FOREVER */
  244. sleep(symon_interval * 2); /* alarm will interrupt sleep */
  245. now = time(NULL);
  246. if (flag_hup == 1) {
  247. flag_hup = 0;
  248. SLIST_INIT(&newmul);
  249. if (flag_unsecure) {
  250. if (!read_config_file(&newmul, cfgpath)) {
  251. info("new configuration contains errors; keeping old configuration");
  252. free_muxlist(&newmul);
  253. } else {
  254. free_muxlist(&mul);
  255. mul = newmul;
  256. info("read configuration file '%.200s' successfully", cfgpath);
  257. /* init modules */
  258. SLIST_FOREACH(mux, &mul, muxes) {
  259. init_symon_packet(mux);
  260. connect2mux(mux);
  261. SLIST_FOREACH(stream, &mux->sl, streams) {
  262. (streamfunc[stream->type].init) (stream);
  263. }
  264. }
  265. set_stream_use(&mul);
  266. }
  267. } else {
  268. info("configuration unreachable because of privsep; keeping old configuration");
  269. }
  270. } else {
  271. /* check timing to catch ntp drifts */
  272. if (now < last_update ||
  273. now > last_update + symon_interval + symon_interval) {
  274. info("last update seems long ago - assuming system time change");
  275. last_update = now;
  276. } else if (now < last_update + symon_interval) {
  277. debug("did not sleep %d seconds - skipping a measurement", symon_interval);
  278. continue;
  279. }
  280. last_update = now;
  281. /* populate for modules that get all their measurements in one go */
  282. for (i = 0; i < MT_EOT; i++)
  283. if (streamfunc[i].used && (streamfunc[i].gets != NULL))
  284. (streamfunc[i].gets) ();
  285. SLIST_FOREACH(mux, &mul, muxes) {
  286. prepare_packet(mux);
  287. SLIST_FOREACH(stream, &mux->sl, streams)
  288. stream_in_packet(stream, mux);
  289. finish_packet(mux);
  290. send_packet(mux);
  291. }
  292. }
  293. }
  294. return (EX_SOFTWARE); /* NOTREACHED */
  295. }