diff src/core/nginx.c @ 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 c209dc4eed17
children 3d455e37abf8
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)