From 108dfff2c7aebadb78e485ed564caf559367bf7c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 22 Nov 2022 12:14:33 +0100 Subject: tree-wide: modernizations with RET_NERRNO() --- src/shared/ask-password-api.c | 30 +++++++++++++----------------- src/shared/barrier.c | 4 +--- 2 files changed, 14 insertions(+), 20 deletions(-) (limited to 'src/shared') diff --git a/src/shared/ask-password-api.c b/src/shared/ask-password-api.c index e7db23c201..1ad5ddd503 100644 --- a/src/shared/ask-password-api.c +++ b/src/shared/ask-password-api.c @@ -235,8 +235,7 @@ int ask_password_plymouth( if (notify < 0) return -errno; - r = inotify_add_watch(notify, flag_file, IN_ATTRIB); /* for the link count */ - if (r < 0) + if (inotify_add_watch(notify, flag_file, IN_ATTRIB) < 0) /* for the link count */ return -errno; } @@ -244,8 +243,7 @@ int ask_password_plymouth( if (fd < 0) return -errno; - r = connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); - if (r < 0) + if (connect(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) return -errno; if (FLAGS_SET(flags, ASK_PASSWORD_ACCEPT_CACHED)) { @@ -469,10 +467,9 @@ int ask_password_tty( new_termios.c_cc[VMIN] = 1; new_termios.c_cc[VTIME] = 0; - if (tcsetattr(ttyfd, TCSADRAIN, &new_termios) < 0) { - r = -errno; + r = RET_NERRNO(tcsetattr(ttyfd, TCSADRAIN, &new_termios)); + if (r < 0) goto finish; - } reset_tty = true; } @@ -496,11 +493,11 @@ int ask_password_tty( else timeout = USEC_INFINITY; - if (flag_file) - if (access(flag_file, F_OK) < 0) { - r = -errno; + if (flag_file) { + r = RET_NERRNO(access(flag_file, F_OK)); + if (r < 0) goto finish; - } + } r = ppoll_usec(pollfd, notify >= 0 ? 2 : 1, timeout); if (r == -EINTR) @@ -752,10 +749,10 @@ int ask_password_agent( r = -errno; goto finish; } - if (inotify_add_watch(notify, "/run/systemd/ask-password", IN_ATTRIB /* for mtime */) < 0) { - r = -errno; + + r = RET_NERRNO(inotify_add_watch(notify, "/run/systemd/ask-password", IN_ATTRIB /* for mtime */)); + if (r < 0) goto finish; - } } fd = mkostemp_safe(temp); @@ -818,10 +815,9 @@ int ask_password_agent( final[sizeof(final)-10] = 's'; final[sizeof(final)-9] = 'k'; - if (rename(temp, final) < 0) { - r = -errno; + r = RET_NERRNO(rename(temp, final)); + if (r < 0) goto finish; - } zero(pollfd); pollfd[FD_SOCKET].fd = socket_fd; diff --git a/src/shared/barrier.c b/src/shared/barrier.c index cbe54a60cd..d76a61a5db 100644 --- a/src/shared/barrier.c +++ b/src/shared/barrier.c @@ -92,7 +92,6 @@ */ int barrier_create(Barrier *b) { _unused_ _cleanup_(barrier_destroyp) Barrier *staging = b; - int r; assert(b); @@ -104,8 +103,7 @@ int barrier_create(Barrier *b) { if (b->them < 0) return -errno; - r = pipe2(b->pipe, O_CLOEXEC | O_NONBLOCK); - if (r < 0) + if (pipe2(b->pipe, O_CLOEXEC | O_NONBLOCK) < 0) return -errno; staging = NULL; -- cgit v1.2.3 From 6976bf5cd614761eb4bd57d39e24f7eca1d6b863 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 22 Nov 2022 12:42:46 +0100 Subject: varlink: also handle EINTR gracefully when waiting for EIO via ppoll() --- src/shared/varlink.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/shared') diff --git a/src/shared/varlink.c b/src/shared/varlink.c index 4f7ac97689..4d2cfee491 100644 --- a/src/shared/varlink.c +++ b/src/shared/varlink.c @@ -1025,7 +1025,7 @@ static void handle_revents(Varlink *v, int revents) { if ((revents & (POLLOUT|POLLHUP)) == 0) return; - varlink_log(v, "Anynchronous connection completed."); + varlink_log(v, "Asynchronous connection completed."); v->connecting = false; } else { /* Note that we don't care much about POLLIN/POLLOUT here, we'll just try reading and writing @@ -1075,6 +1075,9 @@ int varlink_wait(Varlink *v, usec_t timeout) { return events; r = fd_wait_for_event(fd, events, t); + if (r < 0 && ERRNO_IS_TRANSIENT(r)) /* Treat EINTR as not a timeout, but also nothing happened, and + * the caller gets a chance to call back into us */ + return 1; if (r <= 0) return r; @@ -1161,8 +1164,12 @@ int varlink_flush(Varlink *v) { } r = fd_wait_for_event(v->fd, POLLOUT, USEC_INFINITY); - if (r < 0) + if (r < 0) { + if (ERRNO_IS_TRANSIENT(r)) + continue; + return varlink_log_errno(v, r, "Poll failed on fd: %m"); + } assert(r != 0); -- cgit v1.2.3 From 80b780ba178a84b248ecee47eef82358480c9492 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 22 Nov 2022 12:56:38 +0100 Subject: utmp-wtmp: fix error in case isatty() fails --- src/shared/utmp-wtmp.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/shared') diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c index d2c8473c60..20add0e81b 100644 --- a/src/shared/utmp-wtmp.c +++ b/src/shared/utmp-wtmp.c @@ -292,8 +292,10 @@ static int write_to_terminal(const char *tty, const char *message) { assert(message); fd = open(tty, O_WRONLY|O_NONBLOCK|O_NOCTTY|O_CLOEXEC); - if (fd < 0 || !isatty(fd)) + if (fd < 0) return -errno; + if (!isatty(fd)) + return -ENOTTY; p = message; left = strlen(message); -- cgit v1.2.3 From 22ecfa83123dbfa2322346ac4e25ad2193a3b10c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 22 Nov 2022 12:56:55 +0100 Subject: utmp-wtmp: handle EINTR gracefully when waiting to write to tty --- src/shared/utmp-wtmp.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/shared') diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c index 20add0e81b..37a5bf7990 100644 --- a/src/shared/utmp-wtmp.c +++ b/src/shared/utmp-wtmp.c @@ -12,6 +12,7 @@ #include #include "alloc-util.h" +#include "errno-util.h" #include "fd-util.h" #include "hostname-util.h" #include "io-util.h" @@ -300,7 +301,7 @@ static int write_to_terminal(const char *tty, const char *message) { p = message; left = strlen(message); - end = now(CLOCK_MONOTONIC) + TIMEOUT_USEC; + end = usec_add(now(CLOCK_MONOTONIC), TIMEOUT_USEC); while (left > 0) { ssize_t n; @@ -308,19 +309,21 @@ static int write_to_terminal(const char *tty, const char *message) { int k; t = now(CLOCK_MONOTONIC); - if (t >= end) return -ETIME; k = fd_wait_for_event(fd, POLLOUT, end - t); - if (k < 0) + if (k < 0) { + if (ERRNO_IS_TRANSIENT(k)) + continue; return k; + } if (k == 0) return -ETIME; n = write(fd, p, left); if (n < 0) { - if (errno == EAGAIN) + if (ERRNO_IS_TRANSIENT(errno)) continue; return -errno; -- cgit v1.2.3