# HG changeset patch # User Igor Sysoev # Date 1075322334 0 # Node ID bf2faf694c194267f0226c9ab0de79bb9cdb9251 # Parent 5d4f49dc8de582dad93fe1ea672a34f4ca154a87 nginx-0.0.1-2004-01-28-23:38:54 import diff --git a/src/core/nginx.c b/src/core/nginx.c --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -85,8 +85,8 @@ ngx_int_t ngx_process; ngx_pid_t ngx_new_binary; ngx_int_t ngx_inherited; -ngx_int_t ngx_signal; ngx_int_t ngx_reap; +ngx_int_t ngx_timer; ngx_int_t ngx_terminate; ngx_int_t ngx_quit; ngx_int_t ngx_noaccept; @@ -229,16 +229,18 @@ int main(int argc, char *const *argv, ch static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx) { - int signo; - char *name; - sigset_t set, wset; - struct timeval tv; - ngx_uint_t i, live; - ngx_msec_t delay; - ngx_core_conf_t *ccf; + int signo; + sigset_t set; + struct timeval tv; + struct itimerval itv; + ngx_uint_t i, live; + ngx_msec_t delay; + ngx_core_conf_t *ccf; sigemptyset(&set); sigaddset(&set, SIGCHLD); + sigaddset(&set, SIGALRM); + sigaddset(&set, SIGINT); sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL)); sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL)); sigaddset(&set, ngx_signal_value(NGX_NOACCEPT_SIGNAL)); @@ -246,16 +248,15 @@ static void ngx_master_process_cycle(ngx sigaddset(&set, ngx_signal_value(NGX_SHUTDOWN_SIGNAL)); sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL)); - sigemptyset(&wset); - if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, "sigprocmask() failed"); } + sigemptyset(&set); + ngx_setproctitle("master process"); - ngx_signal = 0; ngx_new_binary = 0; delay = 0; signo = 0; @@ -267,6 +268,23 @@ static void ngx_master_process_cycle(ngx if (ngx_process == NGX_PROCESS_MASTER) { ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL, "worker process", NGX_PROCESS_RESPAWN); + + /* + * we have to limit the maximum life time of the worker processes + * by 1 month because our millisecond event timer is limited + * by 49 days on 32-bit platforms + */ + + itv.it_interval.tv_sec = 0; + itv.it_interval.tv_usec = 0; + itv.it_value.tv_sec = 30 * 24 * 60 * 60; + itv.it_value.tv_usec = 0; + + if (setitimer(ITIMER_REAL, &itv, NULL) == -1) { + ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, + "setitimer() failed"); + } + live = 1; } else { @@ -295,55 +313,32 @@ static void ngx_master_process_cycle(ngx if (ngx_process == NGX_PROCESS_MASTER) { if (delay) { - ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, - "temination cycle"); + delay *= 2; - if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) { - ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, - "sigprocmask() failed"); - continue; - } + ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, + "temination cycle: %d", delay); - /* - * there is very big chance that the pending signals - * would be delivered right on the sigprocmask() return - */ - - if (!ngx_signal) { - - delay *= 2; - - ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, - "msleep %d", delay); - - ngx_msleep(delay); + itv.it_interval.tv_sec = 0; + itv.it_interval.tv_usec = 0; + itv.it_value.tv_sec = delay / 1000; + itv.it_value.tv_usec = (delay % 1000 ) * 1000; - ngx_gettimeofday(&tv); - ngx_time_update(tv.tv_sec); - - ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, - "wake up"); + if (setitimer(ITIMER_REAL, &itv, NULL) == -1) { + ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, + "setitimer() failed"); } - - if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { - ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, - "sigprocmask() failed"); - } + } - ngx_signal = 0; + ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, + "sigsuspend"); - } else { - ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, - "sigsuspend"); + sigsuspend(&set); - sigsuspend(&wset); + ngx_gettimeofday(&tv); + ngx_time_update(tv.tv_sec); - ngx_gettimeofday(&tv); - ngx_time_update(tv.tv_sec); - - ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, - "wake up"); - } + ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, + "wake up"); } else { /* NGX_PROCESS_SINGLE */ ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, @@ -430,6 +425,9 @@ static void ngx_master_process_cycle(ngx } else if (ngx_quit) { signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); + } else if (ngx_timer) { + signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); + } else { if (ngx_noaccept) { @@ -512,7 +510,7 @@ static void ngx_master_process_cycle(ngx } } - if (ngx_reopen || ngx_reconfigure) { + if (ngx_reopen || ngx_reconfigure || ngx_timer) { break; } } @@ -520,6 +518,9 @@ static void ngx_master_process_cycle(ngx if (ngx_reopen) { ngx_reopen = 0; + } else if (ngx_timer) { + ngx_timer = 0; + } else if (ngx_noaccept) { ngx_noaccept = 0; ngx_reconfigure = 0; diff --git a/src/event/modules/ngx_devpoll_module.c b/src/event/modules/ngx_devpoll_module.c --- a/src/event/modules/ngx_devpoll_module.c +++ b/src/event/modules/ngx_devpoll_module.c @@ -1,6 +1,6 @@ /* - * Copyright (C) 2002-2003 Igor Sysoev, http://sysoev.ru + * Copyright (C) 2002-2004 Igor Sysoev, http://sysoev.ru/en/ */ diff --git a/src/http/modules/ngx_http_gzip_filter.c b/src/http/modules/ngx_http_gzip_filter.c --- a/src/http/modules/ngx_http_gzip_filter.c +++ b/src/http/modules/ngx_http_gzip_filter.c @@ -240,11 +240,9 @@ static int ngx_http_gzip_body_filter(ngx ngx_http_gzip_ctx_t *ctx; ngx_http_gzip_conf_t *conf; - if (!(ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module))) { - return ngx_http_next_body_filter(r, in); - } + ctx = ngx_http_get_module_ctx(r, ngx_http_gzip_filter_module); - if (ctx->done) { + if (ctx == NULL || ctx->done) { return ngx_http_next_body_filter(r, in); } diff --git a/src/http/modules/ngx_http_ssi_filter.c b/src/http/modules/ngx_http_ssi_filter.c --- a/src/http/modules/ngx_http_ssi_filter.c +++ b/src/http/modules/ngx_http_ssi_filter.c @@ -193,7 +193,7 @@ static int ngx_http_ssi_body_filter(ngx_ ctx = ngx_http_get_module_ctx(r, ngx_http_ssi_filter_module); if (ctx == NULL || (in == NULL && ctx->in == NULL)) { - return ngx_http_next_body_filter(r, NULL); + return ngx_http_next_body_filter(r, in); } /* add the incoming hunk to the chain ctx->in */ 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 @@ -53,8 +53,8 @@ extern ngx_int_t ngx_process; extern ngx_pid_t ngx_new_binary; extern ngx_int_t ngx_inherited; -extern ngx_int_t ngx_signal; extern ngx_int_t ngx_reap; +extern ngx_int_t ngx_timer; extern ngx_int_t ngx_quit; extern ngx_int_t ngx_terminate; extern ngx_int_t ngx_noaccept; 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 @@ -42,6 +42,8 @@ ngx_signal_t signals[] = { "SIG" ngx_value(NGX_CHANGEBIN_SIGNAL), ngx_signal_handler }, + { SIGALRM, "SIGALRM", ngx_signal_handler }, + { SIGINT, "SIGINT", ngx_signal_handler }, { SIGCHLD, "SIGCHLD", ngx_signal_handler }, @@ -99,7 +101,6 @@ void ngx_signal_handler(int signo) ngx_err_t err; ngx_signal_t *sig; - ngx_signal = 1; ignore = 0; err = ngx_errno; @@ -172,6 +173,14 @@ void ngx_signal_handler(int signo) action = ", changing binary"; break; + case SIGALRM: + if (!ngx_terminate) { + ngx_timer = 1; + action = ", shutting down old worker process"; + } + + break; + case SIGCHLD: ngx_reap = 1; break;