mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-08-31 22:45:39 +00:00
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.
This commit is contained in:
@@ -21,20 +21,16 @@
|
||||
#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) \
|
||||
|
@@ -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)
|
||||
|
@@ -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
|
||||
*/
|
||||
|
@@ -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);
|
||||
}
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user