summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--SConstruct2
-rw-r--r--daemon.c28
-rw-r--r--daemon.h1
-rw-r--r--main.c5
-rw-r--r--options.c1
-rw-r--r--options.h1
6 files changed, 33 insertions, 5 deletions
diff --git a/SConstruct b/SConstruct
index 31e4e5a..bed3520 100644
--- a/SConstruct
+++ b/SConstruct
@@ -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'])
diff --git a/daemon.c b/daemon.c
index 0de94fa..3cccedd 100644
--- a/daemon.c
+++ b/daemon.c
@@ -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() {
diff --git a/daemon.h b/daemon.h
index dfb7b5b..db706dc 100644
--- a/daemon.h
+++ b/daemon.h
@@ -2,6 +2,7 @@
#define DAEMON_H
void daemonize();
+void daemon_kill();
void daemonize_finished();
#endif
diff --git a/main.c b/main.c
index eb22061..20c96ff 100644
--- a/main.c
+++ b/main.c
@@ -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();
}
diff --git a/options.c b/options.c
index ee237aa..2c8daa4 100644
--- a/options.c
+++ b/options.c
@@ -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},
};
diff --git a/options.h b/options.h
index 51249c1..ce2de37 100644
--- a/options.h
+++ b/options.h
@@ -5,6 +5,7 @@
struct options {
gboolean daemonize;
+ gboolean kill;
};
void options_parse(int argc, char **argv, struct options *options);