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