From: Yu Watanabe Date: Tue, 8 Jul 2025 09:19:05 +0000 (+0900) Subject: journalctl: do not fail on SIGTERM/SIGINT, or when STDOUT is disconnected X-Git-Tag: v258-rc1~104^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=390097724b36cb849ffc89caa223dc60a3762e35;p=thirdparty%2Fsystemd.git journalctl: do not fail on SIGTERM/SIGINT, or when STDOUT is disconnected The current behavior is not useful when e.g. pipefail is enabled. Let's exit cleanly in such cases. Closes #38114. --- diff --git a/src/journal/journalctl-show.c b/src/journal/journalctl-show.c index 10af3c7b367..b5e149d7de9 100644 --- a/src/journal/journalctl-show.c +++ b/src/journal/journalctl-show.c @@ -371,7 +371,7 @@ static int on_synchronize_reply( static int on_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL; Context *c = ASSERT_PTR(userdata); - int r; + int r = 0; assert(s); assert(si); @@ -381,7 +381,7 @@ static int on_signal(sd_event_source *s, const struct signalfd_siginfo *si, void goto finish; if (c->synchronize_varlink) /* Already pending? Then exit immediately, so that user can cancel the sync */ - return sd_event_exit(c->event, EXIT_SUCCESS); + goto finish; r = varlink_connect_journal(&vl); if (r < 0) { @@ -417,7 +417,7 @@ static int on_signal(sd_event_source *s, const struct signalfd_siginfo *si, void return 0; finish: - return sd_event_exit(c->event, si->ssi_signo); + return sd_event_exit(c->event, r); } static int setup_event(Context *c, int fd) { @@ -436,12 +436,12 @@ static int setup_event(Context *c, int fd) { (void) sd_event_add_signal(e, /* ret= */ NULL, SIGTERM | SD_EVENT_SIGNAL_PROCMASK, on_signal, c); (void) sd_event_add_signal(e, /* ret= */ NULL, SIGINT | SD_EVENT_SIGNAL_PROCMASK, on_signal, c); - r = sd_event_add_io(e, NULL, fd, EPOLLIN, &on_journal_event, c); + r = sd_event_add_io(e, /* ret = */ NULL, fd, EPOLLIN, &on_journal_event, c); 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)); + r = sd_event_add_io(e, /* ret = */ NULL, STDOUT_FILENO, EPOLLHUP|EPOLLERR, /* callback = */ NULL, /* userdata = */ NULL); if (r == -EPERM) /* Installing an epoll watch on a regular file doesn't work and fails with EPERM. Which is * totally OK, handle it gracefully. epoll_ctl() documents EPERM as the error returned when @@ -544,8 +544,6 @@ int action_show(char **matches) { } if (arg_follow) { - int sig; - assert(poll_fd >= 0); r = setup_event(&c, poll_fd); @@ -555,14 +553,12 @@ int action_show(char **matches) { r = sd_event_loop(c.event); if (r < 0) return r; - sig = r; r = update_cursor(c.journal); if (r < 0) return r; - /* re-send the original signal. */ - return sig; + return 0; } (void) sd_notify(/* unset_environment= */ false, "READY=1"); diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 7d138df03a0..4642cf45764 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -1189,4 +1189,4 @@ static int run(int argc, char *argv[]) { } } -DEFINE_MAIN_FUNCTION_WITH_POSITIVE_SIGNAL(run); +DEFINE_MAIN_FUNCTION(run);