blob: 90159a667757a7caede4baabc1b4ece7ebdcb515 [file] [log] [blame]
Andrew Geissler82c905d2020-04-13 13:39:40 -05001Description: Fix NGINX pidfile handling
2Author: Tj <ubuntu@iam.tj>
3Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/nginx/+bug/1581864
4Last-Update: 2019-06-04
5---
6This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
7
8Upstream-Status: Pending
9
10This patch is from ubuntu, https://github.com/aroth-arsoft/pkg-nginx/blob
11/master/debian/patches/nginx-fix-pidfile.patch, for fix below
12error info:
13nginx.service: failed to parse pid from file /run/nginx/nginx.pid:
14invalid argument
15
16Signed-off-by: Changqing Li <changqing.li@windriver.com>
17
18diff --git a/src/core/nginx.c b/src/core/nginx.c
19index 9fcb0eb2..083eba1d 100644
20--- a/src/core/nginx.c
21+++ b/src/core/nginx.c
22@@ -338,14 +338,21 @@ main(int argc, char *const *argv)
23 ngx_process = NGX_PROCESS_MASTER;
24 }
25
26+ /* tell-tale to detect if this is parent or child process */
27+ ngx_int_t child_pid = NGX_BUSY;
28+
29 #if !(NGX_WIN32)
30
31 if (ngx_init_signals(cycle->log) != NGX_OK) {
32 return 1;
33 }
34
35+ /* tell-tale that this code has been executed */
36+ child_pid--;
37+
38 if (!ngx_inherited && ccf->daemon) {
39- if (ngx_daemon(cycle->log) != NGX_OK) {
40+ child_pid = ngx_daemon(cycle->log);
41+ if (child_pid == NGX_ERROR) {
42 return 1;
43 }
44
45@@ -358,8 +365,19 @@ main(int argc, char *const *argv)
46
47 #endif
48
49- if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
50- return 1;
51+ /* If ngx_daemon() returned the child's PID in the parent process
52+ * after the fork() set ngx_pid to the child_pid, which gets
53+ * written to the PID file, then exit.
54+ * For NGX_WIN32 always write the PID file
55+ * For others, only write it from the parent process */
56+ if (child_pid < NGX_OK || child_pid > NGX_OK) {
57+ ngx_pid = child_pid > NGX_OK ? child_pid : ngx_pid;
58+ if (ngx_create_pidfile(&ccf->pid, cycle->log) != NGX_OK) {
59+ return 1;
60+ }
61+ }
62+ if (child_pid > NGX_OK) {
63+ exit(0);
64 }
65
66 if (ngx_log_redirect_stderr(cycle) != NGX_OK) {
67diff --git a/src/os/unix/ngx_daemon.c b/src/os/unix/ngx_daemon.c
68index 385c49b6..3719854c 100644
69--- a/src/os/unix/ngx_daemon.c
70+++ b/src/os/unix/ngx_daemon.c
71@@ -7,14 +7,17 @@
72
73 #include <ngx_config.h>
74 #include <ngx_core.h>
75+#include <unistd.h>
76
77
78 ngx_int_t
79 ngx_daemon(ngx_log_t *log)
80 {
81 int fd;
82+ /* retain the return value for passing back to caller */
83+ pid_t pid_child = fork();
84
85- switch (fork()) {
86+ switch (pid_child) {
87 case -1:
88 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "fork() failed");
89 return NGX_ERROR;
90@@ -23,7 +26,8 @@ ngx_daemon(ngx_log_t *log)
91 break;
92
93 default:
94- exit(0);
95+ /* let caller do the exit() */
96+ return pid_child;
97 }
98
99 ngx_parent = ngx_pid;