bg.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * bg.c -- handles:
  3. * moving the process to the background, i.e. forking, while keeping threads
  4. * happy.
  5. *
  6. */
  7. #include "common.h"
  8. #include "bg.h"
  9. #include "thread.h"
  10. #include "main.h"
  11. #include <signal.h>
  12. #ifdef HAVE_SYS_PTRACE_H
  13. # include <sys/ptrace.h>
  14. #endif /* HAVE_SYS_PTRACE_H */
  15. #ifndef CYGWIN_HACKS
  16. # include <sys/wait.h>
  17. #endif /* !CYGWIN_HACKS */
  18. #include <sys/types.h>
  19. #include <errno.h>
  20. #include <unistd.h>
  21. #include <string.h>
  22. #include <fcntl.h>
  23. #include <unistd.h>
  24. #include <sys/stat.h>
  25. time_t lastfork = 0;
  26. #if !defined(CYGWIN_HACKS) && !defined(__sun__)
  27. #ifdef NO
  28. pid_t watcher; /* my child/watcher */
  29. static void init_watcher(pid_t);
  30. #endif
  31. #endif /* !CYGWIN_HACKS */
  32. int close_tty()
  33. {
  34. int fd = -1;
  35. if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
  36. dup2(fd, STDIN_FILENO);
  37. dup2(fd, STDOUT_FILENO);
  38. dup2(fd, STDERR_FILENO);
  39. if (fd > 2)
  40. close(fd);
  41. return 1;
  42. }
  43. return 0;
  44. }
  45. static int my_daemon(int nochdir, int noclose)
  46. {
  47. switch (fork()) {
  48. case -1:
  49. return (-1);
  50. case 0:
  51. break;
  52. default:
  53. _exit(0);
  54. }
  55. if (setsid() == -1)
  56. return (-1);
  57. if (!nochdir)
  58. (void) chdir("/");
  59. if (!noclose)
  60. close_tty();
  61. return (0);
  62. }
  63. pid_t
  64. do_fork()
  65. {
  66. if (my_daemon(1, 1))
  67. fatal(strerror(errno), 0);
  68. pid_t pid = getpid();
  69. writepid(conf.bot->pid_file, pid);
  70. lastfork = now;
  71. #if !defined(CYGWIN_HACKS) && !defined(__sun__)
  72. #ifdef NO
  73. if (conf.watcher)
  74. init_watcher(pid);
  75. #endif
  76. #endif /* !CYGWIN_HACKS */
  77. return pid;
  78. }
  79. void
  80. writepid(const char *pidfile, pid_t pid)
  81. {
  82. FILE *fp = NULL;
  83. sdprintf("Writing pid to: %s", pidfile);
  84. /* Need to attempt to write pid now, not later. */
  85. unlink(pidfile);
  86. if ((fp = fopen(pidfile, "w"))) {
  87. fprintf(fp, "%u\n", pid);
  88. if (fflush(fp)) {
  89. /* Kill bot incase a botchk is run from crond. */
  90. printf("* Warning! Could not write %s file!\n", pidfile);
  91. printf(" Try freeing some disk space\n");
  92. fclose(fp);
  93. unlink(pidfile);
  94. exit(1);
  95. } else
  96. fclose(fp);
  97. } else
  98. printf("* Warning! Could not write %s file!\n", pidfile);
  99. }
  100. #if !defined(CYGWIN_HACKS) && !defined(__sun__)
  101. #ifdef NO
  102. static void
  103. init_watcher(pid_t parent)
  104. {
  105. int x = fork();
  106. if (x == -1)
  107. fatal("Could not fork off a watcher process", 0);
  108. if (x != 0) { /* parent [bot] */
  109. watcher = x;
  110. /* printf("WATCHER: %d\n", watcher); */
  111. return;
  112. } else { /* child [watcher] */
  113. watcher = getpid();
  114. /* printf("MY PARENT: %d\n", parent); */
  115. /* printf("my pid: %d\n", watcher); */
  116. if (ptrace(PT_ATTACH, parent, 0, 0) == -1)
  117. fatal("Cannot attach to parent", 0);
  118. while (1) {
  119. int status = 0, sig = 0, ret = 0;
  120. waitpid(parent, &status, 0);
  121. sig = WSTOPSIG(status);
  122. if (sig) {
  123. ret = ptrace(PT_CONTINUE, parent, (char *) 1, sig);
  124. if (ret == -1) /* send the signal! */
  125. fatal("Could not send signal to parent", 0);
  126. /* printf("Sent signal %s (%d) to parent\n", strsignal(sig), sig); */
  127. } else {
  128. ret = ptrace(PT_CONTINUE, parent, (char *) 1, 0);
  129. if (ret == -1) {
  130. if (errno == ESRCH) /* parent is gone! */
  131. exit(0); /* just exit */
  132. else
  133. fatal("Could not continue parent", 0);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. #endif
  140. #endif /* !CYGWIN_HACKS */