1
2 /*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9
10
11 ngx_int_t ngx_daemon(ngx_log_t *log)
12 {
13 int fd;
14
15 switch (fork()) {
16 case -1:
17 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "fork() failed");
18 return NGX_ERROR;
19
20 case 0:
21 break;
22
23 default:
24 exit(0);
25 }
26
27 ngx_pid = ngx_getpid();
28
29 if (setsid() == -1) {
30 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "setsid() failed");
31 return NGX_ERROR;
32 }
33
34 umask(0);
35
36 fd = open("/dev/null", O_RDWR);
37 if (fd == -1) {
38 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
39 "open(\"/dev/null\") failed");
40 return NGX_ERROR;
41 }
42
43 if (dup2(fd, STDIN_FILENO) == -1) {
44 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDIN) failed");
45 return NGX_ERROR;
46 }
47
48 if (dup2(fd, STDOUT_FILENO) == -1) {
49 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDOUT) failed");
50 return NGX_ERROR;
51 }
52
53 #if 0
54 if (dup2(fd, STDERR_FILENO) == -1) {
55 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDERR) failed");
56 return NGX_ERROR;
57 }
58 #endif
59
60 if (fd > STDERR_FILENO) {
61 if (close(fd) == -1) {
62 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() failed");
63 return NGX_ERROR;
64 }
65 }
66
67 return NGX_OK;
68 }
69
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.