]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
machine: split operation initialization into two steps
authorIvan Kruglov <mail@ikruglov.com>
Tue, 17 Dec 2024 11:24:51 +0000 (12:24 +0100)
committerIvan Kruglov <mail@ikruglov.com>
Mon, 6 Jan 2025 13:41:49 +0000 (14:41 +0100)
src/machine/operation.c
src/machine/operation.h

index 00b9e092499a4c6813efe26f52598f9d5a66fb3c..9e4e7e3dceef9c87200c90300b5565f9d9560b77 100644 (file)
@@ -88,21 +88,24 @@ static int operation_done(sd_event_source *s, const siginfo_t *si, void *userdat
         return 0;
 }
 
-int operation_new(Manager *manager, Machine *machine, pid_t child, sd_bus_message *message, sd_varlink *link, int errno_fd, Operation **ret) {
+int operation_new(Manager *manager, Machine *machine, pid_t child, int errno_fd, Operation **ret) {
         Operation *o;
         int r;
 
         assert(manager);
         assert(child > 1);
         assert(errno_fd >= 0);
-        assert(message || link);
-        assert(!(message && link));
+        assert(ret);
 
-        o = new0(Operation, 1);
+        o = new(Operation, 1);
         if (!o)
                 return -ENOMEM;
 
-        o->extra_fd = -EBADF;
+        *o = (Operation) {
+                .pid = child,
+                .errno_fd = errno_fd,
+                .extra_fd = -EBADF
+        };
 
         r = sd_event_add_child(manager->event, &o->event_source, child, WEXITED, operation_done, o);
         if (r < 0) {
@@ -110,11 +113,6 @@ int operation_new(Manager *manager, Machine *machine, pid_t child, sd_bus_messag
                 return r;
         }
 
-        o->pid = child;
-        o->message = sd_bus_message_ref(message);
-        o->link = sd_varlink_ref(link);
-        o->errno_fd = errno_fd;
-
         LIST_PREPEND(operations, manager->operations, o);
         manager->n_operations++;
         o->manager = manager;
@@ -128,9 +126,7 @@ int operation_new(Manager *manager, Machine *machine, pid_t child, sd_bus_messag
 
         /* At this point we took ownership of both the child and the errno file descriptor! */
 
-        if (ret)
-                *ret = o;
-
+        *ret = o;
         return 0;
 }
 
index 75bf918c2b1cf52830f02f87c80b9d4bd54599c0..1a92847b80d490e79a3a11eb9f649e42a2ae7e96 100644 (file)
@@ -31,11 +31,69 @@ struct Operation {
         LIST_FIELDS(Operation, operations_by_machine);
 };
 
-int operation_new(Manager *manager, Machine *machine, pid_t child, sd_bus_message *message, sd_varlink *link, int errno_fd, Operation **ret);
+int operation_new(Manager *manager, Machine *machine, pid_t child, int errno_fd, Operation **ret);
 Operation *operation_free(Operation *o);
-static inline int operation_new_with_bus_reply(Manager *manager, Machine *machine, pid_t child, sd_bus_message *message, int errno_fd, Operation **ret) {
-        return operation_new(manager, machine, child, message, /* link = */ NULL, errno_fd, ret);
+
+static inline void operation_attach_bus_reply(Operation *op, sd_bus_message *message) {
+        assert(op);
+        assert(!op->message);
+        assert(!op->link);
+        assert(message);
+
+        op->message = sd_bus_message_ref(message);
+}
+
+static inline void operation_attach_varlink_reply(Operation *op, sd_varlink *link) {
+        assert(op);
+        assert(!op->message);
+        assert(!op->link);
+        assert(link);
+
+        op->link = sd_varlink_ref(link);
+}
+
+static inline int operation_new_with_bus_reply(
+                Manager *manager,
+                Machine *machine,
+                pid_t child,
+                sd_bus_message *message,
+                int errno_fd,
+                Operation **ret) {
+
+        Operation *op;
+        int r;
+
+        r = operation_new(manager, machine, child, errno_fd, &op);
+        if (r < 0)
+                return r;
+
+        operation_attach_bus_reply(op, message);
+
+        if (ret)
+                *ret = op;
+
+        return 0;
 }
-static inline int operation_new_with_varlink_reply(Manager *manager, Machine *machine, pid_t child, sd_varlink *link, int errno_fd, Operation **ret) {
-        return operation_new(manager, machine, child, /* message = */ NULL, link, errno_fd, ret);
+
+static inline int operation_new_with_varlink_reply(
+                Manager *manager,
+                Machine *machine,
+                pid_t child,
+                sd_varlink *link,
+                int errno_fd,
+                Operation **ret) {
+
+        Operation *op;
+        int r;
+
+        r = operation_new(manager, machine, child, errno_fd, &op);
+        if (r < 0)
+                return r;
+
+        operation_attach_varlink_reply(op, link);
+
+        if (ret)
+                *ret = op;
+
+        return 0;
 }