1 /*	$NetBSD: errno2result.c,v 1.1.1.1 2009/12/13 16:54:32 kardel Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2000-2002  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id: errno2result.c,v 1.17 2007/06/19 23:47:18 tbox Exp */
21 
22 /*! \file */
23 
24 #include <config.h>
25 
26 #include <isc/result.h>
27 #include <isc/strerror.h>
28 #include <isc/util.h>
29 
30 #include "errno2result.h"
31 
32 /*%
33  * Convert a POSIX errno value into an isc_result_t.  The
34  * list of supported errno values is not complete; new users
35  * of this function should add any expected errors that are
36  * not already there.
37  */
38 isc_result_t
39 isc__errno2result(int posixerrno) {
40 	char strbuf[ISC_STRERRORSIZE];
41 
42 	switch (posixerrno) {
43 	case ENOTDIR:
44 	case ELOOP:
45 	case EINVAL:		/* XXX sometimes this is not for files */
46 	case ENAMETOOLONG:
47 	case EBADF:
48 		return (ISC_R_INVALIDFILE);
49 	case ENOENT:
50 		return (ISC_R_FILENOTFOUND);
51 	case EACCES:
52 	case EPERM:
53 		return (ISC_R_NOPERM);
54 	case EEXIST:
55 		return (ISC_R_FILEEXISTS);
56 	case EIO:
57 		return (ISC_R_IOERROR);
58 	case ENOMEM:
59 		return (ISC_R_NOMEMORY);
60 	case ENFILE:
61 	case EMFILE:
62 		return (ISC_R_TOOMANYOPENFILES);
63 	case EPIPE:
64 #ifdef ECONNRESET
65 	case ECONNRESET:
66 #endif
67 #ifdef ECONNABORTED
68 	case ECONNABORTED:
69 #endif
70 		return (ISC_R_CONNECTIONRESET);
71 #ifdef ENOTCONN
72 	case ENOTCONN:
73 		return (ISC_R_NOTCONNECTED);
74 #endif
75 #ifdef ETIMEDOUT
76 	case ETIMEDOUT:
77 		return (ISC_R_TIMEDOUT);
78 #endif
79 #ifdef ENOBUFS
80 	case ENOBUFS:
81 		return (ISC_R_NORESOURCES);
82 #endif
83 #ifdef EAFNOSUPPORT
84 	case EAFNOSUPPORT:
85 		return (ISC_R_FAMILYNOSUPPORT);
86 #endif
87 #ifdef ENETDOWN
88 	case ENETDOWN:
89 		return (ISC_R_NETDOWN);
90 #endif
91 #ifdef EHOSTDOWN
92 	case EHOSTDOWN:
93 		return (ISC_R_HOSTDOWN);
94 #endif
95 #ifdef ENETUNREACH
96 	case ENETUNREACH:
97 		return (ISC_R_NETUNREACH);
98 #endif
99 #ifdef EHOSTUNREACH
100 	case EHOSTUNREACH:
101 		return (ISC_R_HOSTUNREACH);
102 #endif
103 #ifdef EADDRINUSE
104 	case EADDRINUSE:
105 		return (ISC_R_ADDRINUSE);
106 #endif
107 	case EADDRNOTAVAIL:
108 		return (ISC_R_ADDRNOTAVAIL);
109 	case ECONNREFUSED:
110 		return (ISC_R_CONNREFUSED);
111 	default:
112 		isc__strerror(posixerrno, strbuf, sizeof(strbuf));
113 		UNEXPECTED_ERROR(__FILE__, __LINE__,
114 				 "unable to convert errno "
115 				 "to isc_result: %d: %s",
116 				 posixerrno, strbuf);
117 		/*
118 		 * XXXDCL would be nice if perhaps this function could
119 		 * return the system's error string, so the caller
120 		 * might have something more descriptive than "unexpected
121 		 * error" to log with.
122 		 */
123 		return (ISC_R_UNEXPECTED);
124 	}
125 }
126