bg.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * bg.c -- handles:
  22. * moving the process to the background, i.e. forking, while keeping threads
  23. * happy.
  24. *
  25. */
  26. #include "common.h"
  27. #include "bg.h"
  28. #include "thread.h"
  29. #include "main.h"
  30. #include <signal.h>
  31. #ifdef HAVE_SYS_PTRACE_H
  32. # include <sys/ptrace.h>
  33. #endif /* HAVE_SYS_PTRACE_H */
  34. #ifndef CYGWIN_HACKS
  35. # include <sys/wait.h>
  36. #endif /* !CYGWIN_HACKS */
  37. #include <sys/types.h>
  38. #include <errno.h>
  39. #include <unistd.h>
  40. #include <string.h>
  41. #include <fcntl.h>
  42. #include <unistd.h>
  43. #include <sys/stat.h>
  44. time_t lastfork = 0;
  45. #if !defined(CYGWIN_HACKS) && !defined(__sun__)
  46. #ifdef NO
  47. pid_t watcher; /* my child/watcher */
  48. static void init_watcher(pid_t);
  49. #endif
  50. #endif /* !CYGWIN_HACKS */
  51. int close_tty()
  52. {
  53. int fd = -1;
  54. if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
  55. dup2(fd, STDIN_FILENO);
  56. dup2(fd, STDOUT_FILENO);
  57. dup2(fd, STDERR_FILENO);
  58. if (fd > 2)
  59. close(fd);
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. static int my_daemon(int nochdir, int noclose)
  65. {
  66. switch (fork()) {
  67. case -1:
  68. return (-1);
  69. case 0:
  70. break;
  71. default:
  72. _exit(0);
  73. }
  74. if (setsid() == -1)
  75. return (-1);
  76. if (!nochdir)
  77. (void) chdir("/");
  78. if (!noclose)
  79. close_tty();
  80. return (0);
  81. }
  82. pid_t
  83. do_fork()
  84. {
  85. if (my_daemon(1, 1))
  86. fatal(strerror(errno), 0);
  87. pid_t pid = getpid();
  88. writepid(conf.bot->pid_file, pid);
  89. lastfork = now;
  90. #if !defined(CYGWIN_HACKS) && !defined(__sun__)
  91. #ifdef NO
  92. if (conf.watcher)
  93. init_watcher(pid);
  94. #endif
  95. #endif /* !CYGWIN_HACKS */
  96. return pid;
  97. }
  98. void
  99. writepid(const char *pidfile, pid_t pid)
  100. {
  101. FILE *fp = NULL;
  102. sdprintf("Writing pid to: %s", pidfile);
  103. /* Need to attempt to write pid now, not later. */
  104. unlink(pidfile);
  105. if ((fp = fopen(pidfile, "w"))) {
  106. fprintf(fp, "%u\n", pid);
  107. if (fflush(fp)) {
  108. /* Kill bot incase a botchk is run from crond. */
  109. printf("* Warning! Could not write %s file!\n", pidfile);
  110. printf(" Try freeing some disk space\n");
  111. fclose(fp);
  112. unlink(pidfile);
  113. exit(1);
  114. } else
  115. fclose(fp);
  116. } else
  117. printf("* Warning! Could not write %s file!\n", pidfile);
  118. }
  119. #if !defined(CYGWIN_HACKS) && !defined(__sun__)
  120. #ifdef NO
  121. static void
  122. init_watcher(pid_t parent)
  123. {
  124. int x = fork();
  125. if (x == -1)
  126. fatal("Could not fork off a watcher process", 0);
  127. if (x != 0) { /* parent [bot] */
  128. watcher = x;
  129. /* printf("WATCHER: %d\n", watcher); */
  130. return;
  131. } else { /* child [watcher] */
  132. watcher = getpid();
  133. /* printf("MY PARENT: %d\n", parent); */
  134. /* printf("my pid: %d\n", watcher); */
  135. if (ptrace(PT_ATTACH, parent, 0, 0) == -1)
  136. fatal("Cannot attach to parent", 0);
  137. while (1) {
  138. int status = 0, sig = 0, ret = 0;
  139. waitpid(parent, &status, 0);
  140. sig = WSTOPSIG(status);
  141. if (sig) {
  142. ret = ptrace(PT_CONTINUE, parent, (char *) 1, sig);
  143. if (ret == -1) /* send the signal! */
  144. fatal("Could not send signal to parent", 0);
  145. /* printf("Sent signal %s (%d) to parent\n", strsignal(sig), sig); */
  146. } else {
  147. ret = ptrace(PT_CONTINUE, parent, (char *) 1, 0);
  148. if (ret == -1) {
  149. if (errno == ESRCH) /* parent is gone! */
  150. exit(0); /* just exit */
  151. else
  152. fatal("Could not continue parent", 0);
  153. }
  154. }
  155. }
  156. }
  157. }
  158. #endif
  159. #endif /* !CYGWIN_HACKS */