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