Debian Codegraph @

src/nspawn/nspawn-register.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "bus-error.h"
7 #include "bus-locator.h"
8 #include "bus-unit-util.h"
9 #include "bus-util.h"
10 #include "bus-wait-for-jobs.h"
11 #include "nspawn-mount.h"
12 #include "nspawn-register.h"
13 #include "nspawn-settings.h"
14 #include "pidref.h"
15 #include "special.h"
16 #include "stat-util.h"
17 #include "string-util.h"
18 #include "unit-def.h"
19 #include "unit-name.h"
20
21 static int append_machine_properties(
22 sd_bus_message *m,
23 CustomMount *mounts,
24 unsigned n_mounts,
25 int kill_signal,
26 bool coredump_receive) {
27
28 int r;
29
30 assert(m);
31
32 r = sd_bus_message_append(m, "(sv)", "DevicePolicy", "s", "closed");
33 if (r < 0)
34 return bus_log_create_error(r);
35
36 /* If you make changes here, also make sure to update [email protected], to keep the device
37 * policies in sync regardless if we are run with or without the --keep-unit switch. */
38 r = sd_bus_message_append(m, "(sv)", "DeviceAllow", "a(ss)", 3,
39 /* Allow the container to access and create the API device node, so that
40 * PrivateDevices= in the container can work fine. */
41 "/dev/net/tun", "rwm",
42 /* Allow the container to access ptys. However, do not permit the container
43 * to ever create these device nodes. */
44 "char-pts", "rw",
45 /* Allow the container to access and create the FUSE API device node. */
46 "/dev/fuse", "rwm");
47 if (r < 0)
48 return bus_log_create_error(r);
49
50 FOREACH_ARRAY(cm, mounts, n_mounts) {
51 if (cm->type != CUSTOM_MOUNT_BIND)
52 continue;
53
54 r = is_device_node(cm->source);
55 if (r == -ENOENT) {
56 /* The bind source might only appear as the image is put together, hence don't complain */
57 log_debug_errno(r, "Bind mount source %s not found, ignoring: %m", cm->source);
58 continue;
59 }
60 if (r < 0)
61 return log_error_errno(r, "Failed to stat %s: %m", cm->source);
62
63 if (r) {
64 r = sd_bus_message_append(m, "(sv)", "DeviceAllow", "a(ss)", 1,
65 cm->source, cm->read_only ? "r" : "rw");
66 if (r < 0)
67 return log_error_errno(r, "Failed to append message arguments: %m");
68 }
69 }
70
71 if (kill_signal != 0) {
72 r = sd_bus_message_append(m, "(sv)", "KillSignal", "i", kill_signal);
73 if (r < 0)
74 return bus_log_create_error(r);
75
76 r = sd_bus_message_append(m, "(sv)", "KillMode", "s", "mixed");
77 if (r < 0)
78 return bus_log_create_error(r);
79 }
80
81 if (coredump_receive) {
82 r = sd_bus_message_append(m, "(sv)", "CoredumpReceive", "b", true);
83 if (r < 0)
84 return bus_log_create_error(r);
85 }
86
87 return 0;
88 }
89
90 static int append_controller_property(sd_bus *bus, sd_bus_message *m) {
91 const char *unique;
92 int r;
93
94 assert(bus);
95 assert(m);
96
97 r = sd_bus_get_unique_name(bus, &unique);
98 if (r < 0)
99 return log_error_errno(r, "Failed to get unique name: %m");
100
101 r = sd_bus_message_append(m, "(sv)", "Controller", "s", unique);
102 if (r < 0)
103 return bus_log_create_error(r);
104
105 return 0;
106 }
107
108 static int can_set_coredump_receive(sd_bus *bus) {
109 _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL;
110 _cleanup_free_ char *path = NULL;
111 int b, r;
112
113 assert(bus);
114
115 path = unit_dbus_path_from_name(SPECIAL_INIT_SCOPE);
116 if (!path)
117 return log_oom();
118
119 r = sd_bus_get_property_trivial(
120 bus,
121 "org.freedesktop.systemd1",
122 path,
123 "org.freedesktop.systemd1.Scope",
124 "CoredumpReceive",
125 &e,
126 'b', &b);
127 if (r < 0 && !sd_bus_error_has_names(&e, SD_BUS_ERROR_UNKNOWN_PROPERTY, SD_BUS_ERROR_PROPERTY_READ_ONLY))
128 log_warning_errno(r, "Failed to determine if CoredumpReceive= can be set, assuming it cannot be: %s",
129 bus_error_message(&e, r));
130
131 return r >= 0;
132 }
133
134 int allocate_scope(
135 sd_bus *bus,
136 const char *machine_name,
137 const PidRef* pid,
138 const char *slice,
139 CustomMount *mounts,
140 unsigned n_mounts,
141 int kill_signal,
142 char **properties,
143 sd_bus_message *properties_message,
144 StartMode start_mode,
145 AllocateScopeFlags flags) {
146
147 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL, *reply = NULL;
148 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
149 _cleanup_(bus_wait_for_jobs_freep) BusWaitForJobs *w = NULL;
150 _cleanup_free_ char *scope = NULL;
151 const char *object;
152 int r;
153
154 assert(bus);
155
156 r = bus_wait_for_jobs_new(bus, &w);
157 if (r < 0)
158 return log_error_errno(r, "Could not watch job: %m");
159
160 r = unit_name_mangle_with_suffix(machine_name, "as machine name", 0, ".scope", &scope);
161 if (r < 0)
162 return log_error_errno(r, "Failed to mangle scope name: %m");
163
164 r = bus_message_new_method_call(bus, &m, bus_systemd_mgr, "StartTransientUnit");
165 if (r < 0)
166 return bus_log_create_error(r);
167
168 r = sd_bus_message_append(m, "ss", scope, "fail");
169 if (r < 0)
170 return bus_log_create_error(r);
171
172 /* Properties */
173 r = sd_bus_message_open_container(m, 'a', "(sv)");
174 if (r < 0)
175 return bus_log_create_error(r);
176
177 r = bus_append_scope_pidref(m, pid, FLAGS_SET(flags, ALLOCATE_SCOPE_ALLOW_PIDFD));
178 if (r < 0)
179 return bus_log_create_error(r);
180
181 _cleanup_free_ char *description = strjoin("Container ", machine_name);
182 if (!description)
183 return log_oom();
184
185 r = sd_bus_message_append(m, "(sv)(sv)(sv)(sv)(sv)",
186 "Description", "s", description,
187 "Delegate", "b", 1,
188 "CollectMode", "s", "inactive-or-failed",
189 "AddRef", "b", 1,
190 "Slice", "s", isempty(slice) ? SPECIAL_MACHINE_SLICE : slice);
191 if (r < 0)
192 return bus_log_create_error(r);
193
194 r = append_controller_property(bus, m);
195 if (r < 0)
196 return r;
197
198 if (properties_message) {
199 r = sd_bus_message_copy(m, properties_message, true);
200 if (r < 0)
201 return bus_log_create_error(r);
202 }
203
204 r = append_machine_properties(
205 m,
206 mounts,
207 n_mounts,
208 kill_signal,
209 start_mode == START_BOOT && can_set_coredump_receive(bus) > 0);
210 if (r < 0)
211 return r;
212
213 r = bus_append_unit_property_assignment_many(m, UNIT_SCOPE, properties);
214 if (r < 0)
215 return r;
216
217 r = sd_bus_message_close_container(m);
218 if (r < 0)
219 return bus_log_create_error(r);
220
221 /* No auxiliary units */
222 r = sd_bus_message_append(
223 m,
224 "a(sa(sv))",
225 0);
226 if (r < 0)
227 return bus_log_create_error(r);
228
229 r = sd_bus_call(bus, m, 0, &error, &reply);
230 if (r < 0) {
231 /* If this failed with a property we couldn't write, this is quite likely because the server
232 * doesn't support PIDFDs yet, let's try without. */
233 if (FLAGS_SET(flags, ALLOCATE_SCOPE_ALLOW_PIDFD) &&
234 sd_bus_error_has_names(&error, SD_BUS_ERROR_UNKNOWN_PROPERTY, SD_BUS_ERROR_PROPERTY_READ_ONLY))
235 return allocate_scope(
236 bus,
237 machine_name,
238 pid,
239 slice,
240 mounts,
241 n_mounts,
242 kill_signal,
243 properties,
244 properties_message,
245 start_mode,
246 flags & ~ALLOCATE_SCOPE_ALLOW_PIDFD);
247
248 return log_error_errno(r, "Failed to allocate scope: %s", bus_error_message(&error, r));
249 }
250
251 r = sd_bus_message_read(reply, "o", &object);
252 if (r < 0)
253 return bus_log_parse_error(r);
254
255 r = bus_wait_for_jobs_one(
256 w,
257 object,
258 BUS_WAIT_JOBS_LOG_ERROR,
259 /* extra_args= */ NULL);
260 if (r < 0)
261 return r;
262
263 return 0;
264 }
265
266 int terminate_scope(
267 sd_bus *bus,
268 const char *machine_name) {
269
270 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
271 _cleanup_free_ char *scope = NULL;
272 int r;
273
274 r = unit_name_mangle_with_suffix(machine_name, "to terminate", /* flags= */ 0, ".scope", &scope);
275 if (r < 0)
276 return log_error_errno(r, "Failed to mangle scope name: %m");
277
278 r = bus_call_method(bus, bus_systemd_mgr, "AbandonScope", &error, /* ret_reply= */ NULL, "s", scope);
279 if (r < 0) {
280 log_debug_errno(r, "Failed to abandon scope '%s', ignoring: %s", scope, bus_error_message(&error, r));
281 sd_bus_error_free(&error);
282 }
283
284 r = bus_call_method(
285 bus,
286 bus_systemd_mgr,
287 "KillUnit",
288 &error,
289 NULL,
290 "ssi",
291 scope,
292 "all",
293 (int32_t) SIGKILL);
294 if (r < 0) {
295 log_debug_errno(r, "Failed to SIGKILL scope '%s', ignoring: %s", scope, bus_error_message(&error, r));
296 sd_bus_error_free(&error);
297 }
298
299 r = bus_call_method(bus, bus_systemd_mgr, "UnrefUnit", &error, /* ret_reply= */ NULL, "s", scope);
300 if (r < 0)
301 log_debug_errno(r, "Failed to drop reference to scope '%s', ignoring: %s", scope, bus_error_message(&error, r));
302
303 return 0;
304 }