From: Michał Kępień Date: Wed, 13 Jul 2022 11:19:32 +0000 (+0200) Subject: Add an ERRNO_CHECK() preprocessor macro X-Git-Tag: v9.19.4~33^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=365b47caee41f525c858a92796674f154dfd9131;p=thirdparty%2Fbind9.git Add an ERRNO_CHECK() preprocessor macro In a number of situations in pthreads-related code, a common sequence of steps is taken: if the value returned by a library function is not 0, pass errno to strerror_r(), log the string returned by the latter, and immediately abort execution. Add an ERRNO_CHECK() preprocessor macro which takes those exact steps and use it wherever (conveniently) possible. Notes: 1. The "log the return value of strerror_r() and abort" pattern is used in a number of other places that this commit does not touch; only "!= 0" checks followed by isc_error_fatal() calls with non-customized error messages are replaced here. 2. This change temporarily breaks file name & line number reporting for isc__mutex_init() errors, to prevent breaking the build. This issue will be rectified in a subsequent change. --- diff --git a/lib/isc/include/isc/condition.h b/lib/isc/include/isc/condition.h index 54e497df0fd..ca75f106953 100644 --- a/lib/isc/include/isc/condition.h +++ b/lib/isc/include/isc/condition.h @@ -21,20 +21,16 @@ #include #include #include -#include #include #include +#include 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) \ + { \ + int _ret = pthread_cond_init(cond, NULL); \ + ERRNO_CHECK(pthread_cond_init, _ret); \ } #define isc_condition_wait(cp, mp) \ diff --git a/lib/isc/include/isc/mutex.h b/lib/isc/include/isc/mutex.h index c454dd2b247..8a8562bd8c4 100644 --- a/lib/isc/include/isc/mutex.h +++ b/lib/isc/include/isc/mutex.h @@ -26,9 +26,9 @@ ISC_LANG_BEGINDECLS typedef pthread_mutex_t isc_mutex_t; void -isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line); +isc__mutex_init(isc_mutex_t *mp); -#define isc_mutex_init(mp) isc__mutex_init((mp), __FILE__, __LINE__) +#define isc_mutex_init(mp) isc__mutex_init((mp)) #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 297cad9d100..f0338c5844b 100644 --- a/lib/isc/include/isc/util.h +++ b/lib/isc/include/isc/util.h @@ -309,7 +309,10 @@ mock_assert(const int result, const char *const expression, /* * Errors */ -#include /* Contractual promise. */ +#include /* for errno */ + +#include /* Contractual promise. */ +#include /* for ISC_STRERRORSIZE */ /*% Unexpected Error */ #define UNEXPECTED_ERROR isc_error_unexpected @@ -330,6 +333,15 @@ mock_assert(const int result, const char *const expression, #endif /* UNIT_TESTING */ +/*% Runtime check which logs the error string corresponding to errno */ +#define ERRNO_CHECK(func, ret) \ + if ((ret) != 0) { \ + char _strerrorbuf[ISC_STRERRORSIZE]; \ + strerror_r(errno, _strerrorbuf, sizeof(_strerrorbuf)); \ + isc_error_fatal(__FILE__, __LINE__, "%s() failed in %s(): %s", \ + #func, __func__, _strerrorbuf); \ + } + /*% * Time */ diff --git a/lib/isc/mutex.c b/lib/isc/mutex.c index 0b050fa43db..a73c2761936 100644 --- a/lib/isc/mutex.c +++ b/lib/isc/mutex.c @@ -41,7 +41,7 @@ initialize_attr(void) { #endif /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */ void -isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) { +isc__mutex_init(isc_mutex_t *mp) { int err; #ifdef HAVE_PTHREAD_MUTEX_ADAPTIVE_NP @@ -53,10 +53,5 @@ isc__mutex_init(isc_mutex_t *mp, const char *file, unsigned int line) { #else /* HAVE_PTHREAD_MUTEX_ADAPTIVE_NP */ err = 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); - } + ERRNO_CHECK(pthread_mutex_init, err); } diff --git a/lib/isc/thread.c b/lib/isc/thread.c index 4c7380cac1e..fef4998daba 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) { @@ -65,24 +58,18 @@ isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg, #if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \ defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) ret = pthread_attr_getstacksize(&attr, &stacksize); - if (ret != 0) { - _FATAL(ret, "pthread_attr_getstacksize()"); - } + ERRNO_CHECK(pthread_attr_getstacksize, ret); if (stacksize < THREAD_MINSTACKSIZE) { ret = pthread_attr_setstacksize(&attr, THREAD_MINSTACKSIZE); - if (ret != 0) { - _FATAL(ret, "pthread_attr_setstacksize()"); - } + ERRNO_CHECK(pthread_attr_setstacksize, ret); } #endif /* if defined(HAVE_PTHREAD_ATTR_GETSTACKSIZE) && \ * defined(HAVE_PTHREAD_ATTR_SETSTACKSIZE) */ ret = pthread_create(thread, &attr, isc__trampoline_run, trampoline_arg); - if (ret != 0) { - _FATAL(ret, "pthread_create()"); - } + ERRNO_CHECK(pthread_create, ret); pthread_attr_destroy(&attr); @@ -92,9 +79,8 @@ isc_thread_create(isc_threadfunc_t func, isc_threadarg_t arg, 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()"); - } + + ERRNO_CHECK(pthread_join, ret); } void