1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
14 #include "alloc-util.h"
15 #include "btrfs-util.h"
16 #include "bus-common-errors.h"
18 #include "cgroup-util.h"
21 #include "format-util.h"
22 #include "hostname-util.h"
23 #include "image-dbus.h"
25 #include "machine-dbus.h"
26 #include "machine-image.h"
27 #include "machine-pool.h"
29 #include "path-util.h"
30 #include "process-util.h"
31 #include "stdio-util.h"
33 #include "unit-name.h"
34 #include "user-util.h"
36 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_pool_path
, "s", "/var/lib/machines");
38 static int property_get_pool_usage(
41 const char *interface
,
43 sd_bus_message
*reply
,
45 sd_bus_error
*error
) {
47 _cleanup_close_
int fd
= -1;
48 uint64_t usage
= (uint64_t) -1;
54 /* We try to read the quota info from /var/lib/machines, as
55 * well as the usage of the loopback file
56 * /var/lib/machines.raw, and pick the larger value. */
58 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
62 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
66 if (stat("/var/lib/machines.raw", &st
) >= 0) {
67 if (usage
== (uint64_t) -1 || st
.st_blocks
* 512ULL > usage
)
68 usage
= st
.st_blocks
* 512ULL;
71 return sd_bus_message_append(reply
, "t", usage
);
74 static int property_get_pool_limit(
77 const char *interface
,
79 sd_bus_message
*reply
,
81 sd_bus_error
*error
) {
83 _cleanup_close_
int fd
= -1;
84 uint64_t size
= (uint64_t) -1;
90 /* We try to read the quota limit from /var/lib/machines, as
91 * well as the size of the loopback file
92 * /var/lib/machines.raw, and pick the smaller value. */
94 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
98 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
99 size
= q
.referenced_max
;
102 if (stat("/var/lib/machines.raw", &st
) >= 0) {
103 if (size
== (uint64_t) -1 || (uint64_t) st
.st_size
< size
)
107 return sd_bus_message_append(reply
, "t", size
);
110 static int method_get_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
111 _cleanup_free_
char *p
= NULL
;
112 Manager
*m
= userdata
;
120 r
= sd_bus_message_read(message
, "s", &name
);
124 machine
= hashmap_get(m
->machines
, name
);
126 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
128 p
= machine_bus_path(machine
);
132 return sd_bus_reply_method_return(message
, "o", p
);
135 static int method_get_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
136 _cleanup_free_
char *p
= NULL
;
137 Manager
*m
= userdata
;
144 r
= sd_bus_message_read(message
, "s", &name
);
148 r
= image_find(IMAGE_MACHINE
, name
, NULL
);
150 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
154 p
= image_bus_path(name
);
158 return sd_bus_reply_method_return(message
, "o", p
);
161 static int method_get_machine_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
162 _cleanup_free_
char *p
= NULL
;
163 Manager
*m
= userdata
;
164 Machine
*machine
= NULL
;
171 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
173 r
= sd_bus_message_read(message
, "u", &pid
);
181 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
183 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
187 r
= sd_bus_creds_get_pid(creds
, &pid
);
192 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
196 return sd_bus_error_setf(error
, BUS_ERROR_NO_MACHINE_FOR_PID
, "PID "PID_FMT
" does not belong to any known machine", pid
);
198 p
= machine_bus_path(machine
);
202 return sd_bus_reply_method_return(message
, "o", p
);
205 static int method_list_machines(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
206 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
207 Manager
*m
= userdata
;
215 r
= sd_bus_message_new_method_return(message
, &reply
);
217 return sd_bus_error_set_errno(error
, r
);
219 r
= sd_bus_message_open_container(reply
, 'a', "(ssso)");
221 return sd_bus_error_set_errno(error
, r
);
223 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
224 _cleanup_free_
char *p
= NULL
;
226 p
= machine_bus_path(machine
);
230 r
= sd_bus_message_append(reply
, "(ssso)",
232 strempty(machine_class_to_string(machine
->class)),
236 return sd_bus_error_set_errno(error
, r
);
239 r
= sd_bus_message_close_container(reply
);
241 return sd_bus_error_set_errno(error
, r
);
243 return sd_bus_send(NULL
, reply
, NULL
);
246 static int method_create_or_register_machine(Manager
*manager
, sd_bus_message
*message
, bool read_network
, Machine
**_m
, sd_bus_error
*error
) {
247 const char *name
, *service
, *class, *root_directory
;
248 const int32_t *netif
= NULL
;
254 size_t n
, n_netif
= 0;
261 r
= sd_bus_message_read(message
, "s", &name
);
264 if (!machine_name_is_valid(name
))
265 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine name");
267 r
= sd_bus_message_read_array(message
, 'y', &v
, &n
);
275 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine ID parameter");
277 r
= sd_bus_message_read(message
, "ssus", &service
, &class, &leader
, &root_directory
);
284 r
= sd_bus_message_read_array(message
, 'i', (const void**) &netif
, &n_netif
);
288 n_netif
/= sizeof(int32_t);
290 for (i
= 0; i
< n_netif
; i
++) {
292 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid network interface index %i", netif
[i
]);
297 c
= _MACHINE_CLASS_INVALID
;
299 c
= machine_class_from_string(class);
301 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine class parameter");
305 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
307 if (!isempty(root_directory
) && !path_is_absolute(root_directory
))
308 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Root directory must be empty or an absolute path");
311 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
313 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
317 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
319 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
324 if (hashmap_get(manager
->machines
, name
))
325 return sd_bus_error_setf(error
, BUS_ERROR_MACHINE_EXISTS
, "Machine '%s' already exists", name
);
327 r
= manager_add_machine(manager
, name
, &m
);
335 if (!isempty(service
)) {
336 m
->service
= strdup(service
);
343 if (!isempty(root_directory
)) {
344 m
->root_directory
= strdup(root_directory
);
345 if (!m
->root_directory
) {
352 assert_cc(sizeof(int32_t) == sizeof(int));
353 m
->netif
= memdup(netif
, sizeof(int32_t) * n_netif
);
359 m
->n_netif
= n_netif
;
367 machine_add_to_gc_queue(m
);
371 static int method_create_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
372 Manager
*manager
= userdata
;
379 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
383 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
387 r
= machine_start(m
, message
, error
);
391 m
->create_message
= sd_bus_message_ref(message
);
395 machine_add_to_gc_queue(m
);
399 static int method_create_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
400 return method_create_machine_internal(message
, true, userdata
, error
);
403 static int method_create_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
404 return method_create_machine_internal(message
, false, userdata
, error
);
407 static int method_register_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
408 Manager
*manager
= userdata
;
409 _cleanup_free_
char *p
= NULL
;
416 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
420 r
= cg_pid_get_unit(m
->leader
, &m
->unit
);
422 r
= sd_bus_error_set_errnof(error
, r
,
423 "Failed to determine unit of process "PID_FMT
" : %m",
428 r
= machine_start(m
, NULL
, error
);
432 p
= machine_bus_path(m
);
438 return sd_bus_reply_method_return(message
, "o", p
);
441 machine_add_to_gc_queue(m
);
445 static int method_register_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
446 return method_register_machine_internal(message
, true, userdata
, error
);
449 static int method_register_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
450 return method_register_machine_internal(message
, false, userdata
, error
);
453 static int method_terminate_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
454 Manager
*m
= userdata
;
462 r
= sd_bus_message_read(message
, "s", &name
);
464 return sd_bus_error_set_errno(error
, r
);
466 machine
= hashmap_get(m
->machines
, name
);
468 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
470 return bus_machine_method_terminate(message
, machine
, error
);
473 static int method_kill_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
474 Manager
*m
= userdata
;
482 r
= sd_bus_message_read(message
, "s", &name
);
484 return sd_bus_error_set_errno(error
, r
);
486 machine
= hashmap_get(m
->machines
, name
);
488 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
490 return bus_machine_method_kill(message
, machine
, error
);
493 static int method_get_machine_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
494 Manager
*m
= userdata
;
502 r
= sd_bus_message_read(message
, "s", &name
);
504 return sd_bus_error_set_errno(error
, r
);
506 machine
= hashmap_get(m
->machines
, name
);
508 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
510 return bus_machine_method_get_addresses(message
, machine
, error
);
513 static int method_get_machine_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
514 Manager
*m
= userdata
;
522 r
= sd_bus_message_read(message
, "s", &name
);
524 return sd_bus_error_set_errno(error
, r
);
526 machine
= hashmap_get(m
->machines
, name
);
528 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
530 return bus_machine_method_get_os_release(message
, machine
, error
);
533 static int method_list_images(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
534 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
535 _cleanup_(image_hashmap_freep
) Hashmap
*images
= NULL
;
536 Manager
*m
= userdata
;
544 images
= hashmap_new(&string_hash_ops
);
548 r
= image_discover(IMAGE_MACHINE
, images
);
552 r
= sd_bus_message_new_method_return(message
, &reply
);
556 r
= sd_bus_message_open_container(reply
, 'a', "(ssbttto)");
560 HASHMAP_FOREACH(image
, images
, i
) {
561 _cleanup_free_
char *p
= NULL
;
563 p
= image_bus_path(image
->name
);
567 r
= sd_bus_message_append(reply
, "(ssbttto)",
569 image_type_to_string(image
->type
),
579 r
= sd_bus_message_close_container(reply
);
583 return sd_bus_send(NULL
, reply
, NULL
);
586 static int method_open_machine_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
587 Manager
*m
= userdata
;
595 r
= sd_bus_message_read(message
, "s", &name
);
597 return sd_bus_error_set_errno(error
, r
);
599 machine
= hashmap_get(m
->machines
, name
);
601 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
603 return bus_machine_method_open_pty(message
, machine
, error
);
606 static int method_open_machine_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
607 Manager
*m
= userdata
;
615 r
= sd_bus_message_read(message
, "s", &name
);
619 machine
= hashmap_get(m
->machines
, name
);
621 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
623 return bus_machine_method_open_login(message
, machine
, error
);
626 static int method_open_machine_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
627 Manager
*m
= userdata
;
636 r
= sd_bus_message_read(message
, "s", &name
);
640 machine
= hashmap_get(m
->machines
, name
);
642 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
644 return bus_machine_method_open_shell(message
, machine
, error
);
647 static int method_bind_mount_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
648 Manager
*m
= userdata
;
656 r
= sd_bus_message_read(message
, "s", &name
);
660 machine
= hashmap_get(m
->machines
, name
);
662 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
664 return bus_machine_method_bind_mount(message
, machine
, error
);
667 static int method_copy_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
668 Manager
*m
= userdata
;
676 r
= sd_bus_message_read(message
, "s", &name
);
680 machine
= hashmap_get(m
->machines
, name
);
682 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
684 return bus_machine_method_copy(message
, machine
, error
);
687 static int method_open_machine_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
688 Manager
*m
= userdata
;
696 r
= sd_bus_message_read(message
, "s", &name
);
700 machine
= hashmap_get(m
->machines
, name
);
702 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
704 return bus_machine_method_open_root_directory(message
, machine
, error
);
707 static int method_get_machine_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
708 Manager
*m
= userdata
;
716 r
= sd_bus_message_read(message
, "s", &name
);
720 machine
= hashmap_get(m
->machines
, name
);
722 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
724 return bus_machine_method_get_uid_shift(message
, machine
, error
);
727 static int method_remove_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
728 _cleanup_(image_unrefp
) Image
* i
= NULL
;
734 r
= sd_bus_message_read(message
, "s", &name
);
738 if (!image_name_is_valid(name
))
739 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
741 r
= image_find(IMAGE_MACHINE
, name
, &i
);
743 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
747 i
->userdata
= userdata
;
748 return bus_image_method_remove(message
, i
, error
);
751 static int method_rename_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
752 _cleanup_(image_unrefp
) Image
* i
= NULL
;
753 const char *old_name
;
758 r
= sd_bus_message_read(message
, "s", &old_name
);
762 if (!image_name_is_valid(old_name
))
763 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", old_name
);
765 r
= image_find(IMAGE_MACHINE
, old_name
, &i
);
767 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", old_name
);
771 i
->userdata
= userdata
;
772 return bus_image_method_rename(message
, i
, error
);
775 static int method_clone_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
776 _cleanup_(image_unrefp
) Image
*i
= NULL
;
777 const char *old_name
;
782 r
= sd_bus_message_read(message
, "s", &old_name
);
786 if (!image_name_is_valid(old_name
))
787 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", old_name
);
789 r
= image_find(IMAGE_MACHINE
, old_name
, &i
);
791 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", old_name
);
795 i
->userdata
= userdata
;
796 return bus_image_method_clone(message
, i
, error
);
799 static int method_mark_image_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
800 _cleanup_(image_unrefp
) Image
*i
= NULL
;
806 r
= sd_bus_message_read(message
, "s", &name
);
810 if (!image_name_is_valid(name
))
811 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
813 r
= image_find(IMAGE_MACHINE
, name
, &i
);
815 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
819 i
->userdata
= userdata
;
820 return bus_image_method_mark_read_only(message
, i
, error
);
823 static int method_get_image_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
824 _cleanup_(image_unrefp
) Image
*i
= NULL
;
830 r
= sd_bus_message_read(message
, "s", &name
);
834 if (!image_name_is_valid(name
))
835 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
837 r
= image_find(IMAGE_MACHINE
, name
, &i
);
839 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
843 i
->userdata
= userdata
;
844 return bus_image_method_get_hostname(message
, i
, error
);
847 static int method_get_image_machine_id(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
848 _cleanup_(image_unrefp
) Image
*i
= NULL
;
854 r
= sd_bus_message_read(message
, "s", &name
);
858 if (!image_name_is_valid(name
))
859 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
861 r
= image_find(IMAGE_MACHINE
, name
, &i
);
863 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
867 i
->userdata
= userdata
;
868 return bus_image_method_get_machine_id(message
, i
, error
);
871 static int method_get_image_machine_info(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
872 _cleanup_(image_unrefp
) Image
*i
= NULL
;
878 r
= sd_bus_message_read(message
, "s", &name
);
882 if (!image_name_is_valid(name
))
883 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
885 r
= image_find(IMAGE_MACHINE
, name
, &i
);
887 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
891 i
->userdata
= userdata
;
892 return bus_image_method_get_machine_info(message
, i
, error
);
895 static int method_get_image_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
896 _cleanup_(image_unrefp
) Image
*i
= NULL
;
902 r
= sd_bus_message_read(message
, "s", &name
);
906 if (!image_name_is_valid(name
))
907 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
909 r
= image_find(IMAGE_MACHINE
, name
, &i
);
911 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
915 i
->userdata
= userdata
;
916 return bus_image_method_get_os_release(message
, i
, error
);
919 static int clean_pool_done(Operation
*operation
, int ret
, sd_bus_error
*error
) {
920 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
921 _cleanup_fclose_
FILE *f
= NULL
;
927 assert(operation
->extra_fd
>= 0);
929 if (lseek(operation
->extra_fd
, 0, SEEK_SET
) == (off_t
) -1)
932 f
= fdopen(operation
->extra_fd
, "re");
936 operation
->extra_fd
= -1;
938 /* The resulting temporary file starts with a boolean value that indicates success or not. */
940 n
= fread(&success
, 1, sizeof(success
), f
);
941 if (n
!= sizeof(success
))
942 return ret
< 0 ? ret
: (errno
!= 0 ? -errno
: -EIO
);
945 _cleanup_free_
char *name
= NULL
;
947 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
948 * set to false followed by the name of the failed image. Let's try to read this and use it for the
949 * error message. If we can't read it, don't mind, and return the naked error. */
951 if (success
) /* The resulting temporary file could not be updated, ignore it. */
954 r
= read_nul_string(f
, &name
);
955 if (r
< 0 || isempty(name
)) /* Same here... */
958 return sd_bus_error_set_errnof(error
, ret
, "Failed to remove image %s: %m", name
);
963 r
= sd_bus_message_new_method_return(operation
->message
, &reply
);
967 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
971 /* On success the resulting temporary file will contain a list of image names that were removed followed by
972 * their size on disk. Let's read that and turn it into a bus message. */
974 _cleanup_free_
char *name
= NULL
;
977 r
= read_nul_string(f
, &name
);
980 if (isempty(name
)) /* reached the end */
984 n
= fread(&size
, 1, sizeof(size
), f
);
985 if (n
!= sizeof(size
))
986 return errno
!= 0 ? -errno
: -EIO
;
988 r
= sd_bus_message_append(reply
, "(st)", name
, size
);
993 r
= sd_bus_message_close_container(reply
);
997 return sd_bus_send(NULL
, reply
, NULL
);
1000 static int method_clean_pool(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1006 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
1007 _cleanup_close_
int result_fd
= -1;
1008 Manager
*m
= userdata
;
1009 Operation
*operation
;
1016 if (m
->n_operations
>= OPERATIONS_MAX
)
1017 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing operations.");
1019 r
= sd_bus_message_read(message
, "s", &mm
);
1023 if (streq(mm
, "all"))
1025 else if (streq(mm
, "hidden"))
1026 mode
= REMOVE_HIDDEN
;
1028 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown mode '%s'.", mm
);
1030 r
= bus_verify_polkit_async(
1033 "org.freedesktop.machine1.manage-machines",
1037 &m
->polkit_registry
,
1042 return 1; /* Will call us back */
1044 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1045 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1047 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
1048 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
1050 result_fd
= open_tmpfile_unlinkable(NULL
, O_RDWR
|O_CLOEXEC
);
1054 /* This might be a slow operation, run it asynchronously in a background process */
1055 r
= safe_fork("(sd-clean)", FORK_RESET_SIGNALS
, &child
);
1057 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1059 _cleanup_(image_hashmap_freep
) Hashmap
*images
= NULL
;
1060 bool success
= true;
1065 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1067 images
= hashmap_new(&string_hash_ops
);
1073 r
= image_discover(IMAGE_MACHINE
, images
);
1077 l
= write(result_fd
, &success
, sizeof(success
));
1083 HASHMAP_FOREACH(image
, images
, i
) {
1085 /* We can't remove vendor images (i.e. those in /usr) */
1086 if (IMAGE_IS_VENDOR(image
))
1089 if (IMAGE_IS_HOST(image
))
1092 if (mode
== REMOVE_HIDDEN
&& !IMAGE_IS_HIDDEN(image
))
1095 r
= image_remove(image
);
1096 if (r
== -EBUSY
) /* keep images that are currently being used. */
1099 /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */
1101 (void) ftruncate(result_fd
, 0);
1102 (void) lseek(result_fd
, 0, SEEK_SET
);
1103 (void) write(result_fd
, &success
, sizeof(success
));
1104 (void) write(result_fd
, image
->name
, strlen(image
->name
)+1);
1108 l
= write(result_fd
, image
->name
, strlen(image
->name
)+1);
1114 l
= write(result_fd
, &image
->usage_exclusive
, sizeof(image
->usage_exclusive
));
1121 result_fd
= safe_close(result_fd
);
1122 _exit(EXIT_SUCCESS
);
1125 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1126 _exit(EXIT_FAILURE
);
1129 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1131 /* The clean-up might take a while, hence install a watch on the child and return */
1133 r
= operation_new(m
, NULL
, child
, message
, errno_pipe_fd
[0], &operation
);
1135 (void) sigkill_wait(child
);
1139 operation
->extra_fd
= result_fd
;
1140 operation
->done
= clean_pool_done
;
1143 errno_pipe_fd
[0] = -1;
1148 static int method_set_pool_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1149 Manager
*m
= userdata
;
1155 r
= sd_bus_message_read(message
, "t", &limit
);
1158 if (!FILE_SIZE_VALID_OR_INFINITY(limit
))
1159 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "New limit out of range");
1161 r
= bus_verify_polkit_async(
1164 "org.freedesktop.machine1.manage-machines",
1168 &m
->polkit_registry
,
1173 return 1; /* Will call us back */
1175 /* Set up the machine directory if necessary */
1176 r
= setup_machine_directory(limit
, error
);
1180 /* Resize the backing loopback device, if there is one, except if we asked to drop any limit */
1181 if (limit
!= (uint64_t) -1) {
1182 r
= btrfs_resize_loopback("/var/lib/machines", limit
, false);
1184 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
1185 if (r
< 0 && r
!= -ENODEV
) /* ignore ENODEV, as that's what is returned if the file system is not on loopback */
1186 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust loopback limit: %m");
1189 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit
);
1191 r
= btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit
);
1193 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
1195 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust quota limit: %m");
1197 return sd_bus_reply_method_return(message
, NULL
);
1200 static int method_set_image_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1201 _cleanup_(image_unrefp
) Image
*i
= NULL
;
1207 r
= sd_bus_message_read(message
, "s", &name
);
1211 if (!image_name_is_valid(name
))
1212 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
1214 r
= image_find(IMAGE_MACHINE
, name
, &i
);
1216 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
1220 i
->userdata
= userdata
;
1221 return bus_image_method_set_limit(message
, i
, error
);
1224 static int method_map_from_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1225 _cleanup_fclose_
FILE *f
= NULL
;
1226 Manager
*m
= userdata
;
1227 const char *name
, *p
;
1232 r
= sd_bus_message_read(message
, "su", &name
, &uid
);
1236 if (!uid_is_valid(uid
))
1237 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
1239 machine
= hashmap_get(m
->machines
, name
);
1241 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
1243 if (machine
->class != MACHINE_CONTAINER
)
1244 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
1246 p
= procfs_file_alloca(machine
->leader
, "uid_map");
1252 uid_t uid_base
, uid_shift
, uid_range
, converted
;
1256 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
1257 if (k
< 0 && feof(f
))
1260 if (ferror(f
) && errno
> 0)
1266 if (uid
< uid_base
|| uid
>= uid_base
+ uid_range
)
1269 converted
= uid
- uid_base
+ uid_shift
;
1270 if (!uid_is_valid(converted
))
1271 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
1273 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
1276 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching user mappings.", name
);
1279 static int method_map_to_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1280 Manager
*m
= userdata
;
1286 r
= sd_bus_message_read(message
, "u", &uid
);
1289 if (!uid_is_valid(uid
))
1290 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
1292 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "User " UID_FMT
" belongs to host UID range", uid
);
1294 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1295 _cleanup_fclose_
FILE *f
= NULL
;
1296 char p
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
1298 if (machine
->class != MACHINE_CONTAINER
)
1301 xsprintf(p
, "/proc/" UID_FMT
"/uid_map", machine
->leader
);
1304 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
1309 _cleanup_free_
char *o
= NULL
;
1310 uid_t uid_base
, uid_shift
, uid_range
, converted
;
1314 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
1315 if (k
< 0 && feof(f
))
1318 if (ferror(f
) && errno
> 0)
1324 if (uid
< uid_shift
|| uid
>= uid_shift
+ uid_range
)
1327 converted
= (uid
- uid_shift
+ uid_base
);
1328 if (!uid_is_valid(converted
))
1329 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
1331 o
= machine_bus_path(machine
);
1335 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1339 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "No matching user mapping for " UID_FMT
".", uid
);
1342 static int method_map_from_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1343 _cleanup_fclose_
FILE *f
= NULL
;
1344 Manager
*m
= groupdata
;
1345 const char *name
, *p
;
1350 r
= sd_bus_message_read(message
, "su", &name
, &gid
);
1354 if (!gid_is_valid(gid
))
1355 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1357 machine
= hashmap_get(m
->machines
, name
);
1359 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
1361 if (machine
->class != MACHINE_CONTAINER
)
1362 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
1364 p
= procfs_file_alloca(machine
->leader
, "gid_map");
1370 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1374 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1375 if (k
< 0 && feof(f
))
1378 if (ferror(f
) && errno
> 0)
1384 if (gid
< gid_base
|| gid
>= gid_base
+ gid_range
)
1387 converted
= gid
- gid_base
+ gid_shift
;
1388 if (!gid_is_valid(converted
))
1389 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1391 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
1394 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Machine '%s' has no matching group mappings.", name
);
1397 static int method_map_to_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1398 Manager
*m
= groupdata
;
1404 r
= sd_bus_message_read(message
, "u", &gid
);
1407 if (!gid_is_valid(gid
))
1408 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1410 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Group " GID_FMT
" belongs to host GID range", gid
);
1412 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1413 _cleanup_fclose_
FILE *f
= NULL
;
1414 char p
[STRLEN("/proc//gid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
1416 if (machine
->class != MACHINE_CONTAINER
)
1419 xsprintf(p
, "/proc/" GID_FMT
"/gid_map", machine
->leader
);
1422 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
1427 _cleanup_free_
char *o
= NULL
;
1428 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1432 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1433 if (k
< 0 && feof(f
))
1436 if (ferror(f
) && errno
> 0)
1442 if (gid
< gid_shift
|| gid
>= gid_shift
+ gid_range
)
1445 converted
= (gid
- gid_shift
+ gid_base
);
1446 if (!gid_is_valid(converted
))
1447 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1449 o
= machine_bus_path(machine
);
1453 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1457 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "No matching group mapping for " GID_FMT
".", gid
);
1460 const sd_bus_vtable manager_vtable
[] = {
1461 SD_BUS_VTABLE_START(0),
1462 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path
, 0, 0),
1463 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage
, 0, 0),
1464 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit
, 0, 0),
1465 SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1466 SD_BUS_METHOD("GetImage", "s", "o", method_get_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1467 SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
1468 SD_BUS_METHOD("ListMachines", NULL
, "a(ssso)", method_list_machines
, SD_BUS_VTABLE_UNPRIVILEGED
),
1469 SD_BUS_METHOD("ListImages", NULL
, "a(ssbttto)", method_list_images
, SD_BUS_VTABLE_UNPRIVILEGED
),
1470 SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine
, 0),
1471 SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network
, 0),
1472 SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine
, 0),
1473 SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network
, 0),
1474 SD_BUS_METHOD("TerminateMachine", "s", NULL
, method_terminate_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1475 SD_BUS_METHOD("KillMachine", "ssi", NULL
, method_kill_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1476 SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1477 SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1478 SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty
, 0),
1479 SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1480 SD_BUS_METHOD("OpenMachineShell", "sssasas", "hs", method_open_machine_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1481 SD_BUS_METHOD("BindMountMachine", "sssbb", NULL
, method_bind_mount_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1482 SD_BUS_METHOD("CopyFromMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1483 SD_BUS_METHOD("CopyToMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1484 SD_BUS_METHOD("OpenMachineRootDirectory", "s", "h", method_open_machine_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1485 SD_BUS_METHOD("GetMachineUIDShift", "s", "u", method_get_machine_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1486 SD_BUS_METHOD("RemoveImage", "s", NULL
, method_remove_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1487 SD_BUS_METHOD("RenameImage", "ss", NULL
, method_rename_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1488 SD_BUS_METHOD("CloneImage", "ssb", NULL
, method_clone_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1489 SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL
, method_mark_image_read_only
, SD_BUS_VTABLE_UNPRIVILEGED
),
1490 SD_BUS_METHOD("GetImageHostname", "s", "s", method_get_image_hostname
, SD_BUS_VTABLE_UNPRIVILEGED
),
1491 SD_BUS_METHOD("GetImageMachineID", "s", "ay", method_get_image_machine_id
, SD_BUS_VTABLE_UNPRIVILEGED
),
1492 SD_BUS_METHOD("GetImageMachineInfo", "s", "a{ss}", method_get_image_machine_info
, SD_BUS_VTABLE_UNPRIVILEGED
),
1493 SD_BUS_METHOD("GetImageOSRelease", "s", "a{ss}", method_get_image_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1494 SD_BUS_METHOD("SetPoolLimit", "t", NULL
, method_set_pool_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1495 SD_BUS_METHOD("SetImageLimit", "st", NULL
, method_set_image_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1496 SD_BUS_METHOD("CleanPool", "s", "a(st)", method_clean_pool
, SD_BUS_VTABLE_UNPRIVILEGED
),
1497 SD_BUS_METHOD("MapFromMachineUser", "su", "u", method_map_from_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1498 SD_BUS_METHOD("MapToMachineUser", "u", "sou", method_map_to_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1499 SD_BUS_METHOD("MapFromMachineGroup", "su", "u", method_map_from_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1500 SD_BUS_METHOD("MapToMachineGroup", "u", "sou", method_map_to_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1501 SD_BUS_SIGNAL("MachineNew", "so", 0),
1502 SD_BUS_SIGNAL("MachineRemoved", "so", 0),
1506 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1507 const char *path
, *result
, *unit
;
1508 Manager
*m
= userdata
;
1516 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
1518 bus_log_parse_error(r
);
1522 machine
= hashmap_get(m
->machine_units
, unit
);
1526 if (streq_ptr(path
, machine
->scope_job
)) {
1527 machine
->scope_job
= mfree(machine
->scope_job
);
1529 if (machine
->started
) {
1530 if (streq(result
, "done"))
1531 machine_send_create_reply(machine
, NULL
);
1533 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
1535 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit %s failed with '%s'", unit
, result
);
1537 machine_send_create_reply(machine
, &e
);
1541 machine_save(machine
);
1544 machine_add_to_gc_queue(machine
);
1548 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1549 _cleanup_free_
char *unit
= NULL
;
1551 Manager
*m
= userdata
;
1558 path
= sd_bus_message_get_path(message
);
1562 r
= unit_name_from_dbus_path(path
, &unit
);
1563 if (r
== -EINVAL
) /* not for a unit */
1570 machine
= hashmap_get(m
->machine_units
, unit
);
1574 machine_add_to_gc_queue(machine
);
1578 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1579 const char *path
, *unit
;
1580 Manager
*m
= userdata
;
1587 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
1589 bus_log_parse_error(r
);
1593 machine
= hashmap_get(m
->machine_units
, unit
);
1597 machine_add_to_gc_queue(machine
);
1601 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1602 Manager
*m
= userdata
;
1610 r
= sd_bus_message_read(message
, "b", &b
);
1612 bus_log_parse_error(r
);
1618 /* systemd finished reloading, let's recheck all our machines */
1619 log_debug("System manager has been reloaded, rechecking machines...");
1621 HASHMAP_FOREACH(machine
, m
->machines
, i
)
1622 machine_add_to_gc_queue(machine
);
1627 int manager_start_scope(
1632 const char *description
,
1633 sd_bus_message
*more_properties
,
1634 sd_bus_error
*error
,
1637 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1644 r
= sd_bus_message_new_method_call(
1647 "org.freedesktop.systemd1",
1648 "/org/freedesktop/systemd1",
1649 "org.freedesktop.systemd1.Manager",
1650 "StartTransientUnit");
1654 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
1658 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1662 if (!isempty(slice
)) {
1663 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
1668 if (!isempty(description
)) {
1669 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
1674 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
1678 r
= sd_bus_message_append(m
, "(sv)", "Delegate", "b", 1);
1682 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", UINT64_C(16384));
1684 return bus_log_create_error(r
);
1686 if (more_properties
) {
1687 r
= sd_bus_message_copy(m
, more_properties
, true);
1692 r
= sd_bus_message_close_container(m
);
1696 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1700 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
1708 r
= sd_bus_message_read(reply
, "o", &j
);
1722 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
1723 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1729 r
= sd_bus_call_method(
1731 "org.freedesktop.systemd1",
1732 "/org/freedesktop/systemd1",
1733 "org.freedesktop.systemd1.Manager",
1737 "ss", unit
, "fail");
1739 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1740 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
1745 sd_bus_error_free(error
);
1756 r
= sd_bus_message_read(reply
, "o", &j
);
1770 int manager_kill_unit(Manager
*manager
, const char *unit
, int signo
, sd_bus_error
*error
) {
1774 return sd_bus_call_method(
1776 "org.freedesktop.systemd1",
1777 "/org/freedesktop/systemd1",
1778 "org.freedesktop.systemd1.Manager",
1782 "ssi", unit
, "all", signo
);
1785 int manager_unit_is_active(Manager
*manager
, const char *unit
) {
1786 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1787 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1788 _cleanup_free_
char *path
= NULL
;
1795 path
= unit_dbus_path_from_name(unit
);
1799 r
= sd_bus_get_property(
1801 "org.freedesktop.systemd1",
1803 "org.freedesktop.systemd1.Unit",
1809 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1810 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1813 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1814 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
1820 r
= sd_bus_message_read(reply
, "s", &state
);
1824 return !STR_IN_SET(state
, "inactive", "failed");
1827 int manager_job_is_active(Manager
*manager
, const char *path
) {
1828 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1829 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1835 r
= sd_bus_get_property(
1837 "org.freedesktop.systemd1",
1839 "org.freedesktop.systemd1.Job",
1845 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1846 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1849 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
1855 /* We don't actually care about the state really. The fact
1856 * that we could read the job state is enough for us */
1861 int manager_get_machine_by_pid(Manager
*m
, pid_t pid
, Machine
**machine
) {
1869 mm
= hashmap_get(m
->machine_leaders
, PID_TO_PTR(pid
));
1871 _cleanup_free_
char *unit
= NULL
;
1873 r
= cg_pid_get_unit(pid
, &unit
);
1875 mm
= hashmap_get(m
->machine_units
, unit
);
1884 int manager_add_machine(Manager
*m
, const char *name
, Machine
**_machine
) {
1890 machine
= hashmap_get(m
->machines
, name
);
1892 machine
= machine_new(m
, _MACHINE_CLASS_INVALID
, name
);
1898 *_machine
= machine
;
1903 int bus_reply_pair_array(sd_bus_message
*m
, char **l
) {
1904 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1908 r
= sd_bus_message_new_method_return(m
, &reply
);
1912 r
= sd_bus_message_open_container(reply
, 'a', "{ss}");
1916 STRV_FOREACH_PAIR(k
, v
, l
) {
1917 r
= sd_bus_message_append(reply
, "{ss}", *k
, *v
);
1922 r
= sd_bus_message_close_container(reply
);
1926 return sd_bus_send(NULL
, reply
, NULL
);