symon.c 10 KB

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