comparison src/os/unix/ngx_errno.c @ 7785:c43a2e8fdf7e

Introduced strerrordesc_np() support. The strerrordesc_np() function, introduced in glibc 2.32, provides an async-signal-safe way to obtain error messages. This makes it possible to avoid copying error messages.
author Maxim Dounin <mdounin@mdounin.ru>
date Mon, 01 Mar 2021 20:00:45 +0300
parents 8cc5b0365ee5
children
comparison
equal deleted inserted replaced
7784:8cc5b0365ee5 7785:c43a2e8fdf7e
7 7
8 #include <ngx_config.h> 8 #include <ngx_config.h>
9 #include <ngx_core.h> 9 #include <ngx_core.h>
10 10
11 11
12 static ngx_str_t ngx_unknown_error = ngx_string("Unknown error");
13
14
15 #if (NGX_HAVE_STRERRORDESC_NP)
16
17 /*
18 * The strerrordesc_np() function, introduced in glibc 2.32, is
19 * async-signal-safe. This makes it possible to use it directly,
20 * without copying error messages.
21 */
22
23
24 u_char *
25 ngx_strerror(ngx_err_t err, u_char *errstr, size_t size)
26 {
27 size_t len;
28 const char *msg;
29
30 msg = strerrordesc_np(err);
31
32 if (msg == NULL) {
33 msg = (char *) ngx_unknown_error.data;
34 len = ngx_unknown_error.len;
35
36 } else {
37 len = ngx_strlen(msg);
38 }
39
40 size = ngx_min(size, len);
41
42 return ngx_cpymem(errstr, msg, size);
43 }
44
45
46 ngx_int_t
47 ngx_strerror_init(void)
48 {
49 return NGX_OK;
50 }
51
52
53 #else
54
12 /* 55 /*
13 * The strerror() messages are copied because: 56 * The strerror() messages are copied because:
14 * 57 *
15 * 1) strerror() and strerror_r() functions are not Async-Signal-Safe, 58 * 1) strerror() and strerror_r() functions are not Async-Signal-Safe,
16 * therefore, they cannot be used in signal handlers; 59 * therefore, they cannot be used in signal handlers;
24 * causing false bug reports. 67 * causing false bug reports.
25 */ 68 */
26 69
27 70
28 static ngx_str_t *ngx_sys_errlist; 71 static ngx_str_t *ngx_sys_errlist;
29 static ngx_str_t ngx_unknown_error = ngx_string("Unknown error");
30 static ngx_err_t ngx_first_error; 72 static ngx_err_t ngx_first_error;
31 static ngx_err_t ngx_last_error; 73 static ngx_err_t ngx_last_error;
32 74
33 75
34 u_char * 76 u_char *
162 err = errno; 204 err = errno;
163 ngx_log_stderr(0, "malloc(%uz) failed (%d: %s)", len, err, strerror(err)); 205 ngx_log_stderr(0, "malloc(%uz) failed (%d: %s)", len, err, strerror(err));
164 206
165 return NGX_ERROR; 207 return NGX_ERROR;
166 } 208 }
209
210 #endif