1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
8 #include "bus-common-errors.h"
9 #include "bus-get-properties.h"
10 #include "bus-internal.h"
11 #include "bus-label.h"
12 #include "bus-locator.h"
13 #include "bus-polkit.h"
19 #include "format-util.h"
21 #include "in-addr-util.h"
23 #include "local-addresses.h"
24 #include "machine-dbus.h"
26 #include "missing_capability.h"
28 #include "mount-util.h"
29 #include "mountpoint-util.h"
30 #include "namespace-util.h"
32 #include "path-util.h"
33 #include "process-util.h"
34 #include "signal-util.h"
36 #include "terminal-util.h"
37 #include "tmpfile-util.h"
38 #include "user-util.h"
40 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class
, machine_class
, MachineClass
);
41 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", Machine
, machine_get_state
, machine_state_to_string
);
43 static int property_get_netif(
46 const char *interface
,
48 sd_bus_message
*reply
,
50 sd_bus_error
*error
) {
52 Machine
*m
= ASSERT_PTR(userdata
);
57 assert_cc(sizeof(int) == sizeof(int32_t));
59 return sd_bus_message_append_array(reply
, 'i', m
->netif
, m
->n_netif
* sizeof(int));
62 int bus_machine_method_unregister(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
63 Machine
*m
= ASSERT_PTR(userdata
);
68 const char *details
[] = {
74 r
= bus_verify_polkit_async(
77 "org.freedesktop.machine1.manage-machines",
81 &m
->manager
->polkit_registry
,
86 return 1; /* Will call us back */
88 r
= machine_finalize(m
);
92 return sd_bus_reply_method_return(message
, NULL
);
95 int bus_machine_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
96 Machine
*m
= ASSERT_PTR(userdata
);
101 const char *details
[] = {
107 r
= bus_verify_polkit_async(
110 "org.freedesktop.machine1.manage-machines",
114 &m
->manager
->polkit_registry
,
119 return 1; /* Will call us back */
125 return sd_bus_reply_method_return(message
, NULL
);
128 int bus_machine_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
129 Machine
*m
= ASSERT_PTR(userdata
);
137 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
144 who
= kill_who_from_string(swho
);
146 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
149 if (!SIGNAL_VALID(signo
))
150 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
152 const char *details
[] = {
158 r
= bus_verify_polkit_async(
161 "org.freedesktop.machine1.manage-machines",
165 &m
->manager
->polkit_registry
,
170 return 1; /* Will call us back */
172 r
= machine_kill(m
, who
, signo
);
176 return sd_bus_reply_method_return(message
, NULL
);
179 int bus_machine_method_get_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
180 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
181 Machine
*m
= ASSERT_PTR(userdata
);
186 r
= sd_bus_message_new_method_return(message
, &reply
);
190 r
= sd_bus_message_open_container(reply
, 'a', "(iay)");
197 _cleanup_free_
struct local_address
*addresses
= NULL
;
200 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
204 for (int i
= 0; i
< n
; i
++) {
205 r
= sd_bus_message_open_container(reply
, 'r', "iay");
209 r
= sd_bus_message_append(reply
, "i", addresses
[i
].family
);
213 r
= sd_bus_message_append_array(reply
, 'y', &addresses
[i
].address
, FAMILY_ADDRESS_SIZE(addresses
[i
].family
));
217 r
= sd_bus_message_close_container(reply
);
225 case MACHINE_CONTAINER
: {
226 _cleanup_close_pair_
int pair
[2] = PIPE_EBADF
;
227 _cleanup_free_
char *us
= NULL
, *them
= NULL
;
228 _cleanup_close_
int netns_fd
= -EBADF
;
232 r
= readlink_malloc("/proc/self/ns/net", &us
);
236 p
= procfs_file_alloca(m
->leader
.pid
, "ns/net");
237 r
= readlink_malloc(p
, &them
);
242 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_NETWORKING
, "Machine %s does not use private networking", m
->name
);
244 r
= namespace_open(m
->leader
.pid
, NULL
, NULL
, &netns_fd
, NULL
, NULL
);
248 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
251 r
= namespace_fork("(sd-addrns)", "(sd-addr)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
252 -1, -1, netns_fd
, -1, -1, &child
);
254 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
256 _cleanup_free_
struct local_address
*addresses
= NULL
;
257 struct local_address
*a
;
260 pair
[0] = safe_close(pair
[0]);
262 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
266 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
267 struct iovec iov
[2] = {
268 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
269 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
272 r
= writev(pair
[1], iov
, 2);
277 pair
[1] = safe_close(pair
[1]);
282 pair
[1] = safe_close(pair
[1]);
287 union in_addr_union in_addr
;
294 iov
[0] = IOVEC_MAKE(&family
, sizeof(family
));
295 iov
[1] = IOVEC_MAKE(&in_addr
, sizeof(in_addr
));
297 n
= recvmsg(pair
[0], &mh
, 0);
300 if ((size_t) n
< sizeof(family
))
303 r
= sd_bus_message_open_container(reply
, 'r', "iay");
307 r
= sd_bus_message_append(reply
, "i", family
);
314 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
317 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
321 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
324 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
330 r
= sd_bus_message_close_container(reply
);
335 r
= wait_for_terminate_and_check("(sd-addrns)", child
, 0);
337 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
338 if (r
!= EXIT_SUCCESS
)
339 return sd_bus_error_set(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
344 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
347 r
= sd_bus_message_close_container(reply
);
351 return sd_bus_send(NULL
, reply
, NULL
);
354 #define EXIT_NOT_FOUND 2
356 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
357 _cleanup_strv_free_
char **l
= NULL
;
358 Machine
*m
= ASSERT_PTR(userdata
);
366 r
= load_os_release_pairs(NULL
, &l
);
372 case MACHINE_CONTAINER
: {
373 _cleanup_close_
int mntns_fd
= -EBADF
, root_fd
= -EBADF
, pidns_fd
= -EBADF
;
374 _cleanup_close_pair_
int pair
[2] = PIPE_EBADF
;
375 _cleanup_fclose_
FILE *f
= NULL
;
378 r
= namespace_open(m
->leader
.pid
, &pidns_fd
, &mntns_fd
, NULL
, NULL
, &root_fd
);
382 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
385 r
= namespace_fork("(sd-osrelns)", "(sd-osrel)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
386 pidns_fd
, mntns_fd
, -1, -1, root_fd
,
389 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
393 pair
[0] = safe_close(pair
[0]);
395 r
= open_os_release(NULL
, NULL
, &fd
);
397 _exit(EXIT_NOT_FOUND
);
401 r
= copy_bytes(fd
, pair
[1], UINT64_MAX
, 0);
408 pair
[1] = safe_close(pair
[1]);
410 f
= take_fdopen(&pair
[0], "r");
414 r
= load_env_file_pairs(f
, "/etc/os-release", &l
);
418 r
= wait_for_terminate_and_check("(sd-osrelns)", child
, 0);
420 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
421 if (r
== EXIT_NOT_FOUND
)
422 return sd_bus_error_set(error
, SD_BUS_ERROR_FAILED
, "Machine does not contain OS release information");
423 if (r
!= EXIT_SUCCESS
)
424 return sd_bus_error_set(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
430 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
433 return bus_reply_pair_array(message
, l
);
436 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
437 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
438 _cleanup_free_
char *pty_name
= NULL
;
439 _cleanup_close_
int master
= -EBADF
;
440 Machine
*m
= ASSERT_PTR(userdata
);
445 const char *details
[] = {
450 r
= bus_verify_polkit_async(
453 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
457 &m
->manager
->polkit_registry
,
462 return 1; /* Will call us back */
464 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
468 r
= sd_bus_message_new_method_return(message
, &reply
);
472 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
476 return sd_bus_send(NULL
, reply
, NULL
);
479 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
491 case MACHINE_CONTAINER
: {
492 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
495 r
= sd_bus_new(&bus
);
499 if (asprintf(&address
, "x-machine-unix:pid=%" PID_PRI
, m
->leader
.pid
) < 0)
502 bus
->address
= address
;
503 bus
->bus_client
= true;
504 bus
->trusted
= false;
505 bus
->runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
507 r
= sd_bus_start(bus
);
509 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
513 *ret
= TAKE_PTR(bus
);
524 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
525 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
526 _cleanup_free_
char *pty_name
= NULL
;
527 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
528 _cleanup_close_
int master
= -EBADF
;
529 sd_bus
*container_bus
= NULL
;
530 Machine
*m
= ASSERT_PTR(userdata
);
531 const char *p
, *getty
;
536 const char *details
[] = {
542 r
= bus_verify_polkit_async(
545 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login",
549 &m
->manager
->polkit_registry
,
554 return 1; /* Will call us back */
556 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
560 p
= path_startswith(pty_name
, "/dev/pts/");
563 r
= container_bus_new(m
, error
, &allocated_bus
);
567 container_bus
= allocated_bus
?: m
->manager
->bus
;
569 getty
= strjoina("container-getty@", p
, ".service");
571 r
= bus_call_method(container_bus
, bus_systemd_mgr
, "StartUnit", error
, NULL
, "ss", getty
, "replace");
575 r
= sd_bus_message_new_method_return(message
, &reply
);
579 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
583 return sd_bus_send(NULL
, reply
, NULL
);
586 int bus_machine_method_open_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
587 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *tm
= NULL
;
588 _cleanup_free_
char *pty_name
= NULL
;
589 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
590 sd_bus
*container_bus
= NULL
;
591 _cleanup_close_
int master
= -EBADF
, slave
= -EBADF
;
592 _cleanup_strv_free_
char **env
= NULL
, **args_wire
= NULL
, **args
= NULL
;
593 _cleanup_free_
char *command_line
= NULL
;
594 Machine
*m
= ASSERT_PTR(userdata
);
595 const char *p
, *unit
, *user
, *path
, *description
, *utmp_id
;
600 r
= sd_bus_message_read(message
, "ss", &user
, &path
);
603 user
= isempty(user
) ? "root" : user
;
604 r
= sd_bus_message_read_strv(message
, &args_wire
);
610 args
= new0(char*, 3 + 1);
613 args
[0] = strdup("sh");
616 args
[1] = strdup("-c");
619 r
= asprintf(&args
[2],
620 "shell=$(getent passwd %s 2>/dev/null | { IFS=: read _ _ _ _ _ _ x; echo \"$x\"; })\n"\
621 "exec \"${shell:-/bin/sh}\" -l", /* -l is means --login */
628 if (!path_is_absolute(path
))
629 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified path '%s' is not absolute", path
);
630 args
= TAKE_PTR(args_wire
);
631 if (strv_isempty(args
)) {
632 args
= strv_free(args
);
634 args
= strv_new(path
);
640 r
= sd_bus_message_read_strv(message
, &env
);
643 if (!strv_env_is_valid(env
))
644 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
646 command_line
= strv_join(args
, " ");
649 const char *details
[] = {
653 "command_line", command_line
,
657 r
= bus_verify_polkit_async(
660 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
664 &m
->manager
->polkit_registry
,
669 return 1; /* Will call us back */
671 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
675 p
= path_startswith(pty_name
, "/dev/pts/");
678 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
682 utmp_id
= path_startswith(pty_name
, "/dev/");
685 r
= container_bus_new(m
, error
, &allocated_bus
);
689 container_bus
= allocated_bus
?: m
->manager
->bus
;
691 r
= bus_message_new_method_call(container_bus
, &tm
, bus_systemd_mgr
, "StartTransientUnit");
696 unit
= strjoina("container-shell@", p
, ".service");
697 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
702 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
706 description
= strjoina("Shell for User ", user
);
707 r
= sd_bus_message_append(tm
,
708 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
709 "Description", "s", description
,
710 "StandardInputFileDescriptor", "h", slave
,
711 "StandardOutputFileDescriptor", "h", slave
,
712 "StandardErrorFileDescriptor", "h", slave
,
713 "SendSIGHUP", "b", true,
714 "IgnoreSIGPIPE", "b", false,
715 "KillMode", "s", "mixed",
716 "TTYPath", "s", pty_name
,
717 "TTYReset", "b", true,
718 "UtmpIdentifier", "s", utmp_id
,
719 "UtmpMode", "s", "user",
720 "PAMName", "s", "login",
721 "WorkingDirectory", "s", "-~");
725 r
= sd_bus_message_append(tm
, "(sv)", "User", "s", user
);
729 if (!strv_isempty(env
)) {
730 r
= sd_bus_message_open_container(tm
, 'r', "sv");
734 r
= sd_bus_message_append(tm
, "s", "Environment");
738 r
= sd_bus_message_open_container(tm
, 'v', "as");
742 r
= sd_bus_message_append_strv(tm
, env
);
746 r
= sd_bus_message_close_container(tm
);
750 r
= sd_bus_message_close_container(tm
);
756 r
= sd_bus_message_open_container(tm
, 'r', "sv");
760 r
= sd_bus_message_append(tm
, "s", "ExecStart");
764 r
= sd_bus_message_open_container(tm
, 'v', "a(sasb)");
768 r
= sd_bus_message_open_container(tm
, 'a', "(sasb)");
772 r
= sd_bus_message_open_container(tm
, 'r', "sasb");
776 r
= sd_bus_message_append(tm
, "s", path
);
780 r
= sd_bus_message_append_strv(tm
, args
);
784 r
= sd_bus_message_append(tm
, "b", true);
788 r
= sd_bus_message_close_container(tm
);
792 r
= sd_bus_message_close_container(tm
);
796 r
= sd_bus_message_close_container(tm
);
800 r
= sd_bus_message_close_container(tm
);
804 r
= sd_bus_message_close_container(tm
);
808 /* Auxiliary units */
809 r
= sd_bus_message_append(tm
, "a(sa(sv))", 0);
813 r
= sd_bus_call(container_bus
, tm
, 0, error
, NULL
);
817 slave
= safe_close(slave
);
819 r
= sd_bus_message_new_method_return(message
, &reply
);
823 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
827 return sd_bus_send(NULL
, reply
, NULL
);
830 int bus_machine_method_bind_mount(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
831 int read_only
, make_file_or_directory
;
832 const char *dest
, *src
, *propagate_directory
;
833 Machine
*m
= ASSERT_PTR(userdata
);
839 if (m
->class != MACHINE_CONTAINER
)
840 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
842 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_file_or_directory
);
846 if (!path_is_absolute(src
) || !path_is_normalized(src
))
847 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and normalized.");
851 else if (!path_is_absolute(dest
) || !path_is_normalized(dest
))
852 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and normalized.");
854 const char *details
[] = {
862 r
= bus_verify_polkit_async(
865 "org.freedesktop.machine1.manage-machines",
869 &m
->manager
->polkit_registry
,
874 return 1; /* Will call us back */
876 r
= machine_get_uid_shift(m
, &uid
);
880 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Can't bind mount on container with user namespacing applied.");
882 propagate_directory
= strjoina("/run/systemd/nspawn/propagate/", m
->name
);
883 r
= bind_mount_in_namespace(
886 "/run/host/incoming/",
889 make_file_or_directory
);
891 return sd_bus_error_set_errnof(error
, r
, "Failed to mount %s on %s in machine's namespace: %m", src
, dest
);
893 return sd_bus_reply_method_return(message
, NULL
);
896 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
897 _cleanup_free_
char *host_basename
= NULL
, *container_basename
= NULL
;
898 const char *src
, *dest
, *host_path
, *container_path
;
899 _cleanup_close_pair_
int errno_pipe_fd
[2] = PIPE_EBADF
;
900 CopyFlags copy_flags
= COPY_REFLINK
|COPY_MERGE
|COPY_HARDLINKS
;
901 _cleanup_close_
int hostfd
= -EBADF
;
902 Machine
*m
= ASSERT_PTR(userdata
);
910 if (m
->manager
->n_operations
>= OPERATIONS_MAX
)
911 return sd_bus_error_set(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
913 if (m
->class != MACHINE_CONTAINER
)
914 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
916 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
920 if (endswith(sd_bus_message_get_member(message
), "WithFlags")) {
923 r
= sd_bus_message_read(message
, "t", &raw_flags
);
927 if ((raw_flags
& ~_MACHINE_COPY_FLAGS_MASK_PUBLIC
) != 0)
930 if (raw_flags
& MACHINE_COPY_REPLACE
)
931 copy_flags
|= COPY_REPLACE
;
934 if (!path_is_absolute(src
))
935 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
939 else if (!path_is_absolute(dest
))
940 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
942 const char *details
[] = {
950 r
= bus_verify_polkit_async(
953 "org.freedesktop.machine1.manage-machines",
957 &m
->manager
->polkit_registry
,
962 return 1; /* Will call us back */
964 r
= machine_get_uid_shift(m
, &uid_shift
);
968 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
971 container_path
= src
;
975 container_path
= dest
;
978 r
= path_extract_filename(host_path
, &host_basename
);
980 return sd_bus_error_set_errnof(error
, r
, "Failed to extract file name of '%s' path: %m", host_path
);
982 r
= path_extract_filename(container_path
, &container_basename
);
984 return sd_bus_error_set_errnof(error
, r
, "Failed to extract file name of '%s' path: %m", container_path
);
986 hostfd
= open_parent(host_path
, O_CLOEXEC
, 0);
988 return sd_bus_error_set_errnof(error
, hostfd
, "Failed to open host directory %s: %m", host_path
);
990 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
991 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
993 r
= safe_fork("(sd-copy)", FORK_RESET_SIGNALS
, &child
);
995 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1001 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1003 q
= procfs_file_alloca(m
->leader
.pid
, "ns/mnt");
1004 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
1006 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
1010 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
1011 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
1015 containerfd
= open_parent(container_path
, O_CLOEXEC
, 0);
1016 if (containerfd
< 0) {
1017 r
= log_error_errno(containerfd
, "Failed to open destination directory: %m");
1021 /* Run the actual copy operation. Note that when a UID shift is set we'll either clamp the UID/GID to
1022 * 0 or to the actual UID shift depending on the direction we copy. If no UID shift is set we'll copy
1023 * the UID/GIDs as they are. */
1025 r
= copy_tree_at(containerfd
, container_basename
, hostfd
, host_basename
, uid_shift
== 0 ? UID_INVALID
: 0, uid_shift
== 0 ? GID_INVALID
: 0, copy_flags
, NULL
, NULL
);
1027 r
= copy_tree_at(hostfd
, host_basename
, containerfd
, container_basename
, uid_shift
== 0 ? UID_INVALID
: uid_shift
, uid_shift
== 0 ? GID_INVALID
: uid_shift
, copy_flags
, NULL
, NULL
);
1029 hostfd
= safe_close(hostfd
);
1030 containerfd
= safe_close(containerfd
);
1033 r
= log_error_errno(r
, "Failed to copy tree: %m");
1037 _exit(EXIT_SUCCESS
);
1040 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1041 _exit(EXIT_FAILURE
);
1044 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1046 /* Copying might take a while, hence install a watch on the child, and return */
1048 r
= operation_new(m
->manager
, m
, child
, message
, errno_pipe_fd
[0], NULL
);
1050 (void) sigkill_wait(child
);
1053 errno_pipe_fd
[0] = -EBADF
;
1058 int bus_machine_method_open_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1059 _cleanup_close_
int fd
= -EBADF
;
1060 Machine
*m
= ASSERT_PTR(userdata
);
1065 const char *details
[] = {
1067 "verb", "open_root_directory",
1071 r
= bus_verify_polkit_async(
1074 "org.freedesktop.machine1.manage-machines",
1078 &m
->manager
->polkit_registry
,
1083 return 1; /* Will call us back */
1088 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1094 case MACHINE_CONTAINER
: {
1095 _cleanup_close_
int mntns_fd
= -EBADF
, root_fd
= -EBADF
;
1096 _cleanup_close_pair_
int pair
[2] = PIPE_EBADF
;
1099 r
= namespace_open(m
->leader
.pid
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
1103 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1106 r
= namespace_fork("(sd-openrootns)", "(sd-openroot)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
1107 -1, mntns_fd
, -1, -1, root_fd
, &child
);
1109 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1111 _cleanup_close_
int dfd
= -EBADF
;
1113 pair
[0] = safe_close(pair
[0]);
1115 dfd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1117 _exit(EXIT_FAILURE
);
1119 r
= send_one_fd(pair
[1], dfd
, 0);
1120 dfd
= safe_close(dfd
);
1122 _exit(EXIT_FAILURE
);
1124 _exit(EXIT_SUCCESS
);
1127 pair
[1] = safe_close(pair
[1]);
1129 r
= wait_for_terminate_and_check("(sd-openrootns)", child
, 0);
1131 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1132 if (r
!= EXIT_SUCCESS
)
1133 return sd_bus_error_set(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1135 fd
= receive_one_fd(pair
[0], MSG_DONTWAIT
);
1143 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Opening the root directory is only supported on container machines.");
1146 return sd_bus_reply_method_return(message
, "h", fd
);
1149 int bus_machine_method_get_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1150 Machine
*m
= ASSERT_PTR(userdata
);
1156 /* You wonder why this is a method and not a property? Well, properties are not supposed to return errors, but
1157 * we kinda have to for this. */
1159 if (m
->class == MACHINE_HOST
)
1160 return sd_bus_reply_method_return(message
, "u", UINT32_C(0));
1162 if (m
->class != MACHINE_CONTAINER
)
1163 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "UID/GID shift may only be determined for container machines.");
1165 r
= machine_get_uid_shift(m
, &shift
);
1167 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Machine %s uses a complex UID/GID mapping, cannot determine shift", m
->name
);
1171 return sd_bus_reply_method_return(message
, "u", (uint32_t) shift
);
1174 static int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1175 Manager
*m
= ASSERT_PTR(userdata
);
1184 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1185 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1186 sd_bus_message
*message
;
1189 message
= sd_bus_get_current_message(bus
);
1193 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1197 r
= sd_bus_creds_get_pid(creds
, &pid
);
1201 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1205 _cleanup_free_
char *e
= NULL
;
1208 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1212 e
= bus_label_unescape(p
);
1216 machine
= hashmap_get(m
->machines
, e
);
1225 char *machine_bus_path(Machine
*m
) {
1226 _cleanup_free_
char *e
= NULL
;
1230 e
= bus_label_escape(m
->name
);
1234 return strjoin("/org/freedesktop/machine1/machine/", e
);
1237 static int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1238 _cleanup_strv_free_
char **l
= NULL
;
1239 Machine
*machine
= NULL
;
1240 Manager
*m
= userdata
;
1247 HASHMAP_FOREACH(machine
, m
->machines
) {
1250 p
= machine_bus_path(machine
);
1254 r
= strv_consume(&l
, p
);
1259 *nodes
= TAKE_PTR(l
);
1264 static const sd_bus_vtable machine_vtable
[] = {
1265 SD_BUS_VTABLE_START(0),
1266 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1267 SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128
, offsetof(Machine
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1268 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1269 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1270 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1271 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1272 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
.pid
), SD_BUS_VTABLE_PROPERTY_CONST
),
1273 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1274 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1275 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1276 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1278 SD_BUS_METHOD("Terminate",
1281 bus_machine_method_terminate
,
1282 SD_BUS_VTABLE_UNPRIVILEGED
),
1283 SD_BUS_METHOD_WITH_ARGS("Kill",
1284 SD_BUS_ARGS("s", who
, "i", signal
),
1286 bus_machine_method_kill
,
1287 SD_BUS_VTABLE_UNPRIVILEGED
),
1288 SD_BUS_METHOD_WITH_ARGS("GetAddresses",
1290 SD_BUS_RESULT("a(iay)", addresses
),
1291 bus_machine_method_get_addresses
,
1292 SD_BUS_VTABLE_UNPRIVILEGED
),
1293 SD_BUS_METHOD_WITH_ARGS("GetOSRelease",
1295 SD_BUS_RESULT("a{ss}", fields
),
1296 bus_machine_method_get_os_release
,
1297 SD_BUS_VTABLE_UNPRIVILEGED
),
1298 SD_BUS_METHOD_WITH_ARGS("GetUIDShift",
1300 SD_BUS_RESULT("u", shift
),
1301 bus_machine_method_get_uid_shift
,
1302 SD_BUS_VTABLE_UNPRIVILEGED
),
1303 SD_BUS_METHOD_WITH_ARGS("OpenPTY",
1305 SD_BUS_RESULT("h", pty
, "s", pty_path
),
1306 bus_machine_method_open_pty
,
1307 SD_BUS_VTABLE_UNPRIVILEGED
),
1308 SD_BUS_METHOD_WITH_ARGS("OpenLogin",
1310 SD_BUS_RESULT("h", pty
, "s", pty_path
),
1311 bus_machine_method_open_login
,
1312 SD_BUS_VTABLE_UNPRIVILEGED
),
1313 SD_BUS_METHOD_WITH_ARGS("OpenShell",
1314 SD_BUS_ARGS("s", user
, "s", path
, "as", args
, "as", environment
),
1315 SD_BUS_RESULT("h", pty
, "s", pty_path
),
1316 bus_machine_method_open_shell
,
1317 SD_BUS_VTABLE_UNPRIVILEGED
),
1318 SD_BUS_METHOD_WITH_ARGS("BindMount",
1319 SD_BUS_ARGS("s", source
, "s", destination
, "b", read_only
, "b", mkdir
),
1321 bus_machine_method_bind_mount
,
1322 SD_BUS_VTABLE_UNPRIVILEGED
),
1323 SD_BUS_METHOD_WITH_ARGS("CopyFrom",
1324 SD_BUS_ARGS("s", source
, "s", destination
),
1326 bus_machine_method_copy
,
1327 SD_BUS_VTABLE_UNPRIVILEGED
),
1328 SD_BUS_METHOD_WITH_ARGS("CopyTo",
1329 SD_BUS_ARGS("s", source
, "s", destination
),
1331 bus_machine_method_copy
,
1332 SD_BUS_VTABLE_UNPRIVILEGED
),
1333 SD_BUS_METHOD_WITH_ARGS("CopyFromWithFlags",
1334 SD_BUS_ARGS("s", source
, "s", destination
, "t", flags
),
1336 bus_machine_method_copy
,
1337 SD_BUS_VTABLE_UNPRIVILEGED
),
1338 SD_BUS_METHOD_WITH_ARGS("CopyToWithFlags",
1339 SD_BUS_ARGS("s", source
, "s", destination
, "t", flags
),
1341 bus_machine_method_copy
,
1342 SD_BUS_VTABLE_UNPRIVILEGED
),
1343 SD_BUS_METHOD_WITH_ARGS("OpenRootDirectory",
1345 SD_BUS_RESULT("h", fd
),
1346 bus_machine_method_open_root_directory
,
1347 SD_BUS_VTABLE_UNPRIVILEGED
),
1352 const BusObjectImplementation machine_object
= {
1353 "/org/freedesktop/machine1/machine",
1354 "org.freedesktop.machine1.Machine",
1355 .fallback_vtables
= BUS_FALLBACK_VTABLES({machine_vtable
, machine_object_find
}),
1356 .node_enumerator
= machine_node_enumerator
,
1359 int machine_send_signal(Machine
*m
, bool new_machine
) {
1360 _cleanup_free_
char *p
= NULL
;
1364 p
= machine_bus_path(m
);
1368 return sd_bus_emit_signal(
1370 "/org/freedesktop/machine1",
1371 "org.freedesktop.machine1.Manager",
1372 new_machine
? "MachineNew" : "MachineRemoved",
1376 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1377 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1378 _cleanup_free_
char *p
= NULL
;
1382 if (!m
->create_message
)
1385 c
= TAKE_PTR(m
->create_message
);
1388 return sd_bus_reply_method_error(c
, error
);
1390 /* Update the machine state file before we notify the client
1391 * about the result. */
1394 p
= machine_bus_path(m
);
1398 return sd_bus_reply_method_return(c
, "o", p
);