bg.cc 2.5 KB

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