]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: define and use STRERROR_OR_EOF()
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Mon, 10 Oct 2022 19:19:43 +0000 (21:19 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 11 Oct 2022 14:58:21 +0000 (16:58 +0200)
src/basic/errno-util.h
src/journal-remote/journal-gatewayd.c
src/libsystemd/sd-bus/test-bus-chat.c
src/login/logind-seat.c
src/test/test-errno-util.c

index f0d24d95cb24c3f0f6bcfa60ffb82d94087c2ec7..1e2e5b9f15b5f8c0a202a293e75c6a13b8195267 100644 (file)
  * 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;
index 3e2a85ce2983c7a4f86c202cfa737a86374e996b..34def4670e9b35bb9cce29e063cfe8e9838f2932 100644 (file)
@@ -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;
         }
 
index df6dd62151d9e27faee4f4f43a49579eb68e483a..93e8ebfb1b2b1463c5d9818aefa628ea8265e71c 100644 (file)
@@ -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;
         }
 
index 43c72da11f9f8e034f2aab9ea05ab9a54083ffa5..d8ad424bfe5881883589ea762d20fb1bae087573 100644 (file)
@@ -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);
index 284f45100248ce638ec2714255fdde66fcf5928b..f858927c924813024844d89c39197bb3221e5b44 100644 (file)
@@ -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);