1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2011 Lennart Poettering
12 #include "alloc-util.h"
13 #include "btrfs-util.h"
14 #include "bus-common-errors.h"
16 #include "cgroup-util.h"
19 #include "format-util.h"
20 #include "hostname-util.h"
21 #include "image-dbus.h"
23 #include "machine-dbus.h"
24 #include "machine-image.h"
25 #include "machine-pool.h"
27 #include "path-util.h"
28 #include "process-util.h"
29 #include "stdio-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;
52 /* We try to read the quota info from /var/lib/machines, as
53 * well as the usage of the loopback file
54 * /var/lib/machines.raw, and pick the larger value. */
56 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
60 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
64 if (stat("/var/lib/machines.raw", &st
) >= 0) {
65 if (usage
== (uint64_t) -1 || st
.st_blocks
* 512ULL > usage
)
66 usage
= st
.st_blocks
* 512ULL;
69 return sd_bus_message_append(reply
, "t", usage
);
72 static int property_get_pool_limit(
75 const char *interface
,
77 sd_bus_message
*reply
,
79 sd_bus_error
*error
) {
81 _cleanup_close_
int fd
= -1;
82 uint64_t size
= (uint64_t) -1;
88 /* We try to read the quota limit from /var/lib/machines, as
89 * well as the size of the loopback file
90 * /var/lib/machines.raw, and pick the smaller value. */
92 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
96 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
97 size
= q
.referenced_max
;
100 if (stat("/var/lib/machines.raw", &st
) >= 0) {
101 if (size
== (uint64_t) -1 || (uint64_t) st
.st_size
< size
)
105 return sd_bus_message_append(reply
, "t", size
);
108 static int method_get_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
109 _cleanup_free_
char *p
= NULL
;
110 Manager
*m
= userdata
;
118 r
= sd_bus_message_read(message
, "s", &name
);
122 machine
= hashmap_get(m
->machines
, name
);
124 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
126 p
= machine_bus_path(machine
);
130 return sd_bus_reply_method_return(message
, "o", p
);
133 static int method_get_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
134 _cleanup_free_
char *p
= NULL
;
135 Manager
*m
= userdata
;
142 r
= sd_bus_message_read(message
, "s", &name
);
146 r
= image_find(IMAGE_MACHINE
, name
, NULL
);
148 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
152 p
= image_bus_path(name
);
156 return sd_bus_reply_method_return(message
, "o", p
);
159 static int method_get_machine_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
160 _cleanup_free_
char *p
= NULL
;
161 Manager
*m
= userdata
;
162 Machine
*machine
= NULL
;
169 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
171 r
= sd_bus_message_read(message
, "u", &pid
);
179 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
181 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
185 r
= sd_bus_creds_get_pid(creds
, &pid
);
190 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
194 return sd_bus_error_setf(error
, BUS_ERROR_NO_MACHINE_FOR_PID
, "PID "PID_FMT
" does not belong to any known machine", pid
);
196 p
= machine_bus_path(machine
);
200 return sd_bus_reply_method_return(message
, "o", p
);
203 static int method_list_machines(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
204 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
205 Manager
*m
= userdata
;
213 r
= sd_bus_message_new_method_return(message
, &reply
);
215 return sd_bus_error_set_errno(error
, r
);
217 r
= sd_bus_message_open_container(reply
, 'a', "(ssso)");
219 return sd_bus_error_set_errno(error
, r
);
221 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
222 _cleanup_free_
char *p
= NULL
;
224 p
= machine_bus_path(machine
);
228 r
= sd_bus_message_append(reply
, "(ssso)",
230 strempty(machine_class_to_string(machine
->class)),
234 return sd_bus_error_set_errno(error
, r
);
237 r
= sd_bus_message_close_container(reply
);
239 return sd_bus_error_set_errno(error
, r
);
241 return sd_bus_send(NULL
, reply
, NULL
);
244 static int method_create_or_register_machine(Manager
*manager
, sd_bus_message
*message
, bool read_network
, Machine
**_m
, sd_bus_error
*error
) {
245 const char *name
, *service
, *class, *root_directory
;
246 const int32_t *netif
= NULL
;
252 size_t n
, n_netif
= 0;
259 r
= sd_bus_message_read(message
, "s", &name
);
262 if (!machine_name_is_valid(name
))
263 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine name");
265 r
= sd_bus_message_read_array(message
, 'y', &v
, &n
);
273 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine ID parameter");
275 r
= sd_bus_message_read(message
, "ssus", &service
, &class, &leader
, &root_directory
);
282 r
= sd_bus_message_read_array(message
, 'i', (const void**) &netif
, &n_netif
);
286 n_netif
/= sizeof(int32_t);
288 for (i
= 0; i
< n_netif
; i
++) {
290 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid network interface index %i", netif
[i
]);
295 c
= _MACHINE_CLASS_INVALID
;
297 c
= machine_class_from_string(class);
299 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine class parameter");
303 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
305 if (!isempty(root_directory
) && !path_is_absolute(root_directory
))
306 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Root directory must be empty or an absolute path");
309 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
311 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
315 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
317 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
322 if (hashmap_get(manager
->machines
, name
))
323 return sd_bus_error_setf(error
, BUS_ERROR_MACHINE_EXISTS
, "Machine '%s' already exists", name
);
325 r
= manager_add_machine(manager
, name
, &m
);
333 if (!isempty(service
)) {
334 m
->service
= strdup(service
);
341 if (!isempty(root_directory
)) {
342 m
->root_directory
= strdup(root_directory
);
343 if (!m
->root_directory
) {
350 assert_cc(sizeof(int32_t) == sizeof(int));
351 m
->netif
= memdup(netif
, sizeof(int32_t) * n_netif
);
357 m
->n_netif
= n_netif
;
365 machine_add_to_gc_queue(m
);
369 static int method_create_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
370 Manager
*manager
= userdata
;
377 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
381 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
385 r
= machine_start(m
, message
, error
);
389 m
->create_message
= sd_bus_message_ref(message
);
393 machine_add_to_gc_queue(m
);
397 static int method_create_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
398 return method_create_machine_internal(message
, true, userdata
, error
);
401 static int method_create_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
402 return method_create_machine_internal(message
, false, userdata
, error
);
405 static int method_register_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
406 Manager
*manager
= userdata
;
407 _cleanup_free_
char *p
= NULL
;
414 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
418 r
= cg_pid_get_unit(m
->leader
, &m
->unit
);
420 r
= sd_bus_error_set_errnof(error
, r
,
421 "Failed to determine unit of process "PID_FMT
" : %m",
426 r
= machine_start(m
, NULL
, error
);
430 p
= machine_bus_path(m
);
436 return sd_bus_reply_method_return(message
, "o", p
);
439 machine_add_to_gc_queue(m
);
443 static int method_register_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
444 return method_register_machine_internal(message
, true, userdata
, error
);
447 static int method_register_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
448 return method_register_machine_internal(message
, false, userdata
, error
);
451 static int redirect_method_to_machine(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
460 r
= sd_bus_message_read(message
, "s", &name
);
462 return sd_bus_error_set_errno(error
, r
);
464 machine
= hashmap_get(m
->machines
, name
);
466 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
468 return method(message
, machine
, error
);
471 static int method_terminate_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
472 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_terminate
);
475 static int method_kill_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
476 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_kill
);
479 static int method_get_machine_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
480 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_addresses
);
483 static int method_get_machine_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
484 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_os_release
);
487 static int method_list_images(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
488 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
489 _cleanup_(image_hashmap_freep
) Hashmap
*images
= NULL
;
490 Manager
*m
= userdata
;
498 images
= hashmap_new(&string_hash_ops
);
502 r
= image_discover(IMAGE_MACHINE
, images
);
506 r
= sd_bus_message_new_method_return(message
, &reply
);
510 r
= sd_bus_message_open_container(reply
, 'a', "(ssbttto)");
514 HASHMAP_FOREACH(image
, images
, i
) {
515 _cleanup_free_
char *p
= NULL
;
517 p
= image_bus_path(image
->name
);
521 r
= sd_bus_message_append(reply
, "(ssbttto)",
523 image_type_to_string(image
->type
),
533 r
= sd_bus_message_close_container(reply
);
537 return sd_bus_send(NULL
, reply
, NULL
);
540 static int method_open_machine_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
541 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_pty
);
544 static int method_open_machine_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
545 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_login
);
548 static int method_open_machine_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
549 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_shell
);
552 static int method_bind_mount_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
553 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_bind_mount
);
556 static int method_copy_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
557 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_copy
);
560 static int method_open_machine_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
561 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_root_directory
);
564 static int method_get_machine_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
565 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_uid_shift
);
568 static int redirect_method_to_image(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
569 _cleanup_(image_unrefp
) Image
* i
= NULL
;
577 r
= sd_bus_message_read(message
, "s", &name
);
581 if (!image_name_is_valid(name
))
582 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
584 r
= image_find(IMAGE_MACHINE
, name
, &i
);
586 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
591 return method(message
, i
, error
);
594 static int method_remove_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
595 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_remove
);
598 static int method_rename_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
599 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_rename
);
602 static int method_clone_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
603 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_clone
);
606 static int method_mark_image_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
607 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_mark_read_only
);
610 static int method_get_image_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
611 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_hostname
);
614 static int method_get_image_machine_id(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
615 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_id
);
618 static int method_get_image_machine_info(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
619 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_info
);
622 static int method_get_image_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
623 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_os_release
);
626 static int clean_pool_done(Operation
*operation
, int ret
, sd_bus_error
*error
) {
627 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
628 _cleanup_fclose_
FILE *f
= NULL
;
634 assert(operation
->extra_fd
>= 0);
636 if (lseek(operation
->extra_fd
, 0, SEEK_SET
) == (off_t
) -1)
639 f
= fdopen(operation
->extra_fd
, "re");
643 operation
->extra_fd
= -1;
645 /* The resulting temporary file starts with a boolean value that indicates success or not. */
647 n
= fread(&success
, 1, sizeof(success
), f
);
648 if (n
!= sizeof(success
))
649 return ret
< 0 ? ret
: (errno
!= 0 ? -errno
: -EIO
);
652 _cleanup_free_
char *name
= NULL
;
654 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
655 * set to false followed by the name of the failed image. Let's try to read this and use it for the
656 * error message. If we can't read it, don't mind, and return the naked error. */
658 if (success
) /* The resulting temporary file could not be updated, ignore it. */
661 r
= read_nul_string(f
, &name
);
662 if (r
< 0 || isempty(name
)) /* Same here... */
665 return sd_bus_error_set_errnof(error
, ret
, "Failed to remove image %s: %m", name
);
670 r
= sd_bus_message_new_method_return(operation
->message
, &reply
);
674 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
678 /* On success the resulting temporary file will contain a list of image names that were removed followed by
679 * their size on disk. Let's read that and turn it into a bus message. */
681 _cleanup_free_
char *name
= NULL
;
684 r
= read_nul_string(f
, &name
);
687 if (isempty(name
)) /* reached the end */
691 n
= fread(&size
, 1, sizeof(size
), f
);
692 if (n
!= sizeof(size
))
693 return errno
!= 0 ? -errno
: -EIO
;
695 r
= sd_bus_message_append(reply
, "(st)", name
, size
);
700 r
= sd_bus_message_close_container(reply
);
704 return sd_bus_send(NULL
, reply
, NULL
);
707 static int method_clean_pool(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
713 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
714 _cleanup_close_
int result_fd
= -1;
715 Manager
*m
= userdata
;
716 Operation
*operation
;
723 if (m
->n_operations
>= OPERATIONS_MAX
)
724 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing operations.");
726 r
= sd_bus_message_read(message
, "s", &mm
);
730 if (streq(mm
, "all"))
732 else if (streq(mm
, "hidden"))
733 mode
= REMOVE_HIDDEN
;
735 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown mode '%s'.", mm
);
737 r
= bus_verify_polkit_async(
740 "org.freedesktop.machine1.manage-machines",
749 return 1; /* Will call us back */
751 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
752 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
754 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
755 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
757 result_fd
= open_tmpfile_unlinkable(NULL
, O_RDWR
|O_CLOEXEC
);
761 /* This might be a slow operation, run it asynchronously in a background process */
762 r
= safe_fork("(sd-clean)", FORK_RESET_SIGNALS
, &child
);
764 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
766 _cleanup_(image_hashmap_freep
) Hashmap
*images
= NULL
;
772 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
774 images
= hashmap_new(&string_hash_ops
);
780 r
= image_discover(IMAGE_MACHINE
, images
);
784 l
= write(result_fd
, &success
, sizeof(success
));
790 HASHMAP_FOREACH(image
, images
, i
) {
792 /* We can't remove vendor images (i.e. those in /usr) */
793 if (IMAGE_IS_VENDOR(image
))
796 if (IMAGE_IS_HOST(image
))
799 if (mode
== REMOVE_HIDDEN
&& !IMAGE_IS_HIDDEN(image
))
802 r
= image_remove(image
);
803 if (r
== -EBUSY
) /* keep images that are currently being used. */
806 /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */
808 (void) ftruncate(result_fd
, 0);
809 (void) lseek(result_fd
, 0, SEEK_SET
);
810 (void) write(result_fd
, &success
, sizeof(success
));
811 (void) write(result_fd
, image
->name
, strlen(image
->name
)+1);
815 l
= write(result_fd
, image
->name
, strlen(image
->name
)+1);
821 l
= write(result_fd
, &image
->usage_exclusive
, sizeof(image
->usage_exclusive
));
828 result_fd
= safe_close(result_fd
);
832 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
836 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
838 /* The clean-up might take a while, hence install a watch on the child and return */
840 r
= operation_new(m
, NULL
, child
, message
, errno_pipe_fd
[0], &operation
);
842 (void) sigkill_wait(child
);
846 operation
->extra_fd
= result_fd
;
847 operation
->done
= clean_pool_done
;
850 errno_pipe_fd
[0] = -1;
855 static int method_set_pool_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
856 Manager
*m
= userdata
;
862 r
= sd_bus_message_read(message
, "t", &limit
);
865 if (!FILE_SIZE_VALID_OR_INFINITY(limit
))
866 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "New limit out of range");
868 r
= bus_verify_polkit_async(
871 "org.freedesktop.machine1.manage-machines",
880 return 1; /* Will call us back */
882 /* Set up the machine directory if necessary */
883 r
= setup_machine_directory(limit
, error
);
887 /* Resize the backing loopback device, if there is one, except if we asked to drop any limit */
888 if (limit
!= (uint64_t) -1) {
889 r
= btrfs_resize_loopback("/var/lib/machines", limit
, false);
891 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
892 if (r
< 0 && r
!= -ENODEV
) /* ignore ENODEV, as that's what is returned if the file system is not on loopback */
893 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust loopback limit: %m");
896 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit
);
898 r
= btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit
);
900 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
902 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust quota limit: %m");
904 return sd_bus_reply_method_return(message
, NULL
);
907 static int method_set_image_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
908 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_set_limit
);
911 static int method_map_from_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
912 _cleanup_fclose_
FILE *f
= NULL
;
913 Manager
*m
= userdata
;
914 const char *name
, *p
;
919 r
= sd_bus_message_read(message
, "su", &name
, &uid
);
923 if (!uid_is_valid(uid
))
924 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
926 machine
= hashmap_get(m
->machines
, name
);
928 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
930 if (machine
->class != MACHINE_CONTAINER
)
931 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
933 p
= procfs_file_alloca(machine
->leader
, "uid_map");
939 uid_t uid_base
, uid_shift
, uid_range
, converted
;
943 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
944 if (k
< 0 && feof(f
))
947 if (ferror(f
) && errno
> 0)
953 if (uid
< uid_base
|| uid
>= uid_base
+ uid_range
)
956 converted
= uid
- uid_base
+ uid_shift
;
957 if (!uid_is_valid(converted
))
958 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
960 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
963 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching user mappings.", name
);
966 static int method_map_to_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
967 Manager
*m
= userdata
;
973 r
= sd_bus_message_read(message
, "u", &uid
);
976 if (!uid_is_valid(uid
))
977 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
979 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "User " UID_FMT
" belongs to host UID range", uid
);
981 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
982 _cleanup_fclose_
FILE *f
= NULL
;
983 char p
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
985 if (machine
->class != MACHINE_CONTAINER
)
988 xsprintf(p
, "/proc/" UID_FMT
"/uid_map", machine
->leader
);
991 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
996 _cleanup_free_
char *o
= NULL
;
997 uid_t uid_base
, uid_shift
, uid_range
, converted
;
1001 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
1002 if (k
< 0 && feof(f
))
1005 if (ferror(f
) && errno
> 0)
1011 /* The private user namespace is disabled, ignoring. */
1015 if (uid
< uid_shift
|| uid
>= uid_shift
+ uid_range
)
1018 converted
= (uid
- uid_shift
+ uid_base
);
1019 if (!uid_is_valid(converted
))
1020 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
1022 o
= machine_bus_path(machine
);
1026 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1030 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "No matching user mapping for " UID_FMT
".", uid
);
1033 static int method_map_from_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1034 _cleanup_fclose_
FILE *f
= NULL
;
1035 Manager
*m
= groupdata
;
1036 const char *name
, *p
;
1041 r
= sd_bus_message_read(message
, "su", &name
, &gid
);
1045 if (!gid_is_valid(gid
))
1046 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1048 machine
= hashmap_get(m
->machines
, name
);
1050 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
1052 if (machine
->class != MACHINE_CONTAINER
)
1053 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
1055 p
= procfs_file_alloca(machine
->leader
, "gid_map");
1061 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1065 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1066 if (k
< 0 && feof(f
))
1069 if (ferror(f
) && errno
> 0)
1075 if (gid
< gid_base
|| gid
>= gid_base
+ gid_range
)
1078 converted
= gid
- gid_base
+ gid_shift
;
1079 if (!gid_is_valid(converted
))
1080 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1082 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
1085 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Machine '%s' has no matching group mappings.", name
);
1088 static int method_map_to_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1089 Manager
*m
= groupdata
;
1095 r
= sd_bus_message_read(message
, "u", &gid
);
1098 if (!gid_is_valid(gid
))
1099 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1101 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Group " GID_FMT
" belongs to host GID range", gid
);
1103 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1104 _cleanup_fclose_
FILE *f
= NULL
;
1105 char p
[STRLEN("/proc//gid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
1107 if (machine
->class != MACHINE_CONTAINER
)
1110 xsprintf(p
, "/proc/" GID_FMT
"/gid_map", machine
->leader
);
1113 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
1118 _cleanup_free_
char *o
= NULL
;
1119 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1123 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1124 if (k
< 0 && feof(f
))
1127 if (ferror(f
) && errno
> 0)
1133 /* The private user namespace is disabled, ignoring. */
1137 if (gid
< gid_shift
|| gid
>= gid_shift
+ gid_range
)
1140 converted
= (gid
- gid_shift
+ gid_base
);
1141 if (!gid_is_valid(converted
))
1142 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1144 o
= machine_bus_path(machine
);
1148 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1152 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "No matching group mapping for " GID_FMT
".", gid
);
1155 const sd_bus_vtable manager_vtable
[] = {
1156 SD_BUS_VTABLE_START(0),
1157 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path
, 0, 0),
1158 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage
, 0, 0),
1159 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit
, 0, 0),
1160 SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1161 SD_BUS_METHOD("GetImage", "s", "o", method_get_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1162 SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
1163 SD_BUS_METHOD("ListMachines", NULL
, "a(ssso)", method_list_machines
, SD_BUS_VTABLE_UNPRIVILEGED
),
1164 SD_BUS_METHOD("ListImages", NULL
, "a(ssbttto)", method_list_images
, SD_BUS_VTABLE_UNPRIVILEGED
),
1165 SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine
, 0),
1166 SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network
, 0),
1167 SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine
, 0),
1168 SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network
, 0),
1169 SD_BUS_METHOD("TerminateMachine", "s", NULL
, method_terminate_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1170 SD_BUS_METHOD("KillMachine", "ssi", NULL
, method_kill_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1171 SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1172 SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1173 SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty
, 0),
1174 SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1175 SD_BUS_METHOD("OpenMachineShell", "sssasas", "hs", method_open_machine_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1176 SD_BUS_METHOD("BindMountMachine", "sssbb", NULL
, method_bind_mount_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1177 SD_BUS_METHOD("CopyFromMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1178 SD_BUS_METHOD("CopyToMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1179 SD_BUS_METHOD("OpenMachineRootDirectory", "s", "h", method_open_machine_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1180 SD_BUS_METHOD("GetMachineUIDShift", "s", "u", method_get_machine_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1181 SD_BUS_METHOD("RemoveImage", "s", NULL
, method_remove_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1182 SD_BUS_METHOD("RenameImage", "ss", NULL
, method_rename_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1183 SD_BUS_METHOD("CloneImage", "ssb", NULL
, method_clone_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1184 SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL
, method_mark_image_read_only
, SD_BUS_VTABLE_UNPRIVILEGED
),
1185 SD_BUS_METHOD("GetImageHostname", "s", "s", method_get_image_hostname
, SD_BUS_VTABLE_UNPRIVILEGED
),
1186 SD_BUS_METHOD("GetImageMachineID", "s", "ay", method_get_image_machine_id
, SD_BUS_VTABLE_UNPRIVILEGED
),
1187 SD_BUS_METHOD("GetImageMachineInfo", "s", "a{ss}", method_get_image_machine_info
, SD_BUS_VTABLE_UNPRIVILEGED
),
1188 SD_BUS_METHOD("GetImageOSRelease", "s", "a{ss}", method_get_image_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1189 SD_BUS_METHOD("SetPoolLimit", "t", NULL
, method_set_pool_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1190 SD_BUS_METHOD("SetImageLimit", "st", NULL
, method_set_image_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1191 SD_BUS_METHOD("CleanPool", "s", "a(st)", method_clean_pool
, SD_BUS_VTABLE_UNPRIVILEGED
),
1192 SD_BUS_METHOD("MapFromMachineUser", "su", "u", method_map_from_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1193 SD_BUS_METHOD("MapToMachineUser", "u", "sou", method_map_to_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1194 SD_BUS_METHOD("MapFromMachineGroup", "su", "u", method_map_from_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1195 SD_BUS_METHOD("MapToMachineGroup", "u", "sou", method_map_to_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1196 SD_BUS_SIGNAL("MachineNew", "so", 0),
1197 SD_BUS_SIGNAL("MachineRemoved", "so", 0),
1201 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1202 const char *path
, *result
, *unit
;
1203 Manager
*m
= userdata
;
1211 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
1213 bus_log_parse_error(r
);
1217 machine
= hashmap_get(m
->machine_units
, unit
);
1221 if (streq_ptr(path
, machine
->scope_job
)) {
1222 machine
->scope_job
= mfree(machine
->scope_job
);
1224 if (machine
->started
) {
1225 if (streq(result
, "done"))
1226 machine_send_create_reply(machine
, NULL
);
1228 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
1230 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit %s failed with '%s'", unit
, result
);
1232 machine_send_create_reply(machine
, &e
);
1236 machine_save(machine
);
1239 machine_add_to_gc_queue(machine
);
1243 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1244 _cleanup_free_
char *unit
= NULL
;
1246 Manager
*m
= userdata
;
1253 path
= sd_bus_message_get_path(message
);
1257 r
= unit_name_from_dbus_path(path
, &unit
);
1258 if (r
== -EINVAL
) /* not for a unit */
1265 machine
= hashmap_get(m
->machine_units
, unit
);
1269 machine_add_to_gc_queue(machine
);
1273 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1274 const char *path
, *unit
;
1275 Manager
*m
= userdata
;
1282 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
1284 bus_log_parse_error(r
);
1288 machine
= hashmap_get(m
->machine_units
, unit
);
1292 machine_add_to_gc_queue(machine
);
1296 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1297 Manager
*m
= userdata
;
1305 r
= sd_bus_message_read(message
, "b", &b
);
1307 bus_log_parse_error(r
);
1313 /* systemd finished reloading, let's recheck all our machines */
1314 log_debug("System manager has been reloaded, rechecking machines...");
1316 HASHMAP_FOREACH(machine
, m
->machines
, i
)
1317 machine_add_to_gc_queue(machine
);
1322 int manager_start_scope(
1327 const char *description
,
1328 sd_bus_message
*more_properties
,
1329 sd_bus_error
*error
,
1332 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1339 r
= sd_bus_message_new_method_call(
1342 "org.freedesktop.systemd1",
1343 "/org/freedesktop/systemd1",
1344 "org.freedesktop.systemd1.Manager",
1345 "StartTransientUnit");
1349 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
1353 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1357 if (!isempty(slice
)) {
1358 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
1363 if (!isempty(description
)) {
1364 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
1369 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
1373 r
= sd_bus_message_append(m
, "(sv)", "Delegate", "b", 1);
1377 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", UINT64_C(16384));
1379 return bus_log_create_error(r
);
1381 if (more_properties
) {
1382 r
= sd_bus_message_copy(m
, more_properties
, true);
1387 r
= sd_bus_message_close_container(m
);
1391 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1395 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
1403 r
= sd_bus_message_read(reply
, "o", &j
);
1417 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
1418 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1424 r
= sd_bus_call_method(
1426 "org.freedesktop.systemd1",
1427 "/org/freedesktop/systemd1",
1428 "org.freedesktop.systemd1.Manager",
1432 "ss", unit
, "fail");
1434 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1435 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
1440 sd_bus_error_free(error
);
1451 r
= sd_bus_message_read(reply
, "o", &j
);
1465 int manager_kill_unit(Manager
*manager
, const char *unit
, int signo
, sd_bus_error
*error
) {
1469 return sd_bus_call_method(
1471 "org.freedesktop.systemd1",
1472 "/org/freedesktop/systemd1",
1473 "org.freedesktop.systemd1.Manager",
1477 "ssi", unit
, "all", signo
);
1480 int manager_unit_is_active(Manager
*manager
, const char *unit
) {
1481 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1482 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1483 _cleanup_free_
char *path
= NULL
;
1490 path
= unit_dbus_path_from_name(unit
);
1494 r
= sd_bus_get_property(
1496 "org.freedesktop.systemd1",
1498 "org.freedesktop.systemd1.Unit",
1504 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1505 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1508 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1509 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
1515 r
= sd_bus_message_read(reply
, "s", &state
);
1519 return !STR_IN_SET(state
, "inactive", "failed");
1522 int manager_job_is_active(Manager
*manager
, const char *path
) {
1523 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1524 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1530 r
= sd_bus_get_property(
1532 "org.freedesktop.systemd1",
1534 "org.freedesktop.systemd1.Job",
1540 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1541 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1544 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
1550 /* We don't actually care about the state really. The fact
1551 * that we could read the job state is enough for us */
1556 int manager_get_machine_by_pid(Manager
*m
, pid_t pid
, Machine
**machine
) {
1564 mm
= hashmap_get(m
->machine_leaders
, PID_TO_PTR(pid
));
1566 _cleanup_free_
char *unit
= NULL
;
1568 r
= cg_pid_get_unit(pid
, &unit
);
1570 mm
= hashmap_get(m
->machine_units
, unit
);
1579 int manager_add_machine(Manager
*m
, const char *name
, Machine
**_machine
) {
1585 machine
= hashmap_get(m
->machines
, name
);
1587 machine
= machine_new(m
, _MACHINE_CLASS_INVALID
, name
);
1593 *_machine
= machine
;