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"
16 #include "format-util.h"
17 #include "hostname-util.h"
18 #include "image-dbus.h"
20 #include "machine-dbus.h"
21 #include "machine-image.h"
22 #include "machine-pool.h"
24 #include "path-util.h"
25 #include "process-util.h"
26 #include "stdio-util.h"
28 #include "tmpfile-util.h"
29 #include "unit-name.h"
30 #include "user-util.h"
32 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_pool_path
, "s", "/var/lib/machines");
34 static int property_get_pool_usage(
37 const char *interface
,
39 sd_bus_message
*reply
,
41 sd_bus_error
*error
) {
43 _cleanup_close_
int fd
= -1;
44 uint64_t usage
= (uint64_t) -1;
49 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
53 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
57 return sd_bus_message_append(reply
, "t", usage
);
60 static int property_get_pool_limit(
63 const char *interface
,
65 sd_bus_message
*reply
,
67 sd_bus_error
*error
) {
69 _cleanup_close_
int fd
= -1;
70 uint64_t size
= (uint64_t) -1;
75 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
79 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
80 size
= q
.referenced_max
;
83 return sd_bus_message_append(reply
, "t", size
);
86 static int method_get_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
87 _cleanup_free_
char *p
= NULL
;
88 Manager
*m
= userdata
;
96 r
= sd_bus_message_read(message
, "s", &name
);
100 machine
= hashmap_get(m
->machines
, name
);
102 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
104 p
= machine_bus_path(machine
);
108 return sd_bus_reply_method_return(message
, "o", p
);
111 static int method_get_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
112 _cleanup_free_
char *p
= NULL
;
113 Manager
*m
= userdata
;
120 r
= sd_bus_message_read(message
, "s", &name
);
124 r
= image_find(IMAGE_MACHINE
, name
, NULL
);
126 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
130 p
= image_bus_path(name
);
134 return sd_bus_reply_method_return(message
, "o", p
);
137 static int method_get_machine_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
138 _cleanup_free_
char *p
= NULL
;
139 Manager
*m
= userdata
;
140 Machine
*machine
= NULL
;
147 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
149 r
= sd_bus_message_read(message
, "u", &pid
);
157 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
159 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
163 r
= sd_bus_creds_get_pid(creds
, &pid
);
168 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
172 return sd_bus_error_setf(error
, BUS_ERROR_NO_MACHINE_FOR_PID
, "PID "PID_FMT
" does not belong to any known machine", pid
);
174 p
= machine_bus_path(machine
);
178 return sd_bus_reply_method_return(message
, "o", p
);
181 static int method_list_machines(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
182 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
183 Manager
*m
= userdata
;
191 r
= sd_bus_message_new_method_return(message
, &reply
);
193 return sd_bus_error_set_errno(error
, r
);
195 r
= sd_bus_message_open_container(reply
, 'a', "(ssso)");
197 return sd_bus_error_set_errno(error
, r
);
199 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
200 _cleanup_free_
char *p
= NULL
;
202 p
= machine_bus_path(machine
);
206 r
= sd_bus_message_append(reply
, "(ssso)",
208 strempty(machine_class_to_string(machine
->class)),
212 return sd_bus_error_set_errno(error
, r
);
215 r
= sd_bus_message_close_container(reply
);
217 return sd_bus_error_set_errno(error
, r
);
219 return sd_bus_send(NULL
, reply
, NULL
);
222 static int method_create_or_register_machine(Manager
*manager
, sd_bus_message
*message
, bool read_network
, Machine
**_m
, sd_bus_error
*error
) {
223 const char *name
, *service
, *class, *root_directory
;
224 const int32_t *netif
= NULL
;
230 size_t n
, n_netif
= 0;
237 r
= sd_bus_message_read(message
, "s", &name
);
240 if (!machine_name_is_valid(name
))
241 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine name");
243 r
= sd_bus_message_read_array(message
, 'y', &v
, &n
);
251 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine ID parameter");
253 r
= sd_bus_message_read(message
, "ssus", &service
, &class, &leader
, &root_directory
);
260 r
= sd_bus_message_read_array(message
, 'i', (const void**) &netif
, &n_netif
);
264 n_netif
/= sizeof(int32_t);
266 for (i
= 0; i
< n_netif
; i
++) {
268 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid network interface index %i", netif
[i
]);
273 c
= _MACHINE_CLASS_INVALID
;
275 c
= machine_class_from_string(class);
277 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine class parameter");
281 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
283 if (!isempty(root_directory
) && !path_is_absolute(root_directory
))
284 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Root directory must be empty or an absolute path");
287 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
289 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
293 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
295 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
300 if (hashmap_get(manager
->machines
, name
))
301 return sd_bus_error_setf(error
, BUS_ERROR_MACHINE_EXISTS
, "Machine '%s' already exists", name
);
303 r
= manager_add_machine(manager
, name
, &m
);
311 if (!isempty(service
)) {
312 m
->service
= strdup(service
);
319 if (!isempty(root_directory
)) {
320 m
->root_directory
= strdup(root_directory
);
321 if (!m
->root_directory
) {
328 assert_cc(sizeof(int32_t) == sizeof(int));
329 m
->netif
= memdup(netif
, sizeof(int32_t) * n_netif
);
335 m
->n_netif
= n_netif
;
343 machine_add_to_gc_queue(m
);
347 static int method_create_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
348 Manager
*manager
= userdata
;
355 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
359 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
363 r
= machine_start(m
, message
, error
);
367 m
->create_message
= sd_bus_message_ref(message
);
371 machine_add_to_gc_queue(m
);
375 static int method_create_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
376 return method_create_machine_internal(message
, true, userdata
, error
);
379 static int method_create_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
380 return method_create_machine_internal(message
, false, userdata
, error
);
383 static int method_register_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
384 Manager
*manager
= userdata
;
385 _cleanup_free_
char *p
= NULL
;
392 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
396 r
= cg_pid_get_unit(m
->leader
, &m
->unit
);
398 r
= sd_bus_error_set_errnof(error
, r
,
399 "Failed to determine unit of process "PID_FMT
" : %m",
404 r
= machine_start(m
, NULL
, error
);
408 p
= machine_bus_path(m
);
414 return sd_bus_reply_method_return(message
, "o", p
);
417 machine_add_to_gc_queue(m
);
421 static int method_register_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
422 return method_register_machine_internal(message
, true, userdata
, error
);
425 static int method_register_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
426 return method_register_machine_internal(message
, false, userdata
, error
);
429 static int redirect_method_to_machine(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
438 r
= sd_bus_message_read(message
, "s", &name
);
440 return sd_bus_error_set_errno(error
, r
);
442 machine
= hashmap_get(m
->machines
, name
);
444 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
446 return method(message
, machine
, error
);
449 static int method_terminate_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
450 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_terminate
);
453 static int method_kill_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
454 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_kill
);
457 static int method_get_machine_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
458 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_addresses
);
461 static int method_get_machine_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
462 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_os_release
);
465 static int method_list_images(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
466 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
467 _cleanup_(image_hashmap_freep
) Hashmap
*images
= NULL
;
468 Manager
*m
= userdata
;
476 images
= hashmap_new(&string_hash_ops
);
480 r
= image_discover(IMAGE_MACHINE
, images
);
484 r
= sd_bus_message_new_method_return(message
, &reply
);
488 r
= sd_bus_message_open_container(reply
, 'a', "(ssbttto)");
492 HASHMAP_FOREACH(image
, images
, i
) {
493 _cleanup_free_
char *p
= NULL
;
495 p
= image_bus_path(image
->name
);
499 r
= sd_bus_message_append(reply
, "(ssbttto)",
501 image_type_to_string(image
->type
),
511 r
= sd_bus_message_close_container(reply
);
515 return sd_bus_send(NULL
, reply
, NULL
);
518 static int method_open_machine_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
519 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_pty
);
522 static int method_open_machine_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
523 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_login
);
526 static int method_open_machine_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
527 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_shell
);
530 static int method_bind_mount_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
531 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_bind_mount
);
534 static int method_copy_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
535 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_copy
);
538 static int method_open_machine_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
539 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_root_directory
);
542 static int method_get_machine_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
543 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_uid_shift
);
546 static int redirect_method_to_image(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
547 _cleanup_(image_unrefp
) Image
* i
= NULL
;
555 r
= sd_bus_message_read(message
, "s", &name
);
559 if (!image_name_is_valid(name
))
560 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
562 r
= image_find(IMAGE_MACHINE
, name
, &i
);
564 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
569 return method(message
, i
, error
);
572 static int method_remove_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
573 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_remove
);
576 static int method_rename_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
577 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_rename
);
580 static int method_clone_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
581 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_clone
);
584 static int method_mark_image_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
585 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_mark_read_only
);
588 static int method_get_image_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
589 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_hostname
);
592 static int method_get_image_machine_id(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
593 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_id
);
596 static int method_get_image_machine_info(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
597 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_info
);
600 static int method_get_image_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
601 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_os_release
);
604 static int clean_pool_done(Operation
*operation
, int ret
, sd_bus_error
*error
) {
605 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
606 _cleanup_fclose_
FILE *f
= NULL
;
612 assert(operation
->extra_fd
>= 0);
614 if (lseek(operation
->extra_fd
, 0, SEEK_SET
) == (off_t
) -1)
617 f
= fdopen(operation
->extra_fd
, "re");
621 operation
->extra_fd
= -1;
623 /* The resulting temporary file starts with a boolean value that indicates success or not. */
625 n
= fread(&success
, 1, sizeof(success
), f
);
626 if (n
!= sizeof(success
))
627 return ret
< 0 ? ret
: (errno
!= 0 ? -errno
: -EIO
);
630 _cleanup_free_
char *name
= NULL
;
632 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
633 * set to false followed by the name of the failed image. Let's try to read this and use it for the
634 * error message. If we can't read it, don't mind, and return the naked error. */
636 if (success
) /* The resulting temporary file could not be updated, ignore it. */
639 r
= read_nul_string(f
, &name
);
640 if (r
< 0 || isempty(name
)) /* Same here... */
643 return sd_bus_error_set_errnof(error
, ret
, "Failed to remove image %s: %m", name
);
648 r
= sd_bus_message_new_method_return(operation
->message
, &reply
);
652 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
656 /* On success the resulting temporary file will contain a list of image names that were removed followed by
657 * their size on disk. Let's read that and turn it into a bus message. */
659 _cleanup_free_
char *name
= NULL
;
662 r
= read_nul_string(f
, &name
);
665 if (isempty(name
)) /* reached the end */
669 n
= fread(&size
, 1, sizeof(size
), f
);
670 if (n
!= sizeof(size
))
671 return errno
!= 0 ? -errno
: -EIO
;
673 r
= sd_bus_message_append(reply
, "(st)", name
, size
);
678 r
= sd_bus_message_close_container(reply
);
682 return sd_bus_send(NULL
, reply
, NULL
);
685 static int method_clean_pool(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
691 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
692 _cleanup_close_
int result_fd
= -1;
693 Manager
*m
= userdata
;
694 Operation
*operation
;
701 if (m
->n_operations
>= OPERATIONS_MAX
)
702 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing operations.");
704 r
= sd_bus_message_read(message
, "s", &mm
);
708 if (streq(mm
, "all"))
710 else if (streq(mm
, "hidden"))
711 mode
= REMOVE_HIDDEN
;
713 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown mode '%s'.", mm
);
715 r
= bus_verify_polkit_async(
718 "org.freedesktop.machine1.manage-machines",
727 return 1; /* Will call us back */
729 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
730 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
732 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
733 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
735 result_fd
= open_tmpfile_unlinkable(NULL
, O_RDWR
|O_CLOEXEC
);
739 /* This might be a slow operation, run it asynchronously in a background process */
740 r
= safe_fork("(sd-clean)", FORK_RESET_SIGNALS
, &child
);
742 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
744 _cleanup_(image_hashmap_freep
) Hashmap
*images
= NULL
;
750 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
752 images
= hashmap_new(&string_hash_ops
);
758 r
= image_discover(IMAGE_MACHINE
, images
);
762 l
= write(result_fd
, &success
, sizeof(success
));
768 HASHMAP_FOREACH(image
, images
, i
) {
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] = -1;
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_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "New limit out of range");
846 r
= bus_verify_polkit_async(
849 "org.freedesktop.machine1.manage-machines",
858 return 1; /* Will call us back */
860 /* Set up the machine directory if necessary */
861 r
= setup_machine_directory(error
);
865 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit
);
867 r
= btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit
);
869 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
871 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust quota limit: %m");
873 return sd_bus_reply_method_return(message
, NULL
);
876 static int method_set_image_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
877 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_set_limit
);
880 static int method_map_from_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
881 _cleanup_fclose_
FILE *f
= NULL
;
882 Manager
*m
= userdata
;
883 const char *name
, *p
;
888 r
= sd_bus_message_read(message
, "su", &name
, &uid
);
892 if (!uid_is_valid(uid
))
893 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
895 machine
= hashmap_get(m
->machines
, name
);
897 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
899 if (machine
->class != MACHINE_CONTAINER
)
900 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
902 p
= procfs_file_alloca(machine
->leader
, "uid_map");
908 uid_t uid_base
, uid_shift
, uid_range
, converted
;
912 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
913 if (k
< 0 && feof(f
))
916 if (ferror(f
) && errno
> 0)
922 if (uid
< uid_base
|| uid
>= uid_base
+ uid_range
)
925 converted
= uid
- uid_base
+ uid_shift
;
926 if (!uid_is_valid(converted
))
927 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
929 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
932 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching user mappings.", name
);
935 static int method_map_to_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
936 Manager
*m
= userdata
;
942 r
= sd_bus_message_read(message
, "u", &uid
);
945 if (!uid_is_valid(uid
))
946 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
948 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "User " UID_FMT
" belongs to host UID range", uid
);
950 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
951 _cleanup_fclose_
FILE *f
= NULL
;
952 char p
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
954 if (machine
->class != MACHINE_CONTAINER
)
957 xsprintf(p
, "/proc/" UID_FMT
"/uid_map", machine
->leader
);
960 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
965 _cleanup_free_
char *o
= NULL
;
966 uid_t uid_base
, uid_shift
, uid_range
, converted
;
970 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
971 if (k
< 0 && feof(f
))
974 if (ferror(f
) && errno
> 0)
980 /* The private user namespace is disabled, ignoring. */
984 if (uid
< uid_shift
|| uid
>= uid_shift
+ uid_range
)
987 converted
= (uid
- uid_shift
+ uid_base
);
988 if (!uid_is_valid(converted
))
989 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
991 o
= machine_bus_path(machine
);
995 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
999 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "No matching user mapping for " UID_FMT
".", uid
);
1002 static int method_map_from_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1003 _cleanup_fclose_
FILE *f
= NULL
;
1004 Manager
*m
= groupdata
;
1005 const char *name
, *p
;
1010 r
= sd_bus_message_read(message
, "su", &name
, &gid
);
1014 if (!gid_is_valid(gid
))
1015 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1017 machine
= hashmap_get(m
->machines
, name
);
1019 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
1021 if (machine
->class != MACHINE_CONTAINER
)
1022 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
1024 p
= procfs_file_alloca(machine
->leader
, "gid_map");
1030 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1034 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1035 if (k
< 0 && feof(f
))
1038 if (ferror(f
) && errno
> 0)
1044 if (gid
< gid_base
|| gid
>= gid_base
+ gid_range
)
1047 converted
= gid
- gid_base
+ gid_shift
;
1048 if (!gid_is_valid(converted
))
1049 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1051 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
1054 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Machine '%s' has no matching group mappings.", name
);
1057 static int method_map_to_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1058 Manager
*m
= groupdata
;
1064 r
= sd_bus_message_read(message
, "u", &gid
);
1067 if (!gid_is_valid(gid
))
1068 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1070 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Group " GID_FMT
" belongs to host GID range", gid
);
1072 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1073 _cleanup_fclose_
FILE *f
= NULL
;
1074 char p
[STRLEN("/proc//gid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
1076 if (machine
->class != MACHINE_CONTAINER
)
1079 xsprintf(p
, "/proc/" GID_FMT
"/gid_map", machine
->leader
);
1082 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
1087 _cleanup_free_
char *o
= NULL
;
1088 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1092 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1093 if (k
< 0 && feof(f
))
1096 if (ferror(f
) && errno
> 0)
1102 /* The private user namespace is disabled, ignoring. */
1106 if (gid
< gid_shift
|| gid
>= gid_shift
+ gid_range
)
1109 converted
= (gid
- gid_shift
+ gid_base
);
1110 if (!gid_is_valid(converted
))
1111 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1113 o
= machine_bus_path(machine
);
1117 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1121 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "No matching group mapping for " GID_FMT
".", gid
);
1124 const sd_bus_vtable manager_vtable
[] = {
1125 SD_BUS_VTABLE_START(0),
1126 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path
, 0, 0),
1127 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage
, 0, 0),
1128 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit
, 0, 0),
1129 SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1130 SD_BUS_METHOD("GetImage", "s", "o", method_get_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1131 SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
1132 SD_BUS_METHOD("ListMachines", NULL
, "a(ssso)", method_list_machines
, SD_BUS_VTABLE_UNPRIVILEGED
),
1133 SD_BUS_METHOD("ListImages", NULL
, "a(ssbttto)", method_list_images
, SD_BUS_VTABLE_UNPRIVILEGED
),
1134 SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine
, 0),
1135 SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network
, 0),
1136 SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine
, 0),
1137 SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network
, 0),
1138 SD_BUS_METHOD("TerminateMachine", "s", NULL
, method_terminate_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1139 SD_BUS_METHOD("KillMachine", "ssi", NULL
, method_kill_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1140 SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1141 SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1142 SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty
, 0),
1143 SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1144 SD_BUS_METHOD("OpenMachineShell", "sssasas", "hs", method_open_machine_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1145 SD_BUS_METHOD("BindMountMachine", "sssbb", NULL
, method_bind_mount_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1146 SD_BUS_METHOD("CopyFromMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1147 SD_BUS_METHOD("CopyToMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1148 SD_BUS_METHOD("OpenMachineRootDirectory", "s", "h", method_open_machine_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1149 SD_BUS_METHOD("GetMachineUIDShift", "s", "u", method_get_machine_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1150 SD_BUS_METHOD("RemoveImage", "s", NULL
, method_remove_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1151 SD_BUS_METHOD("RenameImage", "ss", NULL
, method_rename_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1152 SD_BUS_METHOD("CloneImage", "ssb", NULL
, method_clone_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1153 SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL
, method_mark_image_read_only
, SD_BUS_VTABLE_UNPRIVILEGED
),
1154 SD_BUS_METHOD("GetImageHostname", "s", "s", method_get_image_hostname
, SD_BUS_VTABLE_UNPRIVILEGED
),
1155 SD_BUS_METHOD("GetImageMachineID", "s", "ay", method_get_image_machine_id
, SD_BUS_VTABLE_UNPRIVILEGED
),
1156 SD_BUS_METHOD("GetImageMachineInfo", "s", "a{ss}", method_get_image_machine_info
, SD_BUS_VTABLE_UNPRIVILEGED
),
1157 SD_BUS_METHOD("GetImageOSRelease", "s", "a{ss}", method_get_image_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1158 SD_BUS_METHOD("SetPoolLimit", "t", NULL
, method_set_pool_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1159 SD_BUS_METHOD("SetImageLimit", "st", NULL
, method_set_image_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1160 SD_BUS_METHOD("CleanPool", "s", "a(st)", method_clean_pool
, SD_BUS_VTABLE_UNPRIVILEGED
),
1161 SD_BUS_METHOD("MapFromMachineUser", "su", "u", method_map_from_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1162 SD_BUS_METHOD("MapToMachineUser", "u", "sou", method_map_to_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1163 SD_BUS_METHOD("MapFromMachineGroup", "su", "u", method_map_from_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1164 SD_BUS_METHOD("MapToMachineGroup", "u", "sou", method_map_to_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1165 SD_BUS_SIGNAL("MachineNew", "so", 0),
1166 SD_BUS_SIGNAL("MachineRemoved", "so", 0),
1170 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1171 const char *path
, *result
, *unit
;
1172 Manager
*m
= userdata
;
1180 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
1182 bus_log_parse_error(r
);
1186 machine
= hashmap_get(m
->machine_units
, unit
);
1190 if (streq_ptr(path
, machine
->scope_job
)) {
1191 machine
->scope_job
= mfree(machine
->scope_job
);
1193 if (machine
->started
) {
1194 if (streq(result
, "done"))
1195 machine_send_create_reply(machine
, NULL
);
1197 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
1199 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit %s failed with '%s'", unit
, result
);
1201 machine_send_create_reply(machine
, &e
);
1205 machine_save(machine
);
1208 machine_add_to_gc_queue(machine
);
1212 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1213 _cleanup_free_
char *unit
= NULL
;
1215 Manager
*m
= userdata
;
1222 path
= sd_bus_message_get_path(message
);
1226 r
= unit_name_from_dbus_path(path
, &unit
);
1227 if (r
== -EINVAL
) /* not for a unit */
1234 machine
= hashmap_get(m
->machine_units
, unit
);
1238 machine_add_to_gc_queue(machine
);
1242 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1243 const char *path
, *unit
;
1244 Manager
*m
= userdata
;
1251 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
1253 bus_log_parse_error(r
);
1257 machine
= hashmap_get(m
->machine_units
, unit
);
1261 machine_add_to_gc_queue(machine
);
1265 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1266 Manager
*m
= userdata
;
1274 r
= sd_bus_message_read(message
, "b", &b
);
1276 bus_log_parse_error(r
);
1282 /* systemd finished reloading, let's recheck all our machines */
1283 log_debug("System manager has been reloaded, rechecking machines...");
1285 HASHMAP_FOREACH(machine
, m
->machines
, i
)
1286 machine_add_to_gc_queue(machine
);
1291 int manager_start_scope(
1296 const char *description
,
1297 sd_bus_message
*more_properties
,
1298 sd_bus_error
*error
,
1301 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1308 r
= sd_bus_message_new_method_call(
1311 "org.freedesktop.systemd1",
1312 "/org/freedesktop/systemd1",
1313 "org.freedesktop.systemd1.Manager",
1314 "StartTransientUnit");
1318 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
1322 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1326 if (!isempty(slice
)) {
1327 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
1332 if (!isempty(description
)) {
1333 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
1338 r
= sd_bus_message_append(m
, "(sv)(sv)(sv)(sv)(sv)",
1339 "PIDs", "au", 1, pid
,
1341 "CollectMode", "s", "inactive-or-failed",
1343 "TasksMax", "t", UINT64_C(16384));
1347 if (more_properties
) {
1348 r
= sd_bus_message_copy(m
, more_properties
, true);
1353 r
= sd_bus_message_close_container(m
);
1357 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1361 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
1369 r
= sd_bus_message_read(reply
, "o", &j
);
1383 int manager_unref_unit(
1386 sd_bus_error
*error
) {
1391 return sd_bus_call_method(
1393 "org.freedesktop.systemd1",
1394 "/org/freedesktop/systemd1",
1395 "org.freedesktop.systemd1.Manager",
1403 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
1404 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1410 r
= sd_bus_call_method(
1412 "org.freedesktop.systemd1",
1413 "/org/freedesktop/systemd1",
1414 "org.freedesktop.systemd1.Manager",
1418 "ss", unit
, "fail");
1420 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1421 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
1426 sd_bus_error_free(error
);
1437 r
= sd_bus_message_read(reply
, "o", &j
);
1451 int manager_kill_unit(Manager
*manager
, const char *unit
, int signo
, sd_bus_error
*error
) {
1455 return sd_bus_call_method(
1457 "org.freedesktop.systemd1",
1458 "/org/freedesktop/systemd1",
1459 "org.freedesktop.systemd1.Manager",
1463 "ssi", unit
, "all", signo
);
1466 int manager_unit_is_active(Manager
*manager
, const char *unit
) {
1467 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1468 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1469 _cleanup_free_
char *path
= NULL
;
1476 path
= unit_dbus_path_from_name(unit
);
1480 r
= sd_bus_get_property(
1482 "org.freedesktop.systemd1",
1484 "org.freedesktop.systemd1.Unit",
1490 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1491 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1494 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1495 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
1501 r
= sd_bus_message_read(reply
, "s", &state
);
1505 return !STR_IN_SET(state
, "inactive", "failed");
1508 int manager_job_is_active(Manager
*manager
, const char *path
) {
1509 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1510 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1516 r
= sd_bus_get_property(
1518 "org.freedesktop.systemd1",
1520 "org.freedesktop.systemd1.Job",
1526 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1527 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1530 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
1536 /* We don't actually care about the state really. The fact
1537 * that we could read the job state is enough for us */
1542 int manager_get_machine_by_pid(Manager
*m
, pid_t pid
, Machine
**machine
) {
1550 mm
= hashmap_get(m
->machine_leaders
, PID_TO_PTR(pid
));
1552 _cleanup_free_
char *unit
= NULL
;
1554 r
= cg_pid_get_unit(pid
, &unit
);
1556 mm
= hashmap_get(m
->machine_units
, unit
);
1565 int manager_add_machine(Manager
*m
, const char *name
, Machine
**_machine
) {
1571 machine
= hashmap_get(m
->machines
, name
);
1573 machine
= machine_new(m
, _MACHINE_CLASS_INVALID
, name
);
1579 *_machine
= machine
;