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"
22 #include "iovec-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(
76 "org.freedesktop.machine1.manage-machines",
78 &m
->manager
->polkit_registry
,
83 return 1; /* Will call us back */
85 r
= machine_finalize(m
);
89 return sd_bus_reply_method_return(message
, NULL
);
92 int bus_machine_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
93 Machine
*m
= ASSERT_PTR(userdata
);
98 const char *details
[] = {
104 r
= bus_verify_polkit_async(
106 "org.freedesktop.machine1.manage-machines",
108 &m
->manager
->polkit_registry
,
113 return 1; /* Will call us back */
119 return sd_bus_reply_method_return(message
, NULL
);
122 int bus_machine_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
123 Machine
*m
= ASSERT_PTR(userdata
);
131 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
138 who
= kill_who_from_string(swho
);
140 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
143 if (!SIGNAL_VALID(signo
))
144 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
146 const char *details
[] = {
152 r
= bus_verify_polkit_async(
154 "org.freedesktop.machine1.manage-machines",
156 &m
->manager
->polkit_registry
,
161 return 1; /* Will call us back */
163 r
= machine_kill(m
, who
, signo
);
167 return sd_bus_reply_method_return(message
, NULL
);
170 int bus_machine_method_get_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
171 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
172 Machine
*m
= ASSERT_PTR(userdata
);
177 r
= sd_bus_message_new_method_return(message
, &reply
);
181 r
= sd_bus_message_open_container(reply
, 'a', "(iay)");
188 _cleanup_free_
struct local_address
*addresses
= NULL
;
191 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
195 for (int i
= 0; i
< n
; i
++) {
196 r
= sd_bus_message_open_container(reply
, 'r', "iay");
200 r
= sd_bus_message_append(reply
, "i", addresses
[i
].family
);
204 r
= sd_bus_message_append_array(reply
, 'y', &addresses
[i
].address
, FAMILY_ADDRESS_SIZE(addresses
[i
].family
));
208 r
= sd_bus_message_close_container(reply
);
216 case MACHINE_CONTAINER
: {
217 _cleanup_close_pair_
int pair
[2] = EBADF_PAIR
;
218 _cleanup_free_
char *us
= NULL
, *them
= NULL
;
219 _cleanup_close_
int netns_fd
= -EBADF
;
223 r
= readlink_malloc("/proc/self/ns/net", &us
);
227 p
= procfs_file_alloca(m
->leader
.pid
, "ns/net");
228 r
= readlink_malloc(p
, &them
);
233 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_NETWORKING
, "Machine %s does not use private networking", m
->name
);
235 r
= namespace_open(m
->leader
.pid
, NULL
, NULL
, &netns_fd
, NULL
, NULL
);
239 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
242 r
= namespace_fork("(sd-addrns)", "(sd-addr)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG_SIGKILL
,
243 -1, -1, netns_fd
, -1, -1, &child
);
245 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
247 _cleanup_free_
struct local_address
*addresses
= NULL
;
248 struct local_address
*a
;
251 pair
[0] = safe_close(pair
[0]);
253 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
257 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
258 struct iovec iov
[2] = {
259 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
260 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
263 r
= writev(pair
[1], iov
, 2);
268 pair
[1] = safe_close(pair
[1]);
273 pair
[1] = safe_close(pair
[1]);
278 union in_addr_union in_addr
;
285 iov
[0] = IOVEC_MAKE(&family
, sizeof(family
));
286 iov
[1] = IOVEC_MAKE(&in_addr
, sizeof(in_addr
));
288 n
= recvmsg(pair
[0], &mh
, 0);
291 if ((size_t) n
< sizeof(family
))
294 r
= sd_bus_message_open_container(reply
, 'r', "iay");
298 r
= sd_bus_message_append(reply
, "i", family
);
305 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
308 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
312 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
315 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
321 r
= sd_bus_message_close_container(reply
);
326 r
= wait_for_terminate_and_check("(sd-addrns)", child
, 0);
328 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
329 if (r
!= EXIT_SUCCESS
)
330 return sd_bus_error_set(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
335 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
338 r
= sd_bus_message_close_container(reply
);
342 return sd_bus_send(NULL
, reply
, NULL
);
345 #define EXIT_NOT_FOUND 2
347 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
348 _cleanup_strv_free_
char **l
= NULL
;
349 Machine
*m
= ASSERT_PTR(userdata
);
357 r
= load_os_release_pairs(NULL
, &l
);
363 case MACHINE_CONTAINER
: {
364 _cleanup_close_
int mntns_fd
= -EBADF
, root_fd
= -EBADF
, pidns_fd
= -EBADF
;
365 _cleanup_close_pair_
int pair
[2] = EBADF_PAIR
;
366 _cleanup_fclose_
FILE *f
= NULL
;
369 r
= namespace_open(m
->leader
.pid
, &pidns_fd
, &mntns_fd
, NULL
, NULL
, &root_fd
);
373 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
376 r
= namespace_fork("(sd-osrelns)", "(sd-osrel)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG_SIGKILL
,
377 pidns_fd
, mntns_fd
, -1, -1, root_fd
,
380 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
384 pair
[0] = safe_close(pair
[0]);
386 r
= open_os_release(NULL
, NULL
, &fd
);
388 _exit(EXIT_NOT_FOUND
);
392 r
= copy_bytes(fd
, pair
[1], UINT64_MAX
, 0);
399 pair
[1] = safe_close(pair
[1]);
401 f
= take_fdopen(&pair
[0], "r");
405 r
= load_env_file_pairs(f
, "/etc/os-release", &l
);
409 r
= wait_for_terminate_and_check("(sd-osrelns)", child
, 0);
411 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
412 if (r
== EXIT_NOT_FOUND
)
413 return sd_bus_error_set(error
, SD_BUS_ERROR_FAILED
, "Machine does not contain OS release information");
414 if (r
!= EXIT_SUCCESS
)
415 return sd_bus_error_set(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
421 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
424 return bus_reply_pair_array(message
, l
);
427 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
428 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
429 _cleanup_free_
char *pty_name
= NULL
;
430 _cleanup_close_
int master
= -EBADF
;
431 Machine
*m
= ASSERT_PTR(userdata
);
436 const char *details
[] = {
441 r
= bus_verify_polkit_async(
443 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
445 &m
->manager
->polkit_registry
,
450 return 1; /* Will call us back */
452 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
456 r
= sd_bus_message_new_method_return(message
, &reply
);
460 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
464 return sd_bus_send(NULL
, reply
, NULL
);
467 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
479 case MACHINE_CONTAINER
: {
480 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
483 r
= sd_bus_new(&bus
);
487 if (asprintf(&address
, "x-machine-unix:pid=%" PID_PRI
, m
->leader
.pid
) < 0)
490 bus
->address
= address
;
491 bus
->bus_client
= true;
492 bus
->trusted
= false;
493 bus
->runtime_scope
= RUNTIME_SCOPE_SYSTEM
;
495 r
= sd_bus_start(bus
);
497 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
501 *ret
= TAKE_PTR(bus
);
512 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
513 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
514 _cleanup_free_
char *pty_name
= NULL
;
515 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
516 _cleanup_close_
int master
= -EBADF
;
517 sd_bus
*container_bus
= NULL
;
518 Machine
*m
= ASSERT_PTR(userdata
);
519 const char *p
, *getty
;
524 const char *details
[] = {
530 r
= bus_verify_polkit_async(
532 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login",
534 &m
->manager
->polkit_registry
,
539 return 1; /* Will call us back */
541 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
545 p
= path_startswith(pty_name
, "/dev/pts/");
548 r
= container_bus_new(m
, error
, &allocated_bus
);
552 container_bus
= allocated_bus
?: m
->manager
->bus
;
554 getty
= strjoina("container-getty@", p
, ".service");
556 r
= bus_call_method(container_bus
, bus_systemd_mgr
, "StartUnit", error
, NULL
, "ss", getty
, "replace");
560 r
= sd_bus_message_new_method_return(message
, &reply
);
564 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
568 return sd_bus_send(NULL
, reply
, NULL
);
571 int bus_machine_method_open_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
572 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *tm
= NULL
;
573 _cleanup_free_
char *pty_name
= NULL
;
574 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
575 sd_bus
*container_bus
= NULL
;
576 _cleanup_close_
int master
= -EBADF
, slave
= -EBADF
;
577 _cleanup_strv_free_
char **env
= NULL
, **args_wire
= NULL
, **args
= NULL
;
578 _cleanup_free_
char *command_line
= NULL
;
579 Machine
*m
= ASSERT_PTR(userdata
);
580 const char *p
, *unit
, *user
, *path
, *description
, *utmp_id
;
585 r
= sd_bus_message_read(message
, "ss", &user
, &path
);
588 user
= isempty(user
) ? "root" : user
;
589 r
= sd_bus_message_read_strv(message
, &args_wire
);
595 args
= new0(char*, 3 + 1);
598 args
[0] = strdup("sh");
601 args
[1] = strdup("-c");
604 r
= asprintf(&args
[2],
605 "shell=$(getent passwd %s 2>/dev/null | { IFS=: read _ _ _ _ _ _ x; echo \"$x\"; })\n"\
606 "exec \"${shell:-/bin/sh}\" -l", /* -l is means --login */
613 if (!path_is_absolute(path
))
614 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified path '%s' is not absolute", path
);
615 args
= TAKE_PTR(args_wire
);
616 if (strv_isempty(args
)) {
617 args
= strv_free(args
);
619 args
= strv_new(path
);
625 r
= sd_bus_message_read_strv(message
, &env
);
628 if (!strv_env_is_valid(env
))
629 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
631 command_line
= strv_join(args
, " ");
634 const char *details
[] = {
638 "command_line", command_line
,
642 r
= bus_verify_polkit_async(
644 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
646 &m
->manager
->polkit_registry
,
651 return 1; /* Will call us back */
653 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
657 p
= path_startswith(pty_name
, "/dev/pts/");
660 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
664 utmp_id
= path_startswith(pty_name
, "/dev/");
667 r
= container_bus_new(m
, error
, &allocated_bus
);
671 container_bus
= allocated_bus
?: m
->manager
->bus
;
673 r
= bus_message_new_method_call(container_bus
, &tm
, bus_systemd_mgr
, "StartTransientUnit");
678 unit
= strjoina("container-shell@", p
, ".service");
679 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
684 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
688 description
= strjoina("Shell for User ", user
);
689 r
= sd_bus_message_append(tm
,
690 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
691 "Description", "s", description
,
692 "StandardInputFileDescriptor", "h", slave
,
693 "StandardOutputFileDescriptor", "h", slave
,
694 "StandardErrorFileDescriptor", "h", slave
,
695 "SendSIGHUP", "b", true,
696 "IgnoreSIGPIPE", "b", false,
697 "KillMode", "s", "mixed",
698 "TTYPath", "s", pty_name
,
699 "TTYReset", "b", true,
700 "UtmpIdentifier", "s", utmp_id
,
701 "UtmpMode", "s", "user",
702 "PAMName", "s", "login",
703 "WorkingDirectory", "s", "-~");
707 r
= sd_bus_message_append(tm
, "(sv)", "User", "s", user
);
711 if (!strv_isempty(env
)) {
712 r
= sd_bus_message_open_container(tm
, 'r', "sv");
716 r
= sd_bus_message_append(tm
, "s", "Environment");
720 r
= sd_bus_message_open_container(tm
, 'v', "as");
724 r
= sd_bus_message_append_strv(tm
, env
);
728 r
= sd_bus_message_close_container(tm
);
732 r
= sd_bus_message_close_container(tm
);
738 r
= sd_bus_message_open_container(tm
, 'r', "sv");
742 r
= sd_bus_message_append(tm
, "s", "ExecStart");
746 r
= sd_bus_message_open_container(tm
, 'v', "a(sasb)");
750 r
= sd_bus_message_open_container(tm
, 'a', "(sasb)");
754 r
= sd_bus_message_open_container(tm
, 'r', "sasb");
758 r
= sd_bus_message_append(tm
, "s", path
);
762 r
= sd_bus_message_append_strv(tm
, args
);
766 r
= sd_bus_message_append(tm
, "b", true);
770 r
= sd_bus_message_close_container(tm
);
774 r
= sd_bus_message_close_container(tm
);
778 r
= sd_bus_message_close_container(tm
);
782 r
= sd_bus_message_close_container(tm
);
786 r
= sd_bus_message_close_container(tm
);
790 /* Auxiliary units */
791 r
= sd_bus_message_append(tm
, "a(sa(sv))", 0);
795 r
= sd_bus_call(container_bus
, tm
, 0, error
, NULL
);
799 slave
= safe_close(slave
);
801 r
= sd_bus_message_new_method_return(message
, &reply
);
805 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
809 return sd_bus_send(NULL
, reply
, NULL
);
812 int bus_machine_method_bind_mount(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
813 int read_only
, make_file_or_directory
;
814 const char *dest
, *src
, *propagate_directory
;
815 Machine
*m
= ASSERT_PTR(userdata
);
821 if (m
->class != MACHINE_CONTAINER
)
822 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
824 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_file_or_directory
);
828 if (!path_is_absolute(src
) || !path_is_normalized(src
))
829 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and normalized.");
833 else if (!path_is_absolute(dest
) || !path_is_normalized(dest
))
834 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and normalized.");
836 const char *details
[] = {
844 r
= bus_verify_polkit_async(
846 "org.freedesktop.machine1.manage-machines",
848 &m
->manager
->polkit_registry
,
853 return 1; /* Will call us back */
855 r
= machine_get_uid_shift(m
, &uid
);
859 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Can't bind mount on container with user namespacing applied.");
861 propagate_directory
= strjoina("/run/systemd/nspawn/propagate/", m
->name
);
862 r
= bind_mount_in_namespace(
865 "/run/host/incoming/",
868 make_file_or_directory
);
870 return sd_bus_error_set_errnof(error
, r
, "Failed to mount %s on %s in machine's namespace: %m", src
, dest
);
872 return sd_bus_reply_method_return(message
, NULL
);
875 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
876 _cleanup_free_
char *host_basename
= NULL
, *container_basename
= NULL
;
877 const char *src
, *dest
, *host_path
, *container_path
;
878 _cleanup_close_pair_
int errno_pipe_fd
[2] = EBADF_PAIR
;
879 CopyFlags copy_flags
= COPY_REFLINK
|COPY_MERGE
|COPY_HARDLINKS
;
880 _cleanup_close_
int hostfd
= -EBADF
;
881 Machine
*m
= ASSERT_PTR(userdata
);
889 if (m
->manager
->n_operations
>= OPERATIONS_MAX
)
890 return sd_bus_error_set(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
892 if (m
->class != MACHINE_CONTAINER
)
893 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
895 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
899 if (endswith(sd_bus_message_get_member(message
), "WithFlags")) {
902 r
= sd_bus_message_read(message
, "t", &raw_flags
);
906 if ((raw_flags
& ~_MACHINE_COPY_FLAGS_MASK_PUBLIC
) != 0)
909 if (raw_flags
& MACHINE_COPY_REPLACE
)
910 copy_flags
|= COPY_REPLACE
;
913 if (!path_is_absolute(src
))
914 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
918 else if (!path_is_absolute(dest
))
919 return sd_bus_error_set(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
921 const char *details
[] = {
929 r
= bus_verify_polkit_async(
931 "org.freedesktop.machine1.manage-machines",
933 &m
->manager
->polkit_registry
,
938 return 1; /* Will call us back */
940 r
= machine_get_uid_shift(m
, &uid_shift
);
944 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
947 container_path
= src
;
951 container_path
= dest
;
954 r
= path_extract_filename(host_path
, &host_basename
);
956 return sd_bus_error_set_errnof(error
, r
, "Failed to extract file name of '%s' path: %m", host_path
);
958 r
= path_extract_filename(container_path
, &container_basename
);
960 return sd_bus_error_set_errnof(error
, r
, "Failed to extract file name of '%s' path: %m", container_path
);
962 hostfd
= open_parent(host_path
, O_CLOEXEC
, 0);
964 return sd_bus_error_set_errnof(error
, hostfd
, "Failed to open host directory %s: %m", host_path
);
966 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
967 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
969 r
= safe_fork("(sd-copy)", FORK_RESET_SIGNALS
, &child
);
971 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
977 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
979 q
= procfs_file_alloca(m
->leader
.pid
, "ns/mnt");
980 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
982 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
986 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
987 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
991 containerfd
= open_parent(container_path
, O_CLOEXEC
, 0);
992 if (containerfd
< 0) {
993 r
= log_error_errno(containerfd
, "Failed to open destination directory: %m");
997 /* Run the actual copy operation. Note that when a UID shift is set we'll either clamp the UID/GID to
998 * 0 or to the actual UID shift depending on the direction we copy. If no UID shift is set we'll copy
999 * the UID/GIDs as they are. */
1001 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
);
1003 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
);
1005 hostfd
= safe_close(hostfd
);
1006 containerfd
= safe_close(containerfd
);
1009 r
= log_error_errno(r
, "Failed to copy tree: %m");
1013 _exit(EXIT_SUCCESS
);
1016 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1017 _exit(EXIT_FAILURE
);
1020 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1022 /* Copying might take a while, hence install a watch on the child, and return */
1024 r
= operation_new(m
->manager
, m
, child
, message
, errno_pipe_fd
[0], NULL
);
1026 (void) sigkill_wait(child
);
1029 errno_pipe_fd
[0] = -EBADF
;
1034 int bus_machine_method_open_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1035 _cleanup_close_
int fd
= -EBADF
;
1036 Machine
*m
= ASSERT_PTR(userdata
);
1041 const char *details
[] = {
1043 "verb", "open_root_directory",
1047 r
= bus_verify_polkit_async(
1049 "org.freedesktop.machine1.manage-machines",
1051 &m
->manager
->polkit_registry
,
1056 return 1; /* Will call us back */
1061 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1067 case MACHINE_CONTAINER
: {
1068 _cleanup_close_
int mntns_fd
= -EBADF
, root_fd
= -EBADF
;
1069 _cleanup_close_pair_
int pair
[2] = EBADF_PAIR
;
1072 r
= namespace_open(m
->leader
.pid
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
1076 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1079 r
= namespace_fork("(sd-openrootns)", "(sd-openroot)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG_SIGKILL
,
1080 -1, mntns_fd
, -1, -1, root_fd
, &child
);
1082 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1084 _cleanup_close_
int dfd
= -EBADF
;
1086 pair
[0] = safe_close(pair
[0]);
1088 dfd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1090 _exit(EXIT_FAILURE
);
1092 r
= send_one_fd(pair
[1], dfd
, 0);
1093 dfd
= safe_close(dfd
);
1095 _exit(EXIT_FAILURE
);
1097 _exit(EXIT_SUCCESS
);
1100 pair
[1] = safe_close(pair
[1]);
1102 r
= wait_for_terminate_and_check("(sd-openrootns)", child
, 0);
1104 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1105 if (r
!= EXIT_SUCCESS
)
1106 return sd_bus_error_set(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1108 fd
= receive_one_fd(pair
[0], MSG_DONTWAIT
);
1116 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Opening the root directory is only supported on container machines.");
1119 return sd_bus_reply_method_return(message
, "h", fd
);
1122 int bus_machine_method_get_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1123 Machine
*m
= ASSERT_PTR(userdata
);
1129 /* You wonder why this is a method and not a property? Well, properties are not supposed to return errors, but
1130 * we kinda have to for this. */
1132 if (m
->class == MACHINE_HOST
)
1133 return sd_bus_reply_method_return(message
, "u", UINT32_C(0));
1135 if (m
->class != MACHINE_CONTAINER
)
1136 return sd_bus_error_set(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "UID/GID shift may only be determined for container machines.");
1138 r
= machine_get_uid_shift(m
, &shift
);
1140 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Machine %s uses a complex UID/GID mapping, cannot determine shift", m
->name
);
1144 return sd_bus_reply_method_return(message
, "u", (uint32_t) shift
);
1147 static int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1148 Manager
*m
= ASSERT_PTR(userdata
);
1157 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1158 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1159 sd_bus_message
*message
;
1162 message
= sd_bus_get_current_message(bus
);
1166 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1170 r
= sd_bus_creds_get_pid(creds
, &pid
);
1174 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1178 _cleanup_free_
char *e
= NULL
;
1181 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1185 e
= bus_label_unescape(p
);
1189 machine
= hashmap_get(m
->machines
, e
);
1198 char *machine_bus_path(Machine
*m
) {
1199 _cleanup_free_
char *e
= NULL
;
1203 e
= bus_label_escape(m
->name
);
1207 return strjoin("/org/freedesktop/machine1/machine/", e
);
1210 static int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1211 _cleanup_strv_free_
char **l
= NULL
;
1212 Machine
*machine
= NULL
;
1213 Manager
*m
= userdata
;
1220 HASHMAP_FOREACH(machine
, m
->machines
) {
1223 p
= machine_bus_path(machine
);
1227 r
= strv_consume(&l
, p
);
1232 *nodes
= TAKE_PTR(l
);
1237 static const sd_bus_vtable machine_vtable
[] = {
1238 SD_BUS_VTABLE_START(0),
1239 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1240 SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128
, offsetof(Machine
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1241 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1242 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1243 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1244 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1245 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
.pid
), SD_BUS_VTABLE_PROPERTY_CONST
),
1246 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1247 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1248 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1249 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1251 SD_BUS_METHOD("Terminate",
1254 bus_machine_method_terminate
,
1255 SD_BUS_VTABLE_UNPRIVILEGED
),
1256 SD_BUS_METHOD_WITH_ARGS("Kill",
1257 SD_BUS_ARGS("s", who
, "i", signal
),
1259 bus_machine_method_kill
,
1260 SD_BUS_VTABLE_UNPRIVILEGED
),
1261 SD_BUS_METHOD_WITH_ARGS("GetAddresses",
1263 SD_BUS_RESULT("a(iay)", addresses
),
1264 bus_machine_method_get_addresses
,
1265 SD_BUS_VTABLE_UNPRIVILEGED
),
1266 SD_BUS_METHOD_WITH_ARGS("GetOSRelease",
1268 SD_BUS_RESULT("a{ss}", fields
),
1269 bus_machine_method_get_os_release
,
1270 SD_BUS_VTABLE_UNPRIVILEGED
),
1271 SD_BUS_METHOD_WITH_ARGS("GetUIDShift",
1273 SD_BUS_RESULT("u", shift
),
1274 bus_machine_method_get_uid_shift
,
1275 SD_BUS_VTABLE_UNPRIVILEGED
),
1276 SD_BUS_METHOD_WITH_ARGS("OpenPTY",
1278 SD_BUS_RESULT("h", pty
, "s", pty_path
),
1279 bus_machine_method_open_pty
,
1280 SD_BUS_VTABLE_UNPRIVILEGED
),
1281 SD_BUS_METHOD_WITH_ARGS("OpenLogin",
1283 SD_BUS_RESULT("h", pty
, "s", pty_path
),
1284 bus_machine_method_open_login
,
1285 SD_BUS_VTABLE_UNPRIVILEGED
),
1286 SD_BUS_METHOD_WITH_ARGS("OpenShell",
1287 SD_BUS_ARGS("s", user
, "s", path
, "as", args
, "as", environment
),
1288 SD_BUS_RESULT("h", pty
, "s", pty_path
),
1289 bus_machine_method_open_shell
,
1290 SD_BUS_VTABLE_UNPRIVILEGED
),
1291 SD_BUS_METHOD_WITH_ARGS("BindMount",
1292 SD_BUS_ARGS("s", source
, "s", destination
, "b", read_only
, "b", mkdir
),
1294 bus_machine_method_bind_mount
,
1295 SD_BUS_VTABLE_UNPRIVILEGED
),
1296 SD_BUS_METHOD_WITH_ARGS("CopyFrom",
1297 SD_BUS_ARGS("s", source
, "s", destination
),
1299 bus_machine_method_copy
,
1300 SD_BUS_VTABLE_UNPRIVILEGED
),
1301 SD_BUS_METHOD_WITH_ARGS("CopyTo",
1302 SD_BUS_ARGS("s", source
, "s", destination
),
1304 bus_machine_method_copy
,
1305 SD_BUS_VTABLE_UNPRIVILEGED
),
1306 SD_BUS_METHOD_WITH_ARGS("CopyFromWithFlags",
1307 SD_BUS_ARGS("s", source
, "s", destination
, "t", flags
),
1309 bus_machine_method_copy
,
1310 SD_BUS_VTABLE_UNPRIVILEGED
),
1311 SD_BUS_METHOD_WITH_ARGS("CopyToWithFlags",
1312 SD_BUS_ARGS("s", source
, "s", destination
, "t", flags
),
1314 bus_machine_method_copy
,
1315 SD_BUS_VTABLE_UNPRIVILEGED
),
1316 SD_BUS_METHOD_WITH_ARGS("OpenRootDirectory",
1318 SD_BUS_RESULT("h", fd
),
1319 bus_machine_method_open_root_directory
,
1320 SD_BUS_VTABLE_UNPRIVILEGED
),
1325 const BusObjectImplementation machine_object
= {
1326 "/org/freedesktop/machine1/machine",
1327 "org.freedesktop.machine1.Machine",
1328 .fallback_vtables
= BUS_FALLBACK_VTABLES({machine_vtable
, machine_object_find
}),
1329 .node_enumerator
= machine_node_enumerator
,
1332 int machine_send_signal(Machine
*m
, bool new_machine
) {
1333 _cleanup_free_
char *p
= NULL
;
1337 p
= machine_bus_path(m
);
1341 return sd_bus_emit_signal(
1343 "/org/freedesktop/machine1",
1344 "org.freedesktop.machine1.Manager",
1345 new_machine
? "MachineNew" : "MachineRemoved",
1349 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1350 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1351 _cleanup_free_
char *p
= NULL
;
1355 if (!m
->create_message
)
1358 c
= TAKE_PTR(m
->create_message
);
1361 return sd_bus_reply_method_error(c
, error
);
1363 /* Update the machine state file before we notify the client
1364 * about the result. */
1367 p
= machine_bus_path(m
);
1371 return sd_bus_reply_method_return(c
, "o", p
);