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