From 8f4177b6e22776d5773cd141855348e0f355d47d Mon Sep 17 00:00:00 2001 From: zhwaaaaaa Date: Wed, 21 Feb 2024 22:23:51 +0800 Subject: [PATCH] fix multi process core dump on reload --- conf/my.conf | 2 +- modules/auxiliary/ngx_auxiliary_module.c | 120 ++++++++++++++--------- modules/nacos/ngx_nacos_aux.c | 6 -- 3 files changed, 72 insertions(+), 56 deletions(-) diff --git a/conf/my.conf b/conf/my.conf index 69ff62b..bf9d2d4 100644 --- a/conf/my.conf +++ b/conf/my.conf @@ -1,5 +1,5 @@ #user nobody; -master_process off; +master_process on; daemon off; #error_log logs/error.log; diff --git a/modules/auxiliary/ngx_auxiliary_module.c b/modules/auxiliary/ngx_auxiliary_module.c index a56befb..c703942 100644 --- a/modules/auxiliary/ngx_auxiliary_module.c +++ b/modules/auxiliary/ngx_auxiliary_module.c @@ -11,44 +11,60 @@ static void ngx_pass_open_channel(ngx_cycle_t *cycle); static ngx_int_t ngx_aux_init_master(ngx_cycle_t *cycle); static ngx_core_module_t auxiliary_module = { - ngx_string("auxiliary"), - NULL, // 解析配置文件之前执行 - NULL // 解析配置文件之后执行 + ngx_string("auxiliary"), + NULL, // 解析配置文件之前执行 + NULL // 解析配置文件之后执行 }; -ngx_module_t ngx_auxiliary_module = { - NGX_MODULE_V1, - &auxiliary_module, - NULL, - NGX_CORE_MODULE, - NULL, /* init master */ - NULL, /* init module */ - ngx_aux_init_master, /* init process */ - NULL, /* init thread */ - NULL, /* exit thread */ - NULL, /* exit process */ - NULL, /* exit master */ - NGX_MODULE_V1_PADDING -}; - -#define ngx_aux_get_main_conf_ptr(cf) (ngx_aux_proc_main_conf_t **) &(cf)->cycle->conf_ctx[ngx_auxiliary_module.index] - +ngx_module_t ngx_auxiliary_module = {NGX_MODULE_V1, + &auxiliary_module, + NULL, + NGX_CORE_MODULE, + NULL, /* init master */ + NULL, /* init module */ + ngx_aux_init_master, /* init process */ + NULL, /* init thread */ + NULL, /* exit thread */ + NULL, /* exit process */ + NULL, /* exit master */ + NGX_MODULE_V1_PADDING}; + +#define ngx_aux_get_main_conf_ptr(cf) \ + (ngx_aux_proc_main_conf_t **) &(cf) \ + ->cycle->conf_ctx[ngx_auxiliary_module.index] ngx_int_t ngx_aux_add_proc(ngx_conf_t *cf, ngx_aux_proc_t *proc) { ngx_aux_proc_main_conf_t **ptr, *mcf; + ngx_aux_proc_t **n_proc; + ngx_uint_t i; + ptr = ngx_aux_get_main_conf_ptr(cf); - if (*ptr == NULL) { - *ptr = ngx_palloc(cf->pool, sizeof(ngx_aux_proc_main_conf_t)); - } + mcf = *ptr; if (mcf == NULL) { - return NGX_ERROR; - } - if (ngx_array_init(&mcf->process, cf->pool, 4, sizeof(ngx_aux_proc_t *)) != NGX_OK) { - return NGX_ERROR; + mcf = ngx_palloc(cf->pool, sizeof(ngx_aux_proc_main_conf_t)); + if (mcf == NULL) { + return NGX_ERROR; + } + if (ngx_array_init(&mcf->process, cf->pool, 4, + sizeof(ngx_aux_proc_t *)) != NGX_OK) { + return NGX_ERROR; + } + *ptr = mcf; + } else { + n_proc = mcf->process.elts; + for (i = 0; i < mcf->process.nelts; i++) { + if (n_proc[i]->name.len == proc->name.len && + ngx_strncmp(n_proc[i]->name.data, proc->name.data, + proc->name.len) == 0) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "process \"%V\" exists", &proc->name); + return NGX_ERROR; + } + } } - ngx_aux_proc_t **n_proc = ngx_array_push(&mcf->process); + n_proc = ngx_array_push(&mcf->process); if (n_proc == NULL) { return NGX_ERROR; } @@ -60,9 +76,11 @@ void ngx_aux_start_auxiliary_processes(ngx_cycle_t *cycle, ngx_uint_t respawn) { ngx_aux_proc_main_conf_t *mcf; ngx_aux_proc_t **proc; ngx_uint_t i, n; + size_t len; char buf[256]; - mcf = (ngx_aux_proc_main_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_auxiliary_module); + mcf = (ngx_aux_proc_main_conf_t *) ngx_get_conf(cycle->conf_ctx, + ngx_auxiliary_module); if (mcf == NULL) { return; } @@ -71,16 +89,19 @@ void ngx_aux_start_auxiliary_processes(ngx_cycle_t *cycle, ngx_uint_t respawn) { proc = mcf->process.elts; for (i = 0; i < n; ++i) { - ngx_sprintf((u_char *) buf, "auxiliary: %V", &proc[i]->name); - - ngx_spawn_process(cycle, ngx_aux_process_cycle, proc[i], buf, - respawn ? NGX_PROCESS_JUST_RESPAWN : NGX_PROCESS_RESPAWN); + len = ngx_sprintf((u_char *) buf, "auxiliary: %V", &proc[i]->name) - + (u_char *) buf; + buf[len] = 0; + ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, + "start auxiliary processes %s", buf); + + ngx_spawn_process( + cycle, ngx_aux_process_cycle, proc[i], buf, + respawn ? NGX_PROCESS_JUST_RESPAWN : NGX_PROCESS_RESPAWN); ngx_pass_open_channel(cycle); } - } - static void ngx_pass_open_channel(ngx_cycle_t *cycle) { ngx_int_t i; ngx_channel_t ch; @@ -91,28 +112,25 @@ static void ngx_pass_open_channel(ngx_cycle_t *cycle) { ch.fd = ngx_processes[ngx_process_slot].channel[0]; for (i = 0; i < ngx_last_process; i++) { - - if (i == ngx_process_slot - || ngx_processes[i].pid == -1 - || ngx_processes[i].channel[0] == -1) { + if (i == ngx_process_slot || ngx_processes[i].pid == -1 || + ngx_processes[i].channel[0] == -1) { continue; } ngx_log_debug6(NGX_LOG_DEBUG_CORE, cycle->log, 0, "pass channel s:%i pid:%P fd:%d to s:%i pid:%P fd:%d", - ch.slot, ch.pid, ch.fd, - i, ngx_processes[i].pid, + ch.slot, ch.pid, ch.fd, i, ngx_processes[i].pid, ngx_processes[i].channel[0]); - - ngx_write_channel(ngx_processes[i].channel[0], - &ch, sizeof(ngx_channel_t), cycle->log); + ngx_write_channel(ngx_processes[i].channel[0], &ch, + sizeof(ngx_channel_t), cycle->log); } } static void ngx_aux_process_cycle(ngx_cycle_t *cycle, void *data) { char buf[256]; ngx_int_t ret; + size_t len; ngx_aux_proc_t *proc = data; ngx_process = NGX_PROCESS_HELPER; @@ -123,15 +141,19 @@ static void ngx_aux_process_cycle(ngx_cycle_t *cycle, void *data) { cycle->connection_n = 512; ngx_worker_aux_process_init(cycle); - ngx_sprintf((u_char *) buf, "auxiliary %V", &proc->name); + len = ngx_sprintf((u_char *) buf, "auxiliary %V", &proc->name) - + (u_char *) buf; + buf[len] = 0; ngx_setproctitle(buf); + ret = proc->process(cycle, proc); if (ret != NGX_OK) { - ngx_log_error(NGX_LOG_NOTICE, cycle->log, ret, "exiting auxiliary process: %s", buf); + ngx_log_error(NGX_LOG_NOTICE, cycle->log, ret, + "exiting auxiliary process: %s", buf); exit(1); } - for (;;) { + for (;;) { if (ngx_terminate || ngx_quit) { ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting"); exit(0); @@ -145,7 +167,6 @@ static void ngx_aux_process_cycle(ngx_cycle_t *cycle, void *data) { ngx_process_events_and_timers(cycle); } - } static ngx_int_t ngx_aux_init_master(ngx_cycle_t *cycle) { @@ -157,7 +178,8 @@ static ngx_int_t ngx_aux_init_master(ngx_cycle_t *cycle) { return NGX_OK; } - mcf = (ngx_aux_proc_main_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_auxiliary_module); + mcf = (ngx_aux_proc_main_conf_t *) ngx_get_conf(cycle->conf_ctx, + ngx_auxiliary_module); if (mcf == NULL) { return NGX_ERROR; } diff --git a/modules/nacos/ngx_nacos_aux.c b/modules/nacos/ngx_nacos_aux.c index 0a5a888..9ca6c6c 100644 --- a/modules/nacos/ngx_nacos_aux.c +++ b/modules/nacos/ngx_nacos_aux.c @@ -6,9 +6,7 @@ #include #include #include -#include #include -#include #include static ngx_int_t ngx_aux_nacos_proc_handler(ngx_cycle_t *cycle, @@ -27,10 +25,6 @@ static ngx_aux_proc_t aux_proc = {ngx_string("nacos"), NULL, ngx_aux_nacos_proc_handler}; ngx_int_t ngx_nacos_aux_init(ngx_conf_t *cf) { - if (aux_proc.data != NULL) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "ngx_nacos_aux_init inited"); - return NGX_ERROR; - } aux_ctx.ncf = ngx_nacos_get_main_conf(cf); aux_proc.data = &aux_ctx; return ngx_aux_add_proc(cf, &aux_proc);