From 75a2f5a4b855ba9efd9e6de954eb35fda633909e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 15 Apr 2009 12:17:28 +0930 Subject: [PATCH] daemonize module --- ccan/daemonize/_info.c | 54 ++++++++++++++++++++++++++++++++++++++ ccan/daemonize/daemonize.c | 31 ++++++++++++++++++++++ ccan/daemonize/daemonize.h | 19 ++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 ccan/daemonize/_info.c create mode 100644 ccan/daemonize/daemonize.c create mode 100644 ccan/daemonize/daemonize.h diff --git a/ccan/daemonize/_info.c b/ccan/daemonize/_info.c new file mode 100644 index 00000000..2bdcaa97 --- /dev/null +++ b/ccan/daemonize/_info.c @@ -0,0 +1,54 @@ +#include +#include +#include "config.h" + +/** + * daemonize - routine to turn a process into a well-behaved daemon. + * + * Daemons should detach themselves thoroughly from the process which launched + * them, and not prevent any filesystems from being unmounted. daemonize() + * helps with the process. + * + * Example: + * #include + * #include + * #include + * #include + * #include + * + * 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()) + * err(1, "Failed to become daemon"); + * } else + * usage(argv[1]); + * } + * sleep(60); + * exit(0); + * } + * + * Licence: BSD (2 clause, ie. MIT) + */ +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; +} diff --git a/ccan/daemonize/daemonize.c b/ccan/daemonize/daemonize.c new file mode 100644 index 00000000..861b0fc7 --- /dev/null +++ b/ccan/daemonize/daemonize.c @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include + +/* This code is based on Stevens Advanced Programming in the UNIX + * Environment. */ +bool daemonize(void) +{ + pid_t pid; + + /* Separate from our parent via fork, so init inherits us. */ + if ((pid = fork()) < 0) + return false; + if (pid != 0) + exit(0); + + /* Don't hold files open. */ + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + + /* Session leader so ^C doesn't whack us. */ + setsid(); + /* Move off any mount points we might be in. */ + chdir("/"); + /* Discard our parent's old-fashioned umask prejudices. */ + umask(0); + return true; +} diff --git a/ccan/daemonize/daemonize.h b/ccan/daemonize/daemonize.h new file mode 100644 index 00000000..cc425a49 --- /dev/null +++ b/ccan/daemonize/daemonize.h @@ -0,0 +1,19 @@ +#ifndef CCAN_DAEMONIZE_H +#define CCAN_DAEMONIZE_H +#include + +/** + * daemonize - turn this process into a daemon. + * + * This routine forks us off to become a daemon. It returns false on failure + * (ie. fork() failed) and sets errno. + * + * Side effects for programmers to be aware of: + * - PID changes (our parent exits, we become child of init) + * - stdin, stdout and stderr file descriptors are closed + * - Current working directory changes to / + * - Umask is set to 0. + */ +bool daemonize(void); + +#endif /* CCAN_DAEMONIZE_H */ -- 2.39.2