From 671966ae7bb69fc04952a8accaa52fae5e77d8bb Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 4 Sep 2010 01:20:33 +0200 Subject: Added command-line options and daemonizer. --- daemon.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ daemon.h | 7 +++++++ main.c | 14 ++++++++++++++ options.c | 24 ++++++++++++++++++++++++ options.h | 12 ++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 daemon.c create mode 100644 daemon.h create mode 100644 options.c create mode 100644 options.h diff --git a/daemon.c b/daemon.c new file mode 100644 index 0000000..0de94fa --- /dev/null +++ b/daemon.c @@ -0,0 +1,48 @@ +#include "conf.h" + +#include + +#include +#include +#include + +void daemonize() { + gchar *pidfile = conf_get_string("audist", "pidfile"); + + if(pidfile == NULL) { + g_warning("pidfile not set - can't daemonize"); + return; + } + + FILE *f = fopen(pidfile, "w"); + if(f == NULL) { + g_warning("pidfile \"%s\" not writeable - can't daemonize", pidfile); + return; + } + + switch(fork()) { + case 0: + break; + case -1: + g_error("fork() failed"); + default: + _exit(0); + } + + fprintf(f, "%d\n", getpid()); + fclose(f); + + g_free(pidfile); +} + +void daemonize_finished() { + gchar *pidfile = conf_get_string("audist", "pidfile"); + + if(pidfile == NULL) { + g_warning("pidfile not set"); + return; + } + + g_unlink(pidfile); + g_free(pidfile); +} diff --git a/daemon.h b/daemon.h new file mode 100644 index 0000000..dfb7b5b --- /dev/null +++ b/daemon.h @@ -0,0 +1,7 @@ +#ifndef DAEMON_H +#define DAEMON_H + +void daemonize(); +void daemonize_finished(); + +#endif diff --git a/main.c b/main.c index 07eb66e..eb22061 100644 --- a/main.c +++ b/main.c @@ -4,6 +4,8 @@ #include "conf.h" #include "servers.h" #include "control_service.h" +#include "options.h" +#include "daemon.h" #include #include @@ -11,6 +13,7 @@ #include GMainLoop *main_loop; +struct options options = {0}; static void sig_handler(int sig) { g_debug("caught signal %d", sig); @@ -22,6 +25,12 @@ int main(int argc, char **argv) { conf_load(); + options_parse(argc, argv, &options); + + if(options.daemonize == TRUE) { + daemonize(); + } + control_service_start(); servers_init(); @@ -33,6 +42,7 @@ int main(int argc, char **argv) { httpd_start(); signal(SIGINT, sig_handler); + signal(SIGTERM, sig_handler); main_loop = g_main_loop_new(NULL, FALSE); @@ -47,6 +57,10 @@ int main(int argc, char **argv) { control_service_stop(); + if(options.daemonize == TRUE) { + daemonize_finished(); + } + conf_free(); return 0; diff --git a/options.c b/options.c new file mode 100644 index 0000000..ee237aa --- /dev/null +++ b/options.c @@ -0,0 +1,24 @@ +#include "options.h" +#include "conf.h" + +#include + +#include + +void options_parse(int argc, char **argv, struct options *options) { + GError *error = NULL; + GOptionContext *context; + GOptionEntry entries[] = { + {"daemonize", 'd', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_NONE, &options->daemonize, NULL, NULL}, + {NULL}, + }; + + context = g_option_context_new(NULL); + g_option_context_set_summary(context, "audist daemon"); + g_option_context_add_main_entries(context, entries, NULL); + if(g_option_context_parse(context, &argc, &argv, &error) == FALSE) { + g_warning(error->message); + g_error_free(error); + exit(1); + } +} diff --git a/options.h b/options.h new file mode 100644 index 0000000..51249c1 --- /dev/null +++ b/options.h @@ -0,0 +1,12 @@ +#ifndef OPTIONS_H +#define OPTIONS_H + +#include + +struct options { + gboolean daemonize; +}; + +void options_parse(int argc, char **argv, struct options *options); + +#endif -- cgit v1.2.3