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