changeset 9270:3d455e37abf8

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.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 13 May 2024 06:13:22 +0300
parents 4eb02e5ddb48
children 8c4e2b7de093
files src/core/nginx.c src/os/unix/ngx_daemon.c src/os/unix/ngx_os.h
diffstat 3 files changed, 76 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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;
     }
--- 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;
+}
--- 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);