From 0de343187127f6a5a93602608812e60fc4092c9a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 3 May 2023 14:13:58 +0200 Subject: [PATCH] sd-daemon: add sd_pid_notify_barrier() call and use it in systemd-notify Previously we'd honour --pid= from the main notification we send, but not from the barrier. This is confusing at best. Let's fix that. --- man/sd_notify.xml | 14 ++++++++++++-- src/libsystemd/libsystemd.sym | 1 + src/libsystemd/sd-daemon/sd-daemon.c | 8 ++++++-- src/notify/notify.c | 2 +- src/systemd/sd-daemon.h | 5 +++++ 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/man/sd_notify.xml b/man/sd_notify.xml index b82d145860a..89f1729b13f 100644 --- a/man/sd_notify.xml +++ b/man/sd_notify.xml @@ -24,6 +24,7 @@ sd_pid_notify_with_fds sd_pid_notifyf_with_fds sd_notify_barrier + sd_pid_notify_barrier Notify service manager about start-up completion and other service status changes @@ -83,6 +84,13 @@ int unset_environment uint64_t timeout + + + int sd_pid_notify_barrier + pid_t pid + int unset_environment + uint64_t timeout + @@ -372,8 +380,10 @@ previously sent notification messages and uses the BARRIER=1 command. It takes a relative timeout value in microseconds which is passed to ppoll2 - . A value of UINT64_MAX is interpreted as infinite timeout. - + . A value of UINT64_MAX is interpreted as infinite timeout. + + sd_pid_notify_barrier() is just like sd_notify_barrier(), + but allows specifying the originating PID for the notification message. diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index 75c8f2225b0..8b99d0a7aab 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -823,4 +823,5 @@ global: sd_event_source_set_memory_pressure_type; sd_event_source_set_memory_pressure_period; sd_event_trim_memory; + sd_pid_notify_barrier; } LIBSYSTEMD_253; diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c index 34525b0a7b7..8538b7ab82d 100644 --- a/src/libsystemd/sd-daemon/sd-daemon.c +++ b/src/libsystemd/sd-daemon/sd-daemon.c @@ -601,14 +601,14 @@ finish: return r; } -_public_ int sd_notify_barrier(int unset_environment, uint64_t timeout) { +_public_ int sd_pid_notify_barrier(pid_t pid, int unset_environment, uint64_t timeout) { _cleanup_close_pair_ int pipe_fd[2] = PIPE_EBADF; int r; if (pipe2(pipe_fd, O_CLOEXEC) < 0) return -errno; - r = sd_pid_notify_with_fds(0, unset_environment, "BARRIER=1", &pipe_fd[1], 1); + r = sd_pid_notify_with_fds(pid, unset_environment, "BARRIER=1", &pipe_fd[1], 1); if (r <= 0) return r; @@ -623,6 +623,10 @@ _public_ int sd_notify_barrier(int unset_environment, uint64_t timeout) { return 1; } +_public_ int sd_notify_barrier(int unset_environment, uint64_t timeout) { + return sd_pid_notify_barrier(0, unset_environment, timeout); +} + _public_ int sd_pid_notify(pid_t pid, int unset_environment, const char *state) { return sd_pid_notify_with_fds(pid, unset_environment, state, NULL, 0); } diff --git a/src/notify/notify.c b/src/notify/notify.c index 21e004e4171..8a551a9399c 100644 --- a/src/notify/notify.c +++ b/src/notify/notify.c @@ -447,7 +447,7 @@ static int run(int argc, char* argv[]) { arg_fds = fdset_free(arg_fds); /* Close before we execute anything */ if (!arg_no_block) { - r = sd_notify_barrier(0, 5 * USEC_PER_SEC); + r = sd_pid_notify_barrier(source_pid, /* unset_environment= */ false, 5 * USEC_PER_SEC); if (r < 0) return log_error_errno(r, "Failed to invoke barrier: %m"); if (r == 0) diff --git a/src/systemd/sd-daemon.h b/src/systemd/sd-daemon.h index a8b40ff1f8c..2d095f72df4 100644 --- a/src/systemd/sd-daemon.h +++ b/src/systemd/sd-daemon.h @@ -308,6 +308,11 @@ int sd_pid_notifyf_with_fds(pid_t pid, int unset_environment, const int *fds, si */ int sd_notify_barrier(int unset_environment, uint64_t timeout); +/* + Just like sd_notify_barrier() but also takes a PID to send the barrier message from. +*/ +int sd_pid_notify_barrier(pid_t pid, int unset_environment, uint64_t timeout); + /* Returns > 0 if the system was booted with systemd. Returns < 0 on error. Returns 0 if the system was not booted with systemd. Note -- 2.47.3