bg.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * bg.c -- handles:
  22. * moving the process to the background, i.e. forking, while keeping threads
  23. * happy.
  24. *
  25. */
  26. #include "common.h"
  27. #include "bg.h"
  28. #include "thread.h"
  29. #include "main.h"
  30. #include <signal.h>
  31. #ifdef HAVE_SYS_PTRACE_H
  32. # include <sys/ptrace.h>
  33. #endif /* HAVE_SYS_PTRACE_H */
  34. #ifndef CYGWIN_HACKS
  35. # include <sys/wait.h>
  36. #endif /* !CYGWIN_HACKS */
  37. #include <sys/types.h>
  38. #include <errno.h>
  39. #include <unistd.h>
  40. #include <string.h>
  41. #include <fcntl.h>
  42. #include <unistd.h>
  43. #include <sys/stat.h>
  44. time_t lastfork = 0;
  45. int close_tty()
  46. {
  47. int fd = -1;
  48. if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
  49. dup2(fd, STDIN_FILENO);
  50. dup2(fd, STDOUT_FILENO);
  51. dup2(fd, STDERR_FILENO);
  52. if (fd > 2)
  53. close(fd);
  54. return 1;
  55. }
  56. return 0;
  57. }
  58. static int my_daemon()
  59. {
  60. switch (fork()) {
  61. case -1:
  62. return (-1);
  63. case 0:
  64. break;
  65. default:
  66. _exit(0);
  67. }
  68. if (setsid() == -1)
  69. return (-1);
  70. return (0);
  71. }
  72. pid_t
  73. do_fork()
  74. {
  75. if (my_daemon())
  76. fatal(strerror(errno), 0);
  77. pid_t pid = getpid();
  78. writepid(conf.bot->pid_file, pid);
  79. lastfork = now;
  80. return pid;
  81. }
  82. void
  83. writepid(const char *pidfile, pid_t pid)
  84. {
  85. FILE *fp = NULL;
  86. sdprintf("Writing pid to: %s", pidfile);
  87. /* Need to attempt to write pid now, not later. */
  88. unlink(pidfile);
  89. if ((fp = fopen(pidfile, "w"))) {
  90. fprintf(fp, "%u\n", pid);
  91. if (fflush(fp)) {
  92. /* Kill bot incase a botchk is run from crond. */
  93. printf("* Warning! Could not write %s file!\n", pidfile);
  94. printf(" Try freeing some disk space\n");
  95. fclose(fp);
  96. unlink(pidfile);
  97. exit(1);
  98. } else
  99. fclose(fp);
  100. } else
  101. printf("* Warning! Could not write %s file!\n", pidfile);
  102. }