2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-09-01 06:55:30 +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; int presult;
isc_result_t result; isc_result_t result;
struct timespec ts; struct timespec ts;
char strbuf[ISC_STRERRORSIZE];
REQUIRE(c != NULL && m != NULL && t != NULL); 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); } while (presult == EINTR);
strerror_r(presult, strbuf, sizeof(strbuf)); UNEXPECTED_SYSERROR(presult, "pthread_cond_timedwait()");
UNEXPECTED_ERROR("pthread_cond_timedwait() returned %s", strbuf);
return (ISC_R_UNEXPECTED); 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 noreturn void
isc_error_runtimecheck(const char *, int, const char *); 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 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 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 #ifdef UNIT_TESTING
#define RUNTIME_CHECK(expression) \ #define RUNTIME_CHECK(cond) \
((!(expression)) \ ((cond) ? (void)0 \
? (mock_assert(0, #expression, __FILE__, __LINE__), abort()) \ : (mock_assert(0, #cond, __FILE__, __LINE__), abort()))
: (void)0)
#else /* UNIT_TESTING */ #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 */ #endif /* UNIT_TESTING */
@@ -339,11 +353,7 @@ mock_assert(const int result, const char *const expression,
*/ */
#define PTHREADS_RUNTIME_CHECK(func, ret) \ #define PTHREADS_RUNTIME_CHECK(func, ret) \
if ((ret) != 0) { \ if ((ret) != 0) { \
char _strerrorbuf[ISC_STRERRORSIZE]; \ FATAL_SYSERROR(ret, "%s(): %s()", __func__, #func); \
strerror_r(ret, _strerrorbuf, sizeof(_strerrorbuf)); \
isc_error_fatal(__FILE__, __LINE__, \
"%s(): %s() failed with error %d (%s)", \
__func__, #func, ret, _strerrorbuf); \
} }
/*% /*%

View File

@@ -48,8 +48,7 @@
#define UV_RUNTIME_CHECK(func, ret) \ #define UV_RUNTIME_CHECK(func, ret) \
if (ret != 0) { \ if (ret != 0) { \
isc_error_fatal(__FILE__, __LINE__, "%s failed: %s\n", #func, \ FATAL_ERROR("%s failed: %s\n", #func, uv_strerror(ret)); \
uv_strerror(ret)); \
} }
#define isc_uverr2result(x) \ #define isc_uverr2result(x) \

View File

@@ -45,14 +45,10 @@
static void static void
ignore_signal(int sig, void (*handler)(int)) { 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) { if (sigfillset(&sa.sa_mask) != 0 || sigaction(sig, &sa, NULL) < 0) {
char strbuf[ISC_STRERRORSIZE]; FATAL_SYSERROR(errno, "ignore_signal(%d)", sig);
strerror_r(errno, strbuf, sizeof(strbuf));
isc_error_fatal(__FILE__, __LINE__, "%s() %d setup: %s",
__func__, sig, strbuf);
} }
} }

View File

@@ -122,7 +122,6 @@ static isc_result_t
try_proto(int domain) { try_proto(int domain) {
int s; int s;
isc_result_t result = ISC_R_SUCCESS; isc_result_t result = ISC_R_SUCCESS;
char strbuf[ISC_STRERRORSIZE];
s = socket(domain, SOCK_STREAM, 0); s = socket(domain, SOCK_STREAM, 0);
if (s == -1) { if (s == -1) {
@@ -141,8 +140,7 @@ try_proto(int domain) {
#endif /* ifdef EINVAL */ #endif /* ifdef EINVAL */
return (ISC_R_NOTFOUND); return (ISC_R_NOTFOUND);
default: default:
strerror_r(errno, strbuf, sizeof(strbuf)); UNEXPECTED_SYSERROR(errno, "socket()");
UNEXPECTED_ERROR("socket() failed: %s", strbuf);
return (ISC_R_UNEXPECTED); return (ISC_R_UNEXPECTED);
} }
} }
@@ -222,7 +220,6 @@ static void
try_ipv6only(void) { try_ipv6only(void) {
#ifdef IPV6_V6ONLY #ifdef IPV6_V6ONLY
int s, on; int s, on;
char strbuf[ISC_STRERRORSIZE];
#endif /* ifdef IPV6_V6ONLY */ #endif /* ifdef IPV6_V6ONLY */
isc_result_t result; isc_result_t result;
@@ -239,8 +236,7 @@ try_ipv6only(void) {
/* check for TCP sockets */ /* check for TCP sockets */
s = socket(PF_INET6, SOCK_STREAM, 0); s = socket(PF_INET6, SOCK_STREAM, 0);
if (s == -1) { if (s == -1) {
strerror_r(errno, strbuf, sizeof(strbuf)); UNEXPECTED_SYSERROR(errno, "socket()");
UNEXPECTED_ERROR("socket() failed: %s", strbuf);
ipv6only_result = ISC_R_UNEXPECTED; ipv6only_result = ISC_R_UNEXPECTED;
return; return;
} }
@@ -256,8 +252,7 @@ try_ipv6only(void) {
/* check for UDP sockets */ /* check for UDP sockets */
s = socket(PF_INET6, SOCK_DGRAM, 0); s = socket(PF_INET6, SOCK_DGRAM, 0);
if (s == -1) { if (s == -1) {
strerror_r(errno, strbuf, sizeof(strbuf)); UNEXPECTED_SYSERROR(errno, "socket()");
UNEXPECTED_ERROR("socket() failed: %s", strbuf);
ipv6only_result = ISC_R_UNEXPECTED; ipv6only_result = ISC_R_UNEXPECTED;
return; return;
} }
@@ -285,7 +280,6 @@ initialize_ipv6only(void) {
static void static void
try_ipv6pktinfo(void) { try_ipv6pktinfo(void) {
int s, on; int s, on;
char strbuf[ISC_STRERRORSIZE];
isc_result_t result; isc_result_t result;
int optname; int optname;
@@ -298,8 +292,7 @@ try_ipv6pktinfo(void) {
/* we only use this for UDP sockets */ /* we only use this for UDP sockets */
s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP); s = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (s == -1) { if (s == -1) {
strerror_r(errno, strbuf, sizeof(strbuf)); UNEXPECTED_SYSERROR(errno, "socket()");
UNEXPECTED_ERROR("socket() failed: %s", strbuf);
ipv6pktinfo_result = ISC_R_UNEXPECTED; ipv6pktinfo_result = ISC_R_UNEXPECTED;
return; return;
} }
@@ -405,11 +398,10 @@ static isc_result_t
make_nonblock(int fd) { make_nonblock(int fd) {
int ret; int ret;
int flags; 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 */ #else /* ifdef USE_FIONBIO_IOCTL */
flags = fcntl(fd, F_GETFL, 0); flags = fcntl(fd, F_GETFL, 0);
flags |= O_NONBLOCK; flags |= O_NONBLOCK;
@@ -417,14 +409,11 @@ make_nonblock(int fd) {
#endif /* ifdef USE_FIONBIO_IOCTL */ #endif /* ifdef USE_FIONBIO_IOCTL */
if (ret == -1) { if (ret == -1) {
strerror_r(errno, strbuf, sizeof(strbuf));
#ifdef USE_FIONBIO_IOCTL #ifdef USE_FIONBIO_IOCTL
UNEXPECTED_ERROR("ioctl(%d, FIONBIO, &on): %s", fd, strbuf); UNEXPECTED_SYSERROR(errno, "ioctl(%d, FIONBIO, &on)", fd);
#else /* ifdef USE_FIONBIO_IOCTL */ #else
UNEXPECTED_ERROR("fcntl(%d, F_SETFL, %d): %s", fd, flags, UNEXPECTED_SYSERROR(errno, "fcntl(%d, F_SETFL, %d)", fd, flags);
strbuf); #endif
#endif /* ifdef USE_FIONBIO_IOCTL */
return (ISC_R_UNEXPECTED); return (ISC_R_UNEXPECTED);
} }
@@ -503,7 +492,6 @@ cmsgsend(int s, int level, int type, struct addrinfo *res) {
} }
if (sendmsg(s, &msg, 0) < 0) { if (sendmsg(s, &msg, 0) < 0) {
int debug = ISC_LOG_DEBUG(10);
switch (errno) { switch (errno) {
#ifdef ENOPROTOOPT #ifdef ENOPROTOOPT
case ENOPROTOOPT: case ENOPROTOOPT:
@@ -513,20 +501,17 @@ cmsgsend(int s, int level, int type, struct addrinfo *res) {
#endif /* ifdef EOPNOTSUPP */ #endif /* ifdef EOPNOTSUPP */
case EINVAL: case EINVAL:
case EPERM: case EPERM:
break;
default:
debug = ISC_LOG_NOTICE;
}
strerror_r(errno, strbuf, sizeof(strbuf)); strerror_r(errno, strbuf, sizeof(strbuf));
if (debug != ISC_LOG_NOTICE) {
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(10), ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(10),
"sendmsg: %s", strbuf); "sendmsg: %s", strbuf);
} else { break;
UNEXPECTED_ERROR( default:
"probing sendmsg() with %s=%02x failed: %s", UNEXPECTED_SYSERROR(
errno, "probing sendmsg() with %s=%02x failed",
(type == IP_TOS) ? "IP_TOS" : "IPV6_TCLASS", (type == IP_TOS) ? "IP_TOS" : "IPV6_TCLASS",
dscp, strbuf); dscp);
break;
} }
return (false); return (false);
} }
@@ -586,7 +571,6 @@ try_dscp_v4(void) {
} }
s = socket(res0->ai_family, res0->ai_socktype, res0->ai_protocol); s = socket(res0->ai_family, res0->ai_socktype, res0->ai_protocol);
if (s == -1) { if (s == -1) {
strerror_r(errno, strbuf, sizeof(strbuf)); strerror_r(errno, strbuf, sizeof(strbuf));
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL, isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,

View File

@@ -198,8 +198,7 @@ isc_netmgr_create(isc_mem_t *mctx, isc_loopmgr_t *loopmgr, isc_nm_t **netmgrp) {
isc_nm_t *netmgr = NULL; isc_nm_t *netmgr = NULL;
if (uv_version() < MINIMAL_UV_VERSION) { if (uv_version() < MINIMAL_UV_VERSION) {
isc_error_fatal(__FILE__, __LINE__, FATAL_ERROR("libuv version too old: running with libuv %s "
"libuv version too old: running with libuv %s "
"when compiled with libuv %s will lead to " "when compiled with libuv %s will lead to "
"libuv failures because of unknown flags", "libuv failures because of unknown flags",
uv_version_string(), UV_VERSION_STRING); uv_version_string(), UV_VERSION_STRING);

View File

@@ -41,10 +41,7 @@ isc_stdtime_get(isc_stdtime_t *t) {
struct timespec ts; struct timespec ts;
if (clock_gettime(CLOCKSOURCE, &ts) == -1) { if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
char strbuf[ISC_STRERRORSIZE]; FATAL_SYSERROR(errno, "clock_gettime()");
strerror_r(errno, strbuf, sizeof(strbuf));
isc_error_fatal(__FILE__, __LINE__, "clock_gettime failed: %s",
strbuf);
} }
REQUIRE(ts.tv_sec > 0 && ts.tv_nsec >= 0 && ts.tv_nsec < NS_PER_S); 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); REQUIRE(t != NULL);
if (clock_gettime(clock, &ts) == -1) { if (clock_gettime(clock, &ts) == -1) {
char strbuf[ISC_STRERRORSIZE]; UNEXPECTED_SYSERROR(errno, "clock_gettime()");
strerror_r(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR("%s", strbuf);
return (ISC_R_UNEXPECTED); 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); INSIST(i->nanoseconds < NS_PER_S);
if (clock_gettime(CLOCKSOURCE, &ts) == -1) { if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
char strbuf[ISC_STRERRORSIZE]; UNEXPECTED_SYSERROR(errno, "clock_gettime()");
strerror_r(errno, strbuf, sizeof(strbuf));
UNEXPECTED_ERROR("%s", strbuf);
return (ISC_R_UNEXPECTED); return (ISC_R_UNEXPECTED);
} }