daemon_with_notify.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*-
  2. * Copyright (c) 1990, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. * Copyright (c) 2010
  5. * Stewart Smith
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the University nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <fcntl.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <sys/types.h>
  35. #include <sys/wait.h>
  36. #include <signal.h>
  37. #include <unistd.h>
  38. #include <sys/select.h>
  39. #include <ccan/daemon_with_notify/daemon_with_notify.h>
  40. void sigusr1_handler(int sig);
  41. pid_t parent_pid;
  42. void sigusr1_handler(int sig)
  43. {
  44. if (sig == SIGUSR1)
  45. _exit(EXIT_SUCCESS);
  46. }
  47. int daemon_is_ready(void)
  48. {
  49. kill(parent_pid, SIGUSR1);
  50. return 0;
  51. }
  52. int daemonize(int nochdir, int noclose, int wait_sigusr1)
  53. {
  54. int fd;
  55. pid_t child= -1;
  56. parent_pid= getpid();
  57. signal(SIGUSR1, sigusr1_handler);
  58. child= fork();
  59. switch (child)
  60. {
  61. case -1:
  62. return (-1);
  63. case 0:
  64. break;
  65. default:
  66. if (wait_sigusr1)
  67. {
  68. /* parent */
  69. int exit_code= -1;
  70. int status;
  71. while (waitpid(child, &status, 0) != child);
  72. if (WIFEXITED(status))
  73. {
  74. exit_code= WEXITSTATUS(status);
  75. }
  76. if (WIFSIGNALED(status))
  77. {
  78. exit_code= -1;
  79. }
  80. _exit(exit_code);
  81. }
  82. else
  83. {
  84. _exit(EXIT_SUCCESS);
  85. }
  86. }
  87. /* child */
  88. if (setsid() == -1)
  89. return (-1);
  90. if (nochdir == 0) {
  91. if(chdir("/") != 0) {
  92. perror("chdir");
  93. return (-1);
  94. }
  95. }
  96. if (noclose == 0 && (fd = open("/dev/null", O_RDWR, 0)) != -1) {
  97. if(dup2(fd, STDIN_FILENO) < 0) {
  98. perror("dup2 stdin");
  99. return (-1);
  100. }
  101. if(dup2(fd, STDOUT_FILENO) < 0) {
  102. perror("dup2 stdout");
  103. return (-1);
  104. }
  105. if(dup2(fd, STDERR_FILENO) < 0) {
  106. perror("dup2 stderr");
  107. return (-1);
  108. }
  109. if (fd > STDERR_FILENO) {
  110. if(close(fd) < 0) {
  111. perror("close");
  112. return (-1);
  113. }
  114. }
  115. }
  116. return (0);
  117. }