comparison src/os/unix/ngx_errno.c @ 604:428c6e58046a NGINX_0_9_0

nginx 0.9.0 *) Feature: the "keepalive_disable" directive. *) Feature: the "map" directive supports variables as value of a defined variable. *) Feature: the "map" directive supports empty strings as value of the first parameter. *) Feature: the "map" directive supports expressions as the first parameter. *) Feature: nginx(8) manual page. Thanks to Sergey Osokin. *) Feature: Linux accept4() support. Thanks to Simon Liu. *) Workaround: elimination of Linux linker warning about "sys_errlist" and "sys_nerr"; the warning had appeared in 0.8.35. *) Bugfix: a segmentation fault might occur in a worker process, if the "auth_basic" directive was used. Thanks to Michail Laletin. *) Bugfix: compatibility with ngx_http_eval_module; the bug had appeared in 0.8.42.
author Igor Sysoev <http://sysoev.ru>
date Mon, 29 Nov 2010 00:00:00 +0300
parents 88d3e895bdf9
children eb208e0cf44d
comparison
equal deleted inserted replaced
603:94ea26a3b3aa 604:428c6e58046a
6 6
7 #include <ngx_config.h> 7 #include <ngx_config.h>
8 #include <ngx_core.h> 8 #include <ngx_core.h>
9 9
10 10
11 #if (NGX_HAVE_STRERROR_R) 11 /*
12 * The strerror() messages are copied because:
13 *
14 * 1) strerror() and strerror_r() functions are not Async-Signal-Safe,
15 * therefore, they can not be used in signal handlers;
16 *
17 * 2) a direct sys_errlist[] array may be used instead of these functions,
18 * but Linux linker warns about its usage:
19 *
20 * warning: `sys_errlist' is deprecated; use `strerror' or `strerror_r' instead
21 * warning: `sys_nerr' is deprecated; use `strerror' or `strerror_r' instead
22 *
23 * causing false bug reports.
24 */
25
26
27 static ngx_str_t *ngx_sys_errlist;
28 static ngx_str_t ngx_unknown_error = ngx_string("Unknown error");
29
12 30
13 u_char * 31 u_char *
14 ngx_strerror_r(int err, u_char *errstr, size_t size) 32 ngx_strerror(ngx_err_t err, u_char *errstr, size_t size)
15 { 33 {
16 if (size == 0) { 34 ngx_str_t *msg;
17 return errstr; 35
36 msg = ((ngx_uint_t) err < NGX_SYS_NERR) ? &ngx_sys_errlist[err]:
37 &ngx_unknown_error;
38 size = ngx_min(size, msg->len);
39
40 return ngx_cpymem(errstr, msg->data, size);
41 }
42
43
44 ngx_uint_t
45 ngx_strerror_init(void)
46 {
47 char *msg;
48 u_char *p;
49 size_t len;
50 ngx_err_t err;
51
52 /*
53 * ngx_strerror() is not ready to work at this stage, therefore,
54 * malloc() is used and possible errors are logged using strerror().
55 */
56
57 len = NGX_SYS_NERR * sizeof(ngx_str_t);
58
59 ngx_sys_errlist = malloc(len);
60 if (ngx_sys_errlist == NULL) {
61 goto failed;
18 } 62 }
19 63
20 errstr[0] = '\0'; 64 for (err = 0; err < NGX_SYS_NERR; err++) {
65 msg = strerror(err);
66 len = ngx_strlen(msg);
21 67
22 strerror_r(err, (char *) errstr, size); 68 p = malloc(len);
69 if (p == NULL) {
70 goto failed;
71 }
23 72
24 while (*errstr && size) { 73 ngx_memcpy(p, msg, len);
25 errstr++; 74 ngx_sys_errlist[err].len = len;
26 size--; 75 ngx_sys_errlist[err].data = p;
27 } 76 }
28 77
29 return errstr; 78 return NGX_OK;
79
80 failed:
81
82 err = errno;
83 ngx_log_stderr(0, "malloc(%uz) failed (%d: %s)", len, err, strerror(err));
84
85 return NGX_ERROR;
30 } 86 }
31
32 #elif (NGX_HAVE_GNU_STRERROR_R)
33
34 /* Linux strerror_r() */
35
36 u_char *
37 ngx_strerror_r(int err, u_char *errstr, size_t size)
38 {
39 char *str;
40
41 if (size == 0) {
42 return errstr;
43 }
44
45 errstr[0] = '\0';
46
47 str = strerror_r(err, (char *) errstr, size);
48
49 if (str != (char *) errstr) {
50 return ngx_cpystrn(errstr, (u_char *) str, size);
51 }
52
53 while (*errstr && size) {
54 errstr++;
55 size--;
56 }
57
58 return errstr;
59 }
60
61 #endif