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 int property_get_pool_path(
39 const char *interface
,
41 sd_bus_message
*reply
,
43 sd_bus_error
*error
) {
48 return sd_bus_message_append(reply
, "s", "/var/lib/machines");
51 static int property_get_pool_usage(
54 const char *interface
,
56 sd_bus_message
*reply
,
58 sd_bus_error
*error
) {
60 _cleanup_close_
int fd
= -1;
61 uint64_t usage
= (uint64_t) -1;
67 /* We try to read the quota info from /var/lib/machines, as
68 * well as the usage of the loopback file
69 * /var/lib/machines.raw, and pick the larger value. */
71 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
75 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
79 if (stat("/var/lib/machines.raw", &st
) >= 0) {
80 if (usage
== (uint64_t) -1 || st
.st_blocks
* 512ULL > usage
)
81 usage
= st
.st_blocks
* 512ULL;
84 return sd_bus_message_append(reply
, "t", usage
);
87 static int property_get_pool_limit(
90 const char *interface
,
92 sd_bus_message
*reply
,
94 sd_bus_error
*error
) {
96 _cleanup_close_
int fd
= -1;
97 uint64_t size
= (uint64_t) -1;
103 /* We try to read the quota limit from /var/lib/machines, as
104 * well as the size of the loopback file
105 * /var/lib/machines.raw, and pick the smaller value. */
107 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
111 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
112 size
= q
.referenced_max
;
115 if (stat("/var/lib/machines.raw", &st
) >= 0) {
116 if (size
== (uint64_t) -1 || (uint64_t) st
.st_size
< size
)
120 return sd_bus_message_append(reply
, "t", size
);
123 static int method_get_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
124 _cleanup_free_
char *p
= NULL
;
125 Manager
*m
= userdata
;
133 r
= sd_bus_message_read(message
, "s", &name
);
137 machine
= hashmap_get(m
->machines
, name
);
139 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
141 p
= machine_bus_path(machine
);
145 return sd_bus_reply_method_return(message
, "o", p
);
148 static int method_get_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
149 _cleanup_free_
char *p
= NULL
;
150 Manager
*m
= userdata
;
157 r
= sd_bus_message_read(message
, "s", &name
);
161 r
= image_find(name
, NULL
);
163 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
167 p
= image_bus_path(name
);
171 return sd_bus_reply_method_return(message
, "o", p
);
174 static int method_get_machine_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
175 _cleanup_free_
char *p
= NULL
;
176 Manager
*m
= userdata
;
177 Machine
*machine
= NULL
;
184 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
186 r
= sd_bus_message_read(message
, "u", &pid
);
194 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
196 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
200 r
= sd_bus_creds_get_pid(creds
, &pid
);
205 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
209 return sd_bus_error_setf(error
, BUS_ERROR_NO_MACHINE_FOR_PID
, "PID "PID_FMT
" does not belong to any known machine", pid
);
211 p
= machine_bus_path(machine
);
215 return sd_bus_reply_method_return(message
, "o", p
);
218 static int method_list_machines(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
219 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
220 Manager
*m
= userdata
;
228 r
= sd_bus_message_new_method_return(message
, &reply
);
230 return sd_bus_error_set_errno(error
, r
);
232 r
= sd_bus_message_open_container(reply
, 'a', "(ssso)");
234 return sd_bus_error_set_errno(error
, r
);
236 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
237 _cleanup_free_
char *p
= NULL
;
239 p
= machine_bus_path(machine
);
243 r
= sd_bus_message_append(reply
, "(ssso)",
245 strempty(machine_class_to_string(machine
->class)),
249 return sd_bus_error_set_errno(error
, r
);
252 r
= sd_bus_message_close_container(reply
);
254 return sd_bus_error_set_errno(error
, r
);
256 return sd_bus_send(NULL
, reply
, NULL
);
259 static int method_create_or_register_machine(Manager
*manager
, sd_bus_message
*message
, bool read_network
, Machine
**_m
, sd_bus_error
*error
) {
260 const char *name
, *service
, *class, *root_directory
;
261 const int32_t *netif
= NULL
;
267 size_t n
, n_netif
= 0;
274 r
= sd_bus_message_read(message
, "s", &name
);
277 if (!machine_name_is_valid(name
))
278 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine name");
280 r
= sd_bus_message_read_array(message
, 'y', &v
, &n
);
288 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine ID parameter");
290 r
= sd_bus_message_read(message
, "ssus", &service
, &class, &leader
, &root_directory
);
297 r
= sd_bus_message_read_array(message
, 'i', (const void**) &netif
, &n_netif
);
301 n_netif
/= sizeof(int32_t);
303 for (i
= 0; i
< n_netif
; i
++) {
305 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid network interface index %i", netif
[i
]);
310 c
= _MACHINE_CLASS_INVALID
;
312 c
= machine_class_from_string(class);
314 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine class parameter");
318 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
320 if (!isempty(root_directory
) && !path_is_absolute(root_directory
))
321 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Root directory must be empty or an absolute path");
324 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
326 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
330 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
332 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
337 if (hashmap_get(manager
->machines
, name
))
338 return sd_bus_error_setf(error
, BUS_ERROR_MACHINE_EXISTS
, "Machine '%s' already exists", name
);
340 r
= manager_add_machine(manager
, name
, &m
);
348 if (!isempty(service
)) {
349 m
->service
= strdup(service
);
356 if (!isempty(root_directory
)) {
357 m
->root_directory
= strdup(root_directory
);
358 if (!m
->root_directory
) {
365 assert_cc(sizeof(int32_t) == sizeof(int));
366 m
->netif
= memdup(netif
, sizeof(int32_t) * n_netif
);
372 m
->n_netif
= n_netif
;
380 machine_add_to_gc_queue(m
);
384 static int method_create_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
385 Manager
*manager
= userdata
;
392 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
396 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
400 r
= machine_start(m
, message
, error
);
404 m
->create_message
= sd_bus_message_ref(message
);
408 machine_add_to_gc_queue(m
);
412 static int method_create_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
413 return method_create_machine_internal(message
, true, userdata
, error
);
416 static int method_create_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
417 return method_create_machine_internal(message
, false, userdata
, error
);
420 static int method_register_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
421 Manager
*manager
= userdata
;
422 _cleanup_free_
char *p
= NULL
;
429 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
433 r
= cg_pid_get_unit(m
->leader
, &m
->unit
);
435 r
= sd_bus_error_set_errnof(error
, r
,
436 "Failed to determine unit of process "PID_FMT
" : %m",
441 r
= machine_start(m
, NULL
, error
);
445 p
= machine_bus_path(m
);
451 return sd_bus_reply_method_return(message
, "o", p
);
454 machine_add_to_gc_queue(m
);
458 static int method_register_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
459 return method_register_machine_internal(message
, true, userdata
, error
);
462 static int method_register_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
463 return method_register_machine_internal(message
, false, userdata
, error
);
466 static int method_terminate_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
467 Manager
*m
= userdata
;
475 r
= sd_bus_message_read(message
, "s", &name
);
477 return sd_bus_error_set_errno(error
, r
);
479 machine
= hashmap_get(m
->machines
, name
);
481 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
483 return bus_machine_method_terminate(message
, machine
, error
);
486 static int method_kill_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
487 Manager
*m
= userdata
;
495 r
= sd_bus_message_read(message
, "s", &name
);
497 return sd_bus_error_set_errno(error
, r
);
499 machine
= hashmap_get(m
->machines
, name
);
501 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
503 return bus_machine_method_kill(message
, machine
, error
);
506 static int method_get_machine_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
507 Manager
*m
= userdata
;
515 r
= sd_bus_message_read(message
, "s", &name
);
517 return sd_bus_error_set_errno(error
, r
);
519 machine
= hashmap_get(m
->machines
, name
);
521 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
523 return bus_machine_method_get_addresses(message
, machine
, error
);
526 static int method_get_machine_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
527 Manager
*m
= userdata
;
535 r
= sd_bus_message_read(message
, "s", &name
);
537 return sd_bus_error_set_errno(error
, r
);
539 machine
= hashmap_get(m
->machines
, name
);
541 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
543 return bus_machine_method_get_os_release(message
, machine
, error
);
546 static int method_list_images(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
547 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
548 _cleanup_(image_hashmap_freep
) Hashmap
*images
= NULL
;
549 Manager
*m
= userdata
;
557 images
= hashmap_new(&string_hash_ops
);
561 r
= image_discover(images
);
565 r
= sd_bus_message_new_method_return(message
, &reply
);
569 r
= sd_bus_message_open_container(reply
, 'a', "(ssbttto)");
573 HASHMAP_FOREACH(image
, images
, i
) {
574 _cleanup_free_
char *p
= NULL
;
576 p
= image_bus_path(image
->name
);
580 r
= sd_bus_message_append(reply
, "(ssbttto)",
582 image_type_to_string(image
->type
),
592 r
= sd_bus_message_close_container(reply
);
596 return sd_bus_send(NULL
, reply
, NULL
);
599 static int method_open_machine_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
600 Manager
*m
= userdata
;
608 r
= sd_bus_message_read(message
, "s", &name
);
610 return sd_bus_error_set_errno(error
, r
);
612 machine
= hashmap_get(m
->machines
, name
);
614 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
616 return bus_machine_method_open_pty(message
, machine
, error
);
619 static int method_open_machine_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
620 Manager
*m
= userdata
;
628 r
= sd_bus_message_read(message
, "s", &name
);
632 machine
= hashmap_get(m
->machines
, name
);
634 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
636 return bus_machine_method_open_login(message
, machine
, error
);
639 static int method_open_machine_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
640 Manager
*m
= userdata
;
649 r
= sd_bus_message_read(message
, "s", &name
);
653 machine
= hashmap_get(m
->machines
, name
);
655 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
657 return bus_machine_method_open_shell(message
, machine
, error
);
660 static int method_bind_mount_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
661 Manager
*m
= userdata
;
669 r
= sd_bus_message_read(message
, "s", &name
);
673 machine
= hashmap_get(m
->machines
, name
);
675 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
677 return bus_machine_method_bind_mount(message
, machine
, error
);
680 static int method_copy_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
681 Manager
*m
= userdata
;
689 r
= sd_bus_message_read(message
, "s", &name
);
693 machine
= hashmap_get(m
->machines
, name
);
695 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
697 return bus_machine_method_copy(message
, machine
, error
);
700 static int method_open_machine_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
701 Manager
*m
= userdata
;
709 r
= sd_bus_message_read(message
, "s", &name
);
713 machine
= hashmap_get(m
->machines
, name
);
715 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
717 return bus_machine_method_open_root_directory(message
, machine
, error
);
720 static int method_get_machine_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
721 Manager
*m
= userdata
;
729 r
= sd_bus_message_read(message
, "s", &name
);
733 machine
= hashmap_get(m
->machines
, name
);
735 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
737 return bus_machine_method_get_uid_shift(message
, machine
, error
);
740 static int method_remove_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
741 _cleanup_(image_unrefp
) Image
* i
= NULL
;
747 r
= sd_bus_message_read(message
, "s", &name
);
751 if (!image_name_is_valid(name
))
752 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
754 r
= image_find(name
, &i
);
758 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
760 i
->userdata
= userdata
;
761 return bus_image_method_remove(message
, i
, error
);
764 static int method_rename_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
765 _cleanup_(image_unrefp
) Image
* i
= NULL
;
766 const char *old_name
;
771 r
= sd_bus_message_read(message
, "s", &old_name
);
775 if (!image_name_is_valid(old_name
))
776 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", old_name
);
778 r
= image_find(old_name
, &i
);
782 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", old_name
);
784 i
->userdata
= userdata
;
785 return bus_image_method_rename(message
, i
, error
);
788 static int method_clone_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
789 _cleanup_(image_unrefp
) Image
*i
= NULL
;
790 const char *old_name
;
795 r
= sd_bus_message_read(message
, "s", &old_name
);
799 if (!image_name_is_valid(old_name
))
800 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", old_name
);
802 r
= image_find(old_name
, &i
);
806 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", old_name
);
808 i
->userdata
= userdata
;
809 return bus_image_method_clone(message
, i
, error
);
812 static int method_mark_image_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
813 _cleanup_(image_unrefp
) Image
*i
= NULL
;
819 r
= sd_bus_message_read(message
, "s", &name
);
823 if (!image_name_is_valid(name
))
824 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
826 r
= image_find(name
, &i
);
830 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
832 i
->userdata
= userdata
;
833 return bus_image_method_mark_read_only(message
, i
, error
);
836 static int method_get_image_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
837 _cleanup_(image_unrefp
) Image
*i
= NULL
;
843 r
= sd_bus_message_read(message
, "s", &name
);
847 if (!image_name_is_valid(name
))
848 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
850 r
= image_find(name
, &i
);
854 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
856 i
->userdata
= userdata
;
857 return bus_image_method_get_hostname(message
, i
, error
);
860 static int method_get_image_machine_id(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
861 _cleanup_(image_unrefp
) Image
*i
= NULL
;
867 r
= sd_bus_message_read(message
, "s", &name
);
871 if (!image_name_is_valid(name
))
872 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
874 r
= image_find(name
, &i
);
878 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
880 i
->userdata
= userdata
;
881 return bus_image_method_get_machine_id(message
, i
, error
);
884 static int method_get_image_machine_info(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
885 _cleanup_(image_unrefp
) Image
*i
= NULL
;
891 r
= sd_bus_message_read(message
, "s", &name
);
895 if (!image_name_is_valid(name
))
896 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
898 r
= image_find(name
, &i
);
902 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
904 i
->userdata
= userdata
;
905 return bus_image_method_get_machine_info(message
, i
, error
);
908 static int method_get_image_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
909 _cleanup_(image_unrefp
) Image
*i
= NULL
;
915 r
= sd_bus_message_read(message
, "s", &name
);
919 if (!image_name_is_valid(name
))
920 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
922 r
= image_find(name
, &i
);
926 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
928 i
->userdata
= userdata
;
929 return bus_image_method_get_os_release(message
, i
, error
);
932 static int clean_pool_done(Operation
*operation
, int ret
, sd_bus_error
*error
) {
933 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
934 _cleanup_fclose_
FILE *f
= NULL
;
940 assert(operation
->extra_fd
>= 0);
942 if (lseek(operation
->extra_fd
, 0, SEEK_SET
) == (off_t
) -1)
945 f
= fdopen(operation
->extra_fd
, "re");
949 operation
->extra_fd
= -1;
951 /* The resulting temporary file starts with a boolean value that indicates success or not. */
953 n
= fread(&success
, 1, sizeof(success
), f
);
954 if (n
!= sizeof(success
))
955 return ret
< 0 ? ret
: (errno
!= 0 ? -errno
: -EIO
);
958 _cleanup_free_
char *name
= NULL
;
960 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
961 * set to false followed by the name of the failed image. Let's try to read this and use it for the
962 * error message. If we can't read it, don't mind, and return the naked error. */
964 if (success
) /* The resulting temporary file could not be updated, ignore it. */
967 r
= read_nul_string(f
, &name
);
968 if (r
< 0 || isempty(name
)) /* Same here... */
971 return sd_bus_error_set_errnof(error
, ret
, "Failed to remove image %s: %m", name
);
976 r
= sd_bus_message_new_method_return(operation
->message
, &reply
);
980 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
984 /* On success the resulting temporary file will contain a list of image names that were removed followed by
985 * their size on disk. Let's read that and turn it into a bus message. */
987 _cleanup_free_
char *name
= NULL
;
990 r
= read_nul_string(f
, &name
);
993 if (isempty(name
)) /* reached the end */
997 n
= fread(&size
, 1, sizeof(size
), f
);
998 if (n
!= sizeof(size
))
999 return errno
!= 0 ? -errno
: -EIO
;
1001 r
= sd_bus_message_append(reply
, "(st)", name
, size
);
1006 r
= sd_bus_message_close_container(reply
);
1010 return sd_bus_send(NULL
, reply
, NULL
);
1013 static int method_clean_pool(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1019 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
1020 _cleanup_close_
int result_fd
= -1;
1021 Manager
*m
= userdata
;
1022 Operation
*operation
;
1029 if (m
->n_operations
>= OPERATIONS_MAX
)
1030 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing operations.");
1032 r
= sd_bus_message_read(message
, "s", &mm
);
1036 if (streq(mm
, "all"))
1038 else if (streq(mm
, "hidden"))
1039 mode
= REMOVE_HIDDEN
;
1041 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown mode '%s'.", mm
);
1043 r
= bus_verify_polkit_async(
1046 "org.freedesktop.machine1.manage-machines",
1050 &m
->polkit_registry
,
1055 return 1; /* Will call us back */
1057 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1058 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1060 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
1061 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
1063 result_fd
= open_tmpfile_unlinkable(NULL
, O_RDWR
|O_CLOEXEC
);
1067 /* This might be a slow operation, run it asynchronously in a background process */
1068 r
= safe_fork("(sd-clean)", FORK_RESET_SIGNALS
, &child
);
1070 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1072 _cleanup_(image_hashmap_freep
) Hashmap
*images
= NULL
;
1073 bool success
= true;
1078 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1080 images
= hashmap_new(&string_hash_ops
);
1086 r
= image_discover(images
);
1090 l
= write(result_fd
, &success
, sizeof(success
));
1096 HASHMAP_FOREACH(image
, images
, i
) {
1098 /* We can't remove vendor images (i.e. those in /usr) */
1099 if (IMAGE_IS_VENDOR(image
))
1102 if (IMAGE_IS_HOST(image
))
1105 if (mode
== REMOVE_HIDDEN
&& !IMAGE_IS_HIDDEN(image
))
1108 r
= image_remove(image
);
1109 if (r
== -EBUSY
) /* keep images that are currently being used. */
1112 /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */
1114 (void) ftruncate(result_fd
, 0);
1115 (void) lseek(result_fd
, 0, SEEK_SET
);
1116 (void) write(result_fd
, &success
, sizeof(success
));
1117 (void) write(result_fd
, image
->name
, strlen(image
->name
)+1);
1121 l
= write(result_fd
, image
->name
, strlen(image
->name
)+1);
1127 l
= write(result_fd
, &image
->usage_exclusive
, sizeof(image
->usage_exclusive
));
1134 result_fd
= safe_close(result_fd
);
1135 _exit(EXIT_SUCCESS
);
1138 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1139 _exit(EXIT_FAILURE
);
1142 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1144 /* The clean-up might take a while, hence install a watch on the child and return */
1146 r
= operation_new(m
, NULL
, child
, message
, errno_pipe_fd
[0], &operation
);
1148 (void) sigkill_wait(child
);
1152 operation
->extra_fd
= result_fd
;
1153 operation
->done
= clean_pool_done
;
1156 errno_pipe_fd
[0] = -1;
1161 static int method_set_pool_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1162 Manager
*m
= userdata
;
1168 r
= sd_bus_message_read(message
, "t", &limit
);
1171 if (!FILE_SIZE_VALID_OR_INFINITY(limit
))
1172 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "New limit out of range");
1174 r
= bus_verify_polkit_async(
1177 "org.freedesktop.machine1.manage-machines",
1181 &m
->polkit_registry
,
1186 return 1; /* Will call us back */
1188 /* Set up the machine directory if necessary */
1189 r
= setup_machine_directory(limit
, error
);
1193 /* Resize the backing loopback device, if there is one, except if we asked to drop any limit */
1194 if (limit
!= (uint64_t) -1) {
1195 r
= btrfs_resize_loopback("/var/lib/machines", limit
, false);
1197 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
1198 if (r
< 0 && r
!= -ENODEV
) /* ignore ENODEV, as that's what is returned if the file system is not on loopback */
1199 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust loopback limit: %m");
1202 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit
);
1204 r
= btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit
);
1206 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
1208 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust quota limit: %m");
1210 return sd_bus_reply_method_return(message
, NULL
);
1213 static int method_set_image_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1214 _cleanup_(image_unrefp
) Image
*i
= NULL
;
1220 r
= sd_bus_message_read(message
, "s", &name
);
1224 if (!image_name_is_valid(name
))
1225 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
1227 r
= image_find(name
, &i
);
1231 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
1233 i
->userdata
= userdata
;
1234 return bus_image_method_set_limit(message
, i
, error
);
1237 static int method_map_from_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1238 _cleanup_fclose_
FILE *f
= NULL
;
1239 Manager
*m
= userdata
;
1240 const char *name
, *p
;
1245 r
= sd_bus_message_read(message
, "su", &name
, &uid
);
1249 if (!uid_is_valid(uid
))
1250 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
1252 machine
= hashmap_get(m
->machines
, name
);
1254 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
1256 if (machine
->class != MACHINE_CONTAINER
)
1257 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
1259 p
= procfs_file_alloca(machine
->leader
, "uid_map");
1265 uid_t uid_base
, uid_shift
, uid_range
, converted
;
1269 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
1270 if (k
< 0 && feof(f
))
1273 if (ferror(f
) && errno
> 0)
1279 if (uid
< uid_base
|| uid
>= uid_base
+ uid_range
)
1282 converted
= uid
- uid_base
+ uid_shift
;
1283 if (!uid_is_valid(converted
))
1284 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
1286 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
1289 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching user mappings.", name
);
1292 static int method_map_to_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1293 Manager
*m
= userdata
;
1299 r
= sd_bus_message_read(message
, "u", &uid
);
1302 if (!uid_is_valid(uid
))
1303 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
1305 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "User " UID_FMT
" belongs to host UID range", uid
);
1307 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1308 _cleanup_fclose_
FILE *f
= NULL
;
1309 char p
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
1311 if (machine
->class != MACHINE_CONTAINER
)
1314 xsprintf(p
, "/proc/" UID_FMT
"/uid_map", machine
->leader
);
1317 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
1322 _cleanup_free_
char *o
= NULL
;
1323 uid_t uid_base
, uid_shift
, uid_range
, converted
;
1327 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
1328 if (k
< 0 && feof(f
))
1331 if (ferror(f
) && errno
> 0)
1337 if (uid
< uid_shift
|| uid
>= uid_shift
+ uid_range
)
1340 converted
= (uid
- uid_shift
+ uid_base
);
1341 if (!uid_is_valid(converted
))
1342 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
1344 o
= machine_bus_path(machine
);
1348 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1352 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "No matching user mapping for " UID_FMT
".", uid
);
1355 static int method_map_from_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1356 _cleanup_fclose_
FILE *f
= NULL
;
1357 Manager
*m
= groupdata
;
1358 const char *name
, *p
;
1363 r
= sd_bus_message_read(message
, "su", &name
, &gid
);
1367 if (!gid_is_valid(gid
))
1368 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1370 machine
= hashmap_get(m
->machines
, name
);
1372 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
1374 if (machine
->class != MACHINE_CONTAINER
)
1375 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
1377 p
= procfs_file_alloca(machine
->leader
, "gid_map");
1383 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1387 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1388 if (k
< 0 && feof(f
))
1391 if (ferror(f
) && errno
> 0)
1397 if (gid
< gid_base
|| gid
>= gid_base
+ gid_range
)
1400 converted
= gid
- gid_base
+ gid_shift
;
1401 if (!gid_is_valid(converted
))
1402 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1404 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
1407 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Machine '%s' has no matching group mappings.", name
);
1410 static int method_map_to_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1411 Manager
*m
= groupdata
;
1417 r
= sd_bus_message_read(message
, "u", &gid
);
1420 if (!gid_is_valid(gid
))
1421 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1423 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Group " GID_FMT
" belongs to host GID range", gid
);
1425 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1426 _cleanup_fclose_
FILE *f
= NULL
;
1427 char p
[STRLEN("/proc//gid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
1429 if (machine
->class != MACHINE_CONTAINER
)
1432 xsprintf(p
, "/proc/" GID_FMT
"/gid_map", machine
->leader
);
1435 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
1440 _cleanup_free_
char *o
= NULL
;
1441 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1445 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1446 if (k
< 0 && feof(f
))
1449 if (ferror(f
) && errno
> 0)
1455 if (gid
< gid_shift
|| gid
>= gid_shift
+ gid_range
)
1458 converted
= (gid
- gid_shift
+ gid_base
);
1459 if (!gid_is_valid(converted
))
1460 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1462 o
= machine_bus_path(machine
);
1466 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1470 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "No matching group mapping for " GID_FMT
".", gid
);
1473 const sd_bus_vtable manager_vtable
[] = {
1474 SD_BUS_VTABLE_START(0),
1475 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path
, 0, 0),
1476 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage
, 0, 0),
1477 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit
, 0, 0),
1478 SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1479 SD_BUS_METHOD("GetImage", "s", "o", method_get_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1480 SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
1481 SD_BUS_METHOD("ListMachines", NULL
, "a(ssso)", method_list_machines
, SD_BUS_VTABLE_UNPRIVILEGED
),
1482 SD_BUS_METHOD("ListImages", NULL
, "a(ssbttto)", method_list_images
, SD_BUS_VTABLE_UNPRIVILEGED
),
1483 SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine
, 0),
1484 SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network
, 0),
1485 SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine
, 0),
1486 SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network
, 0),
1487 SD_BUS_METHOD("TerminateMachine", "s", NULL
, method_terminate_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1488 SD_BUS_METHOD("KillMachine", "ssi", NULL
, method_kill_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1489 SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1490 SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1491 SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty
, 0),
1492 SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1493 SD_BUS_METHOD("OpenMachineShell", "sssasas", "hs", method_open_machine_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1494 SD_BUS_METHOD("BindMountMachine", "sssbb", NULL
, method_bind_mount_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1495 SD_BUS_METHOD("CopyFromMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1496 SD_BUS_METHOD("CopyToMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1497 SD_BUS_METHOD("OpenMachineRootDirectory", "s", "h", method_open_machine_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1498 SD_BUS_METHOD("GetMachineUIDShift", "s", "u", method_get_machine_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1499 SD_BUS_METHOD("RemoveImage", "s", NULL
, method_remove_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1500 SD_BUS_METHOD("RenameImage", "ss", NULL
, method_rename_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1501 SD_BUS_METHOD("CloneImage", "ssb", NULL
, method_clone_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1502 SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL
, method_mark_image_read_only
, SD_BUS_VTABLE_UNPRIVILEGED
),
1503 SD_BUS_METHOD("GetImageHostname", "s", "s", method_get_image_hostname
, SD_BUS_VTABLE_UNPRIVILEGED
),
1504 SD_BUS_METHOD("GetImageMachineID", "s", "ay", method_get_image_machine_id
, SD_BUS_VTABLE_UNPRIVILEGED
),
1505 SD_BUS_METHOD("GetImageMachineInfo", "s", "a{ss}", method_get_image_machine_info
, SD_BUS_VTABLE_UNPRIVILEGED
),
1506 SD_BUS_METHOD("GetImageOSRelease", "s", "a{ss}", method_get_image_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1507 SD_BUS_METHOD("SetPoolLimit", "t", NULL
, method_set_pool_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1508 SD_BUS_METHOD("SetImageLimit", "st", NULL
, method_set_image_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1509 SD_BUS_METHOD("CleanPool", "s", "a(st)", method_clean_pool
, SD_BUS_VTABLE_UNPRIVILEGED
),
1510 SD_BUS_METHOD("MapFromMachineUser", "su", "u", method_map_from_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1511 SD_BUS_METHOD("MapToMachineUser", "u", "sou", method_map_to_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1512 SD_BUS_METHOD("MapFromMachineGroup", "su", "u", method_map_from_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1513 SD_BUS_METHOD("MapToMachineGroup", "u", "sou", method_map_to_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1514 SD_BUS_SIGNAL("MachineNew", "so", 0),
1515 SD_BUS_SIGNAL("MachineRemoved", "so", 0),
1519 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1520 const char *path
, *result
, *unit
;
1521 Manager
*m
= userdata
;
1529 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
1531 bus_log_parse_error(r
);
1535 machine
= hashmap_get(m
->machine_units
, unit
);
1539 if (streq_ptr(path
, machine
->scope_job
)) {
1540 machine
->scope_job
= mfree(machine
->scope_job
);
1542 if (machine
->started
) {
1543 if (streq(result
, "done"))
1544 machine_send_create_reply(machine
, NULL
);
1546 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
1548 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit %s failed with '%s'", unit
, result
);
1550 machine_send_create_reply(machine
, &e
);
1554 machine_save(machine
);
1557 machine_add_to_gc_queue(machine
);
1561 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1562 _cleanup_free_
char *unit
= NULL
;
1564 Manager
*m
= userdata
;
1571 path
= sd_bus_message_get_path(message
);
1575 r
= unit_name_from_dbus_path(path
, &unit
);
1576 if (r
== -EINVAL
) /* not for a unit */
1583 machine
= hashmap_get(m
->machine_units
, unit
);
1587 machine_add_to_gc_queue(machine
);
1591 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1592 const char *path
, *unit
;
1593 Manager
*m
= userdata
;
1600 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
1602 bus_log_parse_error(r
);
1606 machine
= hashmap_get(m
->machine_units
, unit
);
1610 machine_add_to_gc_queue(machine
);
1614 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1615 Manager
*m
= userdata
;
1623 r
= sd_bus_message_read(message
, "b", &b
);
1625 bus_log_parse_error(r
);
1631 /* systemd finished reloading, let's recheck all our machines */
1632 log_debug("System manager has been reloaded, rechecking machines...");
1634 HASHMAP_FOREACH(machine
, m
->machines
, i
)
1635 machine_add_to_gc_queue(machine
);
1640 int manager_start_scope(
1645 const char *description
,
1646 sd_bus_message
*more_properties
,
1647 sd_bus_error
*error
,
1650 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1657 r
= sd_bus_message_new_method_call(
1660 "org.freedesktop.systemd1",
1661 "/org/freedesktop/systemd1",
1662 "org.freedesktop.systemd1.Manager",
1663 "StartTransientUnit");
1667 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
1671 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1675 if (!isempty(slice
)) {
1676 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
1681 if (!isempty(description
)) {
1682 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
1687 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
1691 r
= sd_bus_message_append(m
, "(sv)", "Delegate", "b", 1);
1695 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", UINT64_C(16384));
1697 return bus_log_create_error(r
);
1699 if (more_properties
) {
1700 r
= sd_bus_message_copy(m
, more_properties
, true);
1705 r
= sd_bus_message_close_container(m
);
1709 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1713 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
1721 r
= sd_bus_message_read(reply
, "o", &j
);
1735 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
1736 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1742 r
= sd_bus_call_method(
1744 "org.freedesktop.systemd1",
1745 "/org/freedesktop/systemd1",
1746 "org.freedesktop.systemd1.Manager",
1750 "ss", unit
, "fail");
1752 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1753 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
1758 sd_bus_error_free(error
);
1769 r
= sd_bus_message_read(reply
, "o", &j
);
1783 int manager_kill_unit(Manager
*manager
, const char *unit
, int signo
, sd_bus_error
*error
) {
1787 return sd_bus_call_method(
1789 "org.freedesktop.systemd1",
1790 "/org/freedesktop/systemd1",
1791 "org.freedesktop.systemd1.Manager",
1795 "ssi", unit
, "all", signo
);
1798 int manager_unit_is_active(Manager
*manager
, const char *unit
) {
1799 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1800 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1801 _cleanup_free_
char *path
= NULL
;
1808 path
= unit_dbus_path_from_name(unit
);
1812 r
= sd_bus_get_property(
1814 "org.freedesktop.systemd1",
1816 "org.freedesktop.systemd1.Unit",
1822 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1823 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1826 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1827 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
1833 r
= sd_bus_message_read(reply
, "s", &state
);
1837 return !STR_IN_SET(state
, "inactive", "failed");
1840 int manager_job_is_active(Manager
*manager
, const char *path
) {
1841 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1842 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1848 r
= sd_bus_get_property(
1850 "org.freedesktop.systemd1",
1852 "org.freedesktop.systemd1.Job",
1858 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1859 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1862 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
1868 /* We don't actually care about the state really. The fact
1869 * that we could read the job state is enough for us */
1874 int manager_get_machine_by_pid(Manager
*m
, pid_t pid
, Machine
**machine
) {
1882 mm
= hashmap_get(m
->machine_leaders
, PID_TO_PTR(pid
));
1884 _cleanup_free_
char *unit
= NULL
;
1886 r
= cg_pid_get_unit(pid
, &unit
);
1888 mm
= hashmap_get(m
->machine_units
, unit
);
1897 int manager_add_machine(Manager
*m
, const char *name
, Machine
**_machine
) {
1903 machine
= hashmap_get(m
->machines
, name
);
1905 machine
= machine_new(m
, _MACHINE_CLASS_INVALID
, name
);
1911 *_machine
= machine
;
1916 int bus_reply_pair_array(sd_bus_message
*m
, char **l
) {
1917 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1921 r
= sd_bus_message_new_method_return(m
, &reply
);
1925 r
= sd_bus_message_open_container(reply
, 'a', "{ss}");
1929 STRV_FOREACH_PAIR(k
, v
, l
) {
1930 r
= sd_bus_message_append(reply
, "{ss}", *k
, *v
);
1935 r
= sd_bus_message_close_container(reply
);
1939 return sd_bus_send(NULL
, reply
, NULL
);