symon.c 9.6 KB

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