]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/operation.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Lennart Poettering
10 #include "alloc-util.h"
12 #include "operation.h"
13 #include "process-util.h"
15 static int operation_done(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
16 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
17 Operation
*o
= userdata
;
23 log_debug("Operating " PID_FMT
" is now complete with code=%s status=%i",
25 sigchld_code_to_string(si
->si_code
), si
->si_status
);
29 if (si
->si_code
!= CLD_EXITED
) {
30 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
34 if (si
->si_status
== EXIT_SUCCESS
)
36 else if (read(o
->errno_fd
, &r
, sizeof(r
)) != sizeof(r
)) { /* Try to acquire error code for failed operation */
37 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child failed.");
42 /* A completion routine is set for this operation, call it. */
43 r
= o
->done(o
, r
, &error
);
45 if (!sd_bus_error_is_set(&error
))
46 sd_bus_error_set_errno(&error
, r
);
52 /* The default operation when done is to simply return an error on failure or an empty success
53 * message on success. */
55 sd_bus_error_set_errno(&error
, r
);
59 r
= sd_bus_reply_method_return(o
->message
, NULL
);
61 log_error_errno(r
, "Failed to reply to message: %m");
68 r
= sd_bus_reply_method_error(o
->message
, &error
);
70 log_error_errno(r
, "Failed to reply to message: %m");
76 int operation_new(Manager
*manager
, Machine
*machine
, pid_t child
, sd_bus_message
*message
, int errno_fd
, Operation
**ret
) {
83 assert(errno_fd
>= 0);
85 o
= new0(Operation
, 1);
91 r
= sd_event_add_child(manager
->event
, &o
->event_source
, child
, WEXITED
, operation_done
, o
);
98 o
->message
= sd_bus_message_ref(message
);
99 o
->errno_fd
= errno_fd
;
101 LIST_PREPEND(operations
, manager
->operations
, o
);
102 manager
->n_operations
++;
103 o
->manager
= manager
;
106 LIST_PREPEND(operations_by_machine
, machine
->operations
, o
);
107 o
->machine
= machine
;
110 log_debug("Started new operation " PID_FMT
".", child
);
112 /* At this point we took ownership of both the child and the errno file descriptor! */
120 Operation
*operation_free(Operation
*o
) {
124 sd_event_source_unref(o
->event_source
);
126 safe_close(o
->errno_fd
);
127 safe_close(o
->extra_fd
);
130 (void) sigkill_wait(o
->pid
);
132 sd_bus_message_unref(o
->message
);
135 LIST_REMOVE(operations
, o
->manager
->operations
, o
);
136 o
->manager
->n_operations
--;
140 LIST_REMOVE(operations_by_machine
, o
->machine
->operations
, o
);