1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "apr_arch_misc.h"
18 #include "apr_strings.h"
30 * stuffbuffer - like apr_cpystrn() but returns the address of the
31 * dest buffer instead of the address of the terminating '\0'
33 static char *stuffbuffer(char *buf, apr_size_t bufsize, const char *s)
35 apr_cpystrn(buf,s,bufsize);
39 static char *apr_error_string(apr_status_t statcode)
43 return "Could not perform a stat on the file.";
45 return "A new pool could not be created.";
47 return "An invalid date has been provided";
49 return "An invalid socket was returned";
51 return "No process was provided and one was required.";
53 return "No time was provided and one was required.";
55 return "No directory was provided and one was required.";
57 return "No lock was provided and one was required.";
59 return "No poll structure was provided and one was required.";
61 return "No socket was provided and one was required.";
63 return "No thread was provided and one was required.";
65 return "No thread key structure was provided and one was required.";
67 return "No shared memory is currently available";
69 #if APR_HAS_DSO && defined(HAVE_LIBDL)
72 return "DSO load failed";
73 #endif /* HAVE_LIBDL */
75 return "The specified IP address is invalid.";
77 return "The specified network mask is invalid.";
78 case APR_ESYMNOTFOUND:
79 return "Could not find the requested symbol.";
80 case APR_ENOTENOUGHENTROPY:
81 return "Not enough entropy to continue.";
84 "Your code just forked, and you are currently executing in the "
88 "Your code just forked, and you are currently executing in the "
91 return "The specified thread is detached";
93 return "The specified thread is not detached";
95 return "The specified child process is done executing";
96 case APR_CHILD_NOTDONE:
97 return "The specified child process is not done executing";
99 return "The timeout specified has expired";
101 return "Partial results are valid but processing is incomplete";
103 return "Bad character specified on command line";
105 return "Missing parameter for the specified command line option";
107 return "End of file found";
109 return "Could not find specified socket in poll list.";
111 return "Shared memory is implemented anonymously";
113 return "Shared memory is implemented using files";
115 return "Shared memory is implemented using a key system";
118 "There is no error, this value signifies an initialized "
121 return "This function has not been implemented on this platform";
123 return "passwords do not match";
125 return "The given path is absolute";
127 return "The given path is relative";
128 case APR_EINCOMPLETE:
129 return "The given path is incomplete";
131 return "The given path was above the root path";
133 return "The given path is misformatted or contained invalid characters";
135 return "The given path contained wildcard characters";
137 return "The given lock was busy.";
138 case APR_EPROC_UNKNOWN:
139 return "The process is not recognized.";
141 return "Internal error (specific information not available)";
143 return "Error string not specified yet";
151 int apr_canonical_error(apr_status_t err);
153 static char *apr_os_strerror(char* buf, apr_size_t bufsize, int err)
156 unsigned char message[HUGE_STRING_LEN];
161 if (err >= 10000 && err < 12000) { /* socket error codes */
162 return stuffbuffer(buf, bufsize,
163 strerror(apr_canonical_error(err+APR_OS_START_SYSERR)));
165 else if (DosGetMessage(NULL, 0, message, HUGE_STRING_LEN, err,
166 "OSO001.MSG", &len) == 0) {
171 if (len >= sizeof(result))
172 len = sizeof(result) - 1;
174 for (c=0; c<len; c++) {
175 /* skip multiple whitespace */
176 while (apr_isspace(message[c]) && apr_isspace(message[c+1]))
178 *(pos++) = apr_isspace(message[c]) ? ' ' : message[c];
184 sprintf(result, "OS/2 error %d", err);
187 /* Stuff the string into the caller supplied buffer, then return
190 return stuffbuffer(buf, bufsize, result);
193 #elif defined(WIN32) || (defined(NETWARE) && defined(USE_WINSOCK))
195 static const struct {
199 {WSAEINTR, "Interrupted system call"},
200 {WSAEBADF, "Bad file number"},
201 {WSAEACCES, "Permission denied"},
202 {WSAEFAULT, "Bad address"},
203 {WSAEINVAL, "Invalid argument"},
204 {WSAEMFILE, "Too many open sockets"},
205 {WSAEWOULDBLOCK, "Operation would block"},
206 {WSAEINPROGRESS, "Operation now in progress"},
207 {WSAEALREADY, "Operation already in progress"},
208 {WSAENOTSOCK, "Socket operation on non-socket"},
209 {WSAEDESTADDRREQ, "Destination address required"},
210 {WSAEMSGSIZE, "Message too long"},
211 {WSAEPROTOTYPE, "Protocol wrong type for socket"},
212 {WSAENOPROTOOPT, "Bad protocol option"},
213 {WSAEPROTONOSUPPORT, "Protocol not supported"},
214 {WSAESOCKTNOSUPPORT, "Socket type not supported"},
215 {WSAEOPNOTSUPP, "Operation not supported on socket"},
216 {WSAEPFNOSUPPORT, "Protocol family not supported"},
217 {WSAEAFNOSUPPORT, "Address family not supported"},
218 {WSAEADDRINUSE, "Address already in use"},
219 {WSAEADDRNOTAVAIL, "Can't assign requested address"},
220 {WSAENETDOWN, "Network is down"},
221 {WSAENETUNREACH, "Network is unreachable"},
222 {WSAENETRESET, "Net connection reset"},
223 {WSAECONNABORTED, "Software caused connection abort"},
224 {WSAECONNRESET, "Connection reset by peer"},
225 {WSAENOBUFS, "No buffer space available"},
226 {WSAEISCONN, "Socket is already connected"},
227 {WSAENOTCONN, "Socket is not connected"},
228 {WSAESHUTDOWN, "Can't send after socket shutdown"},
229 {WSAETOOMANYREFS, "Too many references, can't splice"},
230 {WSAETIMEDOUT, "Connection timed out"},
231 {WSAECONNREFUSED, "Connection refused"},
232 {WSAELOOP, "Too many levels of symbolic links"},
233 {WSAENAMETOOLONG, "File name too long"},
234 {WSAEHOSTDOWN, "Host is down"},
235 {WSAEHOSTUNREACH, "No route to host"},
236 {WSAENOTEMPTY, "Directory not empty"},
237 {WSAEPROCLIM, "Too many processes"},
238 {WSAEUSERS, "Too many users"},
239 {WSAEDQUOT, "Disc quota exceeded"},
240 {WSAESTALE, "Stale NFS file handle"},
241 {WSAEREMOTE, "Too many levels of remote in path"},
242 {WSASYSNOTREADY, "Network system is unavailable"},
243 {WSAVERNOTSUPPORTED, "Winsock version out of range"},
244 {WSANOTINITIALISED, "WSAStartup not yet called"},
245 {WSAEDISCON, "Graceful shutdown in progress"},
246 {WSAHOST_NOT_FOUND, "Host not found"},
247 {WSANO_DATA, "No host data of that type was found"},
252 static char *apr_os_strerror(char *buf, apr_size_t bufsize, apr_status_t errcode)
258 len = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
259 | FORMAT_MESSAGE_IGNORE_INSERTS,
262 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
266 #else /* _WIN32_WCE speaks unicode */
267 LPTSTR msg = (LPTSTR) buf;
268 len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
269 | FORMAT_MESSAGE_IGNORE_INSERTS,
272 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
274 (DWORD) (bufsize/sizeof(TCHAR)),
276 /* in-place convert to US-ASCII, substituting '?' for non ASCII */
277 for(i = 0; i <= len; i++) {
278 if (msg[i] < 0x80 && msg[i] >= 0) {
279 buf[i] = (char) msg[i];
288 for (i = 0; gaErrorList[i].msg; ++i) {
289 if (gaErrorList[i].code == errcode) {
290 apr_cpystrn(buf, gaErrorList[i].msg, bufsize);
298 /* FormatMessage put the message in the buffer, but it may
299 * have embedded a newline (\r\n), and possible more than one.
300 * Remove the newlines replacing them with a space. This is not
301 * as visually perfect as moving all the remaining message over,
302 * but more efficient.
307 if ((buf[i] == '\r') || (buf[i] == '\n'))
312 /* Windows didn't provide us with a message. Even stuff like * WSAECONNREFUSED won't get a message.
314 apr_snprintf(buf, bufsize, "Unrecognized Win32 error code %d", errcode);
321 /* On Unix, apr_os_strerror() handles error codes from the resolver
324 static char *apr_os_strerror(char* buf, apr_size_t bufsize, int err)
326 #ifdef HAVE_HSTRERROR
327 return stuffbuffer(buf, bufsize, hstrerror(err));
328 #else /* HAVE_HSTRERROR */
333 msg = "Unknown host";
337 #if defined(NO_ADDRESS) && (NO_DATA != NO_ADDRESS)
340 msg = "No address for host";
342 #elif defined(NO_ADDRESS)
344 msg = "No address for host";
348 msg = "Unrecognized resolver error";
350 return stuffbuffer(buf, bufsize, msg);
351 #endif /* HAVE_STRERROR */
355 #if defined(HAVE_STRERROR_R) && defined(STRERROR_R_RC_INT) && !defined(BEOS)
356 /* AIX and Tru64 style */
357 static char *native_strerror(apr_status_t statcode, char *buf,
360 if (strerror_r(statcode, buf, bufsize) < 0) {
361 return stuffbuffer(buf, bufsize,
362 "APR does not understand this error code");
368 #elif defined(HAVE_STRERROR_R)
371 /* BeOS has the function available, but it doesn't provide
372 * the prototype publically (doh!), so to avoid a build warning
373 * we add a suitable prototype here.
376 const char *strerror_r(apr_status_t, char *, apr_size_t);
379 static char *native_strerror(apr_status_t statcode, char *buf,
385 msg = strerror_r(statcode, buf, bufsize);
386 if (buf[0] == '\0') { /* libc didn't use our buffer */
387 return stuffbuffer(buf, bufsize, msg);
394 /* plain old strerror();
395 * thread-safe on some platforms (e.g., Solaris, OS/390)
397 static char *native_strerror(apr_status_t statcode, char *buf,
402 sprintf(err, "Native Error #%d", statcode);
403 return stuffbuffer(buf, bufsize, err);
405 const char *err = strerror(statcode);
407 return stuffbuffer(buf, bufsize, err);
409 return stuffbuffer(buf, bufsize,
410 "APR does not understand this error code");
416 APR_DECLARE(char *) apr_strerror(apr_status_t statcode, char *buf,
419 if (statcode < APR_OS_START_ERROR) {
420 return native_strerror(statcode, buf, bufsize);
422 else if (statcode < APR_OS_START_USERERR) {
423 return stuffbuffer(buf, bufsize, apr_error_string(statcode));
425 else if (statcode < APR_OS_START_EAIERR) {
426 return stuffbuffer(buf, bufsize, "APR does not understand this error code");
428 else if (statcode < APR_OS_START_SYSERR) {
429 #if defined(HAVE_GAI_STRERROR)
430 statcode -= APR_OS_START_EAIERR;
431 #if defined(NEGATIVE_EAI)
432 statcode = -statcode;
434 return stuffbuffer(buf, bufsize, gai_strerror(statcode));
436 return stuffbuffer(buf, bufsize, "APR does not understand this error code");
440 return apr_os_strerror(buf, bufsize, statcode - APR_OS_START_SYSERR);