Nginx源码完全注释(8)ngx_errno.c
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的更多相关文章
- Nginx 源码完全注释(11)ngx_spinlock
		Nginx 是多进程模式的,一个 master 与多个 workers,一般工作在多核 CPU 上,所以自旋锁就是必须用到的.Nginx 中的自旋锁的定义,位于 ngx_spinlock.c 中,如下 ... 
- Nginx源码完全注释(6)core/murmurhash
		下面是摘自 Google Code 的 Murmurhash 开源项目主页上的 Murmurhash2,Nginx 就是采用的这个. uint32_t MurmurHash2 ( const void ... 
- nginx源码完全注释(1)ngx_alloc.h / ngx_alloc.c
		首先看 ngx_alloc.h 文件,主要声明或宏定义了 ngx_alloc,ngx_calloc,ngx_memalign,ngx_free. /* * Copyright (C) Igor Sys ... 
- Nginx 源码完全注释(10)ngx_radix_tree
		ngx_radix_tree.h // 未被使用的节点 #define NGX_RADIX_NO_VALUE (uintptr_t) -1 typedef struct ngx_radix_node_ ... 
- Nginx源码完全注释(9)nginx.c: ngx_get_options
		本文分析 ngxin.c 中的 ngx_get_options 函数,其影响: nginx.c 中的: static ngx_uint_t ngx_show_help; static ngx_uint ... 
- 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 ... 
- Nginx源码完全注释(5)core/ngx_cpuinfo.c
		/* * Copyright (C) Igor Sysoev * Copyright (C) Nginx, Inc. */ #include <ngx_config.h> #include ... 
- Nginx源码完全注释(2)ngx_array.h / ngx_array.c
		数组头文件 ngx_array.h #include <ngx_config.h> #include <ngx_core.h> struct ngx_array_s { voi ... 
- Nginx源码完全注释(4)ngx_queue.h / ngx_queue.c
		队列头文件ngx_queue.h #include <ngx_config.h> #include <ngx_core.h> #ifndef _NGX_QUEUE_H_INCL ... 
随机推荐
- windows 2016 容器管理
			1. docker-compose 安装 python 2.7 pip pip install docker-compose 常见问题: ... 
- 读懂IL代码就这么简单  ---- IL系列文章
			读懂IL代码就这么简单 (一) 读懂IL代码就这么简单(二) 读懂IL代码就这么简单(三)完结篇 出处:http://www.cnblogs.com/zery/tag/IL%20%E7%B3%BB%E ... 
- Linux 权限使用 777 真的好吗?
			Linux 权限使用 777 真的好吗? 开发环境当然不是问题,但是会造成一个习惯,到生产时也容易经常配置成 777. 777 权限可以让你的项目出现很大安全问题.1 linux 775和777权限有 ... 
- numpy中文件的存储和读取-嵩天老师笔记
			numpy中csv文件的存储和读取 CSV文件:(Comma‐Separated Value, 逗号分隔值) 一维和二维数组 存储 np.savetxt(frame,array,fmt='%.18e' ... 
- shell常用测试命令
			预定义变量: 预定义变量是由Bash程序预先定义好的一类特殊变量,用户只能使用预定义变量,而不能创建新的预定义变量,也不能直接为预定义变量赋值.预定义比变量使用"$"符合和另一个符 ... 
- cpp分解质因数
			原理有点像埃氏筛. #include <stdio.h> #include <iostream> #include <stdlib.h> using namespa ... 
- UVALive7261(2015ACM/ICPC北京赛区现场赛A)
			题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ... 
- 杂项-数学软件:MATLAB
			ylbtech-杂项-数学软件:MATLAB MATLAB是美国MathWorks公司出品的商业数学软件,用于算法开发.数据可视化.数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATL ... 
- ALSA声卡笔记2---ASoC驱动框架
			1.简单了解一下ASOC 在嵌入式系统里面的声卡驱动为ASOC(ALSA System on Chip) ,它是在ALSA 驱动程序上封装的一层 分为3大部分,Machine,Platform和C ... 
- 浅谈PHP面向对象编程(二、基础知识)
			和一些面向对象的语言有所不同,PHP并不是一种纯面向对象的语言,包PIP它支持面向对象的程序设计,并可以用于开发大型的商业程序.因此学好面向对象输程对PHP程序员来说也是至关重要的.本章并针对面向对象 ... 
