nginx/src/os/unix/ngx_daemon.c

81 lines
1.5 KiB
C
Raw Normal View History

2003-05-12 23:52:24 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_log.h>
/* daemon in Linux */
int ngx_daemon(ngx_log_t *log)
{
int fd;
switch (fork()) {
case -1:
2003-05-20 23:37:55 +08:00
ngx_log_error(NGX_LOG_EMERG, log, errno, "fork() failed");
2003-05-12 23:52:24 +08:00
return NGX_ERROR;
case 0:
break;
default:
exit(0);
}
if (setsid() == -1) {
2003-05-20 23:37:55 +08:00
ngx_log_error(NGX_LOG_EMERG, log, errno, "setsid() failed");
2003-05-12 23:52:24 +08:00
return NGX_ERROR;
}
#if (__SVR4 || linux)
/* need HUP IGN ? check in Solaris and Linux */
switch (fork()) {
case -1:
2003-05-20 23:37:55 +08:00
ngx_log_error(NGX_LOG_EMERG, log, errno, "fork() failed");
2003-05-12 23:52:24 +08:00
return NGX_ERROR;
case 0:
break;
default:
exit(0);
}
#endif
umask(0);
#if 0
fd = open("/dev/null", O_RDWR);
if (fd == -1) {
2003-05-20 23:37:55 +08:00
ngx_log_error(NGX_LOG_EMERG, log, errno, "open(\"/dev/null\") failed");
2003-05-12 23:52:24 +08:00
return NGX_ERROR;
}
if (dup2(fd, STDIN_FILENO) == -1) {
2003-05-20 23:37:55 +08:00
ngx_log_error(NGX_LOG_EMERG, log, errno, "dup2(STDIN) failed");
2003-05-12 23:52:24 +08:00
return NGX_ERROR;
}
if (dup2(fd, STDOUT_FILENO) == -1) {
2003-05-20 23:37:55 +08:00
ngx_log_error(NGX_LOG_EMERG, log, errno, "dup2(STDOUT) failed");
2003-05-12 23:52:24 +08:00
return NGX_ERROR;
}
if (dup2(fd, STDERR_FILENO) == -1) {
2003-05-20 23:37:55 +08:00
ngx_log_error(NGX_LOG_EMERG, log, errno, "dup2(STDERR) failed");
2003-05-12 23:52:24 +08:00
return NGX_ERROR;
}
if (fd > STDERR_FILENO) {
if (close(fd) == -1) {
2003-05-20 23:37:55 +08:00
ngx_log_error(NGX_LOG_EMERG, log, errno, "close() failed");
2003-05-12 23:52:24 +08:00
return NGX_ERROR;
}
}
#endif
return NGX_OK;
}