1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
14 #include "alloc-util.h"
15 #include "btrfs-util.h"
16 #include "bus-common-errors.h"
18 #include "cgroup-util.h"
21 #include "format-util.h"
22 #include "hostname-util.h"
23 #include "image-dbus.h"
25 #include "machine-dbus.h"
26 #include "machine-image.h"
27 #include "machine-pool.h"
29 #include "path-util.h"
30 #include "process-util.h"
31 #include "stdio-util.h"
33 #include "unit-name.h"
34 #include "user-util.h"
36 static BUS_DEFINE_PROPERTY_GET_GLOBAL(property_get_pool_path
, "s", "/var/lib/machines");
38 static int property_get_pool_usage(
41 const char *interface
,
43 sd_bus_message
*reply
,
45 sd_bus_error
*error
) {
47 _cleanup_close_
int fd
= -1;
48 uint64_t usage
= (uint64_t) -1;
54 /* We try to read the quota info from /var/lib/machines, as
55 * well as the usage of the loopback file
56 * /var/lib/machines.raw, and pick the larger value. */
58 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
62 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
66 if (stat("/var/lib/machines.raw", &st
) >= 0) {
67 if (usage
== (uint64_t) -1 || st
.st_blocks
* 512ULL > usage
)
68 usage
= st
.st_blocks
* 512ULL;
71 return sd_bus_message_append(reply
, "t", usage
);
74 static int property_get_pool_limit(
77 const char *interface
,
79 sd_bus_message
*reply
,
81 sd_bus_error
*error
) {
83 _cleanup_close_
int fd
= -1;
84 uint64_t size
= (uint64_t) -1;
90 /* We try to read the quota limit from /var/lib/machines, as
91 * well as the size of the loopback file
92 * /var/lib/machines.raw, and pick the smaller value. */
94 fd
= open("/var/lib/machines", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
98 if (btrfs_subvol_get_subtree_quota_fd(fd
, 0, &q
) >= 0)
99 size
= q
.referenced_max
;
102 if (stat("/var/lib/machines.raw", &st
) >= 0) {
103 if (size
== (uint64_t) -1 || (uint64_t) st
.st_size
< size
)
107 return sd_bus_message_append(reply
, "t", size
);
110 static int method_get_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
111 _cleanup_free_
char *p
= NULL
;
112 Manager
*m
= userdata
;
120 r
= sd_bus_message_read(message
, "s", &name
);
124 machine
= hashmap_get(m
->machines
, name
);
126 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
128 p
= machine_bus_path(machine
);
132 return sd_bus_reply_method_return(message
, "o", p
);
135 static int method_get_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
136 _cleanup_free_
char *p
= NULL
;
137 Manager
*m
= userdata
;
144 r
= sd_bus_message_read(message
, "s", &name
);
148 r
= image_find(IMAGE_MACHINE
, name
, NULL
);
150 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
154 p
= image_bus_path(name
);
158 return sd_bus_reply_method_return(message
, "o", p
);
161 static int method_get_machine_by_pid(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
162 _cleanup_free_
char *p
= NULL
;
163 Manager
*m
= userdata
;
164 Machine
*machine
= NULL
;
171 assert_cc(sizeof(pid_t
) == sizeof(uint32_t));
173 r
= sd_bus_message_read(message
, "u", &pid
);
181 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
183 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
187 r
= sd_bus_creds_get_pid(creds
, &pid
);
192 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
196 return sd_bus_error_setf(error
, BUS_ERROR_NO_MACHINE_FOR_PID
, "PID "PID_FMT
" does not belong to any known machine", pid
);
198 p
= machine_bus_path(machine
);
202 return sd_bus_reply_method_return(message
, "o", p
);
205 static int method_list_machines(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
206 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
207 Manager
*m
= userdata
;
215 r
= sd_bus_message_new_method_return(message
, &reply
);
217 return sd_bus_error_set_errno(error
, r
);
219 r
= sd_bus_message_open_container(reply
, 'a', "(ssso)");
221 return sd_bus_error_set_errno(error
, r
);
223 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
224 _cleanup_free_
char *p
= NULL
;
226 p
= machine_bus_path(machine
);
230 r
= sd_bus_message_append(reply
, "(ssso)",
232 strempty(machine_class_to_string(machine
->class)),
236 return sd_bus_error_set_errno(error
, r
);
239 r
= sd_bus_message_close_container(reply
);
241 return sd_bus_error_set_errno(error
, r
);
243 return sd_bus_send(NULL
, reply
, NULL
);
246 static int method_create_or_register_machine(Manager
*manager
, sd_bus_message
*message
, bool read_network
, Machine
**_m
, sd_bus_error
*error
) {
247 const char *name
, *service
, *class, *root_directory
;
248 const int32_t *netif
= NULL
;
254 size_t n
, n_netif
= 0;
261 r
= sd_bus_message_read(message
, "s", &name
);
264 if (!machine_name_is_valid(name
))
265 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine name");
267 r
= sd_bus_message_read_array(message
, 'y', &v
, &n
);
275 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine ID parameter");
277 r
= sd_bus_message_read(message
, "ssus", &service
, &class, &leader
, &root_directory
);
284 r
= sd_bus_message_read_array(message
, 'i', (const void**) &netif
, &n_netif
);
288 n_netif
/= sizeof(int32_t);
290 for (i
= 0; i
< n_netif
; i
++) {
292 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid network interface index %i", netif
[i
]);
297 c
= _MACHINE_CLASS_INVALID
;
299 c
= machine_class_from_string(class);
301 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid machine class parameter");
305 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid leader PID");
307 if (!isempty(root_directory
) && !path_is_absolute(root_directory
))
308 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Root directory must be empty or an absolute path");
311 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
313 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
317 assert_cc(sizeof(uint32_t) == sizeof(pid_t
));
319 r
= sd_bus_creds_get_pid(creds
, (pid_t
*) &leader
);
324 if (hashmap_get(manager
->machines
, name
))
325 return sd_bus_error_setf(error
, BUS_ERROR_MACHINE_EXISTS
, "Machine '%s' already exists", name
);
327 r
= manager_add_machine(manager
, name
, &m
);
335 if (!isempty(service
)) {
336 m
->service
= strdup(service
);
343 if (!isempty(root_directory
)) {
344 m
->root_directory
= strdup(root_directory
);
345 if (!m
->root_directory
) {
352 assert_cc(sizeof(int32_t) == sizeof(int));
353 m
->netif
= memdup(netif
, sizeof(int32_t) * n_netif
);
359 m
->n_netif
= n_netif
;
367 machine_add_to_gc_queue(m
);
371 static int method_create_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
372 Manager
*manager
= userdata
;
379 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
383 r
= sd_bus_message_enter_container(message
, 'a', "(sv)");
387 r
= machine_start(m
, message
, error
);
391 m
->create_message
= sd_bus_message_ref(message
);
395 machine_add_to_gc_queue(m
);
399 static int method_create_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
400 return method_create_machine_internal(message
, true, userdata
, error
);
403 static int method_create_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
404 return method_create_machine_internal(message
, false, userdata
, error
);
407 static int method_register_machine_internal(sd_bus_message
*message
, bool read_network
, void *userdata
, sd_bus_error
*error
) {
408 Manager
*manager
= userdata
;
409 _cleanup_free_
char *p
= NULL
;
416 r
= method_create_or_register_machine(manager
, message
, read_network
, &m
, error
);
420 r
= cg_pid_get_unit(m
->leader
, &m
->unit
);
422 r
= sd_bus_error_set_errnof(error
, r
,
423 "Failed to determine unit of process "PID_FMT
" : %m",
428 r
= machine_start(m
, NULL
, error
);
432 p
= machine_bus_path(m
);
438 return sd_bus_reply_method_return(message
, "o", p
);
441 machine_add_to_gc_queue(m
);
445 static int method_register_machine_with_network(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
446 return method_register_machine_internal(message
, true, userdata
, error
);
449 static int method_register_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
450 return method_register_machine_internal(message
, false, userdata
, error
);
453 static int redirect_method_to_machine(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
462 r
= sd_bus_message_read(message
, "s", &name
);
464 return sd_bus_error_set_errno(error
, r
);
466 machine
= hashmap_get(m
->machines
, name
);
468 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
470 return method(message
, machine
, error
);
473 static int method_terminate_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
474 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_terminate
);
477 static int method_kill_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
478 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_kill
);
481 static int method_get_machine_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
482 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_addresses
);
485 static int method_get_machine_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
486 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_os_release
);
489 static int method_list_images(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
490 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
491 _cleanup_(image_hashmap_freep
) Hashmap
*images
= NULL
;
492 Manager
*m
= userdata
;
500 images
= hashmap_new(&string_hash_ops
);
504 r
= image_discover(IMAGE_MACHINE
, images
);
508 r
= sd_bus_message_new_method_return(message
, &reply
);
512 r
= sd_bus_message_open_container(reply
, 'a', "(ssbttto)");
516 HASHMAP_FOREACH(image
, images
, i
) {
517 _cleanup_free_
char *p
= NULL
;
519 p
= image_bus_path(image
->name
);
523 r
= sd_bus_message_append(reply
, "(ssbttto)",
525 image_type_to_string(image
->type
),
535 r
= sd_bus_message_close_container(reply
);
539 return sd_bus_send(NULL
, reply
, NULL
);
542 static int method_open_machine_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
543 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_pty
);
546 static int method_open_machine_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
547 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_login
);
550 static int method_open_machine_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
551 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_shell
);
554 static int method_bind_mount_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
555 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_bind_mount
);
558 static int method_copy_machine(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
559 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_copy
);
562 static int method_open_machine_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
563 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_open_root_directory
);
566 static int method_get_machine_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
567 return redirect_method_to_machine(message
, userdata
, error
, bus_machine_method_get_uid_shift
);
570 static int redirect_method_to_image(sd_bus_message
*message
, Manager
*m
, sd_bus_error
*error
, sd_bus_message_handler_t method
) {
571 _cleanup_(image_unrefp
) Image
* i
= NULL
;
579 r
= sd_bus_message_read(message
, "s", &name
);
583 if (!image_name_is_valid(name
))
584 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Image name '%s' is invalid.", name
);
586 r
= image_find(IMAGE_MACHINE
, name
, &i
);
588 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_IMAGE
, "No image '%s' known", name
);
593 return method(message
, i
, error
);
596 static int method_remove_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
597 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_remove
);
600 static int method_rename_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
601 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_rename
);
604 static int method_clone_image(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
605 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_clone
);
608 static int method_mark_image_read_only(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
609 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_mark_read_only
);
612 static int method_get_image_hostname(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
613 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_hostname
);
616 static int method_get_image_machine_id(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
617 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_id
);
620 static int method_get_image_machine_info(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
621 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_machine_info
);
624 static int method_get_image_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
625 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_get_os_release
);
628 static int clean_pool_done(Operation
*operation
, int ret
, sd_bus_error
*error
) {
629 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
630 _cleanup_fclose_
FILE *f
= NULL
;
636 assert(operation
->extra_fd
>= 0);
638 if (lseek(operation
->extra_fd
, 0, SEEK_SET
) == (off_t
) -1)
641 f
= fdopen(operation
->extra_fd
, "re");
645 operation
->extra_fd
= -1;
647 /* The resulting temporary file starts with a boolean value that indicates success or not. */
649 n
= fread(&success
, 1, sizeof(success
), f
);
650 if (n
!= sizeof(success
))
651 return ret
< 0 ? ret
: (errno
!= 0 ? -errno
: -EIO
);
654 _cleanup_free_
char *name
= NULL
;
656 /* The clean-up operation failed. In this case the resulting temporary file should contain a boolean
657 * set to false followed by the name of the failed image. Let's try to read this and use it for the
658 * error message. If we can't read it, don't mind, and return the naked error. */
660 if (success
) /* The resulting temporary file could not be updated, ignore it. */
663 r
= read_nul_string(f
, &name
);
664 if (r
< 0 || isempty(name
)) /* Same here... */
667 return sd_bus_error_set_errnof(error
, ret
, "Failed to remove image %s: %m", name
);
672 r
= sd_bus_message_new_method_return(operation
->message
, &reply
);
676 r
= sd_bus_message_open_container(reply
, 'a', "(st)");
680 /* On success the resulting temporary file will contain a list of image names that were removed followed by
681 * their size on disk. Let's read that and turn it into a bus message. */
683 _cleanup_free_
char *name
= NULL
;
686 r
= read_nul_string(f
, &name
);
689 if (isempty(name
)) /* reached the end */
693 n
= fread(&size
, 1, sizeof(size
), f
);
694 if (n
!= sizeof(size
))
695 return errno
!= 0 ? -errno
: -EIO
;
697 r
= sd_bus_message_append(reply
, "(st)", name
, size
);
702 r
= sd_bus_message_close_container(reply
);
706 return sd_bus_send(NULL
, reply
, NULL
);
709 static int method_clean_pool(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
715 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
716 _cleanup_close_
int result_fd
= -1;
717 Manager
*m
= userdata
;
718 Operation
*operation
;
725 if (m
->n_operations
>= OPERATIONS_MAX
)
726 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing operations.");
728 r
= sd_bus_message_read(message
, "s", &mm
);
732 if (streq(mm
, "all"))
734 else if (streq(mm
, "hidden"))
735 mode
= REMOVE_HIDDEN
;
737 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Unknown mode '%s'.", mm
);
739 r
= bus_verify_polkit_async(
742 "org.freedesktop.machine1.manage-machines",
751 return 1; /* Will call us back */
753 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
754 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
756 /* Create a temporary file we can dump information about deleted images into. We use a temporary file for this
757 * instead of a pipe or so, since this might grow quit large in theory and we don't want to process this
759 result_fd
= open_tmpfile_unlinkable(NULL
, O_RDWR
|O_CLOEXEC
);
763 /* This might be a slow operation, run it asynchronously in a background process */
764 r
= safe_fork("(sd-clean)", FORK_RESET_SIGNALS
, &child
);
766 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
768 _cleanup_(image_hashmap_freep
) Hashmap
*images
= NULL
;
774 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
776 images
= hashmap_new(&string_hash_ops
);
782 r
= image_discover(IMAGE_MACHINE
, images
);
786 l
= write(result_fd
, &success
, sizeof(success
));
792 HASHMAP_FOREACH(image
, images
, i
) {
794 /* We can't remove vendor images (i.e. those in /usr) */
795 if (IMAGE_IS_VENDOR(image
))
798 if (IMAGE_IS_HOST(image
))
801 if (mode
== REMOVE_HIDDEN
&& !IMAGE_IS_HIDDEN(image
))
804 r
= image_remove(image
);
805 if (r
== -EBUSY
) /* keep images that are currently being used. */
808 /* If the operation failed, let's override everything we wrote, and instead write there at which image we failed. */
810 (void) ftruncate(result_fd
, 0);
811 (void) lseek(result_fd
, 0, SEEK_SET
);
812 (void) write(result_fd
, &success
, sizeof(success
));
813 (void) write(result_fd
, image
->name
, strlen(image
->name
)+1);
817 l
= write(result_fd
, image
->name
, strlen(image
->name
)+1);
823 l
= write(result_fd
, &image
->usage_exclusive
, sizeof(image
->usage_exclusive
));
830 result_fd
= safe_close(result_fd
);
834 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
838 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
840 /* The clean-up might take a while, hence install a watch on the child and return */
842 r
= operation_new(m
, NULL
, child
, message
, errno_pipe_fd
[0], &operation
);
844 (void) sigkill_wait(child
);
848 operation
->extra_fd
= result_fd
;
849 operation
->done
= clean_pool_done
;
852 errno_pipe_fd
[0] = -1;
857 static int method_set_pool_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
858 Manager
*m
= userdata
;
864 r
= sd_bus_message_read(message
, "t", &limit
);
867 if (!FILE_SIZE_VALID_OR_INFINITY(limit
))
868 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "New limit out of range");
870 r
= bus_verify_polkit_async(
873 "org.freedesktop.machine1.manage-machines",
882 return 1; /* Will call us back */
884 /* Set up the machine directory if necessary */
885 r
= setup_machine_directory(limit
, error
);
889 /* Resize the backing loopback device, if there is one, except if we asked to drop any limit */
890 if (limit
!= (uint64_t) -1) {
891 r
= btrfs_resize_loopback("/var/lib/machines", limit
, false);
893 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
894 if (r
< 0 && r
!= -ENODEV
) /* ignore ENODEV, as that's what is returned if the file system is not on loopback */
895 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust loopback limit: %m");
898 (void) btrfs_qgroup_set_limit("/var/lib/machines", 0, limit
);
900 r
= btrfs_subvol_set_subtree_quota_limit("/var/lib/machines", 0, limit
);
902 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Quota is only supported on btrfs.");
904 return sd_bus_error_set_errnof(error
, r
, "Failed to adjust quota limit: %m");
906 return sd_bus_reply_method_return(message
, NULL
);
909 static int method_set_image_limit(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
910 return redirect_method_to_image(message
, userdata
, error
, bus_image_method_set_limit
);
913 static int method_map_from_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
914 _cleanup_fclose_
FILE *f
= NULL
;
915 Manager
*m
= userdata
;
916 const char *name
, *p
;
921 r
= sd_bus_message_read(message
, "su", &name
, &uid
);
925 if (!uid_is_valid(uid
))
926 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
928 machine
= hashmap_get(m
->machines
, name
);
930 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
932 if (machine
->class != MACHINE_CONTAINER
)
933 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
935 p
= procfs_file_alloca(machine
->leader
, "uid_map");
941 uid_t uid_base
, uid_shift
, uid_range
, converted
;
945 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
946 if (k
< 0 && feof(f
))
949 if (ferror(f
) && errno
> 0)
955 if (uid
< uid_base
|| uid
>= uid_base
+ uid_range
)
958 converted
= uid
- uid_base
+ uid_shift
;
959 if (!uid_is_valid(converted
))
960 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
962 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
965 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "Machine '%s' has no matching user mappings.", name
);
968 static int method_map_to_machine_user(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
969 Manager
*m
= userdata
;
975 r
= sd_bus_message_read(message
, "u", &uid
);
978 if (!uid_is_valid(uid
))
979 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
981 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "User " UID_FMT
" belongs to host UID range", uid
);
983 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
984 _cleanup_fclose_
FILE *f
= NULL
;
985 char p
[STRLEN("/proc//uid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
987 if (machine
->class != MACHINE_CONTAINER
)
990 xsprintf(p
, "/proc/" UID_FMT
"/uid_map", machine
->leader
);
993 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
998 _cleanup_free_
char *o
= NULL
;
999 uid_t uid_base
, uid_shift
, uid_range
, converted
;
1003 k
= fscanf(f
, UID_FMT
" " UID_FMT
" " UID_FMT
, &uid_base
, &uid_shift
, &uid_range
);
1004 if (k
< 0 && feof(f
))
1007 if (ferror(f
) && errno
> 0)
1013 if (uid
< uid_shift
|| uid
>= uid_shift
+ uid_range
)
1016 converted
= (uid
- uid_shift
+ uid_base
);
1017 if (!uid_is_valid(converted
))
1018 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid user ID " UID_FMT
, uid
);
1020 o
= machine_bus_path(machine
);
1024 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1028 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_USER_MAPPING
, "No matching user mapping for " UID_FMT
".", uid
);
1031 static int method_map_from_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1032 _cleanup_fclose_
FILE *f
= NULL
;
1033 Manager
*m
= groupdata
;
1034 const char *name
, *p
;
1039 r
= sd_bus_message_read(message
, "su", &name
, &gid
);
1043 if (!gid_is_valid(gid
))
1044 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1046 machine
= hashmap_get(m
->machines
, name
);
1048 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_MACHINE
, "No machine '%s' known", name
);
1050 if (machine
->class != MACHINE_CONTAINER
)
1051 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Not supported for non-container machines.");
1053 p
= procfs_file_alloca(machine
->leader
, "gid_map");
1059 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1063 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1064 if (k
< 0 && feof(f
))
1067 if (ferror(f
) && errno
> 0)
1073 if (gid
< gid_base
|| gid
>= gid_base
+ gid_range
)
1076 converted
= gid
- gid_base
+ gid_shift
;
1077 if (!gid_is_valid(converted
))
1078 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1080 return sd_bus_reply_method_return(message
, "u", (uint32_t) converted
);
1083 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Machine '%s' has no matching group mappings.", name
);
1086 static int method_map_to_machine_group(sd_bus_message
*message
, void *groupdata
, sd_bus_error
*error
) {
1087 Manager
*m
= groupdata
;
1093 r
= sd_bus_message_read(message
, "u", &gid
);
1096 if (!gid_is_valid(gid
))
1097 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1099 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "Group " GID_FMT
" belongs to host GID range", gid
);
1101 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1102 _cleanup_fclose_
FILE *f
= NULL
;
1103 char p
[STRLEN("/proc//gid_map") + DECIMAL_STR_MAX(pid_t
) + 1];
1105 if (machine
->class != MACHINE_CONTAINER
)
1108 xsprintf(p
, "/proc/" GID_FMT
"/gid_map", machine
->leader
);
1111 log_warning_errno(errno
, "Failed to open %s, ignoring,", p
);
1116 _cleanup_free_
char *o
= NULL
;
1117 gid_t gid_base
, gid_shift
, gid_range
, converted
;
1121 k
= fscanf(f
, GID_FMT
" " GID_FMT
" " GID_FMT
, &gid_base
, &gid_shift
, &gid_range
);
1122 if (k
< 0 && feof(f
))
1125 if (ferror(f
) && errno
> 0)
1131 if (gid
< gid_shift
|| gid
>= gid_shift
+ gid_range
)
1134 converted
= (gid
- gid_shift
+ gid_base
);
1135 if (!gid_is_valid(converted
))
1136 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid group ID " GID_FMT
, gid
);
1138 o
= machine_bus_path(machine
);
1142 return sd_bus_reply_method_return(message
, "sou", machine
->name
, o
, (uint32_t) converted
);
1146 return sd_bus_error_setf(error
, BUS_ERROR_NO_SUCH_GROUP_MAPPING
, "No matching group mapping for " GID_FMT
".", gid
);
1149 const sd_bus_vtable manager_vtable
[] = {
1150 SD_BUS_VTABLE_START(0),
1151 SD_BUS_PROPERTY("PoolPath", "s", property_get_pool_path
, 0, 0),
1152 SD_BUS_PROPERTY("PoolUsage", "t", property_get_pool_usage
, 0, 0),
1153 SD_BUS_PROPERTY("PoolLimit", "t", property_get_pool_limit
, 0, 0),
1154 SD_BUS_METHOD("GetMachine", "s", "o", method_get_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1155 SD_BUS_METHOD("GetImage", "s", "o", method_get_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1156 SD_BUS_METHOD("GetMachineByPID", "u", "o", method_get_machine_by_pid
, SD_BUS_VTABLE_UNPRIVILEGED
),
1157 SD_BUS_METHOD("ListMachines", NULL
, "a(ssso)", method_list_machines
, SD_BUS_VTABLE_UNPRIVILEGED
),
1158 SD_BUS_METHOD("ListImages", NULL
, "a(ssbttto)", method_list_images
, SD_BUS_VTABLE_UNPRIVILEGED
),
1159 SD_BUS_METHOD("CreateMachine", "sayssusa(sv)", "o", method_create_machine
, 0),
1160 SD_BUS_METHOD("CreateMachineWithNetwork", "sayssusaia(sv)", "o", method_create_machine_with_network
, 0),
1161 SD_BUS_METHOD("RegisterMachine", "sayssus", "o", method_register_machine
, 0),
1162 SD_BUS_METHOD("RegisterMachineWithNetwork", "sayssusai", "o", method_register_machine_with_network
, 0),
1163 SD_BUS_METHOD("TerminateMachine", "s", NULL
, method_terminate_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1164 SD_BUS_METHOD("KillMachine", "ssi", NULL
, method_kill_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1165 SD_BUS_METHOD("GetMachineAddresses", "s", "a(iay)", method_get_machine_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1166 SD_BUS_METHOD("GetMachineOSRelease", "s", "a{ss}", method_get_machine_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1167 SD_BUS_METHOD("OpenMachinePTY", "s", "hs", method_open_machine_pty
, 0),
1168 SD_BUS_METHOD("OpenMachineLogin", "s", "hs", method_open_machine_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1169 SD_BUS_METHOD("OpenMachineShell", "sssasas", "hs", method_open_machine_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1170 SD_BUS_METHOD("BindMountMachine", "sssbb", NULL
, method_bind_mount_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1171 SD_BUS_METHOD("CopyFromMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1172 SD_BUS_METHOD("CopyToMachine", "sss", NULL
, method_copy_machine
, SD_BUS_VTABLE_UNPRIVILEGED
),
1173 SD_BUS_METHOD("OpenMachineRootDirectory", "s", "h", method_open_machine_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1174 SD_BUS_METHOD("GetMachineUIDShift", "s", "u", method_get_machine_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1175 SD_BUS_METHOD("RemoveImage", "s", NULL
, method_remove_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1176 SD_BUS_METHOD("RenameImage", "ss", NULL
, method_rename_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1177 SD_BUS_METHOD("CloneImage", "ssb", NULL
, method_clone_image
, SD_BUS_VTABLE_UNPRIVILEGED
),
1178 SD_BUS_METHOD("MarkImageReadOnly", "sb", NULL
, method_mark_image_read_only
, SD_BUS_VTABLE_UNPRIVILEGED
),
1179 SD_BUS_METHOD("GetImageHostname", "s", "s", method_get_image_hostname
, SD_BUS_VTABLE_UNPRIVILEGED
),
1180 SD_BUS_METHOD("GetImageMachineID", "s", "ay", method_get_image_machine_id
, SD_BUS_VTABLE_UNPRIVILEGED
),
1181 SD_BUS_METHOD("GetImageMachineInfo", "s", "a{ss}", method_get_image_machine_info
, SD_BUS_VTABLE_UNPRIVILEGED
),
1182 SD_BUS_METHOD("GetImageOSRelease", "s", "a{ss}", method_get_image_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1183 SD_BUS_METHOD("SetPoolLimit", "t", NULL
, method_set_pool_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1184 SD_BUS_METHOD("SetImageLimit", "st", NULL
, method_set_image_limit
, SD_BUS_VTABLE_UNPRIVILEGED
),
1185 SD_BUS_METHOD("CleanPool", "s", "a(st)", method_clean_pool
, SD_BUS_VTABLE_UNPRIVILEGED
),
1186 SD_BUS_METHOD("MapFromMachineUser", "su", "u", method_map_from_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1187 SD_BUS_METHOD("MapToMachineUser", "u", "sou", method_map_to_machine_user
, SD_BUS_VTABLE_UNPRIVILEGED
),
1188 SD_BUS_METHOD("MapFromMachineGroup", "su", "u", method_map_from_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1189 SD_BUS_METHOD("MapToMachineGroup", "u", "sou", method_map_to_machine_group
, SD_BUS_VTABLE_UNPRIVILEGED
),
1190 SD_BUS_SIGNAL("MachineNew", "so", 0),
1191 SD_BUS_SIGNAL("MachineRemoved", "so", 0),
1195 int match_job_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1196 const char *path
, *result
, *unit
;
1197 Manager
*m
= userdata
;
1205 r
= sd_bus_message_read(message
, "uoss", &id
, &path
, &unit
, &result
);
1207 bus_log_parse_error(r
);
1211 machine
= hashmap_get(m
->machine_units
, unit
);
1215 if (streq_ptr(path
, machine
->scope_job
)) {
1216 machine
->scope_job
= mfree(machine
->scope_job
);
1218 if (machine
->started
) {
1219 if (streq(result
, "done"))
1220 machine_send_create_reply(machine
, NULL
);
1222 _cleanup_(sd_bus_error_free
) sd_bus_error e
= SD_BUS_ERROR_NULL
;
1224 sd_bus_error_setf(&e
, BUS_ERROR_JOB_FAILED
, "Start job for unit %s failed with '%s'", unit
, result
);
1226 machine_send_create_reply(machine
, &e
);
1230 machine_save(machine
);
1233 machine_add_to_gc_queue(machine
);
1237 int match_properties_changed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1238 _cleanup_free_
char *unit
= NULL
;
1240 Manager
*m
= userdata
;
1247 path
= sd_bus_message_get_path(message
);
1251 r
= unit_name_from_dbus_path(path
, &unit
);
1252 if (r
== -EINVAL
) /* not for a unit */
1259 machine
= hashmap_get(m
->machine_units
, unit
);
1263 machine_add_to_gc_queue(machine
);
1267 int match_unit_removed(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1268 const char *path
, *unit
;
1269 Manager
*m
= userdata
;
1276 r
= sd_bus_message_read(message
, "so", &unit
, &path
);
1278 bus_log_parse_error(r
);
1282 machine
= hashmap_get(m
->machine_units
, unit
);
1286 machine_add_to_gc_queue(machine
);
1290 int match_reloading(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1291 Manager
*m
= userdata
;
1299 r
= sd_bus_message_read(message
, "b", &b
);
1301 bus_log_parse_error(r
);
1307 /* systemd finished reloading, let's recheck all our machines */
1308 log_debug("System manager has been reloaded, rechecking machines...");
1310 HASHMAP_FOREACH(machine
, m
->machines
, i
)
1311 machine_add_to_gc_queue(machine
);
1316 int manager_start_scope(
1321 const char *description
,
1322 sd_bus_message
*more_properties
,
1323 sd_bus_error
*error
,
1326 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
, *reply
= NULL
;
1333 r
= sd_bus_message_new_method_call(
1336 "org.freedesktop.systemd1",
1337 "/org/freedesktop/systemd1",
1338 "org.freedesktop.systemd1.Manager",
1339 "StartTransientUnit");
1343 r
= sd_bus_message_append(m
, "ss", strempty(scope
), "fail");
1347 r
= sd_bus_message_open_container(m
, 'a', "(sv)");
1351 if (!isempty(slice
)) {
1352 r
= sd_bus_message_append(m
, "(sv)", "Slice", "s", slice
);
1357 if (!isempty(description
)) {
1358 r
= sd_bus_message_append(m
, "(sv)", "Description", "s", description
);
1363 r
= sd_bus_message_append(m
, "(sv)", "PIDs", "au", 1, pid
);
1367 r
= sd_bus_message_append(m
, "(sv)", "Delegate", "b", 1);
1371 r
= sd_bus_message_append(m
, "(sv)", "TasksMax", "t", UINT64_C(16384));
1373 return bus_log_create_error(r
);
1375 if (more_properties
) {
1376 r
= sd_bus_message_copy(m
, more_properties
, true);
1381 r
= sd_bus_message_close_container(m
);
1385 r
= sd_bus_message_append(m
, "a(sa(sv))", 0);
1389 r
= sd_bus_call(manager
->bus
, m
, 0, error
, &reply
);
1397 r
= sd_bus_message_read(reply
, "o", &j
);
1411 int manager_stop_unit(Manager
*manager
, const char *unit
, sd_bus_error
*error
, char **job
) {
1412 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1418 r
= sd_bus_call_method(
1420 "org.freedesktop.systemd1",
1421 "/org/freedesktop/systemd1",
1422 "org.freedesktop.systemd1.Manager",
1426 "ss", unit
, "fail");
1428 if (sd_bus_error_has_name(error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1429 sd_bus_error_has_name(error
, BUS_ERROR_LOAD_FAILED
)) {
1434 sd_bus_error_free(error
);
1445 r
= sd_bus_message_read(reply
, "o", &j
);
1459 int manager_kill_unit(Manager
*manager
, const char *unit
, int signo
, sd_bus_error
*error
) {
1463 return sd_bus_call_method(
1465 "org.freedesktop.systemd1",
1466 "/org/freedesktop/systemd1",
1467 "org.freedesktop.systemd1.Manager",
1471 "ssi", unit
, "all", signo
);
1474 int manager_unit_is_active(Manager
*manager
, const char *unit
) {
1475 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1476 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1477 _cleanup_free_
char *path
= NULL
;
1484 path
= unit_dbus_path_from_name(unit
);
1488 r
= sd_bus_get_property(
1490 "org.freedesktop.systemd1",
1492 "org.freedesktop.systemd1.Unit",
1498 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1499 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1502 if (sd_bus_error_has_name(&error
, BUS_ERROR_NO_SUCH_UNIT
) ||
1503 sd_bus_error_has_name(&error
, BUS_ERROR_LOAD_FAILED
))
1509 r
= sd_bus_message_read(reply
, "s", &state
);
1513 return !STR_IN_SET(state
, "inactive", "failed");
1516 int manager_job_is_active(Manager
*manager
, const char *path
) {
1517 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1518 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1524 r
= sd_bus_get_property(
1526 "org.freedesktop.systemd1",
1528 "org.freedesktop.systemd1.Job",
1534 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_NO_REPLY
) ||
1535 sd_bus_error_has_name(&error
, SD_BUS_ERROR_DISCONNECTED
))
1538 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_UNKNOWN_OBJECT
))
1544 /* We don't actually care about the state really. The fact
1545 * that we could read the job state is enough for us */
1550 int manager_get_machine_by_pid(Manager
*m
, pid_t pid
, Machine
**machine
) {
1558 mm
= hashmap_get(m
->machine_leaders
, PID_TO_PTR(pid
));
1560 _cleanup_free_
char *unit
= NULL
;
1562 r
= cg_pid_get_unit(pid
, &unit
);
1564 mm
= hashmap_get(m
->machine_units
, unit
);
1573 int manager_add_machine(Manager
*m
, const char *name
, Machine
**_machine
) {
1579 machine
= hashmap_get(m
->machines
, name
);
1581 machine
= machine_new(m
, _MACHINE_CLASS_INVALID
, name
);
1587 *_machine
= machine
;