2013-12-02 13:08:08 +00:00
|
|
|
--- a/src/utils/os_unix.c
|
|
|
|
+++ b/src/utils/os_unix.c
|
2014-10-17 06:15:35 +00:00
|
|
|
@@ -10,6 +10,7 @@
|
2013-12-02 13:08:08 +00:00
|
|
|
|
|
|
|
#include <time.h>
|
2014-10-17 06:15:35 +00:00
|
|
|
#include <sys/wait.h>
|
2013-12-02 13:08:08 +00:00
|
|
|
+#include <fcntl.h>
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
#include <sys/capability.h>
|
2015-04-23 08:01:51 +00:00
|
|
|
@@ -155,59 +156,46 @@ int os_gmtime(os_time_t t, struct os_tm
|
2013-12-02 13:08:08 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
-#ifdef __APPLE__
|
|
|
|
-#include <fcntl.h>
|
|
|
|
-static int os_daemon(int nochdir, int noclose)
|
|
|
|
+int os_daemonize(const char *pid_file)
|
|
|
|
{
|
|
|
|
- int devnull;
|
|
|
|
+ int pid = 0, i, devnull;
|
|
|
|
|
|
|
|
- if (chdir("/") < 0)
|
|
|
|
- return -1;
|
|
|
|
+#if defined(__uClinux__) || defined(__sun__)
|
|
|
|
+ return -1;
|
|
|
|
+#else /* defined(__uClinux__) || defined(__sun__) */
|
|
|
|
|
|
|
|
- devnull = open("/dev/null", O_RDWR);
|
|
|
|
- if (devnull < 0)
|
|
|
|
+#ifndef __APPLE__
|
|
|
|
+ pid = fork();
|
|
|
|
+ if (pid < 0)
|
|
|
|
return -1;
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
- if (dup2(devnull, STDIN_FILENO) < 0) {
|
|
|
|
- close(devnull);
|
|
|
|
- return -1;
|
|
|
|
+ if (pid > 0) {
|
|
|
|
+ if (pid_file) {
|
|
|
|
+ FILE *f = fopen(pid_file, "w");
|
|
|
|
+ if (f) {
|
|
|
|
+ fprintf(f, "%u\n", pid);
|
|
|
|
+ fclose(f);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ _exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
- if (dup2(devnull, STDOUT_FILENO) < 0) {
|
|
|
|
- close(devnull);
|
|
|
|
+ if (setsid() < 0)
|
|
|
|
return -1;
|
|
|
|
- }
|
|
|
|
|
|
|
|
- if (dup2(devnull, STDERR_FILENO) < 0) {
|
|
|
|
- close(devnull);
|
|
|
|
+ if (chdir("/") < 0)
|
|
|
|
return -1;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return 0;
|
|
|
|
-}
|
|
|
|
-#else /* __APPLE__ */
|
|
|
|
-#define os_daemon daemon
|
|
|
|
-#endif /* __APPLE__ */
|
2015-03-26 23:33:47 +00:00
|
|
|
|
2015-04-23 08:01:51 +00:00
|
|
|
-
|
2013-12-02 13:08:08 +00:00
|
|
|
-int os_daemonize(const char *pid_file)
|
|
|
|
-{
|
|
|
|
-#if defined(__uClinux__) || defined(__sun__)
|
|
|
|
- return -1;
|
|
|
|
-#else /* defined(__uClinux__) || defined(__sun__) */
|
|
|
|
- if (os_daemon(0, 0)) {
|
|
|
|
- perror("daemon");
|
|
|
|
+ devnull = open("/dev/null", O_RDWR);
|
|
|
|
+ if (devnull < 0)
|
|
|
|
return -1;
|
|
|
|
- }
|
|
|
|
|
|
|
|
- if (pid_file) {
|
|
|
|
- FILE *f = fopen(pid_file, "w");
|
|
|
|
- if (f) {
|
|
|
|
- fprintf(f, "%u\n", getpid());
|
|
|
|
- fclose(f);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
+ for (i = 0; i <= STDERR_FILENO; i++)
|
|
|
|
+ dup2(devnull, i);
|
|
|
|
+
|
|
|
|
+ if (devnull > 2)
|
|
|
|
+ close(devnull);
|
|
|
|
|
|
|
|
return -0;
|
|
|
|
#endif /* defined(__uClinux__) || defined(__sun__) */
|