]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
1ee306e1 LP |
2 | |
3 | #include <errno.h> | |
1ee306e1 | 4 | #include <unistd.h> |
1ee306e1 | 5 | |
c3350683 | 6 | #include "sd-id128.h" |
3ffd4af2 | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
3ffd4af2 | 9 | #include "btrfs-util.h" |
96aad8d1 | 10 | #include "bus-common-errors.h" |
40af3d02 | 11 | #include "bus-get-properties.h" |
9b71e4ab | 12 | #include "bus-locator.h" |
269e4d2d | 13 | #include "bus-polkit.h" |
23c80348 | 14 | #include "cgroup-util.h" |
66855de7 | 15 | #include "errno-util.h" |
3ffd4af2 | 16 | #include "fd-util.h" |
03c2b288 | 17 | #include "fileio.h" |
f97b34a6 | 18 | #include "format-util.h" |
25300b5a | 19 | #include "hostname-util.h" |
3ffd4af2 | 20 | #include "image-dbus.h" |
a90fb858 | 21 | #include "io-util.h" |
3ffd4af2 | 22 | #include "machine-dbus.h" |
003dffde | 23 | #include "machine-image.h" |
4cee5eed | 24 | #include "machine-pool.h" |
c3350683 | 25 | #include "machined.h" |
204f52e3 | 26 | #include "missing_capability.h" |
3ffd4af2 LP |
27 | #include "path-util.h" |
28 | #include "process-util.h" | |
15a5e950 | 29 | #include "stdio-util.h" |
3ffd4af2 | 30 | #include "strv.h" |
e4de7287 | 31 | #include "tmpfile-util.h" |
3ffd4af2 | 32 | #include "unit-name.h" |
b1d4f8e1 | 33 | #include "user-util.h" |
1ee306e1 | 34 | |
74c308ae | 35 | static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_pool_path, "s", "/var/lib/machines"); |
160e3793 LP |
36 | |
37 | static int property_get_pool_usage( | |
38 | sd_bus *bus, | |
39 | const char *path, | |
40 | const char *interface, | |
41 | const char *property, | |
42 | sd_bus_message *reply, | |
43 | void *userdata, | |
44 | sd_bus_error *error) { | |
45 | ||
46 | _cleanup_close_ int fd = -1; | |
47 | uint64_t usage = (uint64_t) -1; | |
160e3793 LP |
48 | |
49 | assert(bus); | |
50 | assert(reply); | |
51 | ||
160e3793 LP |
52 | fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY); |
53 | if (fd >= 0) { | |
54 | BtrfsQuotaInfo q; | |
55 | ||
5bcd08db | 56 | if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0) |
cb81cd80 | 57 | usage = q.referenced; |
160e3793 LP |
58 | } |
59 | ||
160e3793 LP |
60 | return sd_bus_message_append(reply, "t", usage); |
61 | } | |
62 | ||
63 | static int property_get_pool_limit( | |
64 | sd_bus *bus, | |
65 | const char *path, | |
66 | const char *interface, | |
67 | const char *property, | |
68 | sd_bus_message *reply, | |
69 | void *userdata, | |
70 | sd_bus_error *error) { | |
71 | ||
72 | _cleanup_close_ int fd = -1; | |
73 | uint64_t size = (uint64_t) -1; | |
160e3793 LP |
74 | |
75 | assert(bus); | |
76 | assert(reply); | |
77 | ||
160e3793 LP |
78 | fd = open("/var/lib/machines", O_RDONLY|O_CLOEXEC|O_DIRECTORY); |
79 | if (fd >= 0) { | |
80 | BtrfsQuotaInfo q; | |
81 | ||
5bcd08db | 82 | if (btrfs_subvol_get_subtree_quota_fd(fd, 0, &q) >= 0) |
cb81cd80 | 83 | size = q.referenced_max; |
160e3793 LP |
84 | } |
85 | ||
160e3793 LP |
86 | return sd_bus_message_append(reply, "t", size); |
87 | } | |
88 | ||
19070062 | 89 | static int method_get_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 LP |
90 | _cleanup_free_ char *p = NULL; |
91 | Manager *m = userdata; | |
92 | Machine *machine; | |
93 | const char *name; | |
94 | int r; | |
95 | ||
c3350683 LP |
96 | assert(message); |
97 | assert(m); | |
98 | ||
99 | r = sd_bus_message_read(message, "s", &name); | |
100 | if (r < 0) | |
ebcf1f97 | 101 | return r; |
c3350683 LP |
102 | |
103 | machine = hashmap_get(m->machines, name); | |
104 | if (!machine) | |
ebcf1f97 | 105 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); |
c3350683 LP |
106 | |
107 | p = machine_bus_path(machine); | |
108 | if (!p) | |
ebcf1f97 | 109 | return -ENOMEM; |
c3350683 | 110 | |
df2d202e | 111 | return sd_bus_reply_method_return(message, "o", p); |
c3350683 LP |
112 | } |
113 | ||
19070062 | 114 | static int method_get_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c2ce6a3d | 115 | _cleanup_free_ char *p = NULL; |
0a0e594a | 116 | _unused_ Manager *m = userdata; |
c2ce6a3d LP |
117 | const char *name; |
118 | int r; | |
119 | ||
c2ce6a3d LP |
120 | assert(message); |
121 | assert(m); | |
122 | ||
123 | r = sd_bus_message_read(message, "s", &name); | |
124 | if (r < 0) | |
125 | return r; | |
126 | ||
5ef46e5f | 127 | r = image_find(IMAGE_MACHINE, name, NULL); |
3a6ce860 | 128 | if (r == -ENOENT) |
c2ce6a3d LP |
129 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name); |
130 | if (r < 0) | |
131 | return r; | |
132 | ||
133 | p = image_bus_path(name); | |
134 | if (!p) | |
135 | return -ENOMEM; | |
136 | ||
137 | return sd_bus_reply_method_return(message, "o", p); | |
138 | } | |
139 | ||
19070062 | 140 | static int method_get_machine_by_pid(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 LP |
141 | _cleanup_free_ char *p = NULL; |
142 | Manager *m = userdata; | |
143 | Machine *machine = NULL; | |
4e724d9c | 144 | pid_t pid; |
c3350683 LP |
145 | int r; |
146 | ||
c3350683 LP |
147 | assert(message); |
148 | assert(m); | |
149 | ||
4e724d9c LP |
150 | assert_cc(sizeof(pid_t) == sizeof(uint32_t)); |
151 | ||
c3350683 LP |
152 | r = sd_bus_message_read(message, "u", &pid); |
153 | if (r < 0) | |
ebcf1f97 | 154 | return r; |
c3350683 | 155 | |
07b38ba5 | 156 | if (pid < 0) |
06820eaf LP |
157 | return -EINVAL; |
158 | ||
4e724d9c | 159 | if (pid == 0) { |
4afd3348 | 160 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
5b12334d LP |
161 | |
162 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); | |
163 | if (r < 0) | |
164 | return r; | |
165 | ||
166 | r = sd_bus_creds_get_pid(creds, &pid); | |
4e724d9c | 167 | if (r < 0) |
ebcf1f97 | 168 | return r; |
4e724d9c LP |
169 | } |
170 | ||
c3350683 LP |
171 | r = manager_get_machine_by_pid(m, pid, &machine); |
172 | if (r < 0) | |
ebcf1f97 | 173 | return r; |
c3350683 | 174 | if (!machine) |
de0671ee | 175 | return sd_bus_error_setf(error, BUS_ERROR_NO_MACHINE_FOR_PID, "PID "PID_FMT" does not belong to any known machine", pid); |
c3350683 LP |
176 | |
177 | p = machine_bus_path(machine); | |
178 | if (!p) | |
ebcf1f97 | 179 | return -ENOMEM; |
c3350683 | 180 | |
df2d202e | 181 | return sd_bus_reply_method_return(message, "o", p); |
c3350683 LP |
182 | } |
183 | ||
19070062 | 184 | static int method_list_machines(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 185 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
c3350683 LP |
186 | Manager *m = userdata; |
187 | Machine *machine; | |
188 | Iterator i; | |
189 | int r; | |
190 | ||
c3350683 LP |
191 | assert(message); |
192 | assert(m); | |
193 | ||
df2d202e | 194 | r = sd_bus_message_new_method_return(message, &reply); |
c3350683 | 195 | if (r < 0) |
ebcf1f97 | 196 | return sd_bus_error_set_errno(error, r); |
c3350683 LP |
197 | |
198 | r = sd_bus_message_open_container(reply, 'a', "(ssso)"); | |
199 | if (r < 0) | |
ebcf1f97 | 200 | return sd_bus_error_set_errno(error, r); |
c3350683 LP |
201 | |
202 | HASHMAP_FOREACH(machine, m->machines, i) { | |
203 | _cleanup_free_ char *p = NULL; | |
204 | ||
205 | p = machine_bus_path(machine); | |
206 | if (!p) | |
ebcf1f97 | 207 | return -ENOMEM; |
c3350683 LP |
208 | |
209 | r = sd_bus_message_append(reply, "(ssso)", | |
210 | machine->name, | |
211 | strempty(machine_class_to_string(machine->class)), | |
212 | machine->service, | |
213 | p); | |
214 | if (r < 0) | |
ebcf1f97 | 215 | return sd_bus_error_set_errno(error, r); |
c3350683 | 216 | } |
1ee306e1 | 217 | |
c3350683 LP |
218 | r = sd_bus_message_close_container(reply); |
219 | if (r < 0) | |
ebcf1f97 | 220 | return sd_bus_error_set_errno(error, r); |
c3350683 | 221 | |
9030ca46 | 222 | return sd_bus_send(NULL, reply, NULL); |
c3350683 LP |
223 | } |
224 | ||
9b5ed6fe | 225 | static int method_create_or_register_machine(Manager *manager, sd_bus_message *message, bool read_network, Machine **_m, sd_bus_error *error) { |
8aec412f | 226 | const char *name, *service, *class, *root_directory; |
9b5ed6fe | 227 | const int32_t *netif = NULL; |
1ee306e1 LP |
228 | MachineClass c; |
229 | uint32_t leader; | |
230 | sd_id128_t id; | |
c3350683 | 231 | const void *v; |
1ee306e1 | 232 | Machine *m; |
9b5ed6fe | 233 | size_t n, n_netif = 0; |
c3350683 | 234 | int r; |
1ee306e1 | 235 | |
c3350683 | 236 | assert(manager); |
89f7c846 LP |
237 | assert(message); |
238 | assert(_m); | |
1ee306e1 | 239 | |
c3350683 LP |
240 | r = sd_bus_message_read(message, "s", &name); |
241 | if (r < 0) | |
ebcf1f97 | 242 | return r; |
7f0d207d | 243 | if (!machine_name_is_valid(name)) |
ebcf1f97 | 244 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine name"); |
1ee306e1 | 245 | |
c3350683 LP |
246 | r = sd_bus_message_read_array(message, 'y', &v, &n); |
247 | if (r < 0) | |
ebcf1f97 | 248 | return r; |
1ee306e1 LP |
249 | if (n == 0) |
250 | id = SD_ID128_NULL; | |
251 | else if (n == 16) | |
252 | memcpy(&id, v, n); | |
253 | else | |
ebcf1f97 | 254 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine ID parameter"); |
1ee306e1 | 255 | |
c3350683 LP |
256 | r = sd_bus_message_read(message, "ssus", &service, &class, &leader, &root_directory); |
257 | if (r < 0) | |
ebcf1f97 | 258 | return r; |
1ee306e1 | 259 | |
9b5ed6fe LP |
260 | if (read_network) { |
261 | size_t i; | |
262 | ||
263 | r = sd_bus_message_read_array(message, 'i', (const void**) &netif, &n_netif); | |
264 | if (r < 0) | |
265 | return r; | |
266 | ||
267 | n_netif /= sizeof(int32_t); | |
268 | ||
269 | for (i = 0; i < n_netif; i++) { | |
270 | if (netif[i] <= 0) | |
271 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid network interface index %i", netif[i]); | |
272 | } | |
273 | } | |
274 | ||
1ee306e1 LP |
275 | if (isempty(class)) |
276 | c = _MACHINE_CLASS_INVALID; | |
277 | else { | |
278 | c = machine_class_from_string(class); | |
279 | if (c < 0) | |
ebcf1f97 | 280 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid machine class parameter"); |
1ee306e1 LP |
281 | } |
282 | ||
c3350683 | 283 | if (leader == 1) |
ebcf1f97 | 284 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid leader PID"); |
1ee306e1 | 285 | |
c3350683 | 286 | if (!isempty(root_directory) && !path_is_absolute(root_directory)) |
ebcf1f97 | 287 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Root directory must be empty or an absolute path"); |
1ee306e1 | 288 | |
c3350683 | 289 | if (leader == 0) { |
4afd3348 | 290 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
5b12334d LP |
291 | |
292 | r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_PID, &creds); | |
293 | if (r < 0) | |
294 | return r; | |
295 | ||
c3350683 | 296 | assert_cc(sizeof(uint32_t) == sizeof(pid_t)); |
554604b3 | 297 | |
5b12334d | 298 | r = sd_bus_creds_get_pid(creds, (pid_t*) &leader); |
c3350683 | 299 | if (r < 0) |
ebcf1f97 | 300 | return r; |
c3350683 | 301 | } |
554604b3 | 302 | |
1ee306e1 | 303 | if (hashmap_get(manager->machines, name)) |
ebcf1f97 | 304 | return sd_bus_error_setf(error, BUS_ERROR_MACHINE_EXISTS, "Machine '%s' already exists", name); |
1ee306e1 LP |
305 | |
306 | r = manager_add_machine(manager, name, &m); | |
307 | if (r < 0) | |
ebcf1f97 | 308 | return r; |
1ee306e1 LP |
309 | |
310 | m->leader = leader; | |
311 | m->class = c; | |
312 | m->id = id; | |
313 | ||
314 | if (!isempty(service)) { | |
315 | m->service = strdup(service); | |
316 | if (!m->service) { | |
ebcf1f97 | 317 | r = -ENOMEM; |
1ee306e1 LP |
318 | goto fail; |
319 | } | |
320 | } | |
321 | ||
322 | if (!isempty(root_directory)) { | |
323 | m->root_directory = strdup(root_directory); | |
324 | if (!m->root_directory) { | |
ebcf1f97 | 325 | r = -ENOMEM; |
1ee306e1 LP |
326 | goto fail; |
327 | } | |
328 | } | |
329 | ||
9b5ed6fe LP |
330 | if (n_netif > 0) { |
331 | assert_cc(sizeof(int32_t) == sizeof(int)); | |
332 | m->netif = memdup(netif, sizeof(int32_t) * n_netif); | |
333 | if (!m->netif) { | |
334 | r = -ENOMEM; | |
335 | goto fail; | |
336 | } | |
337 | ||
338 | m->n_netif = n_netif; | |
339 | } | |
340 | ||
89f7c846 LP |
341 | *_m = m; |
342 | ||
343 | return 1; | |
344 | ||
345 | fail: | |
346 | machine_add_to_gc_queue(m); | |
347 | return r; | |
348 | } | |
349 | ||
19070062 | 350 | static int method_create_machine_internal(sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) { |
89f7c846 LP |
351 | Manager *manager = userdata; |
352 | Machine *m = NULL; | |
353 | int r; | |
354 | ||
19070062 LP |
355 | assert(message); |
356 | assert(manager); | |
357 | ||
9b5ed6fe | 358 | r = method_create_or_register_machine(manager, message, read_network, &m, error); |
89f7c846 LP |
359 | if (r < 0) |
360 | return r; | |
361 | ||
362 | r = sd_bus_message_enter_container(message, 'a', "(sv)"); | |
363 | if (r < 0) | |
364 | goto fail; | |
365 | ||
ebcf1f97 LP |
366 | r = machine_start(m, message, error); |
367 | if (r < 0) | |
1ee306e1 LP |
368 | goto fail; |
369 | ||
c3350683 | 370 | m->create_message = sd_bus_message_ref(message); |
c3350683 | 371 | return 1; |
1ee306e1 LP |
372 | |
373 | fail: | |
a3e7f417 | 374 | machine_add_to_gc_queue(m); |
89f7c846 LP |
375 | return r; |
376 | } | |
1ee306e1 | 377 | |
19070062 LP |
378 | static int method_create_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
379 | return method_create_machine_internal(message, true, userdata, error); | |
9b5ed6fe LP |
380 | } |
381 | ||
19070062 LP |
382 | static int method_create_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
383 | return method_create_machine_internal(message, false, userdata, error); | |
9b5ed6fe LP |
384 | } |
385 | ||
19070062 | 386 | static int method_register_machine_internal(sd_bus_message *message, bool read_network, void *userdata, sd_bus_error *error) { |
89f7c846 LP |
387 | Manager *manager = userdata; |
388 | _cleanup_free_ char *p = NULL; | |
389 | Machine *m = NULL; | |
390 | int r; | |
391 | ||
19070062 LP |
392 | assert(message); |
393 | assert(manager); | |
394 | ||
9b5ed6fe | 395 | r = method_create_or_register_machine(manager, message, read_network, &m, error); |
89f7c846 LP |
396 | if (r < 0) |
397 | return r; | |
398 | ||
399 | r = cg_pid_get_unit(m->leader, &m->unit); | |
400 | if (r < 0) { | |
048c386e ZJS |
401 | r = sd_bus_error_set_errnof(error, r, |
402 | "Failed to determine unit of process "PID_FMT" : %m", | |
403 | m->leader); | |
89f7c846 LP |
404 | goto fail; |
405 | } | |
406 | ||
407 | r = machine_start(m, NULL, error); | |
408 | if (r < 0) | |
409 | goto fail; | |
410 | ||
411 | p = machine_bus_path(m); | |
412 | if (!p) { | |
413 | r = -ENOMEM; | |
414 | goto fail; | |
415 | } | |
416 | ||
417 | return sd_bus_reply_method_return(message, "o", p); | |
418 | ||
419 | fail: | |
420 | machine_add_to_gc_queue(m); | |
1ee306e1 LP |
421 | return r; |
422 | } | |
423 | ||
19070062 LP |
424 | static int method_register_machine_with_network(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
425 | return method_register_machine_internal(message, true, userdata, error); | |
9b5ed6fe LP |
426 | } |
427 | ||
19070062 LP |
428 | static int method_register_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
429 | return method_register_machine_internal(message, false, userdata, error); | |
9b5ed6fe LP |
430 | } |
431 | ||
eecf0181 | 432 | static int redirect_method_to_machine(sd_bus_message *message, Manager *m, sd_bus_error *error, sd_bus_message_handler_t method) { |
c3350683 LP |
433 | Machine *machine; |
434 | const char *name; | |
1ee306e1 LP |
435 | int r; |
436 | ||
1ee306e1 LP |
437 | assert(message); |
438 | assert(m); | |
eecf0181 | 439 | assert(method); |
1ee306e1 | 440 | |
c3350683 LP |
441 | r = sd_bus_message_read(message, "s", &name); |
442 | if (r < 0) | |
ebcf1f97 | 443 | return sd_bus_error_set_errno(error, r); |
1ee306e1 | 444 | |
c3350683 LP |
445 | machine = hashmap_get(m->machines, name); |
446 | if (!machine) | |
ebcf1f97 | 447 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); |
1ee306e1 | 448 | |
eecf0181 | 449 | return method(message, machine, error); |
c3350683 | 450 | } |
1ee306e1 | 451 | |
ef8ff92e ZJS |
452 | static int method_unregister_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
453 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_unregister); | |
454 | } | |
455 | ||
eecf0181 LP |
456 | static int method_terminate_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
457 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_terminate); | |
458 | } | |
1ee306e1 | 459 | |
eecf0181 LP |
460 | static int method_kill_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
461 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_kill); | |
878cd7e9 LP |
462 | } |
463 | ||
19070062 | 464 | static int method_get_machine_addresses(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 465 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_addresses); |
c3350683 | 466 | } |
1ee306e1 | 467 | |
19070062 | 468 | static int method_get_machine_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 469 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_os_release); |
717603e3 LP |
470 | } |
471 | ||
19070062 | 472 | static int method_list_images(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
4afd3348 | 473 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
b07ec5a1 | 474 | _cleanup_hashmap_free_ Hashmap *images = NULL; |
0a0e594a | 475 | _unused_ Manager *m = userdata; |
cd61c3bf LP |
476 | Image *image; |
477 | Iterator i; | |
478 | int r; | |
479 | ||
cd61c3bf LP |
480 | assert(message); |
481 | assert(m); | |
482 | ||
b07ec5a1 | 483 | images = hashmap_new(&image_hash_ops); |
cd61c3bf LP |
484 | if (!images) |
485 | return -ENOMEM; | |
486 | ||
5ef46e5f | 487 | r = image_discover(IMAGE_MACHINE, images); |
cd61c3bf LP |
488 | if (r < 0) |
489 | return r; | |
490 | ||
491 | r = sd_bus_message_new_method_return(message, &reply); | |
492 | if (r < 0) | |
493 | return r; | |
494 | ||
b6b18498 | 495 | r = sd_bus_message_open_container(reply, 'a', "(ssbttto)"); |
cd61c3bf LP |
496 | if (r < 0) |
497 | return r; | |
498 | ||
499 | HASHMAP_FOREACH(image, images, i) { | |
500 | _cleanup_free_ char *p = NULL; | |
501 | ||
502 | p = image_bus_path(image->name); | |
503 | if (!p) | |
504 | return -ENOMEM; | |
505 | ||
b6b18498 | 506 | r = sd_bus_message_append(reply, "(ssbttto)", |
cd61c3bf LP |
507 | image->name, |
508 | image_type_to_string(image->type), | |
509 | image->read_only, | |
10f9c755 LP |
510 | image->crtime, |
511 | image->mtime, | |
c19de711 | 512 | image->usage, |
cd61c3bf LP |
513 | p); |
514 | if (r < 0) | |
515 | return r; | |
516 | } | |
517 | ||
518 | r = sd_bus_message_close_container(reply); | |
519 | if (r < 0) | |
520 | return r; | |
521 | ||
9030ca46 | 522 | return sd_bus_send(NULL, reply, NULL); |
cd61c3bf LP |
523 | } |
524 | ||
19070062 | 525 | static int method_open_machine_pty(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 526 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_pty); |
40205d70 LP |
527 | } |
528 | ||
19070062 | 529 | static int method_open_machine_login(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 530 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_login); |
5f8cc96a LP |
531 | } |
532 | ||
49af9e13 | 533 | static int method_open_machine_shell(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 534 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_shell); |
49af9e13 LP |
535 | } |
536 | ||
19070062 | 537 | static int method_bind_mount_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 538 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_bind_mount); |
90adaa25 LP |
539 | } |
540 | ||
19070062 | 541 | static int method_copy_machine(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 542 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_copy); |
0370612e LP |
543 | } |
544 | ||
ae203207 | 545 | static int method_open_machine_root_directory(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 546 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_open_root_directory); |
ae203207 LP |
547 | } |
548 | ||
3401419b | 549 | static int method_get_machine_uid_shift(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
eecf0181 | 550 | return redirect_method_to_machine(message, userdata, error, bus_machine_method_get_uid_shift); |
3401419b LP |
551 | } |
552 | ||
9b06c1e1 | 553 | static int redirect_method_to_image(sd_bus_message *message, Manager *m, sd_bus_error *error, sd_bus_message_handler_t method) { |
08682124 LP |
554 | _cleanup_(image_unrefp) Image* i = NULL; |
555 | const char *name; | |
556 | int r; | |
557 | ||
08682124 | 558 | assert(message); |
9b06c1e1 LP |
559 | assert(m); |
560 | assert(method); | |
08682124 LP |
561 | |
562 | r = sd_bus_message_read(message, "s", &name); | |
563 | if (r < 0) | |
564 | return r; | |
565 | ||
566 | if (!image_name_is_valid(name)) | |
567 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Image name '%s' is invalid.", name); | |
568 | ||
5ef46e5f | 569 | r = image_find(IMAGE_MACHINE, name, &i); |
3a6ce860 LP |
570 | if (r == -ENOENT) |
571 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_IMAGE, "No image '%s' known", name); | |
08682124 LP |
572 | if (r < 0) |
573 | return r; | |
08682124 | 574 | |
9b06c1e1 LP |
575 | i->userdata = m; |
576 | return method(message, i, error); | |
08682124 LP |
577 | } |
578 | ||
9b06c1e1 LP |
579 | static int method_remove_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
580 | return redirect_method_to_image(message, userdata, error, bus_image_method_remove); | |
581 | } | |
ebd93cb6 | 582 | |
9b06c1e1 LP |
583 | static int method_rename_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
584 | return redirect_method_to_image(message, userdata, error, bus_image_method_rename); | |
ebd93cb6 LP |
585 | } |
586 | ||
19070062 | 587 | static int method_clone_image(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
9b06c1e1 | 588 | return redirect_method_to_image(message, userdata, error, bus_image_method_clone); |
ebd93cb6 LP |
589 | } |
590 | ||
19070062 | 591 | static int method_mark_image_read_only(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
9b06c1e1 | 592 | return redirect_method_to_image(message, userdata, error, bus_image_method_mark_read_only); |
ebd93cb6 LP |
593 | } |
594 | ||
cf30a8c1 | 595 | static int method_get_image_hostname(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
9b06c1e1 | 596 | return redirect_method_to_image(message, userdata, error, bus_image_method_get_hostname); |
cf30a8c1 LP |
597 | } |
598 | ||
599 | static int method_get_image_machine_id(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
9b06c1e1 | 600 | return redirect_method_to_image(message, userdata, error, bus_image_method_get_machine_id); |
cf30a8c1 LP |
601 | } |
602 | ||
603 | static int method_get_image_machine_info(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
9b06c1e1 | 604 | return redirect_method_to_image(message, userdata, error, bus_image_method_get_machine_info); |
cf30a8c1 LP |
605 | } |
606 | ||
9153b02b | 607 | static int method_get_image_os_release(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
9b06c1e1 | 608 | return redirect_method_to_image(message, userdata, error, bus_image_method_get_os_release); |
9153b02b LP |
609 | } |
610 | ||
03c2b288 LP |
611 | static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) { |
612 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
613 | _cleanup_fclose_ FILE *f = NULL; | |
614 | bool success; | |
615 | size_t n; | |
616 | int r; | |
617 | ||
618 | assert(operation); | |
619 | assert(operation->extra_fd >= 0); | |
620 | ||
621 | if (lseek(operation->extra_fd, 0, SEEK_SET) == (off_t) -1) | |
622 | return -errno; | |
623 | ||
4fa744a3 | 624 | f = take_fdopen(&operation->extra_fd, "r"); |
03c2b288 LP |
625 | if (!f) |
626 | return -errno; | |
627 | ||
03c2b288 LP |
628 | /* The resulting temporary file starts with a boolean value that indicates success or not. */ |
629 | errno = 0; | |
630 | n = fread(&success, 1, sizeof(success), f); | |
631 | if (n != sizeof(success)) | |
66855de7 | 632 | return ret < 0 ? ret : errno_or_else(EIO); |
03c2b288 LP |
633 | |
634 | if (ret < 0) { | |
635 | _cleanup_free_ char *name = NULL; | |
636 | ||
637 | /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean | |
638 | * set to false followed by the name of the failed image. Let's try to read this and use it for the | |
639 | * error message. If we can't read it, don't mind, and return the naked error. */ | |
640 | ||
641 | if (success) /* The resulting temporary file could not be updated, ignore it. */ | |
642 | return ret; | |
643 | ||
41f11239 LP |
644 | r = read_nul_string(f, LONG_LINE_MAX, &name); |
645 | if (r <= 0) /* Same here... */ | |
03c2b288 LP |
646 | return ret; |
647 | ||
648 | return sd_bus_error_set_errnof(error, ret, "Failed to remove image %s: %m", name); | |
649 | } | |
650 | ||
651 | assert(success); | |
652 | ||
653 | r = sd_bus_message_new_method_return(operation->message, &reply); | |
654 | if (r < 0) | |
655 | return r; | |
656 | ||
657 | r = sd_bus_message_open_container(reply, 'a', "(st)"); | |
658 | if (r < 0) | |
659 | return r; | |
660 | ||
661 | /* On success the resulting temporary file will contain a list of image names that were removed followed by | |
662 | * their size on disk. Let's read that and turn it into a bus message. */ | |
663 | for (;;) { | |
664 | _cleanup_free_ char *name = NULL; | |
665 | uint64_t size; | |
666 | ||
41f11239 | 667 | r = read_nul_string(f, LONG_LINE_MAX, &name); |
03c2b288 LP |
668 | if (r < 0) |
669 | return r; | |
41f11239 | 670 | if (r == 0) /* reached the end */ |
03c2b288 LP |
671 | break; |
672 | ||
673 | errno = 0; | |
674 | n = fread(&size, 1, sizeof(size), f); | |
675 | if (n != sizeof(size)) | |
66855de7 | 676 | return errno_or_else(EIO); |
03c2b288 LP |
677 | |
678 | r = sd_bus_message_append(reply, "(st)", name, size); | |
679 | if (r < 0) | |
680 | return r; | |
681 | } | |
682 | ||
683 | r = sd_bus_message_close_container(reply); | |
684 | if (r < 0) | |
685 | return r; | |
686 | ||
687 | return sd_bus_send(NULL, reply, NULL); | |
688 | } | |
689 | ||
d94c2b06 LP |
690 | static int method_clean_pool(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
691 | enum { | |
692 | REMOVE_ALL, | |
693 | REMOVE_HIDDEN, | |
694 | } mode; | |
695 | ||
03c2b288 LP |
696 | _cleanup_close_pair_ int errno_pipe_fd[2] = { -1, -1 }; |
697 | _cleanup_close_ int result_fd = -1; | |
d94c2b06 | 698 | Manager *m = userdata; |
03c2b288 | 699 | Operation *operation; |
d94c2b06 | 700 | const char *mm; |
03c2b288 | 701 | pid_t child; |
d94c2b06 LP |
702 | int r; |
703 | ||
704 | assert(message); | |
705 | ||
03c2b288 LP |
706 | if (m->n_operations >= OPERATIONS_MAX) |
707 | return sd_bus_error_setf(error, SD_BUS_ERROR_LIMITS_EXCEEDED, "Too many ongoing operations."); | |
708 | ||
d94c2b06 LP |
709 | r = sd_bus_message_read(message, "s", &mm); |
710 | if (r < 0) | |
711 | return r; | |
712 | ||
713 | if (streq(mm, "all")) | |
714 | mode = REMOVE_ALL; | |
715 | else if (streq(mm, "hidden")) | |
716 | mode = REMOVE_HIDDEN; | |
717 | else | |
718 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown mode '%s'.", mm); | |
719 | ||
720 | r = bus_verify_polkit_async( | |
721 | message, | |
722 | CAP_SYS_ADMIN, | |
723 | "org.freedesktop.machine1.manage-machines", | |
724 | NULL, | |
725 | false, | |
726 | UID_INVALID, | |
727 | &m->polkit_registry, | |
728 | error); | |
729 | if (r < 0) | |
730 | return r; | |
731 | if (r == 0) | |
732 | return 1; /* Will call us back */ | |
733 | ||
03c2b288 LP |
734 | if (pipe2(errno_pipe_fd, O_CLOEXEC|O_NONBLOCK) < 0) |
735 | return sd_bus_error_set_errnof(error, errno, "Failed to create pipe: %m"); | |
d94c2b06 | 736 | |
03c2b288 LP |
737 | /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this |
738 | * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this | |
595bfe7d | 739 | * continuously */ |
992e8f22 | 740 | result_fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC); |
03c2b288 LP |
741 | if (result_fd < 0) |
742 | return -errno; | |
d94c2b06 | 743 | |
03c2b288 | 744 | /* This might be a slow operation, run it asynchronously in a background process */ |
4c253ed1 LP |
745 | r = safe_fork("(sd-clean)", FORK_RESET_SIGNALS, &child); |
746 | if (r < 0) | |
747 | return sd_bus_error_set_errnof(error, r, "Failed to fork(): %m"); | |
748 | if (r == 0) { | |
b07ec5a1 | 749 | _cleanup_hashmap_free_ Hashmap *images = NULL; |
03c2b288 LP |
750 | bool success = true; |
751 | Image *image; | |
752 | Iterator i; | |
753 | ssize_t l; | |
d94c2b06 | 754 | |
03c2b288 | 755 | errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); |
d94c2b06 | 756 | |
b07ec5a1 | 757 | images = hashmap_new(&image_hash_ops); |
03c2b288 LP |
758 | if (!images) { |
759 | r = -ENOMEM; | |
760 | goto child_fail; | |
761 | } | |
d94c2b06 | 762 | |
5ef46e5f | 763 | r = image_discover(IMAGE_MACHINE, images); |
03c2b288 LP |
764 | if (r < 0) |
765 | goto child_fail; | |
d94c2b06 | 766 | |
03c2b288 LP |
767 | l = write(result_fd, &success, sizeof(success)); |
768 | if (l < 0) { | |
769 | r = -errno; | |
770 | goto child_fail; | |
771 | } | |
d94c2b06 | 772 | |
03c2b288 LP |
773 | HASHMAP_FOREACH(image, images, i) { |
774 | ||
775 | /* We can't remove vendor images (i.e. those in /usr) */ | |
776 | if (IMAGE_IS_VENDOR(image)) | |
777 | continue; | |
778 | ||
779 | if (IMAGE_IS_HOST(image)) | |
780 | continue; | |
781 | ||
782 | if (mode == REMOVE_HIDDEN && !IMAGE_IS_HIDDEN(image)) | |
783 | continue; | |
784 | ||
785 | r = image_remove(image); | |
786 | if (r == -EBUSY) /* keep images that are currently being used. */ | |
787 | continue; | |
788 | if (r < 0) { | |
789 | /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */ | |
790 | success = false; | |
791 | (void) ftruncate(result_fd, 0); | |
792 | (void) lseek(result_fd, 0, SEEK_SET); | |
793 | (void) write(result_fd, &success, sizeof(success)); | |
794 | (void) write(result_fd, image->name, strlen(image->name)+1); | |
795 | goto child_fail; | |
796 | } | |
797 | ||
798 | l = write(result_fd, image->name, strlen(image->name)+1); | |
799 | if (l < 0) { | |
800 | r = -errno; | |
801 | goto child_fail; | |
802 | } | |
803 | ||
804 | l = write(result_fd, &image->usage_exclusive, sizeof(image->usage_exclusive)); | |
805 | if (l < 0) { | |
806 | r = -errno; | |
807 | goto child_fail; | |
808 | } | |
809 | } | |
d94c2b06 | 810 | |
03c2b288 LP |
811 | result_fd = safe_close(result_fd); |
812 | _exit(EXIT_SUCCESS); | |
813 | ||
814 | child_fail: | |
815 | (void) write(errno_pipe_fd[1], &r, sizeof(r)); | |
816 | _exit(EXIT_FAILURE); | |
d94c2b06 LP |
817 | } |
818 | ||
03c2b288 LP |
819 | errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); |
820 | ||
821 | /* The clean-up might take a while, hence install a watch on the child and return */ | |
822 | ||
823 | r = operation_new(m, NULL, child, message, errno_pipe_fd[0], &operation); | |
824 | if (r < 0) { | |
825 | (void) sigkill_wait(child); | |
d94c2b06 | 826 | return r; |
03c2b288 | 827 | } |
d94c2b06 | 828 | |
03c2b288 LP |
829 | operation->extra_fd = result_fd; |
830 | operation->done = clean_pool_done; | |
831 | ||
832 | result_fd = -1; | |
833 | errno_pipe_fd[0] = -1; | |
834 | ||
835 | return 1; | |
d94c2b06 LP |
836 | } |
837 | ||
19070062 | 838 | static int method_set_pool_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
d6ce17c7 LP |
839 | Manager *m = userdata; |
840 | uint64_t limit; | |
841 | int r; | |
842 | ||
19070062 LP |
843 | assert(message); |
844 | ||
d6ce17c7 LP |
845 | r = sd_bus_message_read(message, "t", &limit); |
846 | if (r < 0) | |
847 | return r; | |
a90fb858 LP |
848 | if (!FILE_SIZE_VALID_OR_INFINITY(limit)) |
849 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "New limit out of range"); | |
d6ce17c7 LP |
850 | |
851 | r = bus_verify_polkit_async( | |
852 | message, | |
853 | CAP_SYS_ADMIN, | |
854 | "org.freedesktop.machine1.manage-machines", | |
403ed0e5 | 855 | NULL, |
d6ce17c7 LP |
856 | false, |
857 | UID_INVALID, | |
858 | &m->polkit_registry, | |
859 | error); | |
860 | if (r < 0) | |
861 | return r; | |
862 | if (r == 0) | |
863 | return 1; /* Will call us back */ | |
864 | ||
4cee5eed | 865 | /* Set up the machine directory if necessary */ |
5f7ecd61 | 866 | r = setup_machine_directory(error); |
4cee5eed LP |
867 | if (r < 0) |
868 | return r; | |
869 | ||
5bcd08db LP |
870 | (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit); |
871 | ||
872 | r = btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit); | |
d6ce17c7 LP |
873 | if (r == -ENOTTY) |
874 | return sd_bus_error_setf(error, SD_BUS_ERROR_NOT_SUPPORTED, "Quota is only supported on btrfs."); | |
26166c88 | 875 | if (r < 0) |
d6ce17c7 LP |
876 | return sd_bus_error_set_errnof(error, r, "Failed to adjust quota limit: %m"); |
877 | ||
878 | return sd_bus_reply_method_return(message, NULL); | |
879 | } | |
880 | ||
19070062 | 881 | static int method_set_image_limit(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
9b06c1e1 | 882 | return redirect_method_to_image(message, userdata, error, bus_image_method_set_limit); |
d6ce17c7 LP |
883 | } |
884 | ||
c01ff965 LP |
885 | static int method_map_from_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
886 | _cleanup_fclose_ FILE *f = NULL; | |
887 | Manager *m = userdata; | |
888 | const char *name, *p; | |
889 | Machine *machine; | |
890 | uint32_t uid; | |
891 | int r; | |
892 | ||
893 | r = sd_bus_message_read(message, "su", &name, &uid); | |
894 | if (r < 0) | |
895 | return r; | |
896 | ||
c077529b | 897 | if (!uid_is_valid(uid)) |
c01ff965 LP |
898 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid); |
899 | ||
900 | machine = hashmap_get(m->machines, name); | |
901 | if (!machine) | |
902 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
903 | ||
a79366e2 LP |
904 | if (machine->class != MACHINE_CONTAINER) |
905 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines."); | |
906 | ||
c01ff965 LP |
907 | p = procfs_file_alloca(machine->leader, "uid_map"); |
908 | f = fopen(p, "re"); | |
909 | if (!f) | |
910 | return -errno; | |
911 | ||
912 | for (;;) { | |
913 | uid_t uid_base, uid_shift, uid_range, converted; | |
914 | int k; | |
915 | ||
916 | errno = 0; | |
917 | k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range); | |
918 | if (k < 0 && feof(f)) | |
919 | break; | |
920 | if (k != 3) { | |
66855de7 LP |
921 | if (ferror(f)) |
922 | return errno_or_else(EIO); | |
c01ff965 LP |
923 | |
924 | return -EIO; | |
925 | } | |
926 | ||
927 | if (uid < uid_base || uid >= uid_base + uid_range) | |
928 | continue; | |
929 | ||
930 | converted = uid - uid_base + uid_shift; | |
c077529b | 931 | if (!uid_is_valid(converted)) |
c01ff965 LP |
932 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid); |
933 | ||
934 | return sd_bus_reply_method_return(message, "u", (uint32_t) converted); | |
935 | } | |
936 | ||
937 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "Machine '%s' has no matching user mappings.", name); | |
938 | } | |
939 | ||
940 | static int method_map_to_machine_user(sd_bus_message *message, void *userdata, sd_bus_error *error) { | |
941 | Manager *m = userdata; | |
942 | Machine *machine; | |
943 | uid_t uid; | |
944 | Iterator i; | |
945 | int r; | |
946 | ||
947 | r = sd_bus_message_read(message, "u", &uid); | |
948 | if (r < 0) | |
949 | return r; | |
c077529b | 950 | if (!uid_is_valid(uid)) |
c01ff965 LP |
951 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid); |
952 | if (uid < 0x10000) | |
953 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "User " UID_FMT " belongs to host UID range", uid); | |
954 | ||
955 | HASHMAP_FOREACH(machine, m->machines, i) { | |
956 | _cleanup_fclose_ FILE *f = NULL; | |
fbd0b64f | 957 | char p[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t) + 1]; |
c01ff965 | 958 | |
a79366e2 LP |
959 | if (machine->class != MACHINE_CONTAINER) |
960 | continue; | |
961 | ||
c01ff965 LP |
962 | xsprintf(p, "/proc/" UID_FMT "/uid_map", machine->leader); |
963 | f = fopen(p, "re"); | |
964 | if (!f) { | |
fe4a1d0f | 965 | log_warning_errno(errno, "Failed to open %s, ignoring,", p); |
c01ff965 LP |
966 | continue; |
967 | } | |
968 | ||
969 | for (;;) { | |
970 | _cleanup_free_ char *o = NULL; | |
971 | uid_t uid_base, uid_shift, uid_range, converted; | |
972 | int k; | |
973 | ||
974 | errno = 0; | |
975 | k = fscanf(f, UID_FMT " " UID_FMT " " UID_FMT, &uid_base, &uid_shift, &uid_range); | |
976 | if (k < 0 && feof(f)) | |
977 | break; | |
978 | if (k != 3) { | |
66855de7 LP |
979 | if (ferror(f)) |
980 | return errno_or_else(EIO); | |
c01ff965 LP |
981 | |
982 | return -EIO; | |
983 | } | |
984 | ||
24f5a4c7 YW |
985 | /* The private user namespace is disabled, ignoring. */ |
986 | if (uid_shift == 0) | |
987 | continue; | |
988 | ||
c01ff965 LP |
989 | if (uid < uid_shift || uid >= uid_shift + uid_range) |
990 | continue; | |
991 | ||
992 | converted = (uid - uid_shift + uid_base); | |
c077529b | 993 | if (!uid_is_valid(converted)) |
c01ff965 LP |
994 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid user ID " UID_FMT, uid); |
995 | ||
996 | o = machine_bus_path(machine); | |
997 | if (!o) | |
998 | return -ENOMEM; | |
999 | ||
1000 | return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted); | |
1001 | } | |
1002 | } | |
1003 | ||
1004 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_USER_MAPPING, "No matching user mapping for " UID_FMT ".", uid); | |
1005 | } | |
1006 | ||
1007 | static int method_map_from_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) { | |
1008 | _cleanup_fclose_ FILE *f = NULL; | |
1009 | Manager *m = groupdata; | |
1010 | const char *name, *p; | |
1011 | Machine *machine; | |
1012 | uint32_t gid; | |
1013 | int r; | |
1014 | ||
1015 | r = sd_bus_message_read(message, "su", &name, &gid); | |
1016 | if (r < 0) | |
1017 | return r; | |
1018 | ||
c077529b | 1019 | if (!gid_is_valid(gid)) |
c01ff965 LP |
1020 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid); |
1021 | ||
1022 | machine = hashmap_get(m->machines, name); | |
1023 | if (!machine) | |
1024 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_MACHINE, "No machine '%s' known", name); | |
1025 | ||
a79366e2 LP |
1026 | if (machine->class != MACHINE_CONTAINER) |
1027 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Not supported for non-container machines."); | |
1028 | ||
c01ff965 LP |
1029 | p = procfs_file_alloca(machine->leader, "gid_map"); |
1030 | f = fopen(p, "re"); | |
1031 | if (!f) | |
1032 | return -errno; | |
1033 | ||
1034 | for (;;) { | |
1035 | gid_t gid_base, gid_shift, gid_range, converted; | |
1036 | int k; | |
1037 | ||
1038 | errno = 0; | |
1039 | k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT, &gid_base, &gid_shift, &gid_range); | |
1040 | if (k < 0 && feof(f)) | |
1041 | break; | |
1042 | if (k != 3) { | |
66855de7 LP |
1043 | if (ferror(f)) |
1044 | return errno_or_else(EIO); | |
c01ff965 LP |
1045 | |
1046 | return -EIO; | |
1047 | } | |
1048 | ||
1049 | if (gid < gid_base || gid >= gid_base + gid_range) | |
1050 | continue; | |
1051 | ||
1052 | converted = gid - gid_base + gid_shift; | |
c077529b | 1053 | if (!gid_is_valid(converted)) |
c01ff965 LP |
1054 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid); |
1055 | ||
1056 | return sd_bus_reply_method_return(message, "u", (uint32_t) converted); | |
1057 | } | |
1058 | ||
1059 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Machine '%s' has no matching group mappings.", name); | |
1060 | } | |
1061 | ||
1062 | static int method_map_to_machine_group(sd_bus_message *message, void *groupdata, sd_bus_error *error) { | |
1063 | Manager *m = groupdata; | |
1064 | Machine *machine; | |
1065 | gid_t gid; | |
1066 | Iterator i; | |
1067 | int r; | |
1068 | ||
1069 | r = sd_bus_message_read(message, "u", &gid); | |
1070 | if (r < 0) | |
1071 | return r; | |
c077529b | 1072 | if (!gid_is_valid(gid)) |
c01ff965 LP |
1073 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid); |
1074 | if (gid < 0x10000) | |
1075 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "Group " GID_FMT " belongs to host GID range", gid); | |
1076 | ||
1077 | HASHMAP_FOREACH(machine, m->machines, i) { | |
1078 | _cleanup_fclose_ FILE *f = NULL; | |
fbd0b64f | 1079 | char p[STRLEN("/proc//gid_map") + DECIMAL_STR_MAX(pid_t) + 1]; |
c01ff965 | 1080 | |
a79366e2 LP |
1081 | if (machine->class != MACHINE_CONTAINER) |
1082 | continue; | |
1083 | ||
c01ff965 LP |
1084 | xsprintf(p, "/proc/" GID_FMT "/gid_map", machine->leader); |
1085 | f = fopen(p, "re"); | |
1086 | if (!f) { | |
fe4a1d0f | 1087 | log_warning_errno(errno, "Failed to open %s, ignoring,", p); |
c01ff965 LP |
1088 | continue; |
1089 | } | |
1090 | ||
1091 | for (;;) { | |
1092 | _cleanup_free_ char *o = NULL; | |
1093 | gid_t gid_base, gid_shift, gid_range, converted; | |
1094 | int k; | |
1095 | ||
1096 | errno = 0; | |
1097 | k = fscanf(f, GID_FMT " " GID_FMT " " GID_FMT, &gid_base, &gid_shift, &gid_range); | |
1098 | if (k < 0 && feof(f)) | |
1099 | break; | |
1100 | if (k != 3) { | |
66855de7 LP |
1101 | if (ferror(f)) |
1102 | return errno_or_else(EIO); | |
c01ff965 LP |
1103 | |
1104 | return -EIO; | |
1105 | } | |
1106 | ||
24f5a4c7 YW |
1107 | /* The private user namespace is disabled, ignoring. */ |
1108 | if (gid_shift == 0) | |
1109 | continue; | |
1110 | ||
c01ff965 LP |
1111 | if (gid < gid_shift || gid >= gid_shift + gid_range) |
1112 | continue; | |
1113 | ||
1114 | converted = (gid - gid_shift + gid_base); | |
c077529b | 1115 | if (!gid_is_valid(converted)) |
c01ff965 LP |
1116 | return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid group ID " GID_FMT, gid); |
1117 | ||
1118 | o = machine_bus_path(machine); | |
1119 | if (!o) | |
1120 | return -ENOMEM; | |
1121 | ||
1122 | return sd_bus_reply_method_return(message, "sou", machine->name, o, (uint32_t) converted); | |
1123 | } | |
1124 | } | |
1125 | ||
1126 | return sd_bus_error_setf(error, BUS_ERROR_NO_SUCH_GROUP_MAPPING, "No matching group mapping for " GID_FMT ".", gid); | |
1127 | } | |
1128 | ||
c3350683 LP |
1129 | const sd_bus_vtable manager_vtable[] = { |
1130 | SD_BUS_VTABLE_START(0), | |
bbe17ca1 | 1131 | |
160e3793 LP |
1132 | SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path, 0, 0), |
1133 | SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage, 0, 0), | |
1134 | SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit, 0, 0), | |
bbe17ca1 ZJS |
1135 | |
1136 | SD_BUS_METHOD_WITH_NAMES("GetMachine", | |
1137 | "s", | |
1138 | SD_BUS_PARAM(name), | |
1139 | "o", | |
1140 | SD_BUS_PARAM(machine), | |
1141 | method_get_machine, | |
1142 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1143 | SD_BUS_METHOD_WITH_NAMES("GetImage", | |
1144 | "s", | |
1145 | SD_BUS_PARAM(name), | |
1146 | "o", | |
1147 | SD_BUS_PARAM(image), | |
1148 | method_get_image, | |
1149 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1150 | SD_BUS_METHOD_WITH_NAMES("GetMachineByPID", | |
1151 | "u", | |
1152 | SD_BUS_PARAM(pid), | |
1153 | "o", | |
1154 | SD_BUS_PARAM(machine), | |
1155 | method_get_machine_by_pid, | |
1156 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1157 | SD_BUS_METHOD_WITH_NAMES("ListMachines", | |
1158 | NULL,, | |
1159 | "a(ssso)", | |
1160 | SD_BUS_PARAM(machines), | |
1161 | method_list_machines, | |
1162 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1163 | SD_BUS_METHOD_WITH_NAMES("ListImages", | |
1164 | NULL,, | |
1165 | "a(ssbttto)", | |
1166 | SD_BUS_PARAM(images), | |
1167 | method_list_images, | |
1168 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1169 | SD_BUS_METHOD_WITH_NAMES("CreateMachine", | |
1170 | "sayssusa(sv)", | |
1171 | SD_BUS_PARAM(name) | |
1172 | SD_BUS_PARAM(id) | |
1173 | SD_BUS_PARAM(service) | |
1174 | SD_BUS_PARAM(class) | |
1175 | SD_BUS_PARAM(leader) | |
1176 | SD_BUS_PARAM(root_directory) | |
1177 | SD_BUS_PARAM(scope_properties), | |
1178 | "o", | |
1179 | SD_BUS_PARAM(path), | |
1180 | method_create_machine, 0), | |
1181 | SD_BUS_METHOD_WITH_NAMES("CreateMachineWithNetwork", | |
1182 | "sayssusaia(sv)", | |
1183 | SD_BUS_PARAM(name) | |
1184 | SD_BUS_PARAM(id) | |
1185 | SD_BUS_PARAM(service) | |
1186 | SD_BUS_PARAM(class) | |
1187 | SD_BUS_PARAM(leader) | |
1188 | SD_BUS_PARAM(root_directory) | |
1189 | SD_BUS_PARAM(ifindices) | |
1190 | SD_BUS_PARAM(scope_properties), | |
1191 | "o", | |
1192 | SD_BUS_PARAM(path), | |
1193 | method_create_machine_with_network, 0), | |
1194 | SD_BUS_METHOD_WITH_NAMES("RegisterMachine", | |
1195 | "sayssus", | |
1196 | SD_BUS_PARAM(name) | |
1197 | SD_BUS_PARAM(id) | |
1198 | SD_BUS_PARAM(service) | |
1199 | SD_BUS_PARAM(class) | |
1200 | SD_BUS_PARAM(leader) | |
1201 | SD_BUS_PARAM(root_directory), | |
1202 | "o", | |
1203 | SD_BUS_PARAM(path), | |
1204 | method_register_machine, 0), | |
1205 | SD_BUS_METHOD_WITH_NAMES("RegisterMachineWithNetwork", | |
1206 | "sayssusai", | |
1207 | SD_BUS_PARAM(name) | |
1208 | SD_BUS_PARAM(id) | |
1209 | SD_BUS_PARAM(service) | |
1210 | SD_BUS_PARAM(class) | |
1211 | SD_BUS_PARAM(leader) | |
1212 | SD_BUS_PARAM(root_directory) | |
1213 | SD_BUS_PARAM(ifindices), | |
1214 | "o", | |
1215 | SD_BUS_PARAM(path), | |
1216 | method_register_machine_with_network, 0), | |
1217 | SD_BUS_METHOD_WITH_NAMES("UnregisterMachine", | |
1218 | "s", | |
1219 | SD_BUS_PARAM(name), | |
1220 | NULL,, | |
1221 | method_unregister_machine, | |
1222 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1223 | SD_BUS_METHOD_WITH_NAMES("TerminateMachine", | |
1224 | "s", | |
1225 | SD_BUS_PARAM(id), | |
1226 | NULL,, | |
1227 | method_terminate_machine, | |
1228 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1229 | SD_BUS_METHOD_WITH_NAMES("KillMachine", | |
1230 | "ssi", | |
1231 | SD_BUS_PARAM(name) | |
1232 | SD_BUS_PARAM(who) | |
1233 | SD_BUS_PARAM(signal), | |
1234 | NULL,, | |
1235 | method_kill_machine, | |
1236 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1237 | SD_BUS_METHOD_WITH_NAMES("GetMachineAddresses", | |
1238 | "s", | |
1239 | SD_BUS_PARAM(name), | |
1240 | "a(iay)", | |
1241 | SD_BUS_PARAM(addresses), | |
1242 | method_get_machine_addresses, | |
1243 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1244 | SD_BUS_METHOD_WITH_NAMES("GetMachineOSRelease", | |
1245 | "s", | |
1246 | SD_BUS_PARAM(name), | |
1247 | "a{ss}", | |
1248 | SD_BUS_PARAM(fields), | |
1249 | method_get_machine_os_release, | |
1250 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1251 | SD_BUS_METHOD_WITH_NAMES("OpenMachinePTY", | |
1252 | "s", | |
1253 | SD_BUS_PARAM(name), | |
1254 | "hs", | |
1255 | SD_BUS_PARAM(pty) | |
1256 | SD_BUS_PARAM(pty_path), | |
1257 | method_open_machine_pty, | |
1258 | 0), | |
1259 | SD_BUS_METHOD_WITH_NAMES("OpenMachineLogin", | |
1260 | "s", | |
1261 | SD_BUS_PARAM(name), | |
1262 | "hs", | |
1263 | SD_BUS_PARAM(pty) | |
1264 | SD_BUS_PARAM(pty_path), | |
1265 | method_open_machine_login, | |
1266 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1267 | SD_BUS_METHOD_WITH_NAMES("OpenMachineShell", | |
1268 | "sssasas", | |
1269 | SD_BUS_PARAM(name) | |
1270 | SD_BUS_PARAM(user) | |
1271 | SD_BUS_PARAM(path) | |
1272 | SD_BUS_PARAM(args) | |
1273 | SD_BUS_PARAM(environment), | |
1274 | "hs", | |
1275 | SD_BUS_PARAM(pty) | |
1276 | SD_BUS_PARAM(pty_path), | |
1277 | method_open_machine_shell, | |
1278 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1279 | SD_BUS_METHOD_WITH_NAMES("BindMountMachine", | |
1280 | "sssbb", | |
1281 | SD_BUS_PARAM(name) | |
1282 | SD_BUS_PARAM(source) | |
1283 | SD_BUS_PARAM(destination) | |
1284 | SD_BUS_PARAM(read_only) | |
1285 | SD_BUS_PARAM(mkdir), | |
1286 | NULL,, | |
1287 | method_bind_mount_machine, | |
1288 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1289 | SD_BUS_METHOD_WITH_NAMES("CopyFromMachine", | |
1290 | "sss", | |
1291 | SD_BUS_PARAM(name) | |
1292 | SD_BUS_PARAM(source) | |
1293 | SD_BUS_PARAM(destination), | |
1294 | NULL,, | |
1295 | method_copy_machine, | |
1296 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1297 | SD_BUS_METHOD_WITH_NAMES("CopyToMachine", | |
1298 | "sss", | |
1299 | SD_BUS_PARAM(name) | |
1300 | SD_BUS_PARAM(source) | |
1301 | SD_BUS_PARAM(destination), | |
1302 | NULL,, | |
1303 | method_copy_machine, | |
1304 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1305 | SD_BUS_METHOD_WITH_NAMES("OpenMachineRootDirectory", | |
1306 | "s", | |
1307 | SD_BUS_PARAM(name), | |
1308 | "h", | |
1309 | SD_BUS_PARAM(fd), | |
1310 | method_open_machine_root_directory, | |
1311 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1312 | SD_BUS_METHOD_WITH_NAMES("GetMachineUIDShift", | |
1313 | "s", | |
1314 | SD_BUS_PARAM(name), | |
1315 | "u", | |
1316 | SD_BUS_PARAM(shift), | |
1317 | method_get_machine_uid_shift, | |
1318 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1319 | SD_BUS_METHOD_WITH_NAMES("RemoveImage", | |
1320 | "s", | |
1321 | SD_BUS_PARAM(name), | |
1322 | NULL,, | |
1323 | method_remove_image, | |
1324 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1325 | SD_BUS_METHOD_WITH_NAMES("RenameImage", | |
1326 | "ss", | |
1327 | SD_BUS_PARAM(name) | |
1328 | SD_BUS_PARAM(new_name), | |
1329 | NULL,, | |
1330 | method_rename_image, | |
1331 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1332 | SD_BUS_METHOD_WITH_NAMES("CloneImage", | |
1333 | "ssb", | |
1334 | SD_BUS_PARAM(name) | |
1335 | SD_BUS_PARAM(new_name) | |
1336 | SD_BUS_PARAM(read_only), | |
1337 | NULL,, | |
1338 | method_clone_image, | |
1339 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1340 | SD_BUS_METHOD_WITH_NAMES("MarkImageReadOnly", | |
1341 | "sb", | |
1342 | SD_BUS_PARAM(name) | |
1343 | SD_BUS_PARAM(read_only), | |
1344 | NULL,, | |
1345 | method_mark_image_read_only, | |
1346 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1347 | SD_BUS_METHOD_WITH_NAMES("GetImageHostname", | |
1348 | "s", | |
1349 | SD_BUS_PARAM(name), | |
1350 | "s", | |
1351 | SD_BUS_PARAM(hostname), | |
1352 | method_get_image_hostname, | |
1353 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1354 | SD_BUS_METHOD_WITH_NAMES("GetImageMachineID", | |
1355 | "s", | |
1356 | SD_BUS_PARAM(name), | |
1357 | "ay", | |
1358 | SD_BUS_PARAM(id), | |
1359 | method_get_image_machine_id, | |
1360 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1361 | SD_BUS_METHOD_WITH_NAMES("GetImageMachineInfo", | |
1362 | "s", | |
1363 | SD_BUS_PARAM(name), | |
1364 | "a{ss}", | |
1365 | SD_BUS_PARAM(machine_info), | |
1366 | method_get_image_machine_info, | |
1367 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1368 | SD_BUS_METHOD_WITH_NAMES("GetImageOSRelease", | |
1369 | "s", | |
1370 | SD_BUS_PARAM(name), | |
1371 | "a{ss}", | |
1372 | SD_BUS_PARAM(os_release), | |
1373 | method_get_image_os_release, | |
1374 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1375 | SD_BUS_METHOD_WITH_NAMES("SetPoolLimit", | |
1376 | "t", | |
1377 | SD_BUS_PARAM(size), | |
1378 | NULL,, | |
1379 | method_set_pool_limit, | |
1380 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1381 | SD_BUS_METHOD_WITH_NAMES("SetImageLimit", | |
1382 | "st", | |
1383 | SD_BUS_PARAM(name) | |
1384 | SD_BUS_PARAM(size), | |
1385 | NULL,, | |
1386 | method_set_image_limit, | |
1387 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1388 | SD_BUS_METHOD_WITH_NAMES("CleanPool", | |
1389 | "s", | |
1390 | SD_BUS_PARAM(mode), | |
1391 | "a(st)", | |
1392 | SD_BUS_PARAM(images), | |
1393 | method_clean_pool, | |
1394 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1395 | SD_BUS_METHOD_WITH_NAMES("MapFromMachineUser", | |
1396 | "su", | |
1397 | SD_BUS_PARAM(name) | |
1398 | SD_BUS_PARAM(uid_inner), | |
1399 | "u", | |
1400 | SD_BUS_PARAM(uid_outer), | |
1401 | method_map_from_machine_user, | |
1402 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1403 | SD_BUS_METHOD_WITH_NAMES("MapToMachineUser", | |
1404 | "u", | |
1405 | SD_BUS_PARAM(uid_outer), | |
1406 | "sou", | |
1407 | SD_BUS_PARAM(machine_name) | |
1408 | SD_BUS_PARAM(machine_path) | |
1409 | SD_BUS_PARAM(uid_inner), | |
1410 | method_map_to_machine_user, | |
1411 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1412 | SD_BUS_METHOD_WITH_NAMES("MapFromMachineGroup", | |
1413 | "su", | |
1414 | SD_BUS_PARAM(name) | |
1415 | SD_BUS_PARAM(gid_inner), | |
1416 | "u", | |
1417 | SD_BUS_PARAM(gid_outer), | |
1418 | method_map_from_machine_group, | |
1419 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1420 | SD_BUS_METHOD_WITH_NAMES("MapToMachineGroup", | |
1421 | "u", | |
1422 | SD_BUS_PARAM(gid_outer), | |
1423 | "sou", | |
1424 | SD_BUS_PARAM(machine_name) | |
1425 | SD_BUS_PARAM(machine_path) | |
1426 | SD_BUS_PARAM(gid_inner), | |
1427 | method_map_to_machine_group, | |
1428 | SD_BUS_VTABLE_UNPRIVILEGED), | |
1429 | ||
1430 | SD_BUS_SIGNAL_WITH_NAMES("MachineNew", | |
1431 | "so", | |
1432 | SD_BUS_PARAM(machine) | |
1433 | SD_BUS_PARAM(path), | |
1434 | 0), | |
1435 | SD_BUS_SIGNAL_WITH_NAMES("MachineRemoved", | |
1436 | "so", | |
1437 | SD_BUS_PARAM(machine) | |
1438 | SD_BUS_PARAM(path), | |
1439 | 0), | |
1440 | ||
c3350683 LP |
1441 | SD_BUS_VTABLE_END |
1442 | }; | |
1ee306e1 | 1443 | |
4faa530c ZJS |
1444 | const BusObjectImplementation manager_object = { |
1445 | "/org/freedesktop/machine1", | |
1446 | "org.freedesktop.machine1.Manager", | |
1447 | .vtables = BUS_VTABLES(manager_vtable), | |
1448 | .children = BUS_IMPLEMENTATIONS( &machine_object, | |
1449 | &image_object ), | |
1450 | }; | |
1451 | ||
19070062 | 1452 | int match_job_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 | 1453 | const char *path, *result, *unit; |
1ee306e1 | 1454 | Manager *m = userdata; |
c3350683 LP |
1455 | Machine *machine; |
1456 | uint32_t id; | |
1457 | int r; | |
1ee306e1 | 1458 | |
1ee306e1 | 1459 | assert(message); |
c3350683 | 1460 | assert(m); |
1ee306e1 | 1461 | |
c3350683 LP |
1462 | r = sd_bus_message_read(message, "uoss", &id, &path, &unit, &result); |
1463 | if (r < 0) { | |
ebcf1f97 | 1464 | bus_log_parse_error(r); |
65d73cf0 | 1465 | return 0; |
c3350683 | 1466 | } |
6797c324 | 1467 | |
c3350683 LP |
1468 | machine = hashmap_get(m->machine_units, unit); |
1469 | if (!machine) | |
1470 | return 0; | |
6797c324 | 1471 | |
c3350683 | 1472 | if (streq_ptr(path, machine->scope_job)) { |
491ac9f2 | 1473 | machine->scope_job = mfree(machine->scope_job); |
6797c324 | 1474 | |
c3350683 LP |
1475 | if (machine->started) { |
1476 | if (streq(result, "done")) | |
1477 | machine_send_create_reply(machine, NULL); | |
1478 | else { | |
4afd3348 | 1479 | _cleanup_(sd_bus_error_free) sd_bus_error e = SD_BUS_ERROR_NULL; |
6797c324 | 1480 | |
ebcf1f97 | 1481 | sd_bus_error_setf(&e, BUS_ERROR_JOB_FAILED, "Start job for unit %s failed with '%s'", unit, result); |
6797c324 | 1482 | |
ebcf1f97 | 1483 | machine_send_create_reply(machine, &e); |
c3350683 | 1484 | } |
49f3fffd LP |
1485 | } |
1486 | ||
1487 | machine_save(machine); | |
1ee306e1 LP |
1488 | } |
1489 | ||
c3350683 LP |
1490 | machine_add_to_gc_queue(machine); |
1491 | return 0; | |
1ee306e1 LP |
1492 | } |
1493 | ||
19070062 | 1494 | int match_properties_changed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 | 1495 | _cleanup_free_ char *unit = NULL; |
49f3fffd | 1496 | const char *path; |
c3350683 LP |
1497 | Manager *m = userdata; |
1498 | Machine *machine; | |
ebcf1f97 | 1499 | int r; |
554604b3 | 1500 | |
c3350683 LP |
1501 | assert(message); |
1502 | assert(m); | |
554604b3 | 1503 | |
c3350683 LP |
1504 | path = sd_bus_message_get_path(message); |
1505 | if (!path) | |
554604b3 | 1506 | return 0; |
554604b3 | 1507 | |
ebcf1f97 | 1508 | r = unit_name_from_dbus_path(path, &unit); |
e5f5b5b9 LP |
1509 | if (r == -EINVAL) /* not for a unit */ |
1510 | return 0; | |
9ed794a3 | 1511 | if (r < 0) { |
9b420b3c LP |
1512 | log_oom(); |
1513 | return 0; | |
1514 | } | |
554604b3 | 1515 | |
c3350683 | 1516 | machine = hashmap_get(m->machine_units, unit); |
9b420b3c LP |
1517 | if (!machine) |
1518 | return 0; | |
1519 | ||
9b420b3c | 1520 | machine_add_to_gc_queue(machine); |
c3350683 LP |
1521 | return 0; |
1522 | } | |
554604b3 | 1523 | |
19070062 | 1524 | int match_unit_removed(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 LP |
1525 | const char *path, *unit; |
1526 | Manager *m = userdata; | |
1527 | Machine *machine; | |
1528 | int r; | |
554604b3 | 1529 | |
c3350683 LP |
1530 | assert(message); |
1531 | assert(m); | |
554604b3 | 1532 | |
c3350683 LP |
1533 | r = sd_bus_message_read(message, "so", &unit, &path); |
1534 | if (r < 0) { | |
ebcf1f97 | 1535 | bus_log_parse_error(r); |
9b420b3c | 1536 | return 0; |
554604b3 LP |
1537 | } |
1538 | ||
c3350683 | 1539 | machine = hashmap_get(m->machine_units, unit); |
9b420b3c LP |
1540 | if (!machine) |
1541 | return 0; | |
1542 | ||
9b420b3c | 1543 | machine_add_to_gc_queue(machine); |
c3350683 LP |
1544 | return 0; |
1545 | } | |
554604b3 | 1546 | |
19070062 | 1547 | int match_reloading(sd_bus_message *message, void *userdata, sd_bus_error *error) { |
c3350683 | 1548 | Manager *m = userdata; |
a658cafa LP |
1549 | Machine *machine; |
1550 | Iterator i; | |
c3350683 | 1551 | int b, r; |
554604b3 | 1552 | |
19070062 LP |
1553 | assert(message); |
1554 | assert(m); | |
554604b3 | 1555 | |
c3350683 LP |
1556 | r = sd_bus_message_read(message, "b", &b); |
1557 | if (r < 0) { | |
ebcf1f97 | 1558 | bus_log_parse_error(r); |
65d73cf0 | 1559 | return 0; |
554604b3 | 1560 | } |
a658cafa LP |
1561 | if (b) |
1562 | return 0; | |
554604b3 | 1563 | |
a658cafa LP |
1564 | /* systemd finished reloading, let's recheck all our machines */ |
1565 | log_debug("System manager has been reloaded, rechecking machines..."); | |
554604b3 | 1566 | |
a658cafa LP |
1567 | HASHMAP_FOREACH(machine, m->machines, i) |
1568 | machine_add_to_gc_queue(machine); | |
554604b3 LP |
1569 | |
1570 | return 0; | |
1571 | } | |
1572 | ||
b92d0b4c LP |
1573 | int manager_unref_unit( |
1574 | Manager *m, | |
1575 | const char *unit, | |
1576 | sd_bus_error *error) { | |
1577 | ||
1578 | assert(m); | |
1579 | assert(unit); | |
1580 | ||
14456f76 | 1581 | return bus_call_method(m->bus, bus_systemd_mgr, "UnrefUnit", error, NULL, "s", unit); |
b92d0b4c LP |
1582 | } |
1583 | ||
c3350683 | 1584 | int manager_stop_unit(Manager *manager, const char *unit, sd_bus_error *error, char **job) { |
4afd3348 | 1585 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; |
1ee306e1 LP |
1586 | int r; |
1587 | ||
1588 | assert(manager); | |
1589 | assert(unit); | |
1590 | ||
14456f76 | 1591 | r = bus_call_method(manager->bus, bus_systemd_mgr, "StopUnit", error, &reply, "ss", unit, "fail"); |
1ee306e1 | 1592 | if (r < 0) { |
c3350683 LP |
1593 | if (sd_bus_error_has_name(error, BUS_ERROR_NO_SUCH_UNIT) || |
1594 | sd_bus_error_has_name(error, BUS_ERROR_LOAD_FAILED)) { | |
6797c324 LP |
1595 | |
1596 | if (job) | |
1597 | *job = NULL; | |
1598 | ||
c3350683 | 1599 | sd_bus_error_free(error); |
6797c324 LP |
1600 | return 0; |
1601 | } | |
1602 | ||
1ee306e1 LP |
1603 | return r; |
1604 | } | |
1605 | ||
1606 | if (job) { | |
1607 | const char *j; | |
1608 | char *copy; | |
1609 | ||
c3350683 LP |
1610 | r = sd_bus_message_read(reply, "o", &j); |
1611 | if (r < 0) | |
1612 | return r; | |
1ee306e1 LP |
1613 | |
1614 | copy = strdup(j); | |
1615 | if (!copy) | |
1616 | return -ENOMEM; | |
1617 | ||
1618 | *job = copy; | |
1619 | } | |
1620 | ||
6797c324 | 1621 | return 1; |
1ee306e1 LP |
1622 | } |
1623 | ||
de58a50e | 1624 | int manager_kill_unit(Manager *manager, const char *unit, int signo, sd_bus_error *error) { |
1ee306e1 LP |
1625 | assert(manager); |
1626 | assert(unit); | |
1627 | ||
14456f76 | 1628 | return bus_call_method(manager->bus, bus_systemd_mgr, "KillUnit", error, NULL, "ssi", unit, "all", signo); |
1ee306e1 LP |
1629 | } |
1630 | ||
1631 | int manager_unit_is_active(Manager *manager, const char *unit) { | |
4afd3348 LP |
1632 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1633 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
1ee306e1 | 1634 | _cleanup_free_ char *path = NULL; |
1ee306e1 | 1635 | const char *state; |
1ee306e1 LP |
1636 | int r; |
1637 | ||
1638 | assert(manager); | |
1639 | assert(unit); | |
1640 | ||
1ee306e1 LP |
1641 | path = unit_dbus_path_from_name(unit); |
1642 | if (!path) | |
1643 | return -ENOMEM; | |
1644 | ||
c3350683 | 1645 | r = sd_bus_get_property( |
1ee306e1 LP |
1646 | manager->bus, |
1647 | "org.freedesktop.systemd1", | |
1648 | path, | |
c3350683 LP |
1649 | "org.freedesktop.systemd1.Unit", |
1650 | "ActiveState", | |
1ee306e1 | 1651 | &error, |
c3350683 LP |
1652 | &reply, |
1653 | "s"); | |
1ee306e1 | 1654 | if (r < 0) { |
c3350683 LP |
1655 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) || |
1656 | sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED)) | |
6797c324 | 1657 | return true; |
6797c324 | 1658 | |
c3350683 LP |
1659 | if (sd_bus_error_has_name(&error, BUS_ERROR_NO_SUCH_UNIT) || |
1660 | sd_bus_error_has_name(&error, BUS_ERROR_LOAD_FAILED)) | |
6797c324 | 1661 | return false; |
6797c324 | 1662 | |
1ee306e1 LP |
1663 | return r; |
1664 | } | |
1665 | ||
c3350683 LP |
1666 | r = sd_bus_message_read(reply, "s", &state); |
1667 | if (r < 0) | |
1ee306e1 | 1668 | return -EINVAL; |
1ee306e1 | 1669 | |
9b420b3c | 1670 | return !STR_IN_SET(state, "inactive", "failed"); |
1ee306e1 | 1671 | } |
bd16acf3 | 1672 | |
c3350683 | 1673 | int manager_job_is_active(Manager *manager, const char *path) { |
4afd3348 LP |
1674 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
1675 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
c3350683 | 1676 | int r; |
bd16acf3 | 1677 | |
c3350683 LP |
1678 | assert(manager); |
1679 | assert(path); | |
bd16acf3 | 1680 | |
c3350683 LP |
1681 | r = sd_bus_get_property( |
1682 | manager->bus, | |
1683 | "org.freedesktop.systemd1", | |
1684 | path, | |
1685 | "org.freedesktop.systemd1.Job", | |
1686 | "State", | |
1687 | &error, | |
1688 | &reply, | |
1689 | "s"); | |
1690 | if (r < 0) { | |
1691 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_NO_REPLY) || | |
1692 | sd_bus_error_has_name(&error, SD_BUS_ERROR_DISCONNECTED)) | |
1693 | return true; | |
bd16acf3 | 1694 | |
c3350683 LP |
1695 | if (sd_bus_error_has_name(&error, SD_BUS_ERROR_UNKNOWN_OBJECT)) |
1696 | return false; | |
bd16acf3 | 1697 | |
bd16acf3 | 1698 | return r; |
c3350683 | 1699 | } |
bd16acf3 | 1700 | |
c3350683 LP |
1701 | /* We don't actually care about the state really. The fact |
1702 | * that we could read the job state is enough for us */ | |
bd16acf3 | 1703 | |
c3350683 | 1704 | return true; |
bd16acf3 | 1705 | } |
ab49725f KS |
1706 | |
1707 | int manager_get_machine_by_pid(Manager *m, pid_t pid, Machine **machine) { | |
ab49725f KS |
1708 | Machine *mm; |
1709 | int r; | |
1710 | ||
1711 | assert(m); | |
1712 | assert(pid >= 1); | |
1713 | assert(machine); | |
1714 | ||
4a0b58c4 | 1715 | mm = hashmap_get(m->machine_leaders, PID_TO_PTR(pid)); |
077c8c36 LP |
1716 | if (!mm) { |
1717 | _cleanup_free_ char *unit = NULL; | |
ab49725f | 1718 | |
077c8c36 LP |
1719 | r = cg_pid_get_unit(pid, &unit); |
1720 | if (r >= 0) | |
1721 | mm = hashmap_get(m->machine_units, unit); | |
1722 | } | |
ab49725f KS |
1723 | if (!mm) |
1724 | return 0; | |
1725 | ||
1726 | *machine = mm; | |
1727 | return 1; | |
1728 | } | |
1729 | ||
1730 | int manager_add_machine(Manager *m, const char *name, Machine **_machine) { | |
1731 | Machine *machine; | |
1732 | ||
1733 | assert(m); | |
1734 | assert(name); | |
1735 | ||
1736 | machine = hashmap_get(m->machines, name); | |
1737 | if (!machine) { | |
fbe55073 | 1738 | machine = machine_new(m, _MACHINE_CLASS_INVALID, name); |
ab49725f KS |
1739 | if (!machine) |
1740 | return -ENOMEM; | |
1741 | } | |
1742 | ||
1743 | if (_machine) | |
1744 | *_machine = machine; | |
1745 | ||
1746 | return 0; | |
1747 | } |