1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "bus-locator.h"
7 #include "bus-unit-util.h"
9 #include "bus-wait-for-jobs.h"
10 #include "nspawn-register.h"
11 #include "nspawn-settings.h"
13 #include "stat-util.h"
16 static int append_machine_properties(
21 bool coredump_receive
) {
28 r
= sd_bus_message_append(m
, "(sv)", "DevicePolicy", "s", "closed");
30 return bus_log_create_error(r
);
32 /* If you make changes here, also make sure to update systemd-nspawn@.service, to keep the device policies in
33 * sync regardless if we are run with or without the --keep-unit switch. */
34 r
= sd_bus_message_append(m
, "(sv)", "DeviceAllow", "a(ss)", 2,
35 /* Allow the container to
36 * access and create the API
37 * device nodes, so that
38 * PrivateDevices= in the
41 "/dev/net/tun", "rwm",
42 /* Allow the container
43 * access to ptys. However,
45 * container to ever create
46 * these device nodes. */
49 return bus_log_create_error(r
);
51 for (j
= 0; j
< n_mounts
; j
++) {
52 CustomMount
*cm
= mounts
+ j
;
54 if (cm
->type
!= CUSTOM_MOUNT_BIND
)
57 r
= is_device_node(cm
->source
);
59 /* The bind source might only appear as the image is put together, hence don't complain */
60 log_debug_errno(r
, "Bind mount source %s not found, ignoring: %m", cm
->source
);
64 return log_error_errno(r
, "Failed to stat %s: %m", cm
->source
);
67 r
= sd_bus_message_append(m
, "(sv)", "DeviceAllow", "a(ss)", 1,
68 cm
->source
, cm
->read_only
? "r" : "rw");
70 return log_error_errno(r
, "Failed to append message arguments: %m");
74 if (kill_signal
!= 0) {
75 r
= sd_bus_message_append(m
, "(sv)", "KillSignal", "i", kill_signal
);
77 return bus_log_create_error(r
);
79 r
= sd_bus_message_append(m
, "(sv)", "KillMode", "s", "mixed");
81 return bus_log_create_error(r
);
84 if (coredump_receive
) {
85 r
= sd_bus_message_append(m
, "(sv)", "CoredumpReceive", "b", true);
87 return bus_log_create_error(r
);
93 static int append_controller_property(sd_bus
*bus
, sd_bus_message
*m
) {
100 r
= sd_bus_get_unique_name(bus
, &unique
);
102 return log_error_errno(r
, "Failed to get unique name: %m");
104 r
= sd_bus_message_append(m
, "(sv)", "Controller", "s", unique
);
106 return bus_log_create_error(r
);
111 int register_machine(
113 const char *machine_name
,
115 const char *directory
,
123 sd_bus_message
*properties_message
,
126 StartMode start_mode
) {
128 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
137 "RegisterMachineWithNetwork",
142 SD_BUS_MESSAGE_APPEND_ID128(uuid
),
147 local_ifindex
> 0 ? 1 : 0, local_ifindex
);
149 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
151 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "CreateMachineWithNetwork");
153 return bus_log_create_error(r
);
155 r
= sd_bus_message_append(
159 SD_BUS_MESSAGE_APPEND_ID128(uuid
),
164 local_ifindex
> 0 ? 1 : 0, local_ifindex
);
166 return bus_log_create_error(r
);
168 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
170 return bus_log_create_error(r
);
172 if (!isempty(slice
)) {
173 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
175 return bus_log_create_error(r
);
178 r
= append_controller_property(bus
, m
);
182 r
= append_machine_properties(
187 start_mode
== START_BOOT
);
191 if (properties_message
) {
192 r
= sd_bus_message_copy(m
, properties_message
, true);
194 return bus_log_create_error(r
);
197 r
= bus_append_unit_property_assignment_many(m
, UNIT_SERVICE
, properties
);
201 r
= sd_bus_message_close_container(m
);
203 return bus_log_create_error(r
);
205 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
208 return log_error_errno(r
, "Failed to register machine: %s", bus_error_message(&error
, r
));
213 int unregister_machine(
215 const char *machine_name
) {
217 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
222 r
= bus_call_method(bus
, bus_machine_mgr
, "UnregisterMachine", &error
, NULL
, "s", machine_name
);
224 log_debug("Failed to unregister machine: %s", bus_error_message(&error
, r
));
231 const char *machine_name
,
238 sd_bus_message
*properties_message
,
240 StartMode start_mode
) {
242 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
243 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
244 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
245 _cleanup_free_
char *scope
= NULL
;
246 const char *description
, *object
;
251 r
= bus_wait_for_jobs_new(bus
, &w
);
253 return log_error_errno(r
, "Could not watch job: %m");
255 r
= unit_name_mangle_with_suffix(machine_name
, "as machine name", 0, ".scope", &scope
);
257 return log_error_errno(r
, "Failed to mangle scope name: %m");
259 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
261 return bus_log_create_error(r
);
263 r
= sd_bus_message_append(m
, "ss", scope
, "fail");
265 return bus_log_create_error(r
);
268 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
270 return bus_log_create_error(r
);
272 description
= strjoina("Container ", machine_name
);
275 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
276 r
= pidref_set_pid(&pidref
, pid
);
278 return log_error_errno(r
, "Failed to allocate PID reference: %m");
280 r
= bus_append_scope_pidref(m
, &pidref
);
282 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
284 return bus_log_create_error(r
);
286 r
= sd_bus_message_append(m
, "(sv)(sv)(sv)(sv)(sv)",
287 "Description", "s", description
,
289 "CollectMode", "s", "inactive-or-failed",
291 "Slice", "s", isempty(slice
) ? SPECIAL_MACHINE_SLICE
: slice
);
293 return bus_log_create_error(r
);
295 r
= append_controller_property(bus
, m
);
299 if (properties_message
) {
300 r
= sd_bus_message_copy(m
, properties_message
, true);
302 return bus_log_create_error(r
);
305 r
= append_machine_properties(
310 start_mode
== START_BOOT
);
314 r
= bus_append_unit_property_assignment_many(m
, UNIT_SCOPE
, properties
);
318 r
= sd_bus_message_close_container(m
);
320 return bus_log_create_error(r
);
322 /* No auxiliary units */
323 r
= sd_bus_message_append(
328 return bus_log_create_error(r
);
330 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
332 /* If this failed with a property we couldn't write, this is quite likely because the server
333 * doesn't support PIDFDs yet, let's try without. */
335 sd_bus_error_has_names(&error
, SD_BUS_ERROR_UNKNOWN_PROPERTY
, SD_BUS_ERROR_PROPERTY_READ_ONLY
))
336 return allocate_scope(bus
, machine_name
, pid
, slice
, mounts
, n_mounts
, kill_signal
, properties
, properties_message
, /* allow_pidfd= */ false, start_mode
);
338 return log_error_errno(r
, "Failed to allocate scope: %s", bus_error_message(&error
, r
));
341 r
= sd_bus_message_read(reply
, "o", &object
);
343 return bus_log_parse_error(r
);
345 r
= bus_wait_for_jobs_one(w
, object
, false, NULL
);
354 const char *machine_name
) {
356 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
357 _cleanup_free_
char *scope
= NULL
;
360 r
= unit_name_mangle_with_suffix(machine_name
, "to terminate", 0, ".scope", &scope
);
362 return log_error_errno(r
, "Failed to mangle scope name: %m");
364 r
= bus_call_method(bus
, bus_systemd_mgr
, "AbandonScope", &error
, NULL
, "s", scope
);
366 log_debug_errno(r
, "Failed to abandon scope '%s', ignoring: %s", scope
, bus_error_message(&error
, r
));
367 sd_bus_error_free(&error
);
381 log_debug_errno(r
, "Failed to SIGKILL scope '%s', ignoring: %s", scope
, bus_error_message(&error
, r
));
382 sd_bus_error_free(&error
);
385 r
= bus_call_method(bus
, bus_systemd_mgr
, "UnrefUnit", &error
, NULL
, "s", scope
);
387 log_debug_errno(r
, "Failed to drop reference to scope '%s', ignoring: %s", scope
, bus_error_message(&error
, r
));