errorcodes.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. #include "fspr_arch_misc.h"
  17. #include "fspr_strings.h"
  18. #include "fspr_lib.h"
  19. #include "fspr_dso.h"
  20. #if APR_HAVE_NETDB_H
  21. #include <netdb.h>
  22. #endif
  23. #ifdef HAVE_DLFCN_H
  24. #include <dlfcn.h>
  25. #endif
  26. /*
  27. * stuffbuffer - like fspr_cpystrn() but returns the address of the
  28. * dest buffer instead of the address of the terminating '\0'
  29. */
  30. static char *stuffbuffer(char *buf, fspr_size_t bufsize, const char *s)
  31. {
  32. fspr_cpystrn(buf,s,bufsize);
  33. return buf;
  34. }
  35. static char *fspr_error_string(fspr_status_t statcode)
  36. {
  37. switch (statcode) {
  38. case APR_ENOPOOL:
  39. return "A new pool could not be created.";
  40. case APR_EBADDATE:
  41. return "An invalid date has been provided";
  42. case APR_EINVALSOCK:
  43. return "An invalid socket was returned";
  44. case APR_ENOPROC:
  45. return "No process was provided and one was required.";
  46. case APR_ENOTIME:
  47. return "No time was provided and one was required.";
  48. case APR_ENODIR:
  49. return "No directory was provided and one was required.";
  50. case APR_ENOLOCK:
  51. return "No lock was provided and one was required.";
  52. case APR_ENOPOLL:
  53. return "No poll structure was provided and one was required.";
  54. case APR_ENOSOCKET:
  55. return "No socket was provided and one was required.";
  56. case APR_ENOTHREAD:
  57. return "No thread was provided and one was required.";
  58. case APR_ENOTHDKEY:
  59. return "No thread key structure was provided and one was required.";
  60. case APR_ENOSHMAVAIL:
  61. return "No shared memory is currently available";
  62. case APR_EDSOOPEN:
  63. #if APR_HAS_DSO && defined(HAVE_LIBDL)
  64. return dlerror();
  65. #else
  66. return "DSO load failed";
  67. #endif /* HAVE_LIBDL */
  68. case APR_EBADIP:
  69. return "The specified IP address is invalid.";
  70. case APR_EBADMASK:
  71. return "The specified network mask is invalid.";
  72. case APR_INCHILD:
  73. return
  74. "Your code just forked, and you are currently executing in the "
  75. "child process";
  76. case APR_INPARENT:
  77. return
  78. "Your code just forked, and you are currently executing in the "
  79. "parent process";
  80. case APR_DETACH:
  81. return "The specified thread is detached";
  82. case APR_NOTDETACH:
  83. return "The specified thread is not detached";
  84. case APR_CHILD_DONE:
  85. return "The specified child process is done executing";
  86. case APR_CHILD_NOTDONE:
  87. return "The specified child process is not done executing";
  88. case APR_TIMEUP:
  89. return "The timeout specified has expired";
  90. case APR_INCOMPLETE:
  91. return "Partial results are valid but processing is incomplete";
  92. case APR_BADCH:
  93. return "Bad character specified on command line";
  94. case APR_BADARG:
  95. return "Missing parameter for the specified command line option";
  96. case APR_EOF:
  97. return "End of file found";
  98. case APR_NOTFOUND:
  99. return "Could not find specified socket in poll list.";
  100. case APR_ANONYMOUS:
  101. return "Shared memory is implemented anonymously";
  102. case APR_FILEBASED:
  103. return "Shared memory is implemented using files";
  104. case APR_KEYBASED:
  105. return "Shared memory is implemented using a key system";
  106. case APR_EINIT:
  107. return
  108. "There is no error, this value signifies an initialized "
  109. "error code";
  110. case APR_ENOTIMPL:
  111. return "This function has not been implemented on this platform";
  112. case APR_EMISMATCH:
  113. return "passwords do not match";
  114. case APR_EABSOLUTE:
  115. return "The given path is absolute";
  116. case APR_ERELATIVE:
  117. return "The given path is relative";
  118. case APR_EINCOMPLETE:
  119. return "The given path is incomplete";
  120. case APR_EABOVEROOT:
  121. return "The given path was above the root path";
  122. case APR_EBADPATH:
  123. return "The given path misformatted or contained invalid characters";
  124. case APR_EPATHWILD:
  125. return "The given path contained wildcard characters";
  126. case APR_EPROC_UNKNOWN:
  127. return "The process is not recognized.";
  128. case APR_EGENERAL:
  129. return "Internal error";
  130. default:
  131. return "Error string not specified yet";
  132. }
  133. }
  134. #ifdef OS2
  135. #include <ctype.h>
  136. int fspr_canonical_error(fspr_status_t err);
  137. static char *fspr_os_strerror(char* buf, fspr_size_t bufsize, int err)
  138. {
  139. char result[200];
  140. unsigned char message[HUGE_STRING_LEN];
  141. ULONG len;
  142. char *pos;
  143. int c;
  144. if (err >= 10000 && err < 12000) { /* socket error codes */
  145. return stuffbuffer(buf, bufsize,
  146. strerror(fspr_canonical_error(err+APR_OS_START_SYSERR)));
  147. }
  148. else if (DosGetMessage(NULL, 0, message, HUGE_STRING_LEN, err,
  149. "OSO001.MSG", &len) == 0) {
  150. len--;
  151. message[len] = 0;
  152. pos = result;
  153. if (len >= sizeof(result))
  154. len = sizeof(result-1);
  155. for (c=0; c<len; c++) {
  156. /* skip multiple whitespace */
  157. while (fspr_isspace(message[c]) && fspr_isspace(message[c+1]))
  158. c++;
  159. *(pos++) = fspr_isspace(message[c]) ? ' ' : message[c];
  160. }
  161. *pos = 0;
  162. }
  163. else {
  164. sprintf(result, "OS/2 error %d", err);
  165. }
  166. /* Stuff the string into the caller supplied buffer, then return
  167. * a pointer to it.
  168. */
  169. return stuffbuffer(buf, bufsize, result);
  170. }
  171. #elif defined(WIN32) || (defined(NETWARE) && defined(USE_WINSOCK))
  172. static const struct {
  173. fspr_status_t code;
  174. const char *msg;
  175. } gaErrorList[] = {
  176. WSAEINTR, "Interrupted system call",
  177. WSAEBADF, "Bad file number",
  178. WSAEACCES, "Permission denied",
  179. WSAEFAULT, "Bad address",
  180. WSAEINVAL, "Invalid argument",
  181. WSAEMFILE, "Too many open sockets",
  182. WSAEWOULDBLOCK, "Operation would block",
  183. WSAEINPROGRESS, "Operation now in progress",
  184. WSAEALREADY, "Operation already in progress",
  185. WSAENOTSOCK, "Socket operation on non-socket",
  186. WSAEDESTADDRREQ, "Destination address required",
  187. WSAEMSGSIZE, "Message too long",
  188. WSAEPROTOTYPE, "Protocol wrong type for socket",
  189. WSAENOPROTOOPT, "Bad protocol option",
  190. WSAEPROTONOSUPPORT, "Protocol not supported",
  191. WSAESOCKTNOSUPPORT, "Socket type not supported",
  192. WSAEOPNOTSUPP, "Operation not supported on socket",
  193. WSAEPFNOSUPPORT, "Protocol family not supported",
  194. WSAEAFNOSUPPORT, "Address family not supported",
  195. WSAEADDRINUSE, "Address already in use",
  196. WSAEADDRNOTAVAIL, "Can't assign requested address",
  197. WSAENETDOWN, "Network is down",
  198. WSAENETUNREACH, "Network is unreachable",
  199. WSAENETRESET, "Net connection reset",
  200. WSAECONNABORTED, "Software caused connection abort",
  201. WSAECONNRESET, "Connection reset by peer",
  202. WSAENOBUFS, "No buffer space available",
  203. WSAEISCONN, "Socket is already connected",
  204. WSAENOTCONN, "Socket is not connected",
  205. WSAESHUTDOWN, "Can't send after socket shutdown",
  206. WSAETOOMANYREFS, "Too many references, can't splice",
  207. WSAETIMEDOUT, "Connection timed out",
  208. WSAECONNREFUSED, "Connection refused",
  209. WSAELOOP, "Too many levels of symbolic links",
  210. WSAENAMETOOLONG, "File name too long",
  211. WSAEHOSTDOWN, "Host is down",
  212. WSAEHOSTUNREACH, "No route to host",
  213. WSAENOTEMPTY, "Directory not empty",
  214. WSAEPROCLIM, "Too many processes",
  215. WSAEUSERS, "Too many users",
  216. WSAEDQUOT, "Disc quota exceeded",
  217. WSAESTALE, "Stale NFS file handle",
  218. WSAEREMOTE, "Too many levels of remote in path",
  219. WSASYSNOTREADY, "Network system is unavailable",
  220. WSAVERNOTSUPPORTED, "Winsock version out of range",
  221. WSANOTINITIALISED, "WSAStartup not yet called",
  222. WSAEDISCON, "Graceful shutdown in progress",
  223. WSAHOST_NOT_FOUND, "Host not found",
  224. WSANO_DATA, "No host data of that type was found",
  225. 0, NULL
  226. };
  227. static char *fspr_os_strerror(char *buf, fspr_size_t bufsize, fspr_status_t errcode)
  228. {
  229. fspr_size_t len=0, i;
  230. #ifndef NETWARE
  231. len = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
  232. | FORMAT_MESSAGE_IGNORE_INSERTS,
  233. NULL,
  234. errcode,
  235. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
  236. (LPTSTR) buf,
  237. (DWORD)bufsize,
  238. NULL);
  239. #endif
  240. if (!len) {
  241. for (i = 0; gaErrorList[i].msg; ++i) {
  242. if (gaErrorList[i].code == errcode) {
  243. fspr_cpystrn(buf, gaErrorList[i].msg, bufsize);
  244. len = strlen(buf);
  245. break;
  246. }
  247. }
  248. }
  249. if (len) {
  250. /* FormatMessage put the message in the buffer, but it may
  251. * have embedded a newline (\r\n), and possible more than one.
  252. * Remove the newlines replacing them with a space. This is not
  253. * as visually perfect as moving all the remaining message over,
  254. * but more efficient.
  255. */
  256. i = len;
  257. while (i) {
  258. i--;
  259. if ((buf[i] == '\r') || (buf[i] == '\n'))
  260. buf[i] = ' ';
  261. }
  262. }
  263. else {
  264. /* Windows didn't provide us with a message. Even stuff like * WSAECONNREFUSED won't get a message.
  265. */
  266. fspr_snprintf(buf, bufsize, "Unrecognized Win32 error code %d", errcode);
  267. }
  268. return buf;
  269. }
  270. #else
  271. /* On Unix, fspr_os_strerror() handles error codes from the resolver
  272. * (h_errno).
  273. */
  274. static char *fspr_os_strerror(char* buf, fspr_size_t bufsize, int err)
  275. {
  276. #ifdef HAVE_HSTRERROR
  277. return stuffbuffer(buf, bufsize, hstrerror(err));
  278. #else /* HAVE_HSTRERROR */
  279. const char *msg;
  280. switch(err) {
  281. case HOST_NOT_FOUND:
  282. msg = "Unknown host";
  283. break;
  284. #if defined(NO_DATA)
  285. case NO_DATA:
  286. #if defined(NO_ADDRESS) && (NO_DATA != NO_ADDRESS)
  287. case NO_ADDRESS:
  288. #endif
  289. msg = "No address for host";
  290. break;
  291. #elif defined(NO_ADDRESS)
  292. case NO_ADDRESS:
  293. msg = "No address for host";
  294. break;
  295. #endif /* NO_DATA */
  296. default:
  297. msg = "Unrecognized resolver error";
  298. }
  299. return stuffbuffer(buf, bufsize, msg);
  300. #endif /* HAVE_STRERROR */
  301. }
  302. #endif
  303. #if defined(HAVE_STRERROR_R) && defined(STRERROR_R_RC_INT) && !defined(BEOS)
  304. /* AIX and Tru64 style */
  305. static char *native_strerror(fspr_status_t statcode, char *buf,
  306. fspr_size_t bufsize)
  307. {
  308. if (strerror_r(statcode, buf, bufsize) < 0) {
  309. return stuffbuffer(buf, bufsize,
  310. "APR does not understand this error code");
  311. }
  312. else {
  313. return buf;
  314. }
  315. }
  316. #elif defined(HAVE_STRERROR_R)
  317. /* glibc style */
  318. /* BeOS has the function available, but it doesn't provide
  319. * the prototype publically (doh!), so to avoid a build warning
  320. * we add a suitable prototype here.
  321. */
  322. #if defined(BEOS)
  323. const char *strerror_r(fspr_status_t, char *, fspr_size_t);
  324. #endif
  325. static char *native_strerror(fspr_status_t statcode, char *buf,
  326. fspr_size_t bufsize)
  327. {
  328. const char *msg;
  329. buf[0] = '\0';
  330. msg = strerror_r(statcode, buf, bufsize);
  331. if (buf[0] == '\0') { /* libc didn't use our buffer */
  332. return stuffbuffer(buf, bufsize, msg);
  333. }
  334. else {
  335. return buf;
  336. }
  337. }
  338. #else
  339. /* plain old strerror();
  340. * thread-safe on some platforms (e.g., Solaris, OS/390)
  341. */
  342. static char *native_strerror(fspr_status_t statcode, char *buf,
  343. fspr_size_t bufsize)
  344. {
  345. #ifdef _WIN32_WCE
  346. static char err[32];
  347. sprintf(err, "Native Error #%d", statcode);
  348. return stuffbuffer(buf, bufsize, err);
  349. #else
  350. const char *err = strerror(statcode);
  351. if (err) {
  352. return stuffbuffer(buf, bufsize, err);
  353. } else {
  354. return stuffbuffer(buf, bufsize,
  355. "APR does not understand this error code");
  356. }
  357. #endif
  358. }
  359. #endif
  360. APR_DECLARE(char *) fspr_strerror(fspr_status_t statcode, char *buf,
  361. fspr_size_t bufsize)
  362. {
  363. if (statcode < APR_OS_START_ERROR) {
  364. return native_strerror(statcode, buf, bufsize);
  365. }
  366. else if (statcode < APR_OS_START_USERERR) {
  367. return stuffbuffer(buf, bufsize, fspr_error_string(statcode));
  368. }
  369. else if (statcode < APR_OS_START_EAIERR) {
  370. return stuffbuffer(buf, bufsize, "APR does not understand this error code");
  371. }
  372. else if (statcode < APR_OS_START_SYSERR) {
  373. #if defined(HAVE_GAI_STRERROR)
  374. statcode -= APR_OS_START_EAIERR;
  375. #if defined(NEGATIVE_EAI)
  376. statcode = -statcode;
  377. #endif
  378. return stuffbuffer(buf, bufsize, gai_strerror(statcode));
  379. #else
  380. return stuffbuffer(buf, bufsize, "APR does not understand this error code");
  381. #endif
  382. }
  383. else {
  384. return fspr_os_strerror(buf, bufsize, statcode - APR_OS_START_SYSERR);
  385. }
  386. }