1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 /* When we include libgen.h because we need dirname() we immediately
8 * undefine basename() since libgen.h defines it as a macro to the POSIX
9 * version which is really broken. We prefer GNU basename(). */
13 #include "alloc-util.h"
14 #include "bus-common-errors.h"
15 #include "bus-get-properties.h"
16 #include "bus-internal.h"
17 #include "bus-label.h"
18 #include "bus-locator.h"
19 #include "bus-polkit.h"
25 #include "format-util.h"
27 #include "in-addr-util.h"
29 #include "local-addresses.h"
30 #include "machine-dbus.h"
32 #include "missing_capability.h"
34 #include "mount-util.h"
35 #include "mountpoint-util.h"
36 #include "namespace-util.h"
38 #include "path-util.h"
39 #include "process-util.h"
40 #include "signal-util.h"
42 #include "terminal-util.h"
43 #include "tmpfile-util.h"
44 #include "user-util.h"
46 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class
, machine_class
, MachineClass
);
47 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", Machine
, machine_get_state
, machine_state_to_string
);
49 static int property_get_netif(
52 const char *interface
,
54 sd_bus_message
*reply
,
56 sd_bus_error
*error
) {
58 Machine
*m
= userdata
;
64 assert_cc(sizeof(int) == sizeof(int32_t));
66 return sd_bus_message_append_array(reply
, 'i', m
->netif
, m
->n_netif
* sizeof(int));
69 int bus_machine_method_unregister(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
70 Machine
*m
= userdata
;
76 r
= bus_verify_polkit_async(
79 "org.freedesktop.machine1.manage-machines",
83 &m
->manager
->polkit_registry
,
88 return 1; /* Will call us back */
90 r
= machine_finalize(m
);
94 return sd_bus_reply_method_return(message
, NULL
);
97 int bus_machine_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
98 Machine
*m
= userdata
;
104 r
= bus_verify_polkit_async(
107 "org.freedesktop.machine1.manage-machines",
111 &m
->manager
->polkit_registry
,
116 return 1; /* Will call us back */
122 return sd_bus_reply_method_return(message
, NULL
);
125 int bus_machine_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
126 Machine
*m
= userdata
;
135 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
142 who
= kill_who_from_string(swho
);
144 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
147 if (!SIGNAL_VALID(signo
))
148 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
150 r
= bus_verify_polkit_async(
153 "org.freedesktop.machine1.manage-machines",
157 &m
->manager
->polkit_registry
,
162 return 1; /* Will call us back */
164 r
= machine_kill(m
, who
, signo
);
168 return sd_bus_reply_method_return(message
, NULL
);
171 int bus_machine_method_get_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
172 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
173 Machine
*m
= userdata
;
179 r
= sd_bus_message_new_method_return(message
, &reply
);
183 r
= sd_bus_message_open_container(reply
, 'a', "(iay)");
190 _cleanup_free_
struct local_address
*addresses
= NULL
;
191 struct local_address
*a
;
194 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
198 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
200 r
= sd_bus_message_open_container(reply
, 'r', "iay");
204 r
= sd_bus_message_append(reply
, "i", addresses
[i
].family
);
208 r
= sd_bus_message_append_array(reply
, 'y', &addresses
[i
].address
, FAMILY_ADDRESS_SIZE(addresses
[i
].family
));
212 r
= sd_bus_message_close_container(reply
);
220 case MACHINE_CONTAINER
: {
221 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
222 _cleanup_free_
char *us
= NULL
, *them
= NULL
;
223 _cleanup_close_
int netns_fd
= -1;
227 r
= readlink_malloc("/proc/self/ns/net", &us
);
231 p
= procfs_file_alloca(m
->leader
, "ns/net");
232 r
= readlink_malloc(p
, &them
);
237 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_NETWORKING
, "Machine %s does not use private networking", m
->name
);
239 r
= namespace_open(m
->leader
, NULL
, NULL
, &netns_fd
, NULL
, NULL
);
243 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
246 r
= namespace_fork("(sd-addrns)", "(sd-addr)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
247 -1, -1, netns_fd
, -1, -1, &child
);
249 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
251 _cleanup_free_
struct local_address
*addresses
= NULL
;
252 struct local_address
*a
;
255 pair
[0] = safe_close(pair
[0]);
257 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
261 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
262 struct iovec iov
[2] = {
263 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
264 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
267 r
= writev(pair
[1], iov
, 2);
272 pair
[1] = safe_close(pair
[1]);
277 pair
[1] = safe_close(pair
[1]);
282 union in_addr_union in_addr
;
289 iov
[0] = IOVEC_MAKE(&family
, sizeof(family
));
290 iov
[1] = IOVEC_MAKE(&in_addr
, sizeof(in_addr
));
292 n
= recvmsg(pair
[0], &mh
, 0);
295 if ((size_t) n
< sizeof(family
))
298 r
= sd_bus_message_open_container(reply
, 'r', "iay");
302 r
= sd_bus_message_append(reply
, "i", family
);
309 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
312 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
316 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
319 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
325 r
= sd_bus_message_close_container(reply
);
330 r
= wait_for_terminate_and_check("(sd-addrns)", child
, 0);
332 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
333 if (r
!= EXIT_SUCCESS
)
334 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
339 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
342 r
= sd_bus_message_close_container(reply
);
346 return sd_bus_send(NULL
, reply
, NULL
);
349 #define EXIT_NOT_FOUND 2
351 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
352 _cleanup_strv_free_
char **l
= NULL
;
353 Machine
*m
= userdata
;
362 r
= load_os_release_pairs(NULL
, &l
);
368 case MACHINE_CONTAINER
: {
369 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1, pidns_fd
= -1;
370 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
371 _cleanup_fclose_
FILE *f
= NULL
;
374 r
= namespace_open(m
->leader
, &pidns_fd
, &mntns_fd
, NULL
, NULL
, &root_fd
);
378 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
381 r
= namespace_fork("(sd-osrelns)", "(sd-osrel)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
382 pidns_fd
, mntns_fd
, -1, -1, root_fd
,
385 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
389 pair
[0] = safe_close(pair
[0]);
391 r
= open_os_release(NULL
, NULL
, &fd
);
393 _exit(EXIT_NOT_FOUND
);
397 r
= copy_bytes(fd
, pair
[1], (uint64_t) -1, 0);
404 pair
[1] = safe_close(pair
[1]);
406 f
= take_fdopen(&pair
[0], "r");
410 r
= load_env_file_pairs(f
, "/etc/os-release", &l
);
414 r
= wait_for_terminate_and_check("(sd-osrelns)", child
, 0);
416 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
417 if (r
== EXIT_NOT_FOUND
)
418 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Machine does not contain OS release information");
419 if (r
!= EXIT_SUCCESS
)
420 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
426 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
429 return bus_reply_pair_array(message
, l
);
432 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
433 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
434 _cleanup_free_
char *pty_name
= NULL
;
435 _cleanup_close_
int master
= -1;
436 Machine
*m
= userdata
;
442 r
= bus_verify_polkit_async(
445 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
449 &m
->manager
->polkit_registry
,
454 return 1; /* Will call us back */
456 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
460 r
= sd_bus_message_new_method_return(message
, &reply
);
464 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
468 return sd_bus_send(NULL
, reply
, NULL
);
471 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
483 case MACHINE_CONTAINER
: {
484 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
487 r
= sd_bus_new(&bus
);
491 if (asprintf(&address
, "x-machine-unix:pid=%" PID_PRI
, m
->leader
) < 0)
494 bus
->address
= address
;
495 bus
->bus_client
= true;
496 bus
->trusted
= false;
497 bus
->is_system
= true;
499 r
= sd_bus_start(bus
);
501 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
505 *ret
= TAKE_PTR(bus
);
516 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
517 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
518 _cleanup_free_
char *pty_name
= NULL
;
519 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
520 _cleanup_close_
int master
= -1;
521 sd_bus
*container_bus
= NULL
;
522 Machine
*m
= userdata
;
523 const char *p
, *getty
;
529 r
= bus_verify_polkit_async(
532 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login",
536 &m
->manager
->polkit_registry
,
541 return 1; /* Will call us back */
543 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
547 p
= path_startswith(pty_name
, "/dev/pts/");
550 r
= container_bus_new(m
, error
, &allocated_bus
);
554 container_bus
= allocated_bus
?: m
->manager
->bus
;
556 getty
= strjoina("container-getty@", p
, ".service");
558 r
= bus_call_method(container_bus
, bus_systemd_mgr
, "StartUnit", error
, NULL
, "ss", getty
, "replace");
562 r
= sd_bus_message_new_method_return(message
, &reply
);
566 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
570 return sd_bus_send(NULL
, reply
, NULL
);
573 int bus_machine_method_open_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
574 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *tm
= NULL
;
575 _cleanup_free_
char *pty_name
= NULL
;
576 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
577 sd_bus
*container_bus
= NULL
;
578 _cleanup_close_
int master
= -1, slave
= -1;
579 _cleanup_strv_free_
char **env
= NULL
, **args_wire
= NULL
, **args
= NULL
;
580 Machine
*m
= userdata
;
581 const char *p
, *unit
, *user
, *path
, *description
, *utmp_id
;
587 r
= sd_bus_message_read(message
, "ss", &user
, &path
);
590 user
= isempty(user
) ? "root" : user
;
591 r
= sd_bus_message_read_strv(message
, &args_wire
);
597 args
= new0(char*, 3 + 1);
600 args
[0] = strdup("sh");
603 args
[1] = strdup("-c");
606 r
= asprintf(&args
[2],
607 "shell=$(getent passwd %s 2>/dev/null | { IFS=: read _ _ _ _ _ _ x; echo \"$x\"; })\n"\
608 "exec \"${shell:-/bin/sh}\" -l", /* -l is means --login */
615 if (!path_is_absolute(path
))
616 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified path '%s' is not absolute", path
);
617 args
= TAKE_PTR(args_wire
);
618 if (strv_isempty(args
)) {
619 args
= strv_free(args
);
621 args
= strv_new(path
);
627 r
= sd_bus_message_read_strv(message
, &env
);
630 if (!strv_env_is_valid(env
))
631 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
633 const char *details
[] = {
640 r
= bus_verify_polkit_async(
643 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
647 &m
->manager
->polkit_registry
,
652 return 1; /* Will call us back */
654 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
658 p
= path_startswith(pty_name
, "/dev/pts/");
661 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
665 utmp_id
= path_startswith(pty_name
, "/dev/");
668 r
= container_bus_new(m
, error
, &allocated_bus
);
672 container_bus
= allocated_bus
?: m
->manager
->bus
;
674 r
= bus_message_new_method_call(container_bus
, &tm
, bus_systemd_mgr
, "StartTransientUnit");
679 unit
= strjoina("container-shell@", p
, ".service");
680 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
685 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
689 description
= strjoina("Shell for User ", user
);
690 r
= sd_bus_message_append(tm
,
691 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
692 "Description", "s", description
,
693 "StandardInputFileDescriptor", "h", slave
,
694 "StandardOutputFileDescriptor", "h", slave
,
695 "StandardErrorFileDescriptor", "h", slave
,
696 "SendSIGHUP", "b", true,
697 "IgnoreSIGPIPE", "b", false,
698 "KillMode", "s", "mixed",
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
= userdata
;
822 if (m
->class != MACHINE_CONTAINER
)
823 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
825 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_file_or_directory
);
829 if (!path_is_absolute(src
) || !path_is_normalized(src
))
830 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and normalized.");
834 else if (!path_is_absolute(dest
) || !path_is_normalized(dest
))
835 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and normalized.");
837 r
= bus_verify_polkit_async(
840 "org.freedesktop.machine1.manage-machines",
844 &m
->manager
->polkit_registry
,
849 return 1; /* Will call us back */
851 r
= machine_get_uid_shift(m
, &uid
);
855 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Can't bind mount on container with user namespacing applied.");
857 propagate_directory
= strjoina("/run/systemd/nspawn/propagate/", m
->name
);
858 r
= bind_mount_in_namespace(m
->leader
,
860 "/run/host/incoming/",
861 src
, dest
, read_only
, make_file_or_directory
);
863 return sd_bus_error_set_errnof(error
, r
, "Failed to mount %s on %s in machine's namespace: %m", src
, dest
);
865 return sd_bus_reply_method_return(message
, NULL
);
868 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
869 const char *src
, *dest
, *host_path
, *container_path
, *host_basename
, *container_basename
, *container_dirname
;
870 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
871 CopyFlags copy_flags
= COPY_REFLINK
|COPY_MERGE
|COPY_HARDLINKS
;
872 _cleanup_close_
int hostfd
= -1;
873 Machine
*m
= userdata
;
883 if (m
->manager
->n_operations
>= OPERATIONS_MAX
)
884 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
886 if (m
->class != MACHINE_CONTAINER
)
887 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
889 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
893 if (!path_is_absolute(src
))
894 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
898 else if (!path_is_absolute(dest
))
899 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
901 r
= bus_verify_polkit_async(
904 "org.freedesktop.machine1.manage-machines",
908 &m
->manager
->polkit_registry
,
913 return 1; /* Will call us back */
915 r
= machine_get_uid_shift(m
, &uid_shift
);
919 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
922 container_path
= src
;
926 container_path
= dest
;
929 host_basename
= basename(host_path
);
931 container_basename
= basename(container_path
);
932 t
= strdupa(container_path
);
933 container_dirname
= dirname(t
);
935 hostfd
= open_parent(host_path
, O_CLOEXEC
, 0);
937 return sd_bus_error_set_errnof(error
, hostfd
, "Failed to open host directory %s: %m", host_path
);
939 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
940 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
942 r
= safe_fork("(sd-copy)", FORK_RESET_SIGNALS
, &child
);
944 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
950 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
952 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
953 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
955 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
959 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
960 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
964 containerfd
= open(container_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
965 if (containerfd
< 0) {
966 r
= log_error_errno(errno
, "Failed to open destination directory: %m");
970 /* Run the actual copy operation. Note that when an UID shift is set we'll either clamp the UID/GID to
971 * 0 or to the actual UID shift depending on the direction we copy. If no UID shift is set we'll copy
972 * the UID/GIDs as they are. */
974 r
= copy_tree_at(containerfd
, container_basename
, hostfd
, host_basename
, uid_shift
== 0 ? UID_INVALID
: 0, uid_shift
== 0 ? GID_INVALID
: 0, copy_flags
);
976 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
);
978 hostfd
= safe_close(hostfd
);
979 containerfd
= safe_close(containerfd
);
982 r
= log_error_errno(r
, "Failed to copy tree: %m");
989 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
993 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
995 /* Copying might take a while, hence install a watch on the child, and return */
997 r
= operation_new(m
->manager
, m
, child
, message
, errno_pipe_fd
[0], NULL
);
999 (void) sigkill_wait(child
);
1002 errno_pipe_fd
[0] = -1;
1007 int bus_machine_method_open_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1008 _cleanup_close_
int fd
= -1;
1009 Machine
*m
= userdata
;
1015 r
= bus_verify_polkit_async(
1018 "org.freedesktop.machine1.manage-machines",
1022 &m
->manager
->polkit_registry
,
1027 return 1; /* Will call us back */
1032 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1038 case MACHINE_CONTAINER
: {
1039 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
1040 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1043 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
1047 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1050 r
= namespace_fork("(sd-openrootns)", "(sd-openroot)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
1051 -1, mntns_fd
, -1, -1, root_fd
, &child
);
1053 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1055 _cleanup_close_
int dfd
= -1;
1057 pair
[0] = safe_close(pair
[0]);
1059 dfd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1061 _exit(EXIT_FAILURE
);
1063 r
= send_one_fd(pair
[1], dfd
, 0);
1064 dfd
= safe_close(dfd
);
1066 _exit(EXIT_FAILURE
);
1068 _exit(EXIT_SUCCESS
);
1071 pair
[1] = safe_close(pair
[1]);
1073 r
= wait_for_terminate_and_check("(sd-openrootns)", child
, 0);
1075 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1076 if (r
!= EXIT_SUCCESS
)
1077 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1079 fd
= receive_one_fd(pair
[0], MSG_DONTWAIT
);
1087 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Opening the root directory is only supported on container machines.");
1090 return sd_bus_reply_method_return(message
, "h", fd
);
1093 int bus_machine_method_get_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1094 Machine
*m
= userdata
;
1101 /* You wonder why this is a method and not a property? Well, properties are not supposed to return errors, but
1102 * we kinda have to for this. */
1104 if (m
->class == MACHINE_HOST
)
1105 return sd_bus_reply_method_return(message
, "u", UINT32_C(0));
1107 if (m
->class != MACHINE_CONTAINER
)
1108 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "UID/GID shift may only be determined for container machines.");
1110 r
= machine_get_uid_shift(m
, &shift
);
1112 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Machine %s uses a complex UID/GID mapping, cannot determine shift", m
->name
);
1116 return sd_bus_reply_method_return(message
, "u", (uint32_t) shift
);
1119 static int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1120 Manager
*m
= userdata
;
1130 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1131 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1132 sd_bus_message
*message
;
1135 message
= sd_bus_get_current_message(bus
);
1139 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1143 r
= sd_bus_creds_get_pid(creds
, &pid
);
1147 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1151 _cleanup_free_
char *e
= NULL
;
1154 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1158 e
= bus_label_unescape(p
);
1162 machine
= hashmap_get(m
->machines
, e
);
1171 char *machine_bus_path(Machine
*m
) {
1172 _cleanup_free_
char *e
= NULL
;
1176 e
= bus_label_escape(m
->name
);
1180 return strjoin("/org/freedesktop/machine1/machine/", e
);
1183 static int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1184 _cleanup_strv_free_
char **l
= NULL
;
1185 Machine
*machine
= NULL
;
1186 Manager
*m
= userdata
;
1193 HASHMAP_FOREACH(machine
, m
->machines
) {
1196 p
= machine_bus_path(machine
);
1200 r
= strv_consume(&l
, p
);
1205 *nodes
= TAKE_PTR(l
);
1210 static const sd_bus_vtable machine_vtable
[] = {
1211 SD_BUS_VTABLE_START(0),
1212 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1213 SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128
, offsetof(Machine
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1214 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1215 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1216 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1217 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1218 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
), SD_BUS_VTABLE_PROPERTY_CONST
),
1219 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1220 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1221 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1222 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1224 SD_BUS_METHOD("Terminate",
1227 bus_machine_method_terminate
,
1228 SD_BUS_VTABLE_UNPRIVILEGED
),
1229 SD_BUS_METHOD_WITH_NAMES("Kill",
1232 SD_BUS_PARAM(signal
),
1234 bus_machine_method_kill
,
1235 SD_BUS_VTABLE_UNPRIVILEGED
),
1236 SD_BUS_METHOD_WITH_NAMES("GetAddresses",
1239 SD_BUS_PARAM(addresses
),
1240 bus_machine_method_get_addresses
,
1241 SD_BUS_VTABLE_UNPRIVILEGED
),
1242 SD_BUS_METHOD_WITH_NAMES("GetOSRelease",
1245 SD_BUS_PARAM(fields
),
1246 bus_machine_method_get_os_release
,
1247 SD_BUS_VTABLE_UNPRIVILEGED
),
1248 SD_BUS_METHOD_WITH_NAMES("GetUIDShift",
1251 SD_BUS_PARAM(shift
),
1252 bus_machine_method_get_uid_shift
,
1253 SD_BUS_VTABLE_UNPRIVILEGED
),
1254 SD_BUS_METHOD_WITH_NAMES("OpenPTY",
1258 SD_BUS_PARAM(pty_path
),
1259 bus_machine_method_open_pty
,
1260 SD_BUS_VTABLE_UNPRIVILEGED
),
1261 SD_BUS_METHOD_WITH_NAMES("OpenLogin",
1265 SD_BUS_PARAM(pty_path
),
1266 bus_machine_method_open_login
,
1267 SD_BUS_VTABLE_UNPRIVILEGED
),
1268 SD_BUS_METHOD_WITH_NAMES("OpenShell",
1273 SD_BUS_PARAM(environment
),
1276 SD_BUS_PARAM(pty_path
),
1277 bus_machine_method_open_shell
,
1278 SD_BUS_VTABLE_UNPRIVILEGED
),
1279 SD_BUS_METHOD_WITH_NAMES("BindMount",
1281 SD_BUS_PARAM(source
)
1282 SD_BUS_PARAM(destination
)
1283 SD_BUS_PARAM(read_only
)
1284 SD_BUS_PARAM(mkdir
),
1286 bus_machine_method_bind_mount
,
1287 SD_BUS_VTABLE_UNPRIVILEGED
),
1288 SD_BUS_METHOD_WITH_NAMES("CopyFrom",
1290 SD_BUS_PARAM(source
)
1291 SD_BUS_PARAM(destination
),
1293 bus_machine_method_copy
,
1294 SD_BUS_VTABLE_UNPRIVILEGED
),
1295 SD_BUS_METHOD_WITH_NAMES("CopyTo",
1297 SD_BUS_PARAM(source
)
1298 SD_BUS_PARAM(destination
),
1300 bus_machine_method_copy
,
1301 SD_BUS_VTABLE_UNPRIVILEGED
),
1302 SD_BUS_METHOD_WITH_NAMES("OpenRootDirectory",
1306 bus_machine_method_open_root_directory
,
1307 SD_BUS_VTABLE_UNPRIVILEGED
),
1312 const BusObjectImplementation machine_object
= {
1313 "/org/freedesktop/machine1/machine",
1314 "org.freedesktop.machine1.Machine",
1315 .fallback_vtables
= BUS_FALLBACK_VTABLES({machine_vtable
, machine_object_find
}),
1316 .node_enumerator
= machine_node_enumerator
,
1319 int machine_send_signal(Machine
*m
, bool new_machine
) {
1320 _cleanup_free_
char *p
= NULL
;
1324 p
= machine_bus_path(m
);
1328 return sd_bus_emit_signal(
1330 "/org/freedesktop/machine1",
1331 "org.freedesktop.machine1.Manager",
1332 new_machine
? "MachineNew" : "MachineRemoved",
1336 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1337 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1338 _cleanup_free_
char *p
= NULL
;
1342 if (!m
->create_message
)
1345 c
= TAKE_PTR(m
->create_message
);
1348 return sd_bus_reply_method_error(c
, error
);
1350 /* Update the machine state file before we notify the client
1351 * about the result. */
1354 p
= machine_bus_path(m
);
1358 return sd_bus_reply_method_return(c
, "o", p
);