src/os/unix/ngx_errno.c - nginx-1.7.10

Global variables defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>


  7. /*
  8. * The strerror() messages are copied because:
  9. *
  10. * 1) strerror() and strerror_r() functions are not Async-Signal-Safe,
  11. *    therefore, they cannot be used in signal handlers;
  12. *
  13. * 2) a direct sys_errlist[] array may be used instead of these functions,
  14. *    but Linux linker warns about its usage:
  15. *
  16. * warning: `sys_errlist' is deprecated; use `strerror' or `strerror_r' instead
  17. * warning: `sys_nerr' is deprecated; use `strerror' or `strerror_r' instead
  18. *
  19. *    causing false bug reports.
  20. */


  21. static ngx_str_t  *ngx_sys_errlist;
  22. static ngx_str_t   ngx_unknown_error = ngx_string("Unknown error");


  23. u_char *
  24. ngx_strerror(ngx_err_t err, u_char *errstr, size_t size)
  25. {
  26.     ngx_str_t  *msg;

  27.     msg = ((ngx_uint_t) err < NGX_SYS_NERR) ? &ngx_sys_errlist[err]:
  28.                                               &ngx_unknown_error;
  29.     size = ngx_min(size, msg->len);

  30.     return ngx_cpymem(errstr, msg->data, size);
  31. }


  32. ngx_int_t
  33. ngx_strerror_init(void)
  34. {
  35.     char       *msg;
  36.     u_char     *p;
  37.     size_t      len;
  38.     ngx_err_t   err;

  39.     /*
  40.      * ngx_strerror() is not ready to work at this stage, therefore,
  41.      * malloc() is used and possible errors are logged using strerror().
  42.      */

  43.     len = NGX_SYS_NERR * sizeof(ngx_str_t);

  44.     ngx_sys_errlist = malloc(len);
  45.     if (ngx_sys_errlist == NULL) {
  46.         goto failed;
  47.     }

  48.     for (err = 0; err < NGX_SYS_NERR; err++) {
  49.         msg = strerror(err);
  50.         len = ngx_strlen(msg);

  51.         p = malloc(len);
  52.         if (p == NULL) {
  53.             goto failed;
  54.         }

  55.         ngx_memcpy(p, msg, len);
  56.         ngx_sys_errlist[err].len = len;
  57.         ngx_sys_errlist[err].data = p;
  58.     }

  59.     return NGX_OK;

  60. failed:

  61.     err = errno;
  62.     ngx_log_stderr(0, "malloc(%uz) failed (%d: %s)", len, err, strerror(err));

  63.     return NGX_ERROR;
  64. }