Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 
0011 
0012 static char *ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
0013 static char *ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log);
0014 static void ngx_log_insert(ngx_log_t *log, ngx_log_t *new_log);
0015 
0016 
0017 #if (NGX_DEBUG)
0018 
0019 static void ngx_log_memory_writer(ngx_log_t *log, ngx_uint_t level,
0020     u_char *buf, size_t len);
0021 static void ngx_log_memory_cleanup(void *data);
0022 
0023 
0024 typedef struct {
0025     u_char        *start;
0026     u_char        *end;
0027     u_char        *pos;
0028     ngx_atomic_t   written;
0029 } ngx_log_memory_buf_t;
0030 
0031 #endif
0032 
0033 
0034 static ngx_command_t  ngx_errlog_commands[] = {
0035 
0036     { ngx_string("error_log"),
0037       NGX_MAIN_CONF|NGX_CONF_1MORE,
0038       ngx_error_log,
0039       0,
0040       0,
0041       NULL },
0042 
0043       ngx_null_command
0044 };
0045 
0046 
0047 static ngx_core_module_t  ngx_errlog_module_ctx = {
0048     ngx_string("errlog"),
0049     NULL,
0050     NULL
0051 };
0052 
0053 
0054 ngx_module_t  ngx_errlog_module = {
0055     NGX_MODULE_V1,
0056     &ngx_errlog_module_ctx,                /* module context */
0057     ngx_errlog_commands,                   /* module directives */
0058     NGX_CORE_MODULE,                       /* module type */
0059     NULL,                                  /* init master */
0060     NULL,                                  /* init module */
0061     NULL,                                  /* init process */
0062     NULL,                                  /* init thread */
0063     NULL,                                  /* exit thread */
0064     NULL,                                  /* exit process */
0065     NULL,                                  /* exit master */
0066     NGX_MODULE_V1_PADDING
0067 };
0068 
0069 
0070 static ngx_log_t        ngx_log;
0071 static ngx_open_file_t  ngx_log_file;
0072 ngx_uint_t              ngx_use_stderr = 1;
0073 
0074 
0075 static ngx_str_t err_levels[] = {
0076     ngx_null_string,
0077     ngx_string("emerg"),
0078     ngx_string("alert"),
0079     ngx_string("crit"),
0080     ngx_string("error"),
0081     ngx_string("warn"),
0082     ngx_string("notice"),
0083     ngx_string("info"),
0084     ngx_string("debug")
0085 };
0086 
0087 static const char *debug_levels[] = {
0088     "debug_core", "debug_alloc", "debug_mutex", "debug_event",
0089     "debug_http", "debug_mail", "debug_stream"
0090 };
0091 
0092 
0093 #if (NGX_HAVE_VARIADIC_MACROS)
0094 
0095 void
0096 ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
0097     const char *fmt, ...)
0098 
0099 #else
0100 
0101 void
0102 ngx_log_error_core(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
0103     const char *fmt, va_list args)
0104 
0105 #endif
0106 {
0107 #if (NGX_HAVE_VARIADIC_MACROS)
0108     va_list      args;
0109 #endif
0110     u_char      *p, *last, *msg;
0111     ssize_t      n;
0112     ngx_uint_t   wrote_stderr, debug_connection;
0113     u_char       errstr[NGX_MAX_ERROR_STR];
0114 
0115     last = errstr + NGX_MAX_ERROR_STR;
0116 
0117     p = ngx_cpymem(errstr, ngx_cached_err_log_time.data,
0118                    ngx_cached_err_log_time.len);
0119 
0120     p = ngx_slprintf(p, last, " [%V] ", &err_levels[level]);
0121 
0122     /* pid#tid */
0123     p = ngx_slprintf(p, last, "%P#" NGX_TID_T_FMT ": ",
0124                     ngx_log_pid, ngx_log_tid);
0125 
0126     if (log->connection) {
0127         p = ngx_slprintf(p, last, "*%uA ", log->connection);
0128     }
0129 
0130     msg = p;
0131 
0132 #if (NGX_HAVE_VARIADIC_MACROS)
0133 
0134     va_start(args, fmt);
0135     p = ngx_vslprintf(p, last, fmt, args);
0136     va_end(args);
0137 
0138 #else
0139 
0140     p = ngx_vslprintf(p, last, fmt, args);
0141 
0142 #endif
0143 
0144     if (err) {
0145         p = ngx_log_errno(p, last, err);
0146     }
0147 
0148     if (level != NGX_LOG_DEBUG && log->handler) {
0149         p = log->handler(log, p, last - p);
0150     }
0151 
0152     if (p > last - NGX_LINEFEED_SIZE) {
0153         p = last - NGX_LINEFEED_SIZE;
0154     }
0155 
0156     ngx_linefeed(p);
0157 
0158     wrote_stderr = 0;
0159     debug_connection = (log->log_level & NGX_LOG_DEBUG_CONNECTION) != 0;
0160 
0161     while (log) {
0162 
0163         if (log->log_level < level && !debug_connection) {
0164             break;
0165         }
0166 
0167         if (log->writer) {
0168             log->writer(log, level, errstr, p - errstr);
0169             goto next;
0170         }
0171 
0172         if (ngx_time() == log->disk_full_time) {
0173 
0174             /*
0175              * on FreeBSD writing to a full filesystem with enabled softupdates
0176              * may block process for much longer time than writing to non-full
0177              * filesystem, so we skip writing to a log for one second
0178              */
0179 
0180             goto next;
0181         }
0182 
0183         n = ngx_write_fd(log->file->fd, errstr, p - errstr);
0184 
0185         if (n == -1 && ngx_errno == NGX_ENOSPC) {
0186             log->disk_full_time = ngx_time();
0187         }
0188 
0189         if (log->file->fd == ngx_stderr) {
0190             wrote_stderr = 1;
0191         }
0192 
0193     next:
0194 
0195         log = log->next;
0196     }
0197 
0198     if (!ngx_use_stderr
0199         || level > NGX_LOG_WARN
0200         || wrote_stderr)
0201     {
0202         return;
0203     }
0204 
0205     msg -= (7 + err_levels[level].len + 3);
0206 
0207     (void) ngx_sprintf(msg, "nginx: [%V] ", &err_levels[level]);
0208 
0209     (void) ngx_write_console(ngx_stderr, msg, p - msg);
0210 }
0211 
0212 
0213 #if !(NGX_HAVE_VARIADIC_MACROS)
0214 
0215 void ngx_cdecl
0216 ngx_log_error(ngx_uint_t level, ngx_log_t *log, ngx_err_t err,
0217     const char *fmt, ...)
0218 {
0219     va_list  args;
0220 
0221     if (log->log_level >= level) {
0222         va_start(args, fmt);
0223         ngx_log_error_core(level, log, err, fmt, args);
0224         va_end(args);
0225     }
0226 }
0227 
0228 
0229 void ngx_cdecl
0230 ngx_log_debug_core(ngx_log_t *log, ngx_err_t err, const char *fmt, ...)
0231 {
0232     va_list  args;
0233 
0234     va_start(args, fmt);
0235     ngx_log_error_core(NGX_LOG_DEBUG, log, err, fmt, args);
0236     va_end(args);
0237 }
0238 
0239 #endif
0240 
0241 
0242 void ngx_cdecl
0243 ngx_log_abort(ngx_err_t err, const char *fmt, ...)
0244 {
0245     u_char   *p;
0246     va_list   args;
0247     u_char    errstr[NGX_MAX_CONF_ERRSTR];
0248 
0249     va_start(args, fmt);
0250     p = ngx_vsnprintf(errstr, sizeof(errstr) - 1, fmt, args);
0251     va_end(args);
0252 
0253     ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,
0254                   "%*s", p - errstr, errstr);
0255 }
0256 
0257 
0258 void ngx_cdecl
0259 ngx_log_stderr(ngx_err_t err, const char *fmt, ...)
0260 {
0261     u_char   *p, *last;
0262     va_list   args;
0263     u_char    errstr[NGX_MAX_ERROR_STR];
0264 
0265     last = errstr + NGX_MAX_ERROR_STR;
0266 
0267     p = ngx_cpymem(errstr, "nginx: ", 7);
0268 
0269     va_start(args, fmt);
0270     p = ngx_vslprintf(p, last, fmt, args);
0271     va_end(args);
0272 
0273     if (err) {
0274         p = ngx_log_errno(p, last, err);
0275     }
0276 
0277     if (p > last - NGX_LINEFEED_SIZE) {
0278         p = last - NGX_LINEFEED_SIZE;
0279     }
0280 
0281     ngx_linefeed(p);
0282 
0283     (void) ngx_write_console(ngx_stderr, errstr, p - errstr);
0284 }
0285 
0286 
0287 u_char *
0288 ngx_log_errno(u_char *buf, u_char *last, ngx_err_t err)
0289 {
0290     if (buf > last - 50) {
0291 
0292         /* leave a space for an error code */
0293 
0294         buf = last - 50;
0295         *buf++ = '.';
0296         *buf++ = '.';
0297         *buf++ = '.';
0298     }
0299 
0300 #if (NGX_WIN32)
0301     buf = ngx_slprintf(buf, last, ((unsigned) err < 0x80000000)
0302                                        ? " (%d: " : " (%Xd: ", err);
0303 #else
0304     buf = ngx_slprintf(buf, last, " (%d: ", err);
0305 #endif
0306 
0307     buf = ngx_strerror(err, buf, last - buf);
0308 
0309     if (buf < last) {
0310         *buf++ = ')';
0311     }
0312 
0313     return buf;
0314 }
0315 
0316 
0317 ngx_log_t *
0318 ngx_log_init(u_char *prefix)
0319 {
0320     u_char  *p, *name;
0321     size_t   nlen, plen;
0322 
0323     ngx_log.file = &ngx_log_file;
0324     ngx_log.log_level = NGX_LOG_NOTICE;
0325 
0326     name = (u_char *) NGX_ERROR_LOG_PATH;
0327 
0328     /*
0329      * we use ngx_strlen() here since BCC warns about
0330      * condition is always false and unreachable code
0331      */
0332 
0333     nlen = ngx_strlen(name);
0334 
0335     if (nlen == 0) {
0336         ngx_log_file.fd = ngx_stderr;
0337         return &ngx_log;
0338     }
0339 
0340     p = NULL;
0341 
0342 #if (NGX_WIN32)
0343     if (name[1] != ':') {
0344 #else
0345     if (name[0] != '/') {
0346 #endif
0347 
0348         if (prefix) {
0349             plen = ngx_strlen(prefix);
0350 
0351         } else {
0352 #ifdef NGX_PREFIX
0353             prefix = (u_char *) NGX_PREFIX;
0354             plen = ngx_strlen(prefix);
0355 #else
0356             plen = 0;
0357 #endif
0358         }
0359 
0360         if (plen) {
0361             name = malloc(plen + nlen + 2);
0362             if (name == NULL) {
0363                 return NULL;
0364             }
0365 
0366             p = ngx_cpymem(name, prefix, plen);
0367 
0368             if (!ngx_path_separator(*(p - 1))) {
0369                 *p++ = '/';
0370             }
0371 
0372             ngx_cpystrn(p, (u_char *) NGX_ERROR_LOG_PATH, nlen + 1);
0373 
0374             p = name;
0375         }
0376     }
0377 
0378     ngx_log_file.fd = ngx_open_file(name, NGX_FILE_APPEND,
0379                                     NGX_FILE_CREATE_OR_OPEN,
0380                                     NGX_FILE_DEFAULT_ACCESS);
0381 
0382     if (ngx_log_file.fd == NGX_INVALID_FILE) {
0383         ngx_log_stderr(ngx_errno,
0384                        "[alert] could not open error log file: "
0385                        ngx_open_file_n " \"%s\" failed", name);
0386 #if (NGX_WIN32)
0387         ngx_event_log(ngx_errno,
0388                        "could not open error log file: "
0389                        ngx_open_file_n " \"%s\" failed", name);
0390 #endif
0391 
0392         ngx_log_file.fd = ngx_stderr;
0393     }
0394 
0395     if (p) {
0396         ngx_free(p);
0397     }
0398 
0399     return &ngx_log;
0400 }
0401 
0402 
0403 ngx_int_t
0404 ngx_log_open_default(ngx_cycle_t *cycle)
0405 {
0406     ngx_log_t         *log;
0407     static ngx_str_t   error_log = ngx_string(NGX_ERROR_LOG_PATH);
0408 
0409     if (ngx_log_get_file_log(&cycle->new_log) != NULL) {
0410         return NGX_OK;
0411     }
0412 
0413     if (cycle->new_log.log_level != 0) {
0414         /* there are some error logs, but no files */
0415 
0416         log = ngx_pcalloc(cycle->pool, sizeof(ngx_log_t));
0417         if (log == NULL) {
0418             return NGX_ERROR;
0419         }
0420 
0421     } else {
0422         /* no error logs at all */
0423         log = &cycle->new_log;
0424     }
0425 
0426     log->log_level = NGX_LOG_ERR;
0427 
0428     log->file = ngx_conf_open_file(cycle, &error_log);
0429     if (log->file == NULL) {
0430         return NGX_ERROR;
0431     }
0432 
0433     if (log != &cycle->new_log) {
0434         ngx_log_insert(&cycle->new_log, log);
0435     }
0436 
0437     return NGX_OK;
0438 }
0439 
0440 
0441 ngx_int_t
0442 ngx_log_redirect_stderr(ngx_cycle_t *cycle)
0443 {
0444     ngx_fd_t  fd;
0445 
0446     if (cycle->log_use_stderr) {
0447         return NGX_OK;
0448     }
0449 
0450     /* file log always exists when we are called */
0451     fd = ngx_log_get_file_log(cycle->log)->file->fd;
0452 
0453     if (fd != ngx_stderr) {
0454         if (ngx_set_stderr(fd) == NGX_FILE_ERROR) {
0455             ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
0456                           ngx_set_stderr_n " failed");
0457 
0458             return NGX_ERROR;
0459         }
0460     }
0461 
0462     return NGX_OK;
0463 }
0464 
0465 
0466 ngx_log_t *
0467 ngx_log_get_file_log(ngx_log_t *head)
0468 {
0469     ngx_log_t  *log;
0470 
0471     for (log = head; log; log = log->next) {
0472         if (log->file != NULL) {
0473             return log;
0474         }
0475     }
0476 
0477     return NULL;
0478 }
0479 
0480 
0481 static char *
0482 ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)
0483 {
0484     ngx_uint_t   i, n, d, found;
0485     ngx_str_t   *value;
0486 
0487     if (cf->args->nelts == 2) {
0488         log->log_level = NGX_LOG_ERR;
0489         return NGX_CONF_OK;
0490     }
0491 
0492     value = cf->args->elts;
0493 
0494     for (i = 2; i < cf->args->nelts; i++) {
0495         found = 0;
0496 
0497         for (n = 1; n <= NGX_LOG_DEBUG; n++) {
0498             if (ngx_strcmp(value[i].data, err_levels[n].data) == 0) {
0499 
0500                 if (log->log_level != 0) {
0501                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0502                                        "duplicate log level \"%V\"",
0503                                        &value[i]);
0504                     return NGX_CONF_ERROR;
0505                 }
0506 
0507                 log->log_level = n;
0508                 found = 1;
0509                 break;
0510             }
0511         }
0512 
0513         for (n = 0, d = NGX_LOG_DEBUG_FIRST; d <= NGX_LOG_DEBUG_LAST; d <<= 1) {
0514             if (ngx_strcmp(value[i].data, debug_levels[n++]) == 0) {
0515                 if (log->log_level & ~NGX_LOG_DEBUG_ALL) {
0516                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0517                                        "invalid log level \"%V\"",
0518                                        &value[i]);
0519                     return NGX_CONF_ERROR;
0520                 }
0521 
0522                 log->log_level |= d;
0523                 found = 1;
0524                 break;
0525             }
0526         }
0527 
0528 
0529         if (!found) {
0530             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0531                                "invalid log level \"%V\"", &value[i]);
0532             return NGX_CONF_ERROR;
0533         }
0534     }
0535 
0536     if (log->log_level == NGX_LOG_DEBUG) {
0537         log->log_level = NGX_LOG_DEBUG_ALL;
0538     }
0539 
0540     return NGX_CONF_OK;
0541 }
0542 
0543 
0544 static char *
0545 ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
0546 {
0547     ngx_log_t  *dummy;
0548 
0549     dummy = &cf->cycle->new_log;
0550 
0551     return ngx_log_set_log(cf, &dummy);
0552 }
0553 
0554 
0555 char *
0556 ngx_log_set_log(ngx_conf_t *cf, ngx_log_t **head)
0557 {
0558     ngx_log_t          *new_log;
0559     ngx_str_t          *value, name;
0560     ngx_syslog_peer_t  *peer;
0561 
0562     if (*head != NULL && (*head)->log_level == 0) {
0563         new_log = *head;
0564 
0565     } else {
0566 
0567         new_log = ngx_pcalloc(cf->pool, sizeof(ngx_log_t));
0568         if (new_log == NULL) {
0569             return NGX_CONF_ERROR;
0570         }
0571 
0572         if (*head == NULL) {
0573             *head = new_log;
0574         }
0575     }
0576 
0577     value = cf->args->elts;
0578 
0579     if (ngx_strcmp(value[1].data, "stderr") == 0) {
0580         ngx_str_null(&name);
0581         cf->cycle->log_use_stderr = 1;
0582 
0583         new_log->file = ngx_conf_open_file(cf->cycle, &name);
0584         if (new_log->file == NULL) {
0585             return NGX_CONF_ERROR;
0586         }
0587 
0588     } else if (ngx_strncmp(value[1].data, "memory:", 7) == 0) {
0589 
0590 #if (NGX_DEBUG)
0591         size_t                 size, needed;
0592         ngx_pool_cleanup_t    *cln;
0593         ngx_log_memory_buf_t  *buf;
0594 
0595         value[1].len -= 7;
0596         value[1].data += 7;
0597 
0598         needed = sizeof("MEMLOG  :" NGX_LINEFEED)
0599                  + cf->conf_file->file.name.len
0600                  + NGX_SIZE_T_LEN
0601                  + NGX_INT_T_LEN
0602                  + NGX_MAX_ERROR_STR;
0603 
0604         size = ngx_parse_size(&value[1]);
0605 
0606         if (size == (size_t) NGX_ERROR || size < needed) {
0607             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0608                                "invalid buffer size \"%V\"", &value[1]);
0609             return NGX_CONF_ERROR;
0610         }
0611 
0612         buf = ngx_pcalloc(cf->pool, sizeof(ngx_log_memory_buf_t));
0613         if (buf == NULL) {
0614             return NGX_CONF_ERROR;
0615         }
0616 
0617         buf->start = ngx_pnalloc(cf->pool, size);
0618         if (buf->start == NULL) {
0619             return NGX_CONF_ERROR;
0620         }
0621 
0622         buf->end = buf->start + size;
0623 
0624         buf->pos = ngx_slprintf(buf->start, buf->end, "MEMLOG %uz %V:%ui%N",
0625                                 size, &cf->conf_file->file.name,
0626                                 cf->conf_file->line);
0627 
0628         ngx_memset(buf->pos, ' ', buf->end - buf->pos);
0629 
0630         cln = ngx_pool_cleanup_add(cf->pool, 0);
0631         if (cln == NULL) {
0632             return NGX_CONF_ERROR;
0633         }
0634 
0635         cln->data = new_log;
0636         cln->handler = ngx_log_memory_cleanup;
0637 
0638         new_log->writer = ngx_log_memory_writer;
0639         new_log->wdata = buf;
0640 
0641 #else
0642         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
0643                            "nginx was built without debug support");
0644         return NGX_CONF_ERROR;
0645 #endif
0646 
0647     } else if (ngx_strncmp(value[1].data, "syslog:", 7) == 0) {
0648         peer = ngx_pcalloc(cf->pool, sizeof(ngx_syslog_peer_t));
0649         if (peer == NULL) {
0650             return NGX_CONF_ERROR;
0651         }
0652 
0653         if (ngx_syslog_process_conf(cf, peer) != NGX_CONF_OK) {
0654             return NGX_CONF_ERROR;
0655         }
0656 
0657         new_log->writer = ngx_syslog_writer;
0658         new_log->wdata = peer;
0659 
0660     } else {
0661         new_log->file = ngx_conf_open_file(cf->cycle, &value[1]);
0662         if (new_log->file == NULL) {
0663             return NGX_CONF_ERROR;
0664         }
0665     }
0666 
0667     if (ngx_log_set_levels(cf, new_log) != NGX_CONF_OK) {
0668         return NGX_CONF_ERROR;
0669     }
0670 
0671     if (*head != new_log) {
0672         ngx_log_insert(*head, new_log);
0673     }
0674 
0675     return NGX_CONF_OK;
0676 }
0677 
0678 
0679 static void
0680 ngx_log_insert(ngx_log_t *log, ngx_log_t *new_log)
0681 {
0682     ngx_log_t  tmp;
0683 
0684     if (new_log->log_level > log->log_level) {
0685 
0686         /*
0687          * list head address is permanent, insert new log after
0688          * head and swap its contents with head
0689          */
0690 
0691         tmp = *log;
0692         *log = *new_log;
0693         *new_log = tmp;
0694 
0695         log->next = new_log;
0696         return;
0697     }
0698 
0699     while (log->next) {
0700         if (new_log->log_level > log->next->log_level) {
0701             new_log->next = log->next;
0702             log->next = new_log;
0703             return;
0704         }
0705 
0706         log = log->next;
0707     }
0708 
0709     log->next = new_log;
0710 }
0711 
0712 
0713 #if (NGX_DEBUG)
0714 
0715 static void
0716 ngx_log_memory_writer(ngx_log_t *log, ngx_uint_t level, u_char *buf,
0717     size_t len)
0718 {
0719     u_char                *p;
0720     size_t                 avail, written;
0721     ngx_log_memory_buf_t  *mem;
0722 
0723     mem = log->wdata;
0724 
0725     if (mem == NULL) {
0726         return;
0727     }
0728 
0729     written = ngx_atomic_fetch_add(&mem->written, len);
0730 
0731     p = mem->pos + written % (mem->end - mem->pos);
0732 
0733     avail = mem->end - p;
0734 
0735     if (avail >= len) {
0736         ngx_memcpy(p, buf, len);
0737 
0738     } else {
0739         ngx_memcpy(p, buf, avail);
0740         ngx_memcpy(mem->pos, buf + avail, len - avail);
0741     }
0742 }
0743 
0744 
0745 static void
0746 ngx_log_memory_cleanup(void *data)
0747 {
0748     ngx_log_t *log = data;
0749 
0750     ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "destroy memory log buffer");
0751 
0752     log->wdata = NULL;
0753 }
0754 
0755 #endif