bg.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. time_t lastfork = 0;
  21. #ifndef CYGWIN_HACKS
  22. pid_t watcher; /* my child/watcher */
  23. static void init_watcher(pid_t);
  24. #endif /* !CYGWIN_HACKS */
  25. pid_t
  26. do_fork()
  27. {
  28. if (daemon(1, 1))
  29. fatal(strerror(errno), 0);
  30. pid_t pid = getpid();
  31. writepid(conf.bot->pid_file, pid);
  32. lastfork = now;
  33. #ifndef CYGWIN_HACKS
  34. if (conf.watcher)
  35. init_watcher(pid);
  36. #endif /* !CYGWIN_HACKS */
  37. return pid;
  38. }
  39. void
  40. writepid(const char *pidfile, pid_t pid)
  41. {
  42. FILE *fp = NULL;
  43. sdprintf("Writing pid to: %s", pidfile);
  44. /* Need to attempt to write pid now, not later. */
  45. unlink(pidfile);
  46. if ((fp = fopen(pidfile, "w"))) {
  47. fprintf(fp, "%u\n", pid);
  48. if (fflush(fp)) {
  49. /* Kill bot incase a botchk is run from crond. */
  50. printf(EGG_NOWRITE, pidfile);
  51. printf(" Try freeing some disk space\n");
  52. fclose(fp);
  53. unlink(pidfile);
  54. exit(1);
  55. } else
  56. fclose(fp);
  57. } else
  58. printf(EGG_NOWRITE, pidfile);
  59. }
  60. #ifndef CYGWIN_HACKS
  61. static void
  62. init_watcher(pid_t parent)
  63. {
  64. int x = fork();
  65. if (x == -1)
  66. fatal("Could not fork off a watcher process", 0);
  67. if (x != 0) { /* parent [bot] */
  68. watcher = x;
  69. /* printf("WATCHER: %d\n", watcher); */
  70. return;
  71. } else { /* child [watcher] */
  72. watcher = getpid();
  73. /* printf("MY PARENT: %d\n", parent); */
  74. /* printf("my pid: %d\n", watcher); */
  75. if (ptrace(PT_ATTACH, parent, 0, 0) == -1)
  76. fatal("Cannot attach to parent", 0);
  77. while (1) {
  78. int status = 0, sig = 0, ret = 0;
  79. waitpid(parent, &status, 0);
  80. sig = WSTOPSIG(status);
  81. if (sig) {
  82. ret = ptrace(PT_CONTINUE, parent, (char *) 1, sig);
  83. if (ret == -1) /* send the signal! */
  84. fatal("Could not send signal to parent", 0);
  85. /* printf("Sent signal %s (%d) to parent\n", strsignal(sig), sig); */
  86. } else {
  87. ret = ptrace(PT_CONTINUE, parent, (char *) 1, 0);
  88. if (ret == -1) {
  89. if (errno == ESRCH) /* parent is gone! */
  90. exit(0); /* just exit */
  91. else
  92. fatal("Could not continue parent", 0);
  93. }
  94. }
  95. }
  96. }
  97. }
  98. #endif /* !CYGWIN_HACKS */