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