1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 "discover-image.h"
16 #include "errno-util.h"
19 #include "format-util.h"
20 #include "hostname-util.h"
21 #include "image-dbus.h"
23 #include "machine-dbus.h"
24 #include "machine-pool.h"
26 #include "missing_capability.h"
28 #include "path-util.h"
29 #include "process-util.h"
30 #include "stdio-util.h"
32 #include "tmpfile-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
= -EBADF
;
48 uint64_t usage
= UINT64_MAX
;
53 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
57 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
61 return sd_bus_message_append(reply
, "t", usage
);
64 static int property_get_pool_limit(
67 const char *interface
,
69 sd_bus_message
*reply
,
71 sd_bus_error
*error
) {
73 _cleanup_close_
int fd
= -EBADF
;
74 uint64_t size
= UINT64_MAX
;
79 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
83 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
84 size
= q
.referenced_max
;
87 return sd_bus_message_append(reply
, "t", size
);
90 static int method_get_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
91 _cleanup_free_
char *p
= NULL
;
92 Manager
*m
= ASSERT_PTR(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
= ASSERT_PTR(userdata
);
122 r
= sd_bus_message_read(message
, "s", &name
);
126 r
= image_find(IMAGE_MACHINE
, name
, NULL
, NULL
);
128 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
132 p
= image_bus_path(name
);
136 return sd_bus_reply_method_return(message
, "o", p
);
139 static int method_get_machine_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
140 _cleanup_free_
char *p
= NULL
;
141 Manager
*m
= ASSERT_PTR(userdata
);
142 Machine
*machine
= NULL
;
148 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
150 r
= sd_bus_message_read(message
, "u", &pid
);
158 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
160 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
164 r
= sd_bus_creds_get_pid(creds
, &pid
);
169 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
173 return sd_bus_error_setf(error
, BUS_ERROR_NO_MACHINE_FOR_PID
, "PID "PID_FMT
" does not belong to any known machine", pid
);
175 p
= machine_bus_path(machine
);
179 return sd_bus_reply_method_return(message
, "o", p
);
182 static int method_list_machines(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
183 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
184 Manager
*m
= ASSERT_PTR(userdata
);
190 r
= sd_bus_message_new_method_return(message
, &reply
);
192 return sd_bus_error_set_errno(error
, r
);
194 r
= sd_bus_message_open_container(reply
, 'a', "(ssso)");
196 return sd_bus_error_set_errno(error
, r
);
198 HASHMAP_FOREACH(machine
, m
->machines
) {
199 _cleanup_free_
char *p
= NULL
;
201 p
= machine_bus_path(machine
);
205 r
= sd_bus_message_append(reply
, "(ssso)",
207 strempty(machine_class_to_string(machine
->class)),
211 return sd_bus_error_set_errno(error
, r
);
214 r
= sd_bus_message_close_container(reply
);
216 return sd_bus_error_set_errno(error
, r
);
218 return sd_bus_send(NULL
, reply
, NULL
);
221 static int method_create_or_register_machine(Manager
*manager
, sd_bus_message
*message
, bool read_network
, Machine
**_m
, sd_bus_error
*error
) {
222 const char *name
, *service
, *class, *root_directory
;
223 const int32_t *netif
= NULL
;
229 size_t n
, n_netif
= 0;
236 r
= sd_bus_message_read(message
, "s", &name
);
239 if (!hostname_is_valid(name
, 0))
240 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine name");
242 r
= sd_bus_message_read_array(message
, 'y', &v
, &n
);
250 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine ID parameter");
252 r
= sd_bus_message_read(message
, "ssus", &service
, &class, &leader
, &root_directory
);
257 r
= sd_bus_message_read_array(message
, 'i', (const void**) &netif
, &n_netif
);
261 n_netif
/= sizeof(int32_t);
263 for (size_t i
= 0; i
< n_netif
; i
++) {
265 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid network interface index %i", netif
[i
]);
270 c
= _MACHINE_CLASS_INVALID
;
272 c
= machine_class_from_string(class);
274 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine class parameter");
278 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
280 if (!isempty(root_directory
) && !path_is_absolute(root_directory
))
281 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Root directory must be empty or an absolute path");
284 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
286 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
290 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
292 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
297 if (hashmap_get(manager
->machines
, name
))
298 return sd_bus_error_setf(error
, BUS_ERROR_MACHINE_EXISTS
, "Machine '%s' already exists", name
);
300 r
= manager_add_machine(manager
, name
, &m
);
308 if (!isempty(service
)) {
309 m
->service
= strdup(service
);
316 if (!isempty(root_directory
)) {
317 m
->root_directory
= strdup(root_directory
);
318 if (!m
->root_directory
) {
325 assert_cc(sizeof(int32_t) == sizeof(int));
326 m
->netif
= memdup(netif
, sizeof(int32_t) * n_netif
);
332 m
->n_netif
= n_netif
;
340 machine_add_to_gc_queue(m
);
344 static int method_create_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
345 Manager
*manager
= ASSERT_PTR(userdata
);
351 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
355 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
359 r
= machine_start(m
, message
, error
);
363 m
->create_message
= sd_bus_message_ref(message
);
367 machine_add_to_gc_queue(m
);
371 static int method_create_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
372 return method_create_machine_internal(message
, true, userdata
, error
);
375 static int method_create_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
376 return method_create_machine_internal(message
, false, userdata
, error
);
379 static int method_register_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
380 Manager
*manager
= ASSERT_PTR(userdata
);
381 _cleanup_free_
char *p
= NULL
;
387 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
391 r
= cg_pid_get_unit(m
->leader
, &m
->unit
);
393 r
= sd_bus_error_set_errnof(error
, r
,
394 "Failed to determine unit of process "PID_FMT
" : %m",
399 r
= machine_start(m
, NULL
, error
);
403 p
= machine_bus_path(m
);
409 return sd_bus_reply_method_return(message
, "o", p
);
412 machine_add_to_gc_queue(m
);
416 static int method_register_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
417 return method_register_machine_internal(message
, true, userdata
, error
);
420 static int method_register_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
421 return method_register_machine_internal(message
, false, userdata
, error
);
424 static int redirect_method_to_machine(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
433 r
= sd_bus_message_read(message
, "s", &name
);
435 return sd_bus_error_set_errno(error
, r
);
437 machine
= hashmap_get(m
->machines
, name
);
439 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
441 return method(message
, machine
, error
);
444 static int method_unregister_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
445 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_unregister
);
448 static int method_terminate_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
449 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_terminate
);
452 static int method_kill_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
453 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_kill
);
456 static int method_get_machine_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
457 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_addresses
);
460 static int method_get_machine_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
461 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_os_release
);
464 static int method_list_images(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
465 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
466 _cleanup_hashmap_free_ Hashmap
*images
= NULL
;
467 _unused_ Manager
*m
= ASSERT_PTR(userdata
);
473 images
= hashmap_new(&image_hash_ops
);
477 r
= image_discover(IMAGE_MACHINE
, NULL
, images
);
481 r
= sd_bus_message_new_method_return(message
, &reply
);
485 r
= sd_bus_message_open_container(reply
, 'a', "(ssbttto)");
489 HASHMAP_FOREACH(image
, images
) {
490 _cleanup_free_
char *p
= NULL
;
492 p
= image_bus_path(image
->name
);
496 r
= sd_bus_message_append(reply
, "(ssbttto)",
498 image_type_to_string(image
->type
),
508 r
= sd_bus_message_close_container(reply
);
512 return sd_bus_send(NULL
, reply
, NULL
);
515 static int method_open_machine_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
516 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_pty
);
519 static int method_open_machine_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
520 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_login
);
523 static int method_open_machine_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
524 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_shell
);
527 static int method_bind_mount_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
528 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_bind_mount
);
531 static int method_copy_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
532 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_copy
);
535 static int method_open_machine_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
536 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_root_directory
);
539 static int method_get_machine_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
540 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_uid_shift
);
543 static int redirect_method_to_image(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
544 _cleanup_(image_unrefp
) Image
* i
= NULL
;
552 r
= sd_bus_message_read(message
, "s", &name
);
556 if (!image_name_is_valid(name
))
557 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
559 r
= image_find(IMAGE_MACHINE
, name
, NULL
, &i
);
561 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
566 return method(message
, i
, error
);
569 static int method_remove_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
570 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_remove
);
573 static int method_rename_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
574 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_rename
);
577 static int method_clone_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
578 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_clone
);
581 static int method_mark_image_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
582 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_mark_read_only
);
585 static int method_get_image_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
586 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_hostname
);
589 static int method_get_image_machine_id(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
590 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_id
);
593 static int method_get_image_machine_info(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
594 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_info
);
597 static int method_get_image_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
598 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_os_release
);
601 static int clean_pool_done(Operation
*operation
, int ret
, sd_bus_error
*error
) {
602 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
603 _cleanup_fclose_
FILE *f
= NULL
;
609 assert(operation
->extra_fd
>= 0);
611 if (lseek(operation
->extra_fd
, 0, SEEK_SET
) == (off_t
) -1)
614 f
= take_fdopen(&operation
->extra_fd
, "r");
618 /* The resulting temporary file starts with a boolean value that indicates success or not. */
620 n
= fread(&success
, 1, sizeof(success
), f
);
621 if (n
!= sizeof(success
))
622 return ret
< 0 ? ret
: errno_or_else(EIO
);
625 _cleanup_free_
char *name
= NULL
;
627 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
628 * set to false followed by the name of the failed image. Let's try to read this and use it for the
629 * error message. If we can't read it, don't mind, and return the naked error. */
631 if (success
) /* The resulting temporary file could not be updated, ignore it. */
634 r
= read_nul_string(f
, LONG_LINE_MAX
, &name
);
635 if (r
<= 0) /* Same here... */
638 return sd_bus_error_set_errnof(error
, ret
, "Failed to remove image %s: %m", name
);
643 r
= sd_bus_message_new_method_return(operation
->message
, &reply
);
647 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
651 /* On success the resulting temporary file will contain a list of image names that were removed followed by
652 * their size on disk. Let's read that and turn it into a bus message. */
654 _cleanup_free_
char *name
= NULL
;
657 r
= read_nul_string(f
, LONG_LINE_MAX
, &name
);
660 if (r
== 0) /* reached the end */
664 n
= fread(&size
, 1, sizeof(size
), f
);
665 if (n
!= sizeof(size
))
666 return errno_or_else(EIO
);
668 r
= sd_bus_message_append(reply
, "(st)", name
, size
);
673 r
= sd_bus_message_close_container(reply
);
677 return sd_bus_send(NULL
, reply
, NULL
);
680 static int method_clean_pool(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
686 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
687 _cleanup_close_
int result_fd
= -EBADF
;
688 Manager
*m
= userdata
;
689 Operation
*operation
;
696 if (m
->n_operations
>= OPERATIONS_MAX
)
697 return sd_bus_error_set(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing operations.");
699 r
= sd_bus_message_read(message
, "s", &mm
);
703 if (streq(mm
, "all"))
705 else if (streq(mm
, "hidden"))
706 mode
= REMOVE_HIDDEN
;
708 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown mode '%s'.", mm
);
710 const char *details
[] = {
711 "verb", "clean_pool",
716 r
= bus_verify_polkit_async(
719 "org.freedesktop.machine1.manage-machines",
728 return 1; /* Will call us back */
730 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
731 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
733 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
734 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
736 result_fd
= open_tmpfile_unlinkable(NULL
, O_RDWR
|O_CLOEXEC
);
740 /* This might be a slow operation, run it asynchronously in a background process */
741 r
= safe_fork("(sd-clean)", FORK_RESET_SIGNALS
, &child
);
743 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
745 _cleanup_hashmap_free_ Hashmap
*images
= NULL
;
750 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
752 images
= hashmap_new(&image_hash_ops
);
758 r
= image_discover(IMAGE_MACHINE
, NULL
, images
);
762 l
= write(result_fd
, &success
, sizeof(success
));
768 HASHMAP_FOREACH(image
, images
) {
770 /* We can't remove vendor images (i.e. those in /usr) */
771 if (IMAGE_IS_VENDOR(image
))
774 if (IMAGE_IS_HOST(image
))
777 if (mode
== REMOVE_HIDDEN
&& !IMAGE_IS_HIDDEN(image
))
780 r
= image_remove(image
);
781 if (r
== -EBUSY
) /* keep images that are currently being used. */
784 /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */
786 (void) ftruncate(result_fd
, 0);
787 (void) lseek(result_fd
, 0, SEEK_SET
);
788 (void) write(result_fd
, &success
, sizeof(success
));
789 (void) write(result_fd
, image
->name
, strlen(image
->name
)+1);
793 l
= write(result_fd
, image
->name
, strlen(image
->name
)+1);
799 l
= write(result_fd
, &image
->usage_exclusive
, sizeof(image
->usage_exclusive
));
806 result_fd
= safe_close(result_fd
);
810 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
814 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
816 /* The clean-up might take a while, hence install a watch on the child and return */
818 r
= operation_new(m
, NULL
, child
, message
, errno_pipe_fd
[0], &operation
);
820 (void) sigkill_wait(child
);
824 operation
->extra_fd
= result_fd
;
825 operation
->done
= clean_pool_done
;
828 errno_pipe_fd
[0] = -EBADF
;
833 static int method_set_pool_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
834 Manager
*m
= userdata
;
840 r
= sd_bus_message_read(message
, "t", &limit
);
843 if (!FILE_SIZE_VALID_OR_INFINITY(limit
))
844 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "New limit out of range");
846 const char *details
[] = {
847 "verb", "set_pool_limit",
851 r
= bus_verify_polkit_async(
854 "org.freedesktop.machine1.manage-machines",
863 return 1; /* Will call us back */
865 /* Set up the machine directory if necessary */
866 r
= setup_machine_directory(error
, /* use_btrfs_subvol= */ true, /* use_btrfs_quota= */ true);
870 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit
);
872 r
= btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit
);
874 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
876 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust quota limit: %m");
878 return sd_bus_reply_method_return(message
, NULL
);
881 static int method_set_image_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
882 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_set_limit
);
885 static int method_map_from_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
886 Manager
*m
= userdata
;
893 r
= sd_bus_message_read(message
, "su", &name
, &uid
);
897 if (!uid_is_valid(uid
))
898 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
900 machine
= hashmap_get(m
->machines
, name
);
902 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
904 if (machine
->class != MACHINE_CONTAINER
)
905 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
907 r
= machine_translate_uid(machine
, uid
, &converted
);
909 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching user mappings.", name
);
913 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
916 static int method_map_to_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
917 _cleanup_free_
char *o
= NULL
;
918 Manager
*m
= userdata
;
920 uid_t uid
, converted
;
923 r
= sd_bus_message_read(message
, "u", &uid
);
926 if (!uid_is_valid(uid
))
927 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
929 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "User " UID_FMT
" belongs to host UID range", uid
);
931 r
= manager_find_machine_for_uid(m
, uid
, &machine
, &converted
);
935 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "No matching user mapping for " UID_FMT
".", uid
);
937 o
= machine_bus_path(machine
);
941 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
944 static int method_map_from_machine_group(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
945 Manager
*m
= userdata
;
952 r
= sd_bus_message_read(message
, "su", &name
, &gid
);
956 if (!gid_is_valid(gid
))
957 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
959 machine
= hashmap_get(m
->machines
, name
);
961 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
963 if (machine
->class != MACHINE_CONTAINER
)
964 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
966 r
= machine_translate_gid(machine
, gid
, &converted
);
968 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching group mappings.", name
);
972 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
975 static int method_map_to_machine_group(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
976 _cleanup_free_
char *o
= NULL
;
977 Manager
*m
= userdata
;
979 gid_t gid
, converted
;
982 r
= sd_bus_message_read(message
, "u", &gid
);
985 if (!gid_is_valid(gid
))
986 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
988 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Group " GID_FMT
" belongs to host GID range", gid
);
990 r
= manager_find_machine_for_gid(m
, gid
, &machine
, &converted
);
994 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "No matching group mapping for " GID_FMT
".", gid
);
996 o
= machine_bus_path(machine
);
1000 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1003 const sd_bus_vtable manager_vtable
[] = {
1004 SD_BUS_VTABLE_START(0),
1006 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path
, 0, 0),
1007 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage
, 0, 0),
1008 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit
, 0, 0),
1010 SD_BUS_METHOD_WITH_ARGS("GetMachine",
1011 SD_BUS_ARGS("s", name
),
1012 SD_BUS_RESULT("o", machine
),
1014 SD_BUS_VTABLE_UNPRIVILEGED
),
1015 SD_BUS_METHOD_WITH_ARGS("GetImage",
1016 SD_BUS_ARGS("s", name
),
1017 SD_BUS_RESULT("o", image
),
1019 SD_BUS_VTABLE_UNPRIVILEGED
),
1020 SD_BUS_METHOD_WITH_ARGS("GetMachineByPID",
1021 SD_BUS_ARGS("u", pid
),
1022 SD_BUS_RESULT("o", machine
),
1023 method_get_machine_by_pid
,
1024 SD_BUS_VTABLE_UNPRIVILEGED
),
1025 SD_BUS_METHOD_WITH_ARGS("ListMachines",
1027 SD_BUS_RESULT("a(ssso)", machines
),
1028 method_list_machines
,
1029 SD_BUS_VTABLE_UNPRIVILEGED
),
1030 SD_BUS_METHOD_WITH_ARGS("ListImages",
1032 SD_BUS_RESULT("a(ssbttto)", images
),
1034 SD_BUS_VTABLE_UNPRIVILEGED
),
1035 SD_BUS_METHOD_WITH_ARGS("CreateMachine",
1036 SD_BUS_ARGS("s", name
, "ay", id
, "s", service
, "s", class, "u", leader
, "s", root_directory
, "a(sv)", scope_properties
),
1037 SD_BUS_RESULT("o", path
),
1038 method_create_machine
, 0),
1039 SD_BUS_METHOD_WITH_ARGS("CreateMachineWithNetwork",
1040 SD_BUS_ARGS("s", name
, "ay", id
, "s", service
, "s", class, "u", leader
, "s", root_directory
, "ai", ifindices
, "a(sv)", scope_properties
),
1041 SD_BUS_RESULT("o", path
),
1042 method_create_machine_with_network
, 0),
1043 SD_BUS_METHOD_WITH_ARGS("RegisterMachine",
1044 SD_BUS_ARGS("s", name
, "ay", id
, "s", service
, "s", class, "u", leader
, "s", root_directory
),
1045 SD_BUS_RESULT("o", path
),
1046 method_register_machine
, 0),
1047 SD_BUS_METHOD_WITH_ARGS("RegisterMachineWithNetwork",
1048 SD_BUS_ARGS("s", name
, "ay", id
, "s", service
, "s", class, "u", leader
, "s", root_directory
, "ai", ifindices
),
1049 SD_BUS_RESULT("o", path
),
1050 method_register_machine_with_network
, 0),
1051 SD_BUS_METHOD_WITH_ARGS("UnregisterMachine",
1052 SD_BUS_ARGS("s", name
),
1054 method_unregister_machine
,
1055 SD_BUS_VTABLE_UNPRIVILEGED
),
1056 SD_BUS_METHOD_WITH_ARGS("TerminateMachine",
1057 SD_BUS_ARGS("s", id
),
1059 method_terminate_machine
,
1060 SD_BUS_VTABLE_UNPRIVILEGED
),
1061 SD_BUS_METHOD_WITH_ARGS("KillMachine",
1062 SD_BUS_ARGS("s", name
, "s", who
, "i", signal
),
1064 method_kill_machine
,
1065 SD_BUS_VTABLE_UNPRIVILEGED
),
1066 SD_BUS_METHOD_WITH_ARGS("GetMachineAddresses",
1067 SD_BUS_ARGS("s", name
),
1068 SD_BUS_RESULT("a(iay)", addresses
),
1069 method_get_machine_addresses
,
1070 SD_BUS_VTABLE_UNPRIVILEGED
),
1071 SD_BUS_METHOD_WITH_ARGS("GetMachineOSRelease",
1072 SD_BUS_ARGS("s", name
),
1073 SD_BUS_RESULT("a{ss}", fields
),
1074 method_get_machine_os_release
,
1075 SD_BUS_VTABLE_UNPRIVILEGED
),
1076 SD_BUS_METHOD_WITH_ARGS("OpenMachinePTY",
1077 SD_BUS_ARGS("s", name
),
1078 SD_BUS_RESULT("h", pty
, "s", pty_path
),
1079 method_open_machine_pty
,
1081 SD_BUS_METHOD_WITH_ARGS("OpenMachineLogin",
1082 SD_BUS_ARGS("s", name
),
1083 SD_BUS_RESULT("h", pty
, "s", pty_path
),
1084 method_open_machine_login
,
1085 SD_BUS_VTABLE_UNPRIVILEGED
),
1086 SD_BUS_METHOD_WITH_ARGS("OpenMachineShell",
1087 SD_BUS_ARGS("s", name
, "s", user
, "s", path
, "as", args
, "as", environment
),
1088 SD_BUS_RESULT("h", pty
, "s", pty_path
),
1089 method_open_machine_shell
,
1090 SD_BUS_VTABLE_UNPRIVILEGED
),
1091 SD_BUS_METHOD_WITH_ARGS("BindMountMachine",
1092 SD_BUS_ARGS("s", name
, "s", source
, "s", destination
, "b", read_only
, "b", mkdir
),
1094 method_bind_mount_machine
,
1095 SD_BUS_VTABLE_UNPRIVILEGED
),
1096 SD_BUS_METHOD_WITH_ARGS("CopyFromMachine",
1097 SD_BUS_ARGS("s", name
, "s", source
, "s", destination
),
1099 method_copy_machine
,
1100 SD_BUS_VTABLE_UNPRIVILEGED
),
1101 SD_BUS_METHOD_WITH_ARGS("CopyToMachine",
1102 SD_BUS_ARGS("s", name
, "s", source
, "s", destination
),
1104 method_copy_machine
,
1105 SD_BUS_VTABLE_UNPRIVILEGED
),
1106 SD_BUS_METHOD_WITH_ARGS("CopyFromMachineWithFlags",
1107 SD_BUS_ARGS("s", name
, "s", source
, "s", destination
, "t", flags
),
1109 method_copy_machine
,
1110 SD_BUS_VTABLE_UNPRIVILEGED
),
1111 SD_BUS_METHOD_WITH_ARGS("CopyToMachineWithFlags",
1112 SD_BUS_ARGS("s", name
, "s", source
, "s", destination
, "t", flags
),
1114 method_copy_machine
,
1115 SD_BUS_VTABLE_UNPRIVILEGED
),
1116 SD_BUS_METHOD_WITH_ARGS("OpenMachineRootDirectory",
1117 SD_BUS_ARGS("s", name
),
1118 SD_BUS_RESULT("h", fd
),
1119 method_open_machine_root_directory
,
1120 SD_BUS_VTABLE_UNPRIVILEGED
),
1121 SD_BUS_METHOD_WITH_ARGS("GetMachineUIDShift",
1122 SD_BUS_ARGS("s", name
),
1123 SD_BUS_RESULT("u", shift
),
1124 method_get_machine_uid_shift
,
1125 SD_BUS_VTABLE_UNPRIVILEGED
),
1126 SD_BUS_METHOD_WITH_ARGS("RemoveImage",
1127 SD_BUS_ARGS("s", name
),
1129 method_remove_image
,
1130 SD_BUS_VTABLE_UNPRIVILEGED
),
1131 SD_BUS_METHOD_WITH_ARGS("RenameImage",
1132 SD_BUS_ARGS("s", name
, "s", new_name
),
1134 method_rename_image
,
1135 SD_BUS_VTABLE_UNPRIVILEGED
),
1136 SD_BUS_METHOD_WITH_ARGS("CloneImage",
1137 SD_BUS_ARGS("s", name
, "s", new_name
, "b", read_only
),
1140 SD_BUS_VTABLE_UNPRIVILEGED
),
1141 SD_BUS_METHOD_WITH_ARGS("MarkImageReadOnly",
1142 SD_BUS_ARGS("s", name
, "b", read_only
),
1144 method_mark_image_read_only
,
1145 SD_BUS_VTABLE_UNPRIVILEGED
),
1146 SD_BUS_METHOD_WITH_ARGS("GetImageHostname",
1147 SD_BUS_ARGS("s", name
),
1148 SD_BUS_RESULT("s", hostname
),
1149 method_get_image_hostname
,
1150 SD_BUS_VTABLE_UNPRIVILEGED
),
1151 SD_BUS_METHOD_WITH_ARGS("GetImageMachineID",
1152 SD_BUS_ARGS("s", name
),
1153 SD_BUS_RESULT("ay", id
),
1154 method_get_image_machine_id
,
1155 SD_BUS_VTABLE_UNPRIVILEGED
),
1156 SD_BUS_METHOD_WITH_ARGS("GetImageMachineInfo",
1157 SD_BUS_ARGS("s", name
),
1158 SD_BUS_RESULT("a{ss}", machine_info
),
1159 method_get_image_machine_info
,
1160 SD_BUS_VTABLE_UNPRIVILEGED
),
1161 SD_BUS_METHOD_WITH_ARGS("GetImageOSRelease",
1162 SD_BUS_ARGS("s", name
),
1163 SD_BUS_RESULT("a{ss}", os_release
),
1164 method_get_image_os_release
,
1165 SD_BUS_VTABLE_UNPRIVILEGED
),
1166 SD_BUS_METHOD_WITH_ARGS("SetPoolLimit",
1167 SD_BUS_ARGS("t", size
),
1169 method_set_pool_limit
,
1170 SD_BUS_VTABLE_UNPRIVILEGED
),
1171 SD_BUS_METHOD_WITH_ARGS("SetImageLimit",
1172 SD_BUS_ARGS("s", name
, "t", size
),
1174 method_set_image_limit
,
1175 SD_BUS_VTABLE_UNPRIVILEGED
),
1176 SD_BUS_METHOD_WITH_ARGS("CleanPool",
1177 SD_BUS_ARGS("s", mode
),
1178 SD_BUS_RESULT("a(st)",images
),
1180 SD_BUS_VTABLE_UNPRIVILEGED
),
1181 SD_BUS_METHOD_WITH_ARGS("MapFromMachineUser",
1182 SD_BUS_ARGS("s", name
, "u", uid_inner
),
1183 SD_BUS_RESULT("u", uid_outer
),
1184 method_map_from_machine_user
,
1185 SD_BUS_VTABLE_UNPRIVILEGED
),
1186 SD_BUS_METHOD_WITH_ARGS("MapToMachineUser",
1187 SD_BUS_ARGS("u", uid_outer
),
1188 SD_BUS_RESULT("s", machine_name
, "o", machine_path
, "u", uid_inner
),
1189 method_map_to_machine_user
,
1190 SD_BUS_VTABLE_UNPRIVILEGED
),
1191 SD_BUS_METHOD_WITH_ARGS("MapFromMachineGroup",
1192 SD_BUS_ARGS("s", name
, "u", gid_inner
),
1193 SD_BUS_RESULT("u", gid_outer
),
1194 method_map_from_machine_group
,
1195 SD_BUS_VTABLE_UNPRIVILEGED
),
1196 SD_BUS_METHOD_WITH_ARGS("MapToMachineGroup",
1197 SD_BUS_ARGS("u", gid_outer
),
1198 SD_BUS_RESULT("s", machine_name
, "o", machine_path
, "u", gid_inner
),
1199 method_map_to_machine_group
,
1200 SD_BUS_VTABLE_UNPRIVILEGED
),
1202 SD_BUS_SIGNAL_WITH_ARGS("MachineNew",
1203 SD_BUS_ARGS("s", machine
, "o", path
),
1205 SD_BUS_SIGNAL_WITH_ARGS("MachineRemoved",
1206 SD_BUS_ARGS("s", machine
, "o", path
),
1212 const BusObjectImplementation manager_object
= {
1213 "/org/freedesktop/machine1",
1214 "org.freedesktop.machine1.Manager",
1215 .vtables
= BUS_VTABLES(manager_vtable
),
1216 .children
= BUS_IMPLEMENTATIONS( &machine_object
,
1220 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1221 const char *path
, *result
, *unit
;
1222 Manager
*m
= ASSERT_PTR(userdata
);
1229 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
1231 bus_log_parse_error(r
);
1235 machine
= hashmap_get(m
->machine_units
, unit
);
1239 if (streq_ptr(path
, machine
->scope_job
)) {
1240 machine
->scope_job
= mfree(machine
->scope_job
);
1242 if (machine
->started
) {
1243 if (streq(result
, "done"))
1244 machine_send_create_reply(machine
, NULL
);
1246 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
1248 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit %s failed with '%s'", unit
, result
);
1250 machine_send_create_reply(machine
, &e
);
1254 machine_save(machine
);
1257 machine_add_to_gc_queue(machine
);
1261 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1262 _cleanup_free_
char *unit
= NULL
;
1264 Manager
*m
= ASSERT_PTR(userdata
);
1270 path
= sd_bus_message_get_path(message
);
1274 r
= unit_name_from_dbus_path(path
, &unit
);
1275 if (r
== -EINVAL
) /* not for a unit */
1282 machine
= hashmap_get(m
->machine_units
, unit
);
1286 machine_add_to_gc_queue(machine
);
1290 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1291 const char *path
, *unit
;
1292 Manager
*m
= ASSERT_PTR(userdata
);
1298 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
1300 bus_log_parse_error(r
);
1304 machine
= hashmap_get(m
->machine_units
, unit
);
1308 machine_add_to_gc_queue(machine
);
1312 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1313 Manager
*m
= ASSERT_PTR(userdata
);
1319 r
= sd_bus_message_read(message
, "b", &b
);
1321 bus_log_parse_error(r
);
1327 /* systemd finished reloading, let's recheck all our machines */
1328 log_debug("System manager has been reloaded, rechecking machines...");
1330 HASHMAP_FOREACH(machine
, m
->machines
)
1331 machine_add_to_gc_queue(machine
);
1336 int manager_unref_unit(
1339 sd_bus_error
*error
) {
1344 return bus_call_method(m
->bus
, bus_systemd_mgr
, "UnrefUnit", error
, NULL
, "s", unit
);
1347 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
1348 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1354 r
= bus_call_method(manager
->bus
, bus_systemd_mgr
, "StopUnit", error
, &reply
, "ss", unit
, "fail");
1356 if (sd_bus_error_has_names(error
, BUS_ERROR_NO_SUCH_UNIT
,
1357 BUS_ERROR_LOAD_FAILED
)) {
1362 sd_bus_error_free(error
);
1373 r
= sd_bus_message_read(reply
, "o", &j
);
1387 int manager_kill_unit(Manager
*manager
, const char *unit
, int signo
, sd_bus_error
*error
) {
1391 return bus_call_method(manager
->bus
, bus_systemd_mgr
, "KillUnit", error
, NULL
, "ssi", unit
, "all", signo
);
1394 int manager_unit_is_active(Manager
*manager
, const char *unit
) {
1395 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1396 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1397 _cleanup_free_
char *path
= NULL
;
1404 path
= unit_dbus_path_from_name(unit
);
1408 r
= sd_bus_get_property(
1410 "org.freedesktop.systemd1",
1412 "org.freedesktop.systemd1.Unit",
1418 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
1419 SD_BUS_ERROR_DISCONNECTED
))
1422 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
1423 BUS_ERROR_LOAD_FAILED
))
1429 r
= sd_bus_message_read(reply
, "s", &state
);
1433 return !STR_IN_SET(state
, "inactive", "failed");
1436 int manager_job_is_active(Manager
*manager
, const char *path
) {
1437 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1438 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1444 r
= sd_bus_get_property(
1446 "org.freedesktop.systemd1",
1448 "org.freedesktop.systemd1.Job",
1454 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
1455 SD_BUS_ERROR_DISCONNECTED
))
1458 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
1464 /* We don't actually care about the state really. The fact
1465 * that we could read the job state is enough for us */
1470 int manager_get_machine_by_pid(Manager
*m
, pid_t pid
, Machine
**machine
) {
1478 mm
= hashmap_get(m
->machine_leaders
, PID_TO_PTR(pid
));
1480 _cleanup_free_
char *unit
= NULL
;
1482 r
= cg_pid_get_unit(pid
, &unit
);
1484 mm
= hashmap_get(m
->machine_units
, unit
);
1493 int manager_add_machine(Manager
*m
, const char *name
, Machine
**_machine
) {
1500 machine
= hashmap_get(m
->machines
, name
);
1502 r
= machine_new(m
, _MACHINE_CLASS_INVALID
, name
, &machine
);
1508 *_machine
= machine
;