Klaus Demo nginx / 7f31a20
use copied strerror() messages and autoconfigured sys_nerr value Igor Sysoev 11 years ago
9 changed file(s) with 113 addition(s) and 105 deletion(s). Raw diff Collapse all Expand all
6464 fi
6565 ;;
6666
67 value)
68 # /bin/sh is used to intercept "Killed" or "Abort trap" messages
69 if /bin/sh -c $NGX_AUTOTEST >/dev/null 2>&1; then
70 echo " found"
71 ngx_found=yes
72
73 cat << END >> $NGX_AUTO_CONFIG_H
74
75 #ifndef $ngx_feature_name
76 #define $ngx_feature_name `$NGX_AUTOTEST`
77 #endif
78
79 END
80 else
81 echo " found but is not working"
82 fi
83 ;;
84
6785 bug)
6886 # /bin/sh is used to intercept "Killed" or "Abort trap" messages
6987 if /bin/sh -c $NGX_AUTOTEST >/dev/null 2>&1; then
108108 . auto/feature
109109
110110
111 ngx_feature="strerror_r()"
112 ngx_feature_name="NGX_HAVE_STRERROR_R"
113 ngx_feature_run=yes
114 ngx_feature_incs="#include <string.h>"
115 ngx_feature_path=
116 ngx_feature_libs=
117 ngx_feature_test="char buf[1024]; long n; n = strerror_r(1, buf, 1024);
118 if (n < 0 || n > 1024) return 1;"
119 . auto/feature
120
121
122 # GNU style strerror_r() returns not length, but pointer
123
124 ngx_feature="gnu style strerror_r()"
125 ngx_feature_name="NGX_HAVE_GNU_STRERROR_R"
126 ngx_feature_run=yes
127 ngx_feature_incs="#include <string.h>"
128 ngx_feature_path=
129 ngx_feature_libs=
130 ngx_feature_test="char buf[1024]; long n; n = strerror_r(1, buf, 1024);
131 if (n >= 0 && n < 1024) return 1;"
132 . auto/feature
133
134
135 ngx_feature="sys_errlist[]"
136 ngx_feature_name="NGX_HAVE_SYS_ERRLIST"
137 ngx_feature_run=yes
138 ngx_feature_incs="#include <stdio.h>"
139 ngx_feature_path=
140 ngx_feature_libs=
141 ngx_feature_test="int n = sys_nerr; const char *p = sys_errlist[1];"
111 ngx_feature="sys_nerr"
112 ngx_feature_name="NGX_SYS_NERR"
113 ngx_feature_run=value
114 ngx_feature_incs='#include <stdio.h>'
115 ngx_feature_path=
116 ngx_feature_libs=
117 ngx_feature_test='printf("%d", sys_nerr);'
142118 . auto/feature
143119
144120
269269
270270 ngx_pid = ngx_getpid();
271271
272 if (ngx_strerror_init() != NGX_OK) {
273 return 1;
274 }
275
272276 log = ngx_log_init(ngx_prefix);
273277 if (log == NULL) {
274278 return 1;
247247 buf = ngx_slprintf(buf, last, " (%d: ", err);
248248 #endif
249249
250 buf = ngx_strerror_r(err, buf, last - buf);
250 buf = ngx_strerror(err, buf, last - buf);
251251
252252 if (buf < last) {
253253 *buf++ = ')';
77 #include <ngx_core.h>
88
99
10 #if (NGX_HAVE_STRERROR_R)
10 /*
11 * The strerror() messages are copied because:
12 *
13 * 1) strerror() and strerror_r() functions are not Async-Signal-Safe,
14 * therefore, they can not be used in signal handlers;
15 *
16 * 2) a direct sys_errlist[] array may be used instead of these functions,
17 * but Linux linker warns about its usage:
18 *
19 * warning: `sys_errlist' is deprecated; use `strerror' or `strerror_r' instead
20 * warning: `sys_nerr' is deprecated; use `strerror' or `strerror_r' instead
21 *
22 * causing false bug reports.
23 */
24
25
26 static ngx_str_t *ngx_sys_errlist;
27 static ngx_str_t ngx_unknown_error = ngx_string("Unknown error");
28
1129
1230 u_char *
13 ngx_strerror_r(int err, u_char *errstr, size_t size)
31 ngx_strerror(ngx_err_t err, u_char *errstr, size_t size)
1432 {
15 if (size == 0) {
16 return errstr;
33 ngx_str_t *msg;
34
35 msg = ((ngx_uint_t) err < NGX_SYS_NERR) ? &ngx_sys_errlist[err]:
36 &ngx_unknown_error;
37 size = ngx_min(size, msg->len);
38
39 return ngx_cpymem(errstr, msg->data, size);
40 }
41
42
43 ngx_uint_t
44 ngx_strerror_init(void)
45 {
46 char *msg;
47 u_char *p;
48 size_t len;
49 ngx_err_t err;
50
51 /*
52 * ngx_strerror() is not ready to work at this stage, therefore,
53 * malloc() is used and possible errors are logged using strerror().
54 */
55
56 len = NGX_SYS_NERR * sizeof(ngx_str_t);
57
58 ngx_sys_errlist = malloc(len);
59 if (ngx_sys_errlist == NULL) {
60 goto failed;
1761 }
1862
19 errstr[0] = '\0';
63 for (err = 0; err < NGX_SYS_NERR; err++) {
64 msg = strerror(err);
65 len = ngx_strlen(msg);
2066
21 strerror_r(err, (char *) errstr, size);
67 p = malloc(len);
68 if (p == NULL) {
69 goto failed;
70 }
2271
23 while (*errstr && size) {
24 errstr++;
25 size--;
72 ngx_memcpy(p, msg, len);
73 ngx_sys_errlist[err].len = len;
74 ngx_sys_errlist[err].data = p;
2675 }
2776
28 return errstr;
77 return NGX_OK;
78
79 failed:
80
81 err = errno;
82 ngx_log_stderr(0, "malloc(%uz) failed (%d: %s)", len, err, strerror(err));
83
84 return NGX_ERROR;
2985 }
30
31 #elif (NGX_HAVE_GNU_STRERROR_R)
32
33 /* Linux strerror_r() */
34
35 u_char *
36 ngx_strerror_r(int err, u_char *errstr, size_t size)
37 {
38 char *str;
39
40 if (size == 0) {
41 return errstr;
42 }
43
44 errstr[0] = '\0';
45
46 str = strerror_r(err, (char *) errstr, size);
47
48 if (str != (char *) errstr) {
49 return ngx_cpystrn(errstr, (u_char *) str, size);
50 }
51
52 while (*errstr && size) {
53 errstr++;
54 size--;
55 }
56
57 return errstr;
58 }
59
60 #endif
5959 #define ngx_set_socket_errno(err) errno = err
6060
6161
62 #if (NGX_HAVE_STRERROR_R || NGX_HAVE_GNU_STRERROR_R)
63
64 u_char *ngx_strerror_r(int err, u_char *errstr, size_t size);
65
66 #else
67
68 /* Solaris and Tru64 UNIX have thread-safe strerror() */
69
70 #define ngx_strerror_r(err, errstr, size) \
71 ngx_cpystrn(errstr, (u_char *) strerror(err), size)
72
73 #endif
74
75
76 #if (NGX_HAVE_SYS_ERRLIST)
77
78 #define ngx_sigsafe_strerror(err) \
79 (err > 0 && err < sys_nerr) ? sys_errlist[err] : "Unknown error"
80
81 #else
82
83 #define ngx_sigsafe_strerror(err) ""
84
85 #endif
62 u_char *ngx_strerror(ngx_err_t err, u_char *errstr, size_t size);
63 ngx_uint_t ngx_strerror_init(void);
8664
8765
8866 #endif /* _NGX_ERRNO_H_INCLUDED_ */
478478 */
479479
480480 if (err == NGX_ECHILD) {
481 ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0,
482 "waitpid() failed (%d: %s)",
483 err, ngx_sigsafe_strerror(err));
481 ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, err,
482 "waitpid() failed");
484483 return;
485484 }
486485
487486 #endif
488487
489 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
490 "waitpid() failed (%d: %s)",
491 err, ngx_sigsafe_strerror(err));
488 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,
489 "waitpid() failed");
492490 return;
493491 }
494492
88
99
1010 u_char *
11 ngx_strerror_r(ngx_err_t err, u_char *errstr, size_t size)
11 ngx_strerror(ngx_err_t err, u_char *errstr, size_t size)
1212 {
1313 u_int len;
1414 static u_long lang = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
4949
5050 return &errstr[++len];
5151 }
52
53
54 ngx_uint_t
55 ngx_strerror_init(void)
56 {
57 return NGX_OK;
58 }
5353 #define NGX_EALREADY WSAEALREADY
5454 #define NGX_EINVAL WSAEINVAL
5555
56 u_char *ngx_strerror_r(ngx_err_t err, u_char *errstr, size_t size);
56
57 u_char *ngx_strerror(ngx_err_t err, u_char *errstr, size_t size);
58 ngx_uint_t ngx_strerror_init(void);
5759
5860
5961 #endif /* _NGX_ERRNO_H_INCLUDED_ */