From ef90afd59e8f18f78af5a4111aefaa9cea73f048 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 27 Nov 2025 13:01:59 +0100 Subject: [PATCH 1/3] journal: send READY=1 also when --lines=0 is given If --lines=0 is given, we'd skip the setup and not invoke sd_notify, potentially blocking the caller. Change the condition for the callback to also include that case. Since then the callback would always be set, the 'if' statement is not necessary anymore. --- src/journal/journalctl-show.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/journal/journalctl-show.c b/src/journal/journalctl-show.c index c48c2e47eb..4ebedb092e 100644 --- a/src/journal/journalctl-show.c +++ b/src/journal/journalctl-show.c @@ -362,7 +362,9 @@ static int on_first_event(sd_event_source *s, void *userdata) { return log_error_errno(r, "Failed to get cursor: %m"); } + /* Setup and initial processing are done, we're ready to wait for more data. */ (void) sd_notify(/* unset_environment= */ false, "READY=1"); + return 0; } @@ -472,11 +474,9 @@ static int setup_event(Context *c, int fd) { else 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); - if (r < 0) - return log_error_errno(r, "Failed to add defer event source: %m"); - } + r = sd_event_add_defer(e, NULL, on_first_event, c); + if (r < 0) + return log_error_errno(r, "Failed to add defer event source: %m"); c->event = TAKE_PTR(e); return 0; @@ -583,6 +583,7 @@ int action_show(char **matches) { return 0; } + /* Setup is done, we'll start processing data. */ (void) sd_notify(/* unset_environment= */ false, "READY=1"); r = show(&c); From f943ab6efbcdccd6f656fc868f3cf426ebdd3a33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 27 Nov 2025 13:06:47 +0100 Subject: [PATCH 2/3] systemctl: stop showing last message from previous run --lines=1 means that we show the last message about the unit, whatever it is: $ SYSTEMD_LOG_LEVEL=debug systemctl --verbose start demo2 ... Invoking 'journalctl -q --follow --no-pager --lines=1 --synchronize-on-exit=yes --unit=demo2.service' as child. ... Directory /var/log/journal/7d16833bfa924410851e2a193bcfd4ba added. Journal effective settings seal=no keyed_hash=yes compress=ZSTD compress_threshold_bytes=8B ... Reiterating files to get inotify watches established. Considering root directory '/run/log/journal'. Considering root directory '/var/log/journal'. Considering directory '/var/log/journal/7d16833bfa924410851e2a193bcfd4ba'. Nov 26 17:24:35 rawhide systemd[1]: Finished demo2.service. ... Executing dbus call org.freedesktop.systemd1.Manager StartUnit(demo2.service, replace) ... Job for demo2.service finished. Got result done/Success for job demo2.service. journal: Sending message: {"method":"io.systemd.Journal.Synchronize","parameters":{"offline":false}} Nov 27 13:05:30 rawhide systemd[1]: Starting demo2.service... Nov 27 13:05:30 rawhide systemd[1]: demo2.service: Deactivated successfully. Nov 27 13:05:30 rawhide systemd[1]: Finished demo2.service. ... We obviously should only show _new_ messages, hence change to --lines=0. This works properly after the fix in the previous commit. Fixes #39048. --- src/shared/fork-notify.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/fork-notify.c b/src/shared/fork-notify.c index 624ddab8d1..cf014c2593 100644 --- a/src/shared/fork-notify.c +++ b/src/shared/fork-notify.c @@ -219,7 +219,7 @@ int journal_fork(RuntimeScope scope, char * const* units, PidRef *ret_pidref) { "-q", "--follow", "--no-pager", - "--lines=1", + "--lines=0", "--synchronize-on-exit=yes"); if (!argv) return log_oom_debug(); From 21eb636aaad28707bd371bdfd721dea66277e1f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 27 Nov 2025 15:04:48 +0100 Subject: [PATCH 3/3] sd-notify: print a debug message when sd_notify() succeds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … and also when it fails, in the few cases where this wasn't done already. When I was debugging https://github.com/systemd/systemd/issues/39048, it was quite confusing that we print copious information about D-Bus messages and all other steps we're undertaking, but nothing about the sd-notify messages which are much more important. Add some debug messages for the cases where we didn't print anything. (The case where $NOTIFY_SOCKET is not set in left unchanged. The variable is often left unset and we don't need to spam logs in that trivial case.) --- src/libsystemd/sd-daemon/sd-daemon.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c index b5fe6dfc12..4455f7d543 100644 --- a/src/libsystemd/sd-daemon/sd-daemon.c +++ b/src/libsystemd/sd-daemon/sd-daemon.c @@ -505,13 +505,14 @@ static int pid_notify_with_fds_internal( if (r == -EPROTO) r = socket_address_parse_vsock(&address, e); if (r < 0) - return r; + return log_debug_errno(r, "Address NOTIFY_SOCKET='%s' is neither UNIX nor VSOCK, refusing: %m", e); msghdr.msg_namelen = address.size; /* If we didn't get an address (which is a normal pattern when specifying VSOCK tuples) error out, * we always require a specific CID. */ if (address.sockaddr.vm.svm_family == AF_VSOCK && address.sockaddr.vm.svm_cid == VMADDR_CID_ANY) - return -EINVAL; + return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), + "VSOCK address in NOTIFY_SOCKET='%s' doesn't have CID, refusing.", e); type = address.type == 0 ? SOCK_DGRAM : address.type; @@ -610,7 +611,8 @@ static int pid_notify_with_fds_internal( } else { /* Unless we're using SOCK_STREAM, we expect to write all the contents immediately. */ if (type != SOCK_STREAM && (size_t) n < iovec_total_size(msghdr.msg_iov, msghdr.msg_iovlen)) - return -EIO; + return log_debug_errno(SYNTHETIC_ERRNO(EIO), + "Incomplete notify message sent to '%s': %m", e); /* Make sure we only send fds and ucred once, even if we're using SOCK_STREAM. */ msghdr.msg_control = NULL; @@ -631,6 +633,7 @@ static int pid_notify_with_fds_internal( return log_debug_errno(SYNTHETIC_ERRNO(EPROTO), "Unexpectedly received data on notify socket."); } + log_debug("Notify message sent to '%s': \"%s\"", e, state); return 1; }