daemon.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* $Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $ */
  2. /* $NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $ */
  3. /*-
  4. * Copyright (c) 1990, 1993
  5. * The Regents of the University of California. All rights reserved.
  6. * Copyright (c) 2010
  7. * Stewart Smith
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include "gear_config.h"
  34. #if defined __SUNPRO_C || defined __DECC || defined __HP_cc
  35. # pragma ident "@(#)$Header: /cvsroot/wikipedia/willow/src/bin/willow/daemon.c,v 1.1 2005/05/02 19:15:21 kateturner Exp $"
  36. # pragma ident "$NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $"
  37. #endif
  38. #include <fcntl.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <sys/types.h>
  42. #include <sys/wait.h>
  43. #include <signal.h>
  44. #include <unistd.h>
  45. #include <sys/select.h>
  46. #include <util/daemon.hpp>
  47. #include <iostream>
  48. namespace datadifferential {
  49. namespace util {
  50. pid_t parent_pid;
  51. extern "C"
  52. {
  53. static void sigusr1_handler(int sig)
  54. {
  55. if (sig == SIGUSR1)
  56. {
  57. _exit(EXIT_SUCCESS);
  58. }
  59. }
  60. }
  61. bool daemon_is_ready(bool close_io)
  62. {
  63. if (kill(parent_pid, SIGUSR1) == -1)
  64. {
  65. perror("kill");
  66. return false;
  67. }
  68. if (close_io == false)
  69. {
  70. return true;;
  71. }
  72. int fd;
  73. if ((fd = open("/dev/null", O_RDWR, 0)) < 0)
  74. {
  75. perror("open");
  76. return false;
  77. }
  78. else
  79. {
  80. if (dup2(fd, STDIN_FILENO) < 0)
  81. {
  82. perror("dup2 stdin");
  83. return false;
  84. }
  85. if (dup2(fd, STDOUT_FILENO) < 0)
  86. {
  87. perror("dup2 stdout");
  88. return false;
  89. }
  90. if (dup2(fd, STDERR_FILENO) < 0)
  91. {
  92. perror("dup2 stderr");
  93. return false;
  94. }
  95. if (fd > STDERR_FILENO)
  96. {
  97. if (close(fd) < 0)
  98. {
  99. perror("close");
  100. return false;
  101. }
  102. }
  103. }
  104. return true;
  105. }
  106. #ifndef __INTEL_COMPILER
  107. #pragma GCC diagnostic ignored "-Wold-style-cast"
  108. #endif
  109. bool daemonize(bool is_chdir, bool wait_sigusr1)
  110. {
  111. struct sigaction new_action;
  112. new_action.sa_handler= sigusr1_handler;
  113. sigemptyset(&new_action.sa_mask);
  114. new_action.sa_flags= 0;
  115. sigaction(SIGUSR1, &new_action, NULL);
  116. parent_pid= getpid();
  117. pid_t child= fork();
  118. switch (child)
  119. {
  120. case -1:
  121. return false;
  122. case 0:
  123. break;
  124. default:
  125. if (wait_sigusr1)
  126. {
  127. /* parent */
  128. int exit_code= EXIT_FAILURE;
  129. int status;
  130. if (waitpid(child, &status, 0) != -1)
  131. {
  132. if (WIFEXITED(status))
  133. {
  134. exit_code= WEXITSTATUS(status);
  135. }
  136. if (WIFSIGNALED(status))
  137. {
  138. exit_code= EXIT_FAILURE;
  139. }
  140. }
  141. _exit(exit_code);
  142. }
  143. else
  144. {
  145. _exit(EXIT_SUCCESS);
  146. }
  147. }
  148. /* child */
  149. if (setsid() == -1)
  150. {
  151. perror("setsid");
  152. return false;
  153. }
  154. if (is_chdir)
  155. {
  156. if (chdir("/") < 0)
  157. {
  158. perror("chdir");
  159. return false;
  160. }
  161. }
  162. return true;
  163. }
  164. } /* namespace util */
  165. } /* namespace datadifferential */