]> mj.ucw.cz Git - libucw.git/blob - ucw/daemon.h
LibUCW: Fixed broken DARY_ALLOC() on 64bit architectures.
[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   DAEMON_FLAG_SIMULATE = 2,             // Simulate daemonization (avoid fork etc.)
34 };
35
36 /**
37  * Daemon initialization. Should be run after parsing of options.
38  * It resolves the UID and GID to run with and locks the PID file.
39  * Upon error, it calls @die().
40  **/
41 void daemon_init(struct daemon_params *dp);
42
43 /**
44  * Run the daemon. Should be run when everything is initialized. It forks off
45  * a new process and does all necessary setup. Inside the new process, it calls
46  * @body (and when it returns, it exits the process). In the original process, it writes
47  * the PID file and returns.
48  *
49  * When `DAEMON_FLAG_SIMULATE` is set, it justs calls @body. This is useful
50  * for running of daemons in a debugger.
51  **/
52 void daemon_run(struct daemon_params *dp, void (*body)(struct daemon_params *dp));
53
54 /**
55  * Clean up when the daemon is about to exit. It removes the PID file.
56  **/
57 void daemon_exit(struct daemon_params *dp);
58
59 #define DAEMON_ERR_LEN 256
60
61 /** Parameters passed to daemon_control() **/
62 struct daemon_control_params {
63   const char *pid_file;         // A path to PID file
64   const char *guard_file;       // A path to guard file
65   int action;                   // Action to perform (DAEMON_CONTROL_xxx)
66   char * const *argv;           // Daemon's arguments, NULL-terminated (for DAEMON_CONTROL_START)
67   int signal;                   // Signal to send (for DAEMON_CONTROL_SIGNAL)
68   char error_msg[DAEMON_ERR_LEN];       // A detailed error message returned (for DAEMON_STATUS_ERROR)
69 };
70
71 enum daemon_control_action {
72   DAEMON_CONTROL_CHECK = 1,
73   DAEMON_CONTROL_START,
74   DAEMON_CONTROL_STOP,
75   DAEMON_CONTROL_SIGNAL,
76 };
77
78 /**
79  * Perform an action on a daemon:
80  *
81  * * `DAEMON_CONTROL_START` to start the daemon
82  * * `DAEMON_CONTROL_STOP` to stop the daemon (send `SIGTERM` or `dc->signal` if non-zero)
83  * * `DAEMON_CONTROL_CHECK` to check that the daemon is running
84  * * `DAEMON_CONTROL_SIGNAL` to send a signal to the daemon (send `SIGHUP` or `dc->signal` if non-zero)
85  *
86  * The function returns a status code:
87  *
88  * * `DAEMON_STATUS_OK` if the action has been performed successfully
89  * * `DAEMON_STATUS_ALREADY_DONE` if the daemon is already in the requested state
90  * * `DAEMON_STATUS_NOT_RUNNING` if the action failed, because the daemon is not running
91  * * `DAEMON_STATUS_ERROR` if the action failed for some other reason (in this case,
92  *   `dc->error_msg` contains a full error message)
93  * * `DAEMON_STATUS_STALE` if the daemon was in an undefined state (e.g., a stale PID file);
94  *   for `DAEMON_CONTROL_START`, it means success
95  **/
96 enum daemon_control_status daemon_control(struct daemon_control_params *dc);
97
98 // XXX: Also used as exit codes of the daemon-control utility.
99 enum daemon_control_status {
100   DAEMON_STATUS_OK = 0,
101   DAEMON_STATUS_ALREADY_DONE = 100,
102   DAEMON_STATUS_NOT_RUNNING = 101,
103   DAEMON_STATUS_ERROR = 102,
104   DAEMON_STATUS_STALE = 103,
105 };
106
107 #endif