1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 /* When we include libgen.h because we need dirname() we immediately
9 * undefine basename() since libgen.h defines it as a macro to the POSIX
10 * version which is really broken. We prefer GNU basename(). */
14 #include "alloc-util.h"
15 #include "bus-common-errors.h"
16 #include "bus-internal.h"
17 #include "bus-label.h"
23 #include "format-util.h"
25 #include "in-addr-util.h"
27 #include "local-addresses.h"
28 #include "machine-dbus.h"
32 #include "path-util.h"
33 #include "process-util.h"
34 #include "signal-util.h"
36 #include "terminal-util.h"
37 #include "tmpfile-util.h"
38 #include "user-util.h"
40 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class
, machine_class
, MachineClass
);
41 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", Machine
, machine_get_state
, machine_state_to_string
);
43 static int property_get_netif(
46 const char *interface
,
48 sd_bus_message
*reply
,
50 sd_bus_error
*error
) {
52 Machine
*m
= userdata
;
58 assert_cc(sizeof(int) == sizeof(int32_t));
60 return sd_bus_message_append_array(reply
, 'i', m
->netif
, m
->n_netif
* sizeof(int));
63 int bus_machine_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
64 Machine
*m
= userdata
;
70 r
= bus_verify_polkit_async(
73 "org.freedesktop.machine1.manage-machines",
77 &m
->manager
->polkit_registry
,
82 return 1; /* Will call us back */
88 return sd_bus_reply_method_return(message
, NULL
);
91 int bus_machine_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
92 Machine
*m
= userdata
;
101 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
108 who
= kill_who_from_string(swho
);
110 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
113 if (!SIGNAL_VALID(signo
))
114 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
116 r
= bus_verify_polkit_async(
119 "org.freedesktop.machine1.manage-machines",
123 &m
->manager
->polkit_registry
,
128 return 1; /* Will call us back */
130 r
= machine_kill(m
, who
, signo
);
134 return sd_bus_reply_method_return(message
, NULL
);
137 int bus_machine_method_get_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
138 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
139 Machine
*m
= userdata
;
145 r
= sd_bus_message_new_method_return(message
, &reply
);
149 r
= sd_bus_message_open_container(reply
, 'a', "(iay)");
156 _cleanup_free_
struct local_address
*addresses
= NULL
;
157 struct local_address
*a
;
160 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
164 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
166 r
= sd_bus_message_open_container(reply
, 'r', "iay");
170 r
= sd_bus_message_append(reply
, "i", addresses
[i
].family
);
174 r
= sd_bus_message_append_array(reply
, 'y', &addresses
[i
].address
, FAMILY_ADDRESS_SIZE(addresses
[i
].family
));
178 r
= sd_bus_message_close_container(reply
);
186 case MACHINE_CONTAINER
: {
187 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
188 _cleanup_free_
char *us
= NULL
, *them
= NULL
;
189 _cleanup_close_
int netns_fd
= -1;
193 r
= readlink_malloc("/proc/self/ns/net", &us
);
197 p
= procfs_file_alloca(m
->leader
, "ns/net");
198 r
= readlink_malloc(p
, &them
);
203 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_NETWORKING
, "Machine %s does not use private networking", m
->name
);
205 r
= namespace_open(m
->leader
, NULL
, NULL
, &netns_fd
, NULL
, NULL
);
209 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
212 r
= namespace_fork("(sd-addrns)", "(sd-addr)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
213 -1, -1, netns_fd
, -1, -1, &child
);
215 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
217 _cleanup_free_
struct local_address
*addresses
= NULL
;
218 struct local_address
*a
;
221 pair
[0] = safe_close(pair
[0]);
223 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
227 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
228 struct iovec iov
[2] = {
229 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
230 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
233 r
= writev(pair
[1], iov
, 2);
238 pair
[1] = safe_close(pair
[1]);
243 pair
[1] = safe_close(pair
[1]);
248 union in_addr_union in_addr
;
255 iov
[0] = IOVEC_MAKE(&family
, sizeof(family
));
256 iov
[1] = IOVEC_MAKE(&in_addr
, sizeof(in_addr
));
258 n
= recvmsg(pair
[0], &mh
, 0);
261 if ((size_t) n
< sizeof(family
))
264 r
= sd_bus_message_open_container(reply
, 'r', "iay");
268 r
= sd_bus_message_append(reply
, "i", family
);
275 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
278 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
282 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
285 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
291 r
= sd_bus_message_close_container(reply
);
296 r
= wait_for_terminate_and_check("(sd-addrns)", child
, 0);
298 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
299 if (r
!= EXIT_SUCCESS
)
300 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
305 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
308 r
= sd_bus_message_close_container(reply
);
312 return sd_bus_send(NULL
, reply
, NULL
);
315 #define EXIT_NOT_FOUND 2
317 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
318 _cleanup_strv_free_
char **l
= NULL
;
319 Machine
*m
= userdata
;
328 r
= load_os_release_pairs(NULL
, &l
);
334 case MACHINE_CONTAINER
: {
335 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1, pidns_fd
= -1;
336 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
337 _cleanup_fclose_
FILE *f
= NULL
;
340 r
= namespace_open(m
->leader
, &pidns_fd
, &mntns_fd
, NULL
, NULL
, &root_fd
);
344 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
347 r
= namespace_fork("(sd-osrelns)", "(sd-osrel)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
348 pidns_fd
, mntns_fd
, -1, -1, root_fd
,
351 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
355 pair
[0] = safe_close(pair
[0]);
357 r
= open_os_release(NULL
, NULL
, &fd
);
359 _exit(EXIT_NOT_FOUND
);
363 r
= copy_bytes(fd
, pair
[1], (uint64_t) -1, 0);
370 pair
[1] = safe_close(pair
[1]);
372 f
= fdopen(pair
[0], "re");
378 r
= load_env_file_pairs(f
, "/etc/os-release", &l
);
382 r
= wait_for_terminate_and_check("(sd-osrelns)", child
, 0);
384 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
385 if (r
== EXIT_NOT_FOUND
)
386 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Machine does not contain OS release information");
387 if (r
!= EXIT_SUCCESS
)
388 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
394 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
397 return bus_reply_pair_array(message
, l
);
400 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
401 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
402 _cleanup_free_
char *pty_name
= NULL
;
403 _cleanup_close_
int master
= -1;
404 Machine
*m
= userdata
;
410 r
= bus_verify_polkit_async(
413 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
417 &m
->manager
->polkit_registry
,
422 return 1; /* Will call us back */
424 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
428 r
= ptsname_namespace(master
, &pty_name
);
432 r
= sd_bus_message_new_method_return(message
, &reply
);
436 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
440 return sd_bus_send(NULL
, reply
, NULL
);
443 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
455 case MACHINE_CONTAINER
: {
456 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
459 r
= sd_bus_new(&bus
);
463 if (asprintf(&address
, "x-machine-kernel:pid=%1$" PID_PRI
";x-machine-unix:pid=%1$" PID_PRI
, m
->leader
) < 0)
466 bus
->address
= address
;
467 bus
->bus_client
= true;
468 bus
->trusted
= false;
469 bus
->is_system
= true;
471 r
= sd_bus_start(bus
);
473 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
477 *ret
= TAKE_PTR(bus
);
488 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
489 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
490 _cleanup_free_
char *pty_name
= NULL
;
491 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
492 _cleanup_close_
int master
= -1;
493 sd_bus
*container_bus
= NULL
;
494 Machine
*m
= userdata
;
495 const char *p
, *getty
;
501 r
= bus_verify_polkit_async(
504 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login",
508 &m
->manager
->polkit_registry
,
513 return 1; /* Will call us back */
515 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
519 r
= ptsname_namespace(master
, &pty_name
);
523 p
= path_startswith(pty_name
, "/dev/pts/");
525 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "PTS name %s is invalid", pty_name
);
527 r
= container_bus_new(m
, error
, &allocated_bus
);
531 container_bus
= allocated_bus
?: m
->manager
->bus
;
533 getty
= strjoina("container-getty@", p
, ".service");
535 r
= sd_bus_call_method(
537 "org.freedesktop.systemd1",
538 "/org/freedesktop/systemd1",
539 "org.freedesktop.systemd1.Manager",
542 "ss", getty
, "replace");
546 r
= sd_bus_message_new_method_return(message
, &reply
);
550 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
554 return sd_bus_send(NULL
, reply
, NULL
);
557 int bus_machine_method_open_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
558 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *tm
= NULL
;
559 _cleanup_free_
char *pty_name
= NULL
;
560 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
561 sd_bus
*container_bus
= NULL
;
562 _cleanup_close_
int master
= -1, slave
= -1;
563 _cleanup_strv_free_
char **env
= NULL
, **args_wire
= NULL
, **args
= NULL
;
564 Machine
*m
= userdata
;
565 const char *p
, *unit
, *user
, *path
, *description
, *utmp_id
;
571 r
= sd_bus_message_read(message
, "ss", &user
, &path
);
574 user
= empty_to_null(user
);
575 r
= sd_bus_message_read_strv(message
, &args_wire
);
581 args
= new0(char*, 3 + 1);
584 args
[0] = strdup("sh");
587 args
[1] = strdup("-c");
590 r
= asprintf(&args
[2],
591 "shell=$(getent passwd %s 2>/dev/null | { IFS=: read _ _ _ _ _ _ x; echo \"$x\"; })\n"\
592 "exec \"${shell:-/bin/sh}\" -l", /* -l is means --login */
593 isempty(user
) ? "root" : user
);
599 if (!path_is_absolute(path
))
600 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified path '%s' is not absolute", path
);
601 args
= TAKE_PTR(args_wire
);
602 if (strv_isempty(args
)) {
603 args
= strv_free(args
);
605 args
= strv_new(path
);
611 r
= sd_bus_message_read_strv(message
, &env
);
614 if (!strv_env_is_valid(env
))
615 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
617 r
= bus_verify_polkit_async(
620 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
624 &m
->manager
->polkit_registry
,
629 return 1; /* Will call us back */
631 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
635 r
= ptsname_namespace(master
, &pty_name
);
639 p
= path_startswith(pty_name
, "/dev/pts/");
642 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
646 utmp_id
= path_startswith(pty_name
, "/dev/");
649 r
= container_bus_new(m
, error
, &allocated_bus
);
653 container_bus
= allocated_bus
?: m
->manager
->bus
;
655 r
= sd_bus_message_new_method_call(
658 "org.freedesktop.systemd1",
659 "/org/freedesktop/systemd1",
660 "org.freedesktop.systemd1.Manager",
661 "StartTransientUnit");
666 unit
= strjoina("container-shell@", p
, ".service");
667 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
672 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
676 description
= strjoina("Shell for User ", isempty(user
) ? "root" : user
);
677 r
= sd_bus_message_append(tm
,
678 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
679 "Description", "s", description
,
680 "StandardInputFileDescriptor", "h", slave
,
681 "StandardOutputFileDescriptor", "h", slave
,
682 "StandardErrorFileDescriptor", "h", slave
,
683 "SendSIGHUP", "b", true,
684 "IgnoreSIGPIPE", "b", false,
685 "KillMode", "s", "mixed",
686 "TTYReset", "b", true,
687 "UtmpIdentifier", "s", utmp_id
,
688 "UtmpMode", "s", "user",
689 "PAMName", "s", "login",
690 "WorkingDirectory", "s", "-~");
694 r
= sd_bus_message_append(tm
, "(sv)", "User", "s", isempty(user
) ? "root" : user
);
698 if (!strv_isempty(env
)) {
699 r
= sd_bus_message_open_container(tm
, 'r', "sv");
703 r
= sd_bus_message_append(tm
, "s", "Environment");
707 r
= sd_bus_message_open_container(tm
, 'v', "as");
711 r
= sd_bus_message_append_strv(tm
, env
);
715 r
= sd_bus_message_close_container(tm
);
719 r
= sd_bus_message_close_container(tm
);
725 r
= sd_bus_message_open_container(tm
, 'r', "sv");
729 r
= sd_bus_message_append(tm
, "s", "ExecStart");
733 r
= sd_bus_message_open_container(tm
, 'v', "a(sasb)");
737 r
= sd_bus_message_open_container(tm
, 'a', "(sasb)");
741 r
= sd_bus_message_open_container(tm
, 'r', "sasb");
745 r
= sd_bus_message_append(tm
, "s", path
);
749 r
= sd_bus_message_append_strv(tm
, args
);
753 r
= sd_bus_message_append(tm
, "b", true);
757 r
= sd_bus_message_close_container(tm
);
761 r
= sd_bus_message_close_container(tm
);
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 /* Auxiliary units */
778 r
= sd_bus_message_append(tm
, "a(sa(sv))", 0);
782 r
= sd_bus_call(container_bus
, tm
, 0, error
, NULL
);
786 slave
= safe_close(slave
);
788 r
= sd_bus_message_new_method_return(message
, &reply
);
792 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
796 return sd_bus_send(NULL
, reply
, NULL
);
799 int bus_machine_method_bind_mount(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
800 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
801 char mount_slave
[] = "/tmp/propagate.XXXXXX", *mount_tmp
, *mount_outside
, *p
;
802 bool mount_slave_created
= false, mount_slave_mounted
= false,
803 mount_tmp_created
= false, mount_tmp_mounted
= false,
804 mount_outside_created
= false, mount_outside_mounted
= false;
805 _cleanup_free_
char *chased_src
= NULL
;
806 int read_only
, make_file_or_directory
;
807 const char *dest
, *src
;
808 Machine
*m
= userdata
;
817 if (m
->class != MACHINE_CONTAINER
)
818 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
820 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_file_or_directory
);
824 if (!path_is_absolute(src
) || !path_is_normalized(src
))
825 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and not contain ../.");
829 else if (!path_is_absolute(dest
) || !path_is_normalized(dest
))
830 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and not contain ../.");
832 r
= bus_verify_polkit_async(
835 "org.freedesktop.machine1.manage-machines",
839 &m
->manager
->polkit_registry
,
844 return 1; /* Will call us back */
846 r
= machine_get_uid_shift(m
, &uid
);
850 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Can't bind mount on container with user namespacing applied.");
852 /* One day, when bind mounting /proc/self/fd/n works across
853 * namespace boundaries we should rework this logic to make
856 p
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/");
857 if (laccess(p
, F_OK
) < 0)
858 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Container does not allow propagation of mount points.");
860 r
= chase_symlinks(src
, NULL
, CHASE_TRAIL_SLASH
, &chased_src
);
862 return sd_bus_error_set_errnof(error
, r
, "Failed to resolve source path: %m");
864 if (lstat(chased_src
, &st
) < 0)
865 return sd_bus_error_set_errnof(error
, errno
, "Failed to stat() source path: %m");
866 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… */
867 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Source directory can't be a symbolic link");
869 /* Our goal is to install a new bind mount into the container,
870 possibly read-only. This is irritatingly complex
871 unfortunately, currently.
873 First, we start by creating a private playground in /tmp,
874 that we can mount MS_SLAVE. (Which is necessary, since
875 MS_MOVE cannot be applied to mounts with MS_SHARED parent
878 if (!mkdtemp(mount_slave
))
879 return sd_bus_error_set_errnof(error
, errno
, "Failed to create playground %s: %m", mount_slave
);
881 mount_slave_created
= true;
883 if (mount(mount_slave
, mount_slave
, NULL
, MS_BIND
, NULL
) < 0) {
884 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to make bind mount %s: %m", mount_slave
);
888 mount_slave_mounted
= true;
890 if (mount(NULL
, mount_slave
, NULL
, MS_SLAVE
, NULL
) < 0) {
891 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount slave %s: %m", mount_slave
);
895 /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
896 mount_tmp
= strjoina(mount_slave
, "/mount");
897 if (S_ISDIR(st
.st_mode
))
898 r
= mkdir_errno_wrapper(mount_tmp
, 0700);
900 r
= touch(mount_tmp
);
902 sd_bus_error_set_errnof(error
, errno
, "Failed to create temporary mount point %s: %m", mount_tmp
);
906 mount_tmp_created
= true;
908 if (mount(chased_src
, mount_tmp
, NULL
, MS_BIND
, NULL
) < 0) {
909 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to mount %s: %m", chased_src
);
913 mount_tmp_mounted
= true;
915 /* Third, we remount the new bind mount read-only if requested. */
917 if (mount(NULL
, mount_tmp
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
) < 0) {
918 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount read-only %s: %m", mount_tmp
);
922 /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
925 mount_outside
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/XXXXXX");
926 if (S_ISDIR(st
.st_mode
))
927 r
= mkdtemp(mount_outside
) ? 0 : -errno
;
929 r
= mkostemp_safe(mount_outside
);
933 sd_bus_error_set_errnof(error
, errno
, "Cannot create propagation file or directory %s: %m", mount_outside
);
937 mount_outside_created
= true;
939 if (mount(mount_tmp
, mount_outside
, NULL
, MS_MOVE
, NULL
) < 0) {
940 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to move %s to %s: %m", mount_tmp
, mount_outside
);
944 mount_outside_mounted
= true;
945 mount_tmp_mounted
= false;
947 if (S_ISDIR(st
.st_mode
))
948 (void) rmdir(mount_tmp
);
950 (void) unlink(mount_tmp
);
951 mount_tmp_created
= false;
953 (void) umount(mount_slave
);
954 mount_slave_mounted
= false;
956 (void) rmdir(mount_slave
);
957 mount_slave_created
= false;
959 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0) {
960 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
964 r
= safe_fork("(sd-bindmnt)", FORK_RESET_SIGNALS
, &child
);
966 sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
970 const char *mount_inside
;
974 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
976 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
977 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
979 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
983 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
984 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
988 if (make_file_or_directory
) {
989 if (S_ISDIR(st
.st_mode
))
990 (void) mkdir_p(dest
, 0755);
992 (void) mkdir_parents(dest
, 0755);
993 safe_close(open(dest
, O_CREAT
|O_EXCL
|O_WRONLY
|O_CLOEXEC
|O_NOCTTY
, 0600));
997 /* Fifth, move the mount to the right place inside */
998 mount_inside
= strjoina("/run/systemd/nspawn/incoming/", basename(mount_outside
));
999 if (mount(mount_inside
, dest
, NULL
, MS_MOVE
, NULL
) < 0) {
1000 r
= log_error_errno(errno
, "Failed to mount: %m");
1004 _exit(EXIT_SUCCESS
);
1007 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1008 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1010 _exit(EXIT_FAILURE
);
1013 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1015 r
= wait_for_terminate_and_check("(sd-bindmnt)", child
, 0);
1017 r
= sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1020 if (r
!= EXIT_SUCCESS
) {
1021 if (read(errno_pipe_fd
[0], &r
, sizeof(r
)) == sizeof(r
))
1022 r
= sd_bus_error_set_errnof(error
, r
, "Failed to mount: %m");
1024 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child failed.");
1028 r
= sd_bus_reply_method_return(message
, NULL
);
1031 if (mount_outside_mounted
)
1032 (void) umount(mount_outside
);
1033 if (mount_outside_created
) {
1034 if (S_ISDIR(st
.st_mode
))
1035 (void) rmdir(mount_outside
);
1037 (void) unlink(mount_outside
);
1040 if (mount_tmp_mounted
)
1041 (void) umount(mount_tmp
);
1042 if (mount_tmp_created
) {
1043 if (S_ISDIR(st
.st_mode
))
1044 (void) rmdir(mount_tmp
);
1046 (void) unlink(mount_tmp
);
1049 if (mount_slave_mounted
)
1050 (void) umount(mount_slave
);
1051 if (mount_slave_created
)
1052 (void) rmdir(mount_slave
);
1057 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1058 const char *src
, *dest
, *host_path
, *container_path
, *host_basename
, *container_basename
, *container_dirname
;
1059 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
1060 CopyFlags copy_flags
= COPY_REFLINK
|COPY_MERGE
;
1061 _cleanup_close_
int hostfd
= -1;
1062 Machine
*m
= userdata
;
1072 if (m
->manager
->n_operations
>= OPERATIONS_MAX
)
1073 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
1075 if (m
->class != MACHINE_CONTAINER
)
1076 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
1078 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
1082 if (!path_is_absolute(src
))
1083 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
1087 else if (!path_is_absolute(dest
))
1088 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
1090 r
= bus_verify_polkit_async(
1093 "org.freedesktop.machine1.manage-machines",
1097 &m
->manager
->polkit_registry
,
1102 return 1; /* Will call us back */
1104 r
= machine_get_uid_shift(m
, &uid_shift
);
1108 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
1111 container_path
= src
;
1115 container_path
= dest
;
1118 host_basename
= basename(host_path
);
1120 container_basename
= basename(container_path
);
1121 t
= strdupa(container_path
);
1122 container_dirname
= dirname(t
);
1124 hostfd
= open_parent(host_path
, O_CLOEXEC
, 0);
1126 return sd_bus_error_set_errnof(error
, hostfd
, "Failed to open host directory %s: %m", host_path
);
1128 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1129 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1131 r
= safe_fork("(sd-copy)", FORK_RESET_SIGNALS
, &child
);
1133 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1139 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1141 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
1142 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
1144 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
1148 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
1149 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
1153 containerfd
= open(container_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1154 if (containerfd
< 0) {
1155 r
= log_error_errno(errno
, "Failed to open destination directory: %m");
1159 /* Run the actual copy operation. Note that when an UID shift is set we'll either clamp the UID/GID to
1160 * 0 or to the actual UID shift depending on the direction we copy. If no UID shift is set we'll copy
1161 * the UID/GIDs as they are. */
1163 r
= copy_tree_at(containerfd
, container_basename
, hostfd
, host_basename
, uid_shift
== 0 ? UID_INVALID
: 0, uid_shift
== 0 ? GID_INVALID
: 0, copy_flags
);
1165 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
);
1167 hostfd
= safe_close(hostfd
);
1168 containerfd
= safe_close(containerfd
);
1171 r
= log_error_errno(r
, "Failed to copy tree: %m");
1175 _exit(EXIT_SUCCESS
);
1178 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1179 _exit(EXIT_FAILURE
);
1182 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1184 /* Copying might take a while, hence install a watch on the child, and return */
1186 r
= operation_new(m
->manager
, m
, child
, message
, errno_pipe_fd
[0], NULL
);
1188 (void) sigkill_wait(child
);
1191 errno_pipe_fd
[0] = -1;
1196 int bus_machine_method_open_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1197 _cleanup_close_
int fd
= -1;
1198 Machine
*m
= userdata
;
1204 r
= bus_verify_polkit_async(
1207 "org.freedesktop.machine1.manage-machines",
1211 &m
->manager
->polkit_registry
,
1216 return 1; /* Will call us back */
1221 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1227 case MACHINE_CONTAINER
: {
1228 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
1229 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1232 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
1236 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1239 r
= namespace_fork("(sd-openrootns)", "(sd-openroot)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
1240 -1, mntns_fd
, -1, -1, root_fd
, &child
);
1242 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1244 _cleanup_close_
int dfd
= -1;
1246 pair
[0] = safe_close(pair
[0]);
1248 dfd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1250 _exit(EXIT_FAILURE
);
1252 r
= send_one_fd(pair
[1], dfd
, 0);
1253 dfd
= safe_close(dfd
);
1255 _exit(EXIT_FAILURE
);
1257 _exit(EXIT_SUCCESS
);
1260 pair
[1] = safe_close(pair
[1]);
1262 r
= wait_for_terminate_and_check("(sd-openrootns)", child
, 0);
1264 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1265 if (r
!= EXIT_SUCCESS
)
1266 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1268 fd
= receive_one_fd(pair
[0], MSG_DONTWAIT
);
1276 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Opening the root directory is only supported on container machines.");
1279 return sd_bus_reply_method_return(message
, "h", fd
);
1282 int bus_machine_method_get_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1283 Machine
*m
= userdata
;
1290 /* You wonder why this is a method and not a property? Well, properties are not supposed to return errors, but
1291 * we kinda have to for this. */
1293 if (m
->class == MACHINE_HOST
)
1294 return sd_bus_reply_method_return(message
, "u", UINT32_C(0));
1296 if (m
->class != MACHINE_CONTAINER
)
1297 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "UID/GID shift may only be determined for container machines.");
1299 r
= machine_get_uid_shift(m
, &shift
);
1301 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Machine %s uses a complex UID/GID mapping, cannot determine shift", m
->name
);
1305 return sd_bus_reply_method_return(message
, "u", (uint32_t) shift
);
1308 const sd_bus_vtable machine_vtable
[] = {
1309 SD_BUS_VTABLE_START(0),
1310 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1311 SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128
, offsetof(Machine
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1312 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1313 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1314 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1315 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1316 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
), SD_BUS_VTABLE_PROPERTY_CONST
),
1317 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1318 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1319 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1320 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1321 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_machine_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
1322 SD_BUS_METHOD("Kill", "si", NULL
, bus_machine_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
1323 SD_BUS_METHOD("GetAddresses", NULL
, "a(iay)", bus_machine_method_get_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1324 SD_BUS_METHOD("GetOSRelease", NULL
, "a{ss}", bus_machine_method_get_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1325 SD_BUS_METHOD("GetUIDShift", NULL
, "u", bus_machine_method_get_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1326 SD_BUS_METHOD("OpenPTY", NULL
, "hs", bus_machine_method_open_pty
, SD_BUS_VTABLE_UNPRIVILEGED
),
1327 SD_BUS_METHOD("OpenLogin", NULL
, "hs", bus_machine_method_open_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1328 SD_BUS_METHOD("OpenShell", "ssasas", "hs", bus_machine_method_open_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1329 SD_BUS_METHOD("BindMount", "ssbb", NULL
, bus_machine_method_bind_mount
, SD_BUS_VTABLE_UNPRIVILEGED
),
1330 SD_BUS_METHOD("CopyFrom", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1331 SD_BUS_METHOD("CopyTo", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1332 SD_BUS_METHOD("OpenRootDirectory", NULL
, "h", bus_machine_method_open_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1336 int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1337 Manager
*m
= userdata
;
1347 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1348 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1349 sd_bus_message
*message
;
1352 message
= sd_bus_get_current_message(bus
);
1356 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1360 r
= sd_bus_creds_get_pid(creds
, &pid
);
1364 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1368 _cleanup_free_
char *e
= NULL
;
1371 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1375 e
= bus_label_unescape(p
);
1379 machine
= hashmap_get(m
->machines
, e
);
1388 char *machine_bus_path(Machine
*m
) {
1389 _cleanup_free_
char *e
= NULL
;
1393 e
= bus_label_escape(m
->name
);
1397 return strappend("/org/freedesktop/machine1/machine/", e
);
1400 int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1401 _cleanup_strv_free_
char **l
= NULL
;
1402 Machine
*machine
= NULL
;
1403 Manager
*m
= userdata
;
1411 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1414 p
= machine_bus_path(machine
);
1418 r
= strv_consume(&l
, p
);
1423 *nodes
= TAKE_PTR(l
);
1428 int machine_send_signal(Machine
*m
, bool new_machine
) {
1429 _cleanup_free_
char *p
= NULL
;
1433 p
= machine_bus_path(m
);
1437 return sd_bus_emit_signal(
1439 "/org/freedesktop/machine1",
1440 "org.freedesktop.machine1.Manager",
1441 new_machine
? "MachineNew" : "MachineRemoved",
1445 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1446 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1447 _cleanup_free_
char *p
= NULL
;
1451 if (!m
->create_message
)
1454 c
= TAKE_PTR(m
->create_message
);
1457 return sd_bus_reply_method_error(c
, error
);
1459 /* Update the machine state file before we notify the client
1460 * about the result. */
1463 p
= machine_bus_path(m
);
1467 return sd_bus_reply_method_return(c
, "o", p
);