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"
12 #include "stat-util.h"
15 static int append_machine_properties(
26 r
= sd_bus_message_append(m
, "(sv)", "DevicePolicy", "s", "closed");
28 return bus_log_create_error(r
);
30 /* If you make changes here, also make sure to update systemd-nspawn@.service, to keep the device policies in
31 * sync regardless if we are run with or without the --keep-unit switch. */
32 r
= sd_bus_message_append(m
, "(sv)", "DeviceAllow", "a(ss)", 2,
33 /* Allow the container to
34 * access and create the API
35 * device nodes, so that
36 * PrivateDevices= in the
39 "/dev/net/tun", "rwm",
40 /* Allow the container
41 * access to ptys. However,
43 * container to ever create
44 * these device nodes. */
47 return bus_log_create_error(r
);
49 for (j
= 0; j
< n_mounts
; j
++) {
50 CustomMount
*cm
= mounts
+ j
;
52 if (cm
->type
!= CUSTOM_MOUNT_BIND
)
55 r
= is_device_node(cm
->source
);
57 /* The bind source might only appear as the image is put together, hence don't complain */
58 log_debug_errno(r
, "Bind mount source %s not found, ignoring: %m", cm
->source
);
62 return log_error_errno(r
, "Failed to stat %s: %m", cm
->source
);
65 r
= sd_bus_message_append(m
, "(sv)", "DeviceAllow", "a(ss)", 1,
66 cm
->source
, cm
->read_only
? "r" : "rw");
68 return log_error_errno(r
, "Failed to append message arguments: %m");
72 if (kill_signal
!= 0) {
73 r
= sd_bus_message_append(m
, "(sv)", "KillSignal", "i", kill_signal
);
75 return bus_log_create_error(r
);
77 r
= sd_bus_message_append(m
, "(sv)", "KillMode", "s", "mixed");
79 return bus_log_create_error(r
);
85 static int append_controller_property(sd_bus
*bus
, sd_bus_message
*m
) {
92 r
= sd_bus_get_unique_name(bus
, &unique
);
94 return log_error_errno(r
, "Failed to get unique name: %m");
96 r
= sd_bus_message_append(m
, "(sv)", "Controller", "s", unique
);
98 return bus_log_create_error(r
);
103 int register_machine(
105 const char *machine_name
,
107 const char *directory
,
115 sd_bus_message
*properties_message
,
117 const char *service
) {
119 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
128 "RegisterMachineWithNetwork",
133 SD_BUS_MESSAGE_APPEND_ID128(uuid
),
138 local_ifindex
> 0 ? 1 : 0, local_ifindex
);
140 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
142 r
= bus_message_new_method_call(bus
, &m
, bus_machine_mgr
, "CreateMachineWithNetwork");
144 return bus_log_create_error(r
);
146 r
= sd_bus_message_append(
150 SD_BUS_MESSAGE_APPEND_ID128(uuid
),
155 local_ifindex
> 0 ? 1 : 0, local_ifindex
);
157 return bus_log_create_error(r
);
159 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
161 return bus_log_create_error(r
);
163 if (!isempty(slice
)) {
164 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
166 return bus_log_create_error(r
);
169 r
= append_controller_property(bus
, m
);
173 r
= append_machine_properties(
181 if (properties_message
) {
182 r
= sd_bus_message_copy(m
, properties_message
, true);
184 return bus_log_create_error(r
);
187 r
= bus_append_unit_property_assignment_many(m
, UNIT_SERVICE
, properties
);
191 r
= sd_bus_message_close_container(m
);
193 return bus_log_create_error(r
);
195 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
198 return log_error_errno(r
, "Failed to register machine: %s", bus_error_message(&error
, r
));
203 int unregister_machine(
205 const char *machine_name
) {
207 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
212 r
= bus_call_method(bus
, bus_machine_mgr
, "UnregisterMachine", &error
, NULL
, "s", machine_name
);
214 log_debug("Failed to unregister machine: %s", bus_error_message(&error
, r
));
221 const char *machine_name
,
228 sd_bus_message
*properties_message
,
231 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
232 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
233 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*w
= NULL
;
234 _cleanup_free_
char *scope
= NULL
;
235 const char *description
, *object
;
240 r
= bus_wait_for_jobs_new(bus
, &w
);
242 return log_error_errno(r
, "Could not watch job: %m");
244 r
= unit_name_mangle_with_suffix(machine_name
, "as machine name", 0, ".scope", &scope
);
246 return log_error_errno(r
, "Failed to mangle scope name: %m");
248 r
= bus_message_new_method_call(bus
, &m
, bus_systemd_mgr
, "StartTransientUnit");
250 return bus_log_create_error(r
);
252 r
= sd_bus_message_append(m
, "ss", scope
, "fail");
254 return bus_log_create_error(r
);
257 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
259 return bus_log_create_error(r
);
261 description
= strjoina("Container ", machine_name
);
264 _cleanup_(pidref_done
) PidRef pidref
= PIDREF_NULL
;
265 r
= pidref_set_pid(&pidref
, pid
);
267 return log_error_errno(r
, "Failed to allocate PID reference: %m");
269 r
= bus_append_scope_pidref(m
, &pidref
);
271 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
273 return bus_log_create_error(r
);
275 r
= sd_bus_message_append(m
, "(sv)(sv)(sv)(sv)(sv)",
276 "Description", "s", description
,
278 "CollectMode", "s", "inactive-or-failed",
280 "Slice", "s", isempty(slice
) ? SPECIAL_MACHINE_SLICE
: slice
);
282 return bus_log_create_error(r
);
284 r
= append_controller_property(bus
, m
);
288 if (properties_message
) {
289 r
= sd_bus_message_copy(m
, properties_message
, true);
291 return bus_log_create_error(r
);
294 r
= append_machine_properties(
302 r
= bus_append_unit_property_assignment_many(m
, UNIT_SCOPE
, properties
);
306 r
= sd_bus_message_close_container(m
);
308 return bus_log_create_error(r
);
310 /* No auxiliary units */
311 r
= sd_bus_message_append(
316 return bus_log_create_error(r
);
318 r
= sd_bus_call(bus
, m
, 0, &error
, &reply
);
320 /* If this failed with a property we couldn't write, this is quite likely because the server
321 * doesn't support PIDFDs yet, let's try without. */
323 sd_bus_error_has_names(&error
, SD_BUS_ERROR_UNKNOWN_PROPERTY
, SD_BUS_ERROR_PROPERTY_READ_ONLY
))
324 return allocate_scope(bus
, machine_name
, pid
, slice
, mounts
, n_mounts
, kill_signal
, properties
, properties_message
, /* allow_pidfd= */ false);
326 return log_error_errno(r
, "Failed to allocate scope: %s", bus_error_message(&error
, r
));
329 r
= sd_bus_message_read(reply
, "o", &object
);
331 return bus_log_parse_error(r
);
333 r
= bus_wait_for_jobs_one(w
, object
, false, NULL
);
342 const char *machine_name
) {
344 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
345 _cleanup_free_
char *scope
= NULL
;
348 r
= unit_name_mangle_with_suffix(machine_name
, "to terminate", 0, ".scope", &scope
);
350 return log_error_errno(r
, "Failed to mangle scope name: %m");
352 r
= bus_call_method(bus
, bus_systemd_mgr
, "AbandonScope", &error
, NULL
, "s", scope
);
354 log_debug_errno(r
, "Failed to abandon scope '%s', ignoring: %s", scope
, bus_error_message(&error
, r
));
355 sd_bus_error_free(&error
);
369 log_debug_errno(r
, "Failed to SIGKILL scope '%s', ignoring: %s", scope
, bus_error_message(&error
, r
));
370 sd_bus_error_free(&error
);
373 r
= bus_call_method(bus
, bus_systemd_mgr
, "UnrefUnit", &error
, NULL
, "s", scope
);
375 log_debug_errno(r
, "Failed to drop reference to scope '%s', ignoring: %s", scope
, bus_error_message(&error
, r
));