comparison src/os/unix/ngx_daemon.c @ 86:3973260705cc

nginx-0.0.1-2003-05-12-19:52:24 import
author Igor Sysoev <igor@sysoev.ru>
date Mon, 12 May 2003 15:52:24 +0000
parents
children 19cc647ecd91
comparison
equal deleted inserted replaced
85:3549c2bf9eaf 86:3973260705cc
1
2 #include <ngx_config.h>
3 #include <ngx_core.h>
4 #include <ngx_log.h>
5
6 /* daemon in Linux */
7
8 int ngx_daemon(ngx_log_t *log)
9 {
10 int fd;
11
12 switch (fork()) {
13 case -1:
14 ngx_log_error(NGX_LOG_ALERT, log, errno, "fork() failed");
15 return NGX_ERROR;
16
17 case 0:
18 break;
19
20 default:
21 exit(0);
22 }
23
24 if (setsid() == -1) {
25 ngx_log_error(NGX_LOG_ALERT, log, errno, "setsid() failed");
26 return NGX_ERROR;
27 }
28
29 #if (__SVR4 || linux)
30
31 /* need HUP IGN ? check in Solaris and Linux */
32
33 switch (fork()) {
34 case -1:
35 ngx_log_error(NGX_LOG_ALERT, log, errno, "fork() failed");
36 return NGX_ERROR;
37
38 case 0:
39 break;
40
41 default:
42 exit(0);
43 }
44
45 #endif
46
47 umask(0);
48
49 #if 0
50 fd = open("/dev/null", O_RDWR);
51 if (fd == -1) {
52 ngx_log_error(NGX_LOG_ALERT, log, errno, "open(\"/dev/null\") failed");
53 return NGX_ERROR;
54 }
55
56 if (dup2(fd, STDIN_FILENO) == -1) {
57 ngx_log_error(NGX_LOG_ALERT, log, errno, "dup2(STDIN) failed");
58 return NGX_ERROR;
59 }
60
61 if (dup2(fd, STDOUT_FILENO) == -1) {
62 ngx_log_error(NGX_LOG_ALERT, log, errno, "dup2(STDOUT) failed");
63 return NGX_ERROR;
64 }
65
66 if (dup2(fd, STDERR_FILENO) == -1) {
67 ngx_log_error(NGX_LOG_ALERT, log, errno, "dup2(STDERR) failed");
68 return NGX_ERROR;
69 }
70
71 if (fd > STDERR_FILENO) {
72 if (close(fd) == -1) {
73 ngx_log_error(NGX_LOG_ALERT, log, errno, "close() failed");
74 return NGX_ERROR;
75 }
76 }
77 #endif
78
79 return NGX_OK;
80 }