comparison src/core/ngx_cycle.c @ 7685:4f30f75dbdf3

Core: close PID file when writing fails. Reported by Jinhua Tan.
author Ruslan Ermilov <ru@nginx.com>
date Tue, 21 Jul 2020 20:34:29 +0300
parents 9c038f5e0464
children f18db38a9826
comparison
equal deleted inserted replaced
7684:32a343635b50 7685:4f30f75dbdf3
1007 1007
1008 ngx_int_t 1008 ngx_int_t
1009 ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log) 1009 ngx_create_pidfile(ngx_str_t *name, ngx_log_t *log)
1010 { 1010 {
1011 size_t len; 1011 size_t len;
1012 ngx_int_t rc;
1012 ngx_uint_t create; 1013 ngx_uint_t create;
1013 ngx_file_t file; 1014 ngx_file_t file;
1014 u_char pid[NGX_INT64_LEN + 2]; 1015 u_char pid[NGX_INT64_LEN + 2];
1015 1016
1016 if (ngx_process > NGX_PROCESS_MASTER) { 1017 if (ngx_process > NGX_PROCESS_MASTER) {
1031 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, 1032 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,
1032 ngx_open_file_n " \"%s\" failed", file.name.data); 1033 ngx_open_file_n " \"%s\" failed", file.name.data);
1033 return NGX_ERROR; 1034 return NGX_ERROR;
1034 } 1035 }
1035 1036
1037 rc = NGX_OK;
1038
1036 if (!ngx_test_config) { 1039 if (!ngx_test_config) {
1037 len = ngx_snprintf(pid, NGX_INT64_LEN + 2, "%P%N", ngx_pid) - pid; 1040 len = ngx_snprintf(pid, NGX_INT64_LEN + 2, "%P%N", ngx_pid) - pid;
1038 1041
1039 if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) { 1042 if (ngx_write_file(&file, pid, len, 0) == NGX_ERROR) {
1040 return NGX_ERROR; 1043 rc = NGX_ERROR;
1041 } 1044 }
1042 } 1045 }
1043 1046
1044 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) { 1047 if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
1045 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, 1048 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
1046 ngx_close_file_n " \"%s\" failed", file.name.data); 1049 ngx_close_file_n " \"%s\" failed", file.name.data);
1047 } 1050 }
1048 1051
1049 return NGX_OK; 1052 return rc;
1050 } 1053 }
1051 1054
1052 1055
1053 void 1056 void
1054 ngx_delete_pidfile(ngx_cycle_t *cycle) 1057 ngx_delete_pidfile(ngx_cycle_t *cycle)