diff options
-rw-r--r-- | SConstruct | 2 | ||||
-rw-r--r-- | daemon.c | 28 | ||||
-rw-r--r-- | daemon.h | 1 | ||||
-rw-r--r-- | main.c | 5 | ||||
-rw-r--r-- | options.c | 1 | ||||
-rw-r--r-- | options.h | 1 |
6 files changed, 33 insertions, 5 deletions
@@ -6,7 +6,7 @@ conf = Configure(env) conf.CheckLib('mp3lame') env = conf.Finish() -env.Append(CCFLAGS = ['-std=c99']) +env.Append(CCFLAGS = ['-std=gnu99']) if GetOption('release'): env.Append(CCFLAGS = ['-O2']) @@ -5,21 +5,23 @@ #include <stdlib.h> #include <stdio.h> #include <unistd.h> +#include <sys/types.h> +#include <signal.h> void daemonize() { gchar *pidfile = conf_get_string("audist", "pidfile"); if(pidfile == NULL) { - g_warning("pidfile not set - can't daemonize"); - return; + g_error("pidfile not set - can't daemonize"); } FILE *f = fopen(pidfile, "w"); if(f == NULL) { - g_warning("pidfile \"%s\" not writeable - can't daemonize", pidfile); - return; + g_error("pidfile \"%s\" not writeable - can't daemonize", pidfile); } + g_free(pidfile); + switch(fork()) { case 0: break; @@ -31,8 +33,26 @@ void daemonize() { fprintf(f, "%d\n", getpid()); fclose(f); +} + +void daemon_kill() { + gchar *pidfile = conf_get_string("audist", "pidfile"); + if(pidfile == NULL) { + g_error("pidfile not set"); + } + + FILE *f = fopen(pidfile, "r"); g_free(pidfile); + if(f == NULL) { + g_error("can't read pidfile"); + } + + int pid; + fscanf(f, "%d", &pid); + fclose(f); + + kill(pid, SIGTERM); } void daemonize_finished() { @@ -2,6 +2,7 @@ #define DAEMON_H void daemonize(); +void daemon_kill(); void daemonize_finished(); #endif @@ -27,6 +27,11 @@ int main(int argc, char **argv) { options_parse(argc, argv, &options); + if(options.kill == TRUE) { + daemon_kill(); + return 0; + } + if(options.daemonize == TRUE) { daemonize(); } @@ -10,6 +10,7 @@ void options_parse(int argc, char **argv, struct options *options) { GOptionContext *context; GOptionEntry entries[] = { {"daemonize", 'd', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_NONE, &options->daemonize, NULL, NULL}, + {"kill", 'k', G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_NONE, &options->kill, NULL, NULL}, {NULL}, }; @@ -5,6 +5,7 @@ struct options { gboolean daemonize; + gboolean kill; }; void options_parse(int argc, char **argv, struct options *options); |