15185a700Sflorian /*
25185a700Sflorian * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
35185a700Sflorian *
45185a700Sflorian * Permission to use, copy, modify, and/or distribute this software for any
55185a700Sflorian * purpose with or without fee is hereby granted, provided that the above
65185a700Sflorian * copyright notice and this permission notice appear in all copies.
75185a700Sflorian *
85185a700Sflorian * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
95185a700Sflorian * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
105185a700Sflorian * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
115185a700Sflorian * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
125185a700Sflorian * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
135185a700Sflorian * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
145185a700Sflorian * PERFORMANCE OF THIS SOFTWARE.
155185a700Sflorian */
165185a700Sflorian
17*1fb015a8Sflorian /* $Id: errno2result.c,v 1.5 2020/09/14 08:40:44 florian Exp $ */
185185a700Sflorian
195185a700Sflorian /*! \file */
205185a700Sflorian
21c6d1a7a6Sjsg #include <errno.h>
2240adc7c5Sjung #include <string.h>
235185a700Sflorian
245185a700Sflorian #include <isc/result.h>
255185a700Sflorian #include <isc/util.h>
265185a700Sflorian
275185a700Sflorian #include "errno2result.h"
285185a700Sflorian
295185a700Sflorian /*%
305185a700Sflorian * Convert a POSIX errno value into an isc_result_t. The
315185a700Sflorian * list of supported errno values is not complete; new users
325185a700Sflorian * of this function should add any expected errors that are
335185a700Sflorian * not already there.
345185a700Sflorian */
355185a700Sflorian isc_result_t
isc___errno2result(int posixerrno,int dolog,const char * file,unsigned int line)36*1fb015a8Sflorian isc___errno2result(int posixerrno, int dolog,
375185a700Sflorian const char *file, unsigned int line)
385185a700Sflorian {
395185a700Sflorian switch (posixerrno) {
405185a700Sflorian case ENOTDIR:
415185a700Sflorian case ELOOP:
425185a700Sflorian case EINVAL: /* XXX sometimes this is not for files */
435185a700Sflorian case ENAMETOOLONG:
445185a700Sflorian case EBADF:
455185a700Sflorian return (ISC_R_INVALIDFILE);
465185a700Sflorian case ENOENT:
475185a700Sflorian return (ISC_R_FILENOTFOUND);
485185a700Sflorian case EACCES:
495185a700Sflorian case EPERM:
505185a700Sflorian return (ISC_R_NOPERM);
515185a700Sflorian case EEXIST:
525185a700Sflorian return (ISC_R_FILEEXISTS);
535185a700Sflorian case EIO:
545185a700Sflorian return (ISC_R_IOERROR);
555185a700Sflorian case ENOMEM:
565185a700Sflorian return (ISC_R_NOMEMORY);
575185a700Sflorian case ENFILE:
585185a700Sflorian case EMFILE:
595185a700Sflorian return (ISC_R_TOOMANYOPENFILES);
605185a700Sflorian case EOVERFLOW:
615185a700Sflorian return (ISC_R_RANGE);
625185a700Sflorian case EPIPE:
635185a700Sflorian case ECONNRESET:
645185a700Sflorian case ECONNABORTED:
655185a700Sflorian return (ISC_R_CONNECTIONRESET);
665185a700Sflorian case ENOTCONN:
675185a700Sflorian return (ISC_R_NOTCONNECTED);
685185a700Sflorian case ETIMEDOUT:
695185a700Sflorian return (ISC_R_TIMEDOUT);
705185a700Sflorian case ENOBUFS:
715185a700Sflorian return (ISC_R_NORESOURCES);
725185a700Sflorian case EAFNOSUPPORT:
735185a700Sflorian return (ISC_R_FAMILYNOSUPPORT);
745185a700Sflorian case ENETDOWN:
755185a700Sflorian return (ISC_R_NETDOWN);
765185a700Sflorian case EHOSTDOWN:
775185a700Sflorian return (ISC_R_HOSTDOWN);
785185a700Sflorian case ENETUNREACH:
795185a700Sflorian return (ISC_R_NETUNREACH);
805185a700Sflorian case EHOSTUNREACH:
815185a700Sflorian return (ISC_R_HOSTUNREACH);
825185a700Sflorian case EADDRINUSE:
835185a700Sflorian return (ISC_R_ADDRINUSE);
845185a700Sflorian case EADDRNOTAVAIL:
855185a700Sflorian return (ISC_R_ADDRNOTAVAIL);
865185a700Sflorian case ECONNREFUSED:
875185a700Sflorian return (ISC_R_CONNREFUSED);
885185a700Sflorian default:
895185a700Sflorian if (dolog) {
905185a700Sflorian UNEXPECTED_ERROR(file, line, "unable to convert errno "
915185a700Sflorian "to isc_result: %d: %s",
9240adc7c5Sjung posixerrno, strerror(posixerrno));
935185a700Sflorian }
945185a700Sflorian /*
955185a700Sflorian * XXXDCL would be nice if perhaps this function could
965185a700Sflorian * return the system's error string, so the caller
975185a700Sflorian * might have something more descriptive than "unexpected
985185a700Sflorian * error" to log with.
995185a700Sflorian */
1005185a700Sflorian return (ISC_R_UNEXPECTED);
1015185a700Sflorian }
1025185a700Sflorian }
103