]>
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
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
25 #include "operation.h"
26 #include "process-util.h"
28 static int operation_done(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
29 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
30 Operation
*o
= userdata
;
36 log_debug("Operating " PID_FMT
" is now complete with code=%s status=%i",
38 sigchld_code_to_string(si
->si_code
), si
->si_status
);
42 if (si
->si_code
!= CLD_EXITED
) {
43 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
47 if (si
->si_status
== EXIT_SUCCESS
)
49 else if (read(o
->errno_fd
, &r
, sizeof(r
)) != sizeof(r
)) { /* Try to acquire error code for failed operation */
50 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child failed.");
55 /* A completion routine is set for this operation, call it. */
56 r
= o
->done(o
, r
, &error
);
58 if (!sd_bus_error_is_set(&error
))
59 sd_bus_error_set_errno(&error
, r
);
65 /* The default operation when done is to simply return an error on failure or an empty success
66 * message on success. */
68 sd_bus_error_set_errno(&error
, r
);
72 r
= sd_bus_reply_method_return(o
->message
, NULL
);
74 log_error_errno(r
, "Failed to reply to message: %m");
81 r
= sd_bus_reply_method_error(o
->message
, &error
);
83 log_error_errno(r
, "Failed to reply to message: %m");
89 int operation_new(Manager
*manager
, Machine
*machine
, pid_t child
, sd_bus_message
*message
, int errno_fd
, Operation
**ret
) {
96 assert(errno_fd
>= 0);
98 o
= new0(Operation
, 1);
104 r
= sd_event_add_child(manager
->event
, &o
->event_source
, child
, WEXITED
, operation_done
, o
);
111 o
->message
= sd_bus_message_ref(message
);
112 o
->errno_fd
= errno_fd
;
114 LIST_PREPEND(operations
, manager
->operations
, o
);
115 manager
->n_operations
++;
116 o
->manager
= manager
;
119 LIST_PREPEND(operations_by_machine
, machine
->operations
, o
);
120 o
->machine
= machine
;
123 log_debug("Started new operation " PID_FMT
".", child
);
125 /* At this point we took ownership of both the child and the errno file descriptor! */
133 Operation
*operation_free(Operation
*o
) {
137 sd_event_source_unref(o
->event_source
);
139 safe_close(o
->errno_fd
);
140 safe_close(o
->extra_fd
);
143 (void) sigkill_wait(o
->pid
);
145 sd_bus_message_unref(o
->message
);
148 LIST_REMOVE(operations
, o
->manager
->operations
, o
);
149 o
->manager
->n_operations
--;
153 LIST_REMOVE(operations_by_machine
, o
->machine
->operations
, o
);