120-daemonize_fix.patch 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. --- a/src/utils/os_unix.c
  2. +++ b/src/utils/os_unix.c
  3. @@ -10,6 +10,7 @@
  4. #include <time.h>
  5. #include <sys/wait.h>
  6. +#include <fcntl.h>
  7. #ifdef ANDROID
  8. #include <sys/capability.h>
  9. @@ -179,59 +180,46 @@ int os_gmtime(os_time_t t, struct os_tm
  10. return 0;
  11. }
  12. -
  13. -#ifdef __APPLE__
  14. -#include <fcntl.h>
  15. -static int os_daemon(int nochdir, int noclose)
  16. +int os_daemonize(const char *pid_file)
  17. {
  18. - int devnull;
  19. + int pid = 0, i, devnull;
  20. - if (chdir("/") < 0)
  21. - return -1;
  22. +#if defined(__uClinux__) || defined(__sun__)
  23. + return -1;
  24. +#else /* defined(__uClinux__) || defined(__sun__) */
  25. - devnull = open("/dev/null", O_RDWR);
  26. - if (devnull < 0)
  27. +#ifndef __APPLE__
  28. + pid = fork();
  29. + if (pid < 0)
  30. return -1;
  31. +#endif
  32. - if (dup2(devnull, STDIN_FILENO) < 0) {
  33. - close(devnull);
  34. - return -1;
  35. + if (pid > 0) {
  36. + if (pid_file) {
  37. + FILE *f = fopen(pid_file, "w");
  38. + if (f) {
  39. + fprintf(f, "%u\n", pid);
  40. + fclose(f);
  41. + }
  42. + }
  43. + _exit(0);
  44. }
  45. - if (dup2(devnull, STDOUT_FILENO) < 0) {
  46. - close(devnull);
  47. + if (setsid() < 0)
  48. return -1;
  49. - }
  50. - if (dup2(devnull, STDERR_FILENO) < 0) {
  51. - close(devnull);
  52. + if (chdir("/") < 0)
  53. return -1;
  54. - }
  55. - return 0;
  56. -}
  57. -#else /* __APPLE__ */
  58. -#define os_daemon daemon
  59. -#endif /* __APPLE__ */
  60. -
  61. -
  62. -int os_daemonize(const char *pid_file)
  63. -{
  64. -#if defined(__uClinux__) || defined(__sun__)
  65. - return -1;
  66. -#else /* defined(__uClinux__) || defined(__sun__) */
  67. - if (os_daemon(0, 0)) {
  68. - perror("daemon");
  69. + devnull = open("/dev/null", O_RDWR);
  70. + if (devnull < 0)
  71. return -1;
  72. - }
  73. - if (pid_file) {
  74. - FILE *f = fopen(pid_file, "w");
  75. - if (f) {
  76. - fprintf(f, "%u\n", getpid());
  77. - fclose(f);
  78. - }
  79. - }
  80. + for (i = 0; i <= STDERR_FILENO; i++)
  81. + dup2(devnull, i);
  82. +
  83. + if (devnull > 2)
  84. + close(devnull);
  85. return -0;
  86. #endif /* defined(__uClinux__) || defined(__sun__) */