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 "user-util.h"
39 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class
, machine_class
, MachineClass
);
40 static BUS_DEFINE_PROPERTY_GET2(property_get_state
, "s", Machine
, machine_get_state
, machine_state_to_string
);
42 static int property_get_netif(
45 const char *interface
,
47 sd_bus_message
*reply
,
49 sd_bus_error
*error
) {
51 Machine
*m
= userdata
;
57 assert_cc(sizeof(int) == sizeof(int32_t));
59 return sd_bus_message_append_array(reply
, 'i', m
->netif
, m
->n_netif
* sizeof(int));
62 int bus_machine_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
63 Machine
*m
= userdata
;
69 r
= bus_verify_polkit_async(
72 "org.freedesktop.machine1.manage-machines",
76 &m
->manager
->polkit_registry
,
81 return 1; /* Will call us back */
87 return sd_bus_reply_method_return(message
, NULL
);
90 int bus_machine_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
91 Machine
*m
= userdata
;
100 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
107 who
= kill_who_from_string(swho
);
109 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
112 if (!SIGNAL_VALID(signo
))
113 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
115 r
= bus_verify_polkit_async(
118 "org.freedesktop.machine1.manage-machines",
122 &m
->manager
->polkit_registry
,
127 return 1; /* Will call us back */
129 r
= machine_kill(m
, who
, signo
);
133 return sd_bus_reply_method_return(message
, NULL
);
136 int bus_machine_method_get_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
137 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
138 Machine
*m
= userdata
;
144 r
= sd_bus_message_new_method_return(message
, &reply
);
148 r
= sd_bus_message_open_container(reply
, 'a', "(iay)");
155 _cleanup_free_
struct local_address
*addresses
= NULL
;
156 struct local_address
*a
;
159 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
163 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
165 r
= sd_bus_message_open_container(reply
, 'r', "iay");
169 r
= sd_bus_message_append(reply
, "i", addresses
[i
].family
);
173 r
= sd_bus_message_append_array(reply
, 'y', &addresses
[i
].address
, FAMILY_ADDRESS_SIZE(addresses
[i
].family
));
177 r
= sd_bus_message_close_container(reply
);
185 case MACHINE_CONTAINER
: {
186 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
187 _cleanup_free_
char *us
= NULL
, *them
= NULL
;
188 _cleanup_close_
int netns_fd
= -1;
192 r
= readlink_malloc("/proc/self/ns/net", &us
);
196 p
= procfs_file_alloca(m
->leader
, "ns/net");
197 r
= readlink_malloc(p
, &them
);
202 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_NETWORKING
, "Machine %s does not use private networking", m
->name
);
204 r
= namespace_open(m
->leader
, NULL
, NULL
, &netns_fd
, NULL
, NULL
);
208 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
211 r
= namespace_fork("(sd-addrns)", "(sd-addr)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
212 -1, -1, netns_fd
, -1, -1, &child
);
214 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
216 _cleanup_free_
struct local_address
*addresses
= NULL
;
217 struct local_address
*a
;
220 pair
[0] = safe_close(pair
[0]);
222 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
226 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
227 struct iovec iov
[2] = {
228 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
229 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
232 r
= writev(pair
[1], iov
, 2);
237 pair
[1] = safe_close(pair
[1]);
242 pair
[1] = safe_close(pair
[1]);
247 union in_addr_union in_addr
;
254 iov
[0] = IOVEC_MAKE(&family
, sizeof(family
));
255 iov
[1] = IOVEC_MAKE(&in_addr
, sizeof(in_addr
));
257 n
= recvmsg(pair
[0], &mh
, 0);
260 if ((size_t) n
< sizeof(family
))
263 r
= sd_bus_message_open_container(reply
, 'r', "iay");
267 r
= sd_bus_message_append(reply
, "i", family
);
274 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
277 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
281 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
284 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
290 r
= sd_bus_message_close_container(reply
);
295 r
= wait_for_terminate_and_check("(sd-addrns)", child
, 0);
297 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
298 if (r
!= EXIT_SUCCESS
)
299 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
304 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
307 r
= sd_bus_message_close_container(reply
);
311 return sd_bus_send(NULL
, reply
, NULL
);
314 #define EXIT_NOT_FOUND 2
316 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
317 _cleanup_strv_free_
char **l
= NULL
;
318 Machine
*m
= userdata
;
327 r
= load_os_release_pairs(NULL
, &l
);
333 case MACHINE_CONTAINER
: {
334 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1, pidns_fd
= -1;
335 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
336 _cleanup_fclose_
FILE *f
= NULL
;
339 r
= namespace_open(m
->leader
, &pidns_fd
, &mntns_fd
, NULL
, NULL
, &root_fd
);
343 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
346 r
= namespace_fork("(sd-osrelns)", "(sd-osrel)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
347 pidns_fd
, mntns_fd
, -1, -1, root_fd
,
350 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
354 pair
[0] = safe_close(pair
[0]);
356 r
= open_os_release(NULL
, NULL
, &fd
);
358 _exit(EXIT_NOT_FOUND
);
362 r
= copy_bytes(fd
, pair
[1], (uint64_t) -1, 0);
369 pair
[1] = safe_close(pair
[1]);
371 f
= fdopen(pair
[0], "re");
377 r
= load_env_file_pairs(f
, "/etc/os-release", &l
);
381 r
= wait_for_terminate_and_check("(sd-osrelns)", child
, 0);
383 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
384 if (r
== EXIT_NOT_FOUND
)
385 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Machine does not contain OS release information");
386 if (r
!= EXIT_SUCCESS
)
387 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
393 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
396 return bus_reply_pair_array(message
, l
);
399 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
400 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
401 _cleanup_free_
char *pty_name
= NULL
;
402 _cleanup_close_
int master
= -1;
403 Machine
*m
= userdata
;
409 r
= bus_verify_polkit_async(
412 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
416 &m
->manager
->polkit_registry
,
421 return 1; /* Will call us back */
423 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
427 r
= ptsname_namespace(master
, &pty_name
);
431 r
= sd_bus_message_new_method_return(message
, &reply
);
435 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
439 return sd_bus_send(NULL
, reply
, NULL
);
442 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
454 case MACHINE_CONTAINER
: {
455 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
458 r
= sd_bus_new(&bus
);
462 if (asprintf(&address
, "x-machine-kernel:pid=%1$" PID_PRI
";x-machine-unix:pid=%1$" PID_PRI
, m
->leader
) < 0)
465 bus
->address
= address
;
466 bus
->bus_client
= true;
467 bus
->trusted
= false;
468 bus
->is_system
= true;
470 r
= sd_bus_start(bus
);
472 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
476 *ret
= TAKE_PTR(bus
);
487 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
488 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
489 _cleanup_free_
char *pty_name
= NULL
;
490 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
491 _cleanup_close_
int master
= -1;
492 sd_bus
*container_bus
= NULL
;
493 Machine
*m
= userdata
;
494 const char *p
, *getty
;
500 r
= bus_verify_polkit_async(
503 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login",
507 &m
->manager
->polkit_registry
,
512 return 1; /* Will call us back */
514 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
518 r
= ptsname_namespace(master
, &pty_name
);
522 p
= path_startswith(pty_name
, "/dev/pts/");
524 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "PTS name %s is invalid", pty_name
);
526 r
= container_bus_new(m
, error
, &allocated_bus
);
530 container_bus
= allocated_bus
?: m
->manager
->bus
;
532 getty
= strjoina("container-getty@", p
, ".service");
534 r
= sd_bus_call_method(
536 "org.freedesktop.systemd1",
537 "/org/freedesktop/systemd1",
538 "org.freedesktop.systemd1.Manager",
541 "ss", getty
, "replace");
545 r
= sd_bus_message_new_method_return(message
, &reply
);
549 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
553 return sd_bus_send(NULL
, reply
, NULL
);
556 int bus_machine_method_open_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
557 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *tm
= NULL
;
558 _cleanup_free_
char *pty_name
= NULL
;
559 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
560 sd_bus
*container_bus
= NULL
;
561 _cleanup_close_
int master
= -1, slave
= -1;
562 _cleanup_strv_free_
char **env
= NULL
, **args_wire
= NULL
, **args
= NULL
;
563 Machine
*m
= userdata
;
564 const char *p
, *unit
, *user
, *path
, *description
, *utmp_id
;
570 r
= sd_bus_message_read(message
, "ss", &user
, &path
);
573 user
= empty_to_null(user
);
574 r
= sd_bus_message_read_strv(message
, &args_wire
);
580 args
= new0(char*, 3 + 1);
583 args
[0] = strdup("sh");
586 args
[1] = strdup("-c");
589 r
= asprintf(&args
[2],
590 "shell=$(getent passwd %s 2>/dev/null | { IFS=: read _ _ _ _ _ _ x; echo \"$x\"; })\n"\
591 "exec \"${shell:-/bin/sh}\" -l", /* -l is means --login */
592 isempty(user
) ? "root" : user
);
598 if (!path_is_absolute(path
))
599 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified path '%s' is not absolute", path
);
600 args
= TAKE_PTR(args_wire
);
601 if (strv_isempty(args
)) {
602 args
= strv_free(args
);
604 args
= strv_new(path
);
610 r
= sd_bus_message_read_strv(message
, &env
);
613 if (!strv_env_is_valid(env
))
614 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
616 r
= bus_verify_polkit_async(
619 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
623 &m
->manager
->polkit_registry
,
628 return 1; /* Will call us back */
630 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
634 r
= ptsname_namespace(master
, &pty_name
);
638 p
= path_startswith(pty_name
, "/dev/pts/");
641 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
645 utmp_id
= path_startswith(pty_name
, "/dev/");
648 r
= container_bus_new(m
, error
, &allocated_bus
);
652 container_bus
= allocated_bus
?: m
->manager
->bus
;
654 r
= sd_bus_message_new_method_call(
657 "org.freedesktop.systemd1",
658 "/org/freedesktop/systemd1",
659 "org.freedesktop.systemd1.Manager",
660 "StartTransientUnit");
665 unit
= strjoina("container-shell@", p
, ".service");
666 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
671 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
675 description
= strjoina("Shell for User ", isempty(user
) ? "root" : user
);
676 r
= sd_bus_message_append(tm
,
677 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
678 "Description", "s", description
,
679 "StandardInputFileDescriptor", "h", slave
,
680 "StandardOutputFileDescriptor", "h", slave
,
681 "StandardErrorFileDescriptor", "h", slave
,
682 "SendSIGHUP", "b", true,
683 "IgnoreSIGPIPE", "b", false,
684 "KillMode", "s", "mixed",
685 "TTYReset", "b", true,
686 "UtmpIdentifier", "s", utmp_id
,
687 "UtmpMode", "s", "user",
688 "PAMName", "s", "login",
689 "WorkingDirectory", "s", "-~");
693 r
= sd_bus_message_append(tm
, "(sv)", "User", "s", isempty(user
) ? "root" : user
);
697 if (!strv_isempty(env
)) {
698 r
= sd_bus_message_open_container(tm
, 'r', "sv");
702 r
= sd_bus_message_append(tm
, "s", "Environment");
706 r
= sd_bus_message_open_container(tm
, 'v', "as");
710 r
= sd_bus_message_append_strv(tm
, env
);
714 r
= sd_bus_message_close_container(tm
);
718 r
= sd_bus_message_close_container(tm
);
724 r
= sd_bus_message_open_container(tm
, 'r', "sv");
728 r
= sd_bus_message_append(tm
, "s", "ExecStart");
732 r
= sd_bus_message_open_container(tm
, 'v', "a(sasb)");
736 r
= sd_bus_message_open_container(tm
, 'a', "(sasb)");
740 r
= sd_bus_message_open_container(tm
, 'r', "sasb");
744 r
= sd_bus_message_append(tm
, "s", path
);
748 r
= sd_bus_message_append_strv(tm
, args
);
752 r
= sd_bus_message_append(tm
, "b", true);
756 r
= sd_bus_message_close_container(tm
);
760 r
= sd_bus_message_close_container(tm
);
764 r
= sd_bus_message_close_container(tm
);
768 r
= sd_bus_message_close_container(tm
);
772 r
= sd_bus_message_close_container(tm
);
776 /* Auxiliary units */
777 r
= sd_bus_message_append(tm
, "a(sa(sv))", 0);
781 r
= sd_bus_call(container_bus
, tm
, 0, error
, NULL
);
785 slave
= safe_close(slave
);
787 r
= sd_bus_message_new_method_return(message
, &reply
);
791 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
795 return sd_bus_send(NULL
, reply
, NULL
);
798 int bus_machine_method_bind_mount(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
799 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
800 char mount_slave
[] = "/tmp/propagate.XXXXXX", *mount_tmp
, *mount_outside
, *p
;
801 bool mount_slave_created
= false, mount_slave_mounted
= false,
802 mount_tmp_created
= false, mount_tmp_mounted
= false,
803 mount_outside_created
= false, mount_outside_mounted
= false;
804 _cleanup_free_
char *chased_src
= NULL
;
805 int read_only
, make_file_or_directory
;
806 const char *dest
, *src
;
807 Machine
*m
= userdata
;
816 if (m
->class != MACHINE_CONTAINER
)
817 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
819 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_file_or_directory
);
823 if (!path_is_absolute(src
) || !path_is_normalized(src
))
824 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and not contain ../.");
828 else if (!path_is_absolute(dest
) || !path_is_normalized(dest
))
829 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and not contain ../.");
831 r
= bus_verify_polkit_async(
834 "org.freedesktop.machine1.manage-machines",
838 &m
->manager
->polkit_registry
,
843 return 1; /* Will call us back */
845 r
= machine_get_uid_shift(m
, &uid
);
849 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Can't bind mount on container with user namespacing applied.");
851 /* One day, when bind mounting /proc/self/fd/n works across
852 * namespace boundaries we should rework this logic to make
855 p
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/");
856 if (laccess(p
, F_OK
) < 0)
857 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Container does not allow propagation of mount points.");
859 r
= chase_symlinks(src
, NULL
, CHASE_TRAIL_SLASH
, &chased_src
);
861 return sd_bus_error_set_errnof(error
, r
, "Failed to resolve source path: %m");
863 if (lstat(chased_src
, &st
) < 0)
864 return sd_bus_error_set_errnof(error
, errno
, "Failed to stat() source path: %m");
865 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… */
866 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Source directory can't be a symbolic link");
868 /* Our goal is to install a new bind mount into the container,
869 possibly read-only. This is irritatingly complex
870 unfortunately, currently.
872 First, we start by creating a private playground in /tmp,
873 that we can mount MS_SLAVE. (Which is necessary, since
874 MS_MOVE cannot be applied to mounts with MS_SHARED parent
877 if (!mkdtemp(mount_slave
))
878 return sd_bus_error_set_errnof(error
, errno
, "Failed to create playground %s: %m", mount_slave
);
880 mount_slave_created
= true;
882 if (mount(mount_slave
, mount_slave
, NULL
, MS_BIND
, NULL
) < 0) {
883 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to make bind mount %s: %m", mount_slave
);
887 mount_slave_mounted
= true;
889 if (mount(NULL
, mount_slave
, NULL
, MS_SLAVE
, NULL
) < 0) {
890 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount slave %s: %m", mount_slave
);
894 /* Second, we mount the source file or directory to a directory inside of our MS_SLAVE playground. */
895 mount_tmp
= strjoina(mount_slave
, "/mount");
896 if (S_ISDIR(st
.st_mode
))
897 r
= mkdir_errno_wrapper(mount_tmp
, 0700);
899 r
= touch(mount_tmp
);
901 sd_bus_error_set_errnof(error
, errno
, "Failed to create temporary mount point %s: %m", mount_tmp
);
905 mount_tmp_created
= true;
907 if (mount(chased_src
, mount_tmp
, NULL
, MS_BIND
, NULL
) < 0) {
908 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to mount %s: %m", chased_src
);
912 mount_tmp_mounted
= true;
914 /* Third, we remount the new bind mount read-only if requested. */
916 if (mount(NULL
, mount_tmp
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
) < 0) {
917 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount read-only %s: %m", mount_tmp
);
921 /* Fourth, we move the new bind mount into the propagation directory. This way it will appear there read-only
924 mount_outside
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/XXXXXX");
925 if (S_ISDIR(st
.st_mode
))
926 r
= mkdtemp(mount_outside
) ? 0 : -errno
;
928 r
= mkostemp_safe(mount_outside
);
932 sd_bus_error_set_errnof(error
, errno
, "Cannot create propagation file or directory %s: %m", mount_outside
);
936 mount_outside_created
= true;
938 if (mount(mount_tmp
, mount_outside
, NULL
, MS_MOVE
, NULL
) < 0) {
939 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to move %s to %s: %m", mount_tmp
, mount_outside
);
943 mount_outside_mounted
= true;
944 mount_tmp_mounted
= false;
946 if (S_ISDIR(st
.st_mode
))
947 (void) rmdir(mount_tmp
);
949 (void) unlink(mount_tmp
);
950 mount_tmp_created
= false;
952 (void) umount(mount_slave
);
953 mount_slave_mounted
= false;
955 (void) rmdir(mount_slave
);
956 mount_slave_created
= false;
958 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0) {
959 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
963 r
= safe_fork("(sd-bindmnt)", FORK_RESET_SIGNALS
, &child
);
965 sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
969 const char *mount_inside
;
973 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
975 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
976 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
978 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
982 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
983 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
987 if (make_file_or_directory
) {
988 if (S_ISDIR(st
.st_mode
))
989 (void) mkdir_p(dest
, 0755);
991 (void) mkdir_parents(dest
, 0755);
992 safe_close(open(dest
, O_CREAT
|O_EXCL
|O_WRONLY
|O_CLOEXEC
|O_NOCTTY
, 0600));
996 /* Fifth, move the mount to the right place inside */
997 mount_inside
= strjoina("/run/systemd/nspawn/incoming/", basename(mount_outside
));
998 if (mount(mount_inside
, dest
, NULL
, MS_MOVE
, NULL
) < 0) {
999 r
= log_error_errno(errno
, "Failed to mount: %m");
1003 _exit(EXIT_SUCCESS
);
1006 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1007 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1009 _exit(EXIT_FAILURE
);
1012 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1014 r
= wait_for_terminate_and_check("(sd-bindmnt)", child
, 0);
1016 r
= sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1019 if (r
!= EXIT_SUCCESS
) {
1020 if (read(errno_pipe_fd
[0], &r
, sizeof(r
)) == sizeof(r
))
1021 r
= sd_bus_error_set_errnof(error
, r
, "Failed to mount: %m");
1023 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child failed.");
1027 r
= sd_bus_reply_method_return(message
, NULL
);
1030 if (mount_outside_mounted
)
1031 (void) umount(mount_outside
);
1032 if (mount_outside_created
) {
1033 if (S_ISDIR(st
.st_mode
))
1034 (void) rmdir(mount_outside
);
1036 (void) unlink(mount_outside
);
1039 if (mount_tmp_mounted
)
1040 (void) umount(mount_tmp
);
1041 if (mount_tmp_created
) {
1042 if (S_ISDIR(st
.st_mode
))
1043 (void) rmdir(mount_tmp
);
1045 (void) unlink(mount_tmp
);
1048 if (mount_slave_mounted
)
1049 (void) umount(mount_slave
);
1050 if (mount_slave_created
)
1051 (void) rmdir(mount_slave
);
1056 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1057 const char *src
, *dest
, *host_path
, *container_path
, *host_basename
, *container_basename
, *container_dirname
;
1058 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
1059 CopyFlags copy_flags
= COPY_REFLINK
|COPY_MERGE
;
1060 _cleanup_close_
int hostfd
= -1;
1061 Machine
*m
= userdata
;
1071 if (m
->manager
->n_operations
>= OPERATIONS_MAX
)
1072 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
1074 if (m
->class != MACHINE_CONTAINER
)
1075 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
1077 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
1081 if (!path_is_absolute(src
))
1082 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
1086 else if (!path_is_absolute(dest
))
1087 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
1089 r
= bus_verify_polkit_async(
1092 "org.freedesktop.machine1.manage-machines",
1096 &m
->manager
->polkit_registry
,
1101 return 1; /* Will call us back */
1103 r
= machine_get_uid_shift(m
, &uid_shift
);
1107 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
1110 container_path
= src
;
1114 container_path
= dest
;
1117 host_basename
= basename(host_path
);
1119 container_basename
= basename(container_path
);
1120 t
= strdupa(container_path
);
1121 container_dirname
= dirname(t
);
1123 hostfd
= open_parent(host_path
, O_CLOEXEC
, 0);
1125 return sd_bus_error_set_errnof(error
, hostfd
, "Failed to open host directory %s: %m", host_path
);
1127 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1128 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1130 r
= safe_fork("(sd-copy)", FORK_RESET_SIGNALS
, &child
);
1132 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1138 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1140 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
1141 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
1143 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
1147 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
1148 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
1152 containerfd
= open(container_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1153 if (containerfd
< 0) {
1154 r
= log_error_errno(errno
, "Failed to open destination directory: %m");
1158 /* Run the actual copy operation. Note that when an UID shift is set we'll either clamp the UID/GID to
1159 * 0 or to the actual UID shift depending on the direction we copy. If no UID shift is set we'll copy
1160 * the UID/GIDs as they are. */
1162 r
= copy_tree_at(containerfd
, container_basename
, hostfd
, host_basename
, uid_shift
== 0 ? UID_INVALID
: 0, uid_shift
== 0 ? GID_INVALID
: 0, copy_flags
);
1164 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
);
1166 hostfd
= safe_close(hostfd
);
1167 containerfd
= safe_close(containerfd
);
1170 r
= log_error_errno(r
, "Failed to copy tree: %m");
1174 _exit(EXIT_SUCCESS
);
1177 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1178 _exit(EXIT_FAILURE
);
1181 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1183 /* Copying might take a while, hence install a watch on the child, and return */
1185 r
= operation_new(m
->manager
, m
, child
, message
, errno_pipe_fd
[0], NULL
);
1187 (void) sigkill_wait(child
);
1190 errno_pipe_fd
[0] = -1;
1195 int bus_machine_method_open_root_directory(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1196 _cleanup_close_
int fd
= -1;
1197 Machine
*m
= userdata
;
1203 r
= bus_verify_polkit_async(
1206 "org.freedesktop.machine1.manage-machines",
1210 &m
->manager
->polkit_registry
,
1215 return 1; /* Will call us back */
1220 fd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1226 case MACHINE_CONTAINER
: {
1227 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
1228 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1231 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
1235 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1238 r
= namespace_fork("(sd-openrootns)", "(sd-openroot)", NULL
, 0, FORK_RESET_SIGNALS
|FORK_DEATHSIG
,
1239 -1, mntns_fd
, -1, -1, root_fd
, &child
);
1241 return sd_bus_error_set_errnof(error
, r
, "Failed to fork(): %m");
1243 _cleanup_close_
int dfd
= -1;
1245 pair
[0] = safe_close(pair
[0]);
1247 dfd
= open("/", O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
1249 _exit(EXIT_FAILURE
);
1251 r
= send_one_fd(pair
[1], dfd
, 0);
1252 dfd
= safe_close(dfd
);
1254 _exit(EXIT_FAILURE
);
1256 _exit(EXIT_SUCCESS
);
1259 pair
[1] = safe_close(pair
[1]);
1261 r
= wait_for_terminate_and_check("(sd-openrootns)", child
, 0);
1263 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1264 if (r
!= EXIT_SUCCESS
)
1265 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1267 fd
= receive_one_fd(pair
[0], MSG_DONTWAIT
);
1275 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Opening the root directory is only supported on container machines.");
1278 return sd_bus_reply_method_return(message
, "h", fd
);
1281 int bus_machine_method_get_uid_shift(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1282 Machine
*m
= userdata
;
1289 /* You wonder why this is a method and not a property? Well, properties are not supposed to return errors, but
1290 * we kinda have to for this. */
1292 if (m
->class == MACHINE_HOST
)
1293 return sd_bus_reply_method_return(message
, "u", UINT32_C(0));
1295 if (m
->class != MACHINE_CONTAINER
)
1296 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "UID/GID shift may only be determined for container machines.");
1298 r
= machine_get_uid_shift(m
, &shift
);
1300 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Machine %s uses a complex UID/GID mapping, cannot determine shift", m
->name
);
1304 return sd_bus_reply_method_return(message
, "u", (uint32_t) shift
);
1307 const sd_bus_vtable machine_vtable
[] = {
1308 SD_BUS_VTABLE_START(0),
1309 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1310 SD_BUS_PROPERTY("Id", "ay", bus_property_get_id128
, offsetof(Machine
, id
), SD_BUS_VTABLE_PROPERTY_CONST
),
1311 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1312 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1313 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1314 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1315 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
), SD_BUS_VTABLE_PROPERTY_CONST
),
1316 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1317 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1318 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1319 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1320 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_machine_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
1321 SD_BUS_METHOD("Kill", "si", NULL
, bus_machine_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
1322 SD_BUS_METHOD("GetAddresses", NULL
, "a(iay)", bus_machine_method_get_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1323 SD_BUS_METHOD("GetOSRelease", NULL
, "a{ss}", bus_machine_method_get_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1324 SD_BUS_METHOD("GetUIDShift", NULL
, "u", bus_machine_method_get_uid_shift
, SD_BUS_VTABLE_UNPRIVILEGED
),
1325 SD_BUS_METHOD("OpenPTY", NULL
, "hs", bus_machine_method_open_pty
, SD_BUS_VTABLE_UNPRIVILEGED
),
1326 SD_BUS_METHOD("OpenLogin", NULL
, "hs", bus_machine_method_open_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1327 SD_BUS_METHOD("OpenShell", "ssasas", "hs", bus_machine_method_open_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1328 SD_BUS_METHOD("BindMount", "ssbb", NULL
, bus_machine_method_bind_mount
, SD_BUS_VTABLE_UNPRIVILEGED
),
1329 SD_BUS_METHOD("CopyFrom", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1330 SD_BUS_METHOD("CopyTo", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1331 SD_BUS_METHOD("OpenRootDirectory", NULL
, "h", bus_machine_method_open_root_directory
, SD_BUS_VTABLE_UNPRIVILEGED
),
1335 int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1336 Manager
*m
= userdata
;
1346 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1347 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1348 sd_bus_message
*message
;
1351 message
= sd_bus_get_current_message(bus
);
1355 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1359 r
= sd_bus_creds_get_pid(creds
, &pid
);
1363 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1367 _cleanup_free_
char *e
= NULL
;
1370 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1374 e
= bus_label_unescape(p
);
1378 machine
= hashmap_get(m
->machines
, e
);
1387 char *machine_bus_path(Machine
*m
) {
1388 _cleanup_free_
char *e
= NULL
;
1392 e
= bus_label_escape(m
->name
);
1396 return strappend("/org/freedesktop/machine1/machine/", e
);
1399 int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1400 _cleanup_strv_free_
char **l
= NULL
;
1401 Machine
*machine
= NULL
;
1402 Manager
*m
= userdata
;
1410 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1413 p
= machine_bus_path(machine
);
1417 r
= strv_consume(&l
, p
);
1422 *nodes
= TAKE_PTR(l
);
1427 int machine_send_signal(Machine
*m
, bool new_machine
) {
1428 _cleanup_free_
char *p
= NULL
;
1432 p
= machine_bus_path(m
);
1436 return sd_bus_emit_signal(
1438 "/org/freedesktop/machine1",
1439 "org.freedesktop.machine1.Manager",
1440 new_machine
? "MachineNew" : "MachineRemoved",
1444 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1445 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1446 _cleanup_free_
char *p
= NULL
;
1450 if (!m
->create_message
)
1453 c
= TAKE_PTR(m
->create_message
);
1456 return sd_bus_reply_method_error(c
, error
);
1458 /* Update the machine state file before we notify the client
1459 * about the result. */
1462 p
= machine_bus_path(m
);
1466 return sd_bus_reply_method_return(c
, "o", p
);