dfork.h 5.3 KB

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