震驚--你所不知道的nginx初始化-錯誤日誌部分

微信公衆號:Nginx源碼分析
關注可瞭解更多的Nginx知識。任何問題或建議,請公衆號留言;
關注公衆號,有趣有內涵的文章第一時間送達!node

本節內容

從本節開始,咱們開始真正的分析nginx源碼。我並無一個準確的前後順序,想到哪就寫到哪裏。
固然,有不少地方我也不懂代碼的含義,這些地方我會代表,而後先略過去。linux

啓動

main函數在src/core/nginx.c文件中。
咱們首先看一下nginx啓動的部分,啓動部分有不少函數,咱們逐個的分析一下每一個函數的做用。nginx

ngx_debug_init

這個函數是設置debug相關功能的,在linux環境下,只是一個空的宏定義,能夠略過。web

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腳本中設置的,以下:bash

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數組元素的值。app

 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數組的內容,函數很是簡單dom

 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 : 保存錯誤信息的內存空間長度socket

總結

本文就先簡單的分析一下啓動過程當中的錯誤信息的處理方法。若是喜歡本文,能夠關注公衆號Nginx源碼分析.



喜歡本文的朋友們,歡迎長按下圖關注訂閱號 Nginx源碼分析,更多精彩內容第一時間送達
Nginx源碼分析
Nginx源碼分析
相關文章
相關標籤/搜索