summaryrefslogtreecommitdiffstats
path: root/libdaemon/dfork.h
blob: 47b1630aa98c262d7b50c0edce6b34ef473a14df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef foodaemonforkhfoo
#define foodaemonforkhfoo

/***
  This file is part of libdaemon.

  Copyright 2003-2008 Lennart Poettering

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.

***/

#include <sys/types.h>

#ifdef __cplusplus
extern "C" {
#endif

/** \mainpage libdaemon
 *
 * libdaemon
 *
 * For a brief explanation of libdaemons's purpose, have a look on the
 * README file. Thank you!
 *
 */

/** \example testd.c
 * This is an example for the usage of libdaemon
 */

/** \file
 *
 * Contains an API for doing a daemonizing fork().
 *
 * You may daemonize by calling daemon_fork(), a function similar to
 * the plain fork(). If you want to return a return value of the
 * initialization procedure of the child from the parent, you may use
 * the daemon_retval_xxx() functions.
 */

/** Does a daemonizing fork(). For the new daemon process STDIN,
 * STDOUT, STDERR are connected to /dev/null, the process is a session
 * leader, the current directory is changed to /, the umask is set to
 * 777.
 * @return On success, the PID of the child process is returned in the
 * parent's thread of execution, and a 0 is returned in the child's
 * thread of execution. On failure, -1 will be returned in the
 * parent's context, no child process will be created, and errno will
 * be set appropriately.
 */
pid_t daemon_fork(void);

/** Allocate and initialize resources required by the
 * daemon_retval_xxx() functions. These functions allow the child to
 * send a value to the parent after completing its initialisation.
 * Call this in the parent before forking.
 * @return zero on success, nonzero on failure.
 */
int daemon_retval_init(void);

/** Frees the resources allocated by daemon_retval_init(). This should
 * be called if neither daemon_retval_wait() nor daemon_retval_send()
 * is called in the current process. The resources allocated by
 * daemon_retval_init() should be freed in both parent and daemon
 * process. This may be achieved by using daemon_retval_wait()
 * resp. daemon_retval_send(), or by using daemon_retval_done().
 */
void daemon_retval_done(void);

/** Return the value sent by the child via the daemon_retval_send()
 * function, but wait only the specified number of seconds before
 * timing out and returning a negative number. Should be called just
 * once from the parent process only. A subsequent call to
 * daemon_retval_done() in the parent is ignored.
 *
 * @param timeout Thetimeout in seconds
 * @return The integer passed daemon_retval_send() in the daemon process, or -1 on failure.
 */
int daemon_retval_wait(int timeout);

/** Send the specified integer to the parent process. Do not send -1
 * because this signifies a library error. Should be called just once
 * from the daemon process only. A subsequent call to
 * daemon_retval_done() in the daemon is ignored.  @param s The
 * integer to pass to daemon_retval_wait() in the parent process
 * @return Zero on success, nonzero on failure.
 */
int daemon_retval_send(int s);

/** This variable is defined to 1 iff daemon_close_all() and
 * daemon_close_allv() are supported.
 * @since 0.11
 * @see daemon_close_all(), daemon_close_allv() */
#define DAEMON_CLOSE_ALL_AVAILABLE 1

/** Close all file descriptors except those passed. List needs to be
 * terminated by -1. FDs 0, 1, 2 will be kept open anyway.
 * @since 0.11
 * @see DAEMON_CLOSE_ALL_AVAILABLE */
int daemon_close_all(int except_fd, ...);

/** Same as daemon_close_all but takes an array of fds, terminated by
 * -1
 * @since 0.11
 * @see DAEMON_CLOSE_ALL_AVAILABLE */
int daemon_close_allv(const int except_fds[]);

/** This variable is defined to 1 iff daemon_unblock_sigs() and
 * daemon_unblock_sigsv() are supported.
 * @since 0.13
 * @see daemon_unblock_sigs(), daemon_unblock_sigsv()*/
#define DAEMON_UNBLOCK_SIGS_AVAILABLE 1

/** Unblock all signals except those passed. List needs to be
 * terminated by -1.
 * @since 0.13
 * @see DAEMON_UNBLOCK_SIGS_AVAILABLE */
int daemon_unblock_sigs(int except, ...);

/** Same as daemon_unblock_sigs() but takes an array of signals,
 * terminated by -1
 * @since 0.13
 * @see DAEMON_UNBLOCK_SIGS_AVAILABLE */
int daemon_unblock_sigsv(const int except[]);

/** This variable is defined to 1 iff daemon_reset_sigs() and
 * daemon_reset_sigsv() are supported.
 * @since 0.13
 * @see daemon_reset_sigs(), daemon_reset_sigsv() */
#define DAEMON_RESET_SIGS_AVAILABLE 1

/** Reset all signal handlers except those passed. List needs to be
 * terminated by -1.
 * @since 0.13
 * @see DAEMON_RESET_SIGS_AVAILABLE */
int daemon_reset_sigs(int except, ...);

/** Same as daemon_reset_sigs() but takes an array of signals,
 * terminated by -1
 * @since 0.13
 * @see DAEMON_RESET_SIGS_AVAILABLE */
int daemon_reset_sigsv(const int except[]);

#ifdef __cplusplus
}
#endif

#endif