]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/operation.c
757052d4ebad2ee942f6819611eb6f1b6b3e203a
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2016 Lennart Poettering
8 #include "alloc-util.h"
10 #include "operation.h"
11 #include "process-util.h"
13 static int operation_done(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
14 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
15 Operation
*o
= userdata
;
21 log_debug("Operating " PID_FMT
" is now complete with code=%s status=%i",
23 sigchld_code_to_string(si
->si_code
), si
->si_status
);
27 if (si
->si_code
!= CLD_EXITED
) {
28 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
32 if (si
->si_status
== EXIT_SUCCESS
)
34 else if (read(o
->errno_fd
, &r
, sizeof(r
)) != sizeof(r
)) { /* Try to acquire error code for failed operation */
35 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child failed.");
40 /* A completion routine is set for this operation, call it. */
41 r
= o
->done(o
, r
, &error
);
43 if (!sd_bus_error_is_set(&error
))
44 sd_bus_error_set_errno(&error
, r
);
50 /* The default operation when done is to simply return an error on failure or an empty success
51 * message on success. */
53 sd_bus_error_set_errno(&error
, r
);
57 r
= sd_bus_reply_method_return(o
->message
, NULL
);
59 log_error_errno(r
, "Failed to reply to message: %m");
66 r
= sd_bus_reply_method_error(o
->message
, &error
);
68 log_error_errno(r
, "Failed to reply to message: %m");
74 int operation_new(Manager
*manager
, Machine
*machine
, pid_t child
, sd_bus_message
*message
, int errno_fd
, Operation
**ret
) {
81 assert(errno_fd
>= 0);
83 o
= new0(Operation
, 1);
89 r
= sd_event_add_child(manager
->event
, &o
->event_source
, child
, WEXITED
, operation_done
, o
);
96 o
->message
= sd_bus_message_ref(message
);
97 o
->errno_fd
= errno_fd
;
99 LIST_PREPEND(operations
, manager
->operations
, o
);
100 manager
->n_operations
++;
101 o
->manager
= manager
;
104 LIST_PREPEND(operations_by_machine
, machine
->operations
, o
);
105 o
->machine
= machine
;
108 log_debug("Started new operation " PID_FMT
".", child
);
110 /* At this point we took ownership of both the child and the errno file descriptor! */
118 Operation
*operation_free(Operation
*o
) {
122 sd_event_source_unref(o
->event_source
);
124 safe_close(o
->errno_fd
);
125 safe_close(o
->extra_fd
);
128 (void) sigkill_wait(o
->pid
);
130 sd_bus_message_unref(o
->message
);
133 LIST_REMOVE(operations
, o
->manager
->operations
, o
);
134 o
->manager
->n_operations
--;
138 LIST_REMOVE(operations_by_machine
, o
->machine
->operations
, o
);