bg.cc 2.5 KB

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