# HG changeset patch # User Igor Sysoev # Date 1074012203 0 # Node ID 2e9a8a14a0cf58e24ace87b0255d46a0a9683e11 # Parent d427f1c3fe9518f400584a9ff67e50c0df9d6968 nginx-0.0.1-2004-01-13-19:43:23 import diff --git a/src/core/nginx.c b/src/core/nginx.c --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -76,10 +76,12 @@ u_int ngx_connection_counter; ngx_int_t ngx_process; ngx_int_t ngx_inherited; + +ngx_int_t ngx_signal; ngx_int_t ngx_reap; ngx_int_t ngx_terminate; ngx_int_t ngx_quit; -ngx_int_t ngx_pause; +ngx_int_t ngx_noaccept; ngx_int_t ngx_reconfigure; ngx_int_t ngx_reopen; ngx_int_t ngx_change_binary; @@ -233,7 +235,7 @@ static void ngx_master_process_cycle(ngx int signo; ngx_msec_t delay; struct timeval tv; - ngx_uint_t i, live, first; + ngx_uint_t i, live, mark; sigset_t set, wset; delay = 125; @@ -242,7 +244,7 @@ static void ngx_master_process_cycle(ngx sigaddset(&set, SIGCHLD); sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL)); sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL)); - sigaddset(&set, ngx_signal_value(NGX_PAUSE_SIGNAL)); + sigaddset(&set, ngx_signal_value(NGX_NOACCEPT_SIGNAL)); sigaddset(&set, ngx_signal_value(NGX_TERMINATE_SIGNAL)); sigaddset(&set, ngx_signal_value(NGX_SHUTDOWN_SIGNAL)); sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL)); @@ -254,6 +256,10 @@ static void ngx_master_process_cycle(ngx "sigprocmask() failed"); } + ngx_signal = 0; + signo = 0; + mark = 1; + for ( ;; ) { ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "new cycle"); @@ -278,96 +284,99 @@ static void ngx_master_process_cycle(ngx for ( ;; ) { - signo = 0; - first = 1; - /* an event loop */ for ( ;; ) { if (ngx_process == NGX_PROCESS_MASTER) { - sigsuspend(&wset); + if (signo) { + ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, + "signal cycle"); + + if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) { + ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, + "sigprocmask() failed"); + continue; + } + + /* + * there is very big chance that the pending signals + * would be delivered right on the sigprocmask() return + */ + + if (!ngx_signal) { + + if (delay < 15000) { + delay *= 2; + } + + ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, + "msleep %d", delay); - ngx_gettimeofday(&tv); - ngx_time_update(tv.tv_sec); + ngx_msleep(delay); + + ngx_gettimeofday(&tv); + ngx_time_update(tv.tv_sec); + + ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, + "wake up"); + } + if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { + ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, + "sigprocmask() failed"); + } + + ngx_signal = 0; + + } else { + sigsuspend(&wset); + + ngx_gettimeofday(&tv); + ngx_time_update(tv.tv_sec); + } + + /* TODO: broken */ } else if (ngx_process == NGX_PROCESS_SINGLE) { ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "worker cycle"); ngx_process_events(cycle->log); - - } else if (ngx_process == NGX_PROCESS_QUITING - || ngx_process == NGX_PROCESS_PAUSED) - { - ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, - "quit cycle"); + } - if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) { - ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, - "sigprocmask() failed"); - continue; - } - - if (ngx_reap == 0) { - - if (delay < 15000) { - delay *= 2; + if (ngx_reap) { + live = 0; + for (i = 0; i < ngx_last_process; i++) { + if (ngx_processes[i].exiting + && !ngx_processes[i].exited) + { + live = 1; + continue; } - ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, - "msleep %d", delay); - - ngx_msleep(delay); - - ngx_gettimeofday(&tv); - ngx_time_update(tv.tv_sec); - - ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, - "wake up"); + if (i != --ngx_last_process) { + ngx_processes[i--] = + ngx_processes[ngx_last_process]; + } } - if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { - ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, - "sigprocmask() failed"); - } - - if (ngx_reap) { - ngx_reap = 0; - - live = 0; - for (i = 0; i < ngx_last_process; i++) { - if (ngx_processes[i].exiting - && !ngx_processes[i].exited) + if (!live) { + if (ngx_terminate || ngx_quit) { + if (ngx_delete_file(ctx->pid.name.data) + == NGX_FILE_ERROR) { - live = 1; - continue; - } - - if (i != --ngx_last_process) { - ngx_processes[i--] = - ngx_processes[ngx_last_process]; - } - } - - if (live == 0) { - if (ngx_process == NGX_PROCESS_QUITING) { - if (ngx_delete_file(ctx->pid.name.data) - == NGX_FILE_ERROR) - { - ngx_log_error(NGX_LOG_ALERT, cycle->log, - ngx_errno, - ngx_delete_file_n - " \"%s\" failed", - ctx->pid.name.data); - } - - } else { /* NGX_PROCESS_PAUSED */ - ngx_pause = 0; + ngx_log_error(NGX_LOG_ALERT, cycle->log, + ngx_errno, + ngx_delete_file_n + " \"%s\" failed", + ctx->pid.name.data); } ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit"); exit(0); + + } else { + signo = 0; } } } @@ -378,65 +387,72 @@ static void ngx_master_process_cycle(ngx } else { signo = ngx_signal_value(NGX_TERMINATE_SIGNAL); } - ngx_process = NGX_PROCESS_QUITING; - } - if (ngx_quit) { + } else if (ngx_quit) { signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); - ngx_process = NGX_PROCESS_QUITING; - } + + } else { - if (ngx_pause) { - signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); - ngx_process = NGX_PROCESS_PAUSED; - } + if (ngx_reap) { + ngx_respawn_processes(cycle); + } - if (ngx_reap) { - ngx_reap = 0; - ngx_respawn_processes(cycle); - } + if (ngx_noaccept) { + if (mark == 0) { + mark = 1; + } + signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); + } - if (ngx_change_binary) { - ngx_change_binary = 0; - ngx_log_error(NGX_LOG_INFO, cycle->log, 0, - "changing binary"); - ngx_exec_new_binary(cycle, ctx->argv); - } + if (ngx_change_binary) { + ngx_change_binary = 0; + ngx_log_error(NGX_LOG_INFO, cycle->log, 0, + "changing binary"); + ngx_exec_new_binary(cycle, ctx->argv); + } - if (ngx_reconfigure) { - signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); - ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reconfiguring"); - } + if (ngx_reconfigure) { + mark = 1; + signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); + ngx_log_error(NGX_LOG_INFO, cycle->log, 0, + "reconfiguring"); + } - if (ngx_reopen) { - signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); - ngx_log_error(NGX_LOG_INFO, cycle->log, 0, - "reopening logs"); - ngx_reopen_files(cycle); - ngx_reopen = 0; + if (ngx_reopen) { + mark = 1; + ngx_reopen = 0; + signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); + ngx_log_error(NGX_LOG_INFO, cycle->log, 0, + "reopening logs"); + ngx_reopen_files(cycle); + } } if (signo) { - if (first) { + if (mark == 1) { for (i = 0; i < ngx_last_process; i++) { if (!ngx_processes[i].detached) { ngx_processes[i].signal = 1; } } - first = 0; + mark = -1; delay = 125; } ngx_signal_processes(cycle, signo); } + if (ngx_reap) { + ngx_reap = 0; + } + if (ngx_reconfigure) { break; } } - if (ngx_process == NGX_PROCESS_PAUSED) { - ngx_process = NGX_PROCESS_MASTER; + if (ngx_noaccept) { + ngx_noaccept = 0; } else { cycle = ngx_init_cycle(cycle); diff --git a/src/core/ngx_config.h b/src/core/ngx_config.h --- a/src/core/ngx_config.h +++ b/src/core/ngx_config.h @@ -65,7 +65,7 @@ typedef u_int ngx_uint_t; /* TODO: #ifndef */ #define NGX_SHUTDOWN_SIGNAL QUIT #define NGX_TERMINATE_SIGNAL TERM -#define NGX_PAUSE_SIGNAL INT +#define NGX_NOACCEPT_SIGNAL ABRT #define NGX_RECONFIGURE_SIGNAL HUP #define NGX_REOPEN_SIGNAL USR1 #define NGX_CHANGEBIN_SIGNAL USR2 diff --git a/src/os/unix/ngx_os.h b/src/os/unix/ngx_os.h --- a/src/os/unix/ngx_os.h +++ b/src/os/unix/ngx_os.h @@ -51,10 +51,11 @@ extern int ngx_inherited_nonblo extern ngx_int_t ngx_process; +extern ngx_int_t ngx_signal; extern ngx_int_t ngx_reap; extern ngx_int_t ngx_quit; extern ngx_int_t ngx_terminate; -extern ngx_int_t ngx_pause; +extern ngx_int_t ngx_noaccept; extern ngx_int_t ngx_reconfigure; extern ngx_int_t ngx_reopen; extern ngx_int_t ngx_change_binary; diff --git a/src/os/unix/ngx_posix_init.c b/src/os/unix/ngx_posix_init.c --- a/src/os/unix/ngx_posix_init.c +++ b/src/os/unix/ngx_posix_init.c @@ -26,8 +26,8 @@ ngx_signal_t signals[] = { "SIG" ngx_value(NGX_REOPEN_SIGNAL), ngx_signal_handler }, - { ngx_signal_value(NGX_PAUSE_SIGNAL), - "SIG" ngx_value(NGX_PAUSE_SIGNAL), + { ngx_signal_value(NGX_NOACCEPT_SIGNAL), + "SIG" ngx_value(NGX_NOACCEPT_SIGNAL), ngx_signal_handler }, { ngx_signal_value(NGX_TERMINATE_SIGNAL), @@ -96,6 +96,8 @@ void ngx_signal_handler(int signo) ngx_err_t err; ngx_signal_t *sig; + ngx_signal = 1; + err = ngx_errno; for (sig = signals; sig->signo != 0; sig++) { @@ -112,8 +114,6 @@ void ngx_signal_handler(int signo) switch (ngx_process) { case NGX_PROCESS_MASTER: - case NGX_PROCESS_QUITING: - case NGX_PROCESS_PAUSED: switch (signo) { case ngx_signal_value(NGX_SHUTDOWN_SIGNAL): @@ -122,13 +122,14 @@ void ngx_signal_handler(int signo) break; case ngx_signal_value(NGX_TERMINATE_SIGNAL): + case SIGINT: ngx_terminate = 1; action = ", exiting"; break; - case ngx_signal_value(NGX_PAUSE_SIGNAL): - ngx_pause = 1; - action = ", pausing"; + case ngx_signal_value(NGX_NOACCEPT_SIGNAL): + ngx_noaccept = 1; + action = ", stop the accepting connections"; break; case ngx_signal_value(NGX_RECONFIGURE_SIGNAL): @@ -162,6 +163,7 @@ void ngx_signal_handler(int signo) break; case ngx_signal_value(NGX_TERMINATE_SIGNAL): + case SIGINT: ngx_terminate = 1; action = ", exiting"; break; @@ -175,7 +177,7 @@ void ngx_signal_handler(int signo) case ngx_signal_value(NGX_RECONFIGURE_SIGNAL): case ngx_signal_value(NGX_REOPEN_SIGNAL): - case ngx_signal_value(NGX_PAUSE_SIGNAL): + case ngx_signal_value(NGX_NOACCEPT_SIGNAL): case ngx_signal_value(NGX_CHANGEBIN_SIGNAL): action = ", ignoring"; break; diff --git a/src/os/unix/ngx_process.c b/src/os/unix/ngx_process.c --- a/src/os/unix/ngx_process.c +++ b/src/os/unix/ngx_process.c @@ -9,7 +9,7 @@ ngx_uint_t ngx_last_process; ngx_process_t ngx_processes[NGX_MAX_PROCESSES]; -ngx_int_t ngx_spawn_process(ngx_cycle_t *cycle, +ngx_pid_t ngx_spawn_process(ngx_cycle_t *cycle, ngx_spawn_proc_pt proc, void *data, char *name, ngx_int_t respawn) { @@ -59,7 +59,7 @@ ngx_int_t ngx_spawn_process(ngx_cycle_t if (respawn >= 0) { ngx_processes[respawn].pid = pid; ngx_processes[respawn].exited = 0; - return NGX_OK; + return pid; } ngx_processes[ngx_last_process].pid = pid; @@ -81,7 +81,7 @@ ngx_int_t ngx_spawn_process(ngx_cycle_t return NGX_ERROR; } - return NGX_OK; + return pid; } diff --git a/src/os/unix/ngx_process.h b/src/os/unix/ngx_process.h --- a/src/os/unix/ngx_process.h +++ b/src/os/unix/ngx_process.h @@ -33,19 +33,17 @@ typedef struct { #define NGX_PROCESS_SINGLE 0 #define NGX_PROCESS_MASTER 1 #define NGX_PROCESS_WORKER 2 -#define NGX_PROCESS_QUITING 3 -#define NGX_PROCESS_PAUSED 4 #define NGX_MAX_PROCESSES 1024 -#define NGX_PROCESS_RESPAWN -1 -#define NGX_PROCESS_NORESPAWN -2 +#define NGX_PROCESS_NORESPAWN -1 +#define NGX_PROCESS_RESPAWN -2 #define NGX_PROCESS_DETACHED -3 #define ngx_getpid getpid -ngx_int_t ngx_spawn_process(ngx_cycle_t *cycle, +ngx_pid_t ngx_spawn_process(ngx_cycle_t *cycle, ngx_spawn_proc_pt proc, void *data, char *name, ngx_int_t respawn); ngx_int_t ngx_exec(ngx_cycle_t *cycle, ngx_exec_ctx_t *ctx);