[PATCH] Core: PID file writing synchronization

Maxim Dounin mdounin at mdounin.ru
Wed May 8 01:14:30 UTC 2024


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1715130855 -10800
#      Wed May 08 04:14:15 2024 +0300
# Node ID 06cfb3c166612afb9c7275762ab8e48d50024c20
# Parent  72cfccd6d587eb0b0e8f8c3c85e6bcecdc246cfe
Core: PID file writing synchronization.

Now, ngx_daemon() does not call exit() in the parent process immediately,
but instead waits for the child process to signal it actually started (and
wrote the PID file if configured to).  This ensures that the PID file
already exists when the parent process exits.

To make sure that signal handlers won't cause unexpected logging in the
parent process if the child process dies (for example, due to errors when
writing the PID file), ngx_init_signals() is moved to the child process.

This resolves "PID file ... not readable (yet?) after start" and "Failed
to parse PID from file..." errors as observed with systemd.

Note that the errors observed are considered to be a bug in systemd, which
isn't able to work properly with traditional Unix daemons.  Still, the
workaround is implemented to make sure there will be no OS vendor patches
trying to address this.

diff --git a/src/core/nginx.c b/src/core/nginx.c
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -342,10 +342,6 @@ main(int argc, char *const *argv)
 
 #if !(NGX_WIN32)
 
-    if (ngx_init_signals(cycle->log) != NGX_OK) {
-        return 1;
-    }
-
     if (!ngx_inherited && ccf->daemon) {
         if (ngx_daemon(cycle->log) != NGX_OK) {
             return 1;
@@ -364,6 +360,18 @@ main(int argc, char *const *argv)
         return 1;
     }
 
+#if !(NGX_WIN32)
+
+    if (ngx_init_signals(cycle->log) != NGX_OK) {
+        return 1;
+    }
+
+    if (ngx_daemon_sync(cycle->log) != NGX_OK) {
+        return 1;
+    }
+
+#endif
+
     if (ngx_log_redirect_stderr(cycle) != NGX_OK) {
         return 1;
     }
diff --git a/src/os/unix/ngx_daemon.c b/src/os/unix/ngx_daemon.c
--- a/src/os/unix/ngx_daemon.c
+++ b/src/os/unix/ngx_daemon.c
@@ -9,10 +9,20 @@
 #include <ngx_core.h>
 
 
+static ngx_fd_t  ngx_daemon_fd = NGX_INVALID_FILE;
+
+
 ngx_int_t
 ngx_daemon(ngx_log_t *log)
 {
-    int  fd;
+    u_char    buf[1];
+    ssize_t   n;
+    ngx_fd_t  fd, pp[2];
+
+    if (pipe(pp) == -1) {
+        ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "pipe() failed");
+        return NGX_ERROR;
+    }
 
     switch (fork()) {
     case -1:
@@ -20,9 +30,38 @@ ngx_daemon(ngx_log_t *log)
         return NGX_ERROR;
 
     case 0:
+        if (close(pp[0]) == -1) {
+            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() pipe failed");
+            return NGX_ERROR;
+        }
+
+        ngx_daemon_fd = pp[1];
         break;
 
     default:
+        if (close(pp[1]) == -1) {
+            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() pipe failed");
+            return NGX_ERROR;
+        }
+
+        n = read(pp[0], buf, 1);
+
+        if (n == 0) {
+            /* child exited */
+            return NGX_ERROR;
+        }
+
+        if (n != 1) {
+            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
+                          "read() pipe failed");
+            return NGX_ERROR;
+        }
+
+        if (close(pp[0]) == -1) {
+            ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() pipe failed");
+            return NGX_ERROR;
+        }
+
         exit(0);
     }
 
@@ -69,3 +108,26 @@ ngx_daemon(ngx_log_t *log)
 
     return NGX_OK;
 }
+
+
+ngx_int_t
+ngx_daemon_sync(ngx_log_t *log)
+{
+    if (ngx_daemon_fd == NGX_INVALID_FILE) {
+        return NGX_OK;
+    }
+
+    if (write(ngx_daemon_fd, "", 1) != 1) {
+        ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "write() pipe failed");
+        return NGX_ERROR;
+    }
+
+    if (close(ngx_daemon_fd) == -1) {
+        ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() pipe failed");
+        return NGX_ERROR;
+    }
+
+    ngx_daemon_fd = NGX_INVALID_FILE;
+
+    return NGX_OK;
+}
diff --git a/src/os/unix/ngx_os.h b/src/os/unix/ngx_os.h
--- a/src/os/unix/ngx_os.h
+++ b/src/os/unix/ngx_os.h
@@ -40,6 +40,7 @@ void ngx_os_status(ngx_log_t *log);
 ngx_int_t ngx_os_specific_init(ngx_log_t *log);
 void ngx_os_specific_status(ngx_log_t *log);
 ngx_int_t ngx_daemon(ngx_log_t *log);
+ngx_int_t ngx_daemon_sync(ngx_log_t *log);
 ngx_int_t ngx_os_signal_process(ngx_cycle_t *cycle, char *sig, ngx_pid_t pid);
 
 




More information about the nginx-devel mailing list