1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
9 #include "btrfs-util.h"
10 #include "bus-common-errors.h"
11 #include "bus-get-properties.h"
12 #include "bus-locator.h"
13 #include "bus-polkit.h"
14 #include "cgroup-util.h"
15 #include "errno-util.h"
18 #include "format-util.h"
19 #include "hostname-util.h"
20 #include "image-dbus.h"
22 #include "machine-dbus.h"
23 #include "machine-image.h"
24 #include "machine-pool.h"
26 #include "missing_capability.h"
27 #include "path-util.h"
28 #include "process-util.h"
29 #include "stdio-util.h"
31 #include "tmpfile-util.h"
32 #include "unit-name.h"
33 #include "user-util.h"
35 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_pool_path
, "s", "/var/lib/machines");
37 static int property_get_pool_usage(
40 const char *interface
,
42 sd_bus_message
*reply
,
44 sd_bus_error
*error
) {
46 _cleanup_close_
int fd
= -1;
47 uint64_t usage
= (uint64_t) -1;
52 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
56 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
60 return sd_bus_message_append(reply
, "t", usage
);
63 static int property_get_pool_limit(
66 const char *interface
,
68 sd_bus_message
*reply
,
70 sd_bus_error
*error
) {
72 _cleanup_close_
int fd
= -1;
73 uint64_t size
= (uint64_t) -1;
78 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
82 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
83 size
= q
.referenced_max
;
86 return sd_bus_message_append(reply
, "t", size
);
89 static int method_get_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
90 _cleanup_free_
char *p
= NULL
;
91 Manager
*m
= userdata
;
99 r
= sd_bus_message_read(message
, "s", &name
);
103 machine
= hashmap_get(m
->machines
, name
);
105 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
107 p
= machine_bus_path(machine
);
111 return sd_bus_reply_method_return(message
, "o", p
);
114 static int method_get_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
115 _cleanup_free_
char *p
= NULL
;
116 _unused_ Manager
*m
= userdata
;
123 r
= sd_bus_message_read(message
, "s", &name
);
127 r
= image_find(IMAGE_MACHINE
, name
, NULL
);
129 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
133 p
= image_bus_path(name
);
137 return sd_bus_reply_method_return(message
, "o", p
);
140 static int method_get_machine_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
141 _cleanup_free_
char *p
= NULL
;
142 Manager
*m
= userdata
;
143 Machine
*machine
= NULL
;
150 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
152 r
= sd_bus_message_read(message
, "u", &pid
);
160 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
162 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
166 r
= sd_bus_creds_get_pid(creds
, &pid
);
171 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
175 return sd_bus_error_setf(error
, BUS_ERROR_NO_MACHINE_FOR_PID
, "PID "PID_FMT
" does not belong to any known machine", pid
);
177 p
= machine_bus_path(machine
);
181 return sd_bus_reply_method_return(message
, "o", p
);
184 static int method_list_machines(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
185 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
186 Manager
*m
= userdata
;
193 r
= sd_bus_message_new_method_return(message
, &reply
);
195 return sd_bus_error_set_errno(error
, r
);
197 r
= sd_bus_message_open_container(reply
, 'a', "(ssso)");
199 return sd_bus_error_set_errno(error
, r
);
201 HASHMAP_FOREACH(machine
, m
->machines
) {
202 _cleanup_free_
char *p
= NULL
;
204 p
= machine_bus_path(machine
);
208 r
= sd_bus_message_append(reply
, "(ssso)",
210 strempty(machine_class_to_string(machine
->class)),
214 return sd_bus_error_set_errno(error
, r
);
217 r
= sd_bus_message_close_container(reply
);
219 return sd_bus_error_set_errno(error
, r
);
221 return sd_bus_send(NULL
, reply
, NULL
);
224 static int method_create_or_register_machine(Manager
*manager
, sd_bus_message
*message
, bool read_network
, Machine
**_m
, sd_bus_error
*error
) {
225 const char *name
, *service
, *class, *root_directory
;
226 const int32_t *netif
= NULL
;
232 size_t n
, n_netif
= 0;
239 r
= sd_bus_message_read(message
, "s", &name
);
242 if (!machine_name_is_valid(name
))
243 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine name");
245 r
= sd_bus_message_read_array(message
, 'y', &v
, &n
);
253 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine ID parameter");
255 r
= sd_bus_message_read(message
, "ssus", &service
, &class, &leader
, &root_directory
);
262 r
= sd_bus_message_read_array(message
, 'i', (const void**) &netif
, &n_netif
);
266 n_netif
/= sizeof(int32_t);
268 for (i
= 0; i
< n_netif
; i
++) {
270 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid network interface index %i", netif
[i
]);
275 c
= _MACHINE_CLASS_INVALID
;
277 c
= machine_class_from_string(class);
279 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine class parameter");
283 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
285 if (!isempty(root_directory
) && !path_is_absolute(root_directory
))
286 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Root directory must be empty or an absolute path");
289 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
291 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
295 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
297 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
302 if (hashmap_get(manager
->machines
, name
))
303 return sd_bus_error_setf(error
, BUS_ERROR_MACHINE_EXISTS
, "Machine '%s' already exists", name
);
305 r
= manager_add_machine(manager
, name
, &m
);
313 if (!isempty(service
)) {
314 m
->service
= strdup(service
);
321 if (!isempty(root_directory
)) {
322 m
->root_directory
= strdup(root_directory
);
323 if (!m
->root_directory
) {
330 assert_cc(sizeof(int32_t) == sizeof(int));
331 m
->netif
= memdup(netif
, sizeof(int32_t) * n_netif
);
337 m
->n_netif
= n_netif
;
345 machine_add_to_gc_queue(m
);
349 static int method_create_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
350 Manager
*manager
= userdata
;
357 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
361 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
365 r
= machine_start(m
, message
, error
);
369 m
->create_message
= sd_bus_message_ref(message
);
373 machine_add_to_gc_queue(m
);
377 static int method_create_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
378 return method_create_machine_internal(message
, true, userdata
, error
);
381 static int method_create_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
382 return method_create_machine_internal(message
, false, userdata
, error
);
385 static int method_register_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
386 Manager
*manager
= userdata
;
387 _cleanup_free_
char *p
= NULL
;
394 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
398 r
= cg_pid_get_unit(m
->leader
, &m
->unit
);
400 r
= sd_bus_error_set_errnof(error
, r
,
401 "Failed to determine unit of process "PID_FMT
" : %m",
406 r
= machine_start(m
, NULL
, error
);
410 p
= machine_bus_path(m
);
416 return sd_bus_reply_method_return(message
, "o", p
);
419 machine_add_to_gc_queue(m
);
423 static int method_register_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
424 return method_register_machine_internal(message
, true, userdata
, error
);
427 static int method_register_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
428 return method_register_machine_internal(message
, false, userdata
, error
);
431 static int redirect_method_to_machine(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
440 r
= sd_bus_message_read(message
, "s", &name
);
442 return sd_bus_error_set_errno(error
, r
);
444 machine
= hashmap_get(m
->machines
, name
);
446 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
448 return method(message
, machine
, error
);
451 static int method_unregister_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
452 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_unregister
);
455 static int method_terminate_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
456 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_terminate
);
459 static int method_kill_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
460 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_kill
);
463 static int method_get_machine_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
464 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_addresses
);
467 static int method_get_machine_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
468 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_os_release
);
471 static int method_list_images(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
472 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
473 _cleanup_hashmap_free_ Hashmap
*images
= NULL
;
474 _unused_ Manager
*m
= userdata
;
481 images
= hashmap_new(&image_hash_ops
);
485 r
= image_discover(IMAGE_MACHINE
, images
);
489 r
= sd_bus_message_new_method_return(message
, &reply
);
493 r
= sd_bus_message_open_container(reply
, 'a', "(ssbttto)");
497 HASHMAP_FOREACH(image
, images
) {
498 _cleanup_free_
char *p
= NULL
;
500 p
= image_bus_path(image
->name
);
504 r
= sd_bus_message_append(reply
, "(ssbttto)",
506 image_type_to_string(image
->type
),
516 r
= sd_bus_message_close_container(reply
);
520 return sd_bus_send(NULL
, reply
, NULL
);
523 static int method_open_machine_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
524 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_pty
);
527 static int method_open_machine_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
528 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_login
);
531 static int method_open_machine_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
532 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_shell
);
535 static int method_bind_mount_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
536 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_bind_mount
);
539 static int method_copy_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
540 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_copy
);
543 static int method_open_machine_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
544 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_root_directory
);
547 static int method_get_machine_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
548 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_uid_shift
);
551 static int redirect_method_to_image(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
552 _cleanup_(image_unrefp
) Image
* i
= NULL
;
560 r
= sd_bus_message_read(message
, "s", &name
);
564 if (!image_name_is_valid(name
))
565 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
567 r
= image_find(IMAGE_MACHINE
, name
, &i
);
569 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
574 return method(message
, i
, error
);
577 static int method_remove_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
578 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_remove
);
581 static int method_rename_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
582 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_rename
);
585 static int method_clone_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
586 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_clone
);
589 static int method_mark_image_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
590 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_mark_read_only
);
593 static int method_get_image_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
594 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_hostname
);
597 static int method_get_image_machine_id(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
598 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_id
);
601 static int method_get_image_machine_info(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
602 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_info
);
605 static int method_get_image_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
606 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_os_release
);
609 static int clean_pool_done(Operation
*operation
, int ret
, sd_bus_error
*error
) {
610 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
611 _cleanup_fclose_
FILE *f
= NULL
;
617 assert(operation
->extra_fd
>= 0);
619 if (lseek(operation
->extra_fd
, 0, SEEK_SET
) == (off_t
) -1)
622 f
= take_fdopen(&operation
->extra_fd
, "r");
626 /* The resulting temporary file starts with a boolean value that indicates success or not. */
628 n
= fread(&success
, 1, sizeof(success
), f
);
629 if (n
!= sizeof(success
))
630 return ret
< 0 ? ret
: errno_or_else(EIO
);
633 _cleanup_free_
char *name
= NULL
;
635 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
636 * set to false followed by the name of the failed image. Let's try to read this and use it for the
637 * error message. If we can't read it, don't mind, and return the naked error. */
639 if (success
) /* The resulting temporary file could not be updated, ignore it. */
642 r
= read_nul_string(f
, LONG_LINE_MAX
, &name
);
643 if (r
<= 0) /* Same here... */
646 return sd_bus_error_set_errnof(error
, ret
, "Failed to remove image %s: %m", name
);
651 r
= sd_bus_message_new_method_return(operation
->message
, &reply
);
655 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
659 /* On success the resulting temporary file will contain a list of image names that were removed followed by
660 * their size on disk. Let's read that and turn it into a bus message. */
662 _cleanup_free_
char *name
= NULL
;
665 r
= read_nul_string(f
, LONG_LINE_MAX
, &name
);
668 if (r
== 0) /* reached the end */
672 n
= fread(&size
, 1, sizeof(size
), f
);
673 if (n
!= sizeof(size
))
674 return errno_or_else(EIO
);
676 r
= sd_bus_message_append(reply
, "(st)", name
, size
);
681 r
= sd_bus_message_close_container(reply
);
685 return sd_bus_send(NULL
, reply
, NULL
);
688 static int method_clean_pool(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
694 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
695 _cleanup_close_
int result_fd
= -1;
696 Manager
*m
= userdata
;
697 Operation
*operation
;
704 if (m
->n_operations
>= OPERATIONS_MAX
)
705 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing operations.");
707 r
= sd_bus_message_read(message
, "s", &mm
);
711 if (streq(mm
, "all"))
713 else if (streq(mm
, "hidden"))
714 mode
= REMOVE_HIDDEN
;
716 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown mode '%s'.", mm
);
718 r
= bus_verify_polkit_async(
721 "org.freedesktop.machine1.manage-machines",
730 return 1; /* Will call us back */
732 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
733 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
735 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
736 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
738 result_fd
= open_tmpfile_unlinkable(NULL
, O_RDWR
|O_CLOEXEC
);
742 /* This might be a slow operation, run it asynchronously in a background process */
743 r
= safe_fork("(sd-clean)", FORK_RESET_SIGNALS
, &child
);
745 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
747 _cleanup_hashmap_free_ Hashmap
*images
= NULL
;
752 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
754 images
= hashmap_new(&image_hash_ops
);
760 r
= image_discover(IMAGE_MACHINE
, images
);
764 l
= write(result_fd
, &success
, sizeof(success
));
770 HASHMAP_FOREACH(image
, images
) {
772 /* We can't remove vendor images (i.e. those in /usr) */
773 if (IMAGE_IS_VENDOR(image
))
776 if (IMAGE_IS_HOST(image
))
779 if (mode
== REMOVE_HIDDEN
&& !IMAGE_IS_HIDDEN(image
))
782 r
= image_remove(image
);
783 if (r
== -EBUSY
) /* keep images that are currently being used. */
786 /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */
788 (void) ftruncate(result_fd
, 0);
789 (void) lseek(result_fd
, 0, SEEK_SET
);
790 (void) write(result_fd
, &success
, sizeof(success
));
791 (void) write(result_fd
, image
->name
, strlen(image
->name
)+1);
795 l
= write(result_fd
, image
->name
, strlen(image
->name
)+1);
801 l
= write(result_fd
, &image
->usage_exclusive
, sizeof(image
->usage_exclusive
));
808 result_fd
= safe_close(result_fd
);
812 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
816 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
818 /* The clean-up might take a while, hence install a watch on the child and return */
820 r
= operation_new(m
, NULL
, child
, message
, errno_pipe_fd
[0], &operation
);
822 (void) sigkill_wait(child
);
826 operation
->extra_fd
= result_fd
;
827 operation
->done
= clean_pool_done
;
830 errno_pipe_fd
[0] = -1;
835 static int method_set_pool_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
836 Manager
*m
= userdata
;
842 r
= sd_bus_message_read(message
, "t", &limit
);
845 if (!FILE_SIZE_VALID_OR_INFINITY(limit
))
846 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "New limit out of range");
848 r
= bus_verify_polkit_async(
851 "org.freedesktop.machine1.manage-machines",
860 return 1; /* Will call us back */
862 /* Set up the machine directory if necessary */
863 r
= setup_machine_directory(error
);
867 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit
);
869 r
= btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit
);
871 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
873 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust quota limit: %m");
875 return sd_bus_reply_method_return(message
, NULL
);
878 static int method_set_image_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
879 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_set_limit
);
882 static int method_map_from_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
883 Manager
*m
= userdata
;
890 r
= sd_bus_message_read(message
, "su", &name
, &uid
);
894 if (!uid_is_valid(uid
))
895 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
897 machine
= hashmap_get(m
->machines
, name
);
899 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
901 if (machine
->class != MACHINE_CONTAINER
)
902 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
904 r
= machine_translate_uid(machine
, uid
, &converted
);
906 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching user mappings.", name
);
910 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
913 static int method_map_to_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
914 _cleanup_free_
char *o
= NULL
;
915 Manager
*m
= userdata
;
917 uid_t uid
, converted
;
920 r
= sd_bus_message_read(message
, "u", &uid
);
923 if (!uid_is_valid(uid
))
924 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
926 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "User " UID_FMT
" belongs to host UID range", uid
);
928 r
= manager_find_machine_for_uid(m
, uid
, &machine
, &converted
);
932 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "No matching user mapping for " UID_FMT
".", uid
);
934 o
= machine_bus_path(machine
);
938 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
941 static int method_map_from_machine_group(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
942 Manager
*m
= userdata
;
949 r
= sd_bus_message_read(message
, "su", &name
, &gid
);
953 if (!gid_is_valid(gid
))
954 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
956 machine
= hashmap_get(m
->machines
, name
);
958 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
960 if (machine
->class != MACHINE_CONTAINER
)
961 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
963 r
= machine_translate_gid(machine
, gid
, &converted
);
965 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching group mappings.", name
);
969 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
972 static int method_map_to_machine_group(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
973 _cleanup_free_
char *o
= NULL
;
974 Manager
*m
= userdata
;
976 gid_t gid
, converted
;
979 r
= sd_bus_message_read(message
, "u", &gid
);
982 if (!gid_is_valid(gid
))
983 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
985 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Group " GID_FMT
" belongs to host GID range", gid
);
987 r
= manager_find_machine_for_gid(m
, gid
, &machine
, &converted
);
991 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "No matching group mapping for " GID_FMT
".", gid
);
993 o
= machine_bus_path(machine
);
997 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1000 const sd_bus_vtable manager_vtable
[] = {
1001 SD_BUS_VTABLE_START(0),
1003 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path
, 0, 0),
1004 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage
, 0, 0),
1005 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit
, 0, 0),
1007 SD_BUS_METHOD_WITH_NAMES("GetMachine",
1011 SD_BUS_PARAM(machine
),
1013 SD_BUS_VTABLE_UNPRIVILEGED
),
1014 SD_BUS_METHOD_WITH_NAMES("GetImage",
1018 SD_BUS_PARAM(image
),
1020 SD_BUS_VTABLE_UNPRIVILEGED
),
1021 SD_BUS_METHOD_WITH_NAMES("GetMachineByPID",
1025 SD_BUS_PARAM(machine
),
1026 method_get_machine_by_pid
,
1027 SD_BUS_VTABLE_UNPRIVILEGED
),
1028 SD_BUS_METHOD_WITH_NAMES("ListMachines",
1031 SD_BUS_PARAM(machines
),
1032 method_list_machines
,
1033 SD_BUS_VTABLE_UNPRIVILEGED
),
1034 SD_BUS_METHOD_WITH_NAMES("ListImages",
1037 SD_BUS_PARAM(images
),
1039 SD_BUS_VTABLE_UNPRIVILEGED
),
1040 SD_BUS_METHOD_WITH_NAMES("CreateMachine",
1044 SD_BUS_PARAM(service
)
1046 SD_BUS_PARAM(leader
)
1047 SD_BUS_PARAM(root_directory
)
1048 SD_BUS_PARAM(scope_properties
),
1051 method_create_machine
, 0),
1052 SD_BUS_METHOD_WITH_NAMES("CreateMachineWithNetwork",
1056 SD_BUS_PARAM(service
)
1058 SD_BUS_PARAM(leader
)
1059 SD_BUS_PARAM(root_directory
)
1060 SD_BUS_PARAM(ifindices
)
1061 SD_BUS_PARAM(scope_properties
),
1064 method_create_machine_with_network
, 0),
1065 SD_BUS_METHOD_WITH_NAMES("RegisterMachine",
1069 SD_BUS_PARAM(service
)
1071 SD_BUS_PARAM(leader
)
1072 SD_BUS_PARAM(root_directory
),
1075 method_register_machine
, 0),
1076 SD_BUS_METHOD_WITH_NAMES("RegisterMachineWithNetwork",
1080 SD_BUS_PARAM(service
)
1082 SD_BUS_PARAM(leader
)
1083 SD_BUS_PARAM(root_directory
)
1084 SD_BUS_PARAM(ifindices
),
1087 method_register_machine_with_network
, 0),
1088 SD_BUS_METHOD_WITH_NAMES("UnregisterMachine",
1092 method_unregister_machine
,
1093 SD_BUS_VTABLE_UNPRIVILEGED
),
1094 SD_BUS_METHOD_WITH_NAMES("TerminateMachine",
1098 method_terminate_machine
,
1099 SD_BUS_VTABLE_UNPRIVILEGED
),
1100 SD_BUS_METHOD_WITH_NAMES("KillMachine",
1104 SD_BUS_PARAM(signal
),
1106 method_kill_machine
,
1107 SD_BUS_VTABLE_UNPRIVILEGED
),
1108 SD_BUS_METHOD_WITH_NAMES("GetMachineAddresses",
1112 SD_BUS_PARAM(addresses
),
1113 method_get_machine_addresses
,
1114 SD_BUS_VTABLE_UNPRIVILEGED
),
1115 SD_BUS_METHOD_WITH_NAMES("GetMachineOSRelease",
1119 SD_BUS_PARAM(fields
),
1120 method_get_machine_os_release
,
1121 SD_BUS_VTABLE_UNPRIVILEGED
),
1122 SD_BUS_METHOD_WITH_NAMES("OpenMachinePTY",
1127 SD_BUS_PARAM(pty_path
),
1128 method_open_machine_pty
,
1130 SD_BUS_METHOD_WITH_NAMES("OpenMachineLogin",
1135 SD_BUS_PARAM(pty_path
),
1136 method_open_machine_login
,
1137 SD_BUS_VTABLE_UNPRIVILEGED
),
1138 SD_BUS_METHOD_WITH_NAMES("OpenMachineShell",
1144 SD_BUS_PARAM(environment
),
1147 SD_BUS_PARAM(pty_path
),
1148 method_open_machine_shell
,
1149 SD_BUS_VTABLE_UNPRIVILEGED
),
1150 SD_BUS_METHOD_WITH_NAMES("BindMountMachine",
1153 SD_BUS_PARAM(source
)
1154 SD_BUS_PARAM(destination
)
1155 SD_BUS_PARAM(read_only
)
1156 SD_BUS_PARAM(mkdir
),
1158 method_bind_mount_machine
,
1159 SD_BUS_VTABLE_UNPRIVILEGED
),
1160 SD_BUS_METHOD_WITH_NAMES("CopyFromMachine",
1163 SD_BUS_PARAM(source
)
1164 SD_BUS_PARAM(destination
),
1166 method_copy_machine
,
1167 SD_BUS_VTABLE_UNPRIVILEGED
),
1168 SD_BUS_METHOD_WITH_NAMES("CopyToMachine",
1171 SD_BUS_PARAM(source
)
1172 SD_BUS_PARAM(destination
),
1174 method_copy_machine
,
1175 SD_BUS_VTABLE_UNPRIVILEGED
),
1176 SD_BUS_METHOD_WITH_NAMES("OpenMachineRootDirectory",
1181 method_open_machine_root_directory
,
1182 SD_BUS_VTABLE_UNPRIVILEGED
),
1183 SD_BUS_METHOD_WITH_NAMES("GetMachineUIDShift",
1187 SD_BUS_PARAM(shift
),
1188 method_get_machine_uid_shift
,
1189 SD_BUS_VTABLE_UNPRIVILEGED
),
1190 SD_BUS_METHOD_WITH_NAMES("RemoveImage",
1194 method_remove_image
,
1195 SD_BUS_VTABLE_UNPRIVILEGED
),
1196 SD_BUS_METHOD_WITH_NAMES("RenameImage",
1199 SD_BUS_PARAM(new_name
),
1201 method_rename_image
,
1202 SD_BUS_VTABLE_UNPRIVILEGED
),
1203 SD_BUS_METHOD_WITH_NAMES("CloneImage",
1206 SD_BUS_PARAM(new_name
)
1207 SD_BUS_PARAM(read_only
),
1210 SD_BUS_VTABLE_UNPRIVILEGED
),
1211 SD_BUS_METHOD_WITH_NAMES("MarkImageReadOnly",
1214 SD_BUS_PARAM(read_only
),
1216 method_mark_image_read_only
,
1217 SD_BUS_VTABLE_UNPRIVILEGED
),
1218 SD_BUS_METHOD_WITH_NAMES("GetImageHostname",
1222 SD_BUS_PARAM(hostname
),
1223 method_get_image_hostname
,
1224 SD_BUS_VTABLE_UNPRIVILEGED
),
1225 SD_BUS_METHOD_WITH_NAMES("GetImageMachineID",
1230 method_get_image_machine_id
,
1231 SD_BUS_VTABLE_UNPRIVILEGED
),
1232 SD_BUS_METHOD_WITH_NAMES("GetImageMachineInfo",
1236 SD_BUS_PARAM(machine_info
),
1237 method_get_image_machine_info
,
1238 SD_BUS_VTABLE_UNPRIVILEGED
),
1239 SD_BUS_METHOD_WITH_NAMES("GetImageOSRelease",
1243 SD_BUS_PARAM(os_release
),
1244 method_get_image_os_release
,
1245 SD_BUS_VTABLE_UNPRIVILEGED
),
1246 SD_BUS_METHOD_WITH_NAMES("SetPoolLimit",
1250 method_set_pool_limit
,
1251 SD_BUS_VTABLE_UNPRIVILEGED
),
1252 SD_BUS_METHOD_WITH_NAMES("SetImageLimit",
1257 method_set_image_limit
,
1258 SD_BUS_VTABLE_UNPRIVILEGED
),
1259 SD_BUS_METHOD_WITH_NAMES("CleanPool",
1263 SD_BUS_PARAM(images
),
1265 SD_BUS_VTABLE_UNPRIVILEGED
),
1266 SD_BUS_METHOD_WITH_NAMES("MapFromMachineUser",
1269 SD_BUS_PARAM(uid_inner
),
1271 SD_BUS_PARAM(uid_outer
),
1272 method_map_from_machine_user
,
1273 SD_BUS_VTABLE_UNPRIVILEGED
),
1274 SD_BUS_METHOD_WITH_NAMES("MapToMachineUser",
1276 SD_BUS_PARAM(uid_outer
),
1278 SD_BUS_PARAM(machine_name
)
1279 SD_BUS_PARAM(machine_path
)
1280 SD_BUS_PARAM(uid_inner
),
1281 method_map_to_machine_user
,
1282 SD_BUS_VTABLE_UNPRIVILEGED
),
1283 SD_BUS_METHOD_WITH_NAMES("MapFromMachineGroup",
1286 SD_BUS_PARAM(gid_inner
),
1288 SD_BUS_PARAM(gid_outer
),
1289 method_map_from_machine_group
,
1290 SD_BUS_VTABLE_UNPRIVILEGED
),
1291 SD_BUS_METHOD_WITH_NAMES("MapToMachineGroup",
1293 SD_BUS_PARAM(gid_outer
),
1295 SD_BUS_PARAM(machine_name
)
1296 SD_BUS_PARAM(machine_path
)
1297 SD_BUS_PARAM(gid_inner
),
1298 method_map_to_machine_group
,
1299 SD_BUS_VTABLE_UNPRIVILEGED
),
1301 SD_BUS_SIGNAL_WITH_NAMES("MachineNew",
1303 SD_BUS_PARAM(machine
)
1306 SD_BUS_SIGNAL_WITH_NAMES("MachineRemoved",
1308 SD_BUS_PARAM(machine
)
1315 const BusObjectImplementation manager_object
= {
1316 "/org/freedesktop/machine1",
1317 "org.freedesktop.machine1.Manager",
1318 .vtables
= BUS_VTABLES(manager_vtable
),
1319 .children
= BUS_IMPLEMENTATIONS( &machine_object
,
1323 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1324 const char *path
, *result
, *unit
;
1325 Manager
*m
= userdata
;
1333 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
1335 bus_log_parse_error(r
);
1339 machine
= hashmap_get(m
->machine_units
, unit
);
1343 if (streq_ptr(path
, machine
->scope_job
)) {
1344 machine
->scope_job
= mfree(machine
->scope_job
);
1346 if (machine
->started
) {
1347 if (streq(result
, "done"))
1348 machine_send_create_reply(machine
, NULL
);
1350 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
1352 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit %s failed with '%s'", unit
, result
);
1354 machine_send_create_reply(machine
, &e
);
1358 machine_save(machine
);
1361 machine_add_to_gc_queue(machine
);
1365 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1366 _cleanup_free_
char *unit
= NULL
;
1368 Manager
*m
= userdata
;
1375 path
= sd_bus_message_get_path(message
);
1379 r
= unit_name_from_dbus_path(path
, &unit
);
1380 if (r
== -EINVAL
) /* not for a unit */
1387 machine
= hashmap_get(m
->machine_units
, unit
);
1391 machine_add_to_gc_queue(machine
);
1395 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1396 const char *path
, *unit
;
1397 Manager
*m
= userdata
;
1404 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
1406 bus_log_parse_error(r
);
1410 machine
= hashmap_get(m
->machine_units
, unit
);
1414 machine_add_to_gc_queue(machine
);
1418 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1419 Manager
*m
= userdata
;
1426 r
= sd_bus_message_read(message
, "b", &b
);
1428 bus_log_parse_error(r
);
1434 /* systemd finished reloading, let's recheck all our machines */
1435 log_debug("System manager has been reloaded, rechecking machines...");
1437 HASHMAP_FOREACH(machine
, m
->machines
)
1438 machine_add_to_gc_queue(machine
);
1443 int manager_unref_unit(
1446 sd_bus_error
*error
) {
1451 return bus_call_method(m
->bus
, bus_systemd_mgr
, "UnrefUnit", error
, NULL
, "s", unit
);
1454 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
1455 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1461 r
= bus_call_method(manager
->bus
, bus_systemd_mgr
, "StopUnit", error
, &reply
, "ss", unit
, "fail");
1463 if (sd_bus_error_has_names(error
, BUS_ERROR_NO_SUCH_UNIT
,
1464 BUS_ERROR_LOAD_FAILED
)) {
1469 sd_bus_error_free(error
);
1480 r
= sd_bus_message_read(reply
, "o", &j
);
1494 int manager_kill_unit(Manager
*manager
, const char *unit
, int signo
, sd_bus_error
*error
) {
1498 return bus_call_method(manager
->bus
, bus_systemd_mgr
, "KillUnit", error
, NULL
, "ssi", unit
, "all", signo
);
1501 int manager_unit_is_active(Manager
*manager
, const char *unit
) {
1502 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1503 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1504 _cleanup_free_
char *path
= NULL
;
1511 path
= unit_dbus_path_from_name(unit
);
1515 r
= sd_bus_get_property(
1517 "org.freedesktop.systemd1",
1519 "org.freedesktop.systemd1.Unit",
1525 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
1526 SD_BUS_ERROR_DISCONNECTED
))
1529 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
1530 BUS_ERROR_LOAD_FAILED
))
1536 r
= sd_bus_message_read(reply
, "s", &state
);
1540 return !STR_IN_SET(state
, "inactive", "failed");
1543 int manager_job_is_active(Manager
*manager
, const char *path
) {
1544 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1545 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1551 r
= sd_bus_get_property(
1553 "org.freedesktop.systemd1",
1555 "org.freedesktop.systemd1.Job",
1561 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
1562 SD_BUS_ERROR_DISCONNECTED
))
1565 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
1571 /* We don't actually care about the state really. The fact
1572 * that we could read the job state is enough for us */
1577 int manager_get_machine_by_pid(Manager
*m
, pid_t pid
, Machine
**machine
) {
1585 mm
= hashmap_get(m
->machine_leaders
, PID_TO_PTR(pid
));
1587 _cleanup_free_
char *unit
= NULL
;
1589 r
= cg_pid_get_unit(pid
, &unit
);
1591 mm
= hashmap_get(m
->machine_units
, unit
);
1600 int manager_add_machine(Manager
*m
, const char *name
, Machine
**_machine
) {
1606 machine
= hashmap_get(m
->machines
, name
);
1608 machine
= machine_new(m
, _MACHINE_CLASS_INVALID
, name
);
1614 *_machine
= machine
;