]> git.ozlabs.org Git - ccan/blob - ccan/daemon_with_notify/_info
move daemon-with-notify to daemon_with_notify as dashes aren't allowed in CCAN module...
[ccan] / ccan / daemon_with_notify / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * daemon_with_notify - daemonize a process, can wait for child to signal readiness
7  *
8  * Daemons should detach themselves thoroughly from the process which launched
9  * them, and not prevent any filesystems from being unmounted.  daemonize()
10  * helps with the process.
11  *
12  * Daemon-with-notify is different in that the child can send a SIGUSR1 to
13  * the parent to indicate it has started (e.g. after memory allocation and
14  * other things that may fail) so that the parent can return a success or
15  * failing exit code and init scripts can pick this up easily.
16  *
17  * Example:
18  *      #include <ccan/daemon_with_notify/daemon_with_notify.h>
19  *      #include <ccan/str/str.h>
20  *      #include <err.h>
21  *      #include <unistd.h>
22  *      #include <stdlib.h>
23  *      
24  *      static void usage(const char *name)
25  *      {
26  *              errx(1, "Usage: %s [--daemonize]\n", name);
27  *      }
28  *      
29  *      // Wait for a minute, possibly as a daemon.
30  *      int main(int argc, char *argv[])
31  *      {
32  *              if (argc != 1) {
33  *                      if (argc == 2 && streq(argv[1], "--daemonize")) {
34  *                              if (!daemonize(1, 1, 1))
35  *                                      err(1, "Failed to become daemon");
36  *                      } else
37  *                              usage(argv[1]);
38  *              }
39  *              sleep(10); // do some init here
40  *              daemon_is_ready();
41  *              sleep(20); // will be done in child, detached from parent
42  *              exit(0);
43  *      }
44  *
45  * License: BSD
46  * Author: Stewart Smith <stewart@flamingspork.com>
47  */
48 int main(int argc, char *argv[])
49 {
50         if (argc != 2)
51                 return 1;
52
53         if (strcmp(argv[1], "depends") == 0) {
54                 return 0;
55         }
56
57         if (strcmp(argv[1], "libs") == 0) {
58                 return 0;
59         }
60
61         return 1;
62 }
63