PageRenderTime 48ms CodeModel.GetById 17ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 47#if 0
 48#include "primpl.h"
 49#else
 50#define _PR_POLL_AVAILABLE 1
 51#include "prerror.h"
 52#endif
 53
 54#if defined (__bsdi__) || defined(NTO) || defined(DARWIN) || defined(BEOS)
 55#undef _PR_POLL_AVAILABLE
 56#endif
 57
 58#if defined(_PR_POLL_AVAILABLE)
 59#include <poll.h>
 60#endif
 61#include <errno.h>
 62
 63/* forward declarations. */
 64void nss_MD_unix_map_default_error(int err);
 65
 66void nss_MD_unix_map_opendir_error(int err)
 67{
 68    nss_MD_unix_map_default_error(err);
 69}
 70
 71void nss_MD_unix_map_closedir_error(int err)
 72{
 73    PRErrorCode prError;
 74    switch (err) {
 75    case EINVAL:	prError = PR_BAD_DESCRIPTOR_ERROR; break;
 76    default:		nss_MD_unix_map_default_error(err); return;
 77    }
 78    PR_SetError(prError, err);
 79}
 80
 81void nss_MD_unix_readdir_error(int err)
 82{
 83    PRErrorCode prError;
 84
 85    switch (err) {
 86    case ENOENT:	prError = PR_NO_MORE_FILES_ERROR; break;
 87#ifdef EOVERFLOW
 88    case EOVERFLOW:	prError = PR_IO_ERROR; break;
 89#endif
 90    case EINVAL:	prError = PR_IO_ERROR; break;
 91    case ENXIO:		prError = PR_IO_ERROR; break;
 92    default:		nss_MD_unix_map_default_error(err); return;
 93    }
 94    PR_SetError(prError, err);
 95}
 96
 97void nss_MD_unix_map_unlink_error(int err)
 98{
 99    PRErrorCode prError;
100    switch (err) {
101    case EPERM:		prError = PR_IS_DIRECTORY_ERROR; break;
102    default:		nss_MD_unix_map_default_error(err); return;
103    }
104    PR_SetError(prError, err);
105}
106
107void nss_MD_unix_map_stat_error(int err)
108{
109    PRErrorCode prError;
110    switch (err) {
111    case ETIMEDOUT:	prError = PR_REMOTE_FILE_ERROR; break;
112    default:		nss_MD_unix_map_default_error(err); return;
113    }
114    PR_SetError(prError, err);
115}
116
117void nss_MD_unix_map_fstat_error(int err)
118{
119    PRErrorCode prError;
120    switch (err) {
121    case ETIMEDOUT:	prError = PR_REMOTE_FILE_ERROR; break;
122    default:		nss_MD_unix_map_default_error(err); return;
123    }
124    PR_SetError(prError, err);
125}
126
127void nss_MD_unix_map_rename_error(int err)
128{
129    PRErrorCode prError;
130    switch (err) {
131    case EEXIST:	prError = PR_DIRECTORY_NOT_EMPTY_ERROR; break;
132    default:		nss_MD_unix_map_default_error(err); return;
133    }
134    PR_SetError(prError, err);
135}
136
137void nss_MD_unix_map_access_error(int err)
138{
139    PRErrorCode prError;
140    switch (err) {
141    case ETIMEDOUT:	prError = PR_REMOTE_FILE_ERROR; break;
142    default:		nss_MD_unix_map_default_error(err); return;
143    }
144    PR_SetError(prError, err);
145}
146
147void nss_MD_unix_map_mkdir_error(int err)
148{
149    nss_MD_unix_map_default_error(err);
150}
151
152void nss_MD_unix_map_rmdir_error(int err)
153{
154    PRErrorCode prError;
155
156    switch (err) {
157    case EEXIST:	prError = PR_DIRECTORY_NOT_EMPTY_ERROR; break;
158    case EINVAL:	prError = PR_DIRECTORY_NOT_EMPTY_ERROR; break;
159    case ETIMEDOUT:	prError = PR_REMOTE_FILE_ERROR; break;
160    default:		nss_MD_unix_map_default_error(err); return;
161    }
162    PR_SetError(prError, err);
163}
164
165void nss_MD_unix_map_read_error(int err)
166{
167    PRErrorCode prError;
168    switch (err) {
169    case EINVAL:	prError = PR_INVALID_METHOD_ERROR; break;
170    case ENXIO:		prError = PR_INVALID_ARGUMENT_ERROR; break;
171    default:		nss_MD_unix_map_default_error(err); return;
172    }
173    PR_SetError(prError, err);
174}
175
176void nss_MD_unix_map_write_error(int err)
177{
178    PRErrorCode prError;
179    switch (err) {
180    case EINVAL:	prError = PR_INVALID_METHOD_ERROR; break;
181    case ENXIO:		prError = PR_INVALID_METHOD_ERROR; break;
182    case ETIMEDOUT:	prError = PR_REMOTE_FILE_ERROR; break;
183    default:		nss_MD_unix_map_default_error(err); return;
184    }
185    PR_SetError(prError, err);
186}
187
188void nss_MD_unix_map_lseek_error(int err)
189{
190    nss_MD_unix_map_default_error(err);
191}
192
193void nss_MD_unix_map_fsync_error(int err)
194{
195    PRErrorCode prError;
196    switch (err) {
197    case ETIMEDOUT:	prError = PR_REMOTE_FILE_ERROR; break;
198    case EINVAL:	prError = PR_INVALID_METHOD_ERROR; break;
199    default:		nss_MD_unix_map_default_error(err); return;
200    }
201    PR_SetError(prError, err);
202}
203
204void nss_MD_unix_map_close_error(int err)
205{
206    PRErrorCode prError;
207    switch (err) {
208    case ETIMEDOUT:	prError = PR_REMOTE_FILE_ERROR; break;
209    default:		nss_MD_unix_map_default_error(err); return;
210    }
211    PR_SetError(prError, err);
212}
213
214void nss_MD_unix_map_socket_error(int err)
215{
216    PRErrorCode prError;
217    switch (err) {
218    case ENOMEM:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
219    default:		nss_MD_unix_map_default_error(err); return;
220    }
221    PR_SetError(prError, err);
222}
223
224void nss_MD_unix_map_socketavailable_error(int err)
225{
226    PR_SetError(PR_BAD_DESCRIPTOR_ERROR, err);
227}
228
229void nss_MD_unix_map_recv_error(int err)
230{
231    nss_MD_unix_map_default_error(err);
232}
233
234void nss_MD_unix_map_recvfrom_error(int err)
235{
236    nss_MD_unix_map_default_error(err);
237}
238
239void nss_MD_unix_map_send_error(int err)
240{
241    nss_MD_unix_map_default_error(err);
242}
243
244void nss_MD_unix_map_sendto_error(int err)
245{
246    nss_MD_unix_map_default_error(err);
247}
248
249void nss_MD_unix_map_writev_error(int err)
250{
251    nss_MD_unix_map_default_error(err);
252}
253
254void nss_MD_unix_map_accept_error(int err)
255{
256    PRErrorCode prError;
257    switch (err) {
258    case ENODEV:	prError = PR_NOT_TCP_SOCKET_ERROR; break;
259    default:		nss_MD_unix_map_default_error(err); return;
260    }
261    PR_SetError(prError, err);
262}
263
264void nss_MD_unix_map_connect_error(int err)
265{
266    PRErrorCode prError;
267    switch (err) {
268    case EACCES:	prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
269#if defined(UNIXWARE) || defined(SNI) || defined(NEC)
270    /*
271     * On some platforms, if we connect to a port on the local host 
272     * (the loopback address) that no process is listening on, we get 
273     * EIO instead of ECONNREFUSED.
274     */
275    case EIO:		prError = PR_CONNECT_REFUSED_ERROR; break;
276#endif
277    case ELOOP:		prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
278    case ENOENT:	prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
279    case ENXIO:		prError = PR_IO_ERROR; break;
280    default:		nss_MD_unix_map_default_error(err); return;
281    }
282    PR_SetError(prError, err);
283}
284
285void nss_MD_unix_map_bind_error(int err)
286{
287    PRErrorCode prError;
288    switch (err) {
289    case EINVAL:	prError = PR_SOCKET_ADDRESS_IS_BOUND_ERROR; break;
290        /*
291         * UNIX domain sockets are not supported in NSPR
292         */
293    case EIO:		prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
294    case EISDIR:	prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
295    case ELOOP:		prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
296    case ENOENT:	prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
297    case ENOTDIR:	prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
298    case EROFS:		prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
299    default:		nss_MD_unix_map_default_error(err); return;
300    }
301    PR_SetError(prError, err);
302}
303
304void nss_MD_unix_map_listen_error(int err)
305{
306    nss_MD_unix_map_default_error(err);
307}
308
309void nss_MD_unix_map_shutdown_error(int err)
310{
311    nss_MD_unix_map_default_error(err);
312}
313
314void nss_MD_unix_map_socketpair_error(int err)
315{
316    PRErrorCode prError;
317    switch (err) {
318    case ENOMEM:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
319    default:		nss_MD_unix_map_default_error(err); return;
320    }
321    PR_SetError(prError, err);
322}
323
324void nss_MD_unix_map_getsockname_error(int err)
325{
326    PRErrorCode prError;
327    switch (err) {
328    case ENOMEM:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
329    default:		nss_MD_unix_map_default_error(err); return;
330    }
331    PR_SetError(prError, err);
332}
333
334void nss_MD_unix_map_getpeername_error(int err)
335{
336    PRErrorCode prError;
337
338    switch (err) {
339    case ENOMEM:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
340    default:		nss_MD_unix_map_default_error(err); return;
341    }
342    PR_SetError(prError, err);
343}
344
345void nss_MD_unix_map_getsockopt_error(int err)
346{
347    PRErrorCode prError;
348    switch (err) {
349    case EINVAL:	prError = PR_BUFFER_OVERFLOW_ERROR; break;
350    case ENOMEM:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
351    default:		nss_MD_unix_map_default_error(err); return;
352    }
353    PR_SetError(prError, err);
354}
355
356void nss_MD_unix_map_setsockopt_error(int err)
357{
358    PRErrorCode prError;
359    switch (err) {
360    case EINVAL:	prError = PR_BUFFER_OVERFLOW_ERROR; break;
361    case ENOMEM:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
362    default:		nss_MD_unix_map_default_error(err); return;
363    }
364    PR_SetError(prError, err);
365}
366
367void nss_MD_unix_map_open_error(int err)
368{
369    PRErrorCode prError;
370    switch (err) {
371    case EAGAIN:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
372    case EBUSY:		prError = PR_IO_ERROR; break;
373    case ENODEV:	prError = PR_FILE_NOT_FOUND_ERROR; break;
374    case ENOMEM:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
375    case ETIMEDOUT:	prError = PR_REMOTE_FILE_ERROR; break;
376    default:		nss_MD_unix_map_default_error(err); return;
377    }
378    PR_SetError(prError, err);
379}
380
381void nss_MD_unix_map_mmap_error(int err)
382{
383    PRErrorCode prError;
384    switch (err) {
385    case EAGAIN:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
386    case EMFILE:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
387    case ENODEV:	prError = PR_OPERATION_NOT_SUPPORTED_ERROR; break;
388    case ENXIO:		prError = PR_INVALID_ARGUMENT_ERROR; break;
389    default:		nss_MD_unix_map_default_error(err); return;
390    }
391    PR_SetError(prError, err);
392}
393
394void nss_MD_unix_map_gethostname_error(int err)
395{
396    nss_MD_unix_map_default_error(err);
397}
398
399void nss_MD_unix_map_select_error(int err)
400{
401    nss_MD_unix_map_default_error(err);
402}
403
404#ifdef _PR_POLL_AVAILABLE
405void nss_MD_unix_map_poll_error(int err)
406{
407    PRErrorCode prError;
408
409    switch (err) {
410    case EAGAIN:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
411    default:		nss_MD_unix_map_default_error(err); return;
412    }
413    PR_SetError(prError, err);
414}
415
416void nss_MD_unix_map_poll_revents_error(int err)
417{
418    if (err & POLLNVAL)
419        PR_SetError(PR_BAD_DESCRIPTOR_ERROR, EBADF);
420    else if (err & POLLHUP)
421        PR_SetError(PR_CONNECT_RESET_ERROR, EPIPE);
422    else if (err & POLLERR)
423        PR_SetError(PR_IO_ERROR, EIO);
424    else
425        PR_SetError(PR_UNKNOWN_ERROR, err);
426}
427#endif /* _PR_POLL_AVAILABLE */
428
429
430void nss_MD_unix_map_flock_error(int err)
431{
432    PRErrorCode prError;
433    switch (err) {
434    case EINVAL:	prError = PR_BAD_DESCRIPTOR_ERROR; break;
435    case EWOULDBLOCK:	prError = PR_FILE_IS_LOCKED_ERROR; break;
436    default:		nss_MD_unix_map_default_error(err); return;
437    }
438    PR_SetError(prError, err);
439}
440
441void nss_MD_unix_map_lockf_error(int err)
442{
443    PRErrorCode prError;
444    switch (err) {
445    case EACCES:	prError = PR_FILE_IS_LOCKED_ERROR; break;
446    case EDEADLK:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
447    default:		nss_MD_unix_map_default_error(err); return;
448    }
449    PR_SetError(prError, err);
450}
451
452#ifdef HPUX11
453void nss_MD_hpux_map_sendfile_error(int err)
454{
455    nss_MD_unix_map_default_error(err);
456}
457#endif /* HPUX11 */
458
459
460void nss_MD_unix_map_default_error(int err)
461{
462    PRErrorCode prError;
463    switch (err ) {
464    case EACCES:	prError = PR_NO_ACCESS_RIGHTS_ERROR; break;
465    case EADDRINUSE:	prError = PR_ADDRESS_IN_USE_ERROR; break;
466    case EADDRNOTAVAIL:	prError = PR_ADDRESS_NOT_AVAILABLE_ERROR; break;
467    case EAFNOSUPPORT:	prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
468    case EAGAIN:	prError = PR_WOULD_BLOCK_ERROR; break;
469    /*
470     * On QNX and Neutrino, EALREADY is defined as EBUSY.
471     */
472#if EALREADY != EBUSY
473    case EALREADY:	prError = PR_ALREADY_INITIATED_ERROR; break;
474#endif
475    case EBADF:		prError = PR_BAD_DESCRIPTOR_ERROR; break;
476#ifdef EBADMSG
477    case EBADMSG:	prError = PR_IO_ERROR; break;
478#endif
479    case EBUSY:		prError = PR_FILESYSTEM_MOUNTED_ERROR; break;
480    case ECONNREFUSED:	prError = PR_CONNECT_REFUSED_ERROR; break;
481    case ECONNRESET:	prError = PR_CONNECT_RESET_ERROR; break;
482    case EDEADLK:	prError = PR_DEADLOCK_ERROR; break;
483#ifdef EDIRCORRUPTED
484    case EDIRCORRUPTED:	prError = PR_DIRECTORY_CORRUPTED_ERROR; break;
485#endif
486#ifdef EDQUOT
487    case EDQUOT:	prError = PR_NO_DEVICE_SPACE_ERROR; break;
488#endif
489    case EEXIST:	prError = PR_FILE_EXISTS_ERROR; break;
490    case EFAULT:	prError = PR_ACCESS_FAULT_ERROR; break;
491    case EFBIG:		prError = PR_FILE_TOO_BIG_ERROR; break;
492    case EINPROGRESS:	prError = PR_IN_PROGRESS_ERROR; break;
493    case EINTR:		prError = PR_PENDING_INTERRUPT_ERROR; break;
494    case EINVAL:	prError = PR_INVALID_ARGUMENT_ERROR; break;
495    case EIO:		prError = PR_IO_ERROR; break;
496    case EISCONN:	prError = PR_IS_CONNECTED_ERROR; break;
497    case EISDIR:	prError = PR_IS_DIRECTORY_ERROR; break;
498    case ELOOP:		prError = PR_LOOP_ERROR; break;
499    case EMFILE:	prError = PR_PROC_DESC_TABLE_FULL_ERROR; break;
500    case EMLINK:	prError = PR_MAX_DIRECTORY_ENTRIES_ERROR; break;
501    case EMSGSIZE:	prError = PR_INVALID_ARGUMENT_ERROR; break;
502#ifdef EMULTIHOP
503    case EMULTIHOP:	prError = PR_REMOTE_FILE_ERROR; break;
504#endif
505    case ENAMETOOLONG:	prError = PR_NAME_TOO_LONG_ERROR; break;
506    case ENETUNREACH:	prError = PR_NETWORK_UNREACHABLE_ERROR; break;
507    case ENFILE:	prError = PR_SYS_DESC_TABLE_FULL_ERROR; break;
508#if !defined(SCO)
509    case ENOBUFS:	prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
510#endif
511    case ENODEV:	prError = PR_FILE_NOT_FOUND_ERROR; break;
512    case ENOENT:	prError = PR_FILE_NOT_FOUND_ERROR; break;
513    case ENOLCK:	prError = PR_FILE_IS_LOCKED_ERROR; break;
514#ifdef ENOLINK 
515    case ENOLINK:	prError = PR_REMOTE_FILE_ERROR; break;
516#endif
517    case ENOMEM:	prError = PR_OUT_OF_MEMORY_ERROR; break;
518    case ENOPROTOOPT:	prError = PR_INVALID_ARGUMENT_ERROR; break;
519    case ENOSPC:	prError = PR_NO_DEVICE_SPACE_ERROR; break;
520#ifdef ENOSR 
521    case ENOSR:		prError = PR_INSUFFICIENT_RESOURCES_ERROR; break;
522#endif
523    case ENOTCONN:	prError = PR_NOT_CONNECTED_ERROR; break;
524    case ENOTDIR:	prError = PR_NOT_DIRECTORY_ERROR; break;
525    case ENOTSOCK:	prError = PR_NOT_SOCKET_ERROR; break;
526    case ENXIO:		prError = PR_FILE_NOT_FOUND_ERROR; break;
527    case EOPNOTSUPP:	prError = PR_NOT_TCP_SOCKET_ERROR; break;
528#ifdef EOVERFLOW
529    case EOVERFLOW:	prError = PR_BUFFER_OVERFLOW_ERROR; break;
530#endif
531    case EPERM:		prError = PR_NO_ACCESS_RIGHTS_ERROR; break;
532    case EPIPE:		prError = PR_CONNECT_RESET_ERROR; break;
533#ifdef EPROTO
534    case EPROTO:	prError = PR_IO_ERROR; break;
535#endif
536    case EPROTONOSUPPORT: prError = PR_PROTOCOL_NOT_SUPPORTED_ERROR; break;
537    case EPROTOTYPE:	prError = PR_ADDRESS_NOT_SUPPORTED_ERROR; break;
538    case ERANGE:	prError = PR_INVALID_METHOD_ERROR; break;
539    case EROFS:		prError = PR_READ_ONLY_FILESYSTEM_ERROR; break;
540    case ESPIPE:	prError = PR_INVALID_METHOD_ERROR; break;
541    case ETIMEDOUT:	prError = PR_IO_TIMEOUT_ERROR; break;
542#if EWOULDBLOCK != EAGAIN
543    case EWOULDBLOCK:	prError = PR_WOULD_BLOCK_ERROR; break;
544#endif
545    case EXDEV:		prError = PR_NOT_SAME_DEVICE_ERROR; break;
546
547    default:		prError = PR_UNKNOWN_ERROR; break;
548    }
549    PR_SetError(prError, err);
550}