bg.c 3.1 KB

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