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 /*
0013  * The strerror() messages are copied because:
0014  *
0015  * 1) strerror() and strerror_r() functions are not Async-Signal-Safe,
0016  *    therefore, they cannot be used in signal handlers;
0017  *
0018  * 2) a direct sys_errlist[] array may be used instead of these functions,
0019  *    but Linux linker warns about its usage:
0020  *
0021  * warning: `sys_errlist' is deprecated; use `strerror' or `strerror_r' instead
0022  * warning: `sys_nerr' is deprecated; use `strerror' or `strerror_r' instead
0023  *
0024  *    causing false bug reports.
0025  */
0026 
0027 
0028 static ngx_str_t  *ngx_sys_errlist;
0029 static ngx_str_t   ngx_unknown_error = ngx_string("Unknown error");
0030 
0031 
0032 u_char *
0033 ngx_strerror(ngx_err_t err, u_char *errstr, size_t size)
0034 {
0035     ngx_str_t  *msg;
0036 
0037     msg = ((ngx_uint_t) err < NGX_SYS_NERR) ? &ngx_sys_errlist[err]:
0038                                               &ngx_unknown_error;
0039     size = ngx_min(size, msg->len);
0040 
0041     return ngx_cpymem(errstr, msg->data, size);
0042 }
0043 
0044 
0045 ngx_int_t
0046 ngx_strerror_init(void)
0047 {
0048     char       *msg;
0049     u_char     *p;
0050     size_t      len;
0051     ngx_err_t   err;
0052 
0053     /*
0054      * ngx_strerror() is not ready to work at this stage, therefore,
0055      * malloc() is used and possible errors are logged using strerror().
0056      */
0057 
0058     len = NGX_SYS_NERR * sizeof(ngx_str_t);
0059 
0060     ngx_sys_errlist = malloc(len);
0061     if (ngx_sys_errlist == NULL) {
0062         goto failed;
0063     }
0064 
0065     for (err = 0; err < NGX_SYS_NERR; err++) {
0066         msg = strerror(err);
0067         len = ngx_strlen(msg);
0068 
0069         p = malloc(len);
0070         if (p == NULL) {
0071             goto failed;
0072         }
0073 
0074         ngx_memcpy(p, msg, len);
0075         ngx_sys_errlist[err].len = len;
0076         ngx_sys_errlist[err].data = p;
0077     }
0078 
0079     return NGX_OK;
0080 
0081 failed:
0082 
0083     err = errno;
0084     ngx_log_stderr(0, "malloc(%uz) failed (%d: %s)", len, err, strerror(err));
0085 
0086     return NGX_ERROR;
0087 }