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
= -1;
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
= -1;
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
= userdata
;
100 r
= sd_bus_message_read(message
, "s", &name
);
104 machine
= hashmap_get(m
->machines
, name
);
106 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
108 p
= machine_bus_path(machine
);
112 return sd_bus_reply_method_return(message
, "o", p
);
115 static int method_get_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
116 _cleanup_free_
char *p
= NULL
;
117 _unused_ Manager
*m
= userdata
;
124 r
= sd_bus_message_read(message
, "s", &name
);
128 r
= image_find(IMAGE_MACHINE
, name
, NULL
, NULL
);
130 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
134 p
= image_bus_path(name
);
138 return sd_bus_reply_method_return(message
, "o", p
);
141 static int method_get_machine_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
142 _cleanup_free_
char *p
= NULL
;
143 Manager
*m
= userdata
;
144 Machine
*machine
= NULL
;
151 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
153 r
= sd_bus_message_read(message
, "u", &pid
);
161 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
163 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
167 r
= sd_bus_creds_get_pid(creds
, &pid
);
172 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
176 return sd_bus_error_setf(error
, BUS_ERROR_NO_MACHINE_FOR_PID
, "PID "PID_FMT
" does not belong to any known machine", pid
);
178 p
= machine_bus_path(machine
);
182 return sd_bus_reply_method_return(message
, "o", p
);
185 static int method_list_machines(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
186 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
187 Manager
*m
= userdata
;
194 r
= sd_bus_message_new_method_return(message
, &reply
);
196 return sd_bus_error_set_errno(error
, r
);
198 r
= sd_bus_message_open_container(reply
, 'a', "(ssso)");
200 return sd_bus_error_set_errno(error
, r
);
202 HASHMAP_FOREACH(machine
, m
->machines
) {
203 _cleanup_free_
char *p
= NULL
;
205 p
= machine_bus_path(machine
);
209 r
= sd_bus_message_append(reply
, "(ssso)",
211 strempty(machine_class_to_string(machine
->class)),
215 return sd_bus_error_set_errno(error
, r
);
218 r
= sd_bus_message_close_container(reply
);
220 return sd_bus_error_set_errno(error
, r
);
222 return sd_bus_send(NULL
, reply
, NULL
);
225 static int method_create_or_register_machine(Manager
*manager
, sd_bus_message
*message
, bool read_network
, Machine
**_m
, sd_bus_error
*error
) {
226 const char *name
, *service
, *class, *root_directory
;
227 const int32_t *netif
= NULL
;
233 size_t n
, n_netif
= 0;
240 r
= sd_bus_message_read(message
, "s", &name
);
243 if (!hostname_is_valid(name
, 0))
244 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine name");
246 r
= sd_bus_message_read_array(message
, 'y', &v
, &n
);
254 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine ID parameter");
256 r
= sd_bus_message_read(message
, "ssus", &service
, &class, &leader
, &root_directory
);
261 r
= sd_bus_message_read_array(message
, 'i', (const void**) &netif
, &n_netif
);
265 n_netif
/= sizeof(int32_t);
267 for (size_t i
= 0; i
< n_netif
; i
++) {
269 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid network interface index %i", netif
[i
]);
274 c
= _MACHINE_CLASS_INVALID
;
276 c
= machine_class_from_string(class);
278 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine class parameter");
282 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
284 if (!isempty(root_directory
) && !path_is_absolute(root_directory
))
285 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Root directory must be empty or an absolute path");
288 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
290 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
294 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
296 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
301 if (hashmap_get(manager
->machines
, name
))
302 return sd_bus_error_setf(error
, BUS_ERROR_MACHINE_EXISTS
, "Machine '%s' already exists", name
);
304 r
= manager_add_machine(manager
, name
, &m
);
312 if (!isempty(service
)) {
313 m
->service
= strdup(service
);
320 if (!isempty(root_directory
)) {
321 m
->root_directory
= strdup(root_directory
);
322 if (!m
->root_directory
) {
329 assert_cc(sizeof(int32_t) == sizeof(int));
330 m
->netif
= memdup(netif
, sizeof(int32_t) * n_netif
);
336 m
->n_netif
= n_netif
;
344 machine_add_to_gc_queue(m
);
348 static int method_create_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
349 Manager
*manager
= userdata
;
356 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
360 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
364 r
= machine_start(m
, message
, error
);
368 m
->create_message
= sd_bus_message_ref(message
);
372 machine_add_to_gc_queue(m
);
376 static int method_create_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
377 return method_create_machine_internal(message
, true, userdata
, error
);
380 static int method_create_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
381 return method_create_machine_internal(message
, false, userdata
, error
);
384 static int method_register_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
385 Manager
*manager
= userdata
;
386 _cleanup_free_
char *p
= NULL
;
393 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
397 r
= cg_pid_get_unit(m
->leader
, &m
->unit
);
399 r
= sd_bus_error_set_errnof(error
, r
,
400 "Failed to determine unit of process "PID_FMT
" : %m",
405 r
= machine_start(m
, NULL
, error
);
409 p
= machine_bus_path(m
);
415 return sd_bus_reply_method_return(message
, "o", p
);
418 machine_add_to_gc_queue(m
);
422 static int method_register_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
423 return method_register_machine_internal(message
, true, userdata
, error
);
426 static int method_register_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
427 return method_register_machine_internal(message
, false, userdata
, error
);
430 static int redirect_method_to_machine(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
439 r
= sd_bus_message_read(message
, "s", &name
);
441 return sd_bus_error_set_errno(error
, r
);
443 machine
= hashmap_get(m
->machines
, name
);
445 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
447 return method(message
, machine
, error
);
450 static int method_unregister_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
451 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_unregister
);
454 static int method_terminate_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
455 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_terminate
);
458 static int method_kill_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
459 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_kill
);
462 static int method_get_machine_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
463 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_addresses
);
466 static int method_get_machine_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
467 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_os_release
);
470 static int method_list_images(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
471 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
472 _cleanup_hashmap_free_ Hashmap
*images
= NULL
;
473 _unused_ Manager
*m
= userdata
;
480 images
= hashmap_new(&image_hash_ops
);
484 r
= image_discover(IMAGE_MACHINE
, NULL
, images
);
488 r
= sd_bus_message_new_method_return(message
, &reply
);
492 r
= sd_bus_message_open_container(reply
, 'a', "(ssbttto)");
496 HASHMAP_FOREACH(image
, images
) {
497 _cleanup_free_
char *p
= NULL
;
499 p
= image_bus_path(image
->name
);
503 r
= sd_bus_message_append(reply
, "(ssbttto)",
505 image_type_to_string(image
->type
),
515 r
= sd_bus_message_close_container(reply
);
519 return sd_bus_send(NULL
, reply
, NULL
);
522 static int method_open_machine_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
523 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_pty
);
526 static int method_open_machine_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
527 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_login
);
530 static int method_open_machine_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
531 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_shell
);
534 static int method_bind_mount_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
535 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_bind_mount
);
538 static int method_copy_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
539 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_copy
);
542 static int method_open_machine_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
543 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_root_directory
);
546 static int method_get_machine_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
547 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_uid_shift
);
550 static int redirect_method_to_image(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
551 _cleanup_(image_unrefp
) Image
* i
= NULL
;
559 r
= sd_bus_message_read(message
, "s", &name
);
563 if (!image_name_is_valid(name
))
564 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
566 r
= image_find(IMAGE_MACHINE
, name
, NULL
, &i
);
568 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
573 return method(message
, i
, error
);
576 static int method_remove_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
577 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_remove
);
580 static int method_rename_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
581 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_rename
);
584 static int method_clone_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
585 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_clone
);
588 static int method_mark_image_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
589 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_mark_read_only
);
592 static int method_get_image_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
593 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_hostname
);
596 static int method_get_image_machine_id(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
597 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_id
);
600 static int method_get_image_machine_info(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
601 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_info
);
604 static int method_get_image_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
605 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_os_release
);
608 static int clean_pool_done(Operation
*operation
, int ret
, sd_bus_error
*error
) {
609 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
610 _cleanup_fclose_
FILE *f
= NULL
;
616 assert(operation
->extra_fd
>= 0);
618 if (lseek(operation
->extra_fd
, 0, SEEK_SET
) == (off_t
) -1)
621 f
= take_fdopen(&operation
->extra_fd
, "r");
625 /* The resulting temporary file starts with a boolean value that indicates success or not. */
627 n
= fread(&success
, 1, sizeof(success
), f
);
628 if (n
!= sizeof(success
))
629 return ret
< 0 ? ret
: errno_or_else(EIO
);
632 _cleanup_free_
char *name
= NULL
;
634 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
635 * set to false followed by the name of the failed image. Let's try to read this and use it for the
636 * error message. If we can't read it, don't mind, and return the naked error. */
638 if (success
) /* The resulting temporary file could not be updated, ignore it. */
641 r
= read_nul_string(f
, LONG_LINE_MAX
, &name
);
642 if (r
<= 0) /* Same here... */
645 return sd_bus_error_set_errnof(error
, ret
, "Failed to remove image %s: %m", name
);
650 r
= sd_bus_message_new_method_return(operation
->message
, &reply
);
654 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
658 /* On success the resulting temporary file will contain a list of image names that were removed followed by
659 * their size on disk. Let's read that and turn it into a bus message. */
661 _cleanup_free_
char *name
= NULL
;
664 r
= read_nul_string(f
, LONG_LINE_MAX
, &name
);
667 if (r
== 0) /* reached the end */
671 n
= fread(&size
, 1, sizeof(size
), f
);
672 if (n
!= sizeof(size
))
673 return errno_or_else(EIO
);
675 r
= sd_bus_message_append(reply
, "(st)", name
, size
);
680 r
= sd_bus_message_close_container(reply
);
684 return sd_bus_send(NULL
, reply
, NULL
);
687 static int method_clean_pool(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
693 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
694 _cleanup_close_
int result_fd
= -1;
695 Manager
*m
= userdata
;
696 Operation
*operation
;
703 if (m
->n_operations
>= OPERATIONS_MAX
)
704 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing operations.");
706 r
= sd_bus_message_read(message
, "s", &mm
);
710 if (streq(mm
, "all"))
712 else if (streq(mm
, "hidden"))
713 mode
= REMOVE_HIDDEN
;
715 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown mode '%s'.", mm
);
717 r
= bus_verify_polkit_async(
720 "org.freedesktop.machine1.manage-machines",
729 return 1; /* Will call us back */
731 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
732 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
734 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
735 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
737 result_fd
= open_tmpfile_unlinkable(NULL
, O_RDWR
|O_CLOEXEC
);
741 /* This might be a slow operation, run it asynchronously in a background process */
742 r
= safe_fork("(sd-clean)", FORK_RESET_SIGNALS
, &child
);
744 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
746 _cleanup_hashmap_free_ Hashmap
*images
= NULL
;
751 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
753 images
= hashmap_new(&image_hash_ops
);
759 r
= image_discover(IMAGE_MACHINE
, NULL
, images
);
763 l
= write(result_fd
, &success
, sizeof(success
));
769 HASHMAP_FOREACH(image
, images
) {
771 /* We can't remove vendor images (i.e. those in /usr) */
772 if (IMAGE_IS_VENDOR(image
))
775 if (IMAGE_IS_HOST(image
))
778 if (mode
== REMOVE_HIDDEN
&& !IMAGE_IS_HIDDEN(image
))
781 r
= image_remove(image
);
782 if (r
== -EBUSY
) /* keep images that are currently being used. */
785 /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */
787 (void) ftruncate(result_fd
, 0);
788 (void) lseek(result_fd
, 0, SEEK_SET
);
789 (void) write(result_fd
, &success
, sizeof(success
));
790 (void) write(result_fd
, image
->name
, strlen(image
->name
)+1);
794 l
= write(result_fd
, image
->name
, strlen(image
->name
)+1);
800 l
= write(result_fd
, &image
->usage_exclusive
, sizeof(image
->usage_exclusive
));
807 result_fd
= safe_close(result_fd
);
811 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
815 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
817 /* The clean-up might take a while, hence install a watch on the child and return */
819 r
= operation_new(m
, NULL
, child
, message
, errno_pipe_fd
[0], &operation
);
821 (void) sigkill_wait(child
);
825 operation
->extra_fd
= result_fd
;
826 operation
->done
= clean_pool_done
;
829 errno_pipe_fd
[0] = -1;
834 static int method_set_pool_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
835 Manager
*m
= userdata
;
841 r
= sd_bus_message_read(message
, "t", &limit
);
844 if (!FILE_SIZE_VALID_OR_INFINITY(limit
))
845 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "New limit out of range");
847 r
= bus_verify_polkit_async(
850 "org.freedesktop.machine1.manage-machines",
859 return 1; /* Will call us back */
861 /* Set up the machine directory if necessary */
862 r
= setup_machine_directory(error
);
866 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit
);
868 r
= btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit
);
870 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
872 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust quota limit: %m");
874 return sd_bus_reply_method_return(message
, NULL
);
877 static int method_set_image_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
878 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_set_limit
);
881 static int method_map_from_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
882 Manager
*m
= userdata
;
889 r
= sd_bus_message_read(message
, "su", &name
, &uid
);
893 if (!uid_is_valid(uid
))
894 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
896 machine
= hashmap_get(m
->machines
, name
);
898 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
900 if (machine
->class != MACHINE_CONTAINER
)
901 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
903 r
= machine_translate_uid(machine
, uid
, &converted
);
905 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching user mappings.", name
);
909 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
912 static int method_map_to_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
913 _cleanup_free_
char *o
= NULL
;
914 Manager
*m
= userdata
;
916 uid_t uid
, converted
;
919 r
= sd_bus_message_read(message
, "u", &uid
);
922 if (!uid_is_valid(uid
))
923 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
925 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "User " UID_FMT
" belongs to host UID range", uid
);
927 r
= manager_find_machine_for_uid(m
, uid
, &machine
, &converted
);
931 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "No matching user mapping for " UID_FMT
".", uid
);
933 o
= machine_bus_path(machine
);
937 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
940 static int method_map_from_machine_group(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
941 Manager
*m
= userdata
;
948 r
= sd_bus_message_read(message
, "su", &name
, &gid
);
952 if (!gid_is_valid(gid
))
953 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
955 machine
= hashmap_get(m
->machines
, name
);
957 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
959 if (machine
->class != MACHINE_CONTAINER
)
960 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
962 r
= machine_translate_gid(machine
, gid
, &converted
);
964 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching group mappings.", name
);
968 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
971 static int method_map_to_machine_group(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
972 _cleanup_free_
char *o
= NULL
;
973 Manager
*m
= userdata
;
975 gid_t gid
, converted
;
978 r
= sd_bus_message_read(message
, "u", &gid
);
981 if (!gid_is_valid(gid
))
982 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
984 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Group " GID_FMT
" belongs to host GID range", gid
);
986 r
= manager_find_machine_for_gid(m
, gid
, &machine
, &converted
);
990 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "No matching group mapping for " GID_FMT
".", gid
);
992 o
= machine_bus_path(machine
);
996 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
999 const sd_bus_vtable manager_vtable
[] = {
1000 SD_BUS_VTABLE_START(0),
1002 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path
, 0, 0),
1003 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage
, 0, 0),
1004 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit
, 0, 0),
1006 SD_BUS_METHOD_WITH_NAMES("GetMachine",
1010 SD_BUS_PARAM(machine
),
1012 SD_BUS_VTABLE_UNPRIVILEGED
),
1013 SD_BUS_METHOD_WITH_NAMES("GetImage",
1017 SD_BUS_PARAM(image
),
1019 SD_BUS_VTABLE_UNPRIVILEGED
),
1020 SD_BUS_METHOD_WITH_NAMES("GetMachineByPID",
1024 SD_BUS_PARAM(machine
),
1025 method_get_machine_by_pid
,
1026 SD_BUS_VTABLE_UNPRIVILEGED
),
1027 SD_BUS_METHOD_WITH_NAMES("ListMachines",
1030 SD_BUS_PARAM(machines
),
1031 method_list_machines
,
1032 SD_BUS_VTABLE_UNPRIVILEGED
),
1033 SD_BUS_METHOD_WITH_NAMES("ListImages",
1036 SD_BUS_PARAM(images
),
1038 SD_BUS_VTABLE_UNPRIVILEGED
),
1039 SD_BUS_METHOD_WITH_NAMES("CreateMachine",
1043 SD_BUS_PARAM(service
)
1045 SD_BUS_PARAM(leader
)
1046 SD_BUS_PARAM(root_directory
)
1047 SD_BUS_PARAM(scope_properties
),
1050 method_create_machine
, 0),
1051 SD_BUS_METHOD_WITH_NAMES("CreateMachineWithNetwork",
1055 SD_BUS_PARAM(service
)
1057 SD_BUS_PARAM(leader
)
1058 SD_BUS_PARAM(root_directory
)
1059 SD_BUS_PARAM(ifindices
)
1060 SD_BUS_PARAM(scope_properties
),
1063 method_create_machine_with_network
, 0),
1064 SD_BUS_METHOD_WITH_NAMES("RegisterMachine",
1068 SD_BUS_PARAM(service
)
1070 SD_BUS_PARAM(leader
)
1071 SD_BUS_PARAM(root_directory
),
1074 method_register_machine
, 0),
1075 SD_BUS_METHOD_WITH_NAMES("RegisterMachineWithNetwork",
1079 SD_BUS_PARAM(service
)
1081 SD_BUS_PARAM(leader
)
1082 SD_BUS_PARAM(root_directory
)
1083 SD_BUS_PARAM(ifindices
),
1086 method_register_machine_with_network
, 0),
1087 SD_BUS_METHOD_WITH_NAMES("UnregisterMachine",
1091 method_unregister_machine
,
1092 SD_BUS_VTABLE_UNPRIVILEGED
),
1093 SD_BUS_METHOD_WITH_NAMES("TerminateMachine",
1097 method_terminate_machine
,
1098 SD_BUS_VTABLE_UNPRIVILEGED
),
1099 SD_BUS_METHOD_WITH_NAMES("KillMachine",
1103 SD_BUS_PARAM(signal
),
1105 method_kill_machine
,
1106 SD_BUS_VTABLE_UNPRIVILEGED
),
1107 SD_BUS_METHOD_WITH_NAMES("GetMachineAddresses",
1111 SD_BUS_PARAM(addresses
),
1112 method_get_machine_addresses
,
1113 SD_BUS_VTABLE_UNPRIVILEGED
),
1114 SD_BUS_METHOD_WITH_NAMES("GetMachineOSRelease",
1118 SD_BUS_PARAM(fields
),
1119 method_get_machine_os_release
,
1120 SD_BUS_VTABLE_UNPRIVILEGED
),
1121 SD_BUS_METHOD_WITH_NAMES("OpenMachinePTY",
1126 SD_BUS_PARAM(pty_path
),
1127 method_open_machine_pty
,
1129 SD_BUS_METHOD_WITH_NAMES("OpenMachineLogin",
1134 SD_BUS_PARAM(pty_path
),
1135 method_open_machine_login
,
1136 SD_BUS_VTABLE_UNPRIVILEGED
),
1137 SD_BUS_METHOD_WITH_NAMES("OpenMachineShell",
1143 SD_BUS_PARAM(environment
),
1146 SD_BUS_PARAM(pty_path
),
1147 method_open_machine_shell
,
1148 SD_BUS_VTABLE_UNPRIVILEGED
),
1149 SD_BUS_METHOD_WITH_NAMES("BindMountMachine",
1152 SD_BUS_PARAM(source
)
1153 SD_BUS_PARAM(destination
)
1154 SD_BUS_PARAM(read_only
)
1155 SD_BUS_PARAM(mkdir
),
1157 method_bind_mount_machine
,
1158 SD_BUS_VTABLE_UNPRIVILEGED
),
1159 SD_BUS_METHOD_WITH_NAMES("CopyFromMachine",
1162 SD_BUS_PARAM(source
)
1163 SD_BUS_PARAM(destination
),
1165 method_copy_machine
,
1166 SD_BUS_VTABLE_UNPRIVILEGED
),
1167 SD_BUS_METHOD_WITH_NAMES("CopyToMachine",
1170 SD_BUS_PARAM(source
)
1171 SD_BUS_PARAM(destination
),
1173 method_copy_machine
,
1174 SD_BUS_VTABLE_UNPRIVILEGED
),
1175 SD_BUS_METHOD_WITH_NAMES("OpenMachineRootDirectory",
1180 method_open_machine_root_directory
,
1181 SD_BUS_VTABLE_UNPRIVILEGED
),
1182 SD_BUS_METHOD_WITH_NAMES("GetMachineUIDShift",
1186 SD_BUS_PARAM(shift
),
1187 method_get_machine_uid_shift
,
1188 SD_BUS_VTABLE_UNPRIVILEGED
),
1189 SD_BUS_METHOD_WITH_NAMES("RemoveImage",
1193 method_remove_image
,
1194 SD_BUS_VTABLE_UNPRIVILEGED
),
1195 SD_BUS_METHOD_WITH_NAMES("RenameImage",
1198 SD_BUS_PARAM(new_name
),
1200 method_rename_image
,
1201 SD_BUS_VTABLE_UNPRIVILEGED
),
1202 SD_BUS_METHOD_WITH_NAMES("CloneImage",
1205 SD_BUS_PARAM(new_name
)
1206 SD_BUS_PARAM(read_only
),
1209 SD_BUS_VTABLE_UNPRIVILEGED
),
1210 SD_BUS_METHOD_WITH_NAMES("MarkImageReadOnly",
1213 SD_BUS_PARAM(read_only
),
1215 method_mark_image_read_only
,
1216 SD_BUS_VTABLE_UNPRIVILEGED
),
1217 SD_BUS_METHOD_WITH_NAMES("GetImageHostname",
1221 SD_BUS_PARAM(hostname
),
1222 method_get_image_hostname
,
1223 SD_BUS_VTABLE_UNPRIVILEGED
),
1224 SD_BUS_METHOD_WITH_NAMES("GetImageMachineID",
1229 method_get_image_machine_id
,
1230 SD_BUS_VTABLE_UNPRIVILEGED
),
1231 SD_BUS_METHOD_WITH_NAMES("GetImageMachineInfo",
1235 SD_BUS_PARAM(machine_info
),
1236 method_get_image_machine_info
,
1237 SD_BUS_VTABLE_UNPRIVILEGED
),
1238 SD_BUS_METHOD_WITH_NAMES("GetImageOSRelease",
1242 SD_BUS_PARAM(os_release
),
1243 method_get_image_os_release
,
1244 SD_BUS_VTABLE_UNPRIVILEGED
),
1245 SD_BUS_METHOD_WITH_NAMES("SetPoolLimit",
1249 method_set_pool_limit
,
1250 SD_BUS_VTABLE_UNPRIVILEGED
),
1251 SD_BUS_METHOD_WITH_NAMES("SetImageLimit",
1256 method_set_image_limit
,
1257 SD_BUS_VTABLE_UNPRIVILEGED
),
1258 SD_BUS_METHOD_WITH_NAMES("CleanPool",
1262 SD_BUS_PARAM(images
),
1264 SD_BUS_VTABLE_UNPRIVILEGED
),
1265 SD_BUS_METHOD_WITH_NAMES("MapFromMachineUser",
1268 SD_BUS_PARAM(uid_inner
),
1270 SD_BUS_PARAM(uid_outer
),
1271 method_map_from_machine_user
,
1272 SD_BUS_VTABLE_UNPRIVILEGED
),
1273 SD_BUS_METHOD_WITH_NAMES("MapToMachineUser",
1275 SD_BUS_PARAM(uid_outer
),
1277 SD_BUS_PARAM(machine_name
)
1278 SD_BUS_PARAM(machine_path
)
1279 SD_BUS_PARAM(uid_inner
),
1280 method_map_to_machine_user
,
1281 SD_BUS_VTABLE_UNPRIVILEGED
),
1282 SD_BUS_METHOD_WITH_NAMES("MapFromMachineGroup",
1285 SD_BUS_PARAM(gid_inner
),
1287 SD_BUS_PARAM(gid_outer
),
1288 method_map_from_machine_group
,
1289 SD_BUS_VTABLE_UNPRIVILEGED
),
1290 SD_BUS_METHOD_WITH_NAMES("MapToMachineGroup",
1292 SD_BUS_PARAM(gid_outer
),
1294 SD_BUS_PARAM(machine_name
)
1295 SD_BUS_PARAM(machine_path
)
1296 SD_BUS_PARAM(gid_inner
),
1297 method_map_to_machine_group
,
1298 SD_BUS_VTABLE_UNPRIVILEGED
),
1300 SD_BUS_SIGNAL_WITH_NAMES("MachineNew",
1302 SD_BUS_PARAM(machine
)
1305 SD_BUS_SIGNAL_WITH_NAMES("MachineRemoved",
1307 SD_BUS_PARAM(machine
)
1314 const BusObjectImplementation manager_object
= {
1315 "/org/freedesktop/machine1",
1316 "org.freedesktop.machine1.Manager",
1317 .vtables
= BUS_VTABLES(manager_vtable
),
1318 .children
= BUS_IMPLEMENTATIONS( &machine_object
,
1322 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1323 const char *path
, *result
, *unit
;
1324 Manager
*m
= userdata
;
1332 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
1334 bus_log_parse_error(r
);
1338 machine
= hashmap_get(m
->machine_units
, unit
);
1342 if (streq_ptr(path
, machine
->scope_job
)) {
1343 machine
->scope_job
= mfree(machine
->scope_job
);
1345 if (machine
->started
) {
1346 if (streq(result
, "done"))
1347 machine_send_create_reply(machine
, NULL
);
1349 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
1351 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit %s failed with '%s'", unit
, result
);
1353 machine_send_create_reply(machine
, &e
);
1357 machine_save(machine
);
1360 machine_add_to_gc_queue(machine
);
1364 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1365 _cleanup_free_
char *unit
= NULL
;
1367 Manager
*m
= userdata
;
1374 path
= sd_bus_message_get_path(message
);
1378 r
= unit_name_from_dbus_path(path
, &unit
);
1379 if (r
== -EINVAL
) /* not for a unit */
1386 machine
= hashmap_get(m
->machine_units
, unit
);
1390 machine_add_to_gc_queue(machine
);
1394 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1395 const char *path
, *unit
;
1396 Manager
*m
= userdata
;
1403 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
1405 bus_log_parse_error(r
);
1409 machine
= hashmap_get(m
->machine_units
, unit
);
1413 machine_add_to_gc_queue(machine
);
1417 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1418 Manager
*m
= userdata
;
1425 r
= sd_bus_message_read(message
, "b", &b
);
1427 bus_log_parse_error(r
);
1433 /* systemd finished reloading, let's recheck all our machines */
1434 log_debug("System manager has been reloaded, rechecking machines...");
1436 HASHMAP_FOREACH(machine
, m
->machines
)
1437 machine_add_to_gc_queue(machine
);
1442 int manager_unref_unit(
1445 sd_bus_error
*error
) {
1450 return bus_call_method(m
->bus
, bus_systemd_mgr
, "UnrefUnit", error
, NULL
, "s", unit
);
1453 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
1454 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1460 r
= bus_call_method(manager
->bus
, bus_systemd_mgr
, "StopUnit", error
, &reply
, "ss", unit
, "fail");
1462 if (sd_bus_error_has_names(error
, BUS_ERROR_NO_SUCH_UNIT
,
1463 BUS_ERROR_LOAD_FAILED
)) {
1468 sd_bus_error_free(error
);
1479 r
= sd_bus_message_read(reply
, "o", &j
);
1493 int manager_kill_unit(Manager
*manager
, const char *unit
, int signo
, sd_bus_error
*error
) {
1497 return bus_call_method(manager
->bus
, bus_systemd_mgr
, "KillUnit", error
, NULL
, "ssi", unit
, "all", signo
);
1500 int manager_unit_is_active(Manager
*manager
, const char *unit
) {
1501 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1502 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1503 _cleanup_free_
char *path
= NULL
;
1510 path
= unit_dbus_path_from_name(unit
);
1514 r
= sd_bus_get_property(
1516 "org.freedesktop.systemd1",
1518 "org.freedesktop.systemd1.Unit",
1524 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
1525 SD_BUS_ERROR_DISCONNECTED
))
1528 if (sd_bus_error_has_names(&error
, BUS_ERROR_NO_SUCH_UNIT
,
1529 BUS_ERROR_LOAD_FAILED
))
1535 r
= sd_bus_message_read(reply
, "s", &state
);
1539 return !STR_IN_SET(state
, "inactive", "failed");
1542 int manager_job_is_active(Manager
*manager
, const char *path
) {
1543 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1544 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1550 r
= sd_bus_get_property(
1552 "org.freedesktop.systemd1",
1554 "org.freedesktop.systemd1.Job",
1560 if (sd_bus_error_has_names(&error
, SD_BUS_ERROR_NO_REPLY
,
1561 SD_BUS_ERROR_DISCONNECTED
))
1564 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
1570 /* We don't actually care about the state really. The fact
1571 * that we could read the job state is enough for us */
1576 int manager_get_machine_by_pid(Manager
*m
, pid_t pid
, Machine
**machine
) {
1584 mm
= hashmap_get(m
->machine_leaders
, PID_TO_PTR(pid
));
1586 _cleanup_free_
char *unit
= NULL
;
1588 r
= cg_pid_get_unit(pid
, &unit
);
1590 mm
= hashmap_get(m
->machine_units
, unit
);
1599 int manager_add_machine(Manager
*m
, const char *name
, Machine
**_machine
) {
1605 machine
= hashmap_get(m
->machines
, name
);
1607 machine
= machine_new(m
, _MACHINE_CLASS_INVALID
, name
);
1613 *_machine
= machine
;