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.

129 lines
2.1 KiB

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 <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <syslog.h>
  14. #include <string.h>
  15. #include <signal.h>
  16. #include "daemonize.h"
  17. #include "config.h"
  18. #ifndef USE_DAEMON
  19. int
  20. daemonize(void)
  21. {
  22. int pid, i;
  23. switch(fork())
  24. {
  25. /* fork error */
  26. case -1:
  27. perror("fork()");
  28. exit(1);
  29. /* child process */
  30. case 0:
  31. /* obtain a new process group */
  32. if( (pid = setsid()) < 0)
  33. {
  34. perror("setsid()");
  35. exit(1);
  36. }
  37. /* close all descriptors */
  38. for (i=getdtablesize();i>=0;--i) close(i);
  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:
  47. exit(0);
  48. }
  49. }
  50. #endif
  51. int
  52. writepidfile(const char * fname, int pid)
  53. {
  54. char pidstring[16];
  55. int pidstringlen;
  56. int pidfile;
  57. if(!fname || (strlen(fname) == 0))
  58. return -1;
  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,
  68. "Unable to write to pidfile %s: snprintf(): FAILED", fname);
  69. close(pidfile);
  70. return -1;
  71. }
  72. else
  73. {
  74. if(write(pidfile, pidstring, pidstringlen) < 0)
  75. syslog(LOG_ERR, "Unable to write to pidfile %s: %m", fname);
  76. }
  77. close(pidfile);
  78. return 0;
  79. }
  80. int
  81. checkforrunning(const char * fname)
  82. {
  83. char buffer[64];
  84. int pidfile;
  85. pid_t pid;
  86. if(!fname || (strlen(fname) == 0))
  87. return -1;
  88. if( (pidfile = open(fname, O_RDONLY)) < 0)
  89. return 0;
  90. memset(buffer, 0, 64);
  91. if(read(pidfile, buffer, 63))
  92. {
  93. if( (pid = atol(buffer)) > 0)
  94. {
  95. if(!kill(pid, 0))
  96. {
  97. close(pidfile);
  98. return -2;
  99. }
  100. }
  101. }
  102. close(pidfile);
  103. return 0;
  104. }