symon.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* $Id: symon.c,v 1.31 2003/12/20 16:30:44 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2003 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 <errno.h>
  33. #include <fcntl.h>
  34. #include <limits.h>
  35. #include <pwd.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <sysexits.h>
  40. #include <syslog.h>
  41. #include <unistd.h>
  42. #include "data.h"
  43. #include "error.h"
  44. #include "symon.h"
  45. #include "symonnet.h"
  46. #include "net.h"
  47. #include "readconf.h"
  48. #include "xmalloc.h"
  49. __BEGIN_DECLS
  50. void alarmhandler(int);
  51. void drop_priviledges();
  52. void exithandler(int);
  53. void huphandler(int);
  54. void set_stream_use(struct muxlist *);
  55. __END_DECLS
  56. int flag_unsecure = 0;
  57. int flag_hup = 0;
  58. int symon_interval = SYMON_DEFAULT_INTERVAL;
  59. /* map stream types to inits and getters */
  60. struct funcmap streamfunc[] = {
  61. {MT_IO, 0, NULL, init_io, gets_io, get_io},
  62. {MT_CPU, 0, NULL, init_cpu, NULL, get_cpu},
  63. {MT_MEM, 0, NULL, init_mem, NULL, get_mem},
  64. {MT_IF, 0, NULL, init_if, NULL, get_if},
  65. {MT_PF, 0, privinit_pf, init_pf, NULL, get_pf},
  66. {MT_DEBUG, 0, NULL, init_debug, NULL, get_debug},
  67. {MT_PROC, 0, NULL, init_proc, gets_proc, get_proc},
  68. {MT_MBUF, 0, NULL, init_mbuf, NULL, get_mbuf},
  69. {MT_SENSOR, 0, NULL, init_sensor, NULL, get_sensor},
  70. {MT_EOT, 0, NULL, NULL, NULL}
  71. };
  72. void
  73. set_stream_use(struct muxlist *mul)
  74. {
  75. struct mux *mux;
  76. struct stream *stream;
  77. int i;
  78. for (i = 0; i < MT_EOT; i++)
  79. streamfunc[i].used = 0;
  80. SLIST_FOREACH(mux, mul, muxes) {
  81. SLIST_FOREACH(stream, &mux->sl, streams)
  82. streamfunc[stream->type].used = 1;
  83. }
  84. }
  85. void
  86. drop_priviledges(int unsecure)
  87. {
  88. struct passwd *pw;
  89. if (unsecure) {
  90. if (setegid(getgid()) || setgid(getgid()) ||
  91. seteuid(getuid()) || setuid(getuid()))
  92. fatal("can't drop priviledges: %.200s", strerror(errno));
  93. } else {
  94. if ((pw = getpwnam(SYMON_USER)) == NULL)
  95. fatal("could not get user information for user '%.200s': %.200s",
  96. SYMON_USER, strerror(errno));
  97. if (chroot(pw->pw_dir) < 0)
  98. fatal("chroot failed: %.200s", strerror(errno));
  99. if (chdir("/") < 0)
  100. fatal("chdir / failed: %.200s", strerror(errno));
  101. if (setgroups(1, &pw->pw_gid) ||
  102. setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
  103. seteuid(pw->pw_uid) || setuid(pw->pw_uid))
  104. fatal("can't drop privileges: %.200s", strerror(errno));
  105. }
  106. }
  107. /* alarmhandler that gets called every symon_interval */
  108. void
  109. alarmhandler(int s)
  110. {
  111. /* EMPTY */
  112. }
  113. void
  114. exithandler(int s)
  115. {
  116. info("received signal %d - quitting", s);
  117. exit(1);
  118. }
  119. void
  120. huphandler(int s)
  121. {
  122. info("hup received");
  123. flag_hup = 1;
  124. }
  125. /*
  126. * Symon is a system measurement utility.
  127. *
  128. * The main goals symon hopes to accomplish are:
  129. * - to take fine grained measurements of system parameters
  130. * - with minimal performance impact
  131. * - in a secure way.
  132. *
  133. * Measurements are processed by a second program called symux. symon and symux
  134. * communicate via udp.
  135. */
  136. int
  137. main(int argc, char *argv[])
  138. {
  139. struct muxlist mul, newmul;
  140. struct itimerval alarminterval;
  141. struct stream *stream;
  142. struct mux *mux;
  143. FILE *pidfile;
  144. char *cfgpath;
  145. int ch;
  146. int i;
  147. SLIST_INIT(&mul);
  148. /* reset flags */
  149. flag_debug = 0;
  150. flag_daemon = 0;
  151. flag_unsecure = 0;
  152. cfgpath = SYMON_CONFIG_FILE;
  153. while ((ch = getopt(argc, argv, "dvuf:")) != -1) {
  154. switch (ch) {
  155. case 'd':
  156. flag_debug = 1;
  157. break;
  158. case 'f':
  159. cfgpath = xstrdup(optarg);
  160. break;
  161. case 'u':
  162. flag_unsecure = 1;
  163. break;
  164. case 'v':
  165. info("symon version %s", SYMON_VERSION);
  166. default:
  167. info("usage: %s [-d] [-u] [-v] [-f cfgfile]", __progname);
  168. exit(EX_USAGE);
  169. }
  170. }
  171. if (!read_config_file(&mul, cfgpath))
  172. fatal("configuration file contained errors - aborting");
  173. set_stream_use(&mul);
  174. /* open resources that might not be available after priviledge drop */
  175. for (i = 0; i < MT_EOT; i++)
  176. if (streamfunc[i].used && (streamfunc[i].privinit != NULL))
  177. (streamfunc[i].privinit) ();
  178. if ((pidfile = fopen(SYMON_PID_FILE, "w")) == NULL)
  179. warning("could not open \"%.200s\", %.200s", SYMON_PID_FILE,
  180. strerror(errno));
  181. drop_priviledges(flag_unsecure);
  182. if (flag_debug != 1) {
  183. if (daemon(0, 0) != 0)
  184. fatal("daemonize failed: %.200s", strerror(errno));
  185. flag_daemon = 1;
  186. if (pidfile) {
  187. fprintf(pidfile, "%u\n", (u_int) getpid());
  188. fclose(pidfile);
  189. }
  190. }
  191. info("symon version %s", SYMON_VERSION);
  192. if (flag_debug == 1)
  193. info("program id=%d", (u_int) getpid());
  194. /* setup signal handlers */
  195. signal(SIGALRM, alarmhandler);
  196. signal(SIGHUP, huphandler);
  197. signal(SIGINT, exithandler);
  198. signal(SIGQUIT, exithandler);
  199. signal(SIGTERM, exithandler);
  200. /* prepare crc32 */
  201. init_crc32();
  202. /* init modules */
  203. SLIST_FOREACH(mux, &mul, muxes) {
  204. connect2mux(mux);
  205. SLIST_FOREACH(stream, &mux->sl, streams) {
  206. (streamfunc[stream->type].init) (stream->args);
  207. }
  208. }
  209. set_stream_use(&mul);
  210. /* setup alarm */
  211. timerclear(&alarminterval.it_interval);
  212. timerclear(&alarminterval.it_value);
  213. alarminterval.it_interval.tv_sec =
  214. alarminterval.it_value.tv_sec = symon_interval;
  215. if (setitimer(ITIMER_REAL, &alarminterval, NULL) != 0) {
  216. fatal("alarm setup failed: %.200s", strerror(errno));
  217. }
  218. for (;;) { /* FOREVER */
  219. sleep(symon_interval); /* alarm will interrupt sleep */
  220. if (flag_hup == 1) {
  221. flag_hup = 0;
  222. SLIST_INIT(&newmul);
  223. if (!read_config_file(&newmul, cfgpath)) {
  224. info("new configuration contains errors; keeping old configuration");
  225. free_muxlist(&newmul);
  226. } else {
  227. free_muxlist(&mul);
  228. mul = newmul;
  229. info("read configuration file '%.200s' succesfully", cfgpath);
  230. /* init modules */
  231. SLIST_FOREACH(mux, &mul, muxes) {
  232. connect2mux(mux);
  233. SLIST_FOREACH(stream, &mux->sl, streams) {
  234. (streamfunc[stream->type].init) (stream->args);
  235. }
  236. }
  237. set_stream_use(&mul);
  238. }
  239. } else {
  240. /* populate for modules that get all their measurements in one go */
  241. for (i = 0; i < MT_EOT; i++)
  242. if (streamfunc[i].used && (streamfunc[i].gets != NULL))
  243. (streamfunc[i].gets) ();
  244. SLIST_FOREACH(mux, &mul, muxes) {
  245. prepare_packet(mux);
  246. SLIST_FOREACH(stream, &mux->sl, streams)
  247. stream_in_packet(stream, mux);
  248. finish_packet(mux);
  249. send_packet(mux);
  250. }
  251. }
  252. }
  253. return (EX_SOFTWARE); /* NOTREACHED */
  254. }