b7dd4d
From 67d3aade15bede6b162e8bfe88db60311efb0d1f Mon Sep 17 00:00:00 2001
b7dd4d
From: Lennart Poettering <lennart@poettering.net>
b7dd4d
Date: Wed, 24 Oct 2018 21:49:52 +0200
b7dd4d
Subject: [PATCH] journalctl: in --follow mode watch stdout for POLLHUP/POLLERR
b7dd4d
 and exit
b7dd4d
b7dd4d
Fixes: #9374
b7dd4d
(cherry picked from commit 2a1e0f2228bbdfbc18635e959f47df7da50b62fe)
b7dd4d
b7dd4d
Resolves: #2003236
b7dd4d
---
b7dd4d
 src/journal/journalctl.c | 65 +++++++++++++++++++++++++++++-----------
b7dd4d
 1 file changed, 48 insertions(+), 17 deletions(-)
b7dd4d
b7dd4d
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
b7dd4d
index 56b1be530d..fa83dce562 100644
b7dd4d
--- a/src/journal/journalctl.c
b7dd4d
+++ b/src/journal/journalctl.c
b7dd4d
@@ -2064,14 +2064,46 @@ static int sync_journal(void) {
b7dd4d
         return send_signal_and_wait(SIGRTMIN+1, "/run/systemd/journal/synced");
b7dd4d
 }
b7dd4d
 
b7dd4d
-int main(int argc, char *argv[]) {
b7dd4d
+static int wait_for_change(sd_journal *j, int poll_fd) {
b7dd4d
+        struct pollfd pollfds[] = {
b7dd4d
+                { .fd = poll_fd, .events = POLLIN },
b7dd4d
+                { .fd = STDOUT_FILENO },
b7dd4d
+        };
b7dd4d
+
b7dd4d
+        struct timespec ts;
b7dd4d
+        usec_t timeout;
b7dd4d
         int r;
b7dd4d
+
b7dd4d
+        assert(j);
b7dd4d
+        assert(poll_fd >= 0);
b7dd4d
+
b7dd4d
+        /* Much like sd_journal_wait() but also keeps an eye on STDOUT, and exits as soon as we see a POLLHUP on that,
b7dd4d
+         * i.e. when it is closed. */
b7dd4d
+
b7dd4d
+        r = sd_journal_get_timeout(j, &timeout);
b7dd4d
+        if (r < 0)
b7dd4d
+                return log_error_errno(r, "Failed to determine journal waiting time: %m");
b7dd4d
+
b7dd4d
+        if (ppoll(pollfds, ELEMENTSOF(pollfds), timeout == USEC_INFINITY ? NULL : timespec_store(&ts, timeout), NULL) < 0)
b7dd4d
+                return log_error_errno(errno, "Couldn't wait for journal event: %m");
b7dd4d
+
b7dd4d
+        if (pollfds[1].revents & (POLLHUP|POLLERR)) { /* STDOUT has been closed? */
b7dd4d
+                log_debug("Standard output has been closed.");
b7dd4d
+                return -ECANCELED;
b7dd4d
+        }
b7dd4d
+
b7dd4d
+        r = sd_journal_process(j);
b7dd4d
+        if (r < 0)
b7dd4d
+                return log_error_errno(r, "Failed to process journal events: %m");
b7dd4d
+
b7dd4d
+        return 0;
b7dd4d
+}
b7dd4d
+
b7dd4d
+int main(int argc, char *argv[]) {
b7dd4d
+        bool previous_boot_id_valid = false, first_line = true, ellipsized = false, need_seek = false;
b7dd4d
         _cleanup_(sd_journal_closep) sd_journal *j = NULL;
b7dd4d
-        bool need_seek = false;
b7dd4d
         sd_id128_t previous_boot_id;
b7dd4d
-        bool previous_boot_id_valid = false, first_line = true;
b7dd4d
-        int n_shown = 0;
b7dd4d
-        bool ellipsized = false;
b7dd4d
+        int n_shown = 0, r, poll_fd = -1;
b7dd4d
 
b7dd4d
         setlocale(LC_ALL, "");
b7dd4d
         log_parse_environment();
b7dd4d
@@ -2391,15 +2423,15 @@ int main(int argc, char *argv[]) {
b7dd4d
 
b7dd4d
         /* Opening the fd now means the first sd_journal_wait() will actually wait */
b7dd4d
         if (arg_follow) {
b7dd4d
-                r = sd_journal_get_fd(j);
b7dd4d
-                if (r == -EMFILE) {
b7dd4d
-                        log_warning("Insufficent watch descriptors available. Reverting to -n.");
b7dd4d
+                poll_fd = sd_journal_get_fd(j);
b7dd4d
+                if (poll_fd == -EMFILE) {
b7dd4d
+                        log_warning_errno(poll_fd, "Insufficent watch descriptors available. Reverting to -n.");
b7dd4d
                         arg_follow = false;
b7dd4d
-                } else if (r == -EMEDIUMTYPE) {
b7dd4d
-                        log_error_errno(r, "The --follow switch is not supported in conjunction with reading from STDIN.");
b7dd4d
+                } else if (poll_fd == -EMEDIUMTYPE) {
b7dd4d
+                        log_error_errno(poll_fd, "The --follow switch is not supported in conjunction with reading from STDIN.");
b7dd4d
                         goto finish;
b7dd4d
-                } else if (r < 0) {
b7dd4d
-                        log_error_errno(r, "Failed to get journal fd: %m");
b7dd4d
+                } else if (poll_fd < 0) {
b7dd4d
+                        log_error_errno(poll_fd, "Failed to get journal fd: %m");
b7dd4d
                         goto finish;
b7dd4d
                 }
b7dd4d
         }
b7dd4d
@@ -2621,7 +2653,7 @@ int main(int argc, char *argv[]) {
b7dd4d
                         need_seek = true;
b7dd4d
                         if (r == -EADDRNOTAVAIL)
b7dd4d
                                 break;
b7dd4d
-                        else if (r < 0 || ferror(stdout))
b7dd4d
+                        else if (r < 0)
b7dd4d
                                 goto finish;
b7dd4d
 
b7dd4d
                         n_shown++;
b7dd4d
@@ -2659,11 +2691,10 @@ int main(int argc, char *argv[]) {
b7dd4d
                 }
b7dd4d
 
b7dd4d
                 fflush(stdout);
b7dd4d
-                r = sd_journal_wait(j, (uint64_t) -1);
b7dd4d
-                if (r < 0) {
b7dd4d
-                        log_error_errno(r, "Couldn't wait for journal event: %m");
b7dd4d
+
b7dd4d
+                r = wait_for_change(j, poll_fd);
b7dd4d
+                if (r < 0)
b7dd4d
                         goto finish;
b7dd4d
-                }
b7dd4d
 
b7dd4d
                 first_line = false;
b7dd4d
         }