123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- #include <sys/param.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <limits.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <syslog.h>
- #include <unistd.h>
- #include "data.h"
- #include "error.h"
- #include "mon.h"
- #include "monnet.h"
- #include "net.h"
- #include "readconf.h"
- #include "xmalloc.h"
- __BEGIN_DECLS
- void alarmhandler(int);
- void exithandler(int);
- void huphandler(int);
- __END_DECLS
- int flag_hup = 0;
- #ifdef MON_KVM
- kvm_t *kvmd;
- struct nlist mon_nl[] = {
- {"_ifnet"},
- {"_disklist"},
- {""},
- };
- int
- kread(u_long addr, char *buf, int size)
- {
- if (kvm_read(kvmd, addr, buf, size) != size) {
- warning("kvm_read:%s", kvm_geterr(kvmd));
- return 1;
- }
- return (0);
- }
- #endif
- struct funcmap streamfunc[] = {
- {MT_IO, init_io, get_io},
- {MT_CPU, init_cpu, get_cpu},
- {MT_MEM, init_mem, get_mem},
- {MT_IF, init_if, get_if},
- {MT_EOT, NULL, NULL}
- };
- void
- alarmhandler(int s) {
-
- }
- void
- exithandler(int s) {
- info("received signal %d - quitting", s);
- exit(1);
- }
- void
- huphandler(int s) {
- info("hup received");
- flag_hup = 1;
- }
- int
- main(int argc, char *argv[])
- {
- struct muxlist mul, newmul;
- struct itimerval alarminterval;
- struct stream *stream;
- struct mux *mux;
- char mon_buf[_POSIX2_LINE_MAX];
- FILE *f;
- #ifdef MON_KVM
- char *nlistf = NULL, *memf = NULL;
- #endif
- int ch;
- SLIST_INIT(&mul);
-
- flag_debug = 0;
- flag_daemon = 0;
- while ((ch = getopt(argc, argv, "dv")) != -1) {
- switch (ch) {
- case 'd':
- flag_debug = 1;
- break;
- case 'v':
- info("mon version %s", MON_VERSION);
- default:
- info("usage: %s [-d] [-v]", __progname);
- exit(1);
- }
- }
- #ifdef MON_KVM
-
- if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, mon_buf)) == NULL)
- fatal("kvm_open: %s", mon_buf);
-
- if (kvm_nlist(kvmd, mon_nl) < 0 || mon_nl[0].n_type == 0) {
- if (nlistf)
- fatal("%s: no namelist", nlistf);
- else
- fatal("no namelist");
- }
- #endif
- setegid(getgid());
- setgid(getgid());
- if (flag_debug != 1) {
- if (daemon(0,0) != 0)
- fatal("daemonize failed");
-
- flag_daemon = 1;
-
- f = fopen(MON_PID_FILE, "w");
- if (f) {
- fprintf(f, "%u\n", (u_int) getpid());
- fclose(f);
- }
- }
- info("mon version %s", MON_VERSION);
- if (!read_config_file(&mul, MON_CONFIG_FILE))
- fatal("configuration contained errors; quitting");
- if (flag_debug == 1)
- info("program id=%d", (u_int) getpid());
-
- signal(SIGALRM, alarmhandler);
- signal(SIGHUP, huphandler);
- signal(SIGINT, exithandler);
- signal(SIGQUIT, exithandler);
- signal(SIGTERM, exithandler);
-
- init_crc32();
-
- SLIST_FOREACH(mux, &mul, muxes) {
- connect2mux(mux);
- SLIST_FOREACH(stream, &mux->sl, streams) {
- (streamfunc[stream->type].init)(stream->args);
- }
- }
-
- timerclear(&alarminterval.it_interval);
- timerclear(&alarminterval.it_value);
- alarminterval.it_interval.tv_sec=
- alarminterval.it_value.tv_sec=MON_INTERVAL;
- if (setitimer(ITIMER_REAL, &alarminterval, NULL) != 0) {
- fatal("alarm setup failed -- %s", strerror(errno));
- }
- for (;;) {
- sleep(MON_INTERVAL*2);
- if (flag_hup == 1) {
- flag_hup = 0;
- SLIST_INIT(&newmul);
- if (!read_config_file(&newmul, MON_CONFIG_FILE)) {
- info("new configuration contains errors; keeping old configuration");
- free_muxlist(&newmul);
- } else {
- free_muxlist(&mul);
- mul = newmul;
- info("read configuration file succesfully");
-
- SLIST_FOREACH(mux, &mul, muxes) {
- connect2mux(mux);
- SLIST_FOREACH(stream, &mux->sl, streams) {
- (streamfunc[stream->type].init)(stream->args);
- }
- }
- }
- }
- SLIST_FOREACH(mux, &mul, muxes) {
- prepare_packet(mux);
-
- SLIST_FOREACH(stream, &mux->sl, streams)
- stream_in_packet(stream, mux);
- finish_packet(mux);
- send_packet(mux);
- }
- }
-
- }
|