2015-05-09 wcdj
nginx在的main入口函数中(src/core/nginx.c),首先会调用ngx_strerror_init函数完成对错误码和错误信息的初始化,为什么这么做呢?继续看下内部实现。
此函数定义在src/os/unix/ngx_errno.c中:
static ngx_str_t *ngx_sys_errlist;
static ngx_str_t ngx_unknown_error = ngx_string("Unknown error");
ngx_uint_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().
*/
len = NGX_SYS_NERR * sizeof(ngx_str_t);
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);
p = malloc(len);
if (p =