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