2003-05-12 23:52:24 +08:00
|
|
|
|
2004-09-28 16:34:51 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-09-28 16:34:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-05-12 23:52:24 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
|
|
|
|
|
|
|
|
int ngx_daemon(ngx_log_t *log)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
2003-11-20 00:26:41 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "fork() failed");
|
2003-05-12 23:52:24 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2004-02-04 00:43:54 +08:00
|
|
|
ngx_pid = ngx_getpid();
|
|
|
|
|
2003-05-12 23:52:24 +08:00
|
|
|
if (setsid() == -1) {
|
2003-11-20 00:26:41 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "setsid() failed");
|
2003-05-12 23:52:24 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
umask(0);
|
|
|
|
|
|
|
|
fd = open("/dev/null", O_RDWR);
|
|
|
|
if (fd == -1) {
|
2003-11-20 00:26:41 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
|
|
|
|
"open(\"/dev/null\") failed");
|
2003-05-12 23:52:24 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dup2(fd, STDIN_FILENO) == -1) {
|
2003-11-20 00:26:41 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDIN) failed");
|
2003-05-12 23:52:24 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dup2(fd, STDOUT_FILENO) == -1) {
|
2003-11-20 00:26:41 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDOUT) failed");
|
2003-05-12 23:52:24 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-06-16 02:32:13 +08:00
|
|
|
#if 0
|
2003-05-12 23:52:24 +08:00
|
|
|
if (dup2(fd, STDERR_FILENO) == -1) {
|
2003-11-20 00:26:41 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDERR) failed");
|
2003-05-12 23:52:24 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2003-06-16 02:32:13 +08:00
|
|
|
#endif
|
2003-05-12 23:52:24 +08:00
|
|
|
|
|
|
|
if (fd > STDERR_FILENO) {
|
|
|
|
if (close(fd) == -1) {
|
2003-11-20 00:26:41 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() failed");
|
2003-05-12 23:52:24 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|