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