Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.15.12 ]​[ nginx-1.16.0 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 
0011 
0012 ngx_int_t
0013 ngx_daemon(ngx_log_t *log)
0014 {
0015     int  fd;
0016 
0017     switch (fork()) {
0018     case -1:
0019         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "fork() failed");
0020         return NGX_ERROR;
0021 
0022     case 0:
0023         break;
0024 
0025     default:
0026         exit(0);
0027     }
0028 
0029     ngx_parent = ngx_pid;
0030     ngx_pid = ngx_getpid();
0031 
0032     if (setsid() == -1) {
0033         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "setsid() failed");
0034         return NGX_ERROR;
0035     }
0036 
0037     umask(0);
0038 
0039     fd = open("/dev/null", O_RDWR);
0040     if (fd == -1) {
0041         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
0042                       "open(\"/dev/null\") failed");
0043         return NGX_ERROR;
0044     }
0045 
0046     if (dup2(fd, STDIN_FILENO) == -1) {
0047         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDIN) failed");
0048         return NGX_ERROR;
0049     }
0050 
0051     if (dup2(fd, STDOUT_FILENO) == -1) {
0052         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDOUT) failed");
0053         return NGX_ERROR;
0054     }
0055 
0056 #if 0
0057     if (dup2(fd, STDERR_FILENO) == -1) {
0058         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDERR) failed");
0059         return NGX_ERROR;
0060     }
0061 #endif
0062 
0063     if (fd > STDERR_FILENO) {
0064         if (close(fd) == -1) {
0065             ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() failed");
0066             return NGX_ERROR;
0067         }
0068     }
0069 
0070     return NGX_OK;
0071 }