]> git.ozlabs.org Git - ccan/blob - ccan/daemon-with-notify/_info
fix up daemon-with-notify test
[ccan] / ccan / daemon-with-notify / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * daemon-with-notify - turn a process into a daemon with parent exiting when
7  *                      child has decided that it has started correctly.
8  *
9  * Daemons should detach themselves thoroughly from the process which launched
10  * them, and not prevent any filesystems from being unmounted.  daemonize()
11  * helps with the process.
12  *
13  * Daemon-with-notify is different in that the child can send a SIGUSR1 to
14  * the parent to indicate it has started (e.g. after memory allocation and
15  * other things that may fail) so that the parent can return a success or
16  * failing exit code and init scripts can pick this up easily.
17  *
18  * Example:
19  *      #include <ccan/daemon-with-notify/daemon-with-notify.h>
20  *      #include <ccan/str/str.h>
21  *      #include <err.h>
22  *      #include <unistd.h>
23  *      #include <stdlib.h>
24  *      
25  *      static void usage(const char *name)
26  *      {
27  *              errx(1, "Usage: %s [--daemonize]\n", name);
28  *      }
29  *      
30  *      // Wait for a minute, possibly as a daemon.
31  *      int main(int argc, char *argv[])
32  *      {
33  *              if (argc != 1) {
34  *                      if (argc == 2 && streq(argv[1], "--daemonize")) {
35  *                              if (!daemonize(1, 1, 1))
36  *                                      err(1, "Failed to become daemon");
37  *                      } else
38  *                              usage(argv[1]);
39  *              }
40  *              sleep(10); // do some init here
41  *              daemon_is_ready();
42  *              sleep(20); // will be done in child, detached from parent
43  *              exit(0);
44  *      }
45  *
46  * License: BSD-MIT
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 }