]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/machine/operation.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / machine / operation.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
56599585
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2016 Lennart Poettering
6
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.
11
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.
16
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/>.
19***/
20
fe993888
MS
21#include <sys/wait.h>
22
56599585
LP
23#include "alloc-util.h"
24#include "fd-util.h"
25#include "operation.h"
26#include "process-util.h"
27
28static 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;
31 int r;
32
33 assert(o);
34 assert(si);
35
61233823 36 log_debug("Operating " PID_FMT " is now complete with code=%s status=%i",
56599585
LP
37 o->pid,
38 sigchld_code_to_string(si->si_code), si->si_status);
39
40 o->pid = 0;
41
42 if (si->si_code != CLD_EXITED) {
43 r = sd_bus_error_setf(&error, SD_BUS_ERROR_FAILED, "Child died abnormally.");
44 goto fail;
45 }
46
03c2b288
LP
47 if (si->si_status == EXIT_SUCCESS)
48 r = 0;
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.");
56599585
LP
51 goto fail;
52 }
53
03c2b288
LP
54 if (o->done) {
55 /* A completion routine is set for this operation, call it. */
56 r = o->done(o, r, &error);
57 if (r < 0) {
58 if (!sd_bus_error_is_set(&error))
59 sd_bus_error_set_errno(&error, r);
60
61 goto fail;
62 }
63
64 } else {
61233823 65 /* The default operation when done is to simply return an error on failure or an empty success
03c2b288 66 * message on success. */
3aca8326
LP
67 if (r < 0) {
68 sd_bus_error_set_errno(&error, r);
03c2b288 69 goto fail;
3aca8326 70 }
03c2b288
LP
71
72 r = sd_bus_reply_method_return(o->message, NULL);
73 if (r < 0)
74 log_error_errno(r, "Failed to reply to message: %m");
75 }
56599585
LP
76
77 operation_free(o);
78 return 0;
79
80fail:
81 r = sd_bus_reply_method_error(o->message, &error);
82 if (r < 0)
83 log_error_errno(r, "Failed to reply to message: %m");
84
85 operation_free(o);
86 return 0;
87}
88
03c2b288 89int operation_new(Manager *manager, Machine *machine, pid_t child, sd_bus_message *message, int errno_fd, Operation **ret) {
56599585
LP
90 Operation *o;
91 int r;
92
795c5d31
LP
93 assert(manager);
94 assert(child > 1);
95 assert(message);
96 assert(errno_fd >= 0);
97
56599585
LP
98 o = new0(Operation, 1);
99 if (!o)
100 return -ENOMEM;
101
03c2b288
LP
102 o->extra_fd = -1;
103
795c5d31 104 r = sd_event_add_child(manager->event, &o->event_source, child, WEXITED, operation_done, o);
56599585
LP
105 if (r < 0) {
106 free(o);
107 return r;
108 }
109
110 o->pid = child;
111 o->message = sd_bus_message_ref(message);
112 o->errno_fd = errno_fd;
113
795c5d31
LP
114 LIST_PREPEND(operations, manager->operations, o);
115 manager->n_operations++;
116 o->manager = manager;
117
118 if (machine) {
119 LIST_PREPEND(operations_by_machine, machine->operations, o);
120 o->machine = machine;
121 }
56599585
LP
122
123 log_debug("Started new operation " PID_FMT ".", child);
124
125 /* At this point we took ownership of both the child and the errno file descriptor! */
126
03c2b288
LP
127 if (ret)
128 *ret = o;
129
56599585
LP
130 return 0;
131}
132
133Operation *operation_free(Operation *o) {
134 if (!o)
135 return NULL;
136
137 sd_event_source_unref(o->event_source);
138
139 safe_close(o->errno_fd);
03c2b288 140 safe_close(o->extra_fd);
56599585
LP
141
142 if (o->pid > 1)
89c9030d 143 (void) sigkill_wait(o->pid);
56599585
LP
144
145 sd_bus_message_unref(o->message);
146
147 if (o->manager) {
148 LIST_REMOVE(operations, o->manager->operations, o);
149 o->manager->n_operations--;
150 }
151
795c5d31
LP
152 if (o->machine)
153 LIST_REMOVE(operations_by_machine, o->machine->operations, o);
154
6b430fdb 155 return mfree(o);
56599585 156}