]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Add an ERRNO_CHECK() preprocessor macro
authorMichał Kępień <michal@isc.org>
Wed, 13 Jul 2022 11:19:32 +0000 (13:19 +0200)
committerMichał Kępień <michal@isc.org>
Wed, 13 Jul 2022 11:19:32 +0000 (13:19 +0200)
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.

lib/isc/include/isc/condition.h
lib/isc/include/isc/mutex.h
lib/isc/include/isc/util.h
lib/isc/mutex.c
lib/isc/thread.c

index 54e497df0fda446effa6ce64f848c8cea0aa444c..ca75f1069535454384ec8c46d49951a7f6074e2b 100644 (file)
 #include <isc/lang.h>
 #include <isc/mutex.h>
 #include <isc/result.h>
-#include <isc/strerr.h>
 #include <isc/string.h>
 #include <isc/types.h>
+#include <isc/util.h>
 
 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)                            \
index c454dd2b247b191efceff1e0400405c09aa9330e..8a8562bd8c4af0403e6511774934c07180882c1c 100644 (file)
@@ -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)
index 297cad9d100f63eb560d4f3287f2361da229cdf3..f0338c5844bb6d948d75fc0dc61990b07e30cd20 100644 (file)
@@ -309,7 +309,10 @@ mock_assert(const int result, const char *const expression,
 /*
  * Errors
  */
-#include <isc/error.h> /* Contractual promise. */
+#include <errno.h> /* for errno */
+
+#include <isc/error.h> /* Contractual promise. */
+#include <isc/strerr.h> /* 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
  */
index 0b050fa43db87cd250f4dee3832680d4ce2da6f9..a73c2761936eab1d468e86701c0fd90c8c9bcca2 100644 (file)
@@ -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);
 }
index 4c7380cac1e6e8a0583e93653d4d4f2ac56bb3ab..fef4998daba1ebdc66062c0160e4fedbc241821d 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) {
@@ -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