1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 /* When we include libgen.h because we need dirname() we immediately
9 * undefine basename() since libgen.h defines it as a macro to the POSIX
10 * version which is really broken. We prefer GNU basename(). */
14 #include "alloc-util.h"
15 #include "bus-common-errors.h"
16 #include "bus-internal.h"
17 #include "bus-label.h"
23 #include "format-util.h"
25 #include "in-addr-util.h"
27 #include "local-addresses.h"
28 #include "machine-dbus.h"
30 #include "missing_capability.h"
32 #include "namespace-util.h"
34 #include "path-util.h"
35 #include "process-util.h"
36 #include "signal-util.h"
38 #include "terminal-util.h"
39 #include "tmpfile-util.h"
40 #include "user-util.h"
42 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class
, machine_class
, MachineClass
);
43 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", Machine
, machine_get_state
, machine_state_to_string
);
45 static int property_get_netif(
48 const char *interface
,
50 sd_bus_message
*reply
,
52 sd_bus_error
*error
) {
54 Machine
*m
= userdata
;
60 assert_cc(sizeof(int) == sizeof(int32_t));
62 return sd_bus_message_append_array(reply
, 'i', m
->netif
, m
->n_netif
* sizeof(int));
65 int bus_machine_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
66 Machine
*m
= userdata
;
72 r
= bus_verify_polkit_async(
75 "org.freedesktop.machine1.manage-machines",
79 &m
->manager
->polkit_registry
,
84 return 1; /* Will call us back */
90 return sd_bus_reply_method_return(message
, NULL
);
93 int bus_machine_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
94 Machine
*m
= userdata
;
103 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
110 who
= kill_who_from_string(swho
);
112 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
115 if (!SIGNAL_VALID(signo
))
116 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
118 r
= bus_verify_polkit_async(
121 "org.freedesktop.machine1.manage-machines",
125 &m
->manager
->polkit_registry
,
130 return 1; /* Will call us back */
132 r
= machine_kill(m
, who
, signo
);
136 return sd_bus_reply_method_return(message
, NULL
);
139 int bus_machine_method_get_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
140 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
141 Machine
*m
= userdata
;
147 r
= sd_bus_message_new_method_return(message
, &reply
);
151 r
= sd_bus_message_open_container(reply
, 'a', "(iay)");
158 _cleanup_free_
struct local_address
*addresses
= NULL
;
159 struct local_address
*a
;
162 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
166 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
168 r
= sd_bus_message_open_container(reply
, 'r', "iay");
172 r
= sd_bus_message_append(reply
, "i", addresses
[i
].family
);
176 r
= sd_bus_message_append_array(reply
, 'y', &addresses
[i
].address
, FAMILY_ADDRESS_SIZE(addresses
[i
].family
));
180 r
= sd_bus_message_close_container(reply
);
188 case MACHINE_CONTAINER
: {
189 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
190 _cleanup_free_
char *us
= NULL
, *them
= NULL
;
191 _cleanup_close_
int netns_fd
= -1;
195 r
= readlink_malloc("/proc/self/ns/net", &us
);
199 p
= procfs_file_alloca(m
->leader
, "ns/net");
200 r
= readlink_malloc(p
, &them
);
205 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_NETWORKING
, "Machine %s does not use private networking", m
->name
);
207 r
= namespace_open(m
->leader
, NULL
, NULL
, &netns_fd
, NULL
, NULL
);
211 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
214 r
= namespace_fork("(sd-addrns)", "(sd-addr)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
215 -1, -1, netns_fd
, -1, -1, &child
);
217 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
219 _cleanup_free_
struct local_address
*addresses
= NULL
;
220 struct local_address
*a
;
223 pair
[0] = safe_close(pair
[0]);
225 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
229 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
230 struct iovec iov
[2] = {
231 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
232 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
235 r
= writev(pair
[1], iov
, 2);
240 pair
[1] = safe_close(pair
[1]);
245 pair
[1] = safe_close(pair
[1]);
250 union in_addr_union in_addr
;
257 iov
[0] = IOVEC_MAKE(&family
, sizeof(family
));
258 iov
[1] = IOVEC_MAKE(&in_addr
, sizeof(in_addr
));
260 n
= recvmsg(pair
[0], &mh
, 0);
263 if ((size_t) n
< sizeof(family
))
266 r
= sd_bus_message_open_container(reply
, 'r', "iay");
270 r
= sd_bus_message_append(reply
, "i", family
);
277 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
280 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
284 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
287 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
293 r
= sd_bus_message_close_container(reply
);
298 r
= wait_for_terminate_and_check("(sd-addrns)", child
, 0);
300 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
301 if (r
!= EXIT_SUCCESS
)
302 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
307 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
310 r
= sd_bus_message_close_container(reply
);
314 return sd_bus_send(NULL
, reply
, NULL
);
317 #define EXIT_NOT_FOUND 2
319 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
320 _cleanup_strv_free_
char **l
= NULL
;
321 Machine
*m
= userdata
;
330 r
= load_os_release_pairs(NULL
, &l
);
336 case MACHINE_CONTAINER
: {
337 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1, pidns_fd
= -1;
338 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
339 _cleanup_fclose_
FILE *f
= NULL
;
342 r
= namespace_open(m
->leader
, &pidns_fd
, &mntns_fd
, NULL
, NULL
, &root_fd
);
346 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
349 r
= namespace_fork("(sd-osrelns)", "(sd-osrel)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
350 pidns_fd
, mntns_fd
, -1, -1, root_fd
,
353 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
357 pair
[0] = safe_close(pair
[0]);
359 r
= open_os_release(NULL
, NULL
, &fd
);
361 _exit(EXIT_NOT_FOUND
);
365 r
= copy_bytes(fd
, pair
[1], (uint64_t) -1, 0);
372 pair
[1] = safe_close(pair
[1]);
374 f
= fdopen(pair
[0], "r");
380 r
= load_env_file_pairs(f
, "/etc/os-release", &l
);
384 r
= wait_for_terminate_and_check("(sd-osrelns)", child
, 0);
386 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
387 if (r
== EXIT_NOT_FOUND
)
388 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Machine does not contain OS release information");
389 if (r
!= EXIT_SUCCESS
)
390 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
396 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
399 return bus_reply_pair_array(message
, l
);
402 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
403 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
404 _cleanup_free_
char *pty_name
= NULL
;
405 _cleanup_close_
int master
= -1;
406 Machine
*m
= userdata
;
412 r
= bus_verify_polkit_async(
415 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
419 &m
->manager
->polkit_registry
,
424 return 1; /* Will call us back */
426 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
430 r
= sd_bus_message_new_method_return(message
, &reply
);
434 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
438 return sd_bus_send(NULL
, reply
, NULL
);
441 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
453 case MACHINE_CONTAINER
: {
454 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
457 r
= sd_bus_new(&bus
);
461 if (asprintf(&address
, "x-machine-kernel:pid=%1$" PID_PRI
";x-machine-unix:pid=%1$" PID_PRI
, m
->leader
) < 0)
464 bus
->address
= address
;
465 bus
->bus_client
= true;
466 bus
->trusted
= false;
467 bus
->is_system
= true;
469 r
= sd_bus_start(bus
);
471 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
475 *ret
= TAKE_PTR(bus
);
486 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
487 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
488 _cleanup_free_
char *pty_name
= NULL
;
489 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
490 _cleanup_close_
int master
= -1;
491 sd_bus
*container_bus
= NULL
;
492 Machine
*m
= userdata
;
493 const char *p
, *getty
;
499 r
= bus_verify_polkit_async(
502 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login",
506 &m
->manager
->polkit_registry
,
511 return 1; /* Will call us back */
513 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
517 p
= path_startswith(pty_name
, "/dev/pts/");
520 r
= container_bus_new(m
, error
, &allocated_bus
);
524 container_bus
= allocated_bus
?: m
->manager
->bus
;
526 getty
= strjoina("container-getty@", p
, ".service");
528 r
= sd_bus_call_method(
530 "org.freedesktop.systemd1",
531 "/org/freedesktop/systemd1",
532 "org.freedesktop.systemd1.Manager",
535 "ss", getty
, "replace");
539 r
= sd_bus_message_new_method_return(message
, &reply
);
543 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
547 return sd_bus_send(NULL
, reply
, NULL
);
550 int bus_machine_method_open_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
551 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *tm
= NULL
;
552 _cleanup_free_
char *pty_name
= NULL
;
553 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
554 sd_bus
*container_bus
= NULL
;
555 _cleanup_close_
int master
= -1, slave
= -1;
556 _cleanup_strv_free_
char **env
= NULL
, **args_wire
= NULL
, **args
= NULL
;
557 Machine
*m
= userdata
;
558 const char *p
, *unit
, *user
, *path
, *description
, *utmp_id
;
564 r
= sd_bus_message_read(message
, "ss", &user
, &path
);
567 user
= empty_to_null(user
);
568 r
= sd_bus_message_read_strv(message
, &args_wire
);
574 args
= new0(char*, 3 + 1);
577 args
[0] = strdup("sh");
580 args
[1] = strdup("-c");
583 r
= asprintf(&args
[2],
584 "shell=$(getent passwd %s 2>/dev/null | { IFS=: read _ _ _ _ _ _ x; echo \"$x\"; })\n"\
585 "exec \"${shell:-/bin/sh}\" -l", /* -l is means --login */
586 isempty(user
) ? "root" : user
);
592 if (!path_is_absolute(path
))
593 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified path '%s' is not absolute", path
);
594 args
= TAKE_PTR(args_wire
);
595 if (strv_isempty(args
)) {
596 args
= strv_free(args
);
598 args
= strv_new(path
);
604 r
= sd_bus_message_read_strv(message
, &env
);
607 if (!strv_env_is_valid(env
))
608 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
610 r
= bus_verify_polkit_async(
613 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
617 &m
->manager
->polkit_registry
,
622 return 1; /* Will call us back */
624 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
628 p
= path_startswith(pty_name
, "/dev/pts/");
631 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
635 utmp_id
= path_startswith(pty_name
, "/dev/");
638 r
= container_bus_new(m
, error
, &allocated_bus
);
642 container_bus
= allocated_bus
?: m
->manager
->bus
;
644 r
= sd_bus_message_new_method_call(
647 "org.freedesktop.systemd1",
648 "/org/freedesktop/systemd1",
649 "org.freedesktop.systemd1.Manager",
650 "StartTransientUnit");
655 unit
= strjoina("container-shell@", p
, ".service");
656 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
661 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
665 description
= strjoina("Shell for User ", isempty(user
) ? "root" : user
);
666 r
= sd_bus_message_append(tm
,
667 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
668 "Description", "s", description
,
669 "StandardInputFileDescriptor", "h", slave
,
670 "StandardOutputFileDescriptor", "h", slave
,
671 "StandardErrorFileDescriptor", "h", slave
,
672 "SendSIGHUP", "b", true,
673 "IgnoreSIGPIPE", "b", false,
674 "KillMode", "s", "mixed",
675 "TTYReset", "b", true,
676 "UtmpIdentifier", "s", utmp_id
,
677 "UtmpMode", "s", "user",
678 "PAMName", "s", "login",
679 "WorkingDirectory", "s", "-~");
683 r
= sd_bus_message_append(tm
, "(sv)", "User", "s", isempty(user
) ? "root" : user
);
687 if (!strv_isempty(env
)) {
688 r
= sd_bus_message_open_container(tm
, 'r', "sv");
692 r
= sd_bus_message_append(tm
, "s", "Environment");
696 r
= sd_bus_message_open_container(tm
, 'v', "as");
700 r
= sd_bus_message_append_strv(tm
, env
);
704 r
= sd_bus_message_close_container(tm
);
708 r
= sd_bus_message_close_container(tm
);
714 r
= sd_bus_message_open_container(tm
, 'r', "sv");
718 r
= sd_bus_message_append(tm
, "s", "ExecStart");
722 r
= sd_bus_message_open_container(tm
, 'v', "a(sasb)");
726 r
= sd_bus_message_open_container(tm
, 'a', "(sasb)");
730 r
= sd_bus_message_open_container(tm
, 'r', "sasb");
734 r
= sd_bus_message_append(tm
, "s", path
);
738 r
= sd_bus_message_append_strv(tm
, args
);
742 r
= sd_bus_message_append(tm
, "b", true);
746 r
= sd_bus_message_close_container(tm
);
750 r
= sd_bus_message_close_container(tm
);
754 r
= sd_bus_message_close_container(tm
);
758 r
= sd_bus_message_close_container(tm
);
762 r
= sd_bus_message_close_container(tm
);
766 /* Auxiliary units */
767 r
= sd_bus_message_append(tm
, "a(sa(sv))", 0);
771 r
= sd_bus_call(container_bus
, tm
, 0, error
, NULL
);
775 slave
= safe_close(slave
);
777 r
= sd_bus_message_new_method_return(message
, &reply
);
781 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
785 return sd_bus_send(NULL
, reply
, NULL
);
788 int bus_machine_method_bind_mount(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
789 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
790 char mount_slave
[] = "/tmp/propagate.XXXXXX", *mount_tmp
, *mount_outside
, *p
;
791 bool mount_slave_created
= false, mount_slave_mounted
= false,
792 mount_tmp_created
= false, mount_tmp_mounted
= false,
793 mount_outside_created
= false, mount_outside_mounted
= false;
794 _cleanup_free_
char *chased_src
= NULL
;
795 int read_only
, make_file_or_directory
;
796 const char *dest
, *src
;
797 Machine
*m
= userdata
;
806 if (m
->class != MACHINE_CONTAINER
)
807 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
809 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_file_or_directory
);
813 if (!path_is_absolute(src
) || !path_is_normalized(src
))
814 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and not contain ../.");
818 else if (!path_is_absolute(dest
) || !path_is_normalized(dest
))
819 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and not contain ../.");
821 r
= bus_verify_polkit_async(
824 "org.freedesktop.machine1.manage-machines",
828 &m
->manager
->polkit_registry
,
833 return 1; /* Will call us back */
835 r
= machine_get_uid_shift(m
, &uid
);
839 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Can't bind mount on container with user namespacing applied.");
841 /* One day, when bind mounting /proc/self/fd/n works across
842 * namespace boundaries we should rework this logic to make
845 p
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/");
846 if (laccess(p
, F_OK
) < 0)
847 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Container does not allow propagation of mount points.");
849 r
= chase_symlinks(src
, NULL
, CHASE_TRAIL_SLASH
, &chased_src
);
851 return sd_bus_error_set_errnof(error
, r
, "Failed to resolve source path: %m");
853 if (lstat(chased_src
, &st
) < 0)
854 return sd_bus_error_set_errnof(error
, errno
, "Failed to stat() source path: %m");
855 if (S_ISLNK(st
.st_mode
)) /* This shouldn't really happen, given that we just chased the symlinks above, but let's better be safe… */
856 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Source directory can't be a symbolic link");
858 /* Our goal is to install a new bind mount into the container,
859 possibly read-only. This is irritatingly complex
860 unfortunately, currently.
862 First, we start by creating a private playground in /tmp,
863 that we can mount MS_SLAVE. (Which is necessary, since
864 MS_MOVE cannot be applied to mounts with MS_SHARED parent
867 if (!mkdtemp(mount_slave
))
868 return sd_bus_error_set_errnof(error
, errno
, "Failed to create playground %s: %m", mount_slave
);
870 mount_slave_created
= true;
872 if (mount(mount_slave
, mount_slave
, NULL
, MS_BIND
, NULL
) < 0) {
873 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to make bind mount %s: %m", mount_slave
);
877 mount_slave_mounted
= true;
879 if (mount(NULL
, mount_slave
, NULL
, MS_SLAVE
, NULL
) < 0) {
880 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount slave %s: %m", mount_slave
);
884 /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
885 mount_tmp
= strjoina(mount_slave
, "/mount");
886 if (S_ISDIR(st
.st_mode
))
887 r
= mkdir_errno_wrapper(mount_tmp
, 0700);
889 r
= touch(mount_tmp
);
891 sd_bus_error_set_errnof(error
, errno
, "Failed to create temporary mount point %s: %m", mount_tmp
);
895 mount_tmp_created
= true;
897 if (mount(chased_src
, mount_tmp
, NULL
, MS_BIND
, NULL
) < 0) {
898 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to mount %s: %m", chased_src
);
902 mount_tmp_mounted
= true;
904 /* Third, we remount the new bind mount read-only if requested. */
906 if (mount(NULL
, mount_tmp
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
) < 0) {
907 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount read-only %s: %m", mount_tmp
);
911 /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
914 mount_outside
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/XXXXXX");
915 if (S_ISDIR(st
.st_mode
))
916 r
= mkdtemp(mount_outside
) ? 0 : -errno
;
918 r
= mkostemp_safe(mount_outside
);
922 sd_bus_error_set_errnof(error
, errno
, "Cannot create propagation file or directory %s: %m", mount_outside
);
926 mount_outside_created
= true;
928 if (mount(mount_tmp
, mount_outside
, NULL
, MS_MOVE
, NULL
) < 0) {
929 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to move %s to %s: %m", mount_tmp
, mount_outside
);
933 mount_outside_mounted
= true;
934 mount_tmp_mounted
= false;
936 if (S_ISDIR(st
.st_mode
))
937 (void) rmdir(mount_tmp
);
939 (void) unlink(mount_tmp
);
940 mount_tmp_created
= false;
942 (void) umount(mount_slave
);
943 mount_slave_mounted
= false;
945 (void) rmdir(mount_slave
);
946 mount_slave_created
= false;
948 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0) {
949 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
953 r
= safe_fork("(sd-bindmnt)", FORK_RESET_SIGNALS
, &child
);
955 sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
959 const char *mount_inside
;
963 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
965 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
966 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
968 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
972 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
973 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
977 if (make_file_or_directory
) {
978 if (S_ISDIR(st
.st_mode
))
979 (void) mkdir_p(dest
, 0755);
981 (void) mkdir_parents(dest
, 0755);
982 safe_close(open(dest
, O_CREAT
|O_EXCL
|O_WRONLY
|O_CLOEXEC
|O_NOCTTY
, 0600));
986 /* Fifth, move the mount to the right place inside */
987 mount_inside
= strjoina("/run/systemd/nspawn/incoming/", basename(mount_outside
));
988 if (mount(mount_inside
, dest
, NULL
, MS_MOVE
, NULL
) < 0) {
989 r
= log_error_errno(errno
, "Failed to mount: %m");
996 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
997 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1002 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1004 r
= wait_for_terminate_and_check("(sd-bindmnt)", child
, 0);
1006 r
= sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1009 if (r
!= EXIT_SUCCESS
) {
1010 if (read(errno_pipe_fd
[0], &r
, sizeof(r
)) == sizeof(r
))
1011 r
= sd_bus_error_set_errnof(error
, r
, "Failed to mount: %m");
1013 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child failed.");
1017 r
= sd_bus_reply_method_return(message
, NULL
);
1020 if (mount_outside_mounted
)
1021 (void) umount(mount_outside
);
1022 if (mount_outside_created
) {
1023 if (S_ISDIR(st
.st_mode
))
1024 (void) rmdir(mount_outside
);
1026 (void) unlink(mount_outside
);
1029 if (mount_tmp_mounted
)
1030 (void) umount(mount_tmp
);
1031 if (mount_tmp_created
) {
1032 if (S_ISDIR(st
.st_mode
))
1033 (void) rmdir(mount_tmp
);
1035 (void) unlink(mount_tmp
);
1038 if (mount_slave_mounted
)
1039 (void) umount(mount_slave
);
1040 if (mount_slave_created
)
1041 (void) rmdir(mount_slave
);
1046 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1047 const char *src
, *dest
, *host_path
, *container_path
, *host_basename
, *container_basename
, *container_dirname
;
1048 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
1049 CopyFlags copy_flags
= COPY_REFLINK
|COPY_MERGE
;
1050 _cleanup_close_
int hostfd
= -1;
1051 Machine
*m
= userdata
;
1061 if (m
->manager
->n_operations
>= OPERATIONS_MAX
)
1062 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
1064 if (m
->class != MACHINE_CONTAINER
)
1065 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
1067 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
1071 if (!path_is_absolute(src
))
1072 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
1076 else if (!path_is_absolute(dest
))
1077 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
1079 r
= bus_verify_polkit_async(
1082 "org.freedesktop.machine1.manage-machines",
1086 &m
->manager
->polkit_registry
,
1091 return 1; /* Will call us back */
1093 r
= machine_get_uid_shift(m
, &uid_shift
);
1097 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
1100 container_path
= src
;
1104 container_path
= dest
;
1107 host_basename
= basename(host_path
);
1109 container_basename
= basename(container_path
);
1110 t
= strdupa(container_path
);
1111 container_dirname
= dirname(t
);
1113 hostfd
= open_parent(host_path
, O_CLOEXEC
, 0);
1115 return sd_bus_error_set_errnof(error
, hostfd
, "Failed to open host directory %s: %m", host_path
);
1117 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1118 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1120 r
= safe_fork("(sd-copy)", FORK_RESET_SIGNALS
, &child
);
1122 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1128 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1130 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
1131 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
1133 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
1137 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
1138 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
1142 containerfd
= open(container_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1143 if (containerfd
< 0) {
1144 r
= log_error_errno(errno
, "Failed to open destination directory: %m");
1148 /* Run the actual copy operation. Note that when an UID shift is set we'll either clamp the UID/GID to
1149 * 0 or to the actual UID shift depending on the direction we copy. If no UID shift is set we'll copy
1150 * the UID/GIDs as they are. */
1152 r
= copy_tree_at(containerfd
, container_basename
, hostfd
, host_basename
, uid_shift
== 0 ? UID_INVALID
: 0, uid_shift
== 0 ? GID_INVALID
: 0, copy_flags
);
1154 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
);
1156 hostfd
= safe_close(hostfd
);
1157 containerfd
= safe_close(containerfd
);
1160 r
= log_error_errno(r
, "Failed to copy tree: %m");
1164 _exit(EXIT_SUCCESS
);
1167 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1168 _exit(EXIT_FAILURE
);
1171 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1173 /* Copying might take a while, hence install a watch on the child, and return */
1175 r
= operation_new(m
->manager
, m
, child
, message
, errno_pipe_fd
[0], NULL
);
1177 (void) sigkill_wait(child
);
1180 errno_pipe_fd
[0] = -1;
1185 int bus_machine_method_open_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1186 _cleanup_close_
int fd
= -1;
1187 Machine
*m
= userdata
;
1193 r
= bus_verify_polkit_async(
1196 "org.freedesktop.machine1.manage-machines",
1200 &m
->manager
->polkit_registry
,
1205 return 1; /* Will call us back */
1210 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1216 case MACHINE_CONTAINER
: {
1217 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
1218 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1221 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
1225 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1228 r
= namespace_fork("(sd-openrootns)", "(sd-openroot)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
1229 -1, mntns_fd
, -1, -1, root_fd
, &child
);
1231 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1233 _cleanup_close_
int dfd
= -1;
1235 pair
[0] = safe_close(pair
[0]);
1237 dfd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1239 _exit(EXIT_FAILURE
);
1241 r
= send_one_fd(pair
[1], dfd
, 0);
1242 dfd
= safe_close(dfd
);
1244 _exit(EXIT_FAILURE
);
1246 _exit(EXIT_SUCCESS
);
1249 pair
[1] = safe_close(pair
[1]);
1251 r
= wait_for_terminate_and_check("(sd-openrootns)", child
, 0);
1253 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1254 if (r
!= EXIT_SUCCESS
)
1255 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1257 fd
= receive_one_fd(pair
[0], MSG_DONTWAIT
);
1265 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Opening the root directory is only supported on container machines.");
1268 return sd_bus_reply_method_return(message
, "h", fd
);
1271 int bus_machine_method_get_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1272 Machine
*m
= userdata
;
1279 /* You wonder why this is a method and not a property? Well, properties are not supposed to return errors, but
1280 * we kinda have to for this. */
1282 if (m
->class == MACHINE_HOST
)
1283 return sd_bus_reply_method_return(message
, "u", UINT32_C(0));
1285 if (m
->class != MACHINE_CONTAINER
)
1286 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "UID/GID shift may only be determined for container machines.");
1288 r
= machine_get_uid_shift(m
, &shift
);
1290 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Machine %s uses a complex UID/GID mapping, cannot determine shift", m
->name
);
1294 return sd_bus_reply_method_return(message
, "u", (uint32_t) shift
);
1297 const sd_bus_vtable machine_vtable
[] = {
1298 SD_BUS_VTABLE_START(0),
1299 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1300 SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128
, offsetof(Machine
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1301 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1302 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1303 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1304 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1305 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
), SD_BUS_VTABLE_PROPERTY_CONST
),
1306 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1307 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1308 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1309 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1310 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_machine_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
1311 SD_BUS_METHOD("Kill", "si", NULL
, bus_machine_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
1312 SD_BUS_METHOD("GetAddresses", NULL
, "a(iay)", bus_machine_method_get_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1313 SD_BUS_METHOD("GetOSRelease", NULL
, "a{ss}", bus_machine_method_get_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1314 SD_BUS_METHOD("GetUIDShift", NULL
, "u", bus_machine_method_get_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1315 SD_BUS_METHOD("OpenPTY", NULL
, "hs", bus_machine_method_open_pty
, SD_BUS_VTABLE_UNPRIVILEGED
),
1316 SD_BUS_METHOD("OpenLogin", NULL
, "hs", bus_machine_method_open_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1317 SD_BUS_METHOD("OpenShell", "ssasas", "hs", bus_machine_method_open_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1318 SD_BUS_METHOD("BindMount", "ssbb", NULL
, bus_machine_method_bind_mount
, SD_BUS_VTABLE_UNPRIVILEGED
),
1319 SD_BUS_METHOD("CopyFrom", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1320 SD_BUS_METHOD("CopyTo", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1321 SD_BUS_METHOD("OpenRootDirectory", NULL
, "h", bus_machine_method_open_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1325 int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1326 Manager
*m
= userdata
;
1336 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1337 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1338 sd_bus_message
*message
;
1341 message
= sd_bus_get_current_message(bus
);
1345 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1349 r
= sd_bus_creds_get_pid(creds
, &pid
);
1353 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1357 _cleanup_free_
char *e
= NULL
;
1360 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1364 e
= bus_label_unescape(p
);
1368 machine
= hashmap_get(m
->machines
, e
);
1377 char *machine_bus_path(Machine
*m
) {
1378 _cleanup_free_
char *e
= NULL
;
1382 e
= bus_label_escape(m
->name
);
1386 return strappend("/org/freedesktop/machine1/machine/", e
);
1389 int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1390 _cleanup_strv_free_
char **l
= NULL
;
1391 Machine
*machine
= NULL
;
1392 Manager
*m
= userdata
;
1400 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1403 p
= machine_bus_path(machine
);
1407 r
= strv_consume(&l
, p
);
1412 *nodes
= TAKE_PTR(l
);
1417 int machine_send_signal(Machine
*m
, bool new_machine
) {
1418 _cleanup_free_
char *p
= NULL
;
1422 p
= machine_bus_path(m
);
1426 return sd_bus_emit_signal(
1428 "/org/freedesktop/machine1",
1429 "org.freedesktop.machine1.Manager",
1430 new_machine
? "MachineNew" : "MachineRemoved",
1434 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1435 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1436 _cleanup_free_
char *p
= NULL
;
1440 if (!m
->create_message
)
1443 c
= TAKE_PTR(m
->create_message
);
1446 return sd_bus_reply_method_error(c
, error
);
1448 /* Update the machine state file before we notify the client
1449 * about the result. */
1452 p
= machine_bus_path(m
);
1456 return sd_bus_reply_method_return(c
, "o", p
);