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