bg.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2013 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. #include <sys/wait.h>
  35. #include <sys/types.h>
  36. #include <errno.h>
  37. #include <unistd.h>
  38. #include <string.h>
  39. #include <fcntl.h>
  40. #include <unistd.h>
  41. #include <sys/stat.h>
  42. time_t lastfork = 0;
  43. int close_tty()
  44. {
  45. int fd = -1;
  46. if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
  47. dup2(fd, STDIN_FILENO);
  48. dup2(fd, STDOUT_FILENO);
  49. dup2(fd, STDERR_FILENO);
  50. if (fd > 2)
  51. close(fd);
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. static int my_daemon()
  57. {
  58. switch (fork()) {
  59. case -1:
  60. return (-1);
  61. case 0:
  62. break;
  63. default:
  64. _exit(0);
  65. }
  66. if (setsid() == -1)
  67. return (-1);
  68. return (0);
  69. }
  70. pid_t
  71. do_fork()
  72. {
  73. if (my_daemon())
  74. fatal(strerror(errno), 0);
  75. pid_t pid = getpid();
  76. writepid(conf.bot->pid_file, pid);
  77. lastfork = now;
  78. return pid;
  79. }
  80. void
  81. writepid(const char *pidfile, pid_t pid)
  82. {
  83. FILE *fp = NULL;
  84. sdprintf("Writing pid to: %s", pidfile);
  85. /* Need to attempt to write pid now, not later. */
  86. unlink(pidfile);
  87. if ((fp = fopen(pidfile, "w"))) {
  88. fprintf(fp, "%u\n", pid);
  89. if (fflush(fp)) {
  90. /* Kill bot incase a botchk is run from crond. */
  91. printf("* Warning! Could not write %s file!\n", pidfile);
  92. printf(" Try freeing some disk space\n");
  93. fclose(fp);
  94. unlink(pidfile);
  95. exit(1);
  96. } else
  97. fclose(fp);
  98. } else
  99. printf("* Warning! Could not write %s file!\n", pidfile);
  100. }