1 /* strerror_r.c --- POSIX compatible system error routine
3 Copyright (C) 2010-2011 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2010. */
22 /* Enable declaration of sys_nerr and sys_errlist in <errno.h> on NetBSD. */
23 #define _NETBSD_SOURCE 1
31 #if GNULIB_defined_ESOCK /* native Windows platforms */
33 # include <winsock2.h>
37 /* Reasonable buffer size that should never trigger ERANGE; if this
38 proves too small, we intentionally abort(), to remind us to fix
39 this value as well as strerror-impl.h. */
40 #define STACKBUF_LEN 256
42 #if (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__) && HAVE___XPG_STRERROR_R /* glibc >= 2.3.4, cygwin >= 1.7.9 */
44 # define USE_XPG_STRERROR_R 1
46 #elif HAVE_DECL_STRERROR_R && !(__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__)
48 /* The system's strerror_r function is OK, except that its third argument
49 is 'int', not 'size_t', or its return type is wrong. */
53 # define USE_SYSTEM_STRERROR_R 1
55 #else /* (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__ ? !HAVE___XPG_STRERROR_R : !HAVE_DECL_STRERROR_R) */
57 /* Use the system's strerror(). Exclude glibc and cygwin because the
58 system strerror_r has the wrong return type, and cygwin 1.7.9
59 strerror_r clobbers strerror. */
62 # define USE_SYSTEM_STRERROR 1
64 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __sgi || (defined __sun && !defined _LP64) || defined __CYGWIN__
66 /* No locking needed. */
68 /* Get catgets internationalization functions. */
70 # include <nl_types.h>
73 /* Get sys_nerr, sys_errlist on HP-UX (otherwise only declared in C++ mode).
74 Get sys_nerr, sys_errlist on IRIX (otherwise only declared with _SGIAPI). */
75 # if defined __hpux || defined __sgi
77 extern char *sys_errlist[];
80 /* Get sys_nerr on Solaris. */
81 # if defined __sun && !defined _LP64
85 /* Get sys_nerr, sys_errlist on native Windows. */
90 # include "glthread/lock.h"
92 /* This lock protects the buffer returned by strerror(). We assume that
93 no other uses of strerror() exist in the program. */
94 gl_lock_define_initialized(static, strerror_lock)
100 /* Copy as much of MSG into BUF as possible, without corrupting errno.
101 Return 0 if MSG fit in BUFLEN, otherwise return ERANGE. */
103 safe_copy (char *buf, size_t buflen, const char *msg)
105 size_t len = strlen (msg);
110 /* Although POSIX allows memcpy() to corrupt errno, we don't
111 know of any implementation where this is a real problem. */
112 memcpy (buf, msg, len + 1);
117 memcpy (buf, msg, buflen - 1);
118 buf[buflen - 1] = '\0';
126 strerror_r (int errnum, char *buf, size_t buflen)
129 /* Filter this out now, so that rest of this replacement knows that
130 there is room for a non-empty message and trailing NUL. */
139 #if GNULIB_defined_ETXTBSY \
140 || GNULIB_defined_ESOCK \
141 || GNULIB_defined_ENOMSG \
142 || GNULIB_defined_EIDRM \
143 || GNULIB_defined_ENOLINK \
144 || GNULIB_defined_EPROTO \
145 || GNULIB_defined_EMULTIHOP \
146 || GNULIB_defined_EBADMSG \
147 || GNULIB_defined_EOVERFLOW \
148 || GNULIB_defined_ENOTSUP \
149 || GNULIB_defined_ESTALE \
150 || GNULIB_defined_EDQUOT \
151 || GNULIB_defined_ECANCELED
153 char const *msg = NULL;
154 /* These error messages are taken from glibc/sysdeps/gnu/errlist.c. */
157 # if GNULIB_defined_ETXTBSY
159 msg = "Text file busy";
163 # if GNULIB_defined_ESOCK /* native Windows platforms */
164 /* EWOULDBLOCK is the same as EAGAIN. */
166 msg = "Operation now in progress";
169 msg = "Operation already in progress";
172 msg = "Socket operation on non-socket";
175 msg = "Destination address required";
178 msg = "Message too long";
181 msg = "Protocol wrong type for socket";
184 msg = "Protocol not available";
186 case EPROTONOSUPPORT:
187 msg = "Protocol not supported";
189 case ESOCKTNOSUPPORT:
190 msg = "Socket type not supported";
193 msg = "Operation not supported";
196 msg = "Protocol family not supported";
199 msg = "Address family not supported by protocol";
202 msg = "Address already in use";
205 msg = "Cannot assign requested address";
208 msg = "Network is down";
211 msg = "Network is unreachable";
214 msg = "Network dropped connection on reset";
217 msg = "Software caused connection abort";
220 msg = "Connection reset by peer";
223 msg = "No buffer space available";
226 msg = "Transport endpoint is already connected";
229 msg = "Transport endpoint is not connected";
232 msg = "Cannot send after transport endpoint shutdown";
235 msg = "Too many references: cannot splice";
238 msg = "Connection timed out";
241 msg = "Connection refused";
244 msg = "Too many levels of symbolic links";
247 msg = "Host is down";
250 msg = "No route to host";
253 msg = "Too many processes";
256 msg = "Too many users";
259 msg = "Disk quota exceeded";
262 msg = "Stale NFS file handle";
265 msg = "Object is remote";
268 /* WSA_INVALID_HANDLE maps to EBADF */
269 /* WSA_NOT_ENOUGH_MEMORY maps to ENOMEM */
270 /* WSA_INVALID_PARAMETER maps to EINVAL */
271 case WSA_OPERATION_ABORTED:
272 msg = "Overlapped operation aborted";
274 case WSA_IO_INCOMPLETE:
275 msg = "Overlapped I/O event object not in signaled state";
278 msg = "Overlapped operations will complete later";
280 /* WSAEINTR maps to EINTR */
281 /* WSAEBADF maps to EBADF */
282 /* WSAEACCES maps to EACCES */
283 /* WSAEFAULT maps to EFAULT */
284 /* WSAEINVAL maps to EINVAL */
285 /* WSAEMFILE maps to EMFILE */
286 /* WSAEWOULDBLOCK maps to EWOULDBLOCK */
287 /* WSAEINPROGRESS is EINPROGRESS */
288 /* WSAEALREADY is EALREADY */
289 /* WSAENOTSOCK is ENOTSOCK */
290 /* WSAEDESTADDRREQ is EDESTADDRREQ */
291 /* WSAEMSGSIZE is EMSGSIZE */
292 /* WSAEPROTOTYPE is EPROTOTYPE */
293 /* WSAENOPROTOOPT is ENOPROTOOPT */
294 /* WSAEPROTONOSUPPORT is EPROTONOSUPPORT */
295 /* WSAESOCKTNOSUPPORT is ESOCKTNOSUPPORT */
296 /* WSAEOPNOTSUPP is EOPNOTSUPP */
297 /* WSAEPFNOSUPPORT is EPFNOSUPPORT */
298 /* WSAEAFNOSUPPORT is EAFNOSUPPORT */
299 /* WSAEADDRINUSE is EADDRINUSE */
300 /* WSAEADDRNOTAVAIL is EADDRNOTAVAIL */
301 /* WSAENETDOWN is ENETDOWN */
302 /* WSAENETUNREACH is ENETUNREACH */
303 /* WSAENETRESET is ENETRESET */
304 /* WSAECONNABORTED is ECONNABORTED */
305 /* WSAECONNRESET is ECONNRESET */
306 /* WSAENOBUFS is ENOBUFS */
307 /* WSAEISCONN is EISCONN */
308 /* WSAENOTCONN is ENOTCONN */
309 /* WSAESHUTDOWN is ESHUTDOWN */
310 /* WSAETOOMANYREFS is ETOOMANYREFS */
311 /* WSAETIMEDOUT is ETIMEDOUT */
312 /* WSAECONNREFUSED is ECONNREFUSED */
313 /* WSAELOOP is ELOOP */
314 /* WSAENAMETOOLONG maps to ENAMETOOLONG */
315 /* WSAEHOSTDOWN is EHOSTDOWN */
316 /* WSAEHOSTUNREACH is EHOSTUNREACH */
317 /* WSAENOTEMPTY maps to ENOTEMPTY */
318 /* WSAEPROCLIM is EPROCLIM */
319 /* WSAEUSERS is EUSERS */
320 /* WSAEDQUOT is EDQUOT */
321 /* WSAESTALE is ESTALE */
322 /* WSAEREMOTE is EREMOTE */
324 msg = "Network subsystem is unavailable";
326 case WSAVERNOTSUPPORTED:
327 msg = "Winsock.dll version out of range";
329 case WSANOTINITIALISED:
330 msg = "Successful WSAStartup not yet performed";
333 msg = "Graceful shutdown in progress";
335 case WSAENOMORE: case WSA_E_NO_MORE:
336 msg = "No more results";
338 case WSAECANCELLED: case WSA_E_CANCELLED:
339 msg = "Call was canceled";
341 case WSAEINVALIDPROCTABLE:
342 msg = "Procedure call table is invalid";
344 case WSAEINVALIDPROVIDER:
345 msg = "Service provider is invalid";
347 case WSAEPROVIDERFAILEDINIT:
348 msg = "Service provider failed to initialize";
350 case WSASYSCALLFAILURE:
351 msg = "System call failure";
353 case WSASERVICE_NOT_FOUND:
354 msg = "Service not found";
356 case WSATYPE_NOT_FOUND:
357 msg = "Class type not found";
360 msg = "Database query was refused";
362 case WSAHOST_NOT_FOUND:
363 msg = "Host not found";
366 msg = "Nonauthoritative host not found";
369 msg = "Nonrecoverable error";
372 msg = "Valid name, no data record of requested type";
374 /* WSA_QOS_* omitted */
378 # if GNULIB_defined_ENOMSG
380 msg = "No message of desired type";
384 # if GNULIB_defined_EIDRM
386 msg = "Identifier removed";
390 # if GNULIB_defined_ENOLINK
392 msg = "Link has been severed";
396 # if GNULIB_defined_EPROTO
398 msg = "Protocol error";
402 # if GNULIB_defined_EMULTIHOP
404 msg = "Multihop attempted";
408 # if GNULIB_defined_EBADMSG
414 # if GNULIB_defined_EOVERFLOW
416 msg = "Value too large for defined data type";
420 # if GNULIB_defined_ENOTSUP
422 msg = "Not supported";
426 # if GNULIB_defined_ESTALE
428 msg = "Stale NFS file handle";
432 # if GNULIB_defined_EDQUOT
434 msg = "Disk quota exceeded";
438 # if GNULIB_defined_ECANCELED
440 msg = "Operation canceled";
446 return safe_copy (buf, buflen, msg);
452 int saved_errno = errno;
454 #if USE_XPG_STRERROR_R
457 extern int __xpg_strerror_r (int errnum, char *buf, size_t buflen);
459 ret = __xpg_strerror_r (errnum, buf, buflen);
464 /* glibc 2.13 would not touch buf on err, so we have to fall
465 back to GNU strerror_r which always returns a thread-safe
466 untruncated string to (partially) copy into our buf. */
467 safe_copy (buf, buflen, strerror_r (errnum, buf, buflen));
471 #elif USE_SYSTEM_STRERROR_R
473 if (buflen > INT_MAX)
477 /* On HP-UX 11.31, strerror_r always fails when buflen < 80; it
478 also fails to change buf on EINVAL. */
482 if (buflen < sizeof stackbuf)
484 ret = strerror_r (errnum, stackbuf, sizeof stackbuf);
486 ret = safe_copy (buf, buflen, stackbuf);
489 ret = strerror_r (errnum, buf, buflen);
492 /* Solaris 10 does not populate buf on ERANGE. */
493 ret = strerror_r (errnum, buf, buflen);
494 if (ret == ERANGE && !*buf)
496 char stackbuf[STACKBUF_LEN];
498 /* strerror-impl.h is also affected if our choice of stackbuf
499 size is not large enough. */
500 if (strerror_r (errnum, stackbuf, sizeof stackbuf) == ERANGE)
502 safe_copy (buf, buflen, stackbuf);
507 /* AIX returns 0 rather than ERANGE when truncating strings; try
508 again until we are sure we got the entire string. */
509 if (!ret && strlen (buf) == buflen - 1)
511 char stackbuf[STACKBUF_LEN];
513 strerror_r (errnum, stackbuf, sizeof stackbuf);
514 len = strlen (stackbuf);
515 /* stackbuf should have been large enough. */
516 if (len + 1 == sizeof stackbuf)
523 /* Some old implementations may return (-1, EINVAL) instead of EINVAL. */
527 /* FreeBSD rejects 0; see http://austingroupbugs.net/view.php?id=382. */
528 if (errnum == 0 && ret == EINVAL)
529 ret = safe_copy (buf, buflen, "Success");
531 #else /* USE_SYSTEM_STRERROR */
533 /* Try to do what strerror (errnum) does, but without clobbering the
534 buffer used by strerror(). */
536 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __CYGWIN__ /* NetBSD, HP-UX, native Win32, Cygwin */
538 /* NetBSD: sys_nerr, sys_errlist are declared through _NETBSD_SOURCE
540 HP-UX: sys_nerr, sys_errlist are declared explicitly above.
541 native Win32: sys_nerr, sys_errlist are declared in <stdlib.h>.
542 Cygwin: sys_nerr, sys_errlist are declared in <errno.h>. */
543 if (errnum >= 0 && errnum < sys_nerr)
545 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
546 # if defined __NetBSD__
547 nl_catd catd = catopen ("libc", NL_CAT_LOCALE);
550 ? catgets (catd, 1, errnum, sys_errlist[errnum])
551 : sys_errlist[errnum]);
554 nl_catd catd = catopen ("perror", NL_CAT_LOCALE);
557 ? catgets (catd, 1, 1 + errnum, sys_errlist[errnum])
558 : sys_errlist[errnum]);
561 const char *errmsg = sys_errlist[errnum];
563 if (errmsg == NULL || *errmsg == '\0')
566 ret = safe_copy (buf, buflen, errmsg);
567 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
568 if (catd != (nl_catd)-1)
575 # elif defined __sgi || (defined __sun && !defined _LP64) /* IRIX, Solaris <= 9 32-bit */
577 /* For a valid error number, the system's strerror() function returns
578 a pointer to a not copied string, not to a buffer. */
579 if (errnum >= 0 && errnum < sys_nerr)
581 char *errmsg = strerror (errnum);
583 if (errmsg == NULL || *errmsg == '\0')
586 ret = safe_copy (buf, buflen, errmsg);
593 gl_lock_lock (strerror_lock);
596 char *errmsg = strerror (errnum);
598 /* For invalid error numbers, strerror() on
599 - IRIX 6.5 returns NULL,
600 - HP-UX 11 returns an empty string. */
601 if (errmsg == NULL || *errmsg == '\0')
604 ret = safe_copy (buf, buflen, errmsg);
607 gl_lock_unlock (strerror_lock);
613 if (ret == EINVAL && !*buf)
614 snprintf (buf, buflen, "Unknown error %d", errnum);