/security/nss/lib/ssl/unix_err.c

http://github.com/zpao/v8monkey · C · 550 lines · 440 code · 53 blank · 57 comment · 39 complexity · 44a7b8be1e440e7d7dfd60170ac02821 MD5 · raw file

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3. * This file essentially replicates NSPR's source for the functions that
  4. * map system-specific error codes to NSPR error codes. We would use
  5. * NSPR's functions, instead of duplicating them, but they're private.
  6. * As long as SSL's server session cache code must do platform native I/O
  7. * to accomplish its job, and NSPR's error mapping functions remain private,
  8. * this code will continue to need to be replicated.
  9. *
  10. * ***** BEGIN LICENSE BLOCK *****
  11. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  12. *
  13. * The contents of this file are subject to the Mozilla Public License Version
  14. * 1.1 (the "License"); you may not use this file except in compliance with
  15. * the License. You may obtain a copy of the License at
  16. * http://www.mozilla.org/MPL/
  17. *
  18. * Software distributed under the License is distributed on an "AS IS" basis,
  19. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  20. * for the specific language governing rights and limitations under the
  21. * License.
  22. *
  23. * The Original Code is the Netscape security libraries.
  24. *
  25. * The Initial Developer of the Original Code is
  26. * Netscape Communications Corporation.
  27. * Portions created by the Initial Developer are Copyright (C) 1994-2000
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. *
  32. * Alternatively, the contents of this file may be used under the terms of
  33. * either the GNU General Public License Version 2 or later (the "GPL"), or
  34. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  35. * in which case the provisions of the GPL or the LGPL are applicable instead
  36. * of those above. If you wish to allow use of your version of this file only
  37. * under the terms of either the GPL or the LGPL, and not to allow others to
  38. * use your version of this file under the terms of the MPL, indicate your
  39. * decision by deleting the provisions above and replace them with the notice
  40. * and other provisions required by the GPL or the LGPL. If you do not delete
  41. * the provisions above, a recipient may use your version of this file under
  42. * the terms of any one of the MPL, the GPL or the LGPL.
  43. *
  44. * ***** END LICENSE BLOCK ***** */
  45. /* $Id: unix_err.c,v 1.8 2004/04/27 23:04:39 gerv%gerv.net Exp $ */
  46. #if 0
  47. #include "primpl.h"
  48. #else
  49. #define _PR_POLL_AVAILABLE 1
  50. #include "prerror.h"
  51. #endif
  52. #if defined (__bsdi__) || defined(NTO) || defined(DARWIN) || defined(BEOS)
  53. #undef _PR_POLL_AVAILABLE
  54. #endif
  55. #if defined(_PR_POLL_AVAILABLE)
  56. #include <poll.h>
  57. #endif
  58. #include <errno.h>
  59. /* forward declarations. */
  60. void nss_MD_unix_map_default_error(int err);
  61. void nss_MD_unix_map_opendir_error(int err)
  62. {
  63. nss_MD_unix_map_default_error(err);
  64. }
  65. void nss_MD_unix_map_closedir_error(int err)
  66. {
  67. PRErrorCode prError;
  68. switch (err) {
  69. case EINVAL: prError = PR_BAD_DESCRIPTOR_ERROR; break;
  70. default: nss_MD_unix_map_default_error(err); return;
  71. }
  72. PR_SetError(prError, err);
  73. }
  74. void nss_MD_unix_readdir_error(int err)
  75. {
  76. PRErrorCode prError;
  77. switch (err) {
  78. case ENOENT: prError = PR_NO_MORE_FILES_ERROR; break;
  79. #ifdef EOVERFLOW
  80. case EOVERFLOW: prError = PR_IO_ERROR; break;
  81. #endif
  82. case EINVAL: prError = PR_IO_ERROR; break;
  83. case ENXIO: prError = PR_IO_ERROR; break;
  84. default: nss_MD_unix_map_default_error(err); return;
  85. }
  86. PR_SetError(prError, err);
  87. }
  88. void nss_MD_unix_map_unlink_error(int err)
  89. {
  90. PRErrorCode prError;
  91. switch (err) {
  92. case EPERM: prError = PR_IS_DIRECTORY_ERROR; break;
  93. default: nss_MD_unix_map_default_error(err); return;
  94. }
  95. PR_SetError(prError, err);
  96. }
  97. void nss_MD_unix_map_stat_error(int err)
  98. {
  99. PRErrorCode prError;
  100. switch (err) {
  101. case ETIMEDOUT: prError = PR_REMOTE_FILE_ERROR; break;
  102. default: nss_MD_unix_map_default_error(err); return;
  103. }
  104. PR_SetError(prError, err);
  105. }
  106. void nss_MD_unix_map_fstat_error(int err)
  107. {
  108. PRErrorCode prError;
  109. switch (err) {
  110. case ETIMEDOUT: prError = PR_REMOTE_FILE_ERROR; break;
  111. default: nss_MD_unix_map_default_error(err); return;
  112. }
  113. PR_SetError(prError, err);
  114. }
  115. void nss_MD_unix_map_rename_error(int err)
  116. {
  117. PRErrorCode prError;
  118. switch (err) {
  119. case EEXIST: prError = PR_DIRECTORY_NOT_EMPTY_ERROR; break;
  120. default: nss_MD_unix_map_default_error(err); return;
  121. }
  122. PR_SetError(prError, err);
  123. }
  124. void nss_MD_unix_map_access_error(int err)
  125. {
  126. PRErrorCode prError;
  127. switch (err) {
  128. case ETIMEDOUT: prError = PR_REMOTE_FILE_ERROR; break;
  129. default: nss_MD_unix_map_default_error(err); return;
  130. }
  131. PR_SetError(prError, err);
  132. }
  133. void nss_MD_unix_map_mkdir_error(int err)
  134. {
  135. nss_MD_unix_map_default_error(err);
  136. }
  137. void nss_MD_unix_map_rmdir_error(int err)
  138. {
  139. PRErrorCode prError;
  140. switch (err) {
  141. case EEXIST: prError = PR_DIRECTORY_NOT_EMPTY_ERROR; break;
  142. case EINVAL: prError = PR_DIRECTORY_NOT_EMPTY_ERROR; break;
  143. case ETIMEDOUT: prError = PR_REMOTE_FILE_ERROR; break;
  144. default: nss_MD_unix_map_default_error(err); return;
  145. }
  146. PR_SetError(prError, err);
  147. }
  148. void nss_MD_unix_map_read_error(int err)
  149. {
  150. PRErrorCode prError;
  151. switch (err) {
  152. case EINVAL: prError = PR_INVALID_METHOD_ERROR; break;
  153. case ENXIO: prError = PR_INVALID_ARGUMENT_ERROR; break;
  154. default: nss_MD_unix_map_default_error(err); return;
  155. }
  156. PR_SetError(prError, err);
  157. }
  158. void nss_MD_unix_map_write_error(int err)
  159. {
  160. PRErrorCode prError;
  161. switch (err) {
  162. case EINVAL: prError = PR_INVALID_METHOD_ERROR; break;
  163. case ENXIO: prError = PR_INVALID_METHOD_ERROR; break;
  164. case ETIMEDOUT: prError = PR_REMOTE_FILE_ERROR; break;
  165. default: nss_MD_unix_map_default_error(err); return;
  166. }
  167. PR_SetError(prError, err);
  168. }
  169. void nss_MD_unix_map_lseek_error(int err)
  170. {
  171. nss_MD_unix_map_default_error(err);
  172. }
  173. void nss_MD_unix_map_fsync_error(int err)
  174. {
  175. PRErrorCode prError;
  176. switch (err) {
  177. case ETIMEDOUT: prError = PR_REMOTE_FILE_ERROR; break;
  178. case EINVAL: prError = PR_INVALID_METHOD_ERROR; break;
  179. default: nss_MD_unix_map_default_error(err); return;
  180. }
  181. PR_SetError(prError, err);
  182. }
  183. void nss_MD_unix_map_close_error(int err)
  184. {
  185. PRErrorCode prError;
  186. switch (err) {
  187. case ETIMEDOUT: prError = PR_REMOTE_FILE_ERROR; break;
  188. default: nss_MD_unix_map_default_error(err); return;
  189. }
  190. PR_SetError(prError, err);
  191. }
  192. void nss_MD_unix_map_socket_error(int err)
  193. {
  194. PRErrorCode prError;
  195. switch (err) {
  196. case ENOMEM: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  197. default: nss_MD_unix_map_default_error(err); return;
  198. }
  199. PR_SetError(prError, err);
  200. }
  201. void nss_MD_unix_map_socketavailable_error(int err)
  202. {
  203. PR_SetError(PR_BAD_DESCRIPTOR_ERROR, err);
  204. }
  205. void nss_MD_unix_map_recv_error(int err)
  206. {
  207. nss_MD_unix_map_default_error(err);
  208. }
  209. void nss_MD_unix_map_recvfrom_error(int err)
  210. {
  211. nss_MD_unix_map_default_error(err);
  212. }
  213. void nss_MD_unix_map_send_error(int err)
  214. {
  215. nss_MD_unix_map_default_error(err);
  216. }
  217. void nss_MD_unix_map_sendto_error(int err)
  218. {
  219. nss_MD_unix_map_default_error(err);
  220. }
  221. void nss_MD_unix_map_writev_error(int err)
  222. {
  223. nss_MD_unix_map_default_error(err);
  224. }
  225. void nss_MD_unix_map_accept_error(int err)
  226. {
  227. PRErrorCode prError;
  228. switch (err) {
  229. case ENODEV: prError = PR_NOT_TCP_SOCKET_ERROR; break;
  230. default: nss_MD_unix_map_default_error(err); return;
  231. }
  232. PR_SetError(prError, err);
  233. }
  234. void nss_MD_unix_map_connect_error(int err)
  235. {
  236. PRErrorCode prError;
  237. switch (err) {
  238. case EACCES: prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
  239. #if defined(UNIXWARE) || defined(SNI) || defined(NEC)
  240. /*
  241. * On some platforms, if we connect to a port on the local host
  242. * (the loopback address) that no process is listening on, we get
  243. * EIO instead of ECONNREFUSED.
  244. */
  245. case EIO: prError = PR_CONNECT_REFUSED_ERROR; break;
  246. #endif
  247. case ELOOP: prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
  248. case ENOENT: prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
  249. case ENXIO: prError = PR_IO_ERROR; break;
  250. default: nss_MD_unix_map_default_error(err); return;
  251. }
  252. PR_SetError(prError, err);
  253. }
  254. void nss_MD_unix_map_bind_error(int err)
  255. {
  256. PRErrorCode prError;
  257. switch (err) {
  258. case EINVAL: prError = PR_SOCKET_ADDRESS_IS_BOUND_ERROR; break;
  259. /*
  260. * UNIX domain sockets are not supported in NSPR
  261. */
  262. case EIO: prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
  263. case EISDIR: prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
  264. case ELOOP: prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
  265. case ENOENT: prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
  266. case ENOTDIR: prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
  267. case EROFS: prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
  268. default: nss_MD_unix_map_default_error(err); return;
  269. }
  270. PR_SetError(prError, err);
  271. }
  272. void nss_MD_unix_map_listen_error(int err)
  273. {
  274. nss_MD_unix_map_default_error(err);
  275. }
  276. void nss_MD_unix_map_shutdown_error(int err)
  277. {
  278. nss_MD_unix_map_default_error(err);
  279. }
  280. void nss_MD_unix_map_socketpair_error(int err)
  281. {
  282. PRErrorCode prError;
  283. switch (err) {
  284. case ENOMEM: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  285. default: nss_MD_unix_map_default_error(err); return;
  286. }
  287. PR_SetError(prError, err);
  288. }
  289. void nss_MD_unix_map_getsockname_error(int err)
  290. {
  291. PRErrorCode prError;
  292. switch (err) {
  293. case ENOMEM: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  294. default: nss_MD_unix_map_default_error(err); return;
  295. }
  296. PR_SetError(prError, err);
  297. }
  298. void nss_MD_unix_map_getpeername_error(int err)
  299. {
  300. PRErrorCode prError;
  301. switch (err) {
  302. case ENOMEM: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  303. default: nss_MD_unix_map_default_error(err); return;
  304. }
  305. PR_SetError(prError, err);
  306. }
  307. void nss_MD_unix_map_getsockopt_error(int err)
  308. {
  309. PRErrorCode prError;
  310. switch (err) {
  311. case EINVAL: prError = PR_BUFFER_OVERFLOW_ERROR; break;
  312. case ENOMEM: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  313. default: nss_MD_unix_map_default_error(err); return;
  314. }
  315. PR_SetError(prError, err);
  316. }
  317. void nss_MD_unix_map_setsockopt_error(int err)
  318. {
  319. PRErrorCode prError;
  320. switch (err) {
  321. case EINVAL: prError = PR_BUFFER_OVERFLOW_ERROR; break;
  322. case ENOMEM: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  323. default: nss_MD_unix_map_default_error(err); return;
  324. }
  325. PR_SetError(prError, err);
  326. }
  327. void nss_MD_unix_map_open_error(int err)
  328. {
  329. PRErrorCode prError;
  330. switch (err) {
  331. case EAGAIN: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  332. case EBUSY: prError = PR_IO_ERROR; break;
  333. case ENODEV: prError = PR_FILE_NOT_FOUND_ERROR; break;
  334. case ENOMEM: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  335. case ETIMEDOUT: prError = PR_REMOTE_FILE_ERROR; break;
  336. default: nss_MD_unix_map_default_error(err); return;
  337. }
  338. PR_SetError(prError, err);
  339. }
  340. void nss_MD_unix_map_mmap_error(int err)
  341. {
  342. PRErrorCode prError;
  343. switch (err) {
  344. case EAGAIN: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  345. case EMFILE: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  346. case ENODEV: prError = PR_OPERATION_NOT_SUPPORTED_ERROR; break;
  347. case ENXIO: prError = PR_INVALID_ARGUMENT_ERROR; break;
  348. default: nss_MD_unix_map_default_error(err); return;
  349. }
  350. PR_SetError(prError, err);
  351. }
  352. void nss_MD_unix_map_gethostname_error(int err)
  353. {
  354. nss_MD_unix_map_default_error(err);
  355. }
  356. void nss_MD_unix_map_select_error(int err)
  357. {
  358. nss_MD_unix_map_default_error(err);
  359. }
  360. #ifdef _PR_POLL_AVAILABLE
  361. void nss_MD_unix_map_poll_error(int err)
  362. {
  363. PRErrorCode prError;
  364. switch (err) {
  365. case EAGAIN: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  366. default: nss_MD_unix_map_default_error(err); return;
  367. }
  368. PR_SetError(prError, err);
  369. }
  370. void nss_MD_unix_map_poll_revents_error(int err)
  371. {
  372. if (err & POLLNVAL)
  373. PR_SetError(PR_BAD_DESCRIPTOR_ERROR, EBADF);
  374. else if (err & POLLHUP)
  375. PR_SetError(PR_CONNECT_RESET_ERROR, EPIPE);
  376. else if (err & POLLERR)
  377. PR_SetError(PR_IO_ERROR, EIO);
  378. else
  379. PR_SetError(PR_UNKNOWN_ERROR, err);
  380. }
  381. #endif /* _PR_POLL_AVAILABLE */
  382. void nss_MD_unix_map_flock_error(int err)
  383. {
  384. PRErrorCode prError;
  385. switch (err) {
  386. case EINVAL: prError = PR_BAD_DESCRIPTOR_ERROR; break;
  387. case EWOULDBLOCK: prError = PR_FILE_IS_LOCKED_ERROR; break;
  388. default: nss_MD_unix_map_default_error(err); return;
  389. }
  390. PR_SetError(prError, err);
  391. }
  392. void nss_MD_unix_map_lockf_error(int err)
  393. {
  394. PRErrorCode prError;
  395. switch (err) {
  396. case EACCES: prError = PR_FILE_IS_LOCKED_ERROR; break;
  397. case EDEADLK: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  398. default: nss_MD_unix_map_default_error(err); return;
  399. }
  400. PR_SetError(prError, err);
  401. }
  402. #ifdef HPUX11
  403. void nss_MD_hpux_map_sendfile_error(int err)
  404. {
  405. nss_MD_unix_map_default_error(err);
  406. }
  407. #endif /* HPUX11 */
  408. void nss_MD_unix_map_default_error(int err)
  409. {
  410. PRErrorCode prError;
  411. switch (err ) {
  412. case EACCES: prError = PR_NO_ACCESS_RIGHTS_ERROR; break;
  413. case EADDRINUSE: prError = PR_ADDRESS_IN_USE_ERROR; break;
  414. case EADDRNOTAVAIL: prError = PR_ADDRESS_NOT_AVAILABLE_ERROR; break;
  415. case EAFNOSUPPORT: prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
  416. case EAGAIN: prError = PR_WOULD_BLOCK_ERROR; break;
  417. /*
  418. * On QNX and Neutrino, EALREADY is defined as EBUSY.
  419. */
  420. #if EALREADY != EBUSY
  421. case EALREADY: prError = PR_ALREADY_INITIATED_ERROR; break;
  422. #endif
  423. case EBADF: prError = PR_BAD_DESCRIPTOR_ERROR; break;
  424. #ifdef EBADMSG
  425. case EBADMSG: prError = PR_IO_ERROR; break;
  426. #endif
  427. case EBUSY: prError = PR_FILESYSTEM_MOUNTED_ERROR; break;
  428. case ECONNREFUSED: prError = PR_CONNECT_REFUSED_ERROR; break;
  429. case ECONNRESET: prError = PR_CONNECT_RESET_ERROR; break;
  430. case EDEADLK: prError = PR_DEADLOCK_ERROR; break;
  431. #ifdef EDIRCORRUPTED
  432. case EDIRCORRUPTED: prError = PR_DIRECTORY_CORRUPTED_ERROR; break;
  433. #endif
  434. #ifdef EDQUOT
  435. case EDQUOT: prError = PR_NO_DEVICE_SPACE_ERROR; break;
  436. #endif
  437. case EEXIST: prError = PR_FILE_EXISTS_ERROR; break;
  438. case EFAULT: prError = PR_ACCESS_FAULT_ERROR; break;
  439. case EFBIG: prError = PR_FILE_TOO_BIG_ERROR; break;
  440. case EINPROGRESS: prError = PR_IN_PROGRESS_ERROR; break;
  441. case EINTR: prError = PR_PENDING_INTERRUPT_ERROR; break;
  442. case EINVAL: prError = PR_INVALID_ARGUMENT_ERROR; break;
  443. case EIO: prError = PR_IO_ERROR; break;
  444. case EISCONN: prError = PR_IS_CONNECTED_ERROR; break;
  445. case EISDIR: prError = PR_IS_DIRECTORY_ERROR; break;
  446. case ELOOP: prError = PR_LOOP_ERROR; break;
  447. case EMFILE: prError = PR_PROC_DESC_TABLE_FULL_ERROR; break;
  448. case EMLINK: prError = PR_MAX_DIRECTORY_ENTRIES_ERROR; break;
  449. case EMSGSIZE: prError = PR_INVALID_ARGUMENT_ERROR; break;
  450. #ifdef EMULTIHOP
  451. case EMULTIHOP: prError = PR_REMOTE_FILE_ERROR; break;
  452. #endif
  453. case ENAMETOOLONG: prError = PR_NAME_TOO_LONG_ERROR; break;
  454. case ENETUNREACH: prError = PR_NETWORK_UNREACHABLE_ERROR; break;
  455. case ENFILE: prError = PR_SYS_DESC_TABLE_FULL_ERROR; break;
  456. #if !defined(SCO)
  457. case ENOBUFS: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  458. #endif
  459. case ENODEV: prError = PR_FILE_NOT_FOUND_ERROR; break;
  460. case ENOENT: prError = PR_FILE_NOT_FOUND_ERROR; break;
  461. case ENOLCK: prError = PR_FILE_IS_LOCKED_ERROR; break;
  462. #ifdef ENOLINK
  463. case ENOLINK: prError = PR_REMOTE_FILE_ERROR; break;
  464. #endif
  465. case ENOMEM: prError = PR_OUT_OF_MEMORY_ERROR; break;
  466. case ENOPROTOOPT: prError = PR_INVALID_ARGUMENT_ERROR; break;
  467. case ENOSPC: prError = PR_NO_DEVICE_SPACE_ERROR; break;
  468. #ifdef ENOSR
  469. case ENOSR: prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
  470. #endif
  471. case ENOTCONN: prError = PR_NOT_CONNECTED_ERROR; break;
  472. case ENOTDIR: prError = PR_NOT_DIRECTORY_ERROR; break;
  473. case ENOTSOCK: prError = PR_NOT_SOCKET_ERROR; break;
  474. case ENXIO: prError = PR_FILE_NOT_FOUND_ERROR; break;
  475. case EOPNOTSUPP: prError = PR_NOT_TCP_SOCKET_ERROR; break;
  476. #ifdef EOVERFLOW
  477. case EOVERFLOW: prError = PR_BUFFER_OVERFLOW_ERROR; break;
  478. #endif
  479. case EPERM: prError = PR_NO_ACCESS_RIGHTS_ERROR; break;
  480. case EPIPE: prError = PR_CONNECT_RESET_ERROR; break;
  481. #ifdef EPROTO
  482. case EPROTO: prError = PR_IO_ERROR; break;
  483. #endif
  484. case EPROTONOSUPPORT: prError = PR_PROTOCOL_NOT_SUPPORTED_ERROR; break;
  485. case EPROTOTYPE: prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
  486. case ERANGE: prError = PR_INVALID_METHOD_ERROR; break;
  487. case EROFS: prError = PR_READ_ONLY_FILESYSTEM_ERROR; break;
  488. case ESPIPE: prError = PR_INVALID_METHOD_ERROR; break;
  489. case ETIMEDOUT: prError = PR_IO_TIMEOUT_ERROR; break;
  490. #if EWOULDBLOCK != EAGAIN
  491. case EWOULDBLOCK: prError = PR_WOULD_BLOCK_ERROR; break;
  492. #endif
  493. case EXDEV: prError = PR_NOT_SAME_DEVICE_ERROR; break;
  494. default: prError = PR_UNKNOWN_ERROR; break;
  495. }
  496. PR_SetError(prError, err);
  497. }