diff --git a/lib/isc/app.c b/lib/isc/app.c index c60fd9a671..6720f2d27a 100644 --- a/lib/isc/app.c +++ b/lib/isc/app.c @@ -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"); } } } diff --git a/lib/isc/condition.c b/lib/isc/condition.c index 8cd765a067..dd2fdff21c 100644 --- a/lib/isc/condition.c +++ b/lib/isc/condition.c @@ -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); } diff --git a/lib/isc/include/isc/condition.h b/lib/isc/include/isc/condition.h index 54e497df0f..0e1dcf135b 100644 --- a/lib/isc/include/isc/condition.h +++ b/lib/isc/include/isc/condition.h @@ -27,14 +27,9 @@ 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) \ diff --git a/lib/isc/include/isc/error.h b/lib/isc/include/isc/error.h index ecebab60a2..96552bcb78 100644 --- a/lib/isc/include/isc/error.h +++ b/lib/isc/include/isc/error.h @@ -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 diff --git a/lib/isc/include/isc/mutex.h b/lib/isc/include/isc/mutex.h index c454dd2b24..b7942169ef 100644 --- a/lib/isc/include/isc/mutex.h +++ b/lib/isc/include/isc/mutex.h @@ -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); +int +isc__mutex_init(isc_mutex_t *mp); -#define isc_mutex_init(mp) isc__mutex_init((mp), __FILE__, __LINE__) +#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) diff --git a/lib/isc/include/isc/util.h b/lib/isc/include/isc/util.h index ccc71f07c2..66b82eb1ad 100644 --- a/lib/isc/include/isc/util.h +++ b/lib/isc/include/isc/util.h @@ -309,23 +309,38 @@ mock_assert(const int result, const char *const expression, /* * Errors */ -#include /* Contractual promise. */ +#include /* Contractual promise. */ +#include /* 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 */ diff --git a/lib/isc/mutex.c b/lib/isc/mutex.c index 0b050fa43d..d877d0b936 100644 --- a/lib/isc/mutex.c +++ b/lib/isc/mutex.c @@ -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); - } } diff --git a/lib/isc/net.c b/lib/isc/net.c index 2367c08f5b..4fdc87ee45 100644 --- a/lib/isc/net.c +++ b/lib/isc/net.c @@ -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, diff --git a/lib/isc/netmgr/netmgr.c b/lib/isc/netmgr/netmgr.c index 256e93967c..4c0ba06b49 100644 --- a/lib/isc/netmgr/netmgr.c +++ b/lib/isc/netmgr/netmgr.c @@ -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); diff --git a/lib/isc/stdtime.c b/lib/isc/stdtime.c index ada19d0ef5..086c0c775b 100644 --- a/lib/isc/stdtime.c +++ b/lib/isc/stdtime.c @@ -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); diff --git a/lib/isc/thread.c b/lib/isc/thread.c index 4c7380cac1..62953fc4a6 100644 --- a/lib/isc/thread.c +++ b/lib/isc/thread.c @@ -38,13 +38,6 @@ #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()"); } } diff --git a/lib/isc/time.c b/lib/isc/time.c index a3bd27d42e..05c67ffc0a 100644 --- a/lib/isc/time.c +++ b/lib/isc/time.c @@ -24,7 +24,6 @@ #include #include -#include #include #include #include @@ -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); }