2aacef
From 62686ccc4631b6a5f73722fd7f1dcaca8782431c Mon Sep 17 00:00:00 2001
2aacef
From: Lennart Poettering <lennart@poettering.net>
2aacef
Date: Tue, 22 Nov 2022 12:56:55 +0100
2aacef
Subject: [PATCH] utmp-wtmp: handle EINTR gracefully when waiting to write to
2aacef
 tty
2aacef
2aacef
(cherry picked from commit 22ecfa83123dbfa2322346ac4e25ad2193a3b10c)
2aacef
2aacef
Related: #2137584
2aacef
---
2aacef
 src/shared/utmp-wtmp.c | 11 +++++++----
2aacef
 1 file changed, 7 insertions(+), 4 deletions(-)
2aacef
2aacef
diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c
2aacef
index 20add0e81b..37a5bf7990 100644
2aacef
--- a/src/shared/utmp-wtmp.c
2aacef
+++ b/src/shared/utmp-wtmp.c
2aacef
@@ -12,6 +12,7 @@
2aacef
 #include <utmpx.h>
2aacef
 
2aacef
 #include "alloc-util.h"
2aacef
+#include "errno-util.h"
2aacef
 #include "fd-util.h"
2aacef
 #include "hostname-util.h"
2aacef
 #include "io-util.h"
2aacef
@@ -300,7 +301,7 @@ static int write_to_terminal(const char *tty, const char *message) {
2aacef
         p = message;
2aacef
         left = strlen(message);
2aacef
 
2aacef
-        end = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
2aacef
+        end = usec_add(now(CLOCK_MONOTONIC), TIMEOUT_USEC);
2aacef
 
2aacef
         while (left > 0) {
2aacef
                 ssize_t n;
2aacef
@@ -308,19 +309,21 @@ static int write_to_terminal(const char *tty, const char *message) {
2aacef
                 int k;
2aacef
 
2aacef
                 t = now(CLOCK_MONOTONIC);
2aacef
-
2aacef
                 if (t >= end)
2aacef
                         return -ETIME;
2aacef
 
2aacef
                 k = fd_wait_for_event(fd, POLLOUT, end - t);
2aacef
-                if (k < 0)
2aacef
+                if (k < 0) {
2aacef
+                        if (ERRNO_IS_TRANSIENT(k))
2aacef
+                                continue;
2aacef
                         return k;
2aacef
+                }
2aacef
                 if (k == 0)
2aacef
                         return -ETIME;
2aacef
 
2aacef
                 n = write(fd, p, left);
2aacef
                 if (n < 0) {
2aacef
-                        if (errno == EAGAIN)
2aacef
+                        if (ERRNO_IS_TRANSIENT(errno))
2aacef
                                 continue;
2aacef
 
2aacef
                         return -errno;