1 #ifndef foodaemonforkhfoo 2 #define foodaemonforkhfoo 3 4 /*** 5 This file is part of libdaemon. 6 7 Copyright 2003-2008 Lennart Poettering 8 9 Permission is hereby granted, free of charge, to any person obtaining a copy 10 of this software and associated documentation files (the "Software"), to deal 11 in the Software without restriction, including without limitation the rights 12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 copies of the Software, and to permit persons to whom the Software is 14 furnished to do so, subject to the following conditions: 15 16 The above copyright notice and this permission notice shall be included in 17 all copies or substantial portions of the Software. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 SOFTWARE. 26 27 ***/ 28 29 #include <sys/types.h> 30 31 #ifdef __cplusplus 32 extern "C" { 33 #endif 34 35 /** \mainpage libdaemon 36 * 37 * libdaemon 38 * 39 * For a brief explanation of libdaemons's purpose, have a look on the 40 * README file. Thank you! 41 * 42 */ 43 44 /** \example testd.c 45 * This is an example for the usage of libdaemon 46 */ 47 48 /** \file 49 * 50 * Contains an API for doing a daemonizing fork(). 51 * 52 * You may daemonize by calling daemon_fork(), a function similar to 53 * the plain fork(). If you want to return a return value of the 54 * initialization procedure of the child from the parent, you may use 55 * the daemon_retval_xxx() functions. 56 */ 57 58 /** Does a daemonizing fork(). For the new daemon process STDIN, 59 * STDOUT, STDERR are connected to /dev/null, the process is a session 60 * leader, the current directory is changed to /, the umask is set to 61 * 777. 62 * @return On success, the PID of the child process is returned in the 63 * parent's thread of execution, and a 0 is returned in the child's 64 * thread of execution. On failure, -1 will be returned in the 65 * parent's context, no child process will be created, and errno will 66 * be set appropriately. 67 */ 68 pid_t daemon_fork(void); 69 70 /** Allocate and initialize resources required by the 71 * daemon_retval_xxx() functions. These functions allow the child to 72 * send a value to the parent after completing its initialisation. 73 * Call this in the parent before forking. 74 * @return zero on success, nonzero on failure. 75 */ 76 int daemon_retval_init(void); 77 78 /** Frees the resources allocated by daemon_retval_init(). This should 79 * be called if neither daemon_retval_wait() nor daemon_retval_send() 80 * is called in the current process. The resources allocated by 81 * daemon_retval_init() should be freed in both parent and daemon 82 * process. This may be achieved by using daemon_retval_wait() 83 * resp. daemon_retval_send(), or by using daemon_retval_done(). 84 */ 85 void daemon_retval_done(void); 86 87 /** Return the value sent by the child via the daemon_retval_send() 88 * function, but wait only the specified number of seconds before 89 * timing out and returning a negative number. Should be called just 90 * once from the parent process only. A subsequent call to 91 * daemon_retval_done() in the parent is ignored. 92 * 93 * @param timeout Thetimeout in seconds 94 * @return The integer passed daemon_retval_send() in the daemon process, or -1 on failure. 95 */ 96 int daemon_retval_wait(int timeout); 97 98 /** Send the specified integer to the parent process. Do not send -1 99 * because this signifies a library error. Should be called just once 100 * from the daemon process only. A subsequent call to 101 * daemon_retval_done() in the daemon is ignored. @param s The 102 * integer to pass to daemon_retval_wait() in the parent process 103 * @return Zero on success, nonzero on failure. 104 */ 105 int daemon_retval_send(int s); 106 107 /** This variable is defined to 1 iff daemon_close_all() and 108 * daemon_close_allv() are supported. 109 * @since 0.11 110 * @see daemon_close_all(), daemon_close_allv() */ 111 #define DAEMON_CLOSE_ALL_AVAILABLE 1 112 113 /** Close all file descriptors except those passed. List needs to be 114 * terminated by -1. FDs 0, 1, 2 will be kept open anyway. 115 * @since 0.11 116 * @see DAEMON_CLOSE_ALL_AVAILABLE */ 117 int daemon_close_all(int except_fd, ...); 118 119 /** Same as daemon_close_all but takes an array of fds, terminated by 120 * -1 121 * @since 0.11 122 * @see DAEMON_CLOSE_ALL_AVAILABLE */ 123 int daemon_close_allv(const int except_fds[]); 124 125 /** This variable is defined to 1 iff daemon_unblock_sigs() and 126 * daemon_unblock_sigsv() are supported. 127 * @since 0.13 128 * @see daemon_unblock_sigs(), daemon_unblock_sigsv()*/ 129 #define DAEMON_UNBLOCK_SIGS_AVAILABLE 1 130 131 /** Unblock all signals except those passed. List needs to be 132 * terminated by -1. 133 * @since 0.13 134 * @see DAEMON_UNBLOCK_SIGS_AVAILABLE */ 135 int daemon_unblock_sigs(int except, ...); 136 137 /** Same as daemon_unblock_sigs() but takes an array of signals, 138 * terminated by -1 139 * @since 0.13 140 * @see DAEMON_UNBLOCK_SIGS_AVAILABLE */ 141 int daemon_unblock_sigsv(const int except[]); 142 143 /** This variable is defined to 1 iff daemon_reset_sigs() and 144 * daemon_reset_sigsv() are supported. 145 * @since 0.13 146 * @see daemon_reset_sigs(), daemon_reset_sigsv() */ 147 #define DAEMON_RESET_SIGS_AVAILABLE 1 148 149 /** Reset all signal handlers except those passed. List needs to be 150 * terminated by -1. 151 * @since 0.13 152 * @see DAEMON_RESET_SIGS_AVAILABLE */ 153 int daemon_reset_sigs(int except, ...); 154 155 /** Same as daemon_reset_sigs() but takes an array of signals, 156 * terminated by -1 157 * @since 0.13 158 * @see DAEMON_RESET_SIGS_AVAILABLE */ 159 int daemon_reset_sigsv(const int except[]); 160 161 #ifdef __cplusplus 162 } 163 #endif 164 165 #endif 166