bg.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * bg.c -- handles:
  3. * moving the process to the background, i.e. forking, while keeping threads
  4. * happy.
  5. *
  6. */
  7. #include "main.h"
  8. #include <signal.h>
  9. extern char pid_file[], botnetnick[];
  10. /* Do everything we normally do after we have split off a new
  11. * process to the background. This includes writing a PID file
  12. * and informing the user of the split.
  13. */
  14. static void bg_do_detach(pid_t p)
  15. {
  16. FILE *fp;
  17. /* Need to attempt to write pid now, not later. */
  18. unlink(pid_file);
  19. fp = fopen(pid_file, "w");
  20. if (fp != NULL) {
  21. fprintf(fp, "%u\n", p);
  22. if (fflush(fp)) {
  23. /* Kill bot incase a botchk is run from crond. */
  24. printf(EGG_NOWRITE, pid_file);
  25. printf(" Try freeing some disk space\n");
  26. fclose(fp);
  27. unlink(pid_file);
  28. exit(1);
  29. }
  30. fclose(fp);
  31. } else
  32. printf(EGG_NOWRITE, pid_file);
  33. #ifdef HUB
  34. printf("Launched into the background (pid: %d)\n\n", p);
  35. #else
  36. printf("%s launched into the background (pid: %d ppid: %d)\n\n", botnetnick, p, getpid());
  37. #endif
  38. #if HAVE_SETPGID
  39. setpgid(p, p);
  40. #endif
  41. exit(0);
  42. }
  43. void bg_do_split(void)
  44. {
  45. /* Split off a new process.
  46. */
  47. int xx = fork();
  48. if (xx == -1)
  49. fatal("CANNOT FORK PROCESS.", 0);
  50. if (xx != 0)
  51. bg_do_detach(xx);
  52. }