blob: 0de94fa0448099f62a551c8282b02b528b87601d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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);
}
|