xref: /netbsd-src/external/mpl/dhcp/bind/dist/lib/isc/unix/errno2result.c (revision 4afad4b7fa6d4a0d3dedf41d1587a7250710ae54)
1 /*	$NetBSD: errno2result.c,v 1.1 2024/02/18 20:57:56 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 /*! \file */
17 
18 #include "errno2result.h"
19 #include <stdbool.h>
20 
21 #include <isc/platform.h>
22 #include <isc/result.h>
23 #include <isc/strerr.h>
24 #include <isc/string.h>
25 #include <isc/util.h>
26 
27 /*%
28  * Convert a POSIX errno value into an isc_result_t.  The
29  * list of supported errno values is not complete; new users
30  * of this function should add any expected errors that are
31  * not already there.
32  */
33 isc_result_t
isc___errno2result(int posixerrno,bool dolog,const char * file,unsigned int line)34 isc___errno2result(int posixerrno, bool dolog, const char *file,
35 		   unsigned int line) {
36 	char strbuf[ISC_STRERRORSIZE];
37 
38 	switch (posixerrno) {
39 	case ENOTDIR:
40 	case ELOOP:
41 	case EINVAL: /* XXX sometimes this is not for files */
42 	case ENAMETOOLONG:
43 	case EBADF:
44 		return (ISC_R_INVALIDFILE);
45 	case ENOENT:
46 		return (ISC_R_FILENOTFOUND);
47 	case EACCES:
48 	case EPERM:
49 		return (ISC_R_NOPERM);
50 	case EEXIST:
51 		return (ISC_R_FILEEXISTS);
52 	case EIO:
53 		return (ISC_R_IOERROR);
54 	case ENOMEM:
55 		return (ISC_R_NOMEMORY);
56 	case ENFILE:
57 	case EMFILE:
58 		return (ISC_R_TOOMANYOPENFILES);
59 #ifdef EDQUOT
60 	case EDQUOT:
61 		return (ISC_R_DISCQUOTA);
62 #endif /* ifdef EDQUOT */
63 	case ENOSPC:
64 		return (ISC_R_DISCFULL);
65 #ifdef EOVERFLOW
66 	case EOVERFLOW:
67 		return (ISC_R_RANGE);
68 #endif /* ifdef EOVERFLOW */
69 	case EPIPE:
70 #ifdef ECONNRESET
71 	case ECONNRESET:
72 #endif /* ifdef ECONNRESET */
73 #ifdef ECONNABORTED
74 	case ECONNABORTED:
75 #endif /* ifdef ECONNABORTED */
76 		return (ISC_R_CONNECTIONRESET);
77 #ifdef ENOTCONN
78 	case ENOTCONN:
79 		return (ISC_R_NOTCONNECTED);
80 #endif /* ifdef ENOTCONN */
81 #ifdef ETIMEDOUT
82 	case ETIMEDOUT:
83 		return (ISC_R_TIMEDOUT);
84 #endif /* ifdef ETIMEDOUT */
85 #ifdef ENOBUFS
86 	case ENOBUFS:
87 		return (ISC_R_NORESOURCES);
88 #endif /* ifdef ENOBUFS */
89 #ifdef EAFNOSUPPORT
90 	case EAFNOSUPPORT:
91 		return (ISC_R_FAMILYNOSUPPORT);
92 #endif /* ifdef EAFNOSUPPORT */
93 #ifdef ENETDOWN
94 	case ENETDOWN:
95 		return (ISC_R_NETDOWN);
96 #endif /* ifdef ENETDOWN */
97 #ifdef EHOSTDOWN
98 	case EHOSTDOWN:
99 		return (ISC_R_HOSTDOWN);
100 #endif /* ifdef EHOSTDOWN */
101 #ifdef ENETUNREACH
102 	case ENETUNREACH:
103 		return (ISC_R_NETUNREACH);
104 #endif /* ifdef ENETUNREACH */
105 #ifdef EHOSTUNREACH
106 	case EHOSTUNREACH:
107 		return (ISC_R_HOSTUNREACH);
108 #endif /* ifdef EHOSTUNREACH */
109 #ifdef EADDRINUSE
110 	case EADDRINUSE:
111 		return (ISC_R_ADDRINUSE);
112 #endif /* ifdef EADDRINUSE */
113 	case EADDRNOTAVAIL:
114 		return (ISC_R_ADDRNOTAVAIL);
115 	case ECONNREFUSED:
116 		return (ISC_R_CONNREFUSED);
117 	default:
118 		if (dolog) {
119 			strerror_r(posixerrno, strbuf, sizeof(strbuf));
120 			UNEXPECTED_ERROR(file, line,
121 					 "unable to convert errno "
122 					 "to isc_result: %d: %s",
123 					 posixerrno, strbuf);
124 		}
125 		/*
126 		 * XXXDCL would be nice if perhaps this function could
127 		 * return the system's error string, so the caller
128 		 * might have something more descriptive than "unexpected
129 		 * error" to log with.
130 		 */
131 		return (ISC_R_UNEXPECTED);
132 	}
133 }
134