aboutsummaryrefslogtreecommitdiff
path: root/src/os/unix/ngx_daemon.c
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2003-05-12 15:52:24 +0000
committerIgor Sysoev <igor@sysoev.ru>2003-05-12 15:52:24 +0000
commit6b863e353d54420c323d67f86aad0b90ba04e316 (patch)
tree51d13ab529d2605be3995333d71344c917c5c4f4 /src/os/unix/ngx_daemon.c
parent4fe262b6821a461b3dbb3d6bfd05a8f713157524 (diff)
downloadnginx-6b863e353d54420c323d67f86aad0b90ba04e316.tar.gz
nginx-6b863e353d54420c323d67f86aad0b90ba04e316.zip
nginx-0.0.1-2003-05-12-19:52:24 import
Diffstat (limited to 'src/os/unix/ngx_daemon.c')
-rw-r--r--src/os/unix/ngx_daemon.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/os/unix/ngx_daemon.c b/src/os/unix/ngx_daemon.c
new file mode 100644
index 000000000..6faf85b06
--- /dev/null
+++ b/src/os/unix/ngx_daemon.c
@@ -0,0 +1,80 @@
+
+#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:
+ ngx_log_error(NGX_LOG_ALERT, log, errno, "fork() failed");
+ return NGX_ERROR;
+
+ case 0:
+ break;
+
+ default:
+ exit(0);
+ }
+
+ if (setsid() == -1) {
+ ngx_log_error(NGX_LOG_ALERT, log, errno, "setsid() failed");
+ return NGX_ERROR;
+ }
+
+#if (__SVR4 || linux)
+
+ /* need HUP IGN ? check in Solaris and Linux */
+
+ switch (fork()) {
+ case -1:
+ ngx_log_error(NGX_LOG_ALERT, log, errno, "fork() failed");
+ return NGX_ERROR;
+
+ case 0:
+ break;
+
+ default:
+ exit(0);
+ }
+
+#endif
+
+ umask(0);
+
+#if 0
+ fd = open("/dev/null", O_RDWR);
+ if (fd == -1) {
+ ngx_log_error(NGX_LOG_ALERT, log, errno, "open(\"/dev/null\") failed");
+ return NGX_ERROR;
+ }
+
+ if (dup2(fd, STDIN_FILENO) == -1) {
+ ngx_log_error(NGX_LOG_ALERT, log, errno, "dup2(STDIN) failed");
+ return NGX_ERROR;
+ }
+
+ if (dup2(fd, STDOUT_FILENO) == -1) {
+ ngx_log_error(NGX_LOG_ALERT, log, errno, "dup2(STDOUT) failed");
+ return NGX_ERROR;
+ }
+
+ if (dup2(fd, STDERR_FILENO) == -1) {
+ ngx_log_error(NGX_LOG_ALERT, log, errno, "dup2(STDERR) failed");
+ return NGX_ERROR;
+ }
+
+ if (fd > STDERR_FILENO) {
+ if (close(fd) == -1) {
+ ngx_log_error(NGX_LOG_ALERT, log, errno, "close() failed");
+ return NGX_ERROR;
+ }
+ }
+#endif
+
+ return NGX_OK;
+}