From: Yu Watanabe Date: Wed, 2 Aug 2023 15:53:48 +0000 (+0900) Subject: journalctl: do not add io event source for stdout if it is a file X-Git-Tag: v255-rc1~837 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f882a986c271c3de1c622df0f1586946b4a09fef;p=thirdparty%2Fsystemd.git journalctl: do not add io event source for stdout if it is a file Fixes a bug introduced by 713342d9b09d717e9942ed08bd620c9159a98fb8. Fixes #28636. Fixes https://bugzilla.redhat.com/show_bug.cgi?id=2228089. --- diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 000baae0dfe..3424819540a 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -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);