ab020e
From 62470498cca9a209aa9904668c1949f5229123af Mon Sep 17 00:00:00 2001
ab020e
From: Felix Kaechele <felix@kaechele.ca>
ab020e
Date: Tue, 20 Apr 2021 21:28:18 -0400
ab020e
Subject: [PATCH 2/2] fix PIDFile handling
ab020e
ab020e
Corresponding RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1869026
ab020e
ab020e
Rejected upstream: https://trac.nginx.org/nginx/ticket/1897
ab020e
ab020e
Taken from: https://git.launchpad.net/ubuntu/+source/nginx/tree/debian/patches/nginx-fix-pidfile.patch
ab020e
ab020e
From original patch:
ab020e
Author: Tj <ubuntu@iam.tj>
ab020e
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/nginx/+bug/1581864
ab020e
Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=876365
ab020e
iLast-Update: 2020-06-24
ab020e
ab020e
Signed-off-by: Felix Kaechele <felix@kaechele.ca>
ab020e
---
ab020e
 src/core/nginx.c         | 24 +++++++++++++++++++++---
ab020e
 src/os/unix/ngx_daemon.c |  8 ++++++--
ab020e
 2 files changed, 27 insertions(+), 5 deletions(-)
ab020e
ab020e
diff --git a/src/core/nginx.c b/src/core/nginx.c
ab020e
index 48a20e9..32c0afe 100644
ab020e
--- a/src/core/nginx.c
ab020e
+++ b/src/core/nginx.c
ab020e
@@ -339,14 +339,21 @@ main(int argc, char *const *argv)
ab020e
         ngx_process = NGX_PROCESS_MASTER;
ab020e
     }
ab020e
 
ab020e
+    /* tell-tale to detect if this is parent or child process */
ab020e
+    ngx_int_t child_pid = NGX_BUSY;
ab020e
+
ab020e
 #if !(NGX_WIN32)
ab020e
 
ab020e
     if (ngx_init_signals(cycle->log) != NGX_OK) {
ab020e
         return 1;
ab020e
     }
ab020e
 
ab020e
+    /* tell-tale that this code has been executed */
ab020e
+    child_pid--;
ab020e
+
ab020e
     if (!ngx_inherited && ccf->daemon) {
ab020e
-        if (ngx_daemon(cycle->log) != NGX_OK) {
ab020e
+        child_pid = ngx_daemon(cycle->log);
ab020e
+        if (child_pid == NGX_ERROR) {
ab020e
             return 1;
ab020e
         }
ab020e
 
ab020e
@@ -359,8 +366,19 @@ main(int argc, char *const *argv)
ab020e
 
ab020e
 #endif
ab020e
 
ab020e
-    if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
ab020e
-        return 1;
ab020e
+    /* If ngx_daemon() returned the child's PID in the parent process
ab020e
+     * after the fork() set ngx_pid to the child_pid, which gets
ab020e
+     * written to the PID file, then exit.
ab020e
+     * For NGX_WIN32 always write the PID file
ab020e
+     * For others, only write it from the parent process */
ab020e
+    if (child_pid < NGX_OK || child_pid > NGX_OK) {
ab020e
+	ngx_pid = child_pid > NGX_OK ? child_pid : ngx_pid;
ab020e
+        if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
ab020e
+            return 1;
ab020e
+	}
ab020e
+    }
ab020e
+    if (child_pid > NGX_OK) {
ab020e
+        exit(0);
ab020e
     }
ab020e
 
ab020e
     if (ngx_log_redirect_stderr(cycle) != NGX_OK) {
ab020e
diff --git a/src/os/unix/ngx_daemon.c b/src/os/unix/ngx_daemon.c
ab020e
index 385c49b..3719854 100644
ab020e
--- a/src/os/unix/ngx_daemon.c
ab020e
+++ b/src/os/unix/ngx_daemon.c
ab020e
@@ -7,14 +7,17 @@
ab020e
 
ab020e
 #include <ngx_config.h>
ab020e
 #include <ngx_core.h>
ab020e
+#include <unistd.h>
ab020e
 
ab020e
 
ab020e
 ngx_int_t
ab020e
 ngx_daemon(ngx_log_t *log)
ab020e
 {
ab020e
     int  fd;
ab020e
+    /* retain the return value for passing back to caller */
ab020e
+    pid_t pid_child = fork();
ab020e
 
ab020e
-    switch (fork()) {
ab020e
+    switch (pid_child) {
ab020e
     case -1:
ab020e
         ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "fork() failed");
ab020e
         return NGX_ERROR;
ab020e
@@ -23,7 +26,8 @@ ngx_daemon(ngx_log_t *log)
ab020e
         break;
ab020e
 
ab020e
     default:
ab020e
-        exit(0);
ab020e
+        /* let caller do the exit() */
ab020e
+        return pid_child;
ab020e
     }
ab020e
 
ab020e
     ngx_parent = ngx_pid;
ab020e
-- 
ab020e
2.31.1
ab020e