From: Lennart Poettering Date: Fri, 28 Feb 2025 08:35:24 +0000 (+0100) Subject: notify-recv: add notify_recv() flavour that returns a split up strv instead of he... X-Git-Tag: v258-rc1~1218^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=19ade24464017fb5caa7c5f9c0a484e72541988e;p=thirdparty%2Fsystemd.git notify-recv: add notify_recv() flavour that returns a split up strv instead of he message text as string This is useful at various places, since we split up the message as first thing there anyway. --- diff --git a/src/core/manager.c b/src/core/manager.c index 90d741e563c..36217ead14a 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -2798,7 +2798,6 @@ static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t Manager *m = ASSERT_PTR(userdata); _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; struct ucred ucred; - _cleanup_free_ char *buf = NULL; _cleanup_(fdset_free_asyncp) FDSet *fds = NULL; int r; @@ -2809,7 +2808,8 @@ static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t return 0; } - r = notify_recv_with_fds(m->notify_fd, &buf, &ucred, &pidref, &fds); + _cleanup_strv_free_ char **tags = NULL; + r = notify_recv_with_fds_strv(m->notify_fd, &tags, &ucred, &pidref, &fds); if (r == -EAGAIN) return 0; if (r < 0) @@ -2819,12 +2819,6 @@ static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t * socket. */ return r; - _cleanup_strv_free_ char **tags = strv_split_newlines(buf); - if (!tags) { - log_oom_warning(); - return 0; - } - /* Possibly a barrier fd, let's see. */ if (manager_process_barrier_fd(tags, fds)) { log_debug("Received barrier notification message from PID " PID_FMT ".", pidref.pid); diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 91bc8e8b0e1..92724e53f87 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -4622,8 +4622,8 @@ static int nspawn_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t r assert(userdata); _cleanup_(pidref_done) PidRef sender_pid = PIDREF_NULL; - _cleanup_free_ char *buf = NULL; - r = notify_recv(fd, &buf, /* ret_ucred= */ NULL, &sender_pid); + _cleanup_strv_free_ char **tags = NULL; + r = notify_recv_strv(fd, &tags, /* ret_ucred= */ NULL, &sender_pid); if (r == -EAGAIN) return 0; if (r < 0) @@ -4634,10 +4634,6 @@ static int nspawn_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t r return 0; } - _cleanup_strv_free_ char **tags = strv_split(buf, "\n\r"); - if (!tags) - return log_oom(); - if (DEBUG_LOGGING) { _cleanup_free_ char *joined = strv_join(tags, " "); diff --git a/src/shared/notify-recv.c b/src/shared/notify-recv.c index ce12ec8e01c..08719a20bb4 100644 --- a/src/shared/notify-recv.c +++ b/src/shared/notify-recv.c @@ -4,6 +4,8 @@ #include "fd-util.h" #include "notify-recv.h" #include "socket-util.h" +#include "strv.h" +#include "user-util.h" int notify_recv_with_fds( int fd, @@ -140,3 +142,45 @@ int notify_recv_with_fds( return 0; } + +int notify_recv_with_fds_strv( + int fd, + char ***ret_list, + struct ucred *ret_ucred, + PidRef *ret_pidref, + FDSet **ret_fds) { + + _cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; + _cleanup_(fdset_free_asyncp) FDSet *fds = NULL; + struct ucred ucred = UCRED_INVALID; + _cleanup_free_ char *text = NULL; + int r; + + r = notify_recv_with_fds( + fd, + ret_list ? &text : NULL, + ret_ucred ? &ucred : NULL, + ret_pidref ? &pidref : NULL, + ret_fds ? &fds : NULL); + if (r < 0) + return r; + + if (ret_list) { + char **l = strv_split_newlines(text); + if (!l) { + log_oom_warning(); + return -EAGAIN; + } + + *ret_list = l; + } + + if (ret_ucred) + *ret_ucred = ucred; + if (ret_pidref) + *ret_pidref = TAKE_PIDREF(pidref); + if (ret_fds) + *ret_fds = TAKE_PTR(fds); + + return 0; +} diff --git a/src/shared/notify-recv.h b/src/shared/notify-recv.h index 0a38fd28b4e..f4739d8c120 100644 --- a/src/shared/notify-recv.h +++ b/src/shared/notify-recv.h @@ -16,3 +16,14 @@ int notify_recv_with_fds( static inline int notify_recv(int fd, char **ret_text, struct ucred *ret_ucred, PidRef *ret_pidref) { return notify_recv_with_fds(fd, ret_text, ret_ucred, ret_pidref, NULL); } + +int notify_recv_with_fds_strv( + int fd, + char ***ret_list, + struct ucred *ret_ucred, + PidRef *ret_pidref, + FDSet **ret_fds); + +static inline int notify_recv_strv(int fd, char ***ret_list, struct ucred *ret_ucred, PidRef *ret_pidref) { + return notify_recv_with_fds_strv(fd, ret_list, ret_ucred, ret_pidref, NULL); +}