xref: /netbsd-src/external/bsd/ntp/dist/libntp/lib/isc/unix/errno2result.c (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1*eabc0478Schristos /*	$NetBSD: errno2result.c,v 1.2 2024/08/18 20:47:15 christos Exp $	*/
2897be3a4Schristos 
3897be3a4Schristos /*
4897be3a4Schristos  * Copyright (C) 2004, 2005, 2007, 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
5897be3a4Schristos  * Copyright (C) 2000-2002  Internet Software Consortium.
6897be3a4Schristos  *
7897be3a4Schristos  * Permission to use, copy, modify, and/or distribute this software for any
8897be3a4Schristos  * purpose with or without fee is hereby granted, provided that the above
9897be3a4Schristos  * copyright notice and this permission notice appear in all copies.
10897be3a4Schristos  *
11897be3a4Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12897be3a4Schristos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13897be3a4Schristos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14897be3a4Schristos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15897be3a4Schristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16897be3a4Schristos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17897be3a4Schristos  * PERFORMANCE OF THIS SOFTWARE.
18897be3a4Schristos  */
19897be3a4Schristos 
20897be3a4Schristos /* Id */
21897be3a4Schristos 
22897be3a4Schristos /*! \file */
23897be3a4Schristos 
24897be3a4Schristos #include <config.h>
25897be3a4Schristos 
26897be3a4Schristos #include <isc/result.h>
27897be3a4Schristos #include <isc/strerror.h>
28897be3a4Schristos #include <isc/util.h>
29897be3a4Schristos 
30897be3a4Schristos #include "errno2result.h"
31897be3a4Schristos 
32897be3a4Schristos /*%
33897be3a4Schristos  * Convert a POSIX errno value into an isc_result_t.  The
34897be3a4Schristos  * list of supported errno values is not complete; new users
35897be3a4Schristos  * of this function should add any expected errors that are
36897be3a4Schristos  * not already there.
37897be3a4Schristos  */
38897be3a4Schristos isc_result_t
39897be3a4Schristos isc___errno2result(int posixerrno, const char *file, unsigned int line) {
40897be3a4Schristos 	char strbuf[ISC_STRERRORSIZE];
41897be3a4Schristos 
42897be3a4Schristos 	switch (posixerrno) {
43897be3a4Schristos 	case ENOTDIR:
44897be3a4Schristos 	case ELOOP:
45897be3a4Schristos 	case EINVAL:		/* XXX sometimes this is not for files */
46897be3a4Schristos 	case ENAMETOOLONG:
47897be3a4Schristos 	case EBADF:
48897be3a4Schristos 		return (ISC_R_INVALIDFILE);
49897be3a4Schristos 	case ENOENT:
50897be3a4Schristos 		return (ISC_R_FILENOTFOUND);
51897be3a4Schristos 	case EACCES:
52897be3a4Schristos 	case EPERM:
53897be3a4Schristos 		return (ISC_R_NOPERM);
54897be3a4Schristos 	case EEXIST:
55897be3a4Schristos 		return (ISC_R_FILEEXISTS);
56897be3a4Schristos 	case EIO:
57897be3a4Schristos 		return (ISC_R_IOERROR);
58897be3a4Schristos 	case ENOMEM:
59897be3a4Schristos 		return (ISC_R_NOMEMORY);
60897be3a4Schristos 	case ENFILE:
61897be3a4Schristos 	case EMFILE:
62897be3a4Schristos 		return (ISC_R_TOOMANYOPENFILES);
63897be3a4Schristos 	case EPIPE:
64897be3a4Schristos #ifdef ECONNRESET
65897be3a4Schristos 	case ECONNRESET:
66897be3a4Schristos #endif
67897be3a4Schristos #ifdef ECONNABORTED
68897be3a4Schristos 	case ECONNABORTED:
69897be3a4Schristos #endif
70897be3a4Schristos 		return (ISC_R_CONNECTIONRESET);
71897be3a4Schristos #ifdef ENOTCONN
72897be3a4Schristos 	case ENOTCONN:
73897be3a4Schristos 		return (ISC_R_NOTCONNECTED);
74897be3a4Schristos #endif
75897be3a4Schristos #ifdef ETIMEDOUT
76897be3a4Schristos 	case ETIMEDOUT:
77897be3a4Schristos 		return (ISC_R_TIMEDOUT);
78897be3a4Schristos #endif
79897be3a4Schristos #ifdef ENOBUFS
80897be3a4Schristos 	case ENOBUFS:
81897be3a4Schristos 		return (ISC_R_NORESOURCES);
82897be3a4Schristos #endif
83897be3a4Schristos #ifdef EAFNOSUPPORT
84897be3a4Schristos 	case EAFNOSUPPORT:
85897be3a4Schristos 		return (ISC_R_FAMILYNOSUPPORT);
86897be3a4Schristos #endif
87897be3a4Schristos #ifdef ENETDOWN
88897be3a4Schristos 	case ENETDOWN:
89897be3a4Schristos 		return (ISC_R_NETDOWN);
90897be3a4Schristos #endif
91897be3a4Schristos #ifdef EHOSTDOWN
92897be3a4Schristos 	case EHOSTDOWN:
93897be3a4Schristos 		return (ISC_R_HOSTDOWN);
94897be3a4Schristos #endif
95897be3a4Schristos #ifdef ENETUNREACH
96897be3a4Schristos 	case ENETUNREACH:
97897be3a4Schristos 		return (ISC_R_NETUNREACH);
98897be3a4Schristos #endif
99897be3a4Schristos #ifdef EHOSTUNREACH
100897be3a4Schristos 	case EHOSTUNREACH:
101897be3a4Schristos 		return (ISC_R_HOSTUNREACH);
102897be3a4Schristos #endif
103897be3a4Schristos #ifdef EADDRINUSE
104897be3a4Schristos 	case EADDRINUSE:
105897be3a4Schristos 		return (ISC_R_ADDRINUSE);
106897be3a4Schristos #endif
107897be3a4Schristos 	case EADDRNOTAVAIL:
108897be3a4Schristos 		return (ISC_R_ADDRNOTAVAIL);
109897be3a4Schristos 	case ECONNREFUSED:
110897be3a4Schristos 		return (ISC_R_CONNREFUSED);
111897be3a4Schristos 	default:
112897be3a4Schristos 		isc__strerror(posixerrno, strbuf, sizeof(strbuf));
113897be3a4Schristos 		UNEXPECTED_ERROR(file, line, "unable to convert errno "
114897be3a4Schristos 				 "to isc_result: %d: %s",
115897be3a4Schristos 				 posixerrno, strbuf);
116897be3a4Schristos 		/*
117897be3a4Schristos 		 * XXXDCL would be nice if perhaps this function could
118897be3a4Schristos 		 * return the system's error string, so the caller
119897be3a4Schristos 		 * might have something more descriptive than "unexpected
120897be3a4Schristos 		 * error" to log with.
121897be3a4Schristos 		 */
122897be3a4Schristos 		return (ISC_R_UNEXPECTED);
123897be3a4Schristos 	}
124897be3a4Schristos }
125