]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
notify-recv: add notify_recv() flavour that returns a split up strv instead of he...
authorLennart Poettering <lennart@poettering.net>
Fri, 28 Feb 2025 08:35:24 +0000 (09:35 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 28 Feb 2025 13:17:52 +0000 (14:17 +0100)
This is useful at various places, since we split up the message as first
thing there anyway.

src/core/manager.c
src/nspawn/nspawn.c
src/shared/notify-recv.c
src/shared/notify-recv.h

index 90d741e563cec921bcbbf3a9d5d1e08105e7df28..36217ead14a489196ce5b3a5dadbc5de49f8b744 100644 (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);
index 91bc8e8b0e1cce0b070366ea38ec9c27624a90a8..92724e53f87749c43ad0218c52f2919975520d78 100644 (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, " ");
 
index ce12ec8e01c3e322cc2ba5019409549a34c37365..08719a20bb45f92f61509443f6ec992f684d8224 100644 (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;
+}
index 0a38fd28b4e0d56720cf5cbcfcbc45e9bed872dc..f4739d8c120c7720cce35012b8903e50b6a9063b 100644 (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);
+}