From d42d01d5bc399c03124626b8537781d0848d29bb Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Fri, 27 Jan 2006 16:31:29 +0000 Subject: Make rrdbotd able to become a daemon. --- daemon/rrdbotd.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) (limited to 'daemon/rrdbotd.c') diff --git a/daemon/rrdbotd.c b/daemon/rrdbotd.c index 63cafc1..7b8a79a 100644 --- a/daemon/rrdbotd.c +++ b/daemon/rrdbotd.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -175,12 +176,33 @@ rb_message (int level, const char* msg, ...) static void usage() { - fprintf(stderr, "usage: rrdcollectd [-c confdir] [-w workdir] [-d level] [-p pidfile] [-r retries] [-t timeout]\n"); - fprintf(stderr, " rrdcollectd -v\n"); + fprintf(stderr, "usage: rrdbotd [-c confdir] [-w workdir] [-d level] [-p pidfile] [-r retries] [-t timeout]\n"); + fprintf(stderr, " rrdbotd -v\n"); exit(2); } -#include +static void +on_quit(int signal) +{ + fprintf(stderr, "rrdbotd: got signal to quit\n"); + server_stop(); +} + +static void +writepid(const char* pidfile) +{ + FILE* f = fopen(pidfile, "w"); + if(f == NULL) + { + rb_message(LOG_WARNING, "couldn't open pid file: %s", pidfile); + return; + } + + fprintf(f, "%d\n", (int)getpid()); + if(ferror(f)) + rb_message(LOG_WARNING, "couldn't write to pid file: %s", pidfile); + fclose(f); +} int main(int argc, char* argv[]) @@ -270,12 +292,37 @@ main(int argc, char* argv[]) rb_snmp_engine_init(); if(daemonize) - warnx("TODO: daemon mode not implemented yet"); + { + /* Fork a daemon nicely */ + if(daemon(0, 0) == -1) + err("couldn't fork as a daemon"); + + rb_messagex(LOG_DEBUG, "running as a daemon"); + daemonized = 1; + } + + /* Handle signals */ + signal(SIGPIPE, SIG_IGN); + signal(SIGHUP, SIG_IGN); + signal(SIGINT, on_quit); + signal(SIGTERM, on_quit); + siginterrupt(SIGINT, 1); + siginterrupt(SIGTERM, 1); + + /* Open the system log */ + openlog("rrdbotd", 0, LOG_DAEMON); + + if(pidfile != NULL) + writepid(pidfile); + + rb_messagex(LOG_INFO, "rrdbotd version " VERSION " started up"); /* Now let it go */ if(server_run() == -1) err(1, "critical failure running SNMP engine"); + rb_messagex(LOG_INFO, "rrdbotd stopping"); + /* Cleanups */ rb_snmp_engine_uninit(); rb_config_free(); -- cgit v1.2.3