diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/nginx.c | 35 | ||||
-rw-r--r-- | src/core/ngx_conf_file.c | 2 | ||||
-rw-r--r-- | src/core/ngx_cycle.c | 25 | ||||
-rw-r--r-- | src/core/ngx_log.c | 94 | ||||
-rw-r--r-- | src/core/ngx_log.h | 3 | ||||
-rw-r--r-- | src/os/unix/ngx_files.h | 6 | ||||
-rw-r--r-- | src/os/unix/ngx_os.h | 1 | ||||
-rw-r--r-- | src/os/win32/ngx_files.h | 5 | ||||
-rw-r--r-- | src/os/win32/ngx_os.h | 1 | ||||
-rw-r--r-- | src/os/win32/ngx_win32_init.c | 1 |
10 files changed, 113 insertions, 60 deletions
diff --git a/src/core/nginx.c b/src/core/nginx.c index 28fffa040..63185bcbf 100644 --- a/src/core/nginx.c +++ b/src/core/nginx.c @@ -207,10 +207,10 @@ main(int argc, char *const *argv) } if (ngx_show_version) { - ngx_log_stderr("nginx version: " NGINX_VER); + ngx_log_stderr(0, "nginx version: " NGINX_VER); if (ngx_show_help) { - ngx_log_stderr( + ngx_log_stderr(0, "Usage: nginx [-?hvVt] [-s signal] [-c filename] " "[-g directives]" CRLF CRLF "Options:" CRLF @@ -230,9 +230,9 @@ main(int argc, char *const *argv) if (ngx_show_configure) { #ifdef NGX_COMPILER - ngx_log_stderr("built by " NGX_COMPILER); + ngx_log_stderr(0, "built by " NGX_COMPILER); #endif - ngx_log_stderr("configure arguments: " NGX_CONFIGURE); + ngx_log_stderr(0, "configure arguments: " NGX_CONFIGURE); } if (!ngx_test_config) { @@ -308,7 +308,7 @@ main(int argc, char *const *argv) cycle = ngx_init_cycle(&init_cycle); if (cycle == NULL) { if (ngx_test_config) { - ngx_log_stderr("the configuration file %s test failed", + ngx_log_stderr(0, "configuration file %s test failed", init_cycle.conf_file.data); } @@ -316,7 +316,7 @@ main(int argc, char *const *argv) } if (ngx_test_config) { - ngx_log_stderr("the configuration file %s was tested successfully", + ngx_log_stderr(0, "configuration file %s test is successful", cycle->conf_file.data); return 0; } @@ -355,6 +355,17 @@ main(int argc, char *const *argv) return 1; } + if (cycle->log->file->fd != ngx_stderr) { + + if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) { + ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno, + ngx_set_stderr_n " failed"); + return 1; + } + } + + ngx_use_stderr = 0; + if (ngx_process == NGX_PROCESS_SINGLE) { ngx_single_process_cycle(cycle); @@ -622,7 +633,7 @@ ngx_get_options(int argc, char *const *argv) p = (u_char *) argv[i]; if (*p++ != '-') { - ngx_log_stderr("invalid option: \"%s\"", argv[i]); + ngx_log_stderr(0, "invalid option: \"%s\"", argv[i]); return NGX_ERROR; } @@ -660,7 +671,7 @@ ngx_get_options(int argc, char *const *argv) goto next; } - ngx_log_stderr("the option \"-c\" requires file name"); + ngx_log_stderr(0, "option \"-c\" requires file name"); return NGX_ERROR; case 'g': @@ -674,7 +685,7 @@ ngx_get_options(int argc, char *const *argv) goto next; } - ngx_log_stderr("the option \"-g\" requires parameter"); + ngx_log_stderr(0, "option \"-g\" requires parameter"); return NGX_ERROR; case 's': @@ -685,7 +696,7 @@ ngx_get_options(int argc, char *const *argv) ngx_signal = argv[i]; } else { - ngx_log_stderr("the option \"-s\" requires parameter"); + ngx_log_stderr(0, "option \"-s\" requires parameter"); return NGX_ERROR; } @@ -698,11 +709,11 @@ ngx_get_options(int argc, char *const *argv) goto next; } - ngx_log_stderr("invalid option: \"-s %s\"", ngx_signal); + ngx_log_stderr(0, "invalid option: \"-s %s\"", ngx_signal); return NGX_ERROR; default: - ngx_log_stderr("invalid option: \"%c\"", *(p - 1)); + ngx_log_stderr(0, "invalid option: \"%c\"", *(p - 1)); return NGX_ERROR; } } diff --git a/src/core/ngx_conf_file.c b/src/core/ngx_conf_file.c index 7ec574e0c..9b0f67cd1 100644 --- a/src/core/ngx_conf_file.c +++ b/src/core/ngx_conf_file.c @@ -892,7 +892,7 @@ ngx_conf_open_file(ngx_cycle_t *cycle, ngx_str_t *name) file->name = full; } else { - file->fd = ngx_stderr_fileno; + file->fd = ngx_stderr; file->name.len = 0; file->name.data = NULL; } diff --git a/src/core/ngx_cycle.c b/src/core/ngx_cycle.c index 476ba8328..0c0fd8ad9 100644 --- a/src/core/ngx_cycle.c +++ b/src/core/ngx_cycle.c @@ -270,7 +270,7 @@ ngx_init_cycle(ngx_cycle_t *old_cycle) } if (ngx_test_config) { - ngx_log_stderr("the configuration file %s syntax is ok", + ngx_log_stderr(0, "the configuration file %s syntax is ok", cycle->conf_file.data); } @@ -582,25 +582,14 @@ ngx_init_cycle(ngx_cycle_t *old_cycle) /* commit the new cycle configuration */ -#if !(NGX_WIN32) - - if (!ngx_test_config && cycle->log->file->fd != STDERR_FILENO) { + if (!ngx_use_stderr && cycle->log->file->fd != ngx_stderr) { - ngx_log_debug3(NGX_LOG_DEBUG_CORE, log, 0, - "dup2: %p %d \"%s\"", - cycle->log->file, - cycle->log->file->fd, cycle->log->file->name.data); - - if (dup2(cycle->log->file->fd, STDERR_FILENO) == -1) { - ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, - "dup2(STDERR) failed"); - /* fatal */ - exit(1); + if (ngx_set_stderr(cycle->log->file->fd) == NGX_FILE_ERROR) { + ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, + ngx_set_stderr_n " failed"); } } -#endif - pool->log = cycle->log; for (i = 0; ngx_modules[i]; i++) { @@ -697,7 +686,7 @@ old_shm_zone_done: i = 0; } - if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) { + if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) { continue; } @@ -797,7 +786,7 @@ failed: i = 0; } - if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr_fileno) { + if (file[i].fd == NGX_INVALID_FILE || file[i].fd == ngx_stderr) { continue; } diff --git a/src/core/ngx_log.c b/src/core/ngx_log.c index dabc8e4d4..209964058 100644 --- a/src/core/ngx_log.c +++ b/src/core/ngx_log.c @@ -48,7 +48,8 @@ ngx_module_t ngx_errlog_module = { static ngx_log_t ngx_log; -static ngx_open_file_t ngx_stderr; +static ngx_open_file_t ngx_log_file; +ngx_uint_t ngx_use_stderr = 1; static ngx_str_t err_levels[] = { @@ -86,7 +87,8 @@ ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, #if (NGX_HAVE_VARIADIC_MACROS) va_list args; #endif - u_char errstr[NGX_MAX_ERROR_STR], *p, *last; + u_char *p, *last, *msg; + u_char errstr[NGX_MAX_ERROR_STR]; if (log->file->fd == NGX_INVALID_FILE) { return; @@ -109,6 +111,8 @@ ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, p = ngx_snprintf(p, last - p, "*%uA ", log->connection); } + msg = p; + #if (NGX_HAVE_VARIADIC_MACROS) va_start(args, fmt); @@ -158,6 +162,19 @@ ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, ngx_linefeed(p); (void) ngx_write_fd(log->file->fd, errstr, p - errstr); + + if (!ngx_use_stderr + || level > NGX_LOG_WARN + || log->file->fd == ngx_stderr) + { + return; + } + + msg -= (err_levels[level].len + 4); + + (void) ngx_sprintf(msg, "[%V]: ", &err_levels[level]); + + ngx_write_fd(ngx_stderr, msg, p - msg); } @@ -198,9 +215,9 @@ ngx_log_abort(ngx_err_t err, const char *text, void *param) void ngx_cdecl -ngx_log_stderr(const char *fmt, ...) +ngx_log_stderr(ngx_err_t err, const char *fmt, ...) { - u_char *p; + u_char *p, *last; va_list args; u_char errstr[NGX_MAX_ERROR_STR]; @@ -212,46 +229,75 @@ ngx_log_stderr(const char *fmt, ...) p = errstr + NGX_MAX_ERROR_STR - NGX_LINEFEED_SIZE; } - ngx_linefeed(p); + if (err) { + + last = errstr + NGX_MAX_ERROR_STR; + + if (p > last - 50) { + + /* leave a space for an error code */ + + p = last - 50; + *p++ = '.'; + *p++ = '.'; + *p++ = '.'; + } #if (NGX_WIN32) + p = ngx_snprintf(p, last - p, ((unsigned) err < 0x80000000) + ? " (%d: " : " (%Xd: ", err); +#else + p = ngx_snprintf(p, last - p, " (%d: ", err); +#endif + + p = ngx_strerror_r(err, p, last - p); - if (ngx_stderr_fileno == NULL) { - ngx_stderr_fileno = GetStdHandle(STD_ERROR_HANDLE); + if (p < last) { + *p++ = ')'; + } } -#endif + ngx_linefeed(p); - (void) ngx_write_fd(ngx_stderr_fileno, errstr, p - errstr); + ngx_write_fd(ngx_stderr, errstr, p - errstr); } ngx_log_t * ngx_log_init(void) { - ngx_log.file = &ngx_stderr; + ngx_log.file = &ngx_log_file; ngx_log.log_level = NGX_LOG_NOTICE; -#if (NGX_WIN32) - - ngx_stderr.fd = ngx_open_file((u_char *) NGX_ERROR_LOG_PATH, - NGX_FILE_APPEND, - NGX_FILE_CREATE_OR_OPEN, - NGX_FILE_DEFAULT_ACCESS); + /* + * we use ngx_strlen() here since BCC warns about + * condition is always false and unreachable code + */ - if (ngx_stderr.fd == NGX_INVALID_FILE) { - ngx_event_log(ngx_errno, - "Could not open error log file: " - ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed"); - return NULL; + if (ngx_strlen(NGX_ERROR_LOG_PATH) == 0) { + ngx_log_file.fd = ngx_stderr; + return &ngx_log; } -#else + ngx_log_file.fd = ngx_open_file((u_char *) NGX_ERROR_LOG_PATH, + NGX_FILE_APPEND, + NGX_FILE_CREATE_OR_OPEN, + NGX_FILE_DEFAULT_ACCESS); - ngx_stderr.fd = STDERR_FILENO; + if (ngx_log_file.fd == NGX_INVALID_FILE) { + ngx_log_stderr(ngx_errno, + "[emerg]: could not open error log file: " + ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed"); +#if (NGX_WIN32) + ngx_event_log(ngx_errno, + "could not open error log file: " + ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed"); #endif + return NULL; + } + return &ngx_log; } @@ -349,7 +395,7 @@ ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) value = cf->args->elts; if (value[1].len == 6 && ngx_strcmp(value[1].data, "stderr") == 0) { - cf->cycle->new_log->file->fd = ngx_stderr.fd; + cf->cycle->new_log->file->fd = ngx_stderr; cf->cycle->new_log->file->name.len = 0; cf->cycle->new_log->file->name.data = NULL; diff --git a/src/core/ngx_log.h b/src/core/ngx_log.h index 53136d133..5168cd8e9 100644 --- a/src/core/ngx_log.h +++ b/src/core/ngx_log.h @@ -199,10 +199,11 @@ ngx_log_t *ngx_log_init(void); ngx_log_t *ngx_log_create_errlog(ngx_cycle_t *cycle, ngx_array_t *args); char *ngx_set_error_log_levels(ngx_conf_t *cf, ngx_log_t *log); void ngx_log_abort(ngx_err_t err, const char *text, void *param); -void ngx_cdecl ngx_log_stderr(const char *fmt, ...); +void ngx_cdecl ngx_log_stderr(ngx_err_t err, const char *fmt, ...); extern ngx_module_t ngx_errlog_module; +extern ngx_uint_t ngx_use_stderr; #endif /* _NGX_LOG_H_INCLUDED_ */ diff --git a/src/os/unix/ngx_files.h b/src/os/unix/ngx_files.h index c4d2b7f32..63d080b5c 100644 --- a/src/os/unix/ngx_files.h +++ b/src/os/unix/ngx_files.h @@ -273,8 +273,12 @@ ngx_int_t ngx_directio_off(ngx_fd_t fd); #endif - size_t ngx_fs_bsize(u_char *name); +#define ngx_stderr STDERR_FILENO +#define ngx_set_stderr(fd) dup2(fd, STDERR_FILENO) +#define ngx_set_stderr_n "dup2(STDERR_FILENO)" + + #endif /* _NGX_FILES_H_INCLUDED_ */ diff --git a/src/os/unix/ngx_os.h b/src/os/unix/ngx_os.h index a83c6ed8f..a8b6c6dc0 100644 --- a/src/os/unix/ngx_os.h +++ b/src/os/unix/ngx_os.h @@ -54,7 +54,6 @@ extern ngx_int_t ngx_max_sockets; extern ngx_uint_t ngx_inherited_nonblocking; extern ngx_uint_t ngx_tcp_nodelay_and_tcp_nopush; -#define ngx_stderr_fileno STDERR_FILENO #if (NGX_FREEBSD) #include <ngx_freebsd.h> diff --git a/src/os/win32/ngx_files.h b/src/os/win32/ngx_files.h index 636d14c19..f3561d9b3 100644 --- a/src/os/win32/ngx_files.h +++ b/src/os/win32/ngx_files.h @@ -237,4 +237,9 @@ ngx_int_t ngx_directio_off(ngx_fd_t fd); size_t ngx_fs_bsize(u_char *name); +#define ngx_stderr GetStdHandle(STD_ERROR_HANDLE) +#define ngx_set_stderr(fd) SetStdHandle(STD_ERROR_HANDLE, fd) +#define ngx_set_stderr_n "SetStdHandle(STD_ERROR_HANDLE)" + + #endif /* _NGX_FILES_H_INCLUDED_ */ diff --git a/src/os/win32/ngx_os.h b/src/os/win32/ngx_os.h index ba10c809a..e72af3884 100644 --- a/src/os/win32/ngx_os.h +++ b/src/os/win32/ngx_os.h @@ -58,7 +58,6 @@ extern ngx_int_t ngx_max_sockets; extern ngx_uint_t ngx_inherited_nonblocking; extern ngx_uint_t ngx_tcp_nodelay_and_tcp_nopush; extern ngx_uint_t ngx_win32_version; -extern ngx_fd_t ngx_stderr_fileno; extern char ngx_unique[]; diff --git a/src/os/win32/ngx_win32_init.c b/src/os/win32/ngx_win32_init.c index e64ce39fc..0924c3c70 100644 --- a/src/os/win32/ngx_win32_init.c +++ b/src/os/win32/ngx_win32_init.c @@ -16,7 +16,6 @@ ngx_int_t ngx_max_sockets; ngx_uint_t ngx_inherited_nonblocking = 1; ngx_uint_t ngx_tcp_nodelay_and_tcp_nopush; -ngx_fd_t ngx_stderr_fileno; char ngx_unique[NGX_INT32_LEN + 1]; |