You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
2.2 KiB

7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
7 years ago
​
7 years ago
7 years ago
​
7 years ago
​
​
​
7 years ago
​
​
7 years ago
​
7 years ago
7 years ago
7 years ago
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
7 years ago
7 years ago
​
7 years ago
7 years ago
​
​
7 years ago
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
  1. /* $Id: daemonize.c,v 1.13 2012/03/05 20:36:15 nanard Exp $ */
  2. /* MiniUPnP project
  3. * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
  4. * (c) 2006 Thomas Bernard
  5. * This software is subject to the conditions detailed
  6. * in the LICENCE file provided within the distribution */
  7. #include "daemonize.h"
  8. #include "config.h"
  9. #include <fcntl.h>
  10. #include <signal.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <sys/stat.h>
  15. #include <sys/types.h>
  16. #include <syslog.h>
  17. #include <unistd.h>
  18. #ifndef USE_DAEMON
  19. int daemonize(void)
  20. {
  21. int pid, i;
  22. switch (fork())
  23. {
  24. /* fork error */
  25. case -1: perror("fork()"); exit(1);
  26. /* child process */
  27. case 0:
  28. /* obtain a new process group */
  29. if ((pid = setsid()) < 0)
  30. {
  31. perror("setsid()");
  32. exit(1);
  33. }
  34. /* close all descriptors */
  35. for (i = getdtablesize(); i >= 0; --i)
  36. {
  37. close(i);
  38. }
  39. i = open("/dev/null", O_RDWR); /* open stdin */
  40. dup(i); /* stdout */
  41. dup(i); /* stderr */
  42. umask(027);
  43. chdir("/"); /* chdir to /tmp ? */
  44. return pid;
  45. /* parent process */
  46. default: exit(0);
  47. }
  48. }
  49. #endif
  50. int writepidfile(const char * fname, int pid)
  51. {
  52. char pidstring[16];
  53. int pidstringlen;
  54. int pidfile;
  55. if (! fname || (strlen(fname) == 0))
  56. {
  57. return -1;
  58. }
  59. if ((pidfile = open(fname, O_WRONLY | O_CREAT, 0644)) < 0)
  60. {
  61. syslog(LOG_ERR, "Unable to open pidfile for writing %s: %m", fname);
  62. return -1;
  63. }
  64. pidstringlen = snprintf(pidstring, sizeof(pidstring), "%d\n", pid);
  65. if (pidstringlen <= 0)
  66. {
  67. syslog(LOG_ERR, "Unable to write to pidfile %s: snprintf(): FAILED", fname);
  68. close(pidfile);
  69. return -1;
  70. }
  71. else
  72. {
  73. if (write(pidfile, pidstring, pidstringlen) < 0)
  74. {
  75. syslog(LOG_ERR, "Unable to write to pidfile %s: %m", fname);
  76. }
  77. }
  78. close(pidfile);
  79. return 0;
  80. }
  81. int checkforrunning(const char * fname)
  82. {
  83. char buffer[64];
  84. int pidfile;
  85. pid_t pid;
  86. if (! fname || (strlen(fname) == 0))
  87. {
  88. return -1;
  89. }
  90. if ((pidfile = open(fname, O_RDONLY)) < 0)
  91. {
  92. return 0;
  93. }
  94. memset(buffer, 0, 64);
  95. if (read(pidfile, buffer, 63))
  96. {
  97. if ((pid = atol(buffer)) > 0)
  98. {
  99. if (! kill(pid, 0))
  100. {
  101. close(pidfile);
  102. return -2;
  103. }
  104. }
  105. }
  106. close(pidfile);
  107. return 0;
  108. }