1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-21 02:50:18 +03:00

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.
This commit is contained in:
Lennart Poettering 2025-02-28 09:35:24 +01:00
parent 649c63d6ac
commit 19ade24464
4 changed files with 59 additions and 14 deletions

View File

@ -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);

View File

@ -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, " ");

View File

@ -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;
}

View File

@ -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);
}