震惊--你所不知道的nginx初始化-错误日志部分

2,168 阅读13分钟

微信公众号:Nginx源码分析
关注可了解更多的Nginx知识。任何问题或建议,请公众号留言;
关注公众号,有趣有内涵的文章第一时间送达!

本节内容

从本节开始,我们开始真正的分析nginx源码。我并没有一个准确的先后顺序,想到哪就写到哪里。
当然,有很多地方我也不懂代码的含义,这些地方我会表明,然后先略过去。

启动

main函数在src/core/nginx.c文件中。
我们首先看一下nginx启动的部分,启动部分有很多函数,我们逐个的分析一下每个函数的作用。

ngx_debug_init

这个函数是设置debug相关功能的,在linux环境下,只是一个空的宏定义,可以略过。

ngx_strerror_init

这部分代码是在src/os/unix/ngx_errno.c文件中的,主要用于错误信息相关的输出。
这个文件包含了两个函数ngx_strerror_initngx_strerror,我们先看一下ngx_strerror_init函数,顾名思义,这是一个初始化函数。

代码

 1ngx_int_t
2ngx_strerror_init(void)
3{
4    char       *msg;
5    u_char     *p;
6    size_t      len;
7    ngx_err_t   err;
8    // int i 是测试代码
9    int i ;
10
11    /*
12     * ngx_strerror() is not ready to work at this stage, therefore,
13     * malloc() is used and possible errors are logged using strerror().
14     */

15
16    len = NGX_SYS_NERR * sizeof(ngx_str_t);
17
18    ngx_sys_errlist = malloc(len);
19    if (ngx_sys_errlist == NULL) {
20        goto failed;
21    }
22
23    for (err = 0; err < NGX_SYS_NERR; err++) {
24        msg = strerror(err);
25        len = ngx_strlen(msg);
26
27        p = malloc(len);
28        if (p == NULL) {
29            goto failed;
30        }
31
32        ngx_memcpy(p, msg, len);
33        ngx_sys_errlist[err].len = len;
34        ngx_sys_errlist[err].data = p;
35    }
36// 下面的for循环是我们添加的测试代码    
37    for(i = 0; i < NGX_SYS_NERR; i++){
38       printf("errno is %d, msg is %s\n", i, ngx_sys_errlist[i].data);
39    }
40
41    return NGX_OK;
42
43failed:
44
45    err = errno;
46    ngx_log_stderr(0"malloc(%uz) failed (%d: %s)", len, err, strerror(err));
47
48    return NGX_ERROR;
49}

这里面用到了一个变量NGX_SYS_NERR,这是一个宏定义,是在auto/unix脚本中设置的,如下:

1ngx_feature="sys_nerr"
2ngx_feature_name="NGX_SYS_NERR"
3ngx_feature_run=value
4ngx_feature_incs='#include <errno.h>
5                  #include <stdio.h>'

6ngx_feature_path=
7ngx_feature_libs=
8ngx_feature_test='printf("%d", sys_nerr);'
9. auto/feature

这段脚本通过获取sys_nerr参数的值,然后赋值给NGX_SYS_NERR,这个参数表示的是当前操作系统提供的最大的错误码数值。

我们在ngx_strerror_init中添加了一段测试代码,编译之后,启动nginx可以看到ngx_sys_errlist数组元素的值。

  1errno is 0, msg is Success
2errno is 1, msg is Operation not permitted
3errno is 2, msg is No such file or directory
4errno is 3, msg is No such process
5errno is 4, msg is Interrupted system call
6errno is 5, msg is Input/output error
7errno is 6, msg is No such device or address
8errno is 7, msg is Argument list too long
9errno is 8, msg is Exec format error
10errno is 9, msg is Bad file descriptor
11errno is 10, msg is No child processes
12errno is 11, msg is Resource temporarily unavailable
13errno is 12, msg is Cannot allocate memory
14errno is 13, msg is Permission denied
15errno is 14, msg is Bad address
16errno is 15, msg is Block device required
17errno is 16, msg is Device or resource busy
18errno is 17, msg is File exists
19errno is 18, msg is Invalid cross-device link
20errno is 19, msg is No such device
21errno is 20, msg is Not a directory
22errno is 21, msg is Is a directory
23errno is 22, msg is Invalid argument
24errno is 23, msg is Too many open files in system
25errno is 24, msg is Too many open files
26errno is 25, msg is Inappropriate ioctl for device
27errno is 26, msg is Text file busy
28errno is 27, msg is File too large
29errno is 28, msg is No space left on device
30errno is 29, msg is Illegal seek
31errno is 30, msg is Read-only file system
32errno is 31, msg is Too many links
33errno is 32, msg is Broken pipe
34errno is 33, msg is Numerical argument out of domain
35errno is 34, msg is Numerical result out of range
36errno is 35, msg is Resource deadlock avoided
37errno is 36, msg is File name too long
38errno is 37, msg is No locks available
39errno is 38, msg is Function not implemented!
40errno is 39, msg is Directory not empty
41errno is 40, msg is Too many levels of symbolic links
42errno is 41, msg is Unknown error 41
43errno is 42, msg is No message of desired type
44errno is 43, msg is Identifier removed
45errno is 44, msg is Channel number out of range
46errno is 45, msg is Level 2 not synchronized!
47errno is 46, msg is Level 3 halted
48errno is 47, msg is Level 3 reset
49errno is 48, msg is Link number out of range1
50errno is 49, msg is Protocol driver not attached
51errno is 50, msg is No CSI structure available
52errno is 51, msg is Level 2 halted
53errno is 52, msg is Invalid exchange
54errno is 53, msg is Invalid request descriptor
55errno is 54, msg is Exchange full
56errno is 55, msg is No anode
57errno is 56, msg is Invalid request code
58errno is 57, msg is Invalid slot
59errno is 58, msg is Unknown error 58
60errno is 59, msg is Bad font file format
61errno is 60, msg is Device not a stream
62errno is 61, msg is No data available
63errno is 62, msg is Timer expired
64errno is 63, msg is Out of streams resources1
65errno is 64, msg is Machine is not on the network
66errno is 65, msg is Package not installed
67errno is 66, msg is Object is remote
68errno is 67, msg is Link has been severed
69errno is 68, msg is Advertise error
70errno is 69, msg is Srmount error
71errno is 70, msg is Communication error on send
72errno is 71, msg is Protocol error
73errno is 72, msg is Multihop attempted
74errno is 73, msg is RFS specific error
75errno is 74, msg is Bad message
76errno is 75, msg is Value too large for defined data type
77errno is 76, msg is Name not unique on network
78errno is 77, msg is File descriptor in bad state
79errno is 78, msg is Remote address changed
80errno is 79, msg is Can not access a needed shared library
81errno is 80, msg is Accessing a corrupted shared library
82errno is 81, msg is .lib section in a.out corrupted
83errno is 82, msg is Attempting to link in too many shared libraries
84errno is 83, msg is Cannot exec a shared library directly
85errno is 84, msg is Invalid or incomplete multibyte or wide character
86errno is 85, msg is Interrupted system call should be restarted
87errno is 86, msg is Streams pipe error
88errno is 87, msg is Too many users
89errno is 88, msg is Socket operation on non-socket
90errno is 89, msg is Destination address required
91errno is 90, msg is Message too long
92errno is 91, msg is Protocol wrong type for socket
93errno is 92, msg is Protocol not available
94errno is 93, msg is Protocol not supported
95errno is 94, msg is Socket type not supported
96errno is 95, msg is Operation not supported
97errno is 96, msg is Protocol family not supported
98errno is 97, msg is Address family not supported by protocol!
99errno is 98, msg is Address already in use
100errno is 99, msg is Cannot assign requested address
101errno is 100, msg is Network is down
102errno is 101, msg is Network is unreachable
103errno is 102, msg is Network dropped connection on reset
104errno is 103, msg is Software caused connection abort
105errno is 104, msg is Connection reset by peer1
106errno is 105, msg is No buffer space available
107errno is 106, msg is Transport endpoint is already connected
108errno is 107, msg is Transport endpoint is not connected
109errno is 108, msg is Cannot send after transport endpoint shutdown
110errno is 109, msg is Too many references: cannot splice
111errno is 110, msg is Connection timed out
112errno is 111, msg is Connection refused
113errno is 112, msg is Host is down
114errno is 113, msg is No route to host
115errno is 114, msg is Operation already in progress
116errno is 115, msg is Operation now in progress
117errno is 116, msg is Stale file handle
118errno is 117, msg is Structure needs cleaning1
119errno is 118, msg is Not a XENIX named type file
120errno is 119, msg is No XENIX semaphores available
121errno is 120, msg is Is a named type file
122errno is 121, msg is Remote I/O error
123errno is 122, msg is Disk quota exceeded
124errno is 123, msg is No medium found
125errno is 124, msg is Wrong medium type
126errno is 125, msg is Operation canceled
127errno is 126, msg is Required key not available
128errno is 127, msg is Key has expired
129errno is 128, msg is Key has been revoked
130errno is 129, msg is Key was rejected by service
131errno is 130, msg is Owner died
132errno is 131, msg is State not recoverable
133errno is 132, msg is Operation not possible due to RF-kill
134errno is 133, msg is Memory page has hardware error
135errno is 134, msg is Unknown error 134

ngx_strerror

这个函数是获取上面的ngx_sys_errlist数组的内容,函数非常简单

 1u_char *
2ngx_strerror(ngx_err_t err, u_char *errstr, size_t size)
3
{
4    ngx_str_t  *msg;
5
6    msg = ((ngx_uint_t) err < NGX_SYS_NERR) ? &ngx_sys_errlist[err]:
7                                              &ngx_unknown_error;
8    size = ngx_min(size, msg->len);
9
10    return ngx_cpymem(errstr, msg->data, size);
11}

三个参数的含义如下:
err : 错误的数值no
errstr : 保存错误信息的字符串指针
size : 保存错误信息的内存空间长度

总结

本文就先简单的分析一下启动过程中的错误信息的处理方法。如果喜欢本文,可以关注公众号Nginx源码分析.



喜欢本文的朋友们,欢迎长按下图关注订阅号Nginx源码分析,更多精彩内容第一时间送达
Nginx源码分析
Nginx源码分析