changeset 9268:6a3ee145d0b5

Core: made it possible to disable PID files with "pid off". While it is not generally recommended, this might be beneficial in some configurations, such as with immutable images and direct control by a service manager.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 13 May 2024 06:13:03 +0300
parents 9a5e2296c1be
children 4eb02e5ddb48
files src/core/nginx.c src/core/ngx_cycle.c src/os/unix/ngx_process_cycle.c
diffstat 3 files changed, 40 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/core/nginx.c
+++ b/src/core/nginx.c
@@ -766,7 +766,9 @@ ngx_exec_new_binary(ngx_cycle_t *cycle, 
 
     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
-    if (ngx_rename_file(ccf->pid.data, ccf->oldpid.data) == NGX_FILE_ERROR) {
+    if (ccf->pid.len
+        && ngx_rename_file(ccf->pid.data, ccf->oldpid.data) == NGX_FILE_ERROR)
+    {
         ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
                       ngx_rename_file_n " %s to %s failed "
                       "before executing new binary process \"%s\"",
@@ -781,8 +783,9 @@ ngx_exec_new_binary(ngx_cycle_t *cycle, 
     pid = ngx_execute(cycle, &ctx);
 
     if (pid == NGX_INVALID_PID) {
-        if (ngx_rename_file(ccf->oldpid.data, ccf->pid.data)
-            == NGX_FILE_ERROR)
+        if (ccf->pid.len
+            && ngx_rename_file(ccf->oldpid.data, ccf->pid.data)
+               == NGX_FILE_ERROR)
         {
             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
                           ngx_rename_file_n " %s back to %s failed after "
@@ -1168,20 +1171,26 @@ ngx_core_module_init_conf(ngx_cycle_t *c
         ngx_str_set(&ccf->pid, NGX_PID_PATH);
     }
 
-    if (ngx_conf_full_name(cycle, &ccf->pid, 0) != NGX_OK) {
-        return NGX_CONF_ERROR;
-    }
+    if (ngx_strcmp(ccf->pid.data, "off") == 0) {
+        ngx_str_set(&ccf->pid, "");
+        ngx_str_set(&ccf->oldpid, "");
 
-    ccf->oldpid.len = ccf->pid.len + sizeof(NGX_OLDPID_EXT);
+    } else {
+        if (ngx_conf_full_name(cycle, &ccf->pid, 0) != NGX_OK) {
+            return NGX_CONF_ERROR;
+        }
 
-    ccf->oldpid.data = ngx_pnalloc(cycle->pool, ccf->oldpid.len);
-    if (ccf->oldpid.data == NULL) {
-        return NGX_CONF_ERROR;
+        ccf->oldpid.len = ccf->pid.len + sizeof(NGX_OLDPID_EXT);
+
+        ccf->oldpid.data = ngx_pnalloc(cycle->pool, ccf->oldpid.len);
+        if (ccf->oldpid.data == NULL) {
+            return NGX_CONF_ERROR;
+        }
+
+        ngx_memcpy(ngx_cpymem(ccf->oldpid.data, ccf->pid.data, ccf->pid.len),
+                   NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT));
     }
 
-    ngx_memcpy(ngx_cpymem(ccf->oldpid.data, ccf->pid.data, ccf->pid.len),
-               NGX_OLDPID_EXT, sizeof(NGX_OLDPID_EXT));
-
 
 #if !(NGX_WIN32)
 
--- a/src/core/ngx_cycle.c
+++ b/src/core/ngx_cycle.c
@@ -1027,6 +1027,10 @@ ngx_create_pidfile(ngx_str_t *name, ngx_
         return NGX_OK;
     }
 
+    if (name->len == 0) {
+        return NGX_OK;
+    }
+
     ngx_memzero(&file, sizeof(ngx_file_t));
 
     file.name = *name;
@@ -1070,6 +1074,10 @@ ngx_delete_pidfile(ngx_cycle_t *cycle)
 
     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
+    if (ccf->pid.len == 0) {
+        return;
+    }
+
     name = ngx_new_binary ? ccf->oldpid.data : ccf->pid.data;
 
     if (ngx_delete_file(name) == NGX_FILE_ERROR) {
@@ -1092,6 +1100,12 @@ ngx_signal_process(ngx_cycle_t *cycle, c
 
     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
+    if (ccf->pid.len == 0) {
+        ngx_log_error(NGX_LOG_ERR, cycle->log, 0,
+                      "no PID file configured");
+        return 1;
+    }
+
     ngx_memzero(&file, sizeof(ngx_file_t));
 
     file.name = ccf->pid;
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -619,9 +619,10 @@ ngx_reap_children(ngx_cycle_t *cycle)
                 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx,
                                                        ngx_core_module);
 
-                if (ngx_rename_file((char *) ccf->oldpid.data,
-                                    (char *) ccf->pid.data)
-                    == NGX_FILE_ERROR)
+                if (ccf->pid.len
+                    && ngx_rename_file((char *) ccf->oldpid.data,
+                                       (char *) ccf->pid.data)
+                       == NGX_FILE_ERROR)
                 {
                     ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
                                   ngx_rename_file_n " %s back to %s failed "