1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2015 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "bus-error.h"
26 #include "nspawn-register.h"
27 #include "stat-util.h"
32 const char *machine_name
,
34 const char *directory
,
43 const char *service
) {
45 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
46 _cleanup_bus_flush_close_unref_ sd_bus
*bus
= NULL
;
49 r
= sd_bus_default_system(&bus
);
51 return log_error_errno(r
, "Failed to open system bus: %m");
54 r
= sd_bus_call_method(
56 "org.freedesktop.machine1",
57 "/org/freedesktop/machine1",
58 "org.freedesktop.machine1.Manager",
59 "RegisterMachineWithNetwork",
64 SD_BUS_MESSAGE_APPEND_ID128(uuid
),
69 local_ifindex
> 0 ? 1 : 0, local_ifindex
);
71 _cleanup_bus_message_unref_ sd_bus_message
*m
= NULL
;
75 r
= sd_bus_message_new_method_call(
78 "org.freedesktop.machine1",
79 "/org/freedesktop/machine1",
80 "org.freedesktop.machine1.Manager",
81 "CreateMachineWithNetwork");
83 return bus_log_create_error(r
);
85 r
= sd_bus_message_append(
89 SD_BUS_MESSAGE_APPEND_ID128(uuid
),
94 local_ifindex
> 0 ? 1 : 0, local_ifindex
);
96 return bus_log_create_error(r
);
98 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
100 return bus_log_create_error(r
);
102 if (!isempty(slice
)) {
103 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
105 return bus_log_create_error(r
);
108 r
= sd_bus_message_append(m
, "(sv)", "DevicePolicy", "s", "strict");
110 return bus_log_create_error(r
);
112 /* If you make changes here, also make sure to update
113 * systemd-nspawn@.service, to keep the device
114 * policies in sync regardless if we are run with or
115 * without the --keep-unit switch. */
116 r
= sd_bus_message_append(m
, "(sv)", "DeviceAllow", "a(ss)", 9,
117 /* Allow the container to
118 * access and create the API
119 * device nodes, so that
120 * PrivateDevices= in the
126 "/dev/random", "rwm",
127 "/dev/urandom", "rwm",
129 "/dev/net/tun", "rwm",
130 /* Allow the container
131 * access to ptys. However,
133 * container to ever create
134 * these device nodes. */
135 "/dev/pts/ptmx", "rw",
138 return bus_log_create_error(r
);
140 for (j
= 0; j
< n_mounts
; j
++) {
141 CustomMount
*cm
= mounts
+ j
;
143 if (cm
->type
!= CUSTOM_MOUNT_BIND
)
146 r
= is_device_node(cm
->source
);
148 return log_error_errno(r
, "Failed to stat %s: %m", cm
->source
);
151 r
= sd_bus_message_append(m
, "(sv)", "DeviceAllow", "a(ss)", 1,
152 cm
->source
, cm
->read_only
? "r" : "rw");
154 return log_error_errno(r
, "Failed to append message arguments: %m");
158 if (kill_signal
!= 0) {
159 r
= sd_bus_message_append(m
, "(sv)", "KillSignal", "i", kill_signal
);
161 return bus_log_create_error(r
);
163 r
= sd_bus_message_append(m
, "(sv)", "KillMode", "s", "mixed");
165 return bus_log_create_error(r
);
168 STRV_FOREACH(i
, properties
) {
169 r
= sd_bus_message_open_container(m
, 'r', "sv");
171 return bus_log_create_error(r
);
173 r
= bus_append_unit_property_assignment(m
, *i
);
177 r
= sd_bus_message_close_container(m
);
179 return bus_log_create_error(r
);
182 r
= sd_bus_message_close_container(m
);
184 return bus_log_create_error(r
);
186 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
190 log_error("Failed to register machine: %s", bus_error_message(&error
, r
));
197 int terminate_machine(pid_t pid
) {
198 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
199 _cleanup_bus_message_unref_ sd_bus_message
*reply
= NULL
;
200 _cleanup_bus_flush_close_unref_ sd_bus
*bus
= NULL
;
204 r
= sd_bus_default_system(&bus
);
206 return log_error_errno(r
, "Failed to open system bus: %m");
208 r
= sd_bus_call_method(
210 "org.freedesktop.machine1",
211 "/org/freedesktop/machine1",
212 "org.freedesktop.machine1.Manager",
219 /* Note that the machine might already have been
220 * cleaned up automatically, hence don't consider it a
221 * failure if we cannot get the machine object. */
222 log_debug("Failed to get machine: %s", bus_error_message(&error
, r
));
226 r
= sd_bus_message_read(reply
, "o", &path
);
228 return bus_log_parse_error(r
);
230 r
= sd_bus_call_method(
232 "org.freedesktop.machine1",
234 "org.freedesktop.machine1.Machine",
240 log_debug("Failed to terminate machine: %s", bus_error_message(&error
, r
));