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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include "config.h"
#include <stdio.h>
#include <string.h>
/**
* daemon_with_notify - daemonize a process, can wait for child to signal readiness
*
* Daemons should detach themselves thoroughly from the process which launched
* them, and not prevent any filesystems from being unmounted. daemonize()
* helps with the process.
*
* Daemon-with-notify is different in that the child can send a SIGUSR1 to
* the parent to indicate it has started (e.g. after memory allocation and
* other things that may fail) so that the parent can return a success or
* failing exit code and init scripts can pick this up easily.
*
* Example:
* #include <ccan/daemon_with_notify/daemon_with_notify.h>
* #include <ccan/str/str.h>
* #include <err.h>
* #include <unistd.h>
* #include <stdlib.h>
*
* static void usage(const char *name)
* {
* errx(1, "Usage: %s [--daemonize]\n", name);
* }
*
* // Wait for a minute, possibly as a daemon.
* int main(int argc, char *argv[])
* {
* if (argc != 1) {
* if (argc == 2 && streq(argv[1], "--daemonize")) {
* if (!daemonize(1, 1, 1))
* err(1, "Failed to become daemon");
* } else
* usage(argv[1]);
* }
* sleep(10); // do some init here
* daemon_is_ready();
* sleep(20); // will be done in child, detached from parent
* exit(0);
* }
*
* License: BSD (3 clause)
* Author: Stewart Smith <stewart@flamingspork.com>
*/
int main(int argc, char *argv[])
{
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
return 0;
}
if (strcmp(argv[1], "libs") == 0) {
return 0;
}
return 1;
}
|