1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
/*-------------------------------------------------------------------------
*
* strerror.c
* Replacements for standard strerror() and strerror_r() functions
*
* Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/port/strerror.c
*
*-------------------------------------------------------------------------
*/
#include "c.h"
/*
* Within this file, "strerror" means the platform's function not pg_strerror,
* and likewise for "strerror_r"
*/
#undef strerror
#undef strerror_r
static char *gnuish_strerror_r(int errnum, char *buf, size_t buflen);
static char *get_errno_symbol(int errnum);
#ifdef WIN32
static char *win32_socket_strerror(int errnum, char *buf, size_t buflen);
#endif
/*
* A slightly cleaned-up version of strerror()
*/
char *
pg_strerror(int errnum)
{
static char errorstr_buf[PG_STRERROR_R_BUFLEN];
return pg_strerror_r(errnum, errorstr_buf, sizeof(errorstr_buf));
}
/*
* A slightly cleaned-up version of strerror_r()
*/
char *
pg_strerror_r(int errnum, char *buf, size_t buflen)
{
char *str;
/* If it's a Windows Winsock error, that needs special handling */
#ifdef WIN32
/* Winsock error code range, per WinError.h */
if (errnum >= 10000 && errnum <= 11999)
return win32_socket_strerror(errnum, buf, buflen);
#endif
/* Try the platform's strerror_r(), or maybe just strerror() */
str = gnuish_strerror_r(errnum, buf, buflen);
/*
* Some strerror()s return an empty string for out-of-range errno. This
* is ANSI C spec compliant, but not exactly useful. Also, we may get
* back strings of question marks if libc cannot transcode the message to
* the codeset specified by LC_CTYPE. If we get nothing useful, first try
* get_errno_symbol(), and if that fails, print the numeric errno.
*/
if (str == NULL || *str == '\0' || *str == '?')
str = get_errno_symbol(errnum);
if (str == NULL)
{
snprintf(buf, buflen, _("operating system error %d"), errnum);
str = buf;
}
return str;
}
/*
* Simple wrapper to emulate GNU strerror_r if what the platform provides is
* POSIX. Also, if platform lacks strerror_r altogether, fall back to plain
* strerror; it might not be very thread-safe, but tough luck.
*/
static char *
gnuish_strerror_r(int errnum, char *buf, size_t buflen)
{
#ifdef HAVE_STRERROR_R
#ifdef STRERROR_R_INT
/* POSIX API */
if (strerror_r(errnum, buf, buflen) == 0)
return buf;
return NULL; /* let caller deal with failure */
#else
/* GNU API */
return strerror_r(errnum, buf, buflen);
#endif
#else /* !HAVE_STRERROR_R */
char *sbuf = strerror(errnum);
if (sbuf == NULL) /* can this still happen anywhere? */
return NULL;
/* To minimize thread-unsafety hazard, copy into caller's buffer */
strlcpy(buf, sbuf, buflen);
return buf;
#endif
}
/*
* Returns a symbol (e.g. "ENOENT") for an errno code.
* Returns NULL if the code is unrecognized.
*/
static char *
get_errno_symbol(int errnum)
{
switch (errnum)
{
case E2BIG:
return "E2BIG";
case EACCES:
return "EACCES";
case EADDRINUSE:
return "EADDRINUSE";
case EADDRNOTAVAIL:
return "EADDRNOTAVAIL";
case EAFNOSUPPORT:
return "EAFNOSUPPORT";
#ifdef EAGAIN
case EAGAIN:
return "EAGAIN";
#endif
#ifdef EALREADY
case EALREADY:
return "EALREADY";
#endif
case EBADF:
return "EBADF";
#ifdef EBADMSG
case EBADMSG:
return "EBADMSG";
#endif
case EBUSY:
return "EBUSY";
case ECHILD:
return "ECHILD";
case ECONNABORTED:
return "ECONNABORTED";
case ECONNREFUSED:
return "ECONNREFUSED";
case ECONNRESET:
return "ECONNRESET";
case EDEADLK:
return "EDEADLK";
case EDOM:
return "EDOM";
case EEXIST:
return "EEXIST";
case EFAULT:
return "EFAULT";
case EFBIG:
return "EFBIG";
case EHOSTDOWN:
return "EHOSTDOWN";
case EHOSTUNREACH:
return "EHOSTUNREACH";
case EIDRM:
return "EIDRM";
case EINPROGRESS:
return "EINPROGRESS";
case EINTR:
return "EINTR";
case EINVAL:
return "EINVAL";
case EIO:
return "EIO";
case EISCONN:
return "EISCONN";
case EISDIR:
return "EISDIR";
#ifdef ELOOP
case ELOOP:
return "ELOOP";
#endif
case EMFILE:
return "EMFILE";
case EMLINK:
return "EMLINK";
case EMSGSIZE:
return "EMSGSIZE";
case ENAMETOOLONG:
return "ENAMETOOLONG";
case ENETDOWN:
return "ENETDOWN";
case ENETRESET:
return "ENETRESET";
case ENETUNREACH:
return "ENETUNREACH";
case ENFILE:
return "ENFILE";
case ENOBUFS:
return "ENOBUFS";
case ENODEV:
return "ENODEV";
case ENOENT:
return "ENOENT";
case ENOEXEC:
return "ENOEXEC";
case ENOMEM:
return "ENOMEM";
case ENOSPC:
return "ENOSPC";
case ENOSYS:
return "ENOSYS";
case ENOTCONN:
return "ENOTCONN";
case ENOTDIR:
return "ENOTDIR";
#if defined(ENOTEMPTY) && (ENOTEMPTY != EEXIST) /* same code on AIX */
case ENOTEMPTY:
return "ENOTEMPTY";
#endif
case ENOTSOCK:
return "ENOTSOCK";
#ifdef ENOTSUP
case ENOTSUP:
return "ENOTSUP";
#endif
case ENOTTY:
return "ENOTTY";
case ENXIO:
return "ENXIO";
#if defined(EOPNOTSUPP) && (!defined(ENOTSUP) || (EOPNOTSUPP != ENOTSUP))
case EOPNOTSUPP:
return "EOPNOTSUPP";
#endif
#ifdef EOVERFLOW
case EOVERFLOW:
return "EOVERFLOW";
#endif
case EPERM:
return "EPERM";
case EPIPE:
return "EPIPE";
case EPROTONOSUPPORT:
return "EPROTONOSUPPORT";
case ERANGE:
return "ERANGE";
#ifdef EROFS
case EROFS:
return "EROFS";
#endif
case ESRCH:
return "ESRCH";
case ETIMEDOUT:
return "ETIMEDOUT";
#ifdef ETXTBSY
case ETXTBSY:
return "ETXTBSY";
#endif
#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
case EWOULDBLOCK:
return "EWOULDBLOCK";
#endif
case EXDEV:
return "EXDEV";
}
return NULL;
}
#ifdef WIN32
/*
* Windows' strerror() doesn't know the Winsock codes, so handle them this way
*/
static char *
win32_socket_strerror(int errnum, char *buf, size_t buflen)
{
static HANDLE handleDLL = INVALID_HANDLE_VALUE;
if (handleDLL == INVALID_HANDLE_VALUE)
{
handleDLL = LoadLibraryEx("netmsg.dll", NULL,
DONT_RESOLVE_DLL_REFERENCES | LOAD_LIBRARY_AS_DATAFILE);
if (handleDLL == NULL)
{
snprintf(buf, buflen,
"winsock error %d (could not load netmsg.dll to translate: error code %lu)",
errnum, GetLastError());
return buf;
}
}
ZeroMemory(buf, buflen);
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_FROM_HMODULE,
handleDLL,
errnum,
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
buf,
buflen - 1,
NULL) == 0)
{
/* Failed to get id */
snprintf(buf, buflen, "unrecognized winsock error %d", errnum);
}
return buf;
}
#endif /* WIN32 */
|