2 This file is part of systemd.
4 Copyright 2015 Lennart Poettering
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.
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.
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/>.
22 #include "bus-error.h"
23 #include "bus-unit-util.h"
25 #include "nspawn-register.h"
26 #include "stat-util.h"
31 const char *machine_name
,
33 const char *directory
,
42 const char *service
) {
44 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
45 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
48 r
= sd_bus_default_system(&bus
);
50 return log_error_errno(r
, "Failed to open system bus: %m");
53 r
= sd_bus_call_method(
55 "org.freedesktop.machine1",
56 "/org/freedesktop/machine1",
57 "org.freedesktop.machine1.Manager",
58 "RegisterMachineWithNetwork",
63 SD_BUS_MESSAGE_APPEND_ID128(uuid
),
68 local_ifindex
> 0 ? 1 : 0, local_ifindex
);
70 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
73 r
= sd_bus_message_new_method_call(
76 "org.freedesktop.machine1",
77 "/org/freedesktop/machine1",
78 "org.freedesktop.machine1.Manager",
79 "CreateMachineWithNetwork");
81 return bus_log_create_error(r
);
83 r
= sd_bus_message_append(
87 SD_BUS_MESSAGE_APPEND_ID128(uuid
),
92 local_ifindex
> 0 ? 1 : 0, local_ifindex
);
94 return bus_log_create_error(r
);
96 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
98 return bus_log_create_error(r
);
100 if (!isempty(slice
)) {
101 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
103 return bus_log_create_error(r
);
106 r
= sd_bus_message_append(m
, "(sv)", "DevicePolicy", "s", "closed");
108 return bus_log_create_error(r
);
110 /* If you make changes here, also make sure to update
111 * systemd-nspawn@.service, to keep the device
112 * policies in sync regardless if we are run with or
113 * without the --keep-unit switch. */
114 r
= sd_bus_message_append(m
, "(sv)", "DeviceAllow", "a(ss)", 2,
115 /* Allow the container to
116 * access and create the API
117 * device nodes, so that
118 * PrivateDevices= in the
121 "/dev/net/tun", "rwm",
122 /* Allow the container
123 * access to ptys. However,
125 * container to ever create
126 * these device nodes. */
129 return bus_log_create_error(r
);
131 for (j
= 0; j
< n_mounts
; j
++) {
132 CustomMount
*cm
= mounts
+ j
;
134 if (cm
->type
!= CUSTOM_MOUNT_BIND
)
137 r
= is_device_node(cm
->source
);
139 return log_error_errno(r
, "Failed to stat %s: %m", cm
->source
);
142 r
= sd_bus_message_append(m
, "(sv)", "DeviceAllow", "a(ss)", 1,
143 cm
->source
, cm
->read_only
? "r" : "rw");
145 return log_error_errno(r
, "Failed to append message arguments: %m");
149 if (kill_signal
!= 0) {
150 r
= sd_bus_message_append(m
, "(sv)", "KillSignal", "i", kill_signal
);
152 return bus_log_create_error(r
);
154 r
= sd_bus_message_append(m
, "(sv)", "KillMode", "s", "mixed");
156 return bus_log_create_error(r
);
159 r
= bus_append_unit_property_assignment_many(m
, properties
);
163 r
= sd_bus_message_close_container(m
);
165 return bus_log_create_error(r
);
167 r
= sd_bus_call(bus
, m
, 0, &error
, NULL
);
171 log_error("Failed to register machine: %s", bus_error_message(&error
, r
));
178 int terminate_machine(pid_t pid
) {
179 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
180 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
181 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
185 r
= sd_bus_default_system(&bus
);
187 return log_error_errno(r
, "Failed to open system bus: %m");
189 r
= sd_bus_call_method(
191 "org.freedesktop.machine1",
192 "/org/freedesktop/machine1",
193 "org.freedesktop.machine1.Manager",
200 /* Note that the machine might already have been
201 * cleaned up automatically, hence don't consider it a
202 * failure if we cannot get the machine object. */
203 log_debug("Failed to get machine: %s", bus_error_message(&error
, r
));
207 r
= sd_bus_message_read(reply
, "o", &path
);
209 return bus_log_parse_error(r
);
211 r
= sd_bus_call_method(
213 "org.freedesktop.machine1",
215 "org.freedesktop.machine1.Machine",
221 log_debug("Failed to terminate machine: %s", bus_error_message(&error
, r
));