|
| 1 | +From 03b18ac401de8df804b1cc455ded06359dae2374 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Felix Kaechele <felix@kaechele.ca> |
| 3 | +Date: Tue, 20 Apr 2021 21:28:18 -0400 |
| 4 | +Subject: [PATCH 2/5] fix PIDFile handling |
| 5 | + |
| 6 | +Corresponding RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=1869026 |
| 7 | + |
| 8 | +Rejected upstream: https://trac.nginx.org/nginx/ticket/1897 |
| 9 | + |
| 10 | +Taken from: https://git.launchpad.net/ubuntu/+source/nginx/tree/debian/patches/nginx-fix-pidfile.patch |
| 11 | + |
| 12 | +From original patch: |
| 13 | +Author: Tj <ubuntu@iam.tj> |
| 14 | +Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/nginx/+bug/1581864 |
| 15 | +Bug-Debian: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=876365 |
| 16 | +Last-Update: 2020-06-24 |
| 17 | + |
| 18 | +Signed-off-by: Felix Kaechele <felix@kaechele.ca> |
| 19 | +--- |
| 20 | + src/core/nginx.c | 24 +++++++++++++++++++++--- |
| 21 | + src/os/unix/ngx_daemon.c | 8 ++++++-- |
| 22 | + 2 files changed, 27 insertions(+), 5 deletions(-) |
| 23 | + |
| 24 | +diff --git a/src/core/nginx.c b/src/core/nginx.c |
| 25 | +index 0deb27b7f98a..23edb59ff105 100644 |
| 26 | +--- a/src/core/nginx.c |
| 27 | ++++ b/src/core/nginx.c |
| 28 | +@@ -340,14 +340,21 @@ main(int argc, char *const *argv) |
| 29 | + ngx_process = NGX_PROCESS_MASTER; |
| 30 | + } |
| 31 | + |
| 32 | ++ /* tell-tale to detect if this is parent or child process */ |
| 33 | ++ ngx_int_t child_pid = NGX_BUSY; |
| 34 | ++ |
| 35 | + #if !(NGX_WIN32) |
| 36 | + |
| 37 | + if (ngx_init_signals(cycle->log) != NGX_OK) { |
| 38 | + return 1; |
| 39 | + } |
| 40 | + |
| 41 | ++ /* tell-tale that this code has been executed */ |
| 42 | ++ child_pid--; |
| 43 | ++ |
| 44 | + if (!ngx_inherited && ccf->daemon) { |
| 45 | +- if (ngx_daemon(cycle->log) != NGX_OK) { |
| 46 | ++ child_pid = ngx_daemon(cycle->log); |
| 47 | ++ if (child_pid == NGX_ERROR) { |
| 48 | + return 1; |
| 49 | + } |
| 50 | + |
| 51 | +@@ -360,8 +367,19 @@ main(int argc, char *const *argv) |
| 52 | + |
| 53 | + #endif |
| 54 | + |
| 55 | +- if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) { |
| 56 | +- return 1; |
| 57 | ++ /* If ngx_daemon() returned the child's PID in the parent process |
| 58 | ++ * after the fork() set ngx_pid to the child_pid, which gets |
| 59 | ++ * written to the PID file, then exit. |
| 60 | ++ * For NGX_WIN32 always write the PID file |
| 61 | ++ * For others, only write it from the parent process */ |
| 62 | ++ if (child_pid < NGX_OK || child_pid > NGX_OK) { |
| 63 | ++ ngx_pid = child_pid > NGX_OK ? child_pid : ngx_pid; |
| 64 | ++ if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) { |
| 65 | ++ return 1; |
| 66 | ++ } |
| 67 | ++ } |
| 68 | ++ if (child_pid > NGX_OK) { |
| 69 | ++ exit(0); |
| 70 | + } |
| 71 | + |
| 72 | + if (ngx_log_redirect_stderr(cycle) != NGX_OK) { |
| 73 | +diff --git a/src/os/unix/ngx_daemon.c b/src/os/unix/ngx_daemon.c |
| 74 | +index 385c49b6c3d1..3719854c52b0 100644 |
| 75 | +--- a/src/os/unix/ngx_daemon.c |
| 76 | ++++ b/src/os/unix/ngx_daemon.c |
| 77 | +@@ -7,14 +7,17 @@ |
| 78 | + |
| 79 | + #include <ngx_config.h> |
| 80 | + #include <ngx_core.h> |
| 81 | ++#include <unistd.h> |
| 82 | + |
| 83 | + |
| 84 | + ngx_int_t |
| 85 | + ngx_daemon(ngx_log_t *log) |
| 86 | + { |
| 87 | + int fd; |
| 88 | ++ /* retain the return value for passing back to caller */ |
| 89 | ++ pid_t pid_child = fork(); |
| 90 | + |
| 91 | +- switch (fork()) { |
| 92 | ++ switch (pid_child) { |
| 93 | + case -1: |
| 94 | + ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "fork() failed"); |
| 95 | + return NGX_ERROR; |
| 96 | +@@ -23,7 +26,8 @@ ngx_daemon(ngx_log_t *log) |
| 97 | + break; |
| 98 | + |
| 99 | + default: |
| 100 | +- exit(0); |
| 101 | ++ /* let caller do the exit() */ |
| 102 | ++ return pid_child; |
| 103 | + } |
| 104 | + |
| 105 | + ngx_parent = ngx_pid; |
| 106 | +-- |
| 107 | +2.52.0 |
0 commit comments