From f69ae8585f5ce6cd8d1e6f3ccd6c9c2cf153e846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 10 Oct 2022 21:19:43 +0200 Subject: [PATCH] tree-wide: define and use STRERROR_OR_EOF() --- src/basic/errno-util.h | 5 +++++ src/journal-remote/journal-gatewayd.c | 4 ++-- src/libsystemd/sd-bus/test-bus-chat.c | 2 +- src/login/logind-seat.c | 8 ++++---- src/test/test-errno-util.c | 6 ++++++ 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/basic/errno-util.h b/src/basic/errno-util.h index f0d24d95cb..1e2e5b9f15 100644 --- a/src/basic/errno-util.h +++ b/src/basic/errno-util.h @@ -16,6 +16,11 @@ * Note that we use the GNU variant of strerror_r() here. */ #define STRERROR(errnum) strerror_r(abs(errnum), (char[ERRNO_BUF_LEN]){}, ERRNO_BUF_LEN) +/* A helper to print an error message or message for functions that return 0 on EOF. + * Note that we can't use ({ … }) to define a temporary variable, so errnum is + * evaluated twice. */ +#define STRERROR_OR_EOF(errnum) ((errnum) != 0 ? STRERROR(errnum) : "Unexpected EOF") + static inline void _reset_errno_(int *saved_errno) { if (*saved_errno < 0) /* Invalidated by UNPROTECT_ERRNO? */ return; diff --git a/src/journal-remote/journal-gatewayd.c b/src/journal-remote/journal-gatewayd.c index 3e2a85ce29..34def4670e 100644 --- a/src/journal-remote/journal-gatewayd.c +++ b/src/journal-remote/journal-gatewayd.c @@ -256,7 +256,7 @@ static ssize_t request_reader_entries( errno = 0; k = fread(buf, 1, n, m->tmp); if (k != n) { - log_error("Failed to read from file: %s", errno != 0 ? strerror_safe(errno) : "Premature EOF"); + log_error("Failed to read from file: %s", STRERROR_OR_EOF(errno)); return MHD_CONTENT_READER_END_WITH_ERROR; } @@ -600,7 +600,7 @@ static ssize_t request_reader_fields( errno = 0; k = fread(buf, 1, n, m->tmp); if (k != n) { - log_error("Failed to read from file: %s", errno != 0 ? strerror_safe(errno) : "Premature EOF"); + log_error("Failed to read from file: %s", STRERROR_OR_EOF(errno)); return MHD_CONTENT_READER_END_WITH_ERROR; } diff --git a/src/libsystemd/sd-bus/test-bus-chat.c b/src/libsystemd/sd-bus/test-bus-chat.c index df6dd62151..93e8ebfb1b 100644 --- a/src/libsystemd/sd-bus/test-bus-chat.c +++ b/src/libsystemd/sd-bus/test-bus-chat.c @@ -308,7 +308,7 @@ static void* client1(void *p) { errno = 0; if (read(pp[0], &x, 1) <= 0) { - log_error("Failed to read from pipe: %s", errno != 0 ? strerror_safe(errno) : "early read"); + log_error("Failed to read from pipe: %s", STRERROR_OR_EOF(errno)); goto finish; } diff --git a/src/login/logind-seat.c b/src/login/logind-seat.c index 43c72da11f..d8ad424bfe 100644 --- a/src/login/logind-seat.c +++ b/src/login/logind-seat.c @@ -389,11 +389,11 @@ int seat_read_active_vt(Seat *s) { if (lseek(s->manager->console_active_fd, SEEK_SET, 0) < 0) return log_error_errno(errno, "lseek on console_active_fd failed: %m"); + errno = 0; k = read(s->manager->console_active_fd, t, sizeof(t)-1); - if (k <= 0) { - log_error("Failed to read current console: %s", k < 0 ? strerror_safe(errno) : "EOF"); - return k < 0 ? -errno : -EIO; - } + if (k <= 0) + return log_error_errno(errno ?: EIO, + "Failed to read current console: %s", STRERROR_OR_EOF(errno)); t[k] = 0; truncate_nl(t); diff --git a/src/test/test-errno-util.c b/src/test/test-errno-util.c index 284f451002..f858927c92 100644 --- a/src/test/test-errno-util.c +++ b/src/test/test-errno-util.c @@ -41,4 +41,10 @@ TEST(STRERROR) { assert_se(strstr(c, buf)); } +TEST(STRERROR_OR_ELSE) { + log_info("STRERROR_OR_ELSE(0, \"EOF\") → %s", STRERROR_OR_EOF(0)); + log_info("STRERROR_OR_ELSE(EPERM, \"EOF\") → %s", STRERROR_OR_EOF(EPERM)); + log_info("STRERROR_OR_ELSE(-EPERM, \"EOF\") → %s", STRERROR_OR_EOF(-EPERM)); +} + DEFINE_TEST_MAIN(LOG_INFO);