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