]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/operation.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
8 #include "process-util.h"
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
;
18 log_debug("Operating " PID_FMT
" is now complete with code=%s status=%i",
20 sigchld_code_to_string(si
->si_code
), si
->si_status
);
24 if (si
->si_code
!= CLD_EXITED
) {
25 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
29 if (si
->si_status
== EXIT_SUCCESS
)
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.");
37 /* A completion routine is set for this operation, call it. */
38 r
= o
->done(o
, r
, &error
);
40 if (!sd_bus_error_is_set(&error
))
41 sd_bus_error_set_errno(&error
, r
);
47 /* The default operation when done is to simply return an error on failure or an empty success
48 * message on success. */
50 sd_bus_error_set_errno(&error
, r
);
54 r
= sd_bus_reply_method_return(o
->message
, NULL
);
56 log_error_errno(r
, "Failed to reply to message: %m");
63 r
= sd_bus_reply_method_error(o
->message
, &error
);
65 log_error_errno(r
, "Failed to reply to message: %m");
71 int operation_new(Manager
*manager
, Machine
*machine
, pid_t child
, sd_bus_message
*message
, int errno_fd
, Operation
**ret
) {
78 assert(errno_fd
>= 0);
80 o
= new0(Operation
, 1);
86 r
= sd_event_add_child(manager
->event
, &o
->event_source
, child
, WEXITED
, operation_done
, o
);
93 o
->message
= sd_bus_message_ref(message
);
94 o
->errno_fd
= errno_fd
;
96 LIST_PREPEND(operations
, manager
->operations
, o
);
97 manager
->n_operations
++;
101 LIST_PREPEND(operations_by_machine
, machine
->operations
, o
);
102 o
->machine
= machine
;
105 log_debug("Started new operation " PID_FMT
".", child
);
107 /* At this point we took ownership of both the child and the errno file descriptor! */
115 Operation
*operation_free(Operation
*o
) {
119 sd_event_source_unref(o
->event_source
);
121 safe_close(o
->errno_fd
);
122 safe_close(o
->extra_fd
);
125 (void) sigkill_wait(o
->pid
);
127 sd_bus_message_unref(o
->message
);
130 LIST_REMOVE(operations
, o
->manager
->operations
, o
);
131 o
->manager
->n_operations
--;
135 LIST_REMOVE(operations_by_machine
, o
->machine
->operations
, o
);