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"
26 #include "local-addresses.h"
27 #include "machine-dbus.h"
31 #include "path-util.h"
32 #include "process-util.h"
33 #include "signal-util.h"
35 #include "terminal-util.h"
36 #include "user-util.h"
38 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class
, machine_class
, MachineClass
);
39 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", Machine
, machine_get_state
, machine_state_to_string
);
41 static int property_get_netif(
44 const char *interface
,
46 sd_bus_message
*reply
,
48 sd_bus_error
*error
) {
50 Machine
*m
= userdata
;
56 assert_cc(sizeof(int) == sizeof(int32_t));
58 return sd_bus_message_append_array(reply
, 'i', m
->netif
, m
->n_netif
* sizeof(int));
61 int bus_machine_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
62 Machine
*m
= userdata
;
68 r
= bus_verify_polkit_async(
71 "org.freedesktop.machine1.manage-machines",
75 &m
->manager
->polkit_registry
,
80 return 1; /* Will call us back */
86 return sd_bus_reply_method_return(message
, NULL
);
89 int bus_machine_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
90 Machine
*m
= userdata
;
99 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
106 who
= kill_who_from_string(swho
);
108 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
111 if (!SIGNAL_VALID(signo
))
112 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
114 r
= bus_verify_polkit_async(
117 "org.freedesktop.machine1.manage-machines",
121 &m
->manager
->polkit_registry
,
126 return 1; /* Will call us back */
128 r
= machine_kill(m
, who
, signo
);
132 return sd_bus_reply_method_return(message
, NULL
);
135 int bus_machine_method_get_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
136 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
137 Machine
*m
= userdata
;
143 r
= sd_bus_message_new_method_return(message
, &reply
);
147 r
= sd_bus_message_open_container(reply
, 'a', "(iay)");
154 _cleanup_free_
struct local_address
*addresses
= NULL
;
155 struct local_address
*a
;
158 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
162 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
164 r
= sd_bus_message_open_container(reply
, 'r', "iay");
168 r
= sd_bus_message_append(reply
, "i", addresses
[i
].family
);
172 r
= sd_bus_message_append_array(reply
, 'y', &addresses
[i
].address
, FAMILY_ADDRESS_SIZE(addresses
[i
].family
));
176 r
= sd_bus_message_close_container(reply
);
184 case MACHINE_CONTAINER
: {
185 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
186 _cleanup_free_
char *us
= NULL
, *them
= NULL
;
187 _cleanup_close_
int netns_fd
= -1;
191 r
= readlink_malloc("/proc/self/ns/net", &us
);
195 p
= procfs_file_alloca(m
->leader
, "ns/net");
196 r
= readlink_malloc(p
, &them
);
201 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_NETWORKING
, "Machine %s does not use private networking", m
->name
);
203 r
= namespace_open(m
->leader
, NULL
, NULL
, &netns_fd
, NULL
, NULL
);
207 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
210 r
= namespace_fork("(sd-addrns)", "(sd-addr)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
211 -1, -1, netns_fd
, -1, -1, &child
);
213 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
215 _cleanup_free_
struct local_address
*addresses
= NULL
;
216 struct local_address
*a
;
219 pair
[0] = safe_close(pair
[0]);
221 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
225 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
226 struct iovec iov
[2] = {
227 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
228 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
231 r
= writev(pair
[1], iov
, 2);
236 pair
[1] = safe_close(pair
[1]);
241 pair
[1] = safe_close(pair
[1]);
246 union in_addr_union in_addr
;
253 iov
[0] = (struct iovec
) { .iov_base
= &family
, .iov_len
= sizeof(family
) };
254 iov
[1] = (struct iovec
) { .iov_base
= &in_addr
, .iov_len
= sizeof(in_addr
) };
256 n
= recvmsg(pair
[0], &mh
, 0);
259 if ((size_t) n
< sizeof(family
))
262 r
= sd_bus_message_open_container(reply
, 'r', "iay");
266 r
= sd_bus_message_append(reply
, "i", family
);
273 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
276 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
280 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
283 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
289 r
= sd_bus_message_close_container(reply
);
294 r
= wait_for_terminate_and_check("(sd-addrns)", child
, 0);
296 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
297 if (r
!= EXIT_SUCCESS
)
298 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
303 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
306 r
= sd_bus_message_close_container(reply
);
310 return sd_bus_send(NULL
, reply
, NULL
);
313 #define EXIT_NOT_FOUND 2
315 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
316 _cleanup_strv_free_
char **l
= NULL
;
317 Machine
*m
= userdata
;
326 r
= load_os_release_pairs(NULL
, &l
);
332 case MACHINE_CONTAINER
: {
333 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1, pidns_fd
= -1;
334 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
335 _cleanup_fclose_
FILE *f
= NULL
;
338 r
= namespace_open(m
->leader
, &pidns_fd
, &mntns_fd
, NULL
, NULL
, &root_fd
);
342 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
345 r
= namespace_fork("(sd-osrelns)", "(sd-osrel)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
346 pidns_fd
, mntns_fd
, -1, -1, root_fd
,
349 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
353 pair
[0] = safe_close(pair
[0]);
355 r
= open_os_release(NULL
, NULL
, &fd
);
357 _exit(EXIT_NOT_FOUND
);
361 r
= copy_bytes(fd
, pair
[1], (uint64_t) -1, 0);
368 pair
[1] = safe_close(pair
[1]);
370 f
= fdopen(pair
[0], "re");
376 r
= load_env_file_pairs(f
, "/etc/os-release", &l
);
380 r
= wait_for_terminate_and_check("(sd-osrelns)", child
, 0);
382 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
383 if (r
== EXIT_NOT_FOUND
)
384 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Machine does not contain OS release information");
385 if (r
!= EXIT_SUCCESS
)
386 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
392 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
395 return bus_reply_pair_array(message
, l
);
398 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
399 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
400 _cleanup_free_
char *pty_name
= NULL
;
401 _cleanup_close_
int master
= -1;
402 Machine
*m
= userdata
;
408 r
= bus_verify_polkit_async(
411 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
415 &m
->manager
->polkit_registry
,
420 return 1; /* Will call us back */
422 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
426 r
= ptsname_namespace(master
, &pty_name
);
430 r
= sd_bus_message_new_method_return(message
, &reply
);
434 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
438 return sd_bus_send(NULL
, reply
, NULL
);
441 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
453 case MACHINE_CONTAINER
: {
454 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
457 r
= sd_bus_new(&bus
);
461 if (asprintf(&address
, "x-machine-kernel:pid=%1$" PID_PRI
";x-machine-unix:pid=%1$" PID_PRI
, m
->leader
) < 0)
464 bus
->address
= address
;
465 bus
->bus_client
= true;
466 bus
->trusted
= false;
467 bus
->is_system
= true;
469 r
= sd_bus_start(bus
);
471 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
475 *ret
= TAKE_PTR(bus
);
486 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
487 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
488 _cleanup_free_
char *pty_name
= NULL
;
489 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
490 _cleanup_close_
int master
= -1;
491 sd_bus
*container_bus
= NULL
;
492 Machine
*m
= userdata
;
493 const char *p
, *getty
;
499 r
= bus_verify_polkit_async(
502 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login",
506 &m
->manager
->polkit_registry
,
511 return 1; /* Will call us back */
513 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
517 r
= ptsname_namespace(master
, &pty_name
);
521 p
= path_startswith(pty_name
, "/dev/pts/");
523 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "PTS name %s is invalid", pty_name
);
525 r
= container_bus_new(m
, error
, &allocated_bus
);
529 container_bus
= allocated_bus
?: m
->manager
->bus
;
531 getty
= strjoina("container-getty@", p
, ".service");
533 r
= sd_bus_call_method(
535 "org.freedesktop.systemd1",
536 "/org/freedesktop/systemd1",
537 "org.freedesktop.systemd1.Manager",
540 "ss", getty
, "replace");
544 r
= sd_bus_message_new_method_return(message
, &reply
);
548 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
552 return sd_bus_send(NULL
, reply
, NULL
);
555 int bus_machine_method_open_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
556 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *tm
= NULL
;
557 _cleanup_free_
char *pty_name
= NULL
;
558 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
559 sd_bus
*container_bus
= NULL
;
560 _cleanup_close_
int master
= -1, slave
= -1;
561 _cleanup_strv_free_
char **env
= NULL
, **args_wire
= NULL
, **args
= NULL
;
562 Machine
*m
= userdata
;
563 const char *p
, *unit
, *user
, *path
, *description
, *utmp_id
;
569 r
= sd_bus_message_read(message
, "ss", &user
, &path
);
572 user
= empty_to_null(user
);
573 r
= sd_bus_message_read_strv(message
, &args_wire
);
579 args
= new0(char*, 3 + 1);
582 args
[0] = strdup("sh");
585 args
[1] = strdup("-c");
588 r
= asprintf(&args
[2],
589 "shell=$(getent passwd %s 2>/dev/null | { IFS=: read _ _ _ _ _ _ x; echo \"$x\"; })\n"\
590 "exec \"${shell:-/bin/sh}\" -l", /* -l is means --login */
591 isempty(user
) ? "root" : user
);
597 if (!path_is_absolute(path
))
598 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified path '%s' is not absolute", path
);
599 args
= TAKE_PTR(args_wire
);
600 if (strv_isempty(args
)) {
601 args
= strv_free(args
);
603 args
= strv_new(path
);
609 r
= sd_bus_message_read_strv(message
, &env
);
612 if (!strv_env_is_valid(env
))
613 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
615 r
= bus_verify_polkit_async(
618 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
622 &m
->manager
->polkit_registry
,
627 return 1; /* Will call us back */
629 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
633 r
= ptsname_namespace(master
, &pty_name
);
637 p
= path_startswith(pty_name
, "/dev/pts/");
640 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
644 utmp_id
= path_startswith(pty_name
, "/dev/");
647 r
= container_bus_new(m
, error
, &allocated_bus
);
651 container_bus
= allocated_bus
?: m
->manager
->bus
;
653 r
= sd_bus_message_new_method_call(
656 "org.freedesktop.systemd1",
657 "/org/freedesktop/systemd1",
658 "org.freedesktop.systemd1.Manager",
659 "StartTransientUnit");
664 unit
= strjoina("container-shell@", p
, ".service");
665 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
670 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
674 description
= strjoina("Shell for User ", isempty(user
) ? "root" : user
);
675 r
= sd_bus_message_append(tm
,
676 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
677 "Description", "s", description
,
678 "StandardInputFileDescriptor", "h", slave
,
679 "StandardOutputFileDescriptor", "h", slave
,
680 "StandardErrorFileDescriptor", "h", slave
,
681 "SendSIGHUP", "b", true,
682 "IgnoreSIGPIPE", "b", false,
683 "KillMode", "s", "mixed",
684 "TTYReset", "b", true,
685 "UtmpIdentifier", "s", utmp_id
,
686 "UtmpMode", "s", "user",
687 "PAMName", "s", "login",
688 "WorkingDirectory", "s", "-~");
692 r
= sd_bus_message_append(tm
, "(sv)", "User", "s", isempty(user
) ? "root" : user
);
696 if (!strv_isempty(env
)) {
697 r
= sd_bus_message_open_container(tm
, 'r', "sv");
701 r
= sd_bus_message_append(tm
, "s", "Environment");
705 r
= sd_bus_message_open_container(tm
, 'v', "as");
709 r
= sd_bus_message_append_strv(tm
, env
);
713 r
= sd_bus_message_close_container(tm
);
717 r
= sd_bus_message_close_container(tm
);
723 r
= sd_bus_message_open_container(tm
, 'r', "sv");
727 r
= sd_bus_message_append(tm
, "s", "ExecStart");
731 r
= sd_bus_message_open_container(tm
, 'v', "a(sasb)");
735 r
= sd_bus_message_open_container(tm
, 'a', "(sasb)");
739 r
= sd_bus_message_open_container(tm
, 'r', "sasb");
743 r
= sd_bus_message_append(tm
, "s", path
);
747 r
= sd_bus_message_append_strv(tm
, args
);
751 r
= sd_bus_message_append(tm
, "b", true);
755 r
= sd_bus_message_close_container(tm
);
759 r
= sd_bus_message_close_container(tm
);
763 r
= sd_bus_message_close_container(tm
);
767 r
= sd_bus_message_close_container(tm
);
771 r
= sd_bus_message_close_container(tm
);
775 /* Auxiliary units */
776 r
= sd_bus_message_append(tm
, "a(sa(sv))", 0);
780 r
= sd_bus_call(container_bus
, tm
, 0, error
, NULL
);
784 slave
= safe_close(slave
);
786 r
= sd_bus_message_new_method_return(message
, &reply
);
790 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
794 return sd_bus_send(NULL
, reply
, NULL
);
797 int bus_machine_method_bind_mount(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
798 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
799 char mount_slave
[] = "/tmp/propagate.XXXXXX", *mount_tmp
, *mount_outside
, *p
;
800 bool mount_slave_created
= false, mount_slave_mounted
= false,
801 mount_tmp_created
= false, mount_tmp_mounted
= false,
802 mount_outside_created
= false, mount_outside_mounted
= false;
803 _cleanup_free_
char *chased_src
= NULL
;
804 int read_only
, make_file_or_directory
;
805 const char *dest
, *src
;
806 Machine
*m
= userdata
;
815 if (m
->class != MACHINE_CONTAINER
)
816 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
818 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_file_or_directory
);
822 if (!path_is_absolute(src
) || !path_is_normalized(src
))
823 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and not contain ../.");
827 else if (!path_is_absolute(dest
) || !path_is_normalized(dest
))
828 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and not contain ../.");
830 r
= bus_verify_polkit_async(
833 "org.freedesktop.machine1.manage-machines",
837 &m
->manager
->polkit_registry
,
842 return 1; /* Will call us back */
844 r
= machine_get_uid_shift(m
, &uid
);
848 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Can't bind mount on container with user namespacing applied.");
850 /* One day, when bind mounting /proc/self/fd/n works across
851 * namespace boundaries we should rework this logic to make
854 p
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/");
855 if (laccess(p
, F_OK
) < 0)
856 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Container does not allow propagation of mount points.");
858 r
= chase_symlinks(src
, NULL
, CHASE_TRAIL_SLASH
, &chased_src
);
860 return sd_bus_error_set_errnof(error
, r
, "Failed to resolve source path: %m");
862 if (lstat(chased_src
, &st
) < 0)
863 return sd_bus_error_set_errnof(error
, errno
, "Failed to stat() source path: %m");
864 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… */
865 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Source directory can't be a symbolic link");
867 /* Our goal is to install a new bind mount into the container,
868 possibly read-only. This is irritatingly complex
869 unfortunately, currently.
871 First, we start by creating a private playground in /tmp,
872 that we can mount MS_SLAVE. (Which is necessary, since
873 MS_MOVE cannot be applied to mounts with MS_SHARED parent
876 if (!mkdtemp(mount_slave
))
877 return sd_bus_error_set_errnof(error
, errno
, "Failed to create playground %s: %m", mount_slave
);
879 mount_slave_created
= true;
881 if (mount(mount_slave
, mount_slave
, NULL
, MS_BIND
, NULL
) < 0) {
882 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to make bind mount %s: %m", mount_slave
);
886 mount_slave_mounted
= true;
888 if (mount(NULL
, mount_slave
, NULL
, MS_SLAVE
, NULL
) < 0) {
889 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount slave %s: %m", mount_slave
);
893 /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
894 mount_tmp
= strjoina(mount_slave
, "/mount");
895 if (S_ISDIR(st
.st_mode
))
896 r
= mkdir_errno_wrapper(mount_tmp
, 0700);
898 r
= touch(mount_tmp
);
900 sd_bus_error_set_errnof(error
, errno
, "Failed to create temporary mount point %s: %m", mount_tmp
);
904 mount_tmp_created
= true;
906 if (mount(chased_src
, mount_tmp
, NULL
, MS_BIND
, NULL
) < 0) {
907 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to mount %s: %m", chased_src
);
911 mount_tmp_mounted
= true;
913 /* Third, we remount the new bind mount read-only if requested. */
915 if (mount(NULL
, mount_tmp
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
) < 0) {
916 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount read-only %s: %m", mount_tmp
);
920 /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
923 mount_outside
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/XXXXXX");
924 if (S_ISDIR(st
.st_mode
))
925 r
= mkdtemp(mount_outside
) ? 0 : -errno
;
927 r
= mkostemp_safe(mount_outside
);
931 sd_bus_error_set_errnof(error
, errno
, "Cannot create propagation file or directory %s: %m", mount_outside
);
935 mount_outside_created
= true;
937 if (mount(mount_tmp
, mount_outside
, NULL
, MS_MOVE
, NULL
) < 0) {
938 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to move %s to %s: %m", mount_tmp
, mount_outside
);
942 mount_outside_mounted
= true;
943 mount_tmp_mounted
= false;
945 if (S_ISDIR(st
.st_mode
))
946 (void) rmdir(mount_tmp
);
948 (void) unlink(mount_tmp
);
949 mount_tmp_created
= false;
951 (void) umount(mount_slave
);
952 mount_slave_mounted
= false;
954 (void) rmdir(mount_slave
);
955 mount_slave_created
= false;
957 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0) {
958 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
962 r
= safe_fork("(sd-bindmnt)", FORK_RESET_SIGNALS
, &child
);
964 sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
968 const char *mount_inside
;
972 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
974 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
975 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
977 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
981 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
982 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
986 if (make_file_or_directory
) {
987 if (S_ISDIR(st
.st_mode
))
988 (void) mkdir_p(dest
, 0755);
990 (void) mkdir_parents(dest
, 0755);
991 safe_close(open(dest
, O_CREAT
|O_EXCL
|O_WRONLY
|O_CLOEXEC
|O_NOCTTY
, 0600));
995 /* Fifth, move the mount to the right place inside */
996 mount_inside
= strjoina("/run/systemd/nspawn/incoming/", basename(mount_outside
));
997 if (mount(mount_inside
, dest
, NULL
, MS_MOVE
, NULL
) < 0) {
998 r
= log_error_errno(errno
, "Failed to mount: %m");
1002 _exit(EXIT_SUCCESS
);
1005 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1006 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1008 _exit(EXIT_FAILURE
);
1011 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1013 r
= wait_for_terminate_and_check("(sd-bindmnt)", child
, 0);
1015 r
= sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1018 if (r
!= EXIT_SUCCESS
) {
1019 if (read(errno_pipe_fd
[0], &r
, sizeof(r
)) == sizeof(r
))
1020 r
= sd_bus_error_set_errnof(error
, r
, "Failed to mount: %m");
1022 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child failed.");
1026 r
= sd_bus_reply_method_return(message
, NULL
);
1029 if (mount_outside_mounted
)
1030 (void) umount(mount_outside
);
1031 if (mount_outside_created
) {
1032 if (S_ISDIR(st
.st_mode
))
1033 (void) rmdir(mount_outside
);
1035 (void) unlink(mount_outside
);
1038 if (mount_tmp_mounted
)
1039 (void) umount(mount_tmp
);
1040 if (mount_tmp_created
) {
1041 if (S_ISDIR(st
.st_mode
))
1042 (void) rmdir(mount_tmp
);
1044 (void) unlink(mount_tmp
);
1047 if (mount_slave_mounted
)
1048 (void) umount(mount_slave
);
1049 if (mount_slave_created
)
1050 (void) rmdir(mount_slave
);
1055 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1056 const char *src
, *dest
, *host_path
, *container_path
, *host_basename
, *container_basename
, *container_dirname
;
1057 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
1058 CopyFlags copy_flags
= COPY_REFLINK
|COPY_MERGE
;
1059 _cleanup_close_
int hostfd
= -1;
1060 Machine
*m
= userdata
;
1070 if (m
->manager
->n_operations
>= OPERATIONS_MAX
)
1071 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
1073 if (m
->class != MACHINE_CONTAINER
)
1074 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
1076 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
1080 if (!path_is_absolute(src
))
1081 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
1085 else if (!path_is_absolute(dest
))
1086 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
1088 r
= bus_verify_polkit_async(
1091 "org.freedesktop.machine1.manage-machines",
1095 &m
->manager
->polkit_registry
,
1100 return 1; /* Will call us back */
1102 r
= machine_get_uid_shift(m
, &uid_shift
);
1106 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
1109 container_path
= src
;
1113 container_path
= dest
;
1116 host_basename
= basename(host_path
);
1118 container_basename
= basename(container_path
);
1119 t
= strdupa(container_path
);
1120 container_dirname
= dirname(t
);
1122 hostfd
= open_parent(host_path
, O_CLOEXEC
, 0);
1124 return sd_bus_error_set_errnof(error
, hostfd
, "Failed to open host directory %s: %m", host_path
);
1126 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1127 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1129 r
= safe_fork("(sd-copy)", FORK_RESET_SIGNALS
, &child
);
1131 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1137 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1139 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
1140 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
1142 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
1146 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
1147 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
1151 containerfd
= open(container_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1152 if (containerfd
< 0) {
1153 r
= log_error_errno(errno
, "Failed to open destination directory: %m");
1157 /* Run the actual copy operation. Note that when an UID shift is set we'll either clamp the UID/GID to
1158 * 0 or to the actual UID shift depending on the direction we copy. If no UID shift is set we'll copy
1159 * the UID/GIDs as they are. */
1161 r
= copy_tree_at(containerfd
, container_basename
, hostfd
, host_basename
, uid_shift
== 0 ? UID_INVALID
: 0, uid_shift
== 0 ? GID_INVALID
: 0, copy_flags
);
1163 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
);
1165 hostfd
= safe_close(hostfd
);
1166 containerfd
= safe_close(containerfd
);
1169 r
= log_error_errno(r
, "Failed to copy tree: %m");
1173 _exit(EXIT_SUCCESS
);
1176 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1177 _exit(EXIT_FAILURE
);
1180 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1182 /* Copying might take a while, hence install a watch on the child, and return */
1184 r
= operation_new(m
->manager
, m
, child
, message
, errno_pipe_fd
[0], NULL
);
1186 (void) sigkill_wait(child
);
1189 errno_pipe_fd
[0] = -1;
1194 int bus_machine_method_open_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1195 _cleanup_close_
int fd
= -1;
1196 Machine
*m
= userdata
;
1202 r
= bus_verify_polkit_async(
1205 "org.freedesktop.machine1.manage-machines",
1209 &m
->manager
->polkit_registry
,
1214 return 1; /* Will call us back */
1219 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1225 case MACHINE_CONTAINER
: {
1226 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
1227 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1230 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
1234 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1237 r
= namespace_fork("(sd-openrootns)", "(sd-openroot)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
1238 -1, mntns_fd
, -1, -1, root_fd
, &child
);
1240 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1242 _cleanup_close_
int dfd
= -1;
1244 pair
[0] = safe_close(pair
[0]);
1246 dfd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1248 _exit(EXIT_FAILURE
);
1250 r
= send_one_fd(pair
[1], dfd
, 0);
1251 dfd
= safe_close(dfd
);
1253 _exit(EXIT_FAILURE
);
1255 _exit(EXIT_SUCCESS
);
1258 pair
[1] = safe_close(pair
[1]);
1260 r
= wait_for_terminate_and_check("(sd-openrootns)", child
, 0);
1262 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1263 if (r
!= EXIT_SUCCESS
)
1264 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1266 fd
= receive_one_fd(pair
[0], MSG_DONTWAIT
);
1274 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Opening the root directory is only supported on container machines.");
1277 return sd_bus_reply_method_return(message
, "h", fd
);
1280 int bus_machine_method_get_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1281 Machine
*m
= userdata
;
1288 /* You wonder why this is a method and not a property? Well, properties are not supposed to return errors, but
1289 * we kinda have to for this. */
1291 if (m
->class == MACHINE_HOST
)
1292 return sd_bus_reply_method_return(message
, "u", UINT32_C(0));
1294 if (m
->class != MACHINE_CONTAINER
)
1295 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "UID/GID shift may only be determined for container machines.");
1297 r
= machine_get_uid_shift(m
, &shift
);
1299 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Machine %s uses a complex UID/GID mapping, cannot determine shift", m
->name
);
1303 return sd_bus_reply_method_return(message
, "u", (uint32_t) shift
);
1306 const sd_bus_vtable machine_vtable
[] = {
1307 SD_BUS_VTABLE_START(0),
1308 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1309 SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128
, offsetof(Machine
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1310 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1311 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1312 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1313 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1314 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
), SD_BUS_VTABLE_PROPERTY_CONST
),
1315 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1316 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1317 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1318 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1319 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_machine_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
1320 SD_BUS_METHOD("Kill", "si", NULL
, bus_machine_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
1321 SD_BUS_METHOD("GetAddresses", NULL
, "a(iay)", bus_machine_method_get_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1322 SD_BUS_METHOD("GetOSRelease", NULL
, "a{ss}", bus_machine_method_get_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1323 SD_BUS_METHOD("GetUIDShift", NULL
, "u", bus_machine_method_get_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1324 SD_BUS_METHOD("OpenPTY", NULL
, "hs", bus_machine_method_open_pty
, SD_BUS_VTABLE_UNPRIVILEGED
),
1325 SD_BUS_METHOD("OpenLogin", NULL
, "hs", bus_machine_method_open_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1326 SD_BUS_METHOD("OpenShell", "ssasas", "hs", bus_machine_method_open_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1327 SD_BUS_METHOD("BindMount", "ssbb", NULL
, bus_machine_method_bind_mount
, SD_BUS_VTABLE_UNPRIVILEGED
),
1328 SD_BUS_METHOD("CopyFrom", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1329 SD_BUS_METHOD("CopyTo", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1330 SD_BUS_METHOD("OpenRootDirectory", NULL
, "h", bus_machine_method_open_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1334 int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1335 Manager
*m
= userdata
;
1345 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1346 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1347 sd_bus_message
*message
;
1350 message
= sd_bus_get_current_message(bus
);
1354 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1358 r
= sd_bus_creds_get_pid(creds
, &pid
);
1362 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1366 _cleanup_free_
char *e
= NULL
;
1369 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1373 e
= bus_label_unescape(p
);
1377 machine
= hashmap_get(m
->machines
, e
);
1386 char *machine_bus_path(Machine
*m
) {
1387 _cleanup_free_
char *e
= NULL
;
1391 e
= bus_label_escape(m
->name
);
1395 return strappend("/org/freedesktop/machine1/machine/", e
);
1398 int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1399 _cleanup_strv_free_
char **l
= NULL
;
1400 Machine
*machine
= NULL
;
1401 Manager
*m
= userdata
;
1409 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1412 p
= machine_bus_path(machine
);
1416 r
= strv_consume(&l
, p
);
1421 *nodes
= TAKE_PTR(l
);
1426 int machine_send_signal(Machine
*m
, bool new_machine
) {
1427 _cleanup_free_
char *p
= NULL
;
1431 p
= machine_bus_path(m
);
1435 return sd_bus_emit_signal(
1437 "/org/freedesktop/machine1",
1438 "org.freedesktop.machine1.Manager",
1439 new_machine
? "MachineNew" : "MachineRemoved",
1443 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1444 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1445 _cleanup_free_
char *p
= NULL
;
1449 if (!m
->create_message
)
1452 c
= TAKE_PTR(m
->create_message
);
1455 return sd_bus_reply_method_error(c
, error
);
1457 /* Update the machine state file before we notify the client
1458 * about the result. */
1461 p
= machine_bus_path(m
);
1465 return sd_bus_reply_method_return(c
, "o", p
);