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"
35 #include "path-util.h"
36 #include "process-util.h"
37 #include "signal-util.h"
39 #include "terminal-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
= safe_fork("(sd-addr)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
, &child
);
216 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
218 _cleanup_free_
struct local_address
*addresses
= NULL
;
219 struct local_address
*a
;
222 pair
[0] = safe_close(pair
[0]);
224 r
= namespace_enter(-1, -1, netns_fd
, -1, -1);
228 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
232 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
233 struct iovec iov
[2] = {
234 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
235 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
238 r
= writev(pair
[1], iov
, 2);
243 pair
[1] = safe_close(pair
[1]);
248 pair
[1] = safe_close(pair
[1]);
253 union in_addr_union in_addr
;
260 iov
[0] = (struct iovec
) { .iov_base
= &family
, .iov_len
= sizeof(family
) };
261 iov
[1] = (struct iovec
) { .iov_base
= &in_addr
, .iov_len
= sizeof(in_addr
) };
263 n
= recvmsg(pair
[0], &mh
, 0);
266 if ((size_t) n
< sizeof(family
))
269 r
= sd_bus_message_open_container(reply
, 'r', "iay");
273 r
= sd_bus_message_append(reply
, "i", family
);
280 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
283 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
287 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
290 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
296 r
= sd_bus_message_close_container(reply
);
301 r
= wait_for_terminate_and_check("(sd-addr)", child
, 0);
303 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
304 if (r
!= EXIT_SUCCESS
)
305 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
310 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
313 r
= sd_bus_message_close_container(reply
);
317 return sd_bus_send(NULL
, reply
, NULL
);
320 #define EXIT_NOT_FOUND 2
322 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
323 _cleanup_strv_free_
char **l
= NULL
;
324 Machine
*m
= userdata
;
333 r
= load_env_file_pairs(NULL
, "/etc/os-release", NULL
, &l
);
339 case MACHINE_CONTAINER
: {
340 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
341 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
342 _cleanup_fclose_
FILE *f
= NULL
;
345 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
349 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
352 r
= safe_fork("(sd-osrel)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
, &child
);
354 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
358 pair
[0] = safe_close(pair
[0]);
360 r
= namespace_enter(-1, mntns_fd
, -1, -1, root_fd
);
364 fd
= open("/etc/os-release", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
365 if (fd
< 0 && errno
== ENOENT
) {
366 fd
= open("/usr/lib/os-release", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
367 if (fd
< 0 && errno
== ENOENT
)
368 _exit(EXIT_NOT_FOUND
);
373 r
= copy_bytes(fd
, pair
[1], (uint64_t) -1, 0);
380 pair
[1] = safe_close(pair
[1]);
382 f
= fdopen(pair
[0], "re");
388 r
= load_env_file_pairs(f
, "/etc/os-release", NULL
, &l
);
392 r
= wait_for_terminate_and_check("(sd-osrel)", child
, 0);
394 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
395 if (r
== EXIT_NOT_FOUND
)
396 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Machine does not contain OS release information");
397 if (r
!= EXIT_SUCCESS
)
398 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
404 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
407 return bus_reply_pair_array(message
, l
);
410 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
411 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
412 _cleanup_free_
char *pty_name
= NULL
;
413 _cleanup_close_
int master
= -1;
414 Machine
*m
= userdata
;
420 r
= bus_verify_polkit_async(
423 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
427 &m
->manager
->polkit_registry
,
432 return 1; /* Will call us back */
434 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
438 r
= ptsname_namespace(master
, &pty_name
);
442 r
= sd_bus_message_new_method_return(message
, &reply
);
446 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
450 return sd_bus_send(NULL
, reply
, NULL
);
453 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
465 case MACHINE_CONTAINER
: {
466 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
469 r
= sd_bus_new(&bus
);
473 if (asprintf(&address
, "x-machine-kernel:pid=%1$" PID_PRI
";x-machine-unix:pid=%1$" PID_PRI
, m
->leader
) < 0)
476 bus
->address
= address
;
477 bus
->bus_client
= true;
478 bus
->trusted
= false;
479 bus
->is_system
= true;
481 r
= sd_bus_start(bus
);
483 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
487 *ret
= TAKE_PTR(bus
);
498 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
499 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
500 _cleanup_free_
char *pty_name
= NULL
;
501 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
502 _cleanup_close_
int master
= -1;
503 sd_bus
*container_bus
= NULL
;
504 Machine
*m
= userdata
;
505 const char *p
, *getty
;
511 r
= bus_verify_polkit_async(
514 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login",
518 &m
->manager
->polkit_registry
,
523 return 1; /* Will call us back */
525 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
529 r
= ptsname_namespace(master
, &pty_name
);
533 p
= path_startswith(pty_name
, "/dev/pts/");
535 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "PTS name %s is invalid", pty_name
);
537 r
= container_bus_new(m
, error
, &allocated_bus
);
541 container_bus
= allocated_bus
?: m
->manager
->bus
;
543 getty
= strjoina("container-getty@", p
, ".service");
545 r
= sd_bus_call_method(
547 "org.freedesktop.systemd1",
548 "/org/freedesktop/systemd1",
549 "org.freedesktop.systemd1.Manager",
552 "ss", getty
, "replace");
556 r
= sd_bus_message_new_method_return(message
, &reply
);
560 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
564 return sd_bus_send(NULL
, reply
, NULL
);
567 int bus_machine_method_open_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
568 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *tm
= NULL
;
569 _cleanup_free_
char *pty_name
= NULL
;
570 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
571 sd_bus
*container_bus
= NULL
;
572 _cleanup_close_
int master
= -1, slave
= -1;
573 _cleanup_strv_free_
char **env
= NULL
, **args_wire
= NULL
, **args
= NULL
;
574 Machine
*m
= userdata
;
575 const char *p
, *unit
, *user
, *path
, *description
, *utmp_id
;
581 r
= sd_bus_message_read(message
, "ss", &user
, &path
);
584 user
= empty_to_null(user
);
585 r
= sd_bus_message_read_strv(message
, &args_wire
);
591 args
= new0(char*, 3 + 1);
594 args
[0] = strdup("sh");
597 args
[1] = strdup("-c");
600 r
= asprintf(&args
[2],
601 "shell=$(getent passwd %s 2>/dev/null | { IFS=: read _ _ _ _ _ _ x; echo \"$x\"; })\n"\
602 "exec \"${shell:-/bin/sh}\" -l", /* -l is means --login */
603 isempty(user
) ? "root" : user
);
609 if (!path_is_absolute(path
))
610 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified path '%s' is not absolute", path
);
611 args
= TAKE_PTR(args_wire
);
612 if (strv_isempty(args
)) {
613 args
= strv_free(args
);
615 args
= strv_new(path
, NULL
);
621 r
= sd_bus_message_read_strv(message
, &env
);
624 if (!strv_env_is_valid(env
))
625 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
627 r
= bus_verify_polkit_async(
630 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
634 &m
->manager
->polkit_registry
,
639 return 1; /* Will call us back */
641 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
645 r
= ptsname_namespace(master
, &pty_name
);
649 p
= path_startswith(pty_name
, "/dev/pts/");
652 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
656 utmp_id
= path_startswith(pty_name
, "/dev/");
659 r
= container_bus_new(m
, error
, &allocated_bus
);
663 container_bus
= allocated_bus
?: m
->manager
->bus
;
665 r
= sd_bus_message_new_method_call(
668 "org.freedesktop.systemd1",
669 "/org/freedesktop/systemd1",
670 "org.freedesktop.systemd1.Manager",
671 "StartTransientUnit");
676 unit
= strjoina("container-shell@", p
, ".service");
677 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
682 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
686 description
= strjoina("Shell for User ", isempty(user
) ? "root" : user
);
687 r
= sd_bus_message_append(tm
,
688 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
689 "Description", "s", description
,
690 "StandardInputFileDescriptor", "h", slave
,
691 "StandardOutputFileDescriptor", "h", slave
,
692 "StandardErrorFileDescriptor", "h", slave
,
693 "SendSIGHUP", "b", true,
694 "IgnoreSIGPIPE", "b", false,
695 "KillMode", "s", "mixed",
696 "TTYReset", "b", true,
697 "UtmpIdentifier", "s", utmp_id
,
698 "UtmpMode", "s", "user",
699 "PAMName", "s", "login",
700 "WorkingDirectory", "s", "-~");
704 r
= sd_bus_message_append(tm
, "(sv)", "User", "s", isempty(user
) ? "root" : user
);
708 if (!strv_isempty(env
)) {
709 r
= sd_bus_message_open_container(tm
, 'r', "sv");
713 r
= sd_bus_message_append(tm
, "s", "Environment");
717 r
= sd_bus_message_open_container(tm
, 'v', "as");
721 r
= sd_bus_message_append_strv(tm
, env
);
725 r
= sd_bus_message_close_container(tm
);
729 r
= sd_bus_message_close_container(tm
);
735 r
= sd_bus_message_open_container(tm
, 'r', "sv");
739 r
= sd_bus_message_append(tm
, "s", "ExecStart");
743 r
= sd_bus_message_open_container(tm
, 'v', "a(sasb)");
747 r
= sd_bus_message_open_container(tm
, 'a', "(sasb)");
751 r
= sd_bus_message_open_container(tm
, 'r', "sasb");
755 r
= sd_bus_message_append(tm
, "s", path
);
759 r
= sd_bus_message_append_strv(tm
, args
);
763 r
= sd_bus_message_append(tm
, "b", true);
767 r
= sd_bus_message_close_container(tm
);
771 r
= sd_bus_message_close_container(tm
);
775 r
= sd_bus_message_close_container(tm
);
779 r
= sd_bus_message_close_container(tm
);
783 r
= sd_bus_message_close_container(tm
);
787 /* Auxiliary units */
788 r
= sd_bus_message_append(tm
, "a(sa(sv))", 0);
792 r
= sd_bus_call(container_bus
, tm
, 0, error
, NULL
);
796 slave
= safe_close(slave
);
798 r
= sd_bus_message_new_method_return(message
, &reply
);
802 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
806 return sd_bus_send(NULL
, reply
, NULL
);
809 int bus_machine_method_bind_mount(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
810 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
811 char mount_slave
[] = "/tmp/propagate.XXXXXX", *mount_tmp
, *mount_outside
, *p
;
812 bool mount_slave_created
= false, mount_slave_mounted
= false,
813 mount_tmp_created
= false, mount_tmp_mounted
= false,
814 mount_outside_created
= false, mount_outside_mounted
= false;
815 _cleanup_free_
char *chased_src
= NULL
;
816 int read_only
, make_file_or_directory
;
817 const char *dest
, *src
;
818 Machine
*m
= userdata
;
827 if (m
->class != MACHINE_CONTAINER
)
828 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
830 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_file_or_directory
);
834 if (!path_is_absolute(src
) || !path_is_normalized(src
))
835 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and not contain ../.");
839 else if (!path_is_absolute(dest
) || !path_is_normalized(dest
))
840 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and not contain ../.");
842 r
= bus_verify_polkit_async(
845 "org.freedesktop.machine1.manage-machines",
849 &m
->manager
->polkit_registry
,
854 return 1; /* Will call us back */
856 r
= machine_get_uid_shift(m
, &uid
);
860 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Can't bind mount on container with user namespacing applied.");
862 /* One day, when bind mounting /proc/self/fd/n works across
863 * namespace boundaries we should rework this logic to make
866 p
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/");
867 if (laccess(p
, F_OK
) < 0)
868 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Container does not allow propagation of mount points.");
870 r
= chase_symlinks(src
, NULL
, CHASE_TRAIL_SLASH
, &chased_src
);
872 return sd_bus_error_set_errnof(error
, r
, "Failed to resolve source path: %m");
874 if (lstat(chased_src
, &st
) < 0)
875 return sd_bus_error_set_errnof(error
, errno
, "Failed to stat() source path: %m");
876 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… */
877 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Source directory can't be a symbolic link");
879 /* Our goal is to install a new bind mount into the container,
880 possibly read-only. This is irritatingly complex
881 unfortunately, currently.
883 First, we start by creating a private playground in /tmp,
884 that we can mount MS_SLAVE. (Which is necessary, since
885 MS_MOVE cannot be applied to mounts with MS_SHARED parent
888 if (!mkdtemp(mount_slave
))
889 return sd_bus_error_set_errnof(error
, errno
, "Failed to create playground %s: %m", mount_slave
);
891 mount_slave_created
= true;
893 if (mount(mount_slave
, mount_slave
, NULL
, MS_BIND
, NULL
) < 0) {
894 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to make bind mount %s: %m", mount_slave
);
898 mount_slave_mounted
= true;
900 if (mount(NULL
, mount_slave
, NULL
, MS_SLAVE
, NULL
) < 0) {
901 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount slave %s: %m", mount_slave
);
905 /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
906 mount_tmp
= strjoina(mount_slave
, "/mount");
907 if (S_ISDIR(st
.st_mode
))
908 r
= mkdir_errno_wrapper(mount_tmp
, 0700);
910 r
= touch(mount_tmp
);
912 sd_bus_error_set_errnof(error
, errno
, "Failed to create temporary mount point %s: %m", mount_tmp
);
916 mount_tmp_created
= true;
918 if (mount(chased_src
, mount_tmp
, NULL
, MS_BIND
, NULL
) < 0) {
919 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to mount %s: %m", chased_src
);
923 mount_tmp_mounted
= true;
925 /* Third, we remount the new bind mount read-only if requested. */
927 if (mount(NULL
, mount_tmp
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
) < 0) {
928 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount read-only %s: %m", mount_tmp
);
932 /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
935 mount_outside
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/XXXXXX");
936 if (S_ISDIR(st
.st_mode
))
937 r
= mkdtemp(mount_outside
) ? 0 : -errno
;
939 r
= mkostemp_safe(mount_outside
);
943 sd_bus_error_set_errnof(error
, errno
, "Cannot create propagation file or directory %s: %m", mount_outside
);
947 mount_outside_created
= true;
949 if (mount(mount_tmp
, mount_outside
, NULL
, MS_MOVE
, NULL
) < 0) {
950 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to move %s to %s: %m", mount_tmp
, mount_outside
);
954 mount_outside_mounted
= true;
955 mount_tmp_mounted
= false;
957 if (S_ISDIR(st
.st_mode
))
958 (void) rmdir(mount_tmp
);
960 (void) unlink(mount_tmp
);
961 mount_tmp_created
= false;
963 (void) umount(mount_slave
);
964 mount_slave_mounted
= false;
966 (void) rmdir(mount_slave
);
967 mount_slave_created
= false;
969 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0) {
970 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
974 r
= safe_fork("(sd-bindmnt)", FORK_RESET_SIGNALS
, &child
);
976 sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
980 const char *mount_inside
;
984 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
986 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
987 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
989 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
993 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
994 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
998 if (make_file_or_directory
) {
999 if (S_ISDIR(st
.st_mode
))
1000 (void) mkdir_p(dest
, 0755);
1002 (void) mkdir_parents(dest
, 0755);
1003 safe_close(open(dest
, O_CREAT
|O_EXCL
|O_WRONLY
|O_CLOEXEC
|O_NOCTTY
, 0600));
1007 /* Fifth, move the mount to the right place inside */
1008 mount_inside
= strjoina("/run/systemd/nspawn/incoming/", basename(mount_outside
));
1009 if (mount(mount_inside
, dest
, NULL
, MS_MOVE
, NULL
) < 0) {
1010 r
= log_error_errno(errno
, "Failed to mount: %m");
1014 _exit(EXIT_SUCCESS
);
1017 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1018 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1020 _exit(EXIT_FAILURE
);
1023 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1025 r
= wait_for_terminate_and_check("(sd-bindmnt)", child
, 0);
1027 r
= sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1030 if (r
!= EXIT_SUCCESS
) {
1031 if (read(errno_pipe_fd
[0], &r
, sizeof(r
)) == sizeof(r
))
1032 r
= sd_bus_error_set_errnof(error
, r
, "Failed to mount: %m");
1034 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child failed.");
1038 r
= sd_bus_reply_method_return(message
, NULL
);
1041 if (mount_outside_mounted
)
1042 (void) umount(mount_outside
);
1043 if (mount_outside_created
) {
1044 if (S_ISDIR(st
.st_mode
))
1045 (void) rmdir(mount_outside
);
1047 (void) unlink(mount_outside
);
1050 if (mount_tmp_mounted
)
1051 (void) umount(mount_tmp
);
1052 if (mount_tmp_created
) {
1053 if (S_ISDIR(st
.st_mode
))
1054 (void) rmdir(mount_tmp
);
1056 (void) unlink(mount_tmp
);
1059 if (mount_slave_mounted
)
1060 (void) umount(mount_slave
);
1061 if (mount_slave_created
)
1062 (void) rmdir(mount_slave
);
1067 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1068 const char *src
, *dest
, *host_path
, *container_path
, *host_basename
, *host_dirname
, *container_basename
, *container_dirname
;
1069 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
1070 CopyFlags copy_flags
= COPY_REFLINK
|COPY_MERGE
;
1071 _cleanup_close_
int hostfd
= -1;
1072 Machine
*m
= userdata
;
1082 if (m
->manager
->n_operations
>= OPERATIONS_MAX
)
1083 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
1085 if (m
->class != MACHINE_CONTAINER
)
1086 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
1088 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
1092 if (!path_is_absolute(src
))
1093 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
1097 else if (!path_is_absolute(dest
))
1098 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
1100 r
= bus_verify_polkit_async(
1103 "org.freedesktop.machine1.manage-machines",
1107 &m
->manager
->polkit_registry
,
1112 return 1; /* Will call us back */
1114 r
= machine_get_uid_shift(m
, &uid_shift
);
1118 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
1121 container_path
= src
;
1125 container_path
= dest
;
1128 host_basename
= basename(host_path
);
1129 t
= strdupa(host_path
);
1130 host_dirname
= dirname(t
);
1132 container_basename
= basename(container_path
);
1133 t
= strdupa(container_path
);
1134 container_dirname
= dirname(t
);
1136 hostfd
= open(host_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1138 return sd_bus_error_set_errnof(error
, errno
, "Failed to open host directory %s: %m", host_dirname
);
1140 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1141 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1143 r
= safe_fork("(sd-copy)", FORK_RESET_SIGNALS
, &child
);
1145 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1151 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1153 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
1154 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
1156 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
1160 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
1161 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
1165 containerfd
= open(container_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1166 if (containerfd
< 0) {
1167 r
= log_error_errno(errno
, "Failed to open destination directory: %m");
1171 /* Run the actual copy operation. Note that when an UID shift is set we'll either clamp the UID/GID to
1172 * 0 or to the actual UID shift depending on the direction we copy. If no UID shift is set we'll copy
1173 * the UID/GIDs as they are. */
1175 r
= copy_tree_at(containerfd
, container_basename
, hostfd
, host_basename
, uid_shift
== 0 ? UID_INVALID
: 0, uid_shift
== 0 ? GID_INVALID
: 0, copy_flags
);
1177 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
);
1179 hostfd
= safe_close(hostfd
);
1180 containerfd
= safe_close(containerfd
);
1183 r
= log_error_errno(r
, "Failed to copy tree: %m");
1187 _exit(EXIT_SUCCESS
);
1190 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1191 _exit(EXIT_FAILURE
);
1194 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1196 /* Copying might take a while, hence install a watch on the child, and return */
1198 r
= operation_new(m
->manager
, m
, child
, message
, errno_pipe_fd
[0], NULL
);
1200 (void) sigkill_wait(child
);
1203 errno_pipe_fd
[0] = -1;
1208 int bus_machine_method_open_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1209 _cleanup_close_
int fd
= -1;
1210 Machine
*m
= userdata
;
1216 r
= bus_verify_polkit_async(
1219 "org.freedesktop.machine1.manage-machines",
1223 &m
->manager
->polkit_registry
,
1228 return 1; /* Will call us back */
1233 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1239 case MACHINE_CONTAINER
: {
1240 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
1241 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1244 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
1248 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1251 r
= safe_fork("(sd-openroot)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
, &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 r
= namespace_enter(-1, mntns_fd
, -1, -1, root_fd
);
1261 _exit(EXIT_FAILURE
);
1263 dfd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1265 _exit(EXIT_FAILURE
);
1267 r
= send_one_fd(pair
[1], dfd
, 0);
1268 dfd
= safe_close(dfd
);
1270 _exit(EXIT_FAILURE
);
1272 _exit(EXIT_SUCCESS
);
1275 pair
[1] = safe_close(pair
[1]);
1277 r
= wait_for_terminate_and_check("(sd-openroot)", child
, 0);
1279 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1280 if (r
!= EXIT_SUCCESS
)
1281 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1283 fd
= receive_one_fd(pair
[0], MSG_DONTWAIT
);
1291 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Opening the root directory is only supported on container machines.");
1294 return sd_bus_reply_method_return(message
, "h", fd
);
1297 int bus_machine_method_get_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1298 Machine
*m
= userdata
;
1305 /* You wonder why this is a method and not a property? Well, properties are not supposed to return errors, but
1306 * we kinda have to for this. */
1308 if (m
->class == MACHINE_HOST
)
1309 return sd_bus_reply_method_return(message
, "u", UINT32_C(0));
1311 if (m
->class != MACHINE_CONTAINER
)
1312 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "UID/GID shift may only be determined for container machines.");
1314 r
= machine_get_uid_shift(m
, &shift
);
1316 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Machine %s uses a complex UID/GID mapping, cannot determine shift", m
->name
);
1320 return sd_bus_reply_method_return(message
, "u", (uint32_t) shift
);
1323 const sd_bus_vtable machine_vtable
[] = {
1324 SD_BUS_VTABLE_START(0),
1325 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1326 SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128
, offsetof(Machine
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1327 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1328 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1329 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1330 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1331 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
), SD_BUS_VTABLE_PROPERTY_CONST
),
1332 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1333 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1334 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1335 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1336 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_machine_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
1337 SD_BUS_METHOD("Kill", "si", NULL
, bus_machine_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
1338 SD_BUS_METHOD("GetAddresses", NULL
, "a(iay)", bus_machine_method_get_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1339 SD_BUS_METHOD("GetOSRelease", NULL
, "a{ss}", bus_machine_method_get_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1340 SD_BUS_METHOD("GetUIDShift", NULL
, "u", bus_machine_method_get_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1341 SD_BUS_METHOD("OpenPTY", NULL
, "hs", bus_machine_method_open_pty
, SD_BUS_VTABLE_UNPRIVILEGED
),
1342 SD_BUS_METHOD("OpenLogin", NULL
, "hs", bus_machine_method_open_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1343 SD_BUS_METHOD("OpenShell", "ssasas", "hs", bus_machine_method_open_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1344 SD_BUS_METHOD("BindMount", "ssbb", NULL
, bus_machine_method_bind_mount
, SD_BUS_VTABLE_UNPRIVILEGED
),
1345 SD_BUS_METHOD("CopyFrom", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1346 SD_BUS_METHOD("CopyTo", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1347 SD_BUS_METHOD("OpenRootDirectory", NULL
, "h", bus_machine_method_open_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1351 int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1352 Manager
*m
= userdata
;
1362 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1363 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1364 sd_bus_message
*message
;
1367 message
= sd_bus_get_current_message(bus
);
1371 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1375 r
= sd_bus_creds_get_pid(creds
, &pid
);
1379 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1383 _cleanup_free_
char *e
= NULL
;
1386 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1390 e
= bus_label_unescape(p
);
1394 machine
= hashmap_get(m
->machines
, e
);
1403 char *machine_bus_path(Machine
*m
) {
1404 _cleanup_free_
char *e
= NULL
;
1408 e
= bus_label_escape(m
->name
);
1412 return strappend("/org/freedesktop/machine1/machine/", e
);
1415 int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1416 _cleanup_strv_free_
char **l
= NULL
;
1417 Machine
*machine
= NULL
;
1418 Manager
*m
= userdata
;
1426 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1429 p
= machine_bus_path(machine
);
1433 r
= strv_consume(&l
, p
);
1438 *nodes
= TAKE_PTR(l
);
1443 int machine_send_signal(Machine
*m
, bool new_machine
) {
1444 _cleanup_free_
char *p
= NULL
;
1448 p
= machine_bus_path(m
);
1452 return sd_bus_emit_signal(
1454 "/org/freedesktop/machine1",
1455 "org.freedesktop.machine1.Manager",
1456 new_machine
? "MachineNew" : "MachineRemoved",
1460 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1461 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1462 _cleanup_free_
char *p
= NULL
;
1466 if (!m
->create_message
)
1469 c
= TAKE_PTR(m
->create_message
);
1472 return sd_bus_reply_method_error(c
, error
);
1474 /* Update the machine state file before we notify the client
1475 * about the result. */
1478 p
= machine_bus_path(m
);
1482 return sd_bus_reply_method_return(c
, "o", p
);