diff options
Diffstat (limited to 'daemon.c')
-rw-r--r-- | daemon.c | 48 |
1 files changed, 48 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); +} |