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