]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/portable/portabled-operation.c
Merge pull request #13365 from keszybz/fix-commits-from-pr-13246
[thirdparty/systemd.git] / src / portable / portabled-operation.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "fd-util.h"
5 #include "portabled-operation.h"
6 #include "process-util.h"
7
8 static int operation_done(sd_event_source *s, const siginfo_t *si, void *userdata) {
9 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
10 Operation *o = userdata;
11 int r;
12
13 assert(o);
14 assert(si);
15
16 log_debug("Operating " PID_FMT " is now complete with code=%s status=%i",
17 o->pid,
18 sigchld_code_to_string(si->si_code), si->si_status);
19
20 o->pid = 0;
21
22 if (si->si_code != CLD_EXITED) {
23 r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
24 goto fail;
25 }
26
27 if (si->si_status == EXIT_SUCCESS)
28 r = 0;
29 else if (read(o->errno_fd, &r, sizeof(r)) != sizeof(r)) { /* Try to acquire error code for failed operation */
30 r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Child failed.");
31 goto fail;
32 }
33
34 if (o->done) {
35 /* A completion routine is set for this operation, call it. */
36 r = o->done(o, r, &error);
37 if (r < 0) {
38 if (!sd_bus_error_is_set(&error))
39 sd_bus_error_set_errno(&error, r);
40
41 goto fail;
42 }
43
44 } else {
45 /* The default operation when done is to simply return an error on failure or an empty success
46 * message on success. */
47 if (r < 0) {
48 sd_bus_error_set_errno(&error, r);
49 goto fail;
50 }
51
52 r = sd_bus_reply_method_return(o->message, NULL);
53 if (r < 0)
54 log_error_errno(r, "Failed to reply to message: %m");
55 }
56
57 operation_free(o);
58 return 0;
59
60 fail:
61 r = sd_bus_reply_method_error(o->message, &error);
62 if (r < 0)
63 log_error_errno(r, "Failed to reply to message: %m");
64
65 operation_free(o);
66 return 0;
67 }
68
69 int operation_new(Manager *manager, pid_t child, sd_bus_message *message, int errno_fd, Operation **ret) {
70 Operation *o;
71 int r;
72
73 assert(manager);
74 assert(child > 1);
75 assert(message);
76 assert(errno_fd >= 0);
77
78 o = new0(Operation, 1);
79 if (!o)
80 return -ENOMEM;
81
82 o->extra_fd = -1;
83
84 r = sd_event_add_child(manager->event, &o->event_source, child, WEXITED, operation_done, o);
85 if (r < 0) {
86 free(o);
87 return r;
88 }
89
90 o->pid = child;
91 o->message = sd_bus_message_ref(message);
92 o->errno_fd = errno_fd;
93
94 LIST_PREPEND(operations, manager->operations, o);
95 manager->n_operations++;
96 o->manager = manager;
97
98 log_debug("Started new operation " PID_FMT ".", child);
99
100 /* At this point we took ownership of both the child and the errno file descriptor! */
101
102 if (ret)
103 *ret = o;
104
105 return 0;
106 }
107
108 Operation *operation_free(Operation *o) {
109 if (!o)
110 return NULL;
111
112 sd_event_source_unref(o->event_source);
113
114 safe_close(o->errno_fd);
115 safe_close(o->extra_fd);
116
117 if (o->pid > 1)
118 (void) sigkill_wait(o->pid);
119
120 sd_bus_message_unref(o->message);
121
122 if (o->manager) {
123 LIST_REMOVE(operations, o->manager->operations, o);
124 o->manager->n_operations--;
125 }
126
127 return mfree(o);
128 }