1#ifndef foodaemonforkhfoo
2#define foodaemonforkhfoo
3
4/***
5  This file is part of libdaemon.
6
7  Copyright 2003-2008 Lennart Poettering
8
9  libdaemon is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as
11  published by the Free Software Foundation, either version 2.1 of the
12  License, or (at your option) any later version.
13
14  libdaemon is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18
19  You should have received a copy of the GNU Lesser General Public
20  License along with libdaemon. If not, see
21  <http://www.gnu.org/licenses/>.
22***/
23
24#include <sys/types.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/** \mainpage libdaemon
31 *
32 * libdaemon
33 *
34 * For a brief explanation of libdaemons's purpose, have a look on the
35 * README file. Thank you!
36 *
37 */
38
39/** \example testd.c
40 * This is an example for the usage of libdaemon
41 */
42
43/** \file
44 *
45 * Contains an API for doing a daemonizing fork().
46 *
47 * You may daemonize by calling daemon_fork(), a function similar to
48 * the plain fork(). If you want to return a return value of the
49 * initialization procedure of the child from the parent, you may use
50 * the daemon_retval_xxx() functions.
51 */
52
53/** Does a daemonizing fork(). For the new daemon process STDIN,
54 * STDOUT, STDERR are connected to /dev/null, the process is a session
55 * leader, the current directory is changed to /, the umask is set to
56 * 777.
57 * @return On success, the PID of the child process is returned in the
58 * parent's thread of execution, and a 0 is returned in the child's
59 * thread of execution. On failure, -1 will be returned in the
60 * parent's context, no child process will be created, and errno will
61 * be set appropriately.
62 */
63pid_t daemon_fork(void);
64
65/** Allocate and initialize resources required by the
66 * daemon_retval_xxx() functions. These functions allow the child to
67 * send a value to the parent after completing its initialisation.
68 * Call this in the parent before forking.
69 * @return zero on success, nonzero on failure.
70 */
71int daemon_retval_init(void);
72
73/** Frees the resources allocated by daemon_retval_init(). This should
74 * be called if neither daemon_retval_wait() nor daemon_retval_send()
75 * is called in the current process. The resources allocated by
76 * daemon_retval_init() should be freed in both parent and daemon
77 * process. This may be achieved by using daemon_retval_wait()
78 * resp. daemon_retval_send(), or by using daemon_retval_done().
79 */
80void daemon_retval_done(void);
81
82/** Return the value sent by the child via the daemon_retval_send()
83 * function, but wait only the specified number of seconds before
84 * timing out and returning a negative number. Should be called just
85 * once from the parent process only. A subsequent call to
86 * daemon_retval_done() in the parent is ignored.
87 *
88 * @param timeout Thetimeout in seconds
89 * @return The integer passed daemon_retval_send() in the daemon process, or -1 on failure.
90 */
91int daemon_retval_wait(int timeout);
92
93/** Send the specified integer to the parent process. Do not send -1
94 * because this signifies a library error. Should be called just once
95 * from the daemon process only. A subsequent call to
96 * daemon_retval_done() in the daemon is ignored.  @param s The
97 * integer to pass to daemon_retval_wait() in the parent process
98 * @return Zero on success, nonzero on failure.
99 */
100int daemon_retval_send(int s);
101
102/** This variable is defined to 1 iff daemon_close_all() and
103 * daemon_close_allv() are supported.
104 * @since 0.11
105 * @see daemon_close_all(), daemon_close_allv() */
106#define DAEMON_CLOSE_ALL_AVAILABLE 1
107
108/** Close all file descriptors except those passed. List needs to be
109 * terminated by -1. FDs 0, 1, 2 will be kept open anyway.
110 * @since 0.11
111 * @see DAEMON_CLOSE_ALL_AVAILABLE */
112int daemon_close_all(int except_fd, ...);
113
114/** Same as daemon_close_all but takes an array of fds, terminated by
115 * -1
116 * @since 0.11
117 * @see DAEMON_CLOSE_ALL_AVAILABLE */
118int daemon_close_allv(const int except_fds[]);
119
120/** This variable is defined to 1 iff daemon_unblock_sigs() and
121 * daemon_unblock_sigsv() are supported.
122 * @since 0.13
123 * @see daemon_unblock_sigs(), daemon_unblock_sigsv()*/
124#define DAEMON_UNBLOCK_SIGS_AVAILABLE 1
125
126/** Unblock all signals except those passed. List needs to be
127 * terminated by -1.
128 * @since 0.13
129 * @see DAEMON_UNBLOCK_SIGS_AVAILABLE */
130int daemon_unblock_sigs(int except, ...);
131
132/** Same as daemon_unblock_sigs() but takes an array of signals,
133 * terminated by -1
134 * @since 0.13
135 * @see DAEMON_UNBLOCK_SIGS_AVAILABLE */
136int daemon_unblock_sigsv(const int except[]);
137
138/** This variable is defined to 1 iff daemon_reset_sigs() and
139 * daemon_reset_sigsv() are supported.
140 * @since 0.13
141 * @see daemon_reset_sigs(), daemon_reset_sigsv() */
142#define DAEMON_RESET_SIGS_AVAILABLE 1
143
144/** Reset all signal handlers except those passed. List needs to be
145 * terminated by -1.
146 * @since 0.13
147 * @see DAEMON_RESET_SIGS_AVAILABLE */
148int daemon_reset_sigs(int except, ...);
149
150/** Same as daemon_reset_sigs() but takes an array of signals,
151 * terminated by -1
152 * @since 0.13
153 * @see DAEMON_RESET_SIGS_AVAILABLE */
154int daemon_reset_sigsv(const int except[]);
155
156#ifdef __cplusplus
157}
158#endif
159
160#endif
161