Core: close pid file while writing it failed.

Ruslan Ermilov ru at nginx.com
Tue Jul 21 17:38:43 UTC 2020


On Mon, Jun 22, 2020 at 06:09:14PM +0300, Ruslan Ermilov wrote:
> On Thu, May 21, 2020 at 09:45:24PM +0800, Jim T wrote:
> > Hello!
> > 
> > As far as I understand it, `ngx_create_pidfile` is a function that works
> > independently. There is no action to close the pid file externally, so we
> > need to close the pid file when the writing it failed. There are also
> > reports here https://github.com/nginx/nginx/pull/52.
> > 
> > # HG changeset patch
> > # User Jinhua Tan <312841925 at qq.com>
> > # Date 1590068494 -28800
> > #      Thu May 21 21:41:34 2020 +0800
> > # Node ID 6084ea4d9a4d2ae32f3fc4e2e3b9032ab0b71e30
> > # Parent  3242f98298975e556a7e87130611ce84799fe935
> > Core: close pid file while writing it failed.
> > 
> > diff -r 3242f9829897 -r 6084ea4d9a4d src/core/ngx_cycle.c
> > --- a/src/core/ngx_cycle.c      Wed May 20 12:24:05 2020 +0800
> > +++ b/src/core/ngx_cycle.c      Thu May 21 21:41:34 2020 +0800
> > @@ -1036,6 +1036,12 @@
> >          len = ngx_snprintf(pid, NGX_INT64_LEN + 2, "%P%N", ngx_pid) - pid;
> > 
> >          if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
> > +
> > +            if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
> > +                ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
> > +                              ngx_close_file_n " \"%s\" failed",
> > file.name.data);
> > +            }
> > +
> >              return NGX_ERROR;
> >          }
> >      }
> > 
> > Thank you!
> 
> How's this instead?
> 
> diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c
> --- a/src/core/ngx_cycle.c
> +++ b/src/core/ngx_cycle.c
> @@ -1009,6 +1009,7 @@ ngx_int_t
>  ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log)
>  {
>      size_t      len;
> +    ngx_int_t   rc;
>      ngx_uint_t  create;
>      ngx_file_t  file;
>      u_char      pid[NGX_INT64_LEN + 2];
> @@ -1033,11 +1034,13 @@ ngx_create_pidfile(ngx_str_t *name, ngx_
>          return NGX_ERROR;
>      }
>  
> +    rc = NGX_OK;
> +
>      if (!ngx_test_config) {
>          len = ngx_snprintf(pid, NGX_INT64_LEN + 2, "%P%N", ngx_pid) - pid;
>  
>          if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
> -            return NGX_ERROR;
> +            rc = NGX_ERROR;
>          }
>      }
>  
> @@ -1046,7 +1049,7 @@ ngx_create_pidfile(ngx_str_t *name, ngx_
>                        ngx_close_file_n " \"%s\" failed", file.name.data);
>      }
>  
> -    return NGX_OK;
> +    return rc;
>  }
>  
>  

http://hg.nginx.org/nginx/rev/4f30f75dbdf3


More information about the nginx-devel mailing list