1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 "namespace-util.h"
36 #include "path-util.h"
37 #include "process-util.h"
38 #include "signal-util.h"
40 #include "terminal-util.h"
41 #include "tmpfile-util.h"
42 #include "user-util.h"
44 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class
, machine_class
, MachineClass
);
45 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", Machine
, machine_get_state
, machine_state_to_string
);
47 static int property_get_netif(
50 const char *interface
,
52 sd_bus_message
*reply
,
54 sd_bus_error
*error
) {
56 Machine
*m
= userdata
;
62 assert_cc(sizeof(int) == sizeof(int32_t));
64 return sd_bus_message_append_array(reply
, 'i', m
->netif
, m
->n_netif
* sizeof(int));
67 int bus_machine_method_unregister(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
68 Machine
*m
= userdata
;
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
= userdata
;
102 r
= bus_verify_polkit_async(
105 "org.freedesktop.machine1.manage-machines",
109 &m
->manager
->polkit_registry
,
114 return 1; /* Will call us back */
120 return sd_bus_reply_method_return(message
, NULL
);
123 int bus_machine_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
124 Machine
*m
= userdata
;
133 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
140 who
= kill_who_from_string(swho
);
142 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
145 if (!SIGNAL_VALID(signo
))
146 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
148 r
= bus_verify_polkit_async(
151 "org.freedesktop.machine1.manage-machines",
155 &m
->manager
->polkit_registry
,
160 return 1; /* Will call us back */
162 r
= machine_kill(m
, who
, signo
);
166 return sd_bus_reply_method_return(message
, NULL
);
169 int bus_machine_method_get_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
170 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
171 Machine
*m
= 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
;
189 struct local_address
*a
;
192 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
196 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
198 r
= sd_bus_message_open_container(reply
, 'r', "iay");
202 r
= sd_bus_message_append(reply
, "i", addresses
[i
].family
);
206 r
= sd_bus_message_append_array(reply
, 'y', &addresses
[i
].address
, FAMILY_ADDRESS_SIZE(addresses
[i
].family
));
210 r
= sd_bus_message_close_container(reply
);
218 case MACHINE_CONTAINER
: {
219 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
220 _cleanup_free_
char *us
= NULL
, *them
= NULL
;
221 _cleanup_close_
int netns_fd
= -1;
225 r
= readlink_malloc("/proc/self/ns/net", &us
);
229 p
= procfs_file_alloca(m
->leader
, "ns/net");
230 r
= readlink_malloc(p
, &them
);
235 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_NETWORKING
, "Machine %s does not use private networking", m
->name
);
237 r
= namespace_open(m
->leader
, NULL
, NULL
, &netns_fd
, NULL
, NULL
);
241 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
244 r
= namespace_fork("(sd-addrns)", "(sd-addr)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
245 -1, -1, netns_fd
, -1, -1, &child
);
247 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
249 _cleanup_free_
struct local_address
*addresses
= NULL
;
250 struct local_address
*a
;
253 pair
[0] = safe_close(pair
[0]);
255 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
259 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
260 struct iovec iov
[2] = {
261 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
262 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
265 r
= writev(pair
[1], iov
, 2);
270 pair
[1] = safe_close(pair
[1]);
275 pair
[1] = safe_close(pair
[1]);
280 union in_addr_union in_addr
;
287 iov
[0] = IOVEC_MAKE(&family
, sizeof(family
));
288 iov
[1] = IOVEC_MAKE(&in_addr
, sizeof(in_addr
));
290 n
= recvmsg(pair
[0], &mh
, 0);
293 if ((size_t) n
< sizeof(family
))
296 r
= sd_bus_message_open_container(reply
, 'r', "iay");
300 r
= sd_bus_message_append(reply
, "i", family
);
307 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
310 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
314 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
317 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
323 r
= sd_bus_message_close_container(reply
);
328 r
= wait_for_terminate_and_check("(sd-addrns)", child
, 0);
330 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
331 if (r
!= EXIT_SUCCESS
)
332 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
337 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
340 r
= sd_bus_message_close_container(reply
);
344 return sd_bus_send(NULL
, reply
, NULL
);
347 #define EXIT_NOT_FOUND 2
349 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
350 _cleanup_strv_free_
char **l
= NULL
;
351 Machine
*m
= userdata
;
360 r
= load_os_release_pairs(NULL
, &l
);
366 case MACHINE_CONTAINER
: {
367 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1, pidns_fd
= -1;
368 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
369 _cleanup_fclose_
FILE *f
= NULL
;
372 r
= namespace_open(m
->leader
, &pidns_fd
, &mntns_fd
, NULL
, NULL
, &root_fd
);
376 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
379 r
= namespace_fork("(sd-osrelns)", "(sd-osrel)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
380 pidns_fd
, mntns_fd
, -1, -1, root_fd
,
383 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
387 pair
[0] = safe_close(pair
[0]);
389 r
= open_os_release(NULL
, NULL
, &fd
);
391 _exit(EXIT_NOT_FOUND
);
395 r
= copy_bytes(fd
, pair
[1], (uint64_t) -1, 0);
402 pair
[1] = safe_close(pair
[1]);
404 f
= take_fdopen(&pair
[0], "r");
408 r
= load_env_file_pairs(f
, "/etc/os-release", &l
);
412 r
= wait_for_terminate_and_check("(sd-osrelns)", child
, 0);
414 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
415 if (r
== EXIT_NOT_FOUND
)
416 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Machine does not contain OS release information");
417 if (r
!= EXIT_SUCCESS
)
418 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
424 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
427 return bus_reply_pair_array(message
, l
);
430 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
431 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
432 _cleanup_free_
char *pty_name
= NULL
;
433 _cleanup_close_
int master
= -1;
434 Machine
*m
= userdata
;
440 r
= bus_verify_polkit_async(
443 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
447 &m
->manager
->polkit_registry
,
452 return 1; /* Will call us back */
454 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
458 r
= sd_bus_message_new_method_return(message
, &reply
);
462 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
466 return sd_bus_send(NULL
, reply
, NULL
);
469 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
481 case MACHINE_CONTAINER
: {
482 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
485 r
= sd_bus_new(&bus
);
489 if (asprintf(&address
, "x-machine-kernel:pid=%1$" PID_PRI
";x-machine-unix:pid=%1$" PID_PRI
, m
->leader
) < 0)
492 bus
->address
= address
;
493 bus
->bus_client
= true;
494 bus
->trusted
= false;
495 bus
->is_system
= true;
497 r
= sd_bus_start(bus
);
499 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
503 *ret
= TAKE_PTR(bus
);
514 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
515 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
516 _cleanup_free_
char *pty_name
= NULL
;
517 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
518 _cleanup_close_
int master
= -1;
519 sd_bus
*container_bus
= NULL
;
520 Machine
*m
= userdata
;
521 const char *p
, *getty
;
527 r
= bus_verify_polkit_async(
530 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
= -1, slave
= -1;
577 _cleanup_strv_free_
char **env
= NULL
, **args_wire
= NULL
, **args
= NULL
;
578 Machine
*m
= userdata
;
579 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_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
631 const char *details
[] = {
638 r
= bus_verify_polkit_async(
641 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
645 &m
->manager
->polkit_registry
,
650 return 1; /* Will call us back */
652 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
, &pty_name
);
656 p
= path_startswith(pty_name
, "/dev/pts/");
659 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
663 utmp_id
= path_startswith(pty_name
, "/dev/");
666 r
= container_bus_new(m
, error
, &allocated_bus
);
670 container_bus
= allocated_bus
?: m
->manager
->bus
;
672 r
= bus_message_new_method_call(container_bus
, &tm
, bus_systemd_mgr
, "StartTransientUnit");
677 unit
= strjoina("container-shell@", p
, ".service");
678 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
683 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
687 description
= strjoina("Shell for User ", user
);
688 r
= sd_bus_message_append(tm
,
689 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
690 "Description", "s", description
,
691 "StandardInputFileDescriptor", "h", slave
,
692 "StandardOutputFileDescriptor", "h", slave
,
693 "StandardErrorFileDescriptor", "h", slave
,
694 "SendSIGHUP", "b", true,
695 "IgnoreSIGPIPE", "b", false,
696 "KillMode", "s", "mixed",
697 "TTYReset", "b", true,
698 "UtmpIdentifier", "s", utmp_id
,
699 "UtmpMode", "s", "user",
700 "PAMName", "s", "login",
701 "WorkingDirectory", "s", "-~");
705 r
= sd_bus_message_append(tm
, "(sv)", "User", "s", user
);
709 if (!strv_isempty(env
)) {
710 r
= sd_bus_message_open_container(tm
, 'r', "sv");
714 r
= sd_bus_message_append(tm
, "s", "Environment");
718 r
= sd_bus_message_open_container(tm
, 'v', "as");
722 r
= sd_bus_message_append_strv(tm
, env
);
726 r
= sd_bus_message_close_container(tm
);
730 r
= sd_bus_message_close_container(tm
);
736 r
= sd_bus_message_open_container(tm
, 'r', "sv");
740 r
= sd_bus_message_append(tm
, "s", "ExecStart");
744 r
= sd_bus_message_open_container(tm
, 'v', "a(sasb)");
748 r
= sd_bus_message_open_container(tm
, 'a', "(sasb)");
752 r
= sd_bus_message_open_container(tm
, 'r', "sasb");
756 r
= sd_bus_message_append(tm
, "s", path
);
760 r
= sd_bus_message_append_strv(tm
, args
);
764 r
= sd_bus_message_append(tm
, "b", true);
768 r
= sd_bus_message_close_container(tm
);
772 r
= sd_bus_message_close_container(tm
);
776 r
= sd_bus_message_close_container(tm
);
780 r
= sd_bus_message_close_container(tm
);
784 r
= sd_bus_message_close_container(tm
);
788 /* Auxiliary units */
789 r
= sd_bus_message_append(tm
, "a(sa(sv))", 0);
793 r
= sd_bus_call(container_bus
, tm
, 0, error
, NULL
);
797 slave
= safe_close(slave
);
799 r
= sd_bus_message_new_method_return(message
, &reply
);
803 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
807 return sd_bus_send(NULL
, reply
, NULL
);
810 int bus_machine_method_bind_mount(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
811 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
812 char mount_slave
[] = "/tmp/propagate.XXXXXX", *mount_tmp
, *mount_outside
, *p
;
813 bool mount_slave_created
= false, mount_slave_mounted
= false,
814 mount_tmp_created
= false, mount_tmp_mounted
= false,
815 mount_outside_created
= false, mount_outside_mounted
= false;
816 _cleanup_free_
char *chased_src
= NULL
;
817 int read_only
, make_file_or_directory
;
818 const char *dest
, *src
;
819 Machine
*m
= userdata
;
828 if (m
->class != MACHINE_CONTAINER
)
829 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
831 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_file_or_directory
);
835 if (!path_is_absolute(src
) || !path_is_normalized(src
))
836 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and not contain ../.");
840 else if (!path_is_absolute(dest
) || !path_is_normalized(dest
))
841 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and not contain ../.");
843 r
= bus_verify_polkit_async(
846 "org.freedesktop.machine1.manage-machines",
850 &m
->manager
->polkit_registry
,
855 return 1; /* Will call us back */
857 r
= machine_get_uid_shift(m
, &uid
);
861 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Can't bind mount on container with user namespacing applied.");
863 /* One day, when bind mounting /proc/self/fd/n works across
864 * namespace boundaries we should rework this logic to make
867 p
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/");
868 if (laccess(p
, F_OK
) < 0)
869 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Container does not allow propagation of mount points.");
871 r
= chase_symlinks(src
, NULL
, CHASE_TRAIL_SLASH
, &chased_src
, NULL
);
873 return sd_bus_error_set_errnof(error
, r
, "Failed to resolve source path: %m");
875 if (lstat(chased_src
, &st
) < 0)
876 return sd_bus_error_set_errnof(error
, errno
, "Failed to stat() source path: %m");
877 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… */
878 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Source directory can't be a symbolic link");
880 /* Our goal is to install a new bind mount into the container,
881 possibly read-only. This is irritatingly complex
882 unfortunately, currently.
884 First, we start by creating a private playground in /tmp,
885 that we can mount MS_SLAVE. (Which is necessary, since
886 MS_MOVE cannot be applied to mounts with MS_SHARED parent
889 if (!mkdtemp(mount_slave
))
890 return sd_bus_error_set_errnof(error
, errno
, "Failed to create playground %s: %m", mount_slave
);
892 mount_slave_created
= true;
894 if (mount(mount_slave
, mount_slave
, NULL
, MS_BIND
, NULL
) < 0) {
895 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to make bind mount %s: %m", mount_slave
);
899 mount_slave_mounted
= true;
901 if (mount(NULL
, mount_slave
, NULL
, MS_SLAVE
, NULL
) < 0) {
902 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount slave %s: %m", mount_slave
);
906 /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
907 mount_tmp
= strjoina(mount_slave
, "/mount");
908 if (S_ISDIR(st
.st_mode
))
909 r
= mkdir_errno_wrapper(mount_tmp
, 0700);
911 r
= touch(mount_tmp
);
913 sd_bus_error_set_errnof(error
, errno
, "Failed to create temporary mount point %s: %m", mount_tmp
);
917 mount_tmp_created
= true;
919 if (mount(chased_src
, mount_tmp
, NULL
, MS_BIND
, NULL
) < 0) {
920 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to mount %s: %m", chased_src
);
924 mount_tmp_mounted
= true;
926 /* Third, we remount the new bind mount read-only if requested. */
928 if (mount(NULL
, mount_tmp
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
) < 0) {
929 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount read-only %s: %m", mount_tmp
);
933 /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
936 mount_outside
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/XXXXXX");
937 if (S_ISDIR(st
.st_mode
))
938 r
= mkdtemp(mount_outside
) ? 0 : -errno
;
940 r
= mkostemp_safe(mount_outside
);
944 sd_bus_error_set_errnof(error
, errno
, "Cannot create propagation file or directory %s: %m", mount_outside
);
948 mount_outside_created
= true;
950 if (mount(mount_tmp
, mount_outside
, NULL
, MS_MOVE
, NULL
) < 0) {
951 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to move %s to %s: %m", mount_tmp
, mount_outside
);
955 mount_outside_mounted
= true;
956 mount_tmp_mounted
= false;
958 if (S_ISDIR(st
.st_mode
))
959 (void) rmdir(mount_tmp
);
961 (void) unlink(mount_tmp
);
962 mount_tmp_created
= false;
964 (void) umount(mount_slave
);
965 mount_slave_mounted
= false;
967 (void) rmdir(mount_slave
);
968 mount_slave_created
= false;
970 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0) {
971 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
975 r
= safe_fork("(sd-bindmnt)", FORK_RESET_SIGNALS
, &child
);
977 sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
981 const char *mount_inside
;
985 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
987 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
988 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
990 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
994 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
995 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
999 if (make_file_or_directory
) {
1000 if (S_ISDIR(st
.st_mode
))
1001 (void) mkdir_p(dest
, 0755);
1003 (void) mkdir_parents(dest
, 0755);
1004 safe_close(open(dest
, O_CREAT
|O_EXCL
|O_WRONLY
|O_CLOEXEC
|O_NOCTTY
, 0600));
1008 /* Fifth, move the mount to the right place inside */
1009 mount_inside
= strjoina("/run/systemd/nspawn/incoming/", basename(mount_outside
));
1010 if (mount(mount_inside
, dest
, NULL
, MS_MOVE
, NULL
) < 0) {
1011 r
= log_error_errno(errno
, "Failed to mount: %m");
1015 _exit(EXIT_SUCCESS
);
1018 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1019 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1021 _exit(EXIT_FAILURE
);
1024 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1026 r
= wait_for_terminate_and_check("(sd-bindmnt)", child
, 0);
1028 r
= sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1031 if (r
!= EXIT_SUCCESS
) {
1032 if (read(errno_pipe_fd
[0], &r
, sizeof(r
)) == sizeof(r
))
1033 r
= sd_bus_error_set_errnof(error
, r
, "Failed to mount: %m");
1035 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child failed.");
1039 r
= sd_bus_reply_method_return(message
, NULL
);
1042 if (mount_outside_mounted
)
1043 (void) umount(mount_outside
);
1044 if (mount_outside_created
) {
1045 if (S_ISDIR(st
.st_mode
))
1046 (void) rmdir(mount_outside
);
1048 (void) unlink(mount_outside
);
1051 if (mount_tmp_mounted
)
1052 (void) umount(mount_tmp
);
1053 if (mount_tmp_created
) {
1054 if (S_ISDIR(st
.st_mode
))
1055 (void) rmdir(mount_tmp
);
1057 (void) unlink(mount_tmp
);
1060 if (mount_slave_mounted
)
1061 (void) umount(mount_slave
);
1062 if (mount_slave_created
)
1063 (void) rmdir(mount_slave
);
1068 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1069 const char *src
, *dest
, *host_path
, *container_path
, *host_basename
, *container_basename
, *container_dirname
;
1070 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
1071 CopyFlags copy_flags
= COPY_REFLINK
|COPY_MERGE
;
1072 _cleanup_close_
int hostfd
= -1;
1073 Machine
*m
= userdata
;
1083 if (m
->manager
->n_operations
>= OPERATIONS_MAX
)
1084 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
1086 if (m
->class != MACHINE_CONTAINER
)
1087 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
1089 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
1093 if (!path_is_absolute(src
))
1094 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
1098 else if (!path_is_absolute(dest
))
1099 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
1101 r
= bus_verify_polkit_async(
1104 "org.freedesktop.machine1.manage-machines",
1108 &m
->manager
->polkit_registry
,
1113 return 1; /* Will call us back */
1115 r
= machine_get_uid_shift(m
, &uid_shift
);
1119 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
1122 container_path
= src
;
1126 container_path
= dest
;
1129 host_basename
= basename(host_path
);
1131 container_basename
= basename(container_path
);
1132 t
= strdupa(container_path
);
1133 container_dirname
= dirname(t
);
1135 hostfd
= open_parent(host_path
, O_CLOEXEC
, 0);
1137 return sd_bus_error_set_errnof(error
, hostfd
, "Failed to open host directory %s: %m", host_path
);
1139 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1140 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1142 r
= safe_fork("(sd-copy)", FORK_RESET_SIGNALS
, &child
);
1144 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1150 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1152 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
1153 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
1155 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
1159 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
1160 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
1164 containerfd
= open(container_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1165 if (containerfd
< 0) {
1166 r
= log_error_errno(errno
, "Failed to open destination directory: %m");
1170 /* Run the actual copy operation. Note that when an UID shift is set we'll either clamp the UID/GID to
1171 * 0 or to the actual UID shift depending on the direction we copy. If no UID shift is set we'll copy
1172 * the UID/GIDs as they are. */
1174 r
= copy_tree_at(containerfd
, container_basename
, hostfd
, host_basename
, uid_shift
== 0 ? UID_INVALID
: 0, uid_shift
== 0 ? GID_INVALID
: 0, copy_flags
);
1176 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
);
1178 hostfd
= safe_close(hostfd
);
1179 containerfd
= safe_close(containerfd
);
1182 r
= log_error_errno(r
, "Failed to copy tree: %m");
1186 _exit(EXIT_SUCCESS
);
1189 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1190 _exit(EXIT_FAILURE
);
1193 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1195 /* Copying might take a while, hence install a watch on the child, and return */
1197 r
= operation_new(m
->manager
, m
, child
, message
, errno_pipe_fd
[0], NULL
);
1199 (void) sigkill_wait(child
);
1202 errno_pipe_fd
[0] = -1;
1207 int bus_machine_method_open_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1208 _cleanup_close_
int fd
= -1;
1209 Machine
*m
= userdata
;
1215 r
= bus_verify_polkit_async(
1218 "org.freedesktop.machine1.manage-machines",
1222 &m
->manager
->polkit_registry
,
1227 return 1; /* Will call us back */
1232 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1238 case MACHINE_CONTAINER
: {
1239 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
1240 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1243 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
1247 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1250 r
= namespace_fork("(sd-openrootns)", "(sd-openroot)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
1251 -1, mntns_fd
, -1, -1, root_fd
, &child
);
1253 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1255 _cleanup_close_
int dfd
= -1;
1257 pair
[0] = safe_close(pair
[0]);
1259 dfd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1261 _exit(EXIT_FAILURE
);
1263 r
= send_one_fd(pair
[1], dfd
, 0);
1264 dfd
= safe_close(dfd
);
1266 _exit(EXIT_FAILURE
);
1268 _exit(EXIT_SUCCESS
);
1271 pair
[1] = safe_close(pair
[1]);
1273 r
= wait_for_terminate_and_check("(sd-openrootns)", child
, 0);
1275 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1276 if (r
!= EXIT_SUCCESS
)
1277 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1279 fd
= receive_one_fd(pair
[0], MSG_DONTWAIT
);
1287 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Opening the root directory is only supported on container machines.");
1290 return sd_bus_reply_method_return(message
, "h", fd
);
1293 int bus_machine_method_get_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1294 Machine
*m
= userdata
;
1301 /* You wonder why this is a method and not a property? Well, properties are not supposed to return errors, but
1302 * we kinda have to for this. */
1304 if (m
->class == MACHINE_HOST
)
1305 return sd_bus_reply_method_return(message
, "u", UINT32_C(0));
1307 if (m
->class != MACHINE_CONTAINER
)
1308 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "UID/GID shift may only be determined for container machines.");
1310 r
= machine_get_uid_shift(m
, &shift
);
1312 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Machine %s uses a complex UID/GID mapping, cannot determine shift", m
->name
);
1316 return sd_bus_reply_method_return(message
, "u", (uint32_t) shift
);
1319 static int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1320 Manager
*m
= userdata
;
1330 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1331 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1332 sd_bus_message
*message
;
1335 message
= sd_bus_get_current_message(bus
);
1339 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1343 r
= sd_bus_creds_get_pid(creds
, &pid
);
1347 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1351 _cleanup_free_
char *e
= NULL
;
1354 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1358 e
= bus_label_unescape(p
);
1362 machine
= hashmap_get(m
->machines
, e
);
1371 char *machine_bus_path(Machine
*m
) {
1372 _cleanup_free_
char *e
= NULL
;
1376 e
= bus_label_escape(m
->name
);
1380 return strjoin("/org/freedesktop/machine1/machine/", e
);
1383 static int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1384 _cleanup_strv_free_
char **l
= NULL
;
1385 Machine
*machine
= NULL
;
1386 Manager
*m
= userdata
;
1394 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1397 p
= machine_bus_path(machine
);
1401 r
= strv_consume(&l
, p
);
1406 *nodes
= TAKE_PTR(l
);
1411 static const sd_bus_vtable machine_vtable
[] = {
1412 SD_BUS_VTABLE_START(0),
1413 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1414 SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128
, offsetof(Machine
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1415 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1416 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1417 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1418 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1419 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
), SD_BUS_VTABLE_PROPERTY_CONST
),
1420 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1421 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1422 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1423 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1425 SD_BUS_METHOD("Terminate",
1428 bus_machine_method_terminate
,
1429 SD_BUS_VTABLE_UNPRIVILEGED
),
1430 SD_BUS_METHOD_WITH_NAMES("Kill",
1433 SD_BUS_PARAM(signal
),
1435 bus_machine_method_kill
,
1436 SD_BUS_VTABLE_UNPRIVILEGED
),
1437 SD_BUS_METHOD_WITH_NAMES("GetAddresses",
1440 SD_BUS_PARAM(addresses
),
1441 bus_machine_method_get_addresses
,
1442 SD_BUS_VTABLE_UNPRIVILEGED
),
1443 SD_BUS_METHOD_WITH_NAMES("GetOSRelease",
1446 SD_BUS_PARAM(fields
),
1447 bus_machine_method_get_os_release
,
1448 SD_BUS_VTABLE_UNPRIVILEGED
),
1449 SD_BUS_METHOD_WITH_NAMES("GetUIDShift",
1452 SD_BUS_PARAM(shift
),
1453 bus_machine_method_get_uid_shift
,
1454 SD_BUS_VTABLE_UNPRIVILEGED
),
1455 SD_BUS_METHOD_WITH_NAMES("OpenPTY",
1459 SD_BUS_PARAM(pty_path
),
1460 bus_machine_method_open_pty
,
1461 SD_BUS_VTABLE_UNPRIVILEGED
),
1462 SD_BUS_METHOD_WITH_NAMES("OpenLogin",
1466 SD_BUS_PARAM(pty_path
),
1467 bus_machine_method_open_login
,
1468 SD_BUS_VTABLE_UNPRIVILEGED
),
1469 SD_BUS_METHOD_WITH_NAMES("OpenShell",
1474 SD_BUS_PARAM(environment
),
1477 SD_BUS_PARAM(pty_path
),
1478 bus_machine_method_open_shell
,
1479 SD_BUS_VTABLE_UNPRIVILEGED
),
1480 SD_BUS_METHOD_WITH_NAMES("BindMount",
1482 SD_BUS_PARAM(source
)
1483 SD_BUS_PARAM(destination
)
1484 SD_BUS_PARAM(read_only
)
1485 SD_BUS_PARAM(mkdir
),
1487 bus_machine_method_bind_mount
,
1488 SD_BUS_VTABLE_UNPRIVILEGED
),
1489 SD_BUS_METHOD_WITH_NAMES("CopyFrom",
1491 SD_BUS_PARAM(source
)
1492 SD_BUS_PARAM(destination
),
1494 bus_machine_method_copy
,
1495 SD_BUS_VTABLE_UNPRIVILEGED
),
1496 SD_BUS_METHOD_WITH_NAMES("CopyTo",
1498 SD_BUS_PARAM(source
)
1499 SD_BUS_PARAM(destination
),
1501 bus_machine_method_copy
,
1502 SD_BUS_VTABLE_UNPRIVILEGED
),
1503 SD_BUS_METHOD_WITH_NAMES("OpenRootDirectory",
1507 bus_machine_method_open_root_directory
,
1508 SD_BUS_VTABLE_UNPRIVILEGED
),
1513 const BusObjectImplementation machine_object
= {
1514 "/org/freedesktop/machine1/machine",
1515 "org.freedesktop.machine1.Machine",
1516 .fallback_vtables
= BUS_FALLBACK_VTABLES({machine_vtable
, machine_object_find
}),
1517 .node_enumerator
= machine_node_enumerator
,
1520 int machine_send_signal(Machine
*m
, bool new_machine
) {
1521 _cleanup_free_
char *p
= NULL
;
1525 p
= machine_bus_path(m
);
1529 return sd_bus_emit_signal(
1531 "/org/freedesktop/machine1",
1532 "org.freedesktop.machine1.Manager",
1533 new_machine
? "MachineNew" : "MachineRemoved",
1537 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1538 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1539 _cleanup_free_
char *p
= NULL
;
1543 if (!m
->create_message
)
1546 c
= TAKE_PTR(m
->create_message
);
1549 return sd_bus_reply_method_error(c
, error
);
1551 /* Update the machine state file before we notify the client
1552 * about the result. */
1555 p
= machine_bus_path(m
);
1559 return sd_bus_reply_method_return(c
, "o", p
);