1 /* $NetBSD: errno2result.c,v 1.3 2025/01/26 16:25:37 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/result.h> 22 #include <isc/strerr.h> 23 #include <isc/string.h> 24 #include <isc/util.h> 25 26 /*% 27 * Convert a POSIX errno value into an isc_result_t. The 28 * list of supported errno values is not complete; new users 29 * of this function should add any expected errors that are 30 * not already there. 31 */ 32 isc_result_t 33 isc___errno2result(int posixerrno, bool dolog, const char *file, 34 unsigned int line) { 35 char strbuf[ISC_STRERRORSIZE]; 36 37 switch (posixerrno) { 38 case ENOTDIR: 39 case ELOOP: 40 case EINVAL: /* XXX sometimes this is not for files */ 41 case ENAMETOOLONG: 42 case EBADF: 43 return ISC_R_INVALIDFILE; 44 case EISDIR: 45 return ISC_R_NOTFILE; 46 case ENOENT: 47 return ISC_R_FILENOTFOUND; 48 case EACCES: 49 case EPERM: 50 case EROFS: 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 #ifdef EDQUOT 62 case EDQUOT: 63 return ISC_R_DISCQUOTA; 64 #endif /* ifdef EDQUOT */ 65 case ENOSPC: 66 return ISC_R_DISCFULL; 67 #ifdef EOVERFLOW 68 case EOVERFLOW: 69 return ISC_R_RANGE; 70 #endif /* ifdef EOVERFLOW */ 71 case EPIPE: 72 #ifdef ECONNRESET 73 case ECONNRESET: 74 #endif /* ifdef ECONNRESET */ 75 #ifdef ECONNABORTED 76 case ECONNABORTED: 77 #endif /* ifdef ECONNABORTED */ 78 return ISC_R_CONNECTIONRESET; 79 #ifdef ENOTCONN 80 case ENOTCONN: 81 return ISC_R_NOTCONNECTED; 82 #endif /* ifdef ENOTCONN */ 83 #ifdef ETIMEDOUT 84 case ETIMEDOUT: 85 return ISC_R_TIMEDOUT; 86 #endif /* ifdef ETIMEDOUT */ 87 #ifdef ENOBUFS 88 case ENOBUFS: 89 return ISC_R_NORESOURCES; 90 #endif /* ifdef ENOBUFS */ 91 #ifdef EAFNOSUPPORT 92 case EAFNOSUPPORT: 93 return ISC_R_FAMILYNOSUPPORT; 94 #endif /* ifdef EAFNOSUPPORT */ 95 #ifdef ENETDOWN 96 case ENETDOWN: 97 return ISC_R_NETDOWN; 98 #endif /* ifdef ENETDOWN */ 99 #ifdef EHOSTDOWN 100 case EHOSTDOWN: 101 return ISC_R_HOSTDOWN; 102 #endif /* ifdef EHOSTDOWN */ 103 #ifdef ENETUNREACH 104 case ENETUNREACH: 105 return ISC_R_NETUNREACH; 106 #endif /* ifdef ENETUNREACH */ 107 #ifdef EHOSTUNREACH 108 case EHOSTUNREACH: 109 return ISC_R_HOSTUNREACH; 110 #endif /* ifdef EHOSTUNREACH */ 111 #ifdef EADDRINUSE 112 case EADDRINUSE: 113 return ISC_R_ADDRINUSE; 114 #endif /* ifdef EADDRINUSE */ 115 case EADDRNOTAVAIL: 116 return ISC_R_ADDRNOTAVAIL; 117 case ECONNREFUSED: 118 return ISC_R_CONNREFUSED; 119 default: 120 if (dolog) { 121 strerror_r(posixerrno, strbuf, sizeof(strbuf)); 122 UNEXPECTED_ERROR(file, line, 123 "unable to convert errno " 124 "to isc_result: %d: %s", 125 posixerrno, strbuf); 126 } 127 /* 128 * XXXDCL would be nice if perhaps this function could 129 * return the system's error string, so the caller 130 * might have something more descriptive than "unexpected 131 * error" to log with. 132 */ 133 return ISC_R_UNEXPECTED; 134 } 135 } 136