summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--daemon.c48
-rw-r--r--daemon.h7
-rw-r--r--main.c14
-rw-r--r--options.c24
-rw-r--r--options.h12
5 files changed, 105 insertions, 0 deletions
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 <glib/gstdio.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+
+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 <glib.h>
#include <glib-object.h>
@@ -11,6 +13,7 @@
#include <signal.h>
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 <glib.h>
+
+#include <stdlib.h>
+
+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 <glib.h>
+
+struct options {
+ gboolean daemonize;
+};
+
+void options_parse(int argc, char **argv, struct options *options);
+
+#endif