| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/wait.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-bus.h" |
| 7 |
#include "sd-event.h" |
| 8 |
#include "sd-varlink.h" |
| 9 |
|
| 10 |
#include "alloc-util.h" |
| 11 |
#include "event-util.h" |
| 12 |
#include "fd-util.h" |
| 13 |
#include "format-util.h" |
| 14 |
#include "log.h" |
| 15 |
#include "machine.h" |
| 16 |
#include "machined.h" |
| 17 |
#include "operation.h" |
| 18 |
#include "process-util.h" |
| 19 |
|
| 20 |
static int read_operation_errno(const siginfo_t *si, Operation *o) { |
| 21 |
int r; |
| 22 |
|
| 23 |
assert(si); |
| 24 |
assert(o); |
| 25 |
|
| 26 |
if (si->si_code != CLD_EXITED) |
| 27 |
return log_debug_errno(SYNTHETIC_ERRNO(ESHUTDOWN), "Child died abnormally"); |
| 28 |
|
| 29 |
if (si->si_status == EXIT_SUCCESS) |
| 30 |
r = 0; |
| 31 |
else { |
| 32 |
ssize_t n = read(o->errno_fd, &r, sizeof(r)); |
| 33 |
if (n < 0) |
| 34 |
return log_debug_errno(errno, "Failed to read operation's errno: %m"); |
| 35 |
if (n != sizeof(r)) |
| 36 |
return log_debug_errno(SYNTHETIC_ERRNO(EIO), "Received unexpectedly short message when reading operation's errno"); |
| 37 |
} |
| 38 |
|
| 39 |
return r; |
| 40 |
} |
| 41 |
|
| 42 |
static int operation_done(sd_event_source *s, const siginfo_t *si, void *userdata) { |
| 43 |
Operation *o = ASSERT_PTR(userdata); |
| 44 |
int r; |
| 45 |
|
| 46 |
assert(si); |
| 47 |
|
| 48 |
log_debug("Operation " PID_FMT " is now complete with code=%s status=%i", |
| 49 |
o->pidref.pid, |
| 50 |
sigchld_code_to_string(si->si_code), si->si_status); |
| 51 |
|
| 52 |
pidref_done(&o->pidref); |
| 53 |
|
| 54 |
r = read_operation_errno(si, o); |
| 55 |
if (r < 0) |
| 56 |
log_debug_errno(r, "Operation failed: %m"); |
| 57 |
|
| 58 |
if (o->message) { |
| 59 |
/* If o->done set, call it. It sends a response, but can return |
| 60 |
* an error in which case it expect this code to reply. |
| 61 |
* If o->done is not set, the default action is to simply return |
| 62 |
* an error on failure or an empty success message on success. */ |
| 63 |
|
| 64 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 65 |
if (o->done) |
| 66 |
r = o->done(o, r, &error); |
| 67 |
|
| 68 |
if (r < 0) { |
| 69 |
if (!sd_bus_error_is_set(&error)) |
| 70 |
sd_bus_error_set_errno(&error, r); |
| 71 |
|
| 72 |
r = sd_bus_reply_method_error(o->message, &error); |
| 73 |
if (r < 0) |
| 74 |
log_error_errno(r, "Failed to reply to dbus message: %m"); |
| 75 |
} else if (!o->done) { |
| 76 |
/* when o->done set it's responsible for sending reply in a happy-path case */ |
| 77 |
r = sd_bus_reply_method_return(o->message, NULL); |
| 78 |
if (r < 0) |
| 79 |
log_error_errno(r, "Failed to reply to dbus message: %m"); |
| 80 |
} |
| 81 |
} else if (o->link) { |
| 82 |
/* If o->done set, call it. Unlike o->message case above, this |
| 83 |
* code expect o->done to reply in all cases. |
| 84 |
* If o->done is not set, the default action is to simply return |
| 85 |
* an error on failure or an empty success message on success. */ |
| 86 |
|
| 87 |
if (o->done) |
| 88 |
(void) o->done(o, r, /* error= */ NULL); |
| 89 |
else if (r < 0) |
| 90 |
(void) sd_varlink_error_errno(o->link, r); |
| 91 |
else |
| 92 |
(void) sd_varlink_reply(o->link, NULL); |
| 93 |
} else |
| 94 |
assert_not_reached(); |
| 95 |
|
| 96 |
operation_free(o); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
int operation_new(Manager *manager, Machine *machine, PidRef *child, int errno_fd, Operation **ret) { |
| 101 |
Operation *o; |
| 102 |
int r; |
| 103 |
|
| 104 |
assert(manager); |
| 105 |
assert(pidref_is_set(child)); |
| 106 |
assert(errno_fd >= 0); |
| 107 |
assert(ret); |
| 108 |
|
| 109 |
o = new(Operation, 1); |
| 110 |
if (!o) |
| 111 |
return -ENOMEM; |
| 112 |
|
| 113 |
*o = (Operation) { |
| 114 |
.pidref = *child, |
| 115 |
.errno_fd = errno_fd, |
| 116 |
.extra_fd = -EBADF |
| 117 |
}; |
| 118 |
|
| 119 |
r = event_add_child_pidref(manager->event, &o->event_source, &o->pidref, WEXITED, operation_done, o); |
| 120 |
if (r < 0) { |
| 121 |
free(o); |
| 122 |
return r; |
| 123 |
} |
| 124 |
|
| 125 |
LIST_PREPEND(operations, manager->operations, o); |
| 126 |
manager->n_operations++; |
| 127 |
o->manager = manager; |
| 128 |
|
| 129 |
if (machine) { |
| 130 |
LIST_PREPEND(operations_by_machine, machine->operations, o); |
| 131 |
o->machine = machine; |
| 132 |
} |
| 133 |
|
| 134 |
log_debug("Started new operation " PID_FMT ".", o->pidref.pid); |
| 135 |
|
| 136 |
/* At this point we took ownership of both the child and the errno file descriptor! */ |
| 137 |
|
| 138 |
*ret = o; |
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
Operation *operation_free(Operation *o) { |
| 143 |
if (!o) |
| 144 |
return NULL; |
| 145 |
|
| 146 |
sd_event_source_unref(o->event_source); |
| 147 |
|
| 148 |
safe_close(o->errno_fd); |
| 149 |
safe_close(o->extra_fd); |
| 150 |
|
| 151 |
pidref_done_sigkill_wait(&o->pidref); |
| 152 |
|
| 153 |
sd_bus_message_unref(o->message); |
| 154 |
sd_varlink_unref(o->link); |
| 155 |
|
| 156 |
if (o->manager) { |
| 157 |
LIST_REMOVE(operations, o->manager->operations, o); |
| 158 |
o->manager->n_operations--; |
| 159 |
} |
| 160 |
|
| 161 |
if (o->machine) |
| 162 |
LIST_REMOVE(operations_by_machine, o->machine->operations, o); |
| 163 |
|
| 164 |
return mfree(o); |
| 165 |
} |
| 166 |
|
| 167 |
void operation_attach_bus_reply(Operation *op, sd_bus_message *message) { |
| 168 |
assert(op); |
| 169 |
assert(!op->message); |
| 170 |
assert(!op->link); |
| 171 |
assert(message); |
| 172 |
|
| 173 |
op->message = sd_bus_message_ref(message); |
| 174 |
} |
| 175 |
|
| 176 |
void operation_attach_varlink_reply(Operation *op, sd_varlink *link) { |
| 177 |
assert(op); |
| 178 |
assert(!op->message); |
| 179 |
assert(!op->link); |
| 180 |
assert(link); |
| 181 |
|
| 182 |
op->link = sd_varlink_ref(link); |
| 183 |
} |