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
30 #if GNULIB_defined_ESOCK /* native Windows platforms */
32 # include <winsock2.h>
36 /* Reasonable buffer size that should never trigger ERANGE; if this
37 proves too small, we intentionally abort(), to remind us to fix
38 this value as well as strerror-impl.h. */
39 #define STACKBUF_LEN 256
41 #if (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__) && HAVE___XPG_STRERROR_R /* glibc >= 2.3.4, cygwin >= 1.7.9 */
43 # define USE_XPG_STRERROR_R 1
45 #elif HAVE_DECL_STRERROR_R && !(__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__)
47 /* The system's strerror_r function is OK, except that its third argument
48 is 'int', not 'size_t', or its return type is wrong. */
52 # define USE_SYSTEM_STRERROR_R 1
54 #else /* (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__ ? !HAVE___XPG_STRERROR_R : !HAVE_DECL_STRERROR_R) */
56 /* Use the system's strerror(). Exclude glibc and cygwin because the
57 system strerror_r has the wrong return type, and cygwin 1.7.9
58 strerror_r clobbers strerror. */
61 # define USE_SYSTEM_STRERROR 1
63 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __sgi || (defined __sun && !defined _LP64) || defined __CYGWIN__
65 /* No locking needed. */
67 /* Get catgets internationalization functions. */
69 # include <nl_types.h>
72 /* Get sys_nerr, sys_errlist on HP-UX (otherwise only declared in C++ mode).
73 Get sys_nerr, sys_errlist on IRIX (otherwise only declared with _SGIAPI). */
74 # if defined __hpux || defined __sgi
76 extern char *sys_errlist[];
79 /* Get sys_nerr on Solaris. */
80 # if defined __sun && !defined _LP64
84 /* Get sys_nerr, sys_errlist on native Windows. */
89 # include "glthread/lock.h"
91 /* This lock protects the buffer returned by strerror(). We assume that
92 no other uses of strerror() exist in the program. */
93 gl_lock_define_initialized(static, strerror_lock)
99 /* Copy as much of MSG into BUF as possible, without corrupting errno.
100 Return 0 if MSG fit in BUFLEN, otherwise return ERANGE. */
102 safe_copy (char *buf, size_t buflen, const char *msg)
104 size_t len = strlen (msg);
109 /* Although POSIX allows memcpy() to corrupt errno, we don't
110 know of any implementation where this is a real problem. */
111 memcpy (buf, msg, len + 1);
116 memcpy (buf, msg, buflen - 1);
117 buf[buflen - 1] = '\0';
125 strerror_r (int errnum, char *buf, size_t buflen)
128 /* Filter this out now, so that rest of this replacement knows that
129 there is room for a non-empty message and trailing NUL. */
138 #if GNULIB_defined_ETXTBSY \
139 || GNULIB_defined_ESOCK \
140 || GNULIB_defined_ENOMSG \
141 || GNULIB_defined_EIDRM \
142 || GNULIB_defined_ENOLINK \
143 || GNULIB_defined_EPROTO \
144 || GNULIB_defined_EMULTIHOP \
145 || GNULIB_defined_EBADMSG \
146 || GNULIB_defined_EOVERFLOW \
147 || GNULIB_defined_ENOTSUP \
148 || GNULIB_defined_ESTALE \
149 || GNULIB_defined_EDQUOT \
150 || GNULIB_defined_ECANCELED
152 char const *msg = NULL;
153 /* These error messages are taken from glibc/sysdeps/gnu/errlist.c. */
156 # if GNULIB_defined_ETXTBSY
158 msg = "Text file busy";
162 # if GNULIB_defined_ESOCK /* native Windows platforms */
163 /* EWOULDBLOCK is the same as EAGAIN. */
165 msg = "Operation now in progress";
168 msg = "Operation already in progress";
171 msg = "Socket operation on non-socket";
174 msg = "Destination address required";
177 msg = "Message too long";
180 msg = "Protocol wrong type for socket";
183 msg = "Protocol not available";
185 case EPROTONOSUPPORT:
186 msg = "Protocol not supported";
188 case ESOCKTNOSUPPORT:
189 msg = "Socket type not supported";
192 msg = "Operation not supported";
195 msg = "Protocol family not supported";
198 msg = "Address family not supported by protocol";
201 msg = "Address already in use";
204 msg = "Cannot assign requested address";
207 msg = "Network is down";
210 msg = "Network is unreachable";
213 msg = "Network dropped connection on reset";
216 msg = "Software caused connection abort";
219 msg = "Connection reset by peer";
222 msg = "No buffer space available";
225 msg = "Transport endpoint is already connected";
228 msg = "Transport endpoint is not connected";
231 msg = "Cannot send after transport endpoint shutdown";
234 msg = "Too many references: cannot splice";
237 msg = "Connection timed out";
240 msg = "Connection refused";
243 msg = "Too many levels of symbolic links";
246 msg = "Host is down";
249 msg = "No route to host";
252 msg = "Too many processes";
255 msg = "Too many users";
258 msg = "Disk quota exceeded";
261 msg = "Stale NFS file handle";
264 msg = "Object is remote";
267 /* WSA_INVALID_HANDLE maps to EBADF */
268 /* WSA_NOT_ENOUGH_MEMORY maps to ENOMEM */
269 /* WSA_INVALID_PARAMETER maps to EINVAL */
270 case WSA_OPERATION_ABORTED:
271 msg = "Overlapped operation aborted";
273 case WSA_IO_INCOMPLETE:
274 msg = "Overlapped I/O event object not in signaled state";
277 msg = "Overlapped operations will complete later";
279 /* WSAEINTR maps to EINTR */
280 /* WSAEBADF maps to EBADF */
281 /* WSAEACCES maps to EACCES */
282 /* WSAEFAULT maps to EFAULT */
283 /* WSAEINVAL maps to EINVAL */
284 /* WSAEMFILE maps to EMFILE */
285 /* WSAEWOULDBLOCK maps to EWOULDBLOCK */
286 /* WSAEINPROGRESS is EINPROGRESS */
287 /* WSAEALREADY is EALREADY */
288 /* WSAENOTSOCK is ENOTSOCK */
289 /* WSAEDESTADDRREQ is EDESTADDRREQ */
290 /* WSAEMSGSIZE is EMSGSIZE */
291 /* WSAEPROTOTYPE is EPROTOTYPE */
292 /* WSAENOPROTOOPT is ENOPROTOOPT */
293 /* WSAEPROTONOSUPPORT is EPROTONOSUPPORT */
294 /* WSAESOCKTNOSUPPORT is ESOCKTNOSUPPORT */
295 /* WSAEOPNOTSUPP is EOPNOTSUPP */
296 /* WSAEPFNOSUPPORT is EPFNOSUPPORT */
297 /* WSAEAFNOSUPPORT is EAFNOSUPPORT */
298 /* WSAEADDRINUSE is EADDRINUSE */
299 /* WSAEADDRNOTAVAIL is EADDRNOTAVAIL */
300 /* WSAENETDOWN is ENETDOWN */
301 /* WSAENETUNREACH is ENETUNREACH */
302 /* WSAENETRESET is ENETRESET */
303 /* WSAECONNABORTED is ECONNABORTED */
304 /* WSAECONNRESET is ECONNRESET */
305 /* WSAENOBUFS is ENOBUFS */
306 /* WSAEISCONN is EISCONN */
307 /* WSAENOTCONN is ENOTCONN */
308 /* WSAESHUTDOWN is ESHUTDOWN */
309 /* WSAETOOMANYREFS is ETOOMANYREFS */
310 /* WSAETIMEDOUT is ETIMEDOUT */
311 /* WSAECONNREFUSED is ECONNREFUSED */
312 /* WSAELOOP is ELOOP */
313 /* WSAENAMETOOLONG maps to ENAMETOOLONG */
314 /* WSAEHOSTDOWN is EHOSTDOWN */
315 /* WSAEHOSTUNREACH is EHOSTUNREACH */
316 /* WSAENOTEMPTY maps to ENOTEMPTY */
317 /* WSAEPROCLIM is EPROCLIM */
318 /* WSAEUSERS is EUSERS */
319 /* WSAEDQUOT is EDQUOT */
320 /* WSAESTALE is ESTALE */
321 /* WSAEREMOTE is EREMOTE */
323 msg = "Network subsystem is unavailable";
325 case WSAVERNOTSUPPORTED:
326 msg = "Winsock.dll version out of range";
328 case WSANOTINITIALISED:
329 msg = "Successful WSAStartup not yet performed";
332 msg = "Graceful shutdown in progress";
334 case WSAENOMORE: case WSA_E_NO_MORE:
335 msg = "No more results";
337 case WSAECANCELLED: case WSA_E_CANCELLED:
338 msg = "Call was canceled";
340 case WSAEINVALIDPROCTABLE:
341 msg = "Procedure call table is invalid";
343 case WSAEINVALIDPROVIDER:
344 msg = "Service provider is invalid";
346 case WSAEPROVIDERFAILEDINIT:
347 msg = "Service provider failed to initialize";
349 case WSASYSCALLFAILURE:
350 msg = "System call failure";
352 case WSASERVICE_NOT_FOUND:
353 msg = "Service not found";
355 case WSATYPE_NOT_FOUND:
356 msg = "Class type not found";
359 msg = "Database query was refused";
361 case WSAHOST_NOT_FOUND:
362 msg = "Host not found";
365 msg = "Nonauthoritative host not found";
368 msg = "Nonrecoverable error";
371 msg = "Valid name, no data record of requested type";
373 /* WSA_QOS_* omitted */
377 # if GNULIB_defined_ENOMSG
379 msg = "No message of desired type";
383 # if GNULIB_defined_EIDRM
385 msg = "Identifier removed";
389 # if GNULIB_defined_ENOLINK
391 msg = "Link has been severed";
395 # if GNULIB_defined_EPROTO
397 msg = "Protocol error";
401 # if GNULIB_defined_EMULTIHOP
403 msg = "Multihop attempted";
407 # if GNULIB_defined_EBADMSG
413 # if GNULIB_defined_EOVERFLOW
415 msg = "Value too large for defined data type";
419 # if GNULIB_defined_ENOTSUP
421 msg = "Not supported";
425 # if GNULIB_defined_ESTALE
427 msg = "Stale NFS file handle";
431 # if GNULIB_defined_EDQUOT
433 msg = "Disk quota exceeded";
437 # if GNULIB_defined_ECANCELED
439 msg = "Operation canceled";
445 return safe_copy (buf, buflen, msg);
451 int saved_errno = errno;
453 #if USE_XPG_STRERROR_R
456 extern int __xpg_strerror_r (int errnum, char *buf, size_t buflen);
458 ret = __xpg_strerror_r (errnum, buf, buflen);
463 /* glibc 2.13 would not touch buf on err, so we have to fall
464 back to GNU strerror_r which always returns a thread-safe
465 untruncated string to (partially) copy into our buf. */
466 safe_copy (buf, buflen, strerror_r (errnum, buf, buflen));
470 #elif USE_SYSTEM_STRERROR_R
472 if (buflen > INT_MAX)
476 /* On HP-UX 11.31, strerror_r always fails when buflen < 80; it
477 also fails to change buf on EINVAL. */
481 if (buflen < sizeof stackbuf)
483 ret = strerror_r (errnum, stackbuf, sizeof stackbuf);
485 ret = safe_copy (buf, buflen, stackbuf);
488 ret = strerror_r (errnum, buf, buflen);
491 /* Solaris 10 does not populate buf on ERANGE. */
492 ret = strerror_r (errnum, buf, buflen);
493 if (ret == ERANGE && !*buf)
495 char stackbuf[STACKBUF_LEN];
497 /* strerror-impl.h is also affected if our choice of stackbuf
498 size is not large enough. */
499 if (strerror_r (errnum, stackbuf, sizeof stackbuf) == ERANGE)
501 safe_copy (buf, buflen, stackbuf);
506 /* AIX returns 0 rather than ERANGE when truncating strings; try
507 again until we are sure we got the entire string. */
508 if (!ret && strlen (buf) == buflen - 1)
510 char stackbuf[STACKBUF_LEN];
512 strerror_r (errnum, stackbuf, sizeof stackbuf);
513 len = strlen (stackbuf);
514 /* stackbuf should have been large enough. */
515 if (len + 1 == sizeof stackbuf)
522 /* Some old implementations may return (-1, EINVAL) instead of EINVAL. */
526 /* FreeBSD rejects 0; see http://austingroupbugs.net/view.php?id=382. */
527 if (errnum == 0 && ret == EINVAL)
528 ret = safe_copy (buf, buflen, "Success");
530 #else /* USE_SYSTEM_STRERROR */
532 /* Try to do what strerror (errnum) does, but without clobbering the
533 buffer used by strerror(). */
535 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __CYGWIN__ /* NetBSD, HP-UX, native Win32, Cygwin */
537 /* NetBSD: sys_nerr, sys_errlist are declared through _NETBSD_SOURCE
539 HP-UX: sys_nerr, sys_errlist are declared explicitly above.
540 native Win32: sys_nerr, sys_errlist are declared in <stdlib.h>.
541 Cygwin: sys_nerr, sys_errlist are declared in <errno.h>. */
542 if (errnum >= 0 && errnum < sys_nerr)
544 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
545 # if defined __NetBSD__
546 nl_catd catd = catopen ("libc", NL_CAT_LOCALE);
549 ? catgets (catd, 1, errnum, sys_errlist[errnum])
550 : sys_errlist[errnum]);
553 nl_catd catd = catopen ("perror", NL_CAT_LOCALE);
556 ? catgets (catd, 1, 1 + errnum, sys_errlist[errnum])
557 : sys_errlist[errnum]);
560 const char *errmsg = sys_errlist[errnum];
562 if (errmsg == NULL || *errmsg == '\0')
565 ret = safe_copy (buf, buflen, errmsg);
566 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
567 if (catd != (nl_catd)-1)
574 # elif defined __sgi || (defined __sun && !defined _LP64) /* IRIX, Solaris <= 9 32-bit */
576 /* For a valid error number, the system's strerror() function returns
577 a pointer to a not copied string, not to a buffer. */
578 if (errnum >= 0 && errnum < sys_nerr)
580 char *errmsg = strerror (errnum);
582 if (errmsg == NULL || *errmsg == '\0')
585 ret = safe_copy (buf, buflen, errmsg);
592 gl_lock_lock (strerror_lock);
595 char *errmsg = strerror (errnum);
597 /* For invalid error numbers, strerror() on
598 - IRIX 6.5 returns NULL,
599 - HP-UX 11 returns an empty string. */
600 if (errmsg == NULL || *errmsg == '\0')
603 ret = safe_copy (buf, buflen, errmsg);
606 gl_lock_unlock (strerror_lock);
612 if (ret == EINVAL && !*buf)
613 snprintf (buf, buflen, "Unknown error %d", errnum);