The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 28 | |
Elliott Hughes | eebf5fd | 2018-11-30 16:56:43 -0800 | [diff] [blame] | 29 | // G++ automatically defines _GNU_SOURCE, which then means that <string.h> |
| 30 | // gives us the GNU variant. |
| 31 | #undef _GNU_SOURCE |
| 32 | |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include <errno.h> |
| 36 | #include <limits.h> |
| 37 | |
| 38 | #include <async_safe/log.h> |
| 39 | |
| 40 | #include "private/ErrnoRestorer.h" |
| 41 | |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 42 | #include <string.h> |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 43 | |
| 44 | #include "bionic/pthread_internal.h" |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 45 | |
Steven Moreland | c3060de | 2021-09-22 11:06:57 -0700 | [diff] [blame^] | 46 | #define ERRNO_STRING_MAP_ENTRY(errno, string) [errno] = string " (" #errno ")" |
| 47 | |
Elliott Hughes | eebf5fd | 2018-11-30 16:56:43 -0800 | [diff] [blame] | 48 | static const char* __sys_error_strings[] = { |
| 49 | [0] = "Success", |
Steven Moreland | c3060de | 2021-09-22 11:06:57 -0700 | [diff] [blame^] | 50 | ERRNO_STRING_MAP_ENTRY(EPERM, "Operation not permitted"), |
| 51 | ERRNO_STRING_MAP_ENTRY(ENOENT, "No such file or directory"), |
| 52 | ERRNO_STRING_MAP_ENTRY(ESRCH, "No such process"), |
| 53 | ERRNO_STRING_MAP_ENTRY(EINTR, "Interrupted system call"), |
| 54 | ERRNO_STRING_MAP_ENTRY(EIO, "I/O error"), |
| 55 | ERRNO_STRING_MAP_ENTRY(ENXIO, "No such device or address"), |
| 56 | ERRNO_STRING_MAP_ENTRY(E2BIG, "Argument list too long"), |
| 57 | ERRNO_STRING_MAP_ENTRY(ENOEXEC, "Exec format error"), |
| 58 | ERRNO_STRING_MAP_ENTRY(EBADF, "Bad file descriptor"), |
| 59 | ERRNO_STRING_MAP_ENTRY(ECHILD, "No child processes"), |
| 60 | ERRNO_STRING_MAP_ENTRY(EAGAIN, "Try again"), |
| 61 | ERRNO_STRING_MAP_ENTRY(ENOMEM, "Out of memory"), |
| 62 | ERRNO_STRING_MAP_ENTRY(EACCES, "Permission denied"), |
| 63 | ERRNO_STRING_MAP_ENTRY(EFAULT, "Bad address"), |
| 64 | ERRNO_STRING_MAP_ENTRY(ENOTBLK, "Block device required"), |
| 65 | ERRNO_STRING_MAP_ENTRY(EBUSY, "Device or resource busy"), |
| 66 | ERRNO_STRING_MAP_ENTRY(EEXIST, "File exists"), |
| 67 | ERRNO_STRING_MAP_ENTRY(EXDEV, "Cross-device link"), |
| 68 | ERRNO_STRING_MAP_ENTRY(ENODEV, "No such device"), |
| 69 | ERRNO_STRING_MAP_ENTRY(ENOTDIR, "Not a directory"), |
| 70 | ERRNO_STRING_MAP_ENTRY(EISDIR, "Is a directory"), |
| 71 | ERRNO_STRING_MAP_ENTRY(EINVAL, "Invalid argument"), |
| 72 | ERRNO_STRING_MAP_ENTRY(ENFILE, "File table overflow"), |
| 73 | ERRNO_STRING_MAP_ENTRY(EMFILE, "Too many open files"), |
| 74 | ERRNO_STRING_MAP_ENTRY(ENOTTY, "Inappropriate ioctl for device"), |
| 75 | ERRNO_STRING_MAP_ENTRY(ETXTBSY, "Text file busy"), |
| 76 | ERRNO_STRING_MAP_ENTRY(EFBIG, "File too large"), |
| 77 | ERRNO_STRING_MAP_ENTRY(ENOSPC, "No space left on device"), |
| 78 | ERRNO_STRING_MAP_ENTRY(ESPIPE, "Illegal seek"), |
| 79 | ERRNO_STRING_MAP_ENTRY(EROFS, "Read-only file system"), |
| 80 | ERRNO_STRING_MAP_ENTRY(EMLINK, "Too many links"), |
| 81 | ERRNO_STRING_MAP_ENTRY(EPIPE, "Broken pipe"), |
| 82 | ERRNO_STRING_MAP_ENTRY(EDOM, "Math argument out of domain of func"), |
| 83 | ERRNO_STRING_MAP_ENTRY(ERANGE, "Math result not representable"), |
| 84 | ERRNO_STRING_MAP_ENTRY(EDEADLK, "Resource deadlock would occur"), |
| 85 | ERRNO_STRING_MAP_ENTRY(ENAMETOOLONG, "File name too long"), |
| 86 | ERRNO_STRING_MAP_ENTRY(ENOLCK, "No record locks available"), |
| 87 | ERRNO_STRING_MAP_ENTRY(ENOSYS, "Function not implemented"), |
| 88 | ERRNO_STRING_MAP_ENTRY(ENOTEMPTY, "Directory not empty"), |
| 89 | ERRNO_STRING_MAP_ENTRY(ELOOP, "Too many symbolic links encountered"), |
| 90 | ERRNO_STRING_MAP_ENTRY(ENOMSG, "No message of desired type"), |
| 91 | ERRNO_STRING_MAP_ENTRY(EIDRM, "Identifier removed"), |
| 92 | ERRNO_STRING_MAP_ENTRY(ECHRNG, "Channel number out of range"), |
| 93 | ERRNO_STRING_MAP_ENTRY(EL2NSYNC, "Level 2 not synchronized"), |
| 94 | ERRNO_STRING_MAP_ENTRY(EL3HLT, "Level 3 halted"), |
| 95 | ERRNO_STRING_MAP_ENTRY(EL3RST, "Level 3 reset"), |
| 96 | ERRNO_STRING_MAP_ENTRY(ELNRNG, "Link number out of range"), |
| 97 | ERRNO_STRING_MAP_ENTRY(EUNATCH, "Protocol driver not attached"), |
| 98 | ERRNO_STRING_MAP_ENTRY(ENOCSI, "No CSI structure available"), |
| 99 | ERRNO_STRING_MAP_ENTRY(EL2HLT, "Level 2 halted"), |
| 100 | ERRNO_STRING_MAP_ENTRY(EBADE, "Invalid exchange"), |
| 101 | ERRNO_STRING_MAP_ENTRY(EBADR, "Invalid request descriptor"), |
| 102 | ERRNO_STRING_MAP_ENTRY(EXFULL, "Exchange full"), |
| 103 | ERRNO_STRING_MAP_ENTRY(ENOANO, "No anode"), |
| 104 | ERRNO_STRING_MAP_ENTRY(EBADRQC, "Invalid request code"), |
| 105 | ERRNO_STRING_MAP_ENTRY(EBADSLT, "Invalid slot"), |
| 106 | ERRNO_STRING_MAP_ENTRY(EBFONT, "Bad font file format"), |
| 107 | ERRNO_STRING_MAP_ENTRY(ENOSTR, "Device not a stream"), |
| 108 | ERRNO_STRING_MAP_ENTRY(ENODATA, "No data available"), |
| 109 | ERRNO_STRING_MAP_ENTRY(ETIME, "Timer expired"), |
| 110 | ERRNO_STRING_MAP_ENTRY(ENOSR, "Out of streams resources"), |
| 111 | ERRNO_STRING_MAP_ENTRY(ENONET, "Machine is not on the network"), |
| 112 | ERRNO_STRING_MAP_ENTRY(ENOPKG, "Package not installed"), |
| 113 | ERRNO_STRING_MAP_ENTRY(EREMOTE, "Object is remote"), |
| 114 | ERRNO_STRING_MAP_ENTRY(ENOLINK, "Link has been severed"), |
| 115 | ERRNO_STRING_MAP_ENTRY(EADV, "Advertise error"), |
| 116 | ERRNO_STRING_MAP_ENTRY(ESRMNT, "Srmount error"), |
| 117 | ERRNO_STRING_MAP_ENTRY(ECOMM, "Communication error on send"), |
| 118 | ERRNO_STRING_MAP_ENTRY(EPROTO, "Protocol error"), |
| 119 | ERRNO_STRING_MAP_ENTRY(EMULTIHOP, "Multihop attempted"), |
| 120 | ERRNO_STRING_MAP_ENTRY(EDOTDOT, "RFS specific error"), |
| 121 | ERRNO_STRING_MAP_ENTRY(EBADMSG, "Not a data message"), |
| 122 | ERRNO_STRING_MAP_ENTRY(EOVERFLOW, "Value too large for defined data type"), |
| 123 | ERRNO_STRING_MAP_ENTRY(ENOTUNIQ, "Name not unique on network"), |
| 124 | ERRNO_STRING_MAP_ENTRY(EBADFD, "File descriptor in bad state"), |
| 125 | ERRNO_STRING_MAP_ENTRY(EREMCHG, "Remote address changed"), |
| 126 | ERRNO_STRING_MAP_ENTRY(ELIBACC, "Can not access a needed shared library"), |
| 127 | ERRNO_STRING_MAP_ENTRY(ELIBBAD, "Accessing a corrupted shared library"), |
| 128 | ERRNO_STRING_MAP_ENTRY(ELIBSCN, ".lib section in a.out corrupted"), |
| 129 | ERRNO_STRING_MAP_ENTRY(ELIBMAX, "Attempting to link in too many shared libraries"), |
| 130 | ERRNO_STRING_MAP_ENTRY(ELIBEXEC, "Cannot exec a shared library directly"), |
| 131 | ERRNO_STRING_MAP_ENTRY(EILSEQ, "Illegal byte sequence"), |
| 132 | ERRNO_STRING_MAP_ENTRY(ERESTART, "Interrupted system call should be restarted"), |
| 133 | ERRNO_STRING_MAP_ENTRY(ESTRPIPE, "Streams pipe error"), |
| 134 | ERRNO_STRING_MAP_ENTRY(EUSERS, "Too many users"), |
| 135 | ERRNO_STRING_MAP_ENTRY(ENOTSOCK, "Socket operation on non-socket"), |
| 136 | ERRNO_STRING_MAP_ENTRY(EDESTADDRREQ, "Destination address required"), |
| 137 | ERRNO_STRING_MAP_ENTRY(EMSGSIZE, "Message too long"), |
| 138 | ERRNO_STRING_MAP_ENTRY(EPROTOTYPE, "Protocol wrong type for socket"), |
| 139 | ERRNO_STRING_MAP_ENTRY(ENOPROTOOPT, "Protocol not available"), |
| 140 | ERRNO_STRING_MAP_ENTRY(EPROTONOSUPPORT, "Protocol not supported"), |
| 141 | ERRNO_STRING_MAP_ENTRY(ESOCKTNOSUPPORT, "Socket type not supported"), |
| 142 | ERRNO_STRING_MAP_ENTRY(EOPNOTSUPP, "Operation not supported on transport endpoint"), |
| 143 | ERRNO_STRING_MAP_ENTRY(EPFNOSUPPORT, "Protocol family not supported"), |
| 144 | ERRNO_STRING_MAP_ENTRY(EAFNOSUPPORT, "Address family not supported by protocol"), |
| 145 | ERRNO_STRING_MAP_ENTRY(EADDRINUSE, "Address already in use"), |
| 146 | ERRNO_STRING_MAP_ENTRY(EADDRNOTAVAIL, "Cannot assign requested address"), |
| 147 | ERRNO_STRING_MAP_ENTRY(ENETDOWN, "Network is down"), |
| 148 | ERRNO_STRING_MAP_ENTRY(ENETUNREACH, "Network is unreachable"), |
| 149 | ERRNO_STRING_MAP_ENTRY(ENETRESET, "Network dropped connection because of reset"), |
| 150 | ERRNO_STRING_MAP_ENTRY(ECONNABORTED, "Software caused connection abort"), |
| 151 | ERRNO_STRING_MAP_ENTRY(ECONNRESET, "Connection reset by peer"), |
| 152 | ERRNO_STRING_MAP_ENTRY(ENOBUFS, "No buffer space available"), |
| 153 | ERRNO_STRING_MAP_ENTRY(EISCONN, "Transport endpoint is already connected"), |
| 154 | ERRNO_STRING_MAP_ENTRY(ENOTCONN, "Transport endpoint is not connected"), |
| 155 | ERRNO_STRING_MAP_ENTRY(ESHUTDOWN, "Cannot send after transport endpoint shutdown"), |
| 156 | ERRNO_STRING_MAP_ENTRY(ETOOMANYREFS, "Too many references: cannot splice"), |
| 157 | ERRNO_STRING_MAP_ENTRY(ETIMEDOUT, "Connection timed out"), |
| 158 | ERRNO_STRING_MAP_ENTRY(ECONNREFUSED, "Connection refused"), |
| 159 | ERRNO_STRING_MAP_ENTRY(EHOSTDOWN, "Host is down"), |
| 160 | ERRNO_STRING_MAP_ENTRY(EHOSTUNREACH, "No route to host"), |
| 161 | ERRNO_STRING_MAP_ENTRY(EALREADY, "Operation already in progress"), |
| 162 | ERRNO_STRING_MAP_ENTRY(EINPROGRESS, "Operation now in progress"), |
| 163 | ERRNO_STRING_MAP_ENTRY(ESTALE, "Stale NFS file handle"), |
| 164 | ERRNO_STRING_MAP_ENTRY(EUCLEAN, "Structure needs cleaning"), |
| 165 | ERRNO_STRING_MAP_ENTRY(ENOTNAM, "Not a XENIX named type file"), |
| 166 | ERRNO_STRING_MAP_ENTRY(ENAVAIL, "No XENIX semaphores available"), |
| 167 | ERRNO_STRING_MAP_ENTRY(EISNAM, "Is a named type file"), |
| 168 | ERRNO_STRING_MAP_ENTRY(EREMOTEIO, "Remote I/O error"), |
| 169 | ERRNO_STRING_MAP_ENTRY(EDQUOT, "Quota exceeded"), |
| 170 | ERRNO_STRING_MAP_ENTRY(ENOMEDIUM, "No medium found"), |
| 171 | ERRNO_STRING_MAP_ENTRY(EMEDIUMTYPE, "Wrong medium type"), |
| 172 | ERRNO_STRING_MAP_ENTRY(ECANCELED, "Operation Canceled"), |
| 173 | ERRNO_STRING_MAP_ENTRY(ENOKEY, "Required key not available"), |
| 174 | ERRNO_STRING_MAP_ENTRY(EKEYEXPIRED, "Key has expired"), |
| 175 | ERRNO_STRING_MAP_ENTRY(EKEYREVOKED, "Key has been revoked"), |
| 176 | ERRNO_STRING_MAP_ENTRY(EKEYREJECTED, "Key was rejected by service"), |
| 177 | ERRNO_STRING_MAP_ENTRY(EOWNERDEAD, "Owner died"), |
| 178 | ERRNO_STRING_MAP_ENTRY(ENOTRECOVERABLE, "State not recoverable"), |
| 179 | ERRNO_STRING_MAP_ENTRY(ERFKILL, "Operation not possible due to RF-kill"), |
| 180 | ERRNO_STRING_MAP_ENTRY(EHWPOISON, "Memory page has hardware error"), |
Elliott Hughes | eebf5fd | 2018-11-30 16:56:43 -0800 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | static inline const char* __strerror_lookup(int error_number) { |
| 184 | if (error_number < 0 || error_number >= static_cast<int>(arraysize(__sys_error_strings))) { |
| 185 | return nullptr; |
| 186 | } |
| 187 | return __sys_error_strings[error_number]; |
| 188 | } |
| 189 | |
| 190 | int strerror_r(int error_number, char* buf, size_t buf_len) { |
| 191 | ErrnoRestorer errno_restorer; |
| 192 | size_t length; |
| 193 | |
| 194 | const char* error_name = __strerror_lookup(error_number); |
| 195 | if (error_name != nullptr) { |
| 196 | length = strlcpy(buf, error_name, buf_len); |
| 197 | } else { |
| 198 | length = async_safe_format_buffer(buf, buf_len, "Unknown error %d", error_number); |
| 199 | } |
| 200 | if (length >= buf_len) { |
Colin Cross | 695af0d | 2021-07-30 09:36:57 -0700 | [diff] [blame] | 201 | return ERANGE; |
Elliott Hughes | eebf5fd | 2018-11-30 16:56:43 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | extern "C" char* __gnu_strerror_r(int error_number, char* buf, size_t buf_len) { |
| 208 | ErrnoRestorer errno_restorer; // The glibc strerror_r doesn't set errno if it truncates... |
| 209 | strerror_r(error_number, buf, buf_len); |
| 210 | return buf; // ...and just returns whatever fit. |
| 211 | } |
Elliott Hughes | 4198fa4 | 2012-09-17 15:23:35 -0700 | [diff] [blame] | 212 | |
Irina Tirdea | b5f053b | 2012-09-08 09:17:54 +0300 | [diff] [blame] | 213 | char* strerror(int error_number) { |
Elliott Hughes | 4198fa4 | 2012-09-17 15:23:35 -0700 | [diff] [blame] | 214 | // Just return the original constant in the easy cases. |
| 215 | char* result = const_cast<char*>(__strerror_lookup(error_number)); |
Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 216 | if (result != nullptr) { |
Elliott Hughes | 4198fa4 | 2012-09-17 15:23:35 -0700 | [diff] [blame] | 217 | return result; |
| 218 | } |
| 219 | |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 220 | bionic_tls& tls = __get_bionic_tls(); |
| 221 | result = tls.strerror_buf; |
| 222 | strerror_r(error_number, result, sizeof(tls.strerror_buf)); |
Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 223 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 224 | } |