From d8e1f07a93c014d57b7be0fd24fbee1978c95451 Mon Sep 17 00:00:00 2001 From: Igor Sysoev Date: Thu, 22 Jan 2004 06:47:28 +0000 Subject: [PATCH] nginx-0.0.1-2004-01-22-09:47:28 import --- src/core/nginx.c | 70 +++++---- src/core/ngx_config.h | 4 + src/core/ngx_connection.c | 2 +- src/core/ngx_cycle.c | 276 ----------------------------------- src/core/ngx_log.c | 105 +++++-------- src/event/ngx_event_accept.c | 5 +- src/http/ngx_http.c | 4 - src/os/unix/ngx_posix_init.c | 4 +- 8 files changed, 93 insertions(+), 377 deletions(-) diff --git a/src/core/nginx.c b/src/core/nginx.c index ba02b8062..f1315c875 100644 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -6,12 +6,13 @@ typedef struct { - int daemon; - int master; - uid_t user; - gid_t group; - ngx_str_t pid; - ngx_str_t newpid; + ngx_flag_t daemon; + ngx_flag_t master; + ngx_flag_t worker_reopen; + uid_t user; + gid_t group; + ngx_str_t pid; + ngx_str_t newpid; } ngx_core_conf_t; @@ -56,6 +57,13 @@ static ngx_command_t ngx_core_commands[] = { offsetof(ngx_core_conf_t, master), NULL }, + { ngx_string("worker_reopen"), + NGX_MAIN_CONF|NGX_CONF_TAKE1, + ngx_conf_set_core_flag_slot, + 0, + offsetof(ngx_core_conf_t, worker_reopen), + NULL }, + ngx_null_command }; @@ -221,12 +229,13 @@ int main(int argc, char *const *argv, char **envp) 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, sent; - ngx_msec_t delay; + int signo; + char *name; + sigset_t set, wset; + struct timeval tv; + ngx_uint_t i, live, sent; + ngx_msec_t delay; + ngx_core_conf_t *ccf; delay = 125; @@ -273,6 +282,9 @@ static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx) } } + ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, + ngx_core_module); + /* a cycle with the same configuration because a new one is invalid */ for ( ;; ) { @@ -340,10 +352,13 @@ static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx) "worker cycle"); ngx_process_events(cycle->log); + live = 0; } if (ngx_reap) { + ngx_reap = 0; ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, + "reap childs"); live = 0; @@ -438,8 +453,17 @@ static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx) } if (ngx_reopen) { - /* STUB */ - signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); + if (ngx_process == NGX_PROCESS_MASTER) { + if (ccf->worker_reopen > 0) { + signo = ngx_signal_value(NGX_REOPEN_SIGNAL); + ngx_reopen = 0; + } else { + signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL); + } + + } else { /* NGX_PROCESS_SINGLE */ + ngx_reopen = 0; + } ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reopening logs"); @@ -469,8 +493,6 @@ static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx) continue; } - sent = 1; - ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0, "kill (" PID_T_FMT ", %d)" , ngx_processes[i].pid, @@ -489,20 +511,12 @@ static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx) if (ngx_processes[i].signal != ngx_signal_value(NGX_REOPEN_SIGNAL)) { + sent = 1; ngx_processes[i].exiting = 1; } } - if (ngx_reap) { - ngx_reap = 0; - } - - /* STUB */ - if (ngx_reopen) { - break; - } - - if (ngx_reconfigure) { + if (ngx_reopen || ngx_reconfigure) { break; } } @@ -512,6 +526,7 @@ static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx) } else if (ngx_noaccept) { ngx_noaccept = 0; + ngx_reconfigure = 0; } else { cycle = ngx_init_cycle(cycle); @@ -521,9 +536,9 @@ static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx) } ngx_cycle = cycle; + ngx_reconfigure = 0; } - ngx_reconfigure = 0; break; } } @@ -752,6 +767,7 @@ static ngx_int_t ngx_core_module_init(ngx_cycle_t *cycle) */ ccf->daemon = NGX_CONF_UNSET; ccf->master = NGX_CONF_UNSET; + ccf->worker_reopen = NGX_CONF_UNSET; ccf->user = (uid_t) NGX_CONF_UNSET; ccf->group = (gid_t) NGX_CONF_UNSET; diff --git a/src/core/ngx_config.h b/src/core/ngx_config.h index 522c645cf..a1c93242e 100644 --- a/src/core/ngx_config.h +++ b/src/core/ngx_config.h @@ -43,6 +43,10 @@ typedef int ngx_int_t; typedef u_int ngx_uint_t; + +typedef int ngx_flag_t; + + /* STUB: autoconf */ #define PTR_FMT "%08X" diff --git a/src/core/ngx_connection.c b/src/core/ngx_connection.c index 7c515e642..6298ff160 100644 --- a/src/core/ngx_connection.c +++ b/src/core/ngx_connection.c @@ -4,7 +4,7 @@ #include -ngx_os_io_t ngx_io; +ngx_os_io_t ngx_io; ngx_int_t ngx_set_inherited_sockets(ngx_cycle_t *cycle) diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c index 12c053fa1..29a327054 100644 --- a/src/core/ngx_cycle.c +++ b/src/core/ngx_cycle.c @@ -362,242 +362,6 @@ ngx_cycle_t *ngx_init_cycle(ngx_cycle_t *old_cycle) } -#if 0 - - -static ngx_int_t ngx_set_inherited_sockets(ngx_cycle_t *cycle, char **envp) -{ - char *p, *v; - ngx_socket_t s; - ngx_listening_t *ls; - struct sockaddr_in *addr_in; - - for ( /* void */ ; *envp; envp++) { - if (ngx_strncmp(*envp, NGINX_VAR, NGINX_VAR_LEN) != 0) { - continue; - } - - ngx_log_error(NGX_LOG_INFO, cycle->log, 0, - "using inherited sockets from \"%s\"", *envp); - - ngx_init_array(cycle->listening, cycle->pool, - 10, sizeof(ngx_listening_t), NGX_ERROR); - - for (p = *envp + NGINX_VAR_LEN, v = p; *p; p++) { - if (*p == ':' || *p == ';') { - s = ngx_atoi(v, p - v); - if (s == NGX_ERROR) { - ngx_log_error(NGX_LOG_EMERG, cycle->log, 0, - "invalid socket number \"%s\" " - "in NGINX enviroment variable, " - "ignoring the rest of the variable", v); - break; - } - v = p + 1; - - if (!(ls = ngx_push_array(&cycle->listening))) { - return NGX_ERROR; - } - - ls->fd = s; - - /* AF_INET only */ - - ls->sockaddr = ngx_palloc(cycle->pool, - sizeof(struct sockaddr_in)); - if (ls->sockaddr == NULL) { - return NGX_ERROR; - } - - ls->socklen = sizeof(struct sockaddr_in); - if (getsockname(s, ls->sockaddr, &ls->socklen) == -1) { - ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno, - "getsockname() of the inherited " - "socket #%d failed", s); - ls->ignore = 1; - continue; - } - - addr_in = (struct sockaddr_in *) ls->sockaddr; - - if (addr_in->sin_family != AF_INET) { - ngx_log_error(NGX_LOG_CRIT, cycle->log, ngx_socket_errno, - "the inherited socket #%d has " - "unsupported family", s); - ls->ignore = 1; - continue; - } - ls->addr_text_max_len = INET_ADDRSTRLEN; - - ls->addr_text.data = ngx_palloc(cycle->pool, - ls->addr_text_max_len); - if (ls->addr_text.data == NULL) { - return NGX_ERROR; - } - - addr_in->sin_len = 0; - - ls->family = addr_in->sin_family; - ls->addr_text.len = ngx_sock_ntop(ls->family, ls->sockaddr, - ls->addr_text.data, - ls->addr_text_max_len); - if (ls->addr_text.len == 0) { - return NGX_ERROR; - } - } - } - - break; - } - - return NGX_OK; -} - - -static ngx_int_t ngx_open_listening_sockets(ngx_cycle_t *cycle) -{ - int tries, failed, reuseaddr, i; - ngx_err_t err; - ngx_log_t *log; - ngx_socket_t s; - ngx_listening_t *ls; - - reuseaddr = 1; -#if (NGX_SUPPRESS_WARN) - failed = 0; -#endif - - log = cycle->log; - - /* TODO: tries configurable */ - - for (tries = 10; tries; tries--) { - failed = 0; - - /* for each listening socket */ - - ls = cycle->listening.elts; - for (i = 0; i < cycle->listening.nelts; i++) { - - if (ls[i].ignore) { - continue; - } - - if (ls[i].fd != -1) { - continue; - } - - if (ls[i].inherited) { - - /* TODO: close on exit */ - /* TODO: nonblocking */ - /* TODO: deferred accept */ - - continue; - } - - s = ngx_socket(ls[i].family, ls[i].type, ls[i].protocol, - ls[i].flags); - - if (s == -1) { - ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, - ngx_socket_n " %s failed", ls[i].addr_text.data); - return NGX_ERROR; - } - -#if (WIN32) - /* - * Winsock assignes a socket number divisible by 4 - * so to find a connection we divide a socket number by 4. - */ - - if (s % 4) { - ngx_log_error(NGX_LOG_EMERG, ls->log, 0, - ngx_socket_n " created socket %d", s); - return NGX_ERROR; - } -#endif - - if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, - (const void *) &reuseaddr, sizeof(int)) == -1) { - ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, - "setsockopt(SO_REUSEADDR) %s failed", - ls[i].addr_text.data); - return NGX_ERROR; - } - - /* TODO: close on exit */ - - if (!(ngx_event_flags & NGX_USE_AIO_EVENT)) { - if (ngx_nonblocking(s) == -1) { - ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, - ngx_nonblocking_n " %s failed", - ls[i].addr_text.data); - return NGX_ERROR; - } - } - -#if 0 - if (ls[i].nonblocking) { - if (ngx_nonblocking(s) == -1) { - ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, - ngx_nonblocking_n " %s failed", - ls[i].addr_text.data); - return NGX_ERROR; - } - } -#endif - - if (bind(s, ls[i].sockaddr, ls[i].socklen) == -1) { - err = ngx_socket_errno; - ngx_log_error(NGX_LOG_EMERG, log, err, - "bind() to %s failed", ls[i].addr_text.data); - - if (err != NGX_EADDRINUSE) - return NGX_ERROR; - - if (ngx_close_socket(s) == -1) - ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, - ngx_close_socket_n " %s failed", - ls[i].addr_text.data); - - failed = 1; - continue; - } - - if (listen(s, ls[i].backlog) == -1) { - ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, - "listen() to %s failed", ls[i].addr_text.data); - return NGX_ERROR; - } - - /* TODO: deferred accept */ - - ls[i].fd = s; - } - - if (!failed) - break; - - /* TODO: delay configurable */ - - ngx_log_error(NGX_LOG_NOTICE, log, 0, - "try again to bind() after 500ms"); - ngx_msleep(500); - } - - if (failed) { - ngx_log_error(NGX_LOG_EMERG, log, 0, "still can not bind()"); - return NGX_ERROR; - } - - return NGX_OK; -} - - -#endif - - void ngx_reopen_files(ngx_cycle_t *cycle) { ngx_fd_t fd; @@ -664,46 +428,6 @@ void ngx_reopen_files(ngx_cycle_t *cycle) } -#if 0 - -static void ngx_close_listening_sockets(ngx_cycle_t *cycle) -{ - ngx_int_t i; - ngx_socket_t fd; - ngx_listening_t *ls; - - if (ngx_event_flags & NGX_USE_IOCP_EVENT) { - return; - } - - ls = cycle->listening.elts; - for (i = 0; i < cycle->listening.nelts; i++) { - fd = ls[i].fd; - -#if (WIN32) - /* - * Winsock assignes a socket number divisible by 4 - * so to find a connection we divide a socket number by 4. - */ - - fd /= 4; -#endif - - ngx_del_event(&cycle->read_events[fd], NGX_READ_EVENT, NGX_CLOSE_EVENT); - - if (ngx_close_socket(ls[i].fd) == -1) { - ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_socket_errno, - ngx_close_socket_n " %s failed", - ls[i].addr_text.data); - } - - cycle->connections[fd].fd = -1; - } -} - -#endif - - static void ngx_clean_old_cycles(ngx_event_t *ev) { int i, n, found, live; diff --git a/src/core/ngx_log.c b/src/core/ngx_log.c index caf246a81..1f7bb11a9 100644 --- a/src/core/ngx_log.c +++ b/src/core/ngx_log.c @@ -3,7 +3,7 @@ #include -ngx_inline static int ngx_log_is_full(ngx_log_t *log, char *errstr, size_t len); +static void ngx_log_write(ngx_log_t *log, char *errstr, size_t len); static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); @@ -55,13 +55,10 @@ void ngx_log_error_core(int level, ngx_log_t *log, ngx_err_t err, #endif { char errstr[MAX_ERROR_STR]; - size_t len; + size_t len, max; #if (HAVE_VARIADIC_MACROS) va_list args; #endif -#if (WIN32) - u_long written; -#endif if (log->file->fd == NGX_INVALID_FILE) { return; @@ -70,37 +67,42 @@ void ngx_log_error_core(int level, ngx_log_t *log, ngx_err_t err, ngx_memcpy(errstr, ngx_cached_err_log_time.data, ngx_cached_err_log_time.len); +#if (WIN32) + max = MAX_ERROR_STR - 2; +#else + max = MAX_ERROR_STR - 1; +#endif + len = ngx_cached_err_log_time.len; - len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, - " [%s] ", err_levels[level]); + len += ngx_snprintf(errstr + len, max - len, " [%s] ", err_levels[level]); /* pid#tid */ - len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, + len += ngx_snprintf(errstr + len, max - len, PID_T_FMT "#%d: ", ngx_getpid(), /* STUB */ 0); if (log->data) { - len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, + len += ngx_snprintf(errstr + len, max - len, "*%u ", * (u_int *) log->data); } #if (HAVE_VARIADIC_MACROS) va_start(args, fmt); - len += ngx_vsnprintf(errstr + len, sizeof(errstr) - len - 1, fmt, args); + len += ngx_vsnprintf(errstr + len, max - len, fmt, args); va_end(args); #else - len += ngx_vsnprintf(errstr + len, sizeof(errstr) - len - 1, fmt, args); + len += ngx_vsnprintf(errstr + len, max - len, fmt, args); #endif if (err) { - if (len > sizeof(errstr) - 50) { + if (len > max - 50) { /* leave a space for an error code */ - len = sizeof(errstr) - 50; + len = max - 50; errstr[len++] = '.'; errstr[len++] = '.'; errstr[len++] = '.'; @@ -108,48 +110,56 @@ void ngx_log_error_core(int level, ngx_log_t *log, ngx_err_t err, #if (WIN32) if ((unsigned) err >= 0x80000000) { - len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, - " (%X: ", err); + len += ngx_snprintf(errstr + len, max - len, " (%X: ", err); } else { - len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, - " (%d: ", err); + len += ngx_snprintf(errstr + len, max - len, " (%d: ", err); } #else - len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, - " (%d: ", err); + len += ngx_snprintf(errstr + len, max - len, " (%d: ", err); #endif - if (ngx_log_is_full(log, errstr, len)) { + if (len >= max) { + ngx_log_write(log, errstr, max); return; } - len += ngx_strerror_r(err, errstr + len, sizeof(errstr) - len - 1); + len += ngx_strerror_r(err, errstr + len, max - len); - if (ngx_log_is_full(log, errstr, len)) { + if (len >= max) { + ngx_log_write(log, errstr, max); return; } errstr[len++] = ')'; - if (ngx_log_is_full(log, errstr, len)) { + if (len >= max) { + ngx_log_write(log, errstr, max); return; } } else { - if (ngx_log_is_full(log, errstr, len)) { + if (len >= max) { + ngx_log_write(log, errstr, max); return; } } if (level != NGX_LOG_DEBUG && log->handler) { - len += log->handler(log->data, errstr + len, sizeof(errstr) - len - 1); + len += log->handler(log->data, errstr + len, max - len); - if (ngx_log_is_full(log, errstr, len)) { - return; + if (len >= max) { + len = max; } } + ngx_log_write(log, errstr, len); +} + + +static void ngx_log_write(ngx_log_t *log, char *errstr, size_t len) +{ #if (WIN32) + u_long written; errstr[len++] = CR; errstr[len++] = LF; @@ -164,38 +174,6 @@ void ngx_log_error_core(int level, ngx_log_t *log, ngx_err_t err, } -ngx_inline static int ngx_log_is_full(ngx_log_t *log, char *errstr, size_t len) -{ -#if (WIN32) - u_long written; - - if (len > MAX_ERROR_STR - 2) { - len = MAX_ERROR_STR - 2; - - errstr[len++] = CR; - errstr[len++] = LF; - WriteFile(log->file->fd, errstr, len, &written, NULL); - - return 1; - } - -#else - - if (len > MAX_ERROR_STR - 1) { - len = MAX_ERROR_STR - 1; - - errstr[len++] = LF; - write(log->file->fd, errstr, len); - - return 1; - } - -#endif - - return 0; -} - - #if !(HAVE_VARIADIC_MACROS) void ngx_log_error(int level, ngx_log_t *log, ngx_err_t err, @@ -320,7 +298,7 @@ ngx_log_t *ngx_log_create_errlog(ngx_cycle_t *cycle, ngx_array_t *args) ngx_test_null(log, ngx_pcalloc(cycle->pool, sizeof(ngx_log_t)), NULL); ngx_test_null(log->file, ngx_conf_open_file(cycle, name), NULL); -#if 1 +#if 0 /* STUB */ log->log_level = NGX_LOG_DEBUG | NGX_LOG_DEBUG_CORE | NGX_LOG_DEBUG_ALLOC | NGX_LOG_DEBUG_EVENT | NGX_LOG_DEBUG_HTTP; #endif @@ -369,9 +347,8 @@ char *ngx_set_error_log_levels(ngx_conf_t *cf, ngx_log_t *log) } } - d = NGX_LOG_DEBUG_FIRST; - for (n = 0; n < /* STUB */ 4; n++) { - if (ngx_strcmp(value[i].data, debug_levels[n]) == 0) { + for (n = 0, d = NGX_LOG_DEBUG_FIRST; d <= NGX_LOG_DEBUG_LAST; d <<= 1) { + if (ngx_strcmp(value[i].data, debug_levels[n++]) == 0) { if (log->log_level & ~NGX_LOG_DEBUG_ALL) { ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid log level \"%s\"", @@ -381,8 +358,6 @@ char *ngx_set_error_log_levels(ngx_conf_t *cf, ngx_log_t *log) log->log_level |= d; } - - d <<= 1; } diff --git a/src/event/ngx_event_accept.c b/src/event/ngx_event_accept.c index a5a130fde..1c1066263 100644 --- a/src/event/ngx_event_accept.c +++ b/src/event/ngx_event_accept.c @@ -210,7 +210,10 @@ void ngx_event_accept(ngx_event_t *ev) c->number = ngx_connection_counter++; - ngx_log_debug(ev->log, "accept: %d, %d" _ s _ c->number); + ngx_log_debug(ev->log, "LOG: %x" _ ev->log->log_level); + + ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, + "accept: %d, %d", s, c->number); if (ev->deferred_accept) { rev->ready = 1; diff --git a/src/http/ngx_http.c b/src/http/ngx_http.c index 8aeefac5f..818dda972 100644 --- a/src/http/ngx_http.c +++ b/src/http/ngx_http.c @@ -501,10 +501,6 @@ static char *ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ls->handler = ngx_http_init_connection; -#if 0 - ls->log = cf->cycle->log; -#endif - cscf = in_addr[a].core_srv_conf; ls->pool_size = cscf->connection_pool_size; ls->post_accept_timeout = cscf->post_accept_timeout; diff --git a/src/os/unix/ngx_posix_init.c b/src/os/unix/ngx_posix_init.c index b23dc3122..bc0eb5151 100644 --- a/src/os/unix/ngx_posix_init.c +++ b/src/os/unix/ngx_posix_init.c @@ -118,6 +118,7 @@ void ngx_signal_handler(int signo) switch (ngx_process) { case NGX_PROCESS_MASTER: + case NGX_PROCESS_SINGLE: switch (signo) { case ngx_signal_value(NGX_SHUTDOWN_SIGNAL): @@ -192,15 +193,12 @@ void ngx_signal_handler(int signo) action = ", exiting"; break; -#if 0 case ngx_signal_value(NGX_REOPEN_SIGNAL): ngx_reopen = 1; action = ", reopen logs"; break; -#endif case ngx_signal_value(NGX_RECONFIGURE_SIGNAL): - case ngx_signal_value(NGX_REOPEN_SIGNAL): case ngx_signal_value(NGX_NOACCEPT_SIGNAL): case ngx_signal_value(NGX_CHANGEBIN_SIGNAL): action = ", ignoring";