daemon.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 <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.h>
  47. namespace gearmand
  48. {
  49. pid_t parent_pid;
  50. extern "C"
  51. {
  52. static void sigusr1_handler(int sig)
  53. {
  54. if (sig == SIGUSR1)
  55. {
  56. _exit(EXIT_SUCCESS);
  57. }
  58. }
  59. }
  60. bool daemon_is_ready(bool close_io)
  61. {
  62. kill(parent_pid, SIGUSR1);
  63. if (not close_io)
  64. return true;;
  65. int fd;
  66. if ((fd = open("/dev/null", O_RDWR, 0)) < 0)
  67. {
  68. perror("open");
  69. return false;
  70. }
  71. else
  72. {
  73. if(dup2(fd, STDIN_FILENO) < 0)
  74. {
  75. perror("dup2 stdin");
  76. return false;
  77. }
  78. if(dup2(fd, STDOUT_FILENO) < 0)
  79. {
  80. perror("dup2 stdout");
  81. return false;
  82. }
  83. if(dup2(fd, STDERR_FILENO) < 0)
  84. {
  85. perror("dup2 stderr");
  86. return false;
  87. }
  88. if (fd > STDERR_FILENO)
  89. {
  90. if (close(fd) < 0)
  91. {
  92. perror("close");
  93. return false;
  94. }
  95. }
  96. }
  97. return true;
  98. }
  99. #ifndef __INTEL_COMPILER
  100. #pragma GCC diagnostic ignored "-Wold-style-cast"
  101. #endif
  102. bool daemonize(bool is_chdir, bool wait_sigusr1)
  103. {
  104. pid_t child= -1;
  105. parent_pid= getpid();
  106. signal(SIGUSR1, sigusr1_handler);
  107. child= fork();
  108. switch (child)
  109. {
  110. case -1:
  111. return false;
  112. case 0:
  113. break;
  114. default:
  115. if (wait_sigusr1)
  116. {
  117. /* parent */
  118. int exit_code= EXIT_FAILURE;
  119. int status;
  120. while (waitpid(child, &status, 0) != child)
  121. { }
  122. if (WIFEXITED(status))
  123. {
  124. exit_code= WEXITSTATUS(status);
  125. }
  126. if (WIFSIGNALED(status))
  127. {
  128. exit_code= EXIT_FAILURE;
  129. }
  130. _exit(exit_code);
  131. }
  132. else
  133. {
  134. _exit(EXIT_SUCCESS);
  135. }
  136. }
  137. /* child */
  138. if (setsid() == -1)
  139. {
  140. perror("setsid");
  141. return false;
  142. }
  143. if (is_chdir)
  144. {
  145. if (chdir("/") < 0)
  146. {
  147. perror("chdir");
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153. } /* namespace drizzled */