2 * UCW Library -- Daemonization
4 * (c) 2012 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include <sys/types.h>
15 struct daemon_params {
16 uns flags; // DAEMON_FLAG_xxx
17 const char *pid_file; // A path to PID file (optional)
18 const char *run_as_user; // User name or "#uid" (optional)
19 const char *run_as_group; // Group name or "#gid" (optional)
30 DAEMON_FLAG_PRESERVE_CWD = 1, // Skip chdir("/")
34 * Daemon initialization. Should be run after parsing of options.
35 * It resolves the UID and GID to run with and locks the PID file.
36 * Upon error, it calls @die().
38 void daemon_init(struct daemon_params *dp);
41 * Run the daemon. Should be run when everything is initialized. It forks off
42 * a new process and does all necessary setup. Inside the new process, it calls
43 * @body (and when it returns, it exits the process). In the original process, it writes
44 * the PID file and returns.
46 void daemon_run(struct daemon_params *dp, void (*body)(struct daemon_params *dp));
49 * Clean up when the daemon is about to exit. It removes the PID file.
51 void daemon_exit(struct daemon_params *dp);