errno.h中的strerror(int errno)可以确定指定的errno的错误的提示信息。在 Nginx 中,将所有错误提示信息预先存储在一个数组里,而预先确定这个数组的大小,是在自动化脚本中完成的,如下是auto/unix脚本:(其中自动化脚本auto/feature的作用参考《解剖 Nginx·自动脚本篇(4)工具型脚本系列》一文)


// auto/unix ngx_feature="sys_nerr"
ngx_feature_name="NGX_SYS_NERR"
ngx_feature_run=value
ngx_feature_incs='#include
#include '
ngx_feature_path=
ngx_feature_libs=
ngx_feature_test='printf("%d", sys_nerr);'
. auto/feature

但是对于某些平台是没有sys_nerr的,此时引用auto/feature后得到的ngx_found变量的值为no,否则为yes。当为no时,则执行下面这段:


// auto/unix
if [ $ngx_found = no ]; then
ngx_feature="_sys_nerr"
ngx_feature_name="NGX_SYS_NERR"
ngx_feature_run=value
ngx_feature_incs='#include
#include '
ngx_feature_path=
ngx_feature_libs=
ngx_feature_test='printf("%d", _sys_nerr);'
. auto/feature
fi

上面这段会在 Cygwin 环境下起作用,因为 Cygwin 定义了_sys_nerr。如果此后ngx_found还是no,则运行:


if [ $ngx_found = no ]; then
ngx_feature='maximum errno'
ngx_feature_name=NGX_SYS_NERR
ngx_feature_run=value
ngx_feature_incs='#include
#include
#include '
ngx_feature_path=
ngx_feature_libs=
ngx_feature_test='int n;
char *p;
for (n = 1; n < 1000; n++) {
errno = 0;
p = strerror(n);
if (errno == EINVAL
|| p == NULL
|| strncmp(p, "Unknown error", 13) == 0)
{
break;
}
}
printf("%d", n);'
. auto/feature
fi

上面这段会在 Solaris 上生效,因为 Solaris 没有sys_nerr_sys_nerr

这样就会在objs/ngx_auto_config.h文件中加入如下一句;


// Mac OS X 10.8
#ifndef NGX_SYS_NERR
#define NGX_SYS_NERR 107
#endif

// Linux ubuntu 3.2.0-30-generic-pae
#ifndef NGX_SYS_NERR
#define NGX_SYS_NERR 135
#endif

Igor 把所有 strerror 消息都拷贝到一个数组里,他是这样解释原因的:

  • 1) strerror() and strerror_r() functions are not Async-Signal-Safe, therefore, they cannot be used in signal handlers;
  • 2) a direct sys_errlist[] array may be used instead of these functions, but Linux linker warns about its usage:

      warning: `sys_errlist' is deprecated; use `strerror' or `strerror_r' instead
    warning: `sys_nerr' is deprecated; use `strerror' or `strerror_r' instead
    causing false bug reports.

如下是src/os/unix/ngx_errno.c


static ngx_str_t *ngx_sys_errlist;
static ngx_str_t ngx_unknown_error = ngx_string("Unknown error"); // 替代 strerror,传入 err 错误码、已分配内存的 errstr 指针以及其内存大小
// 调用该函数后,errstr 和返回值均为给定错误码相应的错误消息
u_char *
ngx_strerror(ngx_err_t err, u_char *errstr, size_t size)
{
ngx_str_t *msg; msg = ((ngx_uint_t) err < NGX_SYS_NERR) ? &ngx_sys_errlist[err]:
&ngx_unknown_error;
size = ngx_min(size, msg->len); return ngx_cpymem(errstr, msg->data, size);
} ngx_int_t
ngx_strerror_init(void)
{
char *msg;
u_char *p;
size_t len;
ngx_err_t err; /*
* ngx_strerror() is not ready to work at this stage, therefore,
* malloc() is used and possible errors are logged using strerror().
*/ // 所有 strerror 消息的数量所需要的 ngx_str_t 的内存字节数
//(注意不是消息本身,因为小内容是存在 ngx_str_t 的 data 里的)
len = NGX_SYS_NERR * sizeof(ngx_str_t); // 分配内存,注意此时是直接使用 malloc 的
// TODO
ngx_sys_errlist = malloc(len);
if (ngx_sys_errlist == NULL) {
goto failed;
} // 初始化错误消息的每一项
for (err = 0; err < NGX_SYS_NERR; err++) { // 获取该消息
msg = strerror(err);
// 该条消息的大小
len = ngx_strlen(msg); // 分配 len 大小的内存
p = malloc(len);
if (p == NULL) {
goto failed;
} ngx_memcpy(p, msg, len);
ngx_sys_errlist[err].len = len;
ngx_sys_errlist[err].data = p;
} return NGX_OK; failed: err = errno;
ngx_log_stderr(0, "malloc(%uz) failed (%d: %s)", len, err, strerror(err)); return NGX_ERROR;
}

Nginx源码完全注释(8)ngx_errno.c的更多相关文章

  1. Nginx 源码完全注释(11)ngx_spinlock

    Nginx 是多进程模式的,一个 master 与多个 workers,一般工作在多核 CPU 上,所以自旋锁就是必须用到的.Nginx 中的自旋锁的定义,位于 ngx_spinlock.c 中,如下 ...

  2. Nginx源码完全注释(6)core/murmurhash

    下面是摘自 Google Code 的 Murmurhash 开源项目主页上的 Murmurhash2,Nginx 就是采用的这个. uint32_t MurmurHash2 ( const void ...

  3. nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c

    首先看 ngx_alloc.h 文件,主要声明或宏定义了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free. /* * Copyright (C) Igor Sys ...

  4. Nginx 源码完全注释(10)ngx_radix_tree

    ngx_radix_tree.h // 未被使用的节点 #define NGX_RADIX_NO_VALUE (uintptr_t) -1 typedef struct ngx_radix_node_ ...

  5. Nginx源码完全注释(9)nginx.c: ngx_get_options

    本文分析 ngxin.c 中的 ngx_get_options 函数,其影响: nginx.c 中的: static ngx_uint_t ngx_show_help; static ngx_uint ...

  6. Nginx源码完全注释(7)ngx_palloc.h/ngx_palloc.c

    ngx_palloc.h /* * NGX_MAX_ALLOC_FROM_POOL should be (ngx_pagesize - 1), i.e. 4095 on x86. * On Windo ...

  7. Nginx源码完全注释(5)core/ngx_cpuinfo.c

    /* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #include <ngx_config.h> #include ...

  8. Nginx源码完全注释(2)ngx_array.h / ngx_array.c

    数组头文件 ngx_array.h #include <ngx_config.h> #include <ngx_core.h> struct ngx_array_s { voi ...

  9. Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c

    队列头文件ngx_queue.h #include <ngx_config.h> #include <ngx_core.h> #ifndef _NGX_QUEUE_H_INCL ...

随机推荐

  1. lapis 数据库配置

    备注:  目前支持 postgresql .mysql (实际使用大家可以尝试用下tidb.CockroachDB)   1. pg数据库配置 // config.lua local config = ...

  2. mysql+matlab配置

    mysql 中一直出现'> 单双引号没有配对 mysql 连接matlab 1, 到mysql官网下载 http://dev.mysql.com/downloads/connector/j/(m ...

  3. jeecg选择按钮带入其他单据值

    前端的标签 <input class="inputxt" id="fshimian" name="fshimian" ignore=& ...

  4. php的闭包

    闭包是指在创建时封装周围状态的函数,即使闭包所在的环境的不存在了,闭包中封装的状态依然存在. 匿名函数其实就是没有名称的函数,匿名函数可以赋值给变量,还能像其他任何PHP函数对象那样传递.不过匿名函数 ...

  5. 初学FPGA一些建议

    数字电路: 这是大学里的基本课程 ,涵盖了一般数字电路的组合电路.时序电路.寄存器传输.储存器以及可编程逻辑电路(FPGA 就是其中一种),还有比较好的添加了计算机的指令集结构.处理器设计等计算机方面 ...

  6. 【转】使用 JMeter 完成常用的压力测试

    本文介绍了 JMeter 相关的基本概念.并以 JMeter 为例,介绍了使用它来完成最常用的三种类型服务器,即 Web 服务器.数据库服务器和消息中间件,压力测试的方法.步骤以及注意事项.      ...

  7. 北京师范大学第十六届程序设计竞赛决赛 F 汤圆防漏理论

    链接:https://www.nowcoder.com/acm/contest/117/F来源:牛客网 汤圆防漏理论 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他 ...

  8. C++:const_cast类型转换

    针对const_cast,太多人在用同一个示例问同一个问题:void main(){   const int a = 3;   const int *pc = &a;   int *p = c ...

  9. eclipse安装使用注意点

    1.eclipse tomcat集成找不到server http://blog.csdn.net/wugangsunny/article/details/25246565 2.Eclipse java ...

  10. 仅用CSS3创建h5预加载旋转圈

    <head> <meta charset="UTF-8"> <title></title> <style type=" ...