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
32 #if GNULIB_defined_ESOCK /* native Windows platforms */
34 # include <winsock2.h>
38 /* Reasonable buffer size that should never trigger ERANGE; if this
39 proves too small, we intentionally abort(), to remind us to fix
40 this value as well as strerror-impl.h. */
41 #define STACKBUF_LEN 256
43 #if (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__) && HAVE___XPG_STRERROR_R /* glibc >= 2.3.4, cygwin >= 1.7.9 */
45 # define USE_XPG_STRERROR_R 1
47 #elif HAVE_DECL_STRERROR_R && !(__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__)
49 /* The system's strerror_r function is OK, except that its third argument
50 is 'int', not 'size_t', or its return type is wrong. */
54 # define USE_SYSTEM_STRERROR_R 1
56 #else /* (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__ ? !HAVE___XPG_STRERROR_R : !HAVE_DECL_STRERROR_R) */
58 /* Use the system's strerror(). Exclude glibc and cygwin because the
59 system strerror_r has the wrong return type, and cygwin 1.7.9
60 strerror_r clobbers strerror. */
63 # define USE_SYSTEM_STRERROR 1
65 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __sgi || (defined __sun && !defined _LP64) || defined __CYGWIN__
67 /* No locking needed. */
69 /* Get catgets internationalization functions. */
71 # include <nl_types.h>
74 /* Get sys_nerr, sys_errlist on HP-UX (otherwise only declared in C++ mode).
75 Get sys_nerr, sys_errlist on IRIX (otherwise only declared with _SGIAPI). */
76 # if defined __hpux || defined __sgi
78 extern char *sys_errlist[];
81 /* Get sys_nerr on Solaris. */
82 # if defined __sun && !defined _LP64
88 # include "glthread/lock.h"
90 /* This lock protects the buffer returned by strerror(). We assume that
91 no other uses of strerror() exist in the program. */
92 gl_lock_define_initialized(static, strerror_lock)
98 /* Copy as much of MSG into BUF as possible, without corrupting errno.
99 Return 0 if MSG fit in BUFLEN, otherwise return ERANGE. */
101 safe_copy (char *buf, size_t buflen, const char *msg)
103 size_t len = strlen (msg);
108 /* Although POSIX allows memcpy() to corrupt errno, we don't
109 know of any implementation where this is a real problem. */
110 memcpy (buf, msg, len + 1);
115 memcpy (buf, msg, buflen - 1);
116 buf[buflen - 1] = '\0';
124 strerror_r (int errnum, char *buf, size_t buflen)
127 /* Filter this out now, so that rest of this replacement knows that
128 there is room for a non-empty message and trailing NUL. */
137 #if GNULIB_defined_ETXTBSY \
138 || GNULIB_defined_ESOCK \
139 || GNULIB_defined_ENOMSG \
140 || GNULIB_defined_EIDRM \
141 || GNULIB_defined_ENOLINK \
142 || GNULIB_defined_EPROTO \
143 || GNULIB_defined_EMULTIHOP \
144 || GNULIB_defined_EBADMSG \
145 || GNULIB_defined_EOVERFLOW \
146 || GNULIB_defined_ENOTSUP \
147 || GNULIB_defined_ESTALE \
148 || GNULIB_defined_EDQUOT \
149 || GNULIB_defined_ECANCELED
151 char const *msg = NULL;
152 /* These error messages are taken from glibc/sysdeps/gnu/errlist.c. */
155 # if GNULIB_defined_ETXTBSY
157 msg = "Text file busy";
161 # if GNULIB_defined_ESOCK /* native Windows platforms */
162 /* EWOULDBLOCK is the same as EAGAIN. */
164 msg = "Operation now in progress";
167 msg = "Operation already in progress";
170 msg = "Socket operation on non-socket";
173 msg = "Destination address required";
176 msg = "Message too long";
179 msg = "Protocol wrong type for socket";
182 msg = "Protocol not available";
184 case EPROTONOSUPPORT:
185 msg = "Protocol not supported";
187 case ESOCKTNOSUPPORT:
188 msg = "Socket type not supported";
191 msg = "Operation not supported";
194 msg = "Protocol family not supported";
197 msg = "Address family not supported by protocol";
200 msg = "Address already in use";
203 msg = "Cannot assign requested address";
206 msg = "Network is down";
209 msg = "Network is unreachable";
212 msg = "Network dropped connection on reset";
215 msg = "Software caused connection abort";
218 msg = "Connection reset by peer";
221 msg = "No buffer space available";
224 msg = "Transport endpoint is already connected";
227 msg = "Transport endpoint is not connected";
230 msg = "Cannot send after transport endpoint shutdown";
233 msg = "Too many references: cannot splice";
236 msg = "Connection timed out";
239 msg = "Connection refused";
242 msg = "Too many levels of symbolic links";
245 msg = "Host is down";
248 msg = "No route to host";
251 msg = "Too many processes";
254 msg = "Too many users";
257 msg = "Disk quota exceeded";
260 msg = "Stale NFS file handle";
263 msg = "Object is remote";
266 /* WSA_INVALID_HANDLE maps to EBADF */
267 /* WSA_NOT_ENOUGH_MEMORY maps to ENOMEM */
268 /* WSA_INVALID_PARAMETER maps to EINVAL */
269 case WSA_OPERATION_ABORTED:
270 msg = "Overlapped operation aborted";
272 case WSA_IO_INCOMPLETE:
273 msg = "Overlapped I/O event object not in signaled state";
276 msg = "Overlapped operations will complete later";
278 /* WSAEINTR maps to EINTR */
279 /* WSAEBADF maps to EBADF */
280 /* WSAEACCES maps to EACCES */
281 /* WSAEFAULT maps to EFAULT */
282 /* WSAEINVAL maps to EINVAL */
283 /* WSAEMFILE maps to EMFILE */
284 /* WSAEWOULDBLOCK maps to EWOULDBLOCK */
285 /* WSAEINPROGRESS is EINPROGRESS */
286 /* WSAEALREADY is EALREADY */
287 /* WSAENOTSOCK is ENOTSOCK */
288 /* WSAEDESTADDRREQ is EDESTADDRREQ */
289 /* WSAEMSGSIZE is EMSGSIZE */
290 /* WSAEPROTOTYPE is EPROTOTYPE */
291 /* WSAENOPROTOOPT is ENOPROTOOPT */
292 /* WSAEPROTONOSUPPORT is EPROTONOSUPPORT */
293 /* WSAESOCKTNOSUPPORT is ESOCKTNOSUPPORT */
294 /* WSAEOPNOTSUPP is EOPNOTSUPP */
295 /* WSAEPFNOSUPPORT is EPFNOSUPPORT */
296 /* WSAEAFNOSUPPORT is EAFNOSUPPORT */
297 /* WSAEADDRINUSE is EADDRINUSE */
298 /* WSAEADDRNOTAVAIL is EADDRNOTAVAIL */
299 /* WSAENETDOWN is ENETDOWN */
300 /* WSAENETUNREACH is ENETUNREACH */
301 /* WSAENETRESET is ENETRESET */
302 /* WSAECONNABORTED is ECONNABORTED */
303 /* WSAECONNRESET is ECONNRESET */
304 /* WSAENOBUFS is ENOBUFS */
305 /* WSAEISCONN is EISCONN */
306 /* WSAENOTCONN is ENOTCONN */
307 /* WSAESHUTDOWN is ESHUTDOWN */
308 /* WSAETOOMANYREFS is ETOOMANYREFS */
309 /* WSAETIMEDOUT is ETIMEDOUT */
310 /* WSAECONNREFUSED is ECONNREFUSED */
311 /* WSAELOOP is ELOOP */
312 /* WSAENAMETOOLONG maps to ENAMETOOLONG */
313 /* WSAEHOSTDOWN is EHOSTDOWN */
314 /* WSAEHOSTUNREACH is EHOSTUNREACH */
315 /* WSAENOTEMPTY maps to ENOTEMPTY */
316 /* WSAEPROCLIM is EPROCLIM */
317 /* WSAEUSERS is EUSERS */
318 /* WSAEDQUOT is EDQUOT */
319 /* WSAESTALE is ESTALE */
320 /* WSAEREMOTE is EREMOTE */
322 msg = "Network subsystem is unavailable";
324 case WSAVERNOTSUPPORTED:
325 msg = "Winsock.dll version out of range";
327 case WSANOTINITIALISED:
328 msg = "Successful WSAStartup not yet performed";
331 msg = "Graceful shutdown in progress";
333 case WSAENOMORE: case WSA_E_NO_MORE:
334 msg = "No more results";
336 case WSAECANCELLED: case WSA_E_CANCELLED:
337 msg = "Call was canceled";
339 case WSAEINVALIDPROCTABLE:
340 msg = "Procedure call table is invalid";
342 case WSAEINVALIDPROVIDER:
343 msg = "Service provider is invalid";
345 case WSAEPROVIDERFAILEDINIT:
346 msg = "Service provider failed to initialize";
348 case WSASYSCALLFAILURE:
349 msg = "System call failure";
351 case WSASERVICE_NOT_FOUND:
352 msg = "Service not found";
354 case WSATYPE_NOT_FOUND:
355 msg = "Class type not found";
358 msg = "Database query was refused";
360 case WSAHOST_NOT_FOUND:
361 msg = "Host not found";
364 msg = "Nonauthoritative host not found";
367 msg = "Nonrecoverable error";
370 msg = "Valid name, no data record of requested type";
372 /* WSA_QOS_* omitted */
376 # if GNULIB_defined_ENOMSG
378 msg = "No message of desired type";
382 # if GNULIB_defined_EIDRM
384 msg = "Identifier removed";
388 # if GNULIB_defined_ENOLINK
390 msg = "Link has been severed";
394 # if GNULIB_defined_EPROTO
396 msg = "Protocol error";
400 # if GNULIB_defined_EMULTIHOP
402 msg = "Multihop attempted";
406 # if GNULIB_defined_EBADMSG
412 # if GNULIB_defined_EOVERFLOW
414 msg = "Value too large for defined data type";
418 # if GNULIB_defined_ENOTSUP
420 msg = "Not supported";
424 # if GNULIB_defined_ESTALE
426 msg = "Stale NFS file handle";
430 # if GNULIB_defined_EDQUOT
432 msg = "Disk quota exceeded";
436 # if GNULIB_defined_ECANCELED
438 msg = "Operation canceled";
444 return safe_copy (buf, buflen, msg);
450 int saved_errno = errno;
452 #if USE_XPG_STRERROR_R
455 extern int __xpg_strerror_r (int errnum, char *buf, size_t buflen);
457 ret = __xpg_strerror_r (errnum, buf, buflen);
462 /* glibc 2.13 would not touch buf on err, so we have to fall
463 back to GNU strerror_r which always returns a thread-safe
464 untruncated string to (partially) copy into our buf. */
465 safe_copy (buf, buflen, strerror_r (errnum, buf, buflen));
469 #elif USE_SYSTEM_STRERROR_R
471 if (buflen > INT_MAX)
475 /* On HP-UX 11.31, strerror_r always fails when buflen < 80; it
476 also fails to change buf on EINVAL. */
480 if (buflen < sizeof stackbuf)
482 ret = strerror_r (errnum, stackbuf, sizeof stackbuf);
484 ret = safe_copy (buf, buflen, stackbuf);
487 ret = strerror_r (errnum, buf, buflen);
490 /* Solaris 10 does not populate buf on ERANGE. */
491 ret = strerror_r (errnum, buf, buflen);
492 if (ret == ERANGE && !*buf)
494 char stackbuf[STACKBUF_LEN];
496 /* strerror-impl.h is also affected if our choice of stackbuf
497 size is not large enough. */
498 if (strerror_r (errnum, stackbuf, sizeof stackbuf) == ERANGE)
500 safe_copy (buf, buflen, stackbuf);
505 /* AIX returns 0 rather than ERANGE when truncating strings; try
506 again until we are sure we got the entire string. */
507 if (!ret && strlen (buf) == buflen - 1)
509 char stackbuf[STACKBUF_LEN];
511 strerror_r (errnum, stackbuf, sizeof stackbuf);
512 len = strlen (stackbuf);
513 /* stackbuf should have been large enough. */
514 if (len + 1 == sizeof stackbuf)
521 /* Some old implementations may return (-1, EINVAL) instead of EINVAL. */
525 /* FreeBSD rejects 0; see http://austingroupbugs.net/view.php?id=382. */
526 if (errnum == 0 && ret == EINVAL)
527 ret = safe_copy (buf, buflen, "Success");
529 #else /* USE_SYSTEM_STRERROR */
531 /* Try to do what strerror (errnum) does, but without clobbering the
532 buffer used by strerror(). */
534 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __CYGWIN__ /* NetBSD, HP-UX, native Win32, Cygwin */
536 /* NetBSD: sys_nerr, sys_errlist are declared through _NETBSD_SOURCE
538 HP-UX: sys_nerr, sys_errlist are declared explicitly above.
539 native Win32: sys_nerr, sys_errlist are declared in <stdlib.h>.
540 Cygwin: sys_nerr, sys_errlist are declared in <errno.h>. */
541 if (errnum >= 0 && errnum < sys_nerr)
543 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
544 # if defined __NetBSD__
545 nl_catd catd = catopen ("libc", NL_CAT_LOCALE);
548 ? catgets (catd, 1, errnum, sys_errlist[errnum])
549 : sys_errlist[errnum]);
552 nl_catd catd = catopen ("perror", NL_CAT_LOCALE);
555 ? catgets (catd, 1, 1 + errnum, sys_errlist[errnum])
556 : sys_errlist[errnum]);
559 const char *errmsg = sys_errlist[errnum];
561 if (errmsg == NULL || *errmsg == '\0')
564 ret = safe_copy (buf, buflen, errmsg);
565 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
566 if (catd != (nl_catd)-1)
573 # elif defined __sgi || (defined __sun && !defined _LP64) /* IRIX, Solaris <= 9 32-bit */
575 /* For a valid error number, the system's strerror() function returns
576 a pointer to a not copied string, not to a buffer. */
577 if (errnum >= 0 && errnum < sys_nerr)
579 char *errmsg = strerror (errnum);
581 if (errmsg == NULL || *errmsg == '\0')
584 ret = safe_copy (buf, buflen, errmsg);
591 gl_lock_lock (strerror_lock);
594 char *errmsg = strerror (errnum);
596 /* For invalid error numbers, strerror() on
597 - IRIX 6.5 returns NULL,
598 - HP-UX 11 returns an empty string. */
599 if (errmsg == NULL || *errmsg == '\0')
602 ret = safe_copy (buf, buflen, errmsg);
605 gl_lock_unlock (strerror_lock);
611 if (ret == EINVAL && !*buf)
612 snprintf (buf, buflen, "Unknown error %d", errnum);