bg.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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(int nochdir, int noclose)
  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. if (!nochdir)
  71. (void) chdir("/");
  72. if (!noclose)
  73. close_tty();
  74. return (0);
  75. }
  76. pid_t
  77. do_fork()
  78. {
  79. if (my_daemon(1, 1))
  80. fatal(strerror(errno), 0);
  81. pid_t pid = getpid();
  82. writepid(conf.bot->pid_file, pid);
  83. lastfork = now;
  84. return pid;
  85. }
  86. void
  87. writepid(const char *pidfile, pid_t pid)
  88. {
  89. FILE *fp = NULL;
  90. sdprintf("Writing pid to: %s", pidfile);
  91. /* Need to attempt to write pid now, not later. */
  92. unlink(pidfile);
  93. if ((fp = fopen(pidfile, "w"))) {
  94. fprintf(fp, "%u\n", pid);
  95. if (fflush(fp)) {
  96. /* Kill bot incase a botchk is run from crond. */
  97. printf("* Warning! Could not write %s file!\n", pidfile);
  98. printf(" Try freeing some disk space\n");
  99. fclose(fp);
  100. unlink(pidfile);
  101. exit(1);
  102. } else
  103. fclose(fp);
  104. } else
  105. printf("* Warning! Could not write %s file!\n", pidfile);
  106. }