1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "btrfs-util.h"
11 #include "bus-common-errors.h"
13 #include "cgroup-util.h"
14 #include "errno-util.h"
17 #include "format-util.h"
18 #include "hostname-util.h"
19 #include "image-dbus.h"
21 #include "machine-dbus.h"
22 #include "machine-image.h"
23 #include "machine-pool.h"
25 #include "missing_capability.h"
26 #include "path-util.h"
27 #include "process-util.h"
28 #include "stdio-util.h"
30 #include "tmpfile-util.h"
31 #include "unit-name.h"
32 #include "user-util.h"
34 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_pool_path
, "s", "/var/lib/machines");
36 static int property_get_pool_usage(
39 const char *interface
,
41 sd_bus_message
*reply
,
43 sd_bus_error
*error
) {
45 _cleanup_close_
int fd
= -1;
46 uint64_t usage
= (uint64_t) -1;
51 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
55 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
59 return sd_bus_message_append(reply
, "t", usage
);
62 static int property_get_pool_limit(
65 const char *interface
,
67 sd_bus_message
*reply
,
69 sd_bus_error
*error
) {
71 _cleanup_close_
int fd
= -1;
72 uint64_t size
= (uint64_t) -1;
77 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
81 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
82 size
= q
.referenced_max
;
85 return sd_bus_message_append(reply
, "t", size
);
88 static int method_get_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
89 _cleanup_free_
char *p
= NULL
;
90 Manager
*m
= userdata
;
98 r
= sd_bus_message_read(message
, "s", &name
);
102 machine
= hashmap_get(m
->machines
, name
);
104 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
106 p
= machine_bus_path(machine
);
110 return sd_bus_reply_method_return(message
, "o", p
);
113 static int method_get_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
114 _cleanup_free_
char *p
= NULL
;
115 Manager
*m
= userdata
;
122 r
= sd_bus_message_read(message
, "s", &name
);
126 r
= image_find(IMAGE_MACHINE
, name
, 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
= userdata
;
142 Machine
*machine
= NULL
;
149 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
151 r
= sd_bus_message_read(message
, "u", &pid
);
159 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
161 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
165 r
= sd_bus_creds_get_pid(creds
, &pid
);
170 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
174 return sd_bus_error_setf(error
, BUS_ERROR_NO_MACHINE_FOR_PID
, "PID "PID_FMT
" does not belong to any known machine", pid
);
176 p
= machine_bus_path(machine
);
180 return sd_bus_reply_method_return(message
, "o", p
);
183 static int method_list_machines(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
184 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
185 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
, i
) {
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_terminate_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
452 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_terminate
);
455 static int method_kill_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
456 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_kill
);
459 static int method_get_machine_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
460 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_addresses
);
463 static int method_get_machine_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
464 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_os_release
);
467 static int method_list_images(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
468 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
469 _cleanup_hashmap_free_ Hashmap
*images
= NULL
;
470 Manager
*m
= userdata
;
478 images
= hashmap_new(&image_hash_ops
);
482 r
= image_discover(IMAGE_MACHINE
, images
);
486 r
= sd_bus_message_new_method_return(message
, &reply
);
490 r
= sd_bus_message_open_container(reply
, 'a', "(ssbttto)");
494 HASHMAP_FOREACH(image
, images
, i
) {
495 _cleanup_free_
char *p
= NULL
;
497 p
= image_bus_path(image
->name
);
501 r
= sd_bus_message_append(reply
, "(ssbttto)",
503 image_type_to_string(image
->type
),
513 r
= sd_bus_message_close_container(reply
);
517 return sd_bus_send(NULL
, reply
, NULL
);
520 static int method_open_machine_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
521 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_pty
);
524 static int method_open_machine_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
525 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_login
);
528 static int method_open_machine_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
529 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_shell
);
532 static int method_bind_mount_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
533 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_bind_mount
);
536 static int method_copy_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
537 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_copy
);
540 static int method_open_machine_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
541 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_root_directory
);
544 static int method_get_machine_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
545 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_uid_shift
);
548 static int redirect_method_to_image(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
549 _cleanup_(image_unrefp
) Image
* i
= NULL
;
557 r
= sd_bus_message_read(message
, "s", &name
);
561 if (!image_name_is_valid(name
))
562 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
564 r
= image_find(IMAGE_MACHINE
, name
, &i
);
566 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
571 return method(message
, i
, error
);
574 static int method_remove_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
575 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_remove
);
578 static int method_rename_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
579 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_rename
);
582 static int method_clone_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
583 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_clone
);
586 static int method_mark_image_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
587 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_mark_read_only
);
590 static int method_get_image_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
591 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_hostname
);
594 static int method_get_image_machine_id(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
595 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_id
);
598 static int method_get_image_machine_info(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
599 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_info
);
602 static int method_get_image_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
603 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_os_release
);
606 static int clean_pool_done(Operation
*operation
, int ret
, sd_bus_error
*error
) {
607 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
608 _cleanup_fclose_
FILE *f
= NULL
;
614 assert(operation
->extra_fd
>= 0);
616 if (lseek(operation
->extra_fd
, 0, SEEK_SET
) == (off_t
) -1)
619 f
= fdopen(operation
->extra_fd
, "r");
623 operation
->extra_fd
= -1;
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
;
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
, i
) {
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 _cleanup_fclose_
FILE *f
= NULL
;
884 Manager
*m
= userdata
;
885 const char *name
, *p
;
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 p
= procfs_file_alloca(machine
->leader
, "uid_map");
910 uid_t uid_base
, uid_shift
, uid_range
, converted
;
914 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
915 if (k
< 0 && feof(f
))
919 return errno_or_else(EIO
);
924 if (uid
< uid_base
|| uid
>= uid_base
+ uid_range
)
927 converted
= uid
- uid_base
+ uid_shift
;
928 if (!uid_is_valid(converted
))
929 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
931 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
934 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching user mappings.", name
);
937 static int method_map_to_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
938 Manager
*m
= userdata
;
944 r
= sd_bus_message_read(message
, "u", &uid
);
947 if (!uid_is_valid(uid
))
948 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
950 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "User " UID_FMT
" belongs to host UID range", uid
);
952 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
953 _cleanup_fclose_
FILE *f
= NULL
;
954 char p
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
956 if (machine
->class != MACHINE_CONTAINER
)
959 xsprintf(p
, "/proc/" UID_FMT
"/uid_map", machine
->leader
);
962 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
967 _cleanup_free_
char *o
= NULL
;
968 uid_t uid_base
, uid_shift
, uid_range
, converted
;
972 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
973 if (k
< 0 && feof(f
))
977 return errno_or_else(EIO
);
982 /* The private user namespace is disabled, ignoring. */
986 if (uid
< uid_shift
|| uid
>= uid_shift
+ uid_range
)
989 converted
= (uid
- uid_shift
+ uid_base
);
990 if (!uid_is_valid(converted
))
991 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
993 o
= machine_bus_path(machine
);
997 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1001 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "No matching user mapping for " UID_FMT
".", uid
);
1004 static int method_map_from_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1005 _cleanup_fclose_
FILE *f
= NULL
;
1006 Manager
*m
= groupdata
;
1007 const char *name
, *p
;
1012 r
= sd_bus_message_read(message
, "su", &name
, &gid
);
1016 if (!gid_is_valid(gid
))
1017 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1019 machine
= hashmap_get(m
->machines
, name
);
1021 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
1023 if (machine
->class != MACHINE_CONTAINER
)
1024 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
1026 p
= procfs_file_alloca(machine
->leader
, "gid_map");
1032 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1036 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1037 if (k
< 0 && feof(f
))
1041 return errno_or_else(EIO
);
1046 if (gid
< gid_base
|| gid
>= gid_base
+ gid_range
)
1049 converted
= gid
- gid_base
+ gid_shift
;
1050 if (!gid_is_valid(converted
))
1051 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1053 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
1056 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Machine '%s' has no matching group mappings.", name
);
1059 static int method_map_to_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1060 Manager
*m
= groupdata
;
1066 r
= sd_bus_message_read(message
, "u", &gid
);
1069 if (!gid_is_valid(gid
))
1070 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1072 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Group " GID_FMT
" belongs to host GID range", gid
);
1074 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1075 _cleanup_fclose_
FILE *f
= NULL
;
1076 char p
[STRLEN("/proc//gid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
1078 if (machine
->class != MACHINE_CONTAINER
)
1081 xsprintf(p
, "/proc/" GID_FMT
"/gid_map", machine
->leader
);
1084 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
1089 _cleanup_free_
char *o
= NULL
;
1090 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1094 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1095 if (k
< 0 && feof(f
))
1099 return errno_or_else(EIO
);
1104 /* The private user namespace is disabled, ignoring. */
1108 if (gid
< gid_shift
|| gid
>= gid_shift
+ gid_range
)
1111 converted
= (gid
- gid_shift
+ gid_base
);
1112 if (!gid_is_valid(converted
))
1113 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1115 o
= machine_bus_path(machine
);
1119 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1123 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "No matching group mapping for " GID_FMT
".", gid
);
1126 const sd_bus_vtable manager_vtable
[] = {
1127 SD_BUS_VTABLE_START(0),
1128 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path
, 0, 0),
1129 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage
, 0, 0),
1130 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit
, 0, 0),
1131 SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1132 SD_BUS_METHOD("GetImage", "s", "o", method_get_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1133 SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
1134 SD_BUS_METHOD("ListMachines", NULL
, "a(ssso)", method_list_machines
, SD_BUS_VTABLE_UNPRIVILEGED
),
1135 SD_BUS_METHOD("ListImages", NULL
, "a(ssbttto)", method_list_images
, SD_BUS_VTABLE_UNPRIVILEGED
),
1136 SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine
, 0),
1137 SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network
, 0),
1138 SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine
, 0),
1139 SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network
, 0),
1140 SD_BUS_METHOD("TerminateMachine", "s", NULL
, method_terminate_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1141 SD_BUS_METHOD("KillMachine", "ssi", NULL
, method_kill_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1142 SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1143 SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1144 SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty
, 0),
1145 SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1146 SD_BUS_METHOD("OpenMachineShell", "sssasas", "hs", method_open_machine_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1147 SD_BUS_METHOD("BindMountMachine", "sssbb", NULL
, method_bind_mount_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1148 SD_BUS_METHOD("CopyFromMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1149 SD_BUS_METHOD("CopyToMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1150 SD_BUS_METHOD("OpenMachineRootDirectory", "s", "h", method_open_machine_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1151 SD_BUS_METHOD("GetMachineUIDShift", "s", "u", method_get_machine_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1152 SD_BUS_METHOD("RemoveImage", "s", NULL
, method_remove_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1153 SD_BUS_METHOD("RenameImage", "ss", NULL
, method_rename_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1154 SD_BUS_METHOD("CloneImage", "ssb", NULL
, method_clone_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1155 SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL
, method_mark_image_read_only
, SD_BUS_VTABLE_UNPRIVILEGED
),
1156 SD_BUS_METHOD("GetImageHostname", "s", "s", method_get_image_hostname
, SD_BUS_VTABLE_UNPRIVILEGED
),
1157 SD_BUS_METHOD("GetImageMachineID", "s", "ay", method_get_image_machine_id
, SD_BUS_VTABLE_UNPRIVILEGED
),
1158 SD_BUS_METHOD("GetImageMachineInfo", "s", "a{ss}", method_get_image_machine_info
, SD_BUS_VTABLE_UNPRIVILEGED
),
1159 SD_BUS_METHOD("GetImageOSRelease", "s", "a{ss}", method_get_image_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1160 SD_BUS_METHOD("SetPoolLimit", "t", NULL
, method_set_pool_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1161 SD_BUS_METHOD("SetImageLimit", "st", NULL
, method_set_image_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1162 SD_BUS_METHOD("CleanPool", "s", "a(st)", method_clean_pool
, SD_BUS_VTABLE_UNPRIVILEGED
),
1163 SD_BUS_METHOD("MapFromMachineUser", "su", "u", method_map_from_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1164 SD_BUS_METHOD("MapToMachineUser", "u", "sou", method_map_to_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1165 SD_BUS_METHOD("MapFromMachineGroup", "su", "u", method_map_from_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1166 SD_BUS_METHOD("MapToMachineGroup", "u", "sou", method_map_to_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1167 SD_BUS_SIGNAL("MachineNew", "so", 0),
1168 SD_BUS_SIGNAL("MachineRemoved", "so", 0),
1172 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1173 const char *path
, *result
, *unit
;
1174 Manager
*m
= userdata
;
1182 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
1184 bus_log_parse_error(r
);
1188 machine
= hashmap_get(m
->machine_units
, unit
);
1192 if (streq_ptr(path
, machine
->scope_job
)) {
1193 machine
->scope_job
= mfree(machine
->scope_job
);
1195 if (machine
->started
) {
1196 if (streq(result
, "done"))
1197 machine_send_create_reply(machine
, NULL
);
1199 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
1201 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit %s failed with '%s'", unit
, result
);
1203 machine_send_create_reply(machine
, &e
);
1207 machine_save(machine
);
1210 machine_add_to_gc_queue(machine
);
1214 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1215 _cleanup_free_
char *unit
= NULL
;
1217 Manager
*m
= userdata
;
1224 path
= sd_bus_message_get_path(message
);
1228 r
= unit_name_from_dbus_path(path
, &unit
);
1229 if (r
== -EINVAL
) /* not for a unit */
1236 machine
= hashmap_get(m
->machine_units
, unit
);
1240 machine_add_to_gc_queue(machine
);
1244 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1245 const char *path
, *unit
;
1246 Manager
*m
= userdata
;
1253 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
1255 bus_log_parse_error(r
);
1259 machine
= hashmap_get(m
->machine_units
, unit
);
1263 machine_add_to_gc_queue(machine
);
1267 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1268 Manager
*m
= userdata
;
1276 r
= sd_bus_message_read(message
, "b", &b
);
1278 bus_log_parse_error(r
);
1284 /* systemd finished reloading, let's recheck all our machines */
1285 log_debug("System manager has been reloaded, rechecking machines...");
1287 HASHMAP_FOREACH(machine
, m
->machines
, i
)
1288 machine_add_to_gc_queue(machine
);
1293 int manager_start_scope(
1298 const char *description
,
1299 sd_bus_message
*more_properties
,
1300 sd_bus_error
*error
,
1303 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1310 r
= sd_bus_message_new_method_call(
1313 "org.freedesktop.systemd1",
1314 "/org/freedesktop/systemd1",
1315 "org.freedesktop.systemd1.Manager",
1316 "StartTransientUnit");
1320 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
1324 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1328 if (!isempty(slice
)) {
1329 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
1334 if (!isempty(description
)) {
1335 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
1340 r
= sd_bus_message_append(m
, "(sv)(sv)(sv)(sv)(sv)",
1341 "PIDs", "au", 1, pid
,
1343 "CollectMode", "s", "inactive-or-failed",
1345 "TasksMax", "t", UINT64_C(16384));
1349 if (more_properties
) {
1350 r
= sd_bus_message_copy(m
, more_properties
, true);
1355 r
= sd_bus_message_close_container(m
);
1359 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1363 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
1371 r
= sd_bus_message_read(reply
, "o", &j
);
1385 int manager_unref_unit(
1388 sd_bus_error
*error
) {
1393 return sd_bus_call_method(
1395 "org.freedesktop.systemd1",
1396 "/org/freedesktop/systemd1",
1397 "org.freedesktop.systemd1.Manager",
1405 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
1406 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1412 r
= sd_bus_call_method(
1414 "org.freedesktop.systemd1",
1415 "/org/freedesktop/systemd1",
1416 "org.freedesktop.systemd1.Manager",
1420 "ss", unit
, "fail");
1422 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1423 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
1428 sd_bus_error_free(error
);
1439 r
= sd_bus_message_read(reply
, "o", &j
);
1453 int manager_kill_unit(Manager
*manager
, const char *unit
, int signo
, sd_bus_error
*error
) {
1457 return sd_bus_call_method(
1459 "org.freedesktop.systemd1",
1460 "/org/freedesktop/systemd1",
1461 "org.freedesktop.systemd1.Manager",
1465 "ssi", unit
, "all", signo
);
1468 int manager_unit_is_active(Manager
*manager
, const char *unit
) {
1469 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1470 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1471 _cleanup_free_
char *path
= NULL
;
1478 path
= unit_dbus_path_from_name(unit
);
1482 r
= sd_bus_get_property(
1484 "org.freedesktop.systemd1",
1486 "org.freedesktop.systemd1.Unit",
1492 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1493 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1496 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1497 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
1503 r
= sd_bus_message_read(reply
, "s", &state
);
1507 return !STR_IN_SET(state
, "inactive", "failed");
1510 int manager_job_is_active(Manager
*manager
, const char *path
) {
1511 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1512 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1518 r
= sd_bus_get_property(
1520 "org.freedesktop.systemd1",
1522 "org.freedesktop.systemd1.Job",
1528 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1529 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1532 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
1538 /* We don't actually care about the state really. The fact
1539 * that we could read the job state is enough for us */
1544 int manager_get_machine_by_pid(Manager
*m
, pid_t pid
, Machine
**machine
) {
1552 mm
= hashmap_get(m
->machine_leaders
, PID_TO_PTR(pid
));
1554 _cleanup_free_
char *unit
= NULL
;
1556 r
= cg_pid_get_unit(pid
, &unit
);
1558 mm
= hashmap_get(m
->machine_units
, unit
);
1567 int manager_add_machine(Manager
*m
, const char *name
, Machine
**_machine
) {
1573 machine
= hashmap_get(m
->machines
, name
);
1575 machine
= machine_new(m
, _MACHINE_CLASS_INVALID
, name
);
1581 *_machine
= machine
;