symon.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (c) 2001-2010 Willem Dijkstra
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following
  13. * disclaimer in the documentation and/or other materials provided
  14. * with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  19. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  20. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  22. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. #include <sys/param.h>
  31. #include <sys/time.h>
  32. #include <errno.h>
  33. #include <fcntl.h>
  34. #include <limits.h>
  35. #include <pwd.h>
  36. #include <signal.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <sysexits.h>
  41. #include <syslog.h>
  42. #include <time.h>
  43. #include <unistd.h>
  44. #include "conf.h"
  45. #include "data.h"
  46. #include "error.h"
  47. #include "net.h"
  48. #include "readconf.h"
  49. #include "symon.h"
  50. #include "symonnet.h"
  51. #include "xmalloc.h"
  52. __BEGIN_DECLS
  53. void alarmhandler(int);
  54. void drop_privileges();
  55. void exithandler(int);
  56. void huphandler(int);
  57. void init_streams(struct muxlist *mul);
  58. void drop_privileges(int unsecure);
  59. __END_DECLS
  60. int flag_unsecure = 0;
  61. int flag_hup = 0;
  62. int flag_testconf = 0;
  63. int symon_interval = 0;
  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_SMART, 0, NULL, init_smart, gets_smart, get_smart},
  82. {MT_LOAD, 0, NULL, init_load, gets_load, get_load},
  83. {MT_EOT, 0, NULL, NULL, NULL}
  84. };
  85. void
  86. init_streams(struct muxlist *mul)
  87. {
  88. struct itimerval alarminterval;
  89. struct stream *stream;
  90. struct mux *mux;
  91. mux = NULL;
  92. if ((mul == NULL) || ((mux = SLIST_FIRST(mul)) == NULL))
  93. fatal("empty mux list");
  94. symon_interval = mux->interval;
  95. SLIST_FOREACH(mux, mul, muxes) {
  96. /* determine gcd of alarm times */
  97. symon_interval = gcd(symon_interval, mux->interval);
  98. /* init network */
  99. init_symon_packet(mux);
  100. connect2mux(mux);
  101. /* init modules */
  102. SLIST_FOREACH(stream, &mux->sl, streams) {
  103. (streamfunc[stream->type].init) (stream);
  104. }
  105. }
  106. /* setup alarm */
  107. timerclear(&alarminterval.it_interval);
  108. timerclear(&alarminterval.it_value);
  109. alarminterval.it_interval.tv_sec =
  110. alarminterval.it_value.tv_sec = symon_interval;
  111. if (setitimer(ITIMER_REAL, &alarminterval, NULL) != 0) {
  112. fatal("alarm setup failed: %.200s", strerror(errno));
  113. }
  114. }
  115. void
  116. drop_privileges(int unsecure)
  117. {
  118. struct passwd *pw;
  119. if (unsecure) {
  120. if (setegid(getgid()) || setgid(getgid()) ||
  121. seteuid(getuid()) || setuid(getuid()))
  122. fatal("can't drop privileges: %.200s", strerror(errno));
  123. } else {
  124. if ((pw = getpwnam(SYMON_USER)) == NULL)
  125. fatal("could not get user information for user '%.200s': %.200s",
  126. SYMON_USER, strerror(errno));
  127. if (chroot(pw->pw_dir) < 0)
  128. fatal("chroot failed: %.200s", strerror(errno));
  129. if (chdir("/") < 0)
  130. fatal("chdir / failed: %.200s", strerror(errno));
  131. if (setgroups(1, &pw->pw_gid))
  132. fatal("can't setgroups: %.200s", strerror(errno));
  133. if (setgid(pw->pw_gid))
  134. fatal("can't set group id: %.200s", strerror(errno));
  135. if (setegid(pw->pw_gid))
  136. fatal("can't set effective group id: %.200s", strerror(errno));
  137. if (setuid(pw->pw_uid))
  138. fatal("can't set user id: %.200s", strerror(errno));
  139. if (seteuid(pw->pw_uid))
  140. fatal("can't set effective user id: %.200s", strerror(errno));
  141. }
  142. }
  143. /* alarmhandler that gets called every symon_interval */
  144. void
  145. alarmhandler(int s)
  146. {
  147. /* EMPTY */
  148. }
  149. void
  150. exithandler(int s)
  151. {
  152. info("received signal %d - quitting", s);
  153. exit(1);
  154. }
  155. void
  156. huphandler(int s)
  157. {
  158. info("hup received");
  159. flag_hup = 1;
  160. }
  161. /*
  162. * Symon is a system measurement utility.
  163. *
  164. * The main goals symon hopes to accomplish are:
  165. * - to take fine grained measurements of system parameters
  166. * - with minimal performance impact
  167. * - in a secure way.
  168. *
  169. * Measurements are processed by a second program called symux. symon and symux
  170. * communicate via udp.
  171. */
  172. int
  173. main(int argc, char *argv[])
  174. {
  175. struct muxlist mul, newmul;
  176. struct stream *stream;
  177. struct mux *mux;
  178. time_t now, last_update;
  179. FILE *pidfile;
  180. char *cfgpath;
  181. int ch;
  182. int i;
  183. SLIST_INIT(&mul);
  184. /* reset flags */
  185. flag_debug = 0;
  186. flag_daemon = 0;
  187. flag_unsecure = 0;
  188. cfgpath = SYMON_CONFIG_FILE;
  189. while ((ch = getopt(argc, argv, "df:tuv")) != -1) {
  190. switch (ch) {
  191. case 'd':
  192. flag_debug = 1;
  193. break;
  194. case 'f':
  195. cfgpath = xstrdup(optarg);
  196. break;
  197. case 't':
  198. flag_testconf = 1;
  199. break;
  200. case 'u':
  201. flag_unsecure = 1;
  202. break;
  203. case 'v':
  204. info("symon version %s", SYMON_VERSION);
  205. default:
  206. info("usage: %s [-d] [-t] [-u] [-v] [-f cfgfile]", __progname);
  207. exit(EX_USAGE);
  208. }
  209. }
  210. if (!read_config_file(&mul, cfgpath))
  211. fatal("configuration file contained errors - aborting");
  212. if (flag_testconf) {
  213. info("%s: ok", cfgpath);
  214. exit(EX_OK);
  215. }
  216. SLIST_FOREACH(mux, &mul, muxes) {
  217. SLIST_FOREACH(stream, &mux->sl, streams) {
  218. streamfunc[stream->type].used = 1;
  219. }
  220. }
  221. /* open resources that might not be available after privilege drop */
  222. for (i = 0; i < MT_EOT; i++)
  223. if (streamfunc[i].used && (streamfunc[i].privinit != NULL))
  224. (streamfunc[i].privinit) ();
  225. if ((pidfile = fopen(SYMON_PID_FILE, "w")) == NULL)
  226. warning("could not open \"%.200s\", %.200s", SYMON_PID_FILE,
  227. strerror(errno));
  228. if (flag_debug != 1) {
  229. if (daemon(0, 0) != 0)
  230. fatal("daemonize failed: %.200s", strerror(errno));
  231. flag_daemon = 1;
  232. if (pidfile) {
  233. fprintf(pidfile, "%u\n", (u_int) getpid());
  234. fclose(pidfile);
  235. }
  236. }
  237. drop_privileges(flag_unsecure);
  238. /* TODO: howto log using active timezone, which may be unavailable in
  239. * chroot */
  240. info("symon version %s", SYMON_VERSION);
  241. if (flag_debug == 1)
  242. info("program id=%d", (u_int) getpid());
  243. /* setup signal handlers */
  244. signal(SIGALRM, alarmhandler);
  245. signal(SIGHUP, huphandler);
  246. signal(SIGINT, exithandler);
  247. signal(SIGQUIT, exithandler);
  248. signal(SIGTERM, exithandler);
  249. /* prepare crc32 */
  250. init_crc32();
  251. init_streams(&mul);
  252. last_update = time(NULL);
  253. for (;;) { /* FOREVER */
  254. sleep(symon_interval * 2); /* alarm will interrupt sleep */
  255. now = time(NULL);
  256. if (flag_hup == 1) {
  257. flag_hup = 0;
  258. SLIST_INIT(&newmul);
  259. if (flag_unsecure) {
  260. if (!read_config_file(&newmul, cfgpath)) {
  261. info("new configuration contains errors; keeping old configuration");
  262. free_muxlist(&newmul);
  263. } else {
  264. free_muxlist(&mul);
  265. mul = newmul;
  266. info("read configuration file '%.200s' successfully", cfgpath);
  267. /* init modules */
  268. init_streams(&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
  285. * go. we bunch up calls together to ensure that the measurements
  286. * happen "at the same time" */
  287. for (i = 0; i < MT_EOT; i++)
  288. streamfunc[i].used = 0;
  289. SLIST_FOREACH(mux, &mul, muxes) {
  290. mux->last += symon_interval;
  291. if (mux->last >= mux->interval) {
  292. SLIST_FOREACH(stream, &mux->sl, streams) {
  293. if (streamfunc[stream->type].used == 0) {
  294. streamfunc[stream->type].used = 1;
  295. if (streamfunc[stream->type].gets != NULL)
  296. (streamfunc[stream->type].gets)();
  297. }
  298. }
  299. }
  300. }
  301. SLIST_FOREACH(mux, &mul, muxes) {
  302. if (mux->last >= mux->interval) {
  303. mux->last = 0;
  304. prepare_packet(mux, now);
  305. SLIST_FOREACH(stream, &mux->sl, streams)
  306. stream_in_packet(stream, mux);
  307. finish_packet(mux);
  308. send_packet(mux);
  309. }
  310. }
  311. }
  312. }
  313. return (EX_SOFTWARE); /* NOTREACHED */
  314. }