]> mj.ucw.cz Git - libucw.git/blob - ucw/daemon.h
Daemon: Reverted to one-level locking and added bits of docs
[libucw.git] / ucw / daemon.h
1 /*
2  *      UCW Library -- Daemonization
3  *
4  *      (c) 2012 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_DAEMON_H
11 #define _UCW_DAEMON_H
12
13 #include <sys/types.h>
14
15 /** Parameters passed to the daemon helper. **/
16 struct daemon_params {
17   uns flags;                            // DAEMON_FLAG_xxx
18   const char *pid_file;                 // A path to PID file (optional)
19   const char *run_as_user;              // User name or "#uid" (optional)
20   const char *run_as_group;             // Group name or "#gid" (optional)
21
22   // Internal
23   uid_t run_as_uid;
24   uid_t run_as_gid;
25   int want_setuid;
26   int want_setgid;
27   int pid_fd;
28 };
29
30 /** Flags passed to the daemon helper. **/
31 enum daemon_flags {
32   DAEMON_FLAG_PRESERVE_CWD = 1,         // Skip chdir("/")
33 };
34
35 /**
36  * Daemon initialization. Should be run after parsing of options.
37  * It resolves the UID and GID to run with and locks the PID file.
38  * Upon error, it calls @die().
39  **/
40 void daemon_init(struct daemon_params *dp);
41
42 /**
43  * Run the daemon. Should be run when everything is initialized. It forks off
44  * a new process and does all necessary setup. Inside the new process, it calls
45  * @body (and when it returns, it exits the process). In the original process, it writes
46  * the PID file and returns.
47  **/
48 void daemon_run(struct daemon_params *dp, void (*body)(struct daemon_params *dp));
49
50 /**
51  * Clean up when the daemon is about to exit. It removes the PID file.
52  **/
53 void daemon_exit(struct daemon_params *dp);
54
55 #endif