1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
10 #include <sys/mount.h>
13 /* When we include libgen.h because we need dirname() we immediately
14 * undefine basename() since libgen.h defines it as a macro to the POSIX
15 * version which is really broken. We prefer GNU basename(). */
19 #include "alloc-util.h"
20 #include "bus-common-errors.h"
21 #include "bus-internal.h"
22 #include "bus-label.h"
28 #include "format-util.h"
30 #include "in-addr-util.h"
31 #include "local-addresses.h"
32 #include "machine-dbus.h"
36 #include "path-util.h"
37 #include "process-util.h"
38 #include "signal-util.h"
40 #include "terminal-util.h"
41 #include "user-util.h"
43 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class
, machine_class
, MachineClass
);
44 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", Machine
, machine_get_state
, machine_state_to_string
);
46 static int property_get_netif(
49 const char *interface
,
51 sd_bus_message
*reply
,
53 sd_bus_error
*error
) {
55 Machine
*m
= userdata
;
61 assert_cc(sizeof(int) == sizeof(int32_t));
63 return sd_bus_message_append_array(reply
, 'i', m
->netif
, m
->n_netif
* sizeof(int));
66 int bus_machine_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
67 Machine
*m
= userdata
;
73 r
= bus_verify_polkit_async(
76 "org.freedesktop.machine1.manage-machines",
80 &m
->manager
->polkit_registry
,
85 return 1; /* Will call us back */
91 return sd_bus_reply_method_return(message
, NULL
);
94 int bus_machine_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
95 Machine
*m
= userdata
;
104 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
111 who
= kill_who_from_string(swho
);
113 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
116 if (!SIGNAL_VALID(signo
))
117 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
119 r
= bus_verify_polkit_async(
122 "org.freedesktop.machine1.manage-machines",
126 &m
->manager
->polkit_registry
,
131 return 1; /* Will call us back */
133 r
= machine_kill(m
, who
, signo
);
137 return sd_bus_reply_method_return(message
, NULL
);
140 int bus_machine_method_get_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
141 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
142 Machine
*m
= userdata
;
148 r
= sd_bus_message_new_method_return(message
, &reply
);
152 r
= sd_bus_message_open_container(reply
, 'a', "(iay)");
159 _cleanup_free_
struct local_address
*addresses
= NULL
;
160 struct local_address
*a
;
163 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
167 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
169 r
= sd_bus_message_open_container(reply
, 'r', "iay");
173 r
= sd_bus_message_append(reply
, "i", addresses
[i
].family
);
177 r
= sd_bus_message_append_array(reply
, 'y', &addresses
[i
].address
, FAMILY_ADDRESS_SIZE(addresses
[i
].family
));
181 r
= sd_bus_message_close_container(reply
);
189 case MACHINE_CONTAINER
: {
190 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
191 _cleanup_free_
char *us
= NULL
, *them
= NULL
;
192 _cleanup_close_
int netns_fd
= -1;
196 r
= readlink_malloc("/proc/self/ns/net", &us
);
200 p
= procfs_file_alloca(m
->leader
, "ns/net");
201 r
= readlink_malloc(p
, &them
);
206 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_NETWORKING
, "Machine %s does not use private networking", m
->name
);
208 r
= namespace_open(m
->leader
, NULL
, NULL
, &netns_fd
, NULL
, NULL
);
212 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
215 r
= safe_fork("(sd-addr)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
, &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 r
= namespace_enter(-1, -1, netns_fd
, -1, -1);
229 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
233 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
234 struct iovec iov
[2] = {
235 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
236 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
239 r
= writev(pair
[1], iov
, 2);
244 pair
[1] = safe_close(pair
[1]);
249 pair
[1] = safe_close(pair
[1]);
254 union in_addr_union in_addr
;
261 iov
[0] = (struct iovec
) { .iov_base
= &family
, .iov_len
= sizeof(family
) };
262 iov
[1] = (struct iovec
) { .iov_base
= &in_addr
, .iov_len
= sizeof(in_addr
) };
264 n
= recvmsg(pair
[0], &mh
, 0);
267 if ((size_t) n
< sizeof(family
))
270 r
= sd_bus_message_open_container(reply
, 'r', "iay");
274 r
= sd_bus_message_append(reply
, "i", family
);
281 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
284 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
288 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
291 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
297 r
= sd_bus_message_close_container(reply
);
302 r
= wait_for_terminate_and_check("(sd-addr)", child
, 0);
304 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
305 if (r
!= EXIT_SUCCESS
)
306 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
311 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
314 r
= sd_bus_message_close_container(reply
);
318 return sd_bus_send(NULL
, reply
, NULL
);
321 #define EXIT_NOT_FOUND 2
323 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
324 _cleanup_strv_free_
char **l
= NULL
;
325 Machine
*m
= userdata
;
334 r
= load_os_release_pairs(NULL
, &l
);
340 case MACHINE_CONTAINER
: {
341 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
342 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
343 _cleanup_fclose_
FILE *f
= NULL
;
346 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
350 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
353 r
= safe_fork("(sd-osrel)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
, &child
);
355 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
359 pair
[0] = safe_close(pair
[0]);
361 r
= namespace_enter(-1, mntns_fd
, -1, -1, root_fd
);
365 r
= open_os_release(NULL
, NULL
, &fd
);
367 _exit(EXIT_NOT_FOUND
);
371 r
= copy_bytes(fd
, pair
[1], (uint64_t) -1, 0);
378 pair
[1] = safe_close(pair
[1]);
380 f
= fdopen(pair
[0], "re");
386 r
= load_env_file_pairs(f
, "/etc/os-release", NULL
, &l
);
390 r
= wait_for_terminate_and_check("(sd-osrel)", child
, 0);
392 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
393 if (r
== EXIT_NOT_FOUND
)
394 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Machine does not contain OS release information");
395 if (r
!= EXIT_SUCCESS
)
396 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
402 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
405 return bus_reply_pair_array(message
, l
);
408 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
409 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
410 _cleanup_free_
char *pty_name
= NULL
;
411 _cleanup_close_
int master
= -1;
412 Machine
*m
= userdata
;
418 r
= bus_verify_polkit_async(
421 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
425 &m
->manager
->polkit_registry
,
430 return 1; /* Will call us back */
432 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
436 r
= ptsname_namespace(master
, &pty_name
);
440 r
= sd_bus_message_new_method_return(message
, &reply
);
444 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
448 return sd_bus_send(NULL
, reply
, NULL
);
451 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
463 case MACHINE_CONTAINER
: {
464 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
467 r
= sd_bus_new(&bus
);
471 if (asprintf(&address
, "x-machine-kernel:pid=%1$" PID_PRI
";x-machine-unix:pid=%1$" PID_PRI
, m
->leader
) < 0)
474 bus
->address
= address
;
475 bus
->bus_client
= true;
476 bus
->trusted
= false;
477 bus
->is_system
= true;
479 r
= sd_bus_start(bus
);
481 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
485 *ret
= TAKE_PTR(bus
);
496 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
497 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
498 _cleanup_free_
char *pty_name
= NULL
;
499 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
500 _cleanup_close_
int master
= -1;
501 sd_bus
*container_bus
= NULL
;
502 Machine
*m
= userdata
;
503 const char *p
, *getty
;
509 r
= bus_verify_polkit_async(
512 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login",
516 &m
->manager
->polkit_registry
,
521 return 1; /* Will call us back */
523 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
527 r
= ptsname_namespace(master
, &pty_name
);
531 p
= path_startswith(pty_name
, "/dev/pts/");
533 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "PTS name %s is invalid", pty_name
);
535 r
= container_bus_new(m
, error
, &allocated_bus
);
539 container_bus
= allocated_bus
?: m
->manager
->bus
;
541 getty
= strjoina("container-getty@", p
, ".service");
543 r
= sd_bus_call_method(
545 "org.freedesktop.systemd1",
546 "/org/freedesktop/systemd1",
547 "org.freedesktop.systemd1.Manager",
550 "ss", getty
, "replace");
554 r
= sd_bus_message_new_method_return(message
, &reply
);
558 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
562 return sd_bus_send(NULL
, reply
, NULL
);
565 int bus_machine_method_open_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
566 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *tm
= NULL
;
567 _cleanup_free_
char *pty_name
= NULL
;
568 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
569 sd_bus
*container_bus
= NULL
;
570 _cleanup_close_
int master
= -1, slave
= -1;
571 _cleanup_strv_free_
char **env
= NULL
, **args_wire
= NULL
, **args
= NULL
;
572 Machine
*m
= userdata
;
573 const char *p
, *unit
, *user
, *path
, *description
, *utmp_id
;
579 r
= sd_bus_message_read(message
, "ss", &user
, &path
);
582 user
= empty_to_null(user
);
583 r
= sd_bus_message_read_strv(message
, &args_wire
);
589 args
= new0(char*, 3 + 1);
592 args
[0] = strdup("sh");
595 args
[1] = strdup("-c");
598 r
= asprintf(&args
[2],
599 "shell=$(getent passwd %s 2>/dev/null | { IFS=: read _ _ _ _ _ _ x; echo \"$x\"; })\n"\
600 "exec \"${shell:-/bin/sh}\" -l", /* -l is means --login */
601 isempty(user
) ? "root" : user
);
607 if (!path_is_absolute(path
))
608 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified path '%s' is not absolute", path
);
609 args
= TAKE_PTR(args_wire
);
610 if (strv_isempty(args
)) {
611 args
= strv_free(args
);
613 args
= strv_new(path
, NULL
);
619 r
= sd_bus_message_read_strv(message
, &env
);
622 if (!strv_env_is_valid(env
))
623 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
625 r
= bus_verify_polkit_async(
628 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
632 &m
->manager
->polkit_registry
,
637 return 1; /* Will call us back */
639 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
643 r
= ptsname_namespace(master
, &pty_name
);
647 p
= path_startswith(pty_name
, "/dev/pts/");
650 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
654 utmp_id
= path_startswith(pty_name
, "/dev/");
657 r
= container_bus_new(m
, error
, &allocated_bus
);
661 container_bus
= allocated_bus
?: m
->manager
->bus
;
663 r
= sd_bus_message_new_method_call(
666 "org.freedesktop.systemd1",
667 "/org/freedesktop/systemd1",
668 "org.freedesktop.systemd1.Manager",
669 "StartTransientUnit");
674 unit
= strjoina("container-shell@", p
, ".service");
675 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
680 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
684 description
= strjoina("Shell for User ", isempty(user
) ? "root" : user
);
685 r
= sd_bus_message_append(tm
,
686 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
687 "Description", "s", description
,
688 "StandardInputFileDescriptor", "h", slave
,
689 "StandardOutputFileDescriptor", "h", slave
,
690 "StandardErrorFileDescriptor", "h", slave
,
691 "SendSIGHUP", "b", true,
692 "IgnoreSIGPIPE", "b", false,
693 "KillMode", "s", "mixed",
694 "TTYReset", "b", true,
695 "UtmpIdentifier", "s", utmp_id
,
696 "UtmpMode", "s", "user",
697 "PAMName", "s", "login",
698 "WorkingDirectory", "s", "-~");
702 r
= sd_bus_message_append(tm
, "(sv)", "User", "s", isempty(user
) ? "root" : user
);
706 if (!strv_isempty(env
)) {
707 r
= sd_bus_message_open_container(tm
, 'r', "sv");
711 r
= sd_bus_message_append(tm
, "s", "Environment");
715 r
= sd_bus_message_open_container(tm
, 'v', "as");
719 r
= sd_bus_message_append_strv(tm
, env
);
723 r
= sd_bus_message_close_container(tm
);
727 r
= sd_bus_message_close_container(tm
);
733 r
= sd_bus_message_open_container(tm
, 'r', "sv");
737 r
= sd_bus_message_append(tm
, "s", "ExecStart");
741 r
= sd_bus_message_open_container(tm
, 'v', "a(sasb)");
745 r
= sd_bus_message_open_container(tm
, 'a', "(sasb)");
749 r
= sd_bus_message_open_container(tm
, 'r', "sasb");
753 r
= sd_bus_message_append(tm
, "s", path
);
757 r
= sd_bus_message_append_strv(tm
, args
);
761 r
= sd_bus_message_append(tm
, "b", true);
765 r
= sd_bus_message_close_container(tm
);
769 r
= sd_bus_message_close_container(tm
);
773 r
= sd_bus_message_close_container(tm
);
777 r
= sd_bus_message_close_container(tm
);
781 r
= sd_bus_message_close_container(tm
);
785 /* Auxiliary units */
786 r
= sd_bus_message_append(tm
, "a(sa(sv))", 0);
790 r
= sd_bus_call(container_bus
, tm
, 0, error
, NULL
);
794 slave
= safe_close(slave
);
796 r
= sd_bus_message_new_method_return(message
, &reply
);
800 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
804 return sd_bus_send(NULL
, reply
, NULL
);
807 int bus_machine_method_bind_mount(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
808 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
809 char mount_slave
[] = "/tmp/propagate.XXXXXX", *mount_tmp
, *mount_outside
, *p
;
810 bool mount_slave_created
= false, mount_slave_mounted
= false,
811 mount_tmp_created
= false, mount_tmp_mounted
= false,
812 mount_outside_created
= false, mount_outside_mounted
= false;
813 _cleanup_free_
char *chased_src
= NULL
;
814 int read_only
, make_file_or_directory
;
815 const char *dest
, *src
;
816 Machine
*m
= userdata
;
825 if (m
->class != MACHINE_CONTAINER
)
826 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
828 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_file_or_directory
);
832 if (!path_is_absolute(src
) || !path_is_normalized(src
))
833 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and not contain ../.");
837 else if (!path_is_absolute(dest
) || !path_is_normalized(dest
))
838 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and not contain ../.");
840 r
= bus_verify_polkit_async(
843 "org.freedesktop.machine1.manage-machines",
847 &m
->manager
->polkit_registry
,
852 return 1; /* Will call us back */
854 r
= machine_get_uid_shift(m
, &uid
);
858 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Can't bind mount on container with user namespacing applied.");
860 /* One day, when bind mounting /proc/self/fd/n works across
861 * namespace boundaries we should rework this logic to make
864 p
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/");
865 if (laccess(p
, F_OK
) < 0)
866 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Container does not allow propagation of mount points.");
868 r
= chase_symlinks(src
, NULL
, CHASE_TRAIL_SLASH
, &chased_src
);
870 return sd_bus_error_set_errnof(error
, r
, "Failed to resolve source path: %m");
872 if (lstat(chased_src
, &st
) < 0)
873 return sd_bus_error_set_errnof(error
, errno
, "Failed to stat() source path: %m");
874 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… */
875 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Source directory can't be a symbolic link");
877 /* Our goal is to install a new bind mount into the container,
878 possibly read-only. This is irritatingly complex
879 unfortunately, currently.
881 First, we start by creating a private playground in /tmp,
882 that we can mount MS_SLAVE. (Which is necessary, since
883 MS_MOVE cannot be applied to mounts with MS_SHARED parent
886 if (!mkdtemp(mount_slave
))
887 return sd_bus_error_set_errnof(error
, errno
, "Failed to create playground %s: %m", mount_slave
);
889 mount_slave_created
= true;
891 if (mount(mount_slave
, mount_slave
, NULL
, MS_BIND
, NULL
) < 0) {
892 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to make bind mount %s: %m", mount_slave
);
896 mount_slave_mounted
= true;
898 if (mount(NULL
, mount_slave
, NULL
, MS_SLAVE
, NULL
) < 0) {
899 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount slave %s: %m", mount_slave
);
903 /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
904 mount_tmp
= strjoina(mount_slave
, "/mount");
905 if (S_ISDIR(st
.st_mode
))
906 r
= mkdir_errno_wrapper(mount_tmp
, 0700);
908 r
= touch(mount_tmp
);
910 sd_bus_error_set_errnof(error
, errno
, "Failed to create temporary mount point %s: %m", mount_tmp
);
914 mount_tmp_created
= true;
916 if (mount(chased_src
, mount_tmp
, NULL
, MS_BIND
, NULL
) < 0) {
917 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to mount %s: %m", chased_src
);
921 mount_tmp_mounted
= true;
923 /* Third, we remount the new bind mount read-only if requested. */
925 if (mount(NULL
, mount_tmp
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
) < 0) {
926 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount read-only %s: %m", mount_tmp
);
930 /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
933 mount_outside
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/XXXXXX");
934 if (S_ISDIR(st
.st_mode
))
935 r
= mkdtemp(mount_outside
) ? 0 : -errno
;
937 r
= mkostemp_safe(mount_outside
);
941 sd_bus_error_set_errnof(error
, errno
, "Cannot create propagation file or directory %s: %m", mount_outside
);
945 mount_outside_created
= true;
947 if (mount(mount_tmp
, mount_outside
, NULL
, MS_MOVE
, NULL
) < 0) {
948 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to move %s to %s: %m", mount_tmp
, mount_outside
);
952 mount_outside_mounted
= true;
953 mount_tmp_mounted
= false;
955 if (S_ISDIR(st
.st_mode
))
956 (void) rmdir(mount_tmp
);
958 (void) unlink(mount_tmp
);
959 mount_tmp_created
= false;
961 (void) umount(mount_slave
);
962 mount_slave_mounted
= false;
964 (void) rmdir(mount_slave
);
965 mount_slave_created
= false;
967 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0) {
968 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
972 r
= safe_fork("(sd-bindmnt)", FORK_RESET_SIGNALS
, &child
);
974 sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
978 const char *mount_inside
;
982 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
984 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
985 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
987 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
991 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
992 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
996 if (make_file_or_directory
) {
997 if (S_ISDIR(st
.st_mode
))
998 (void) mkdir_p(dest
, 0755);
1000 (void) mkdir_parents(dest
, 0755);
1001 safe_close(open(dest
, O_CREAT
|O_EXCL
|O_WRONLY
|O_CLOEXEC
|O_NOCTTY
, 0600));
1005 /* Fifth, move the mount to the right place inside */
1006 mount_inside
= strjoina("/run/systemd/nspawn/incoming/", basename(mount_outside
));
1007 if (mount(mount_inside
, dest
, NULL
, MS_MOVE
, NULL
) < 0) {
1008 r
= log_error_errno(errno
, "Failed to mount: %m");
1012 _exit(EXIT_SUCCESS
);
1015 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1016 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1018 _exit(EXIT_FAILURE
);
1021 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1023 r
= wait_for_terminate_and_check("(sd-bindmnt)", child
, 0);
1025 r
= sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1028 if (r
!= EXIT_SUCCESS
) {
1029 if (read(errno_pipe_fd
[0], &r
, sizeof(r
)) == sizeof(r
))
1030 r
= sd_bus_error_set_errnof(error
, r
, "Failed to mount: %m");
1032 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child failed.");
1036 r
= sd_bus_reply_method_return(message
, NULL
);
1039 if (mount_outside_mounted
)
1040 (void) umount(mount_outside
);
1041 if (mount_outside_created
) {
1042 if (S_ISDIR(st
.st_mode
))
1043 (void) rmdir(mount_outside
);
1045 (void) unlink(mount_outside
);
1048 if (mount_tmp_mounted
)
1049 (void) umount(mount_tmp
);
1050 if (mount_tmp_created
) {
1051 if (S_ISDIR(st
.st_mode
))
1052 (void) rmdir(mount_tmp
);
1054 (void) unlink(mount_tmp
);
1057 if (mount_slave_mounted
)
1058 (void) umount(mount_slave
);
1059 if (mount_slave_created
)
1060 (void) rmdir(mount_slave
);
1065 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1066 const char *src
, *dest
, *host_path
, *container_path
, *host_basename
, *host_dirname
, *container_basename
, *container_dirname
;
1067 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
1068 CopyFlags copy_flags
= COPY_REFLINK
|COPY_MERGE
;
1069 _cleanup_close_
int hostfd
= -1;
1070 Machine
*m
= userdata
;
1080 if (m
->manager
->n_operations
>= OPERATIONS_MAX
)
1081 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
1083 if (m
->class != MACHINE_CONTAINER
)
1084 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
1086 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
1090 if (!path_is_absolute(src
))
1091 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
1095 else if (!path_is_absolute(dest
))
1096 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
1098 r
= bus_verify_polkit_async(
1101 "org.freedesktop.machine1.manage-machines",
1105 &m
->manager
->polkit_registry
,
1110 return 1; /* Will call us back */
1112 r
= machine_get_uid_shift(m
, &uid_shift
);
1116 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
1119 container_path
= src
;
1123 container_path
= dest
;
1126 host_basename
= basename(host_path
);
1127 t
= strdupa(host_path
);
1128 host_dirname
= dirname(t
);
1130 container_basename
= basename(container_path
);
1131 t
= strdupa(container_path
);
1132 container_dirname
= dirname(t
);
1134 hostfd
= open(host_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1136 return sd_bus_error_set_errnof(error
, errno
, "Failed to open host directory %s: %m", host_dirname
);
1138 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1139 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1141 r
= safe_fork("(sd-copy)", FORK_RESET_SIGNALS
, &child
);
1143 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1149 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1151 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
1152 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
1154 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
1158 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
1159 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
1163 containerfd
= open(container_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1164 if (containerfd
< 0) {
1165 r
= log_error_errno(errno
, "Failed to open destination directory: %m");
1169 /* Run the actual copy operation. Note that when an UID shift is set we'll either clamp the UID/GID to
1170 * 0 or to the actual UID shift depending on the direction we copy. If no UID shift is set we'll copy
1171 * the UID/GIDs as they are. */
1173 r
= copy_tree_at(containerfd
, container_basename
, hostfd
, host_basename
, uid_shift
== 0 ? UID_INVALID
: 0, uid_shift
== 0 ? GID_INVALID
: 0, copy_flags
);
1175 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
);
1177 hostfd
= safe_close(hostfd
);
1178 containerfd
= safe_close(containerfd
);
1181 r
= log_error_errno(r
, "Failed to copy tree: %m");
1185 _exit(EXIT_SUCCESS
);
1188 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1189 _exit(EXIT_FAILURE
);
1192 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1194 /* Copying might take a while, hence install a watch on the child, and return */
1196 r
= operation_new(m
->manager
, m
, child
, message
, errno_pipe_fd
[0], NULL
);
1198 (void) sigkill_wait(child
);
1201 errno_pipe_fd
[0] = -1;
1206 int bus_machine_method_open_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1207 _cleanup_close_
int fd
= -1;
1208 Machine
*m
= userdata
;
1214 r
= bus_verify_polkit_async(
1217 "org.freedesktop.machine1.manage-machines",
1221 &m
->manager
->polkit_registry
,
1226 return 1; /* Will call us back */
1231 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1237 case MACHINE_CONTAINER
: {
1238 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
1239 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1242 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
1246 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1249 r
= safe_fork("(sd-openroot)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
, &child
);
1251 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1253 _cleanup_close_
int dfd
= -1;
1255 pair
[0] = safe_close(pair
[0]);
1257 r
= namespace_enter(-1, mntns_fd
, -1, -1, root_fd
);
1259 _exit(EXIT_FAILURE
);
1261 dfd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1263 _exit(EXIT_FAILURE
);
1265 r
= send_one_fd(pair
[1], dfd
, 0);
1266 dfd
= safe_close(dfd
);
1268 _exit(EXIT_FAILURE
);
1270 _exit(EXIT_SUCCESS
);
1273 pair
[1] = safe_close(pair
[1]);
1275 r
= wait_for_terminate_and_check("(sd-openroot)", child
, 0);
1277 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1278 if (r
!= EXIT_SUCCESS
)
1279 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1281 fd
= receive_one_fd(pair
[0], MSG_DONTWAIT
);
1289 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Opening the root directory is only supported on container machines.");
1292 return sd_bus_reply_method_return(message
, "h", fd
);
1295 int bus_machine_method_get_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1296 Machine
*m
= userdata
;
1303 /* You wonder why this is a method and not a property? Well, properties are not supposed to return errors, but
1304 * we kinda have to for this. */
1306 if (m
->class == MACHINE_HOST
)
1307 return sd_bus_reply_method_return(message
, "u", UINT32_C(0));
1309 if (m
->class != MACHINE_CONTAINER
)
1310 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "UID/GID shift may only be determined for container machines.");
1312 r
= machine_get_uid_shift(m
, &shift
);
1314 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Machine %s uses a complex UID/GID mapping, cannot determine shift", m
->name
);
1318 return sd_bus_reply_method_return(message
, "u", (uint32_t) shift
);
1321 const sd_bus_vtable machine_vtable
[] = {
1322 SD_BUS_VTABLE_START(0),
1323 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1324 SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128
, offsetof(Machine
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1325 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1326 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1327 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1328 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1329 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
), SD_BUS_VTABLE_PROPERTY_CONST
),
1330 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1331 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1332 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1333 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1334 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_machine_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
1335 SD_BUS_METHOD("Kill", "si", NULL
, bus_machine_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
1336 SD_BUS_METHOD("GetAddresses", NULL
, "a(iay)", bus_machine_method_get_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1337 SD_BUS_METHOD("GetOSRelease", NULL
, "a{ss}", bus_machine_method_get_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1338 SD_BUS_METHOD("GetUIDShift", NULL
, "u", bus_machine_method_get_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1339 SD_BUS_METHOD("OpenPTY", NULL
, "hs", bus_machine_method_open_pty
, SD_BUS_VTABLE_UNPRIVILEGED
),
1340 SD_BUS_METHOD("OpenLogin", NULL
, "hs", bus_machine_method_open_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1341 SD_BUS_METHOD("OpenShell", "ssasas", "hs", bus_machine_method_open_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1342 SD_BUS_METHOD("BindMount", "ssbb", NULL
, bus_machine_method_bind_mount
, SD_BUS_VTABLE_UNPRIVILEGED
),
1343 SD_BUS_METHOD("CopyFrom", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1344 SD_BUS_METHOD("CopyTo", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1345 SD_BUS_METHOD("OpenRootDirectory", NULL
, "h", bus_machine_method_open_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1349 int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1350 Manager
*m
= userdata
;
1360 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1361 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1362 sd_bus_message
*message
;
1365 message
= sd_bus_get_current_message(bus
);
1369 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1373 r
= sd_bus_creds_get_pid(creds
, &pid
);
1377 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1381 _cleanup_free_
char *e
= NULL
;
1384 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1388 e
= bus_label_unescape(p
);
1392 machine
= hashmap_get(m
->machines
, e
);
1401 char *machine_bus_path(Machine
*m
) {
1402 _cleanup_free_
char *e
= NULL
;
1406 e
= bus_label_escape(m
->name
);
1410 return strappend("/org/freedesktop/machine1/machine/", e
);
1413 int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1414 _cleanup_strv_free_
char **l
= NULL
;
1415 Machine
*machine
= NULL
;
1416 Manager
*m
= userdata
;
1424 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1427 p
= machine_bus_path(machine
);
1431 r
= strv_consume(&l
, p
);
1436 *nodes
= TAKE_PTR(l
);
1441 int machine_send_signal(Machine
*m
, bool new_machine
) {
1442 _cleanup_free_
char *p
= NULL
;
1446 p
= machine_bus_path(m
);
1450 return sd_bus_emit_signal(
1452 "/org/freedesktop/machine1",
1453 "org.freedesktop.machine1.Manager",
1454 new_machine
? "MachineNew" : "MachineRemoved",
1458 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1459 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1460 _cleanup_free_
char *p
= NULL
;
1464 if (!m
->create_message
)
1467 c
= TAKE_PTR(m
->create_message
);
1470 return sd_bus_reply_method_error(c
, error
);
1472 /* Update the machine state file before we notify the client
1473 * about the result. */
1476 p
= machine_bus_path(m
);
1480 return sd_bus_reply_method_return(c
, "o", p
);