2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-31 14:35:26 +00:00

De-duplicate some calls to strerror_r()

Specifically, when reporting an unexpected or fatal error.
This commit is contained in:
Tony Finch
2022-10-14 17:18:07 +01:00
parent ec50c58f52
commit a34a2784b1
9 changed files with 53 additions and 78 deletions

View File

@@ -26,7 +26,6 @@ isc__condition_waituntil(pthread_cond_t *c, pthread_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(pthread_cond_t *c, pthread_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);
}

View 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

View File

@@ -320,16 +320,30 @@ mock_assert(const int result, const char *const expression,
#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 */
@@ -337,13 +351,9 @@ mock_assert(const int result, const char *const expression,
* Runtime check which logs the error value returned by a POSIX Threads
* function and the error string that corresponds to it
*/
#define PTHREADS_RUNTIME_CHECK(func, ret) \
if ((ret) != 0) { \
char _strerrorbuf[ISC_STRERRORSIZE]; \
strerror_r(ret, _strerrorbuf, sizeof(_strerrorbuf)); \
isc_error_fatal(__FILE__, __LINE__, \
"%s(): %s() failed with error %d (%s)", \
__func__, #func, ret, _strerrorbuf); \
#define PTHREADS_RUNTIME_CHECK(func, ret) \
if ((ret) != 0) { \
FATAL_SYSERROR(ret, "%s(): %s()", __func__, #func); \
}
/*%

View File

@@ -46,10 +46,9 @@
* These are used with all versions of libuv:
*/
#define UV_RUNTIME_CHECK(func, ret) \
if (ret != 0) { \
isc_error_fatal(__FILE__, __LINE__, "%s failed: %s\n", #func, \
uv_strerror(ret)); \
#define UV_RUNTIME_CHECK(func, ret) \
if (ret != 0) { \
FATAL_ERROR("%s failed: %s\n", #func, uv_strerror(ret)); \
}
#define isc_uverr2result(x) \

View File

@@ -45,14 +45,10 @@
static void
ignore_signal(int sig, void (*handler)(int)) {
struct sigaction sa;
struct sigaction sa = { .sa_handler = handler };
sa = (struct sigaction){ .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__, "%s() %d setup: %s",
__func__, sig, strbuf);
FATAL_SYSERROR(errno, "ignore_signal(%d)", sig);
}
}

View 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;
}
@@ -285,7 +280,6 @@ initialize_ipv6only(void) {
static void
try_ipv6pktinfo(void) {
int s, on;
char strbuf[ISC_STRERRORSIZE];
isc_result_t result;
int optname;
@@ -298,8 +292,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;
}
@@ -405,11 +398,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;
@@ -417,14 +409,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);
}
@@ -503,7 +492,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:
@@ -513,20 +501,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);
}
@@ -586,7 +571,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,

View File

@@ -198,11 +198,10 @@ isc_netmgr_create(isc_mem_t *mctx, isc_loopmgr_t *loopmgr, isc_nm_t **netmgrp) {
isc_nm_t *netmgr = NULL;
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);
}
netmgr = isc_mem_get(mctx, sizeof(*netmgr));

View 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);

View File

@@ -92,9 +92,7 @@ time_now(isc_time_t *t, clockid_t clock) {
REQUIRE(t != NULL);
if (clock_gettime(clock, &ts) == -1) {
char strbuf[ISC_STRERRORSIZE];
strerror_r(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR("%s", strbuf);
UNEXPECTED_SYSERROR(errno, "clock_gettime()");
return (ISC_R_UNEXPECTED);
}
@@ -136,9 +134,7 @@ isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
INSIST(i->nanoseconds < NS_PER_S);
if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
char strbuf[ISC_STRERRORSIZE];
strerror_r(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR("%s", strbuf);
UNEXPECTED_SYSERROR(errno, "clock_gettime()");
return (ISC_R_UNEXPECTED);
}