]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
De-duplicate some calls to strerror_r()
authorTony Finch <dot@dotat.at>
Fri, 14 Oct 2022 16:18:07 +0000 (17:18 +0100)
committerTony Finch <dot@dotat.at>
Mon, 17 Oct 2022 15:00:27 +0000 (16:00 +0100)
Specifically, when reporting an unexpected or fatal error.

(cherry picked from commit a34a2784b191f79d7b55b8f61560539655f68ed3)

12 files changed:
lib/isc/app.c
lib/isc/condition.c
lib/isc/include/isc/condition.h
lib/isc/include/isc/error.h
lib/isc/include/isc/mutex.h
lib/isc/include/isc/util.h
lib/isc/mutex.c
lib/isc/net.c
lib/isc/netmgr/netmgr.c
lib/isc/stdtime.c
lib/isc/thread.c
lib/isc/time.c

index c60fd9a671740a9587d78cdb8b24e7354a455f0f..6720f2d27a1ceb230f2da2348d46192824f44b66 100644 (file)
@@ -75,10 +75,7 @@ handle_signal(int sig, void (*handler)(int)) {
        sa.sa_handler = handler;
 
        if (sigfillset(&sa.sa_mask) != 0 || sigaction(sig, &sa, NULL) < 0) {
-               char strbuf[ISC_STRERRORSIZE];
-               strerror_r(errno, strbuf, sizeof(strbuf));
-               isc_error_fatal(__FILE__, __LINE__,
-                               "handle_signal() %d setup: %s", sig, strbuf);
+               FATAL_SYSERROR(errno, "signal %d", sig);
        }
 }
 
@@ -86,7 +83,6 @@ isc_result_t
 isc_app_ctxstart(isc_appctx_t *ctx) {
        int presult;
        sigset_t sset;
-       char strbuf[ISC_STRERRORSIZE];
 
        REQUIRE(VALID_APPCTX(ctx));
 
@@ -128,15 +124,11 @@ isc_app_ctxstart(isc_appctx_t *ctx) {
        if (sigemptyset(&sset) != 0 || sigaddset(&sset, SIGHUP) != 0 ||
            sigaddset(&sset, SIGINT) != 0 || sigaddset(&sset, SIGTERM) != 0)
        {
-               strerror_r(errno, strbuf, sizeof(strbuf));
-               isc_error_fatal(__FILE__, __LINE__,
-                               "isc_app_start() sigsetops: %s", strbuf);
+               FATAL_SYSERROR(errno, "sigsetops");
        }
        presult = pthread_sigmask(SIG_BLOCK, &sset, NULL);
        if (presult != 0) {
-               strerror_r(presult, strbuf, sizeof(strbuf));
-               isc_error_fatal(__FILE__, __LINE__,
-                               "isc_app_start() pthread_sigmask: %s", strbuf);
+               FATAL_SYSERROR(presult, "pthread_sigmask()");
        }
 
        return (ISC_R_SUCCESS);
@@ -226,11 +218,7 @@ isc_app_ctxrun(isc_appctx_t *ctx) {
                            sigaddset(&sset, SIGINT) != 0 ||
                            sigaddset(&sset, SIGTERM) != 0)
                        {
-                               char strbuf[ISC_STRERRORSIZE];
-                               strerror_r(errno, strbuf, sizeof(strbuf));
-                               isc_error_fatal(__FILE__, __LINE__,
-                                               "isc_app_run() sigsetops: %s",
-                                               strbuf);
+                               FATAL_SYSERROR(errno, "sigsetops");
                        }
 
                        if (sigwait(&sset, &sig) == 0) {
@@ -315,12 +303,7 @@ isc_app_ctxshutdown(isc_appctx_t *ctx) {
                } else {
                        /* Normal single BIND9 context */
                        if (kill(getpid(), SIGTERM) < 0) {
-                               char strbuf[ISC_STRERRORSIZE];
-                               strerror_r(errno, strbuf, sizeof(strbuf));
-                               isc_error_fatal(__FILE__, __LINE__,
-                                               "isc_app_shutdown() "
-                                               "kill: %s",
-                                               strbuf);
+                               FATAL_SYSERROR(errno, "kill");
                        }
                }
        }
@@ -348,12 +331,7 @@ isc_app_ctxsuspend(isc_appctx_t *ctx) {
                } else {
                        /* Normal single BIND9 context */
                        if (kill(getpid(), SIGHUP) < 0) {
-                               char strbuf[ISC_STRERRORSIZE];
-                               strerror_r(errno, strbuf, sizeof(strbuf));
-                               isc_error_fatal(__FILE__, __LINE__,
-                                               "isc_app_reload() "
-                                               "kill: %s",
-                                               strbuf);
+                               FATAL_SYSERROR(errno, "kill");
                        }
                }
        }
index 8cd765a067d2eeb1bd21394d2bf70c13207d5ad5..dd2fdff21cb1ba2c211154de26bc6494dc711897 100644 (file)
@@ -26,7 +26,6 @@ isc_condition_waituntil(isc_condition_t *c, isc_mutex_t *m, isc_time_t *t) {
        int presult;
        isc_result_t result;
        struct timespec ts;
-       char strbuf[ISC_STRERRORSIZE];
 
        REQUIRE(c != NULL && m != NULL && t != NULL);
 
@@ -61,7 +60,6 @@ isc_condition_waituntil(isc_condition_t *c, isc_mutex_t *m, isc_time_t *t) {
                }
        } while (presult == EINTR);
 
-       strerror_r(presult, strbuf, sizeof(strbuf));
-       UNEXPECTED_ERROR("pthread_cond_timedwait() returned %s", strbuf);
+       UNEXPECTED_SYSERROR(presult, "pthread_cond_timedwait()");
        return (ISC_R_UNEXPECTED);
 }
index 54e497df0fda446effa6ce64f848c8cea0aa444c..0e1dcf135b91be62e4f982cdc72bb983c2784592 100644 (file)
 
 typedef pthread_cond_t isc_condition_t;
 
-#define isc_condition_init(cond)                                \
-       if (pthread_cond_init(cond, NULL) != 0) {               \
-               char isc_condition_strbuf[ISC_STRERRORSIZE];    \
-               strerror_r(errno, isc_condition_strbuf,         \
-                          sizeof(isc_condition_strbuf));       \
-               isc_error_fatal(__FILE__, __LINE__,             \
-                               "pthread_cond_init failed: %s", \
-                               isc_condition_strbuf);          \
+#define isc_condition_init(cond)                              \
+       if (pthread_cond_init(cond, NULL) != 0) {             \
+               FATAL_SYSERROR(errno, "pthread_cond_init()"); \
        }
 
 #define isc_condition_wait(cp, mp)                            \
index ecebab60a28ad9cb0f0a7e9ba3bd87ff888288b7..96552bcb78d33070dffdfb745a024df3439867f3 100644 (file)
@@ -44,8 +44,4 @@ isc_error_fatal(const char *, int, const char *, ...) ISC_FORMAT_PRINTF(3, 4);
 noreturn void
 isc_error_runtimecheck(const char *, int, const char *);
 
-#define ISC_ERROR_RUNTIMECHECK(cond) \
-       ((void)((cond) ||            \
-               ((isc_error_runtimecheck)(__FILE__, __LINE__, #cond), 0)))
-
 ISC_LANG_ENDDECLS
index c454dd2b247b191efceff1e0400405c09aa9330e..b7942169ef2fa32bba85fbf4c924d62969750131 100644 (file)
@@ -25,10 +25,16 @@ ISC_LANG_BEGINDECLS
 
 typedef pthread_mutex_t isc_mutex_t;
 
-void
-isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line);
-
-#define isc_mutex_init(mp) isc__mutex_init((mp), __FILE__, __LINE__)
+int
+isc__mutex_init(isc_mutex_t *mp);
+
+#define isc_mutex_init(mp)                                            \
+       do {                                                          \
+               int _err = isc__mutex_init((mp));                     \
+               if (_err != 0) {                                      \
+                       FATAL_SYSERROR(_err, "pthread_mutex_init()"); \
+               }                                                     \
+       } while (0)
 
 #define isc_mutex_lock(mp) \
        ((pthread_mutex_lock((mp)) == 0) ? ISC_R_SUCCESS : ISC_R_UNEXPECTED)
index ccc71f07c2964162b027260b57e4b82870c45d36..66b82eb1ad06dd1c9f12fceaf745368f639afffb 100644 (file)
@@ -309,23 +309,38 @@ mock_assert(const int result, const char *const expression,
 /*
  * Errors
  */
-#include <isc/error.h> /* Contractual promise. */
+#include <isc/error.h> /* Contractual promise. */
+#include <isc/strerr.h> /* for ISC_STRERRORSIZE */
 
 #define UNEXPECTED_ERROR(...) \
        isc_error_unexpected(__FILE__, __LINE__, __VA_ARGS__)
 
 #define FATAL_ERROR(...) isc_error_fatal(__FILE__, __LINE__, __VA_ARGS__)
 
+#define REPORT_SYSERROR(report, err, fmt, ...)                             \
+       {                                                                  \
+               char _strerr[ISC_STRERRORSIZE];                            \
+               strerror_r(err, _strerr, sizeof(_strerr));                 \
+               report(__FILE__, __LINE__, fmt ": %s (%d)", ##__VA_ARGS__, \
+                      _strerr, err);                                      \
+       }
+
+#define UNEXPECTED_SYSERROR(err, ...) \
+       REPORT_SYSERROR(isc_error_unexpected, err, __VA_ARGS__)
+
+#define FATAL_SYSERROR(err, ...) \
+       REPORT_SYSERROR(isc_error_fatal, err, __VA_ARGS__)
+
 #ifdef UNIT_TESTING
 
-#define RUNTIME_CHECK(expression)                                             \
-       ((!(expression))                                                      \
-                ? (mock_assert(0, #expression, __FILE__, __LINE__), abort()) \
-                : (void)0)
+#define RUNTIME_CHECK(cond) \
+       ((cond) ? (void)0   \
+               : (mock_assert(0, #cond, __FILE__, __LINE__), abort()))
 
 #else /* UNIT_TESTING */
 
-#define RUNTIME_CHECK(cond) ISC_ERROR_RUNTIMECHECK(cond)
+#define RUNTIME_CHECK(cond) \
+       ((cond) ? (void)0 : isc_error_runtimecheck(__FILE__, __LINE__, #cond))
 
 #endif /* UNIT_TESTING */
 
index 0b050fa43db87cd250f4dee3832680d4ce2da6f9..d877d0b936405bf8c8baec02aa094b482c3a9f33 100644 (file)
@@ -40,23 +40,15 @@ initialize_attr(void) {
 }
 #endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
 
-void
-isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) {
-       int err;
-
+int
+isc__mutex_init(isc_mutex_t *mp) {
 #ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP
        isc_result_t result = ISC_R_SUCCESS;
        result = isc_once_do(&once_attr, initialize_attr);
        RUNTIME_CHECK(result == ISC_R_SUCCESS);
 
-       err = pthread_mutex_init(mp, &attr);
+       return (pthread_mutex_init(mp, &attr));
 #else  /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
-       err = pthread_mutex_init(mp, NULL);
+       return (pthread_mutex_init(mp, NULL));
 #endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */
-       if (err != 0) {
-               char strbuf[ISC_STRERRORSIZE];
-               strerror_r(err, strbuf, sizeof(strbuf));
-               isc_error_fatal(file, line, "pthread_mutex_init failed: %s",
-                               strbuf);
-       }
 }
index 2367c08f5b9fbc04581c64ff5612f4e1139b361f..4fdc87ee453e6bbf42a37aa9b360c14fe8cc40ac 100644 (file)
@@ -122,7 +122,6 @@ static isc_result_t
 try_proto(int domain) {
        int s;
        isc_result_t result = ISC_R_SUCCESS;
-       char strbuf[ISC_STRERRORSIZE];
 
        s = socket(domain, SOCK_STREAM, 0);
        if (s == -1) {
@@ -141,8 +140,7 @@ try_proto(int domain) {
 #endif /* ifdef EINVAL */
                        return (ISC_R_NOTFOUND);
                default:
-                       strerror_r(errno, strbuf, sizeof(strbuf));
-                       UNEXPECTED_ERROR("socket() failed: %s", strbuf);
+                       UNEXPECTED_SYSERROR(errno, "socket()");
                        return (ISC_R_UNEXPECTED);
                }
        }
@@ -222,7 +220,6 @@ static void
 try_ipv6only(void) {
 #ifdef IPV6_V6ONLY
        int s, on;
-       char strbuf[ISC_STRERRORSIZE];
 #endif /* ifdef IPV6_V6ONLY */
        isc_result_t result;
 
@@ -239,8 +236,7 @@ try_ipv6only(void) {
        /* check for TCP sockets */
        s = socket(PF_INET6, SOCK_STREAM, 0);
        if (s == -1) {
-               strerror_r(errno, strbuf, sizeof(strbuf));
-               UNEXPECTED_ERROR("socket() failed: %s", strbuf);
+               UNEXPECTED_SYSERROR(errno, "socket()");
                ipv6only_result = ISC_R_UNEXPECTED;
                return;
        }
@@ -256,8 +252,7 @@ try_ipv6only(void) {
        /* check for UDP sockets */
        s = socket(PF_INET6, SOCK_DGRAM, 0);
        if (s == -1) {
-               strerror_r(errno, strbuf, sizeof(strbuf));
-               UNEXPECTED_ERROR("socket() failed: %s", strbuf);
+               UNEXPECTED_SYSERROR(errno, "socket()");
                ipv6only_result = ISC_R_UNEXPECTED;
                return;
        }
@@ -286,7 +281,6 @@ initialize_ipv6only(void) {
 static void
 try_ipv6pktinfo(void) {
        int s, on;
-       char strbuf[ISC_STRERRORSIZE];
        isc_result_t result;
        int optname;
 
@@ -299,8 +293,7 @@ try_ipv6pktinfo(void) {
        /* we only use this for UDP sockets */
        s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
        if (s == -1) {
-               strerror_r(errno, strbuf, sizeof(strbuf));
-               UNEXPECTED_ERROR("socket() failed: %s", strbuf);
+               UNEXPECTED_SYSERROR(errno, "socket()");
                ipv6pktinfo_result = ISC_R_UNEXPECTED;
                return;
        }
@@ -407,11 +400,10 @@ static isc_result_t
 make_nonblock(int fd) {
        int ret;
        int flags;
-       char strbuf[ISC_STRERRORSIZE];
-#ifdef USE_FIONBIO_IOCTL
-       int on = 1;
 
-       ret = ioctl(fd, FIONBIO, (char *)&on);
+#ifdef USE_FIONBIO_IOCTL
+       flags = 1;
+       ret = ioctl(fd, FIONBIO, (char *)&flags);
 #else  /* ifdef USE_FIONBIO_IOCTL */
        flags = fcntl(fd, F_GETFL, 0);
        flags |= O_NONBLOCK;
@@ -419,14 +411,11 @@ make_nonblock(int fd) {
 #endif /* ifdef USE_FIONBIO_IOCTL */
 
        if (ret == -1) {
-               strerror_r(errno, strbuf, sizeof(strbuf));
 #ifdef USE_FIONBIO_IOCTL
-               UNEXPECTED_ERROR("ioctl(%d, FIONBIO, &on): %s", fd, strbuf);
-#else  /* ifdef USE_FIONBIO_IOCTL */
-               UNEXPECTED_ERROR("fcntl(%d, F_SETFL, %d): %s", fd, flags,
-                                strbuf);
-#endif /* ifdef USE_FIONBIO_IOCTL */
-
+               UNEXPECTED_SYSERROR(errno, "ioctl(%d, FIONBIO, &on)", fd);
+#else
+               UNEXPECTED_SYSERROR(errno, "fcntl(%d, F_SETFL, %d)", fd, flags);
+#endif
                return (ISC_R_UNEXPECTED);
        }
 
@@ -505,7 +494,6 @@ cmsgsend(int s, int level, int type, struct addrinfo *res) {
        }
 
        if (sendmsg(s, &msg, 0) < 0) {
-               int debug = ISC_LOG_DEBUG(10);
                switch (errno) {
 #ifdef ENOPROTOOPT
                case ENOPROTOOPT:
@@ -515,20 +503,17 @@ cmsgsend(int s, int level, int type, struct addrinfo *res) {
 #endif /* ifdef EOPNOTSUPP */
                case EINVAL:
                case EPERM:
-                       break;
-               default:
-                       debug = ISC_LOG_NOTICE;
-               }
-               strerror_r(errno, strbuf, sizeof(strbuf));
-               if (debug != ISC_LOG_NOTICE) {
+                       strerror_r(errno, strbuf, sizeof(strbuf));
                        isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
                                      ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(10),
                                      "sendmsg: %s", strbuf);
-               } else {
-                       UNEXPECTED_ERROR(
-                               "probing sendmsg() with %s=%02x failed: %s",
+                       break;
+               default:
+                       UNEXPECTED_SYSERROR(
+                               errno, "probing sendmsg() with %s=%02x failed",
                                (type == IP_TOS) ? "IP_TOS" : "IPV6_TCLASS",
-                               dscp, strbuf);
+                               dscp);
+                       break;
                }
                return (false);
        }
@@ -588,7 +573,6 @@ try_dscp_v4(void) {
        }
 
        s = socket(res0->ai_family, res0->ai_socktype, res0->ai_protocol);
-
        if (s == -1) {
                strerror_r(errno, strbuf, sizeof(strbuf));
                isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
index 256e93967c27fec148b6d8fca3a3cb525281512e..4c0ba06b493a782ac89ffe102756471155c17f9b 100644 (file)
@@ -227,11 +227,10 @@ isc__netmgr_create(isc_mem_t *mctx, uint32_t workers, isc_nm_t **netmgrp) {
        REQUIRE(workers > 0);
 
        if (uv_version() < MINIMAL_UV_VERSION) {
-               isc_error_fatal(__FILE__, __LINE__,
-                               "libuv version too old: running with libuv %s "
-                               "when compiled with libuv %s will lead to "
-                               "libuv failures because of unknown flags",
-                               uv_version_string(), UV_VERSION_STRING);
+               FATAL_ERROR("libuv version too old: running with libuv %s "
+                           "when compiled with libuv %s will lead to "
+                           "libuv failures because of unknown flags",
+                           uv_version_string(), UV_VERSION_STRING);
        }
 
        isc__nm_threadpool_initialize(workers);
index ada19d0ef5fea4b85717525c965749ede9d6987e..086c0c775bd448782b052206a392b4229bf30944 100644 (file)
@@ -41,10 +41,7 @@ isc_stdtime_get(isc_stdtime_t *t) {
        struct timespec ts;
 
        if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
-               char strbuf[ISC_STRERRORSIZE];
-               strerror_r(errno, strbuf, sizeof(strbuf));
-               isc_error_fatal(__FILE__, __LINE__, "clock_gettime failed: %s",
-                               strbuf);
+               FATAL_SYSERROR(errno, "clock_gettime()");
        }
 
        REQUIRE(ts.tv_sec > 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_S);
index 4c7380cac1e6e8a0583e93653d4d4f2ac56bb3ab..62953fc4a6d5f6d3bea665be427e263d2d539ef8 100644 (file)
 #define THREAD_MINSTACKSIZE (1024U * 1024)
 #endif /* ifndef THREAD_MINSTACKSIZE */
 
-#define _FATAL(r, f)                                                          \
-       {                                                                     \
-               char strbuf[ISC_STRERRORSIZE];                                \
-               strerror_r(r, strbuf, sizeof(strbuf));                        \
-               isc_error_fatal(__FILE__, __LINE__, f " failed: %s", strbuf); \
-       }
-
 void
 isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg,
                  isc_thread_t *thread) {
@@ -66,13 +59,13 @@ isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg,
        defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE)
        ret = pthread_attr_getstacksize(&attr, &stacksize);
        if (ret != 0) {
-               _FATAL(ret, "pthread_attr_getstacksize()");
+               FATAL_SYSERROR(ret, "pthread_attr_getstacksize()");
        }
 
        if (stacksize < THREAD_MINSTACKSIZE) {
                ret = pthread_attr_setstacksize(&attr, THREAD_MINSTACKSIZE);
                if (ret != 0) {
-                       _FATAL(ret, "pthread_attr_setstacksize()");
+                       FATAL_SYSERROR(ret, "pthread_attr_setstacksize()");
                }
        }
 #endif /* if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \
@@ -81,7 +74,7 @@ isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg,
        ret = pthread_create(thread, &attr, isc__trampoline_run,
                             trampoline_arg);
        if (ret != 0) {
-               _FATAL(ret, "pthread_create()");
+               FATAL_SYSERROR(ret, "pthread_create()");
        }
 
        pthread_attr_destroy(&attr);
@@ -93,7 +86,7 @@ void
 isc_thread_join(isc_thread_t thread, isc_threadresult_t *result) {
        int ret = pthread_join(thread, result);
        if (ret != 0) {
-               _FATAL(ret, "pthread_join()");
+               FATAL_SYSERROR(ret, "pthread_join()");
        }
 }
 
index a3bd27d42e3e6d70f2456608fc139968dc42e6f2..05c67ffc0acaee72f69a88ba0b53d125f67f4126 100644 (file)
@@ -24,7 +24,6 @@
 
 #include <isc/log.h>
 #include <isc/print.h>
-#include <isc/strerr.h>
 #include <isc/string.h>
 #include <isc/time.h>
 #include <isc/tm.h>
@@ -131,13 +130,11 @@ isc_time_isepoch(const isc_time_t *t) {
 static isc_result_t
 time_now(isc_time_t *t, clockid_t clock) {
        struct timespec ts;
-       char strbuf[ISC_STRERRORSIZE];
 
        REQUIRE(t != NULL);
 
        if (clock_gettime(clock, &ts) == -1) {
-               strerror_r(errno, strbuf, sizeof(strbuf));
-               UNEXPECTED_ERROR("%s", strbuf);
+               UNEXPECTED_SYSERROR(errno, "clock_gettime()");
                return (ISC_R_UNEXPECTED);
        }
 
@@ -173,15 +170,13 @@ isc_time_now(isc_time_t *t) {
 isc_result_t
 isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
        struct timespec ts;
-       char strbuf[ISC_STRERRORSIZE];
 
        REQUIRE(t != NULL);
        REQUIRE(i != NULL);
        INSIST(i->nanoseconds < NS_PER_S);
 
        if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
-               strerror_r(errno, strbuf, sizeof(strbuf));
-               UNEXPECTED_ERROR("%s", strbuf);
+               UNEXPECTED_SYSERROR(errno, "clock_gettime()");
                return (ISC_R_UNEXPECTED);
        }