diff options
Diffstat (limited to 'usr.sbin/nginx/src/core/ngx_log.c')
| -rw-r--r-- | usr.sbin/nginx/src/core/ngx_log.c | 385 |
1 files changed, 154 insertions, 231 deletions
diff --git a/usr.sbin/nginx/src/core/ngx_log.c b/usr.sbin/nginx/src/core/ngx_log.c index ddc2bbf6847..375d52f65d4 100644 --- a/usr.sbin/nginx/src/core/ngx_log.c +++ b/usr.sbin/nginx/src/core/ngx_log.c @@ -10,15 +10,8 @@ static char *ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); -#if (NGX_ENABLE_SYSLOG) -static char *ngx_set_syslog(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); -void log_exit(ngx_cycle_t *cycle); - -typedef struct{ - ngx_str_t name; - ngx_int_t macro; -} ngx_string_to_macro_t; -#endif +static char *ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log); +static void ngx_log_insert(ngx_log_t *log, ngx_log_t *new_log); static ngx_command_t ngx_errlog_commands[] = { @@ -30,15 +23,6 @@ static ngx_command_t ngx_errlog_commands[] = { 0, NULL}, -#if (NGX_ENABLE_SYSLOG) - {ngx_string("syslog"), - NGX_MAIN_CONF|NGX_CONF_TAKE12, - ngx_set_syslog, - 0, - 0, - NULL}, -#endif - ngx_null_command }; @@ -61,11 +45,7 @@ ngx_module_t ngx_errlog_module = { NULL, /* init thread */ NULL, /* exit thread */ NULL, /* exit process */ -#if (NGX_ENABLE_SYSLOG) - log_exit, /* exit master */ -#else - NULL, -#endif + NULL, /* exit master */ NGX_MODULE_V1_PADDING }; @@ -74,48 +54,6 @@ static ngx_log_t ngx_log; static ngx_open_file_t ngx_log_file; ngx_uint_t ngx_use_stderr = 1; -#if (NGX_ENABLE_SYSLOG) -static ngx_string_to_macro_t ngx_syslog_facilities[] = { - {ngx_string("auth"), LOG_AUTH}, -#if !(NGX_SOLARIS) - {ngx_string("authpriv"), LOG_AUTHPRIV}, -#endif - {ngx_string("cron"), LOG_CRON}, - {ngx_string("daemon"), LOG_DAEMON}, -#if !(NGX_SOLARIS) - {ngx_string("ftp"), LOG_FTP}, -#endif - {ngx_string("kern"), LOG_KERN}, - {ngx_string("local0"), LOG_LOCAL0}, - {ngx_string("local1"), LOG_LOCAL1}, - {ngx_string("local2"), LOG_LOCAL2}, - {ngx_string("local3"), LOG_LOCAL3}, - {ngx_string("local4"), LOG_LOCAL4}, - {ngx_string("local5"), LOG_LOCAL5}, - {ngx_string("local6"), LOG_LOCAL6}, - {ngx_string("local7"), LOG_LOCAL7}, - {ngx_string("lpr"), LOG_LPR}, - {ngx_string("mail"), LOG_MAIL}, - {ngx_string("news"), LOG_NEWS}, - {ngx_string("syslog"), LOG_SYSLOG}, - {ngx_string("user"), LOG_USER}, - {ngx_string("uucp"), LOG_UUCP}, - { ngx_null_string, 0} -}; - -static ngx_string_to_macro_t ngx_syslog_priorities[] = { - {ngx_string("emerg"), LOG_EMERG}, - {ngx_string("alert"), LOG_ALERT}, - {ngx_string("crit"), LOG_CRIT}, - {ngx_string("error"), LOG_ERR}, - {ngx_string("err"), LOG_ERR}, - {ngx_string("warn"), LOG_WARNING}, - {ngx_string("notice"),LOG_NOTICE}, - {ngx_string("info"), LOG_INFO}, - {ngx_string("debug"), LOG_DEBUG}, - { ngx_null_string, 0} -}; -#endif static ngx_str_t err_levels[] = { ngx_null_string, @@ -150,19 +88,11 @@ ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, #endif { #if (NGX_HAVE_VARIADIC_MACROS) - va_list args; -#endif - u_char *p, *last, *msg; -#if (NGX_ENABLE_SYSLOG) - u_char *errstr_syslog; -#endif - u_char errstr[NGX_MAX_ERROR_STR]; - -#if !(NGX_ENABLE_SYSLOG) - if (log->file->fd == NGX_INVALID_FILE) { - return; - } + va_list args; #endif + u_char *p, *last, *msg; + u_char errstr[NGX_MAX_ERROR_STR]; + ngx_uint_t wrote_stderr, debug_connection; last = errstr + NGX_MAX_ERROR_STR; @@ -171,10 +101,6 @@ ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, p = errstr + ngx_cached_err_log_time.len; -#if (NGX_ENABLE_SYSLOG) - errstr_syslog = p; -#endif - p = ngx_slprintf(p, last, " [%V] ", &err_levels[level]); /* pid#tid */ @@ -213,27 +139,33 @@ ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err, ngx_linefeed(p); -#if (NGX_ENABLE_SYSLOG) - if (log->file != NULL && log->file->name.len != 0) { - (void) ngx_write_fd(log->file->fd, errstr, p - errstr); - } + wrote_stderr = 0; + debug_connection = (log->log_level & NGX_LOG_DEBUG_CONNECTION) != 0; + + while (log) { + + if (log->log_level < level && !debug_connection) { + break; + } + + if (log->writer) { + log->writer(log, level, errstr, p - errstr); + log = log->next; + continue; + } + + (void) ngx_write_fd(log->file->fd, errstr, p - errstr); - /* Don't send the debug level info to syslog */ - if (log->syslog_on && level < NGX_LOG_DEBUG) { - /* write to syslog */ - syslog(log->priority, "%.*s", (int)(p - errstr_syslog), errstr_syslog); + if (log->file->fd == ngx_stderr) { + wrote_stderr = 1; + } + + log = log->next; } -#else - (void) ngx_write_fd(log->file->fd, errstr, p - errstr); -#endif if (!ngx_use_stderr || level > NGX_LOG_WARN -#if (NGX_ENABLE_SYSLOG) - || (log->file != NULL && log->file->fd == ngx_stderr)) -#else - || log->file->fd == ngx_stderr) -#endif + || wrote_stderr) { return; } @@ -437,75 +369,93 @@ ngx_log_init(u_char *prefix) } -ngx_log_t * -ngx_log_create(ngx_cycle_t *cycle, ngx_str_t *name) +ngx_int_t +ngx_log_open_default(ngx_cycle_t *cycle) { - ngx_log_t *log; + ngx_log_t *log; + static ngx_str_t error_log = ngx_string(NGX_ERROR_LOG_PATH); + + if (ngx_log_get_file_log(&cycle->new_log) != NULL) { + return NGX_OK; + } + + if (cycle->new_log.log_level != 0) { + /* there are some error logs, but no files */ - log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t)); - if (log == NULL) { - return NULL; + log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t)); + if (log == NULL) { + return NGX_ERROR; + } + + log->log_level = NGX_LOG_ERR; + ngx_log_insert(&cycle->new_log, log); + + } else { + /* no error logs at all */ + log = &cycle->new_log; + log->log_level = NGX_LOG_ERR; } - log->file = ngx_conf_open_file(cycle, name); + log->file = ngx_conf_open_file(cycle, &error_log); if (log->file == NULL) { - return NULL; + return NGX_ERROR; } - return log; + return NGX_OK; } -#if (NGX_ENABLE_SYSLOG) ngx_int_t -ngx_log_get_priority(ngx_conf_t *cf, ngx_str_t *priority) +ngx_log_redirect_stderr(ngx_cycle_t *cycle) { - ngx_int_t p = 0; - ngx_uint_t n, match = 0; - - for (n = 0; ngx_syslog_priorities[n].name.len != 0; n++) { - if (ngx_strncmp(priority->data, ngx_syslog_priorities[n].name.data, - ngx_syslog_priorities[n].name.len) == 0) { - p = ngx_syslog_priorities[n].macro; - match = 1; - } + ngx_fd_t fd; + + if (cycle->log_use_stderr) { + return NGX_OK; } - if (!match) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "invalid syslog priority \"%V\"", priority); - return -1; + /* file log always exists when we are called */ + fd = ngx_log_get_file_log(cycle->log)->file->fd; + + if (fd != ngx_stderr) { + if (ngx_set_stderr(fd) == NGX_FILE_ERROR) { + ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, + ngx_set_stderr_n " failed"); + + return NGX_ERROR; + } } - return p; + return NGX_OK; } -char * -ngx_log_set_priority(ngx_conf_t *cf, ngx_str_t *priority, ngx_log_t *log) +ngx_log_t * +ngx_log_get_file_log(ngx_log_t *head) { - log->priority = ERR_SYSLOG_PRIORITY; - - if (priority->len == 0) { - return NGX_CONF_OK; - } + ngx_log_t *log; - log->priority = ngx_log_get_priority(cf, priority); - if (log->priority == (-1)) { - return NGX_CONF_ERROR; + for (log = head; log; log = log->next) { + if (log->file != NULL) { + return log; + } } - return NGX_CONF_OK; + return NULL; } -#endif -char * +static char * ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log) { ngx_uint_t i, n, d, found; ngx_str_t *value; + if (cf->args->nelts == 2) { + log->log_level = NGX_LOG_ERR; + return NGX_CONF_OK; + } + value = cf->args->elts; for (i = 2; i < cf->args->nelts; i++) { @@ -561,136 +511,109 @@ ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log) static char * ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { - ngx_str_t *value, name; -#if (NGX_ENABLE_SYSLOG) - u_char *off = NULL; - ngx_str_t priority; + ngx_log_t *dummy; - ngx_str_null(&name); - ngx_str_null(&priority); -#endif + dummy = &cf->cycle->new_log; - if (cf->cycle->new_log.file) { - return "is duplicate"; - } + return ngx_log_set_log(cf, &dummy); +} - value = cf->args->elts; -#if (NGX_ENABLE_SYSLOG) - if (ngx_strncmp(value[1].data, "syslog", sizeof("syslog") - 1) == 0) { - if (!cf->cycle->new_log.syslog_set) { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "You must set the syslog directive and enable it first."); +char * +ngx_log_set_log(ngx_conf_t *cf, ngx_log_t **head) +{ + ngx_log_t *new_log; + ngx_str_t *value, name; + ngx_syslog_peer_t *peer; + + if (*head != NULL && (*head)->log_level == 0) { + new_log = *head; + + } else { + + new_log = ngx_pcalloc(cf->pool, sizeof(ngx_log_t)); + if (new_log == NULL) { return NGX_CONF_ERROR; } - cf->cycle->new_log.syslog_on = 1; + if (*head == NULL) { + *head = new_log; + } + } - if (value[1].data[sizeof("syslog") - 1] == ':') { - priority.len = value[1].len - sizeof("syslog"); - priority.data = value[1].data + sizeof("syslog"); + value = cf->args->elts; - off = (u_char *)ngx_strchr(priority.data, (int) '|'); - if (off != NULL) { - priority.len = off - priority.data; + if (ngx_strcmp(value[1].data, "stderr") == 0) { + ngx_str_null(&name); + cf->cycle->log_use_stderr = 1; - off++; - name.len = value[1].data + value[1].len - off; - name.data = off; - } + new_log->file = ngx_conf_open_file(cf->cycle, &name); + if (new_log->file == NULL) { + return NGX_CONF_ERROR; } - else { - if (value[1].len > sizeof("syslog")) { - name.len = value[1].len - sizeof("syslog"); - name.data = value[1].data + sizeof("syslog"); - } + + + } else if (ngx_strncmp(value[1].data, "syslog:", 7) == 0) { + peer = ngx_pcalloc(cf->pool, sizeof(ngx_syslog_peer_t)); + if (peer == NULL) { + return NGX_CONF_ERROR; } - if (ngx_log_set_priority(cf, &priority, &cf->cycle->new_log) == NGX_CONF_ERROR) { + if (ngx_syslog_process_conf(cf, peer) != NGX_CONF_OK) { return NGX_CONF_ERROR; } - } - else if (ngx_strcmp(value[1].data, "stderr") == 0) { -#else - if (ngx_strcmp(value[1].data, "stderr") == 0) { -#endif - ngx_str_null(&name); + + new_log->writer = ngx_syslog_writer; + new_log->wdata = peer; } else { - name = value[1]; + new_log->file = ngx_conf_open_file(cf->cycle, &value[1]); + if (new_log->file == NULL) { + return NGX_CONF_ERROR; + } } - cf->cycle->new_log.file = ngx_conf_open_file(cf->cycle, &name); - if (cf->cycle->new_log.file == NULL) { - return NULL; + if (ngx_log_set_levels(cf, new_log) != NGX_CONF_OK) { + return NGX_CONF_ERROR; } - if (cf->args->nelts == 2) { - cf->cycle->new_log.log_level = NGX_LOG_ERR; - return NGX_CONF_OK; + if (*head != new_log) { + ngx_log_insert(*head, new_log); } - cf->cycle->new_log.log_level = 0; - - return ngx_log_set_levels(cf, &cf->cycle->new_log); + return NGX_CONF_OK; } -#if (NGX_ENABLE_SYSLOG) - -#define SYSLOG_IDENT_NAME "nginx" - -static char * -ngx_set_syslog(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +static void +ngx_log_insert(ngx_log_t *log, ngx_log_t *new_log) { - char *program; - ngx_str_t *value; - ngx_int_t facility = SYSLOG_FACILITY, match = 0; - ngx_uint_t n; + ngx_log_t tmp; - value = cf->args->elts; + if (new_log->log_level > log->log_level) { - if (cf->cycle->new_log.syslog_set) { - return "is duplicate"; - } + /* + * list head address is permanent, insert new log after + * head and swap its contents with head + */ - cf->cycle->new_log.syslog_set = 1; + tmp = *log; + *log = *new_log; + *new_log = tmp; - for (n = 0; ngx_syslog_facilities[n].name.len != 0; n++) { - if (ngx_strncmp(value[1].data, ngx_syslog_facilities[n].name.data, - ngx_syslog_facilities[n].name.len) == 0) { - facility = ngx_syslog_facilities[n].macro; - match = 1; - break; - } + log->next = new_log; + return; } - if (match) { - cf->cycle->new_log.facility = facility; - cf->cycle->new_log.priority = ERR_SYSLOG_PRIORITY; - } - else { - ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, - "invalid syslog facility \"%V\"", &value[1]); - return NGX_CONF_ERROR; - } + while (log->next) { + if (new_log->log_level > log->next->log_level) { + new_log->next = log->next; + log->next = new_log; + return; + } - program = SYSLOG_IDENT_NAME; - if (cf->args->nelts > 2) { - program = (char *) value[2].data; + log = log->next; } - openlog(program, LOG_ODELAY, facility); - - return NGX_CONF_OK; + log->next = new_log; } - - -void log_exit(ngx_cycle_t *cycle) -{ - if (cycle->new_log.syslog_set) { - closelog(); - } -} -#endif - |
