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 "unit-name.h"
29 #include "user-util.h"
31 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_pool_path
, "s", "/var/lib/machines");
33 static int property_get_pool_usage(
36 const char *interface
,
38 sd_bus_message
*reply
,
40 sd_bus_error
*error
) {
42 _cleanup_close_
int fd
= -1;
43 uint64_t usage
= (uint64_t) -1;
48 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
52 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
56 return sd_bus_message_append(reply
, "t", usage
);
59 static int property_get_pool_limit(
62 const char *interface
,
64 sd_bus_message
*reply
,
66 sd_bus_error
*error
) {
68 _cleanup_close_
int fd
= -1;
69 uint64_t size
= (uint64_t) -1;
74 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
78 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
79 size
= q
.referenced_max
;
82 return sd_bus_message_append(reply
, "t", size
);
85 static int method_get_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
86 _cleanup_free_
char *p
= NULL
;
87 Manager
*m
= userdata
;
95 r
= sd_bus_message_read(message
, "s", &name
);
99 machine
= hashmap_get(m
->machines
, name
);
101 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
103 p
= machine_bus_path(machine
);
107 return sd_bus_reply_method_return(message
, "o", p
);
110 static int method_get_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
111 _cleanup_free_
char *p
= NULL
;
112 Manager
*m
= userdata
;
119 r
= sd_bus_message_read(message
, "s", &name
);
123 r
= image_find(IMAGE_MACHINE
, name
, NULL
);
125 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
129 p
= image_bus_path(name
);
133 return sd_bus_reply_method_return(message
, "o", p
);
136 static int method_get_machine_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
137 _cleanup_free_
char *p
= NULL
;
138 Manager
*m
= userdata
;
139 Machine
*machine
= NULL
;
146 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
148 r
= sd_bus_message_read(message
, "u", &pid
);
156 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
158 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
162 r
= sd_bus_creds_get_pid(creds
, &pid
);
167 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
171 return sd_bus_error_setf(error
, BUS_ERROR_NO_MACHINE_FOR_PID
, "PID "PID_FMT
" does not belong to any known machine", pid
);
173 p
= machine_bus_path(machine
);
177 return sd_bus_reply_method_return(message
, "o", p
);
180 static int method_list_machines(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
181 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
182 Manager
*m
= userdata
;
190 r
= sd_bus_message_new_method_return(message
, &reply
);
192 return sd_bus_error_set_errno(error
, r
);
194 r
= sd_bus_message_open_container(reply
, 'a', "(ssso)");
196 return sd_bus_error_set_errno(error
, r
);
198 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
199 _cleanup_free_
char *p
= NULL
;
201 p
= machine_bus_path(machine
);
205 r
= sd_bus_message_append(reply
, "(ssso)",
207 strempty(machine_class_to_string(machine
->class)),
211 return sd_bus_error_set_errno(error
, r
);
214 r
= sd_bus_message_close_container(reply
);
216 return sd_bus_error_set_errno(error
, r
);
218 return sd_bus_send(NULL
, reply
, NULL
);
221 static int method_create_or_register_machine(Manager
*manager
, sd_bus_message
*message
, bool read_network
, Machine
**_m
, sd_bus_error
*error
) {
222 const char *name
, *service
, *class, *root_directory
;
223 const int32_t *netif
= NULL
;
229 size_t n
, n_netif
= 0;
236 r
= sd_bus_message_read(message
, "s", &name
);
239 if (!machine_name_is_valid(name
))
240 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine name");
242 r
= sd_bus_message_read_array(message
, 'y', &v
, &n
);
250 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine ID parameter");
252 r
= sd_bus_message_read(message
, "ssus", &service
, &class, &leader
, &root_directory
);
259 r
= sd_bus_message_read_array(message
, 'i', (const void**) &netif
, &n_netif
);
263 n_netif
/= sizeof(int32_t);
265 for (i
= 0; i
< n_netif
; i
++) {
267 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid network interface index %i", netif
[i
]);
272 c
= _MACHINE_CLASS_INVALID
;
274 c
= machine_class_from_string(class);
276 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine class parameter");
280 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
282 if (!isempty(root_directory
) && !path_is_absolute(root_directory
))
283 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Root directory must be empty or an absolute path");
286 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
288 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
292 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
294 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
299 if (hashmap_get(manager
->machines
, name
))
300 return sd_bus_error_setf(error
, BUS_ERROR_MACHINE_EXISTS
, "Machine '%s' already exists", name
);
302 r
= manager_add_machine(manager
, name
, &m
);
310 if (!isempty(service
)) {
311 m
->service
= strdup(service
);
318 if (!isempty(root_directory
)) {
319 m
->root_directory
= strdup(root_directory
);
320 if (!m
->root_directory
) {
327 assert_cc(sizeof(int32_t) == sizeof(int));
328 m
->netif
= memdup(netif
, sizeof(int32_t) * n_netif
);
334 m
->n_netif
= n_netif
;
342 machine_add_to_gc_queue(m
);
346 static int method_create_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
347 Manager
*manager
= userdata
;
354 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
358 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
362 r
= machine_start(m
, message
, error
);
366 m
->create_message
= sd_bus_message_ref(message
);
370 machine_add_to_gc_queue(m
);
374 static int method_create_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
375 return method_create_machine_internal(message
, true, userdata
, error
);
378 static int method_create_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
379 return method_create_machine_internal(message
, false, userdata
, error
);
382 static int method_register_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
383 Manager
*manager
= userdata
;
384 _cleanup_free_
char *p
= NULL
;
391 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
395 r
= cg_pid_get_unit(m
->leader
, &m
->unit
);
397 r
= sd_bus_error_set_errnof(error
, r
,
398 "Failed to determine unit of process "PID_FMT
" : %m",
403 r
= machine_start(m
, NULL
, error
);
407 p
= machine_bus_path(m
);
413 return sd_bus_reply_method_return(message
, "o", p
);
416 machine_add_to_gc_queue(m
);
420 static int method_register_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
421 return method_register_machine_internal(message
, true, userdata
, error
);
424 static int method_register_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
425 return method_register_machine_internal(message
, false, userdata
, error
);
428 static int redirect_method_to_machine(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
437 r
= sd_bus_message_read(message
, "s", &name
);
439 return sd_bus_error_set_errno(error
, r
);
441 machine
= hashmap_get(m
->machines
, name
);
443 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
445 return method(message
, machine
, error
);
448 static int method_terminate_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
449 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_terminate
);
452 static int method_kill_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
453 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_kill
);
456 static int method_get_machine_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
457 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_addresses
);
460 static int method_get_machine_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
461 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_os_release
);
464 static int method_list_images(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
465 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
466 _cleanup_(image_hashmap_freep
) Hashmap
*images
= NULL
;
467 Manager
*m
= userdata
;
475 images
= hashmap_new(&string_hash_ops
);
479 r
= image_discover(IMAGE_MACHINE
, images
);
483 r
= sd_bus_message_new_method_return(message
, &reply
);
487 r
= sd_bus_message_open_container(reply
, 'a', "(ssbttto)");
491 HASHMAP_FOREACH(image
, images
, i
) {
492 _cleanup_free_
char *p
= NULL
;
494 p
= image_bus_path(image
->name
);
498 r
= sd_bus_message_append(reply
, "(ssbttto)",
500 image_type_to_string(image
->type
),
510 r
= sd_bus_message_close_container(reply
);
514 return sd_bus_send(NULL
, reply
, NULL
);
517 static int method_open_machine_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
518 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_pty
);
521 static int method_open_machine_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
522 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_login
);
525 static int method_open_machine_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
526 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_shell
);
529 static int method_bind_mount_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
530 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_bind_mount
);
533 static int method_copy_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
534 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_copy
);
537 static int method_open_machine_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
538 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_root_directory
);
541 static int method_get_machine_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
542 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_uid_shift
);
545 static int redirect_method_to_image(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
546 _cleanup_(image_unrefp
) Image
* i
= NULL
;
554 r
= sd_bus_message_read(message
, "s", &name
);
558 if (!image_name_is_valid(name
))
559 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
561 r
= image_find(IMAGE_MACHINE
, name
, &i
);
563 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
568 return method(message
, i
, error
);
571 static int method_remove_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
572 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_remove
);
575 static int method_rename_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
576 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_rename
);
579 static int method_clone_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
580 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_clone
);
583 static int method_mark_image_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
584 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_mark_read_only
);
587 static int method_get_image_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
588 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_hostname
);
591 static int method_get_image_machine_id(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
592 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_id
);
595 static int method_get_image_machine_info(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
596 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_info
);
599 static int method_get_image_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
600 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_os_release
);
603 static int clean_pool_done(Operation
*operation
, int ret
, sd_bus_error
*error
) {
604 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
605 _cleanup_fclose_
FILE *f
= NULL
;
611 assert(operation
->extra_fd
>= 0);
613 if (lseek(operation
->extra_fd
, 0, SEEK_SET
) == (off_t
) -1)
616 f
= fdopen(operation
->extra_fd
, "re");
620 operation
->extra_fd
= -1;
622 /* The resulting temporary file starts with a boolean value that indicates success or not. */
624 n
= fread(&success
, 1, sizeof(success
), f
);
625 if (n
!= sizeof(success
))
626 return ret
< 0 ? ret
: (errno
!= 0 ? -errno
: -EIO
);
629 _cleanup_free_
char *name
= NULL
;
631 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
632 * set to false followed by the name of the failed image. Let's try to read this and use it for the
633 * error message. If we can't read it, don't mind, and return the naked error. */
635 if (success
) /* The resulting temporary file could not be updated, ignore it. */
638 r
= read_nul_string(f
, &name
);
639 if (r
< 0 || isempty(name
)) /* Same here... */
642 return sd_bus_error_set_errnof(error
, ret
, "Failed to remove image %s: %m", name
);
647 r
= sd_bus_message_new_method_return(operation
->message
, &reply
);
651 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
655 /* On success the resulting temporary file will contain a list of image names that were removed followed by
656 * their size on disk. Let's read that and turn it into a bus message. */
658 _cleanup_free_
char *name
= NULL
;
661 r
= read_nul_string(f
, &name
);
664 if (isempty(name
)) /* reached the end */
668 n
= fread(&size
, 1, sizeof(size
), f
);
669 if (n
!= sizeof(size
))
670 return errno
!= 0 ? -errno
: -EIO
;
672 r
= sd_bus_message_append(reply
, "(st)", name
, size
);
677 r
= sd_bus_message_close_container(reply
);
681 return sd_bus_send(NULL
, reply
, NULL
);
684 static int method_clean_pool(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
690 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
691 _cleanup_close_
int result_fd
= -1;
692 Manager
*m
= userdata
;
693 Operation
*operation
;
700 if (m
->n_operations
>= OPERATIONS_MAX
)
701 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing operations.");
703 r
= sd_bus_message_read(message
, "s", &mm
);
707 if (streq(mm
, "all"))
709 else if (streq(mm
, "hidden"))
710 mode
= REMOVE_HIDDEN
;
712 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown mode '%s'.", mm
);
714 r
= bus_verify_polkit_async(
717 "org.freedesktop.machine1.manage-machines",
726 return 1; /* Will call us back */
728 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
729 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
731 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
732 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
734 result_fd
= open_tmpfile_unlinkable(NULL
, O_RDWR
|O_CLOEXEC
);
738 /* This might be a slow operation, run it asynchronously in a background process */
739 r
= safe_fork("(sd-clean)", FORK_RESET_SIGNALS
, &child
);
741 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
743 _cleanup_(image_hashmap_freep
) Hashmap
*images
= NULL
;
749 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
751 images
= hashmap_new(&string_hash_ops
);
757 r
= image_discover(IMAGE_MACHINE
, images
);
761 l
= write(result_fd
, &success
, sizeof(success
));
767 HASHMAP_FOREACH(image
, images
, i
) {
769 /* We can't remove vendor images (i.e. those in /usr) */
770 if (IMAGE_IS_VENDOR(image
))
773 if (IMAGE_IS_HOST(image
))
776 if (mode
== REMOVE_HIDDEN
&& !IMAGE_IS_HIDDEN(image
))
779 r
= image_remove(image
);
780 if (r
== -EBUSY
) /* keep images that are currently being used. */
783 /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */
785 (void) ftruncate(result_fd
, 0);
786 (void) lseek(result_fd
, 0, SEEK_SET
);
787 (void) write(result_fd
, &success
, sizeof(success
));
788 (void) write(result_fd
, image
->name
, strlen(image
->name
)+1);
792 l
= write(result_fd
, image
->name
, strlen(image
->name
)+1);
798 l
= write(result_fd
, &image
->usage_exclusive
, sizeof(image
->usage_exclusive
));
805 result_fd
= safe_close(result_fd
);
809 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
813 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
815 /* The clean-up might take a while, hence install a watch on the child and return */
817 r
= operation_new(m
, NULL
, child
, message
, errno_pipe_fd
[0], &operation
);
819 (void) sigkill_wait(child
);
823 operation
->extra_fd
= result_fd
;
824 operation
->done
= clean_pool_done
;
827 errno_pipe_fd
[0] = -1;
832 static int method_set_pool_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
833 Manager
*m
= userdata
;
839 r
= sd_bus_message_read(message
, "t", &limit
);
842 if (!FILE_SIZE_VALID_OR_INFINITY(limit
))
843 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "New limit out of range");
845 r
= bus_verify_polkit_async(
848 "org.freedesktop.machine1.manage-machines",
857 return 1; /* Will call us back */
859 /* Set up the machine directory if necessary */
860 r
= setup_machine_directory(error
);
864 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit
);
866 r
= btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit
);
868 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
870 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust quota limit: %m");
872 return sd_bus_reply_method_return(message
, NULL
);
875 static int method_set_image_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
876 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_set_limit
);
879 static int method_map_from_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
880 _cleanup_fclose_
FILE *f
= NULL
;
881 Manager
*m
= userdata
;
882 const char *name
, *p
;
887 r
= sd_bus_message_read(message
, "su", &name
, &uid
);
891 if (!uid_is_valid(uid
))
892 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
894 machine
= hashmap_get(m
->machines
, name
);
896 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
898 if (machine
->class != MACHINE_CONTAINER
)
899 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
901 p
= procfs_file_alloca(machine
->leader
, "uid_map");
907 uid_t uid_base
, uid_shift
, uid_range
, converted
;
911 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
912 if (k
< 0 && feof(f
))
915 if (ferror(f
) && errno
> 0)
921 if (uid
< uid_base
|| uid
>= uid_base
+ uid_range
)
924 converted
= uid
- uid_base
+ uid_shift
;
925 if (!uid_is_valid(converted
))
926 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
928 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
931 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching user mappings.", name
);
934 static int method_map_to_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
935 Manager
*m
= userdata
;
941 r
= sd_bus_message_read(message
, "u", &uid
);
944 if (!uid_is_valid(uid
))
945 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
947 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "User " UID_FMT
" belongs to host UID range", uid
);
949 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
950 _cleanup_fclose_
FILE *f
= NULL
;
951 char p
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
953 if (machine
->class != MACHINE_CONTAINER
)
956 xsprintf(p
, "/proc/" UID_FMT
"/uid_map", machine
->leader
);
959 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
964 _cleanup_free_
char *o
= NULL
;
965 uid_t uid_base
, uid_shift
, uid_range
, converted
;
969 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
970 if (k
< 0 && feof(f
))
973 if (ferror(f
) && errno
> 0)
979 /* The private user namespace is disabled, ignoring. */
983 if (uid
< uid_shift
|| uid
>= uid_shift
+ uid_range
)
986 converted
= (uid
- uid_shift
+ uid_base
);
987 if (!uid_is_valid(converted
))
988 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
990 o
= machine_bus_path(machine
);
994 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
998 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "No matching user mapping for " UID_FMT
".", uid
);
1001 static int method_map_from_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1002 _cleanup_fclose_
FILE *f
= NULL
;
1003 Manager
*m
= groupdata
;
1004 const char *name
, *p
;
1009 r
= sd_bus_message_read(message
, "su", &name
, &gid
);
1013 if (!gid_is_valid(gid
))
1014 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1016 machine
= hashmap_get(m
->machines
, name
);
1018 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
1020 if (machine
->class != MACHINE_CONTAINER
)
1021 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
1023 p
= procfs_file_alloca(machine
->leader
, "gid_map");
1029 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1033 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1034 if (k
< 0 && feof(f
))
1037 if (ferror(f
) && errno
> 0)
1043 if (gid
< gid_base
|| gid
>= gid_base
+ gid_range
)
1046 converted
= gid
- gid_base
+ gid_shift
;
1047 if (!gid_is_valid(converted
))
1048 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1050 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
1053 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Machine '%s' has no matching group mappings.", name
);
1056 static int method_map_to_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1057 Manager
*m
= groupdata
;
1063 r
= sd_bus_message_read(message
, "u", &gid
);
1066 if (!gid_is_valid(gid
))
1067 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1069 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Group " GID_FMT
" belongs to host GID range", gid
);
1071 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1072 _cleanup_fclose_
FILE *f
= NULL
;
1073 char p
[STRLEN("/proc//gid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
1075 if (machine
->class != MACHINE_CONTAINER
)
1078 xsprintf(p
, "/proc/" GID_FMT
"/gid_map", machine
->leader
);
1081 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
1086 _cleanup_free_
char *o
= NULL
;
1087 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1091 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1092 if (k
< 0 && feof(f
))
1095 if (ferror(f
) && errno
> 0)
1101 /* The private user namespace is disabled, ignoring. */
1105 if (gid
< gid_shift
|| gid
>= gid_shift
+ gid_range
)
1108 converted
= (gid
- gid_shift
+ gid_base
);
1109 if (!gid_is_valid(converted
))
1110 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1112 o
= machine_bus_path(machine
);
1116 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1120 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "No matching group mapping for " GID_FMT
".", gid
);
1123 const sd_bus_vtable manager_vtable
[] = {
1124 SD_BUS_VTABLE_START(0),
1125 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path
, 0, 0),
1126 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage
, 0, 0),
1127 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit
, 0, 0),
1128 SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1129 SD_BUS_METHOD("GetImage", "s", "o", method_get_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1130 SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
1131 SD_BUS_METHOD("ListMachines", NULL
, "a(ssso)", method_list_machines
, SD_BUS_VTABLE_UNPRIVILEGED
),
1132 SD_BUS_METHOD("ListImages", NULL
, "a(ssbttto)", method_list_images
, SD_BUS_VTABLE_UNPRIVILEGED
),
1133 SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine
, 0),
1134 SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network
, 0),
1135 SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine
, 0),
1136 SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network
, 0),
1137 SD_BUS_METHOD("TerminateMachine", "s", NULL
, method_terminate_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1138 SD_BUS_METHOD("KillMachine", "ssi", NULL
, method_kill_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1139 SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1140 SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1141 SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty
, 0),
1142 SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1143 SD_BUS_METHOD("OpenMachineShell", "sssasas", "hs", method_open_machine_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1144 SD_BUS_METHOD("BindMountMachine", "sssbb", NULL
, method_bind_mount_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1145 SD_BUS_METHOD("CopyFromMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1146 SD_BUS_METHOD("CopyToMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1147 SD_BUS_METHOD("OpenMachineRootDirectory", "s", "h", method_open_machine_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1148 SD_BUS_METHOD("GetMachineUIDShift", "s", "u", method_get_machine_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1149 SD_BUS_METHOD("RemoveImage", "s", NULL
, method_remove_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1150 SD_BUS_METHOD("RenameImage", "ss", NULL
, method_rename_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1151 SD_BUS_METHOD("CloneImage", "ssb", NULL
, method_clone_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1152 SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL
, method_mark_image_read_only
, SD_BUS_VTABLE_UNPRIVILEGED
),
1153 SD_BUS_METHOD("GetImageHostname", "s", "s", method_get_image_hostname
, SD_BUS_VTABLE_UNPRIVILEGED
),
1154 SD_BUS_METHOD("GetImageMachineID", "s", "ay", method_get_image_machine_id
, SD_BUS_VTABLE_UNPRIVILEGED
),
1155 SD_BUS_METHOD("GetImageMachineInfo", "s", "a{ss}", method_get_image_machine_info
, SD_BUS_VTABLE_UNPRIVILEGED
),
1156 SD_BUS_METHOD("GetImageOSRelease", "s", "a{ss}", method_get_image_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1157 SD_BUS_METHOD("SetPoolLimit", "t", NULL
, method_set_pool_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1158 SD_BUS_METHOD("SetImageLimit", "st", NULL
, method_set_image_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1159 SD_BUS_METHOD("CleanPool", "s", "a(st)", method_clean_pool
, SD_BUS_VTABLE_UNPRIVILEGED
),
1160 SD_BUS_METHOD("MapFromMachineUser", "su", "u", method_map_from_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1161 SD_BUS_METHOD("MapToMachineUser", "u", "sou", method_map_to_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1162 SD_BUS_METHOD("MapFromMachineGroup", "su", "u", method_map_from_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1163 SD_BUS_METHOD("MapToMachineGroup", "u", "sou", method_map_to_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1164 SD_BUS_SIGNAL("MachineNew", "so", 0),
1165 SD_BUS_SIGNAL("MachineRemoved", "so", 0),
1169 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1170 const char *path
, *result
, *unit
;
1171 Manager
*m
= userdata
;
1179 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
1181 bus_log_parse_error(r
);
1185 machine
= hashmap_get(m
->machine_units
, unit
);
1189 if (streq_ptr(path
, machine
->scope_job
)) {
1190 machine
->scope_job
= mfree(machine
->scope_job
);
1192 if (machine
->started
) {
1193 if (streq(result
, "done"))
1194 machine_send_create_reply(machine
, NULL
);
1196 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
1198 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit %s failed with '%s'", unit
, result
);
1200 machine_send_create_reply(machine
, &e
);
1204 machine_save(machine
);
1207 machine_add_to_gc_queue(machine
);
1211 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1212 _cleanup_free_
char *unit
= NULL
;
1214 Manager
*m
= userdata
;
1221 path
= sd_bus_message_get_path(message
);
1225 r
= unit_name_from_dbus_path(path
, &unit
);
1226 if (r
== -EINVAL
) /* not for a unit */
1233 machine
= hashmap_get(m
->machine_units
, unit
);
1237 machine_add_to_gc_queue(machine
);
1241 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1242 const char *path
, *unit
;
1243 Manager
*m
= userdata
;
1250 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
1252 bus_log_parse_error(r
);
1256 machine
= hashmap_get(m
->machine_units
, unit
);
1260 machine_add_to_gc_queue(machine
);
1264 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1265 Manager
*m
= userdata
;
1273 r
= sd_bus_message_read(message
, "b", &b
);
1275 bus_log_parse_error(r
);
1281 /* systemd finished reloading, let's recheck all our machines */
1282 log_debug("System manager has been reloaded, rechecking machines...");
1284 HASHMAP_FOREACH(machine
, m
->machines
, i
)
1285 machine_add_to_gc_queue(machine
);
1290 int manager_start_scope(
1295 const char *description
,
1296 sd_bus_message
*more_properties
,
1297 sd_bus_error
*error
,
1300 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1307 r
= sd_bus_message_new_method_call(
1310 "org.freedesktop.systemd1",
1311 "/org/freedesktop/systemd1",
1312 "org.freedesktop.systemd1.Manager",
1313 "StartTransientUnit");
1317 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
1321 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1325 if (!isempty(slice
)) {
1326 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
1331 if (!isempty(description
)) {
1332 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
1337 r
= sd_bus_message_append(m
, "(sv)(sv)(sv)(sv)(sv)",
1338 "PIDs", "au", 1, pid
,
1340 "CollectMode", "s", "inactive-or-failed",
1342 "TasksMax", "t", UINT64_C(16384));
1346 if (more_properties
) {
1347 r
= sd_bus_message_copy(m
, more_properties
, true);
1352 r
= sd_bus_message_close_container(m
);
1356 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1360 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
1368 r
= sd_bus_message_read(reply
, "o", &j
);
1382 int manager_unref_unit(
1385 sd_bus_error
*error
) {
1390 return sd_bus_call_method(
1392 "org.freedesktop.systemd1",
1393 "/org/freedesktop/systemd1",
1394 "org.freedesktop.systemd1.Manager",
1402 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
1403 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1409 r
= sd_bus_call_method(
1411 "org.freedesktop.systemd1",
1412 "/org/freedesktop/systemd1",
1413 "org.freedesktop.systemd1.Manager",
1417 "ss", unit
, "fail");
1419 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1420 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
1425 sd_bus_error_free(error
);
1436 r
= sd_bus_message_read(reply
, "o", &j
);
1450 int manager_kill_unit(Manager
*manager
, const char *unit
, int signo
, sd_bus_error
*error
) {
1454 return sd_bus_call_method(
1456 "org.freedesktop.systemd1",
1457 "/org/freedesktop/systemd1",
1458 "org.freedesktop.systemd1.Manager",
1462 "ssi", unit
, "all", signo
);
1465 int manager_unit_is_active(Manager
*manager
, const char *unit
) {
1466 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1467 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1468 _cleanup_free_
char *path
= NULL
;
1475 path
= unit_dbus_path_from_name(unit
);
1479 r
= sd_bus_get_property(
1481 "org.freedesktop.systemd1",
1483 "org.freedesktop.systemd1.Unit",
1489 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1490 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1493 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1494 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
1500 r
= sd_bus_message_read(reply
, "s", &state
);
1504 return !STR_IN_SET(state
, "inactive", "failed");
1507 int manager_job_is_active(Manager
*manager
, const char *path
) {
1508 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1509 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1515 r
= sd_bus_get_property(
1517 "org.freedesktop.systemd1",
1519 "org.freedesktop.systemd1.Job",
1525 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1526 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1529 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
1535 /* We don't actually care about the state really. The fact
1536 * that we could read the job state is enough for us */
1541 int manager_get_machine_by_pid(Manager
*m
, pid_t pid
, Machine
**machine
) {
1549 mm
= hashmap_get(m
->machine_leaders
, PID_TO_PTR(pid
));
1551 _cleanup_free_
char *unit
= NULL
;
1553 r
= cg_pid_get_unit(pid
, &unit
);
1555 mm
= hashmap_get(m
->machine_units
, unit
);
1564 int manager_add_machine(Manager
*m
, const char *name
, Machine
**_machine
) {
1570 machine
= hashmap_get(m
->machines
, name
);
1572 machine
= machine_new(m
, _MACHINE_CLASS_INVALID
, name
);
1578 *_machine
= machine
;