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