]>
git.proxmox.com Git - systemd.git/blob - src/machine/operation.c
2 This file is part of systemd.
4 Copyright 2016 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "alloc-util.h"
22 #include "operation.h"
23 #include "process-util.h"
25 static int operation_done(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
26 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
27 Operation
*o
= userdata
;
33 log_debug("Operating " PID_FMT
" is now complete with code=%s status=%i",
35 sigchld_code_to_string(si
->si_code
), si
->si_status
);
39 if (si
->si_code
!= CLD_EXITED
) {
40 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
44 if (si
->si_status
== EXIT_SUCCESS
)
46 else if (read(o
->errno_fd
, &r
, sizeof(r
)) != sizeof(r
)) { /* Try to acquire error code for failed operation */
47 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child failed.");
52 /* A completion routine is set for this operation, call it. */
53 r
= o
->done(o
, r
, &error
);
55 if (!sd_bus_error_is_set(&error
))
56 sd_bus_error_set_errno(&error
, r
);
62 /* The default operation when done is to simply return an error on failure or an empty success
63 * message on success. */
67 r
= sd_bus_reply_method_return(o
->message
, NULL
);
69 log_error_errno(r
, "Failed to reply to message: %m");
76 r
= sd_bus_reply_method_error(o
->message
, &error
);
78 log_error_errno(r
, "Failed to reply to message: %m");
84 int operation_new(Manager
*manager
, Machine
*machine
, pid_t child
, sd_bus_message
*message
, int errno_fd
, Operation
**ret
) {
91 assert(errno_fd
>= 0);
93 o
= new0(Operation
, 1);
99 r
= sd_event_add_child(manager
->event
, &o
->event_source
, child
, WEXITED
, operation_done
, o
);
106 o
->message
= sd_bus_message_ref(message
);
107 o
->errno_fd
= errno_fd
;
109 LIST_PREPEND(operations
, manager
->operations
, o
);
110 manager
->n_operations
++;
111 o
->manager
= manager
;
114 LIST_PREPEND(operations_by_machine
, machine
->operations
, o
);
115 o
->machine
= machine
;
118 log_debug("Started new operation " PID_FMT
".", child
);
120 /* At this point we took ownership of both the child and the errno file descriptor! */
128 Operation
*operation_free(Operation
*o
) {
132 sd_event_source_unref(o
->event_source
);
134 safe_close(o
->errno_fd
);
135 safe_close(o
->extra_fd
);
138 (void) sigkill_wait(o
->pid
);
140 sd_bus_message_unref(o
->message
);
143 LIST_REMOVE(operations
, o
->manager
->operations
, o
);
144 o
->manager
->n_operations
--;
148 LIST_REMOVE(operations_by_machine
, o
->machine
->operations
, o
);