changeset 7162:8b84d60ef13d

Fixed "changing binary" when reaper is not init. On some systems, it's possible that reaper of orphaned processes is set to something other than "init" process. On such systems, the changing binary procedure did not work. The fix is to check if PPID has changed, instead of assuming it's always 1 for orphaned processes.
author Ruslan Ermilov <ru@nginx.com>
date Tue, 28 Nov 2017 12:00:24 +0300
parents 325b3042edd6
children fc0d06224eda
files src/core/nginx.c src/os/unix/ngx_daemon.c src/os/unix/ngx_process.c src/os/unix/ngx_process.h src/os/unix/ngx_process_cycle.c src/os/win32/ngx_process.h src/os/win32/ngx_process_cycle.c
diffstat 7 files changed, 12 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/nginx.c	Thu Nov 23 16:33:40 2017 +0300
+++ b/src/core/nginx.c	Tue Nov 28 12:00:24 2017 +0300
@@ -228,6 +228,7 @@
 #endif
 
     ngx_pid = ngx_getpid();
+    ngx_parent = ngx_getppid();
 
     log = ngx_log_init(ngx_prefix);
     if (log == NULL) {
--- a/src/os/unix/ngx_daemon.c	Thu Nov 23 16:33:40 2017 +0300
+++ b/src/os/unix/ngx_daemon.c	Tue Nov 28 12:00:24 2017 +0300
@@ -26,6 +26,7 @@
         exit(0);
     }
 
+    ngx_parent = ngx_pid;
     ngx_pid = ngx_getpid();
 
     if (setsid() == -1) {
--- a/src/os/unix/ngx_process.c	Thu Nov 23 16:33:40 2017 +0300
+++ b/src/os/unix/ngx_process.c	Tue Nov 28 12:00:24 2017 +0300
@@ -194,6 +194,7 @@
         return NGX_INVALID_PID;
 
     case 0:
+        ngx_parent = ngx_pid;
         ngx_pid = ngx_getpid();
         proc(cycle, data);
         break;
@@ -371,12 +372,12 @@
             break;
 
         case ngx_signal_value(NGX_CHANGEBIN_SIGNAL):
-            if (getppid() > 1 || ngx_new_binary > 0) {
+            if (ngx_getppid() == ngx_parent || ngx_new_binary > 0) {
 
                 /*
                  * Ignore the signal in the new binary if its parent is
-                 * not the init process, i.e. the old binary's process
-                 * is still running.  Or ignore the signal in the old binary's
+                 * not changed, i.e. the old binary's process is still
+                 * running.  Or ignore the signal in the old binary's
                  * process if the new binary's process is already running.
                  */
 
--- a/src/os/unix/ngx_process.h	Thu Nov 23 16:33:40 2017 +0300
+++ b/src/os/unix/ngx_process.h	Tue Nov 28 12:00:24 2017 +0300
@@ -54,6 +54,7 @@
 
 
 #define ngx_getpid   getpid
+#define ngx_getppid  getppid
 
 #ifndef ngx_log_pid
 #define ngx_log_pid  ngx_pid
@@ -79,6 +80,7 @@
 extern char         **ngx_os_argv;
 
 extern ngx_pid_t      ngx_pid;
+extern ngx_pid_t      ngx_parent;
 extern ngx_socket_t   ngx_channel;
 extern ngx_int_t      ngx_process_slot;
 extern ngx_int_t      ngx_last_process;
--- a/src/os/unix/ngx_process_cycle.c	Thu Nov 23 16:33:40 2017 +0300
+++ b/src/os/unix/ngx_process_cycle.c	Tue Nov 28 12:00:24 2017 +0300
@@ -31,6 +31,7 @@
 ngx_uint_t    ngx_process;
 ngx_uint_t    ngx_worker;
 ngx_pid_t     ngx_pid;
+ngx_pid_t     ngx_parent;
 
 sig_atomic_t  ngx_reap;
 sig_atomic_t  ngx_sigio;
--- a/src/os/win32/ngx_process.h	Thu Nov 23 16:33:40 2017 +0300
+++ b/src/os/win32/ngx_process.h	Tue Nov 28 12:00:24 2017 +0300
@@ -14,6 +14,7 @@
 
 
 #define ngx_getpid          GetCurrentProcessId
+#define ngx_getppid()       0
 #define ngx_log_pid         ngx_pid
 
 
@@ -73,6 +74,7 @@
 extern ngx_process_t        ngx_processes[NGX_MAX_PROCESSES];
 
 extern ngx_pid_t            ngx_pid;
+extern ngx_pid_t            ngx_parent;
 
 
 #endif /* _NGX_PROCESS_H_INCLUDED_ */
--- a/src/os/win32/ngx_process_cycle.c	Thu Nov 23 16:33:40 2017 +0300
+++ b/src/os/win32/ngx_process_cycle.c	Tue Nov 28 12:00:24 2017 +0300
@@ -31,6 +31,7 @@
 ngx_uint_t     ngx_process;
 ngx_uint_t     ngx_worker;
 ngx_pid_t      ngx_pid;
+ngx_pid_t      ngx_parent;
 
 ngx_uint_t     ngx_inherited;
 ngx_pid_t      ngx_new_binary;