]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: do not add io event source for stdout if it is a file
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 2 Aug 2023 15:53:48 +0000 (00:53 +0900)
committerLuca Boccassi <luca.boccassi@gmail.com>
Wed, 2 Aug 2023 21:04:13 +0000 (22:04 +0100)
Fixes a bug introduced by 713342d9b09d717e9942ed08bd620c9159a98fb8.

Fixes #28636.
Fixes https://bugzilla.redhat.com/show_bug.cgi?id=2228089.

src/journal/journalctl.c

index 000baae0dfe6d08b1b2a114fdc317753e55ce11e..3424819540aaede7d57916d4d16ff9c61e6369ec 100644 (file)
@@ -2310,6 +2310,7 @@ static int on_signal(sd_event_source *s, const struct signalfd_siginfo *si, void
 
 static int setup_event(Context *c, int fd, sd_event **ret) {
         _cleanup_(sd_event_unrefp) sd_event *e = NULL;
+        struct stat st;
         int r;
 
         assert(arg_follow);
@@ -2328,10 +2329,15 @@ static int setup_event(Context *c, int fd, sd_event **ret) {
         if (r < 0)
                 return log_error_errno(r, "Failed to add io event source for journal: %m");
 
-        /* Also keeps an eye on STDOUT, and exits as soon as we see a POLLHUP on that, i.e. when it is closed. */
-        r = sd_event_add_io(e, NULL, STDOUT_FILENO, EPOLLHUP|EPOLLERR, NULL, INT_TO_PTR(-ECANCELED));
-        if (r < 0)
-                return log_error_errno(r, "Failed to add io event source for stdout: %m");
+        if (fstat(STDOUT_FILENO, &st) < 0)
+                return log_error_errno(r, "Failed to stat stdout: %m");
+
+        if (IN_SET(st.st_mode & S_IFMT, S_IFCHR, S_IFIFO, S_IFSOCK)) {
+                /* Also keeps an eye on STDOUT, and exits as soon as we see a POLLHUP on that, i.e. when it is closed. */
+                r = sd_event_add_io(e, NULL, STDOUT_FILENO, EPOLLHUP|EPOLLERR, NULL, INT_TO_PTR(-ECANCELED));
+                if (r < 0)
+                        return log_error_errno(r, "Failed to add io event source for stdout: %m");
+        }
 
         if (arg_lines != 0 || arg_since_set) {
                 r = sd_event_add_defer(e, NULL, on_first_event, c);