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