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