2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/mount.h>
24 /* When we include libgen.h because we need dirname() we immediately
25 * undefine basename() since libgen.h defines it as a macro to the POSIX
26 * version which is really broken. We prefer GNU basename(). */
30 #include "alloc-util.h"
31 #include "bus-common-errors.h"
32 #include "bus-internal.h"
33 #include "bus-label.h"
39 #include "formats-util.h"
41 #include "in-addr-util.h"
42 #include "local-addresses.h"
43 #include "machine-dbus.h"
46 #include "path-util.h"
47 #include "process-util.h"
49 #include "terminal-util.h"
50 #include "user-util.h"
52 static int property_get_id(
55 const char *interface
,
57 sd_bus_message
*reply
,
59 sd_bus_error
*error
) {
61 Machine
*m
= userdata
;
67 return sd_bus_message_append_array(reply
, 'y', &m
->id
, 16);
70 static int property_get_state(
73 const char *interface
,
75 sd_bus_message
*reply
,
77 sd_bus_error
*error
) {
79 Machine
*m
= userdata
;
87 state
= machine_state_to_string(machine_get_state(m
));
89 r
= sd_bus_message_append_basic(reply
, 's', state
);
96 static int property_get_netif(
99 const char *interface
,
100 const char *property
,
101 sd_bus_message
*reply
,
103 sd_bus_error
*error
) {
105 Machine
*m
= userdata
;
111 assert_cc(sizeof(int) == sizeof(int32_t));
113 return sd_bus_message_append_array(reply
, 'i', m
->netif
, m
->n_netif
* sizeof(int));
116 static BUS_DEFINE_PROPERTY_GET_ENUM(property_get_class
, machine_class
, MachineClass
);
118 int bus_machine_method_terminate(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
119 Machine
*m
= userdata
;
125 r
= bus_verify_polkit_async(
128 "org.freedesktop.machine1.manage-machines",
132 &m
->manager
->polkit_registry
,
137 return 1; /* Will call us back */
143 return sd_bus_reply_method_return(message
, NULL
);
146 int bus_machine_method_kill(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
147 Machine
*m
= userdata
;
156 r
= sd_bus_message_read(message
, "si", &swho
, &signo
);
163 who
= kill_who_from_string(swho
);
165 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid kill parameter '%s'", swho
);
168 if (signo
<= 0 || signo
>= _NSIG
)
169 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid signal %i", signo
);
171 r
= bus_verify_polkit_async(
174 "org.freedesktop.machine1.manage-machines",
178 &m
->manager
->polkit_registry
,
183 return 1; /* Will call us back */
185 r
= machine_kill(m
, who
, signo
);
189 return sd_bus_reply_method_return(message
, NULL
);
192 int bus_machine_method_get_addresses(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
193 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
194 Machine
*m
= userdata
;
200 r
= sd_bus_message_new_method_return(message
, &reply
);
204 r
= sd_bus_message_open_container(reply
, 'a', "(iay)");
211 _cleanup_free_
struct local_address
*addresses
= NULL
;
212 struct local_address
*a
;
215 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
219 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
221 r
= sd_bus_message_open_container(reply
, 'r', "iay");
225 r
= sd_bus_message_append(reply
, "i", addresses
[i
].family
);
229 r
= sd_bus_message_append_array(reply
, 'y', &addresses
[i
].address
, FAMILY_ADDRESS_SIZE(addresses
[i
].family
));
233 r
= sd_bus_message_close_container(reply
);
241 case MACHINE_CONTAINER
: {
242 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
243 _cleanup_free_
char *us
= NULL
, *them
= NULL
;
244 _cleanup_close_
int netns_fd
= -1;
249 r
= readlink_malloc("/proc/self/ns/net", &us
);
253 p
= procfs_file_alloca(m
->leader
, "ns/net");
254 r
= readlink_malloc(p
, &them
);
259 return sd_bus_error_setf(error
, BUS_ERROR_NO_PRIVATE_NETWORKING
, "Machine %s does not use private networking", m
->name
);
261 r
= namespace_open(m
->leader
, NULL
, NULL
, &netns_fd
, NULL
, NULL
);
265 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
270 return sd_bus_error_set_errnof(error
, errno
, "Failed to fork(): %m");
273 _cleanup_free_
struct local_address
*addresses
= NULL
;
274 struct local_address
*a
;
277 pair
[0] = safe_close(pair
[0]);
279 r
= namespace_enter(-1, -1, netns_fd
, -1, -1);
283 n
= local_addresses(NULL
, 0, AF_UNSPEC
, &addresses
);
287 for (a
= addresses
, i
= 0; i
< n
; a
++, i
++) {
288 struct iovec iov
[2] = {
289 { .iov_base
= &a
->family
, .iov_len
= sizeof(a
->family
) },
290 { .iov_base
= &a
->address
, .iov_len
= FAMILY_ADDRESS_SIZE(a
->family
) },
293 r
= writev(pair
[1], iov
, 2);
298 pair
[1] = safe_close(pair
[1]);
303 pair
[1] = safe_close(pair
[1]);
308 union in_addr_union in_addr
;
315 iov
[0] = (struct iovec
) { .iov_base
= &family
, .iov_len
= sizeof(family
) };
316 iov
[1] = (struct iovec
) { .iov_base
= &in_addr
, .iov_len
= sizeof(in_addr
) };
318 n
= recvmsg(pair
[0], &mh
, 0);
321 if ((size_t) n
< sizeof(family
))
324 r
= sd_bus_message_open_container(reply
, 'r', "iay");
328 r
= sd_bus_message_append(reply
, "i", family
);
335 if (n
!= sizeof(struct in_addr
) + sizeof(family
))
338 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in
, sizeof(in_addr
.in
));
342 if (n
!= sizeof(struct in6_addr
) + sizeof(family
))
345 r
= sd_bus_message_append_array(reply
, 'y', &in_addr
.in6
, sizeof(in_addr
.in6
));
351 r
= sd_bus_message_close_container(reply
);
356 r
= wait_for_terminate(child
, &si
);
358 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
359 if (si
.si_code
!= CLD_EXITED
|| si
.si_status
!= EXIT_SUCCESS
)
360 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
365 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting IP address data is only supported on container machines.");
368 r
= sd_bus_message_close_container(reply
);
372 return sd_bus_send(NULL
, reply
, NULL
);
375 int bus_machine_method_get_os_release(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
376 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
377 _cleanup_strv_free_
char **l
= NULL
;
378 Machine
*m
= userdata
;
388 r
= load_env_file_pairs(NULL
, "/etc/os-release", NULL
, &l
);
394 case MACHINE_CONTAINER
: {
395 _cleanup_close_
int mntns_fd
= -1, root_fd
= -1;
396 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
397 _cleanup_fclose_
FILE *f
= NULL
;
401 r
= namespace_open(m
->leader
, NULL
, &mntns_fd
, NULL
, NULL
, &root_fd
);
405 if (socketpair(AF_UNIX
, SOCK_SEQPACKET
, 0, pair
) < 0)
410 return sd_bus_error_set_errnof(error
, errno
, "Failed to fork(): %m");
413 _cleanup_close_
int fd
= -1;
415 pair
[0] = safe_close(pair
[0]);
417 r
= namespace_enter(-1, mntns_fd
, -1, -1, root_fd
);
421 fd
= open("/etc/os-release", O_RDONLY
|O_CLOEXEC
);
423 fd
= open("/usr/lib/os-release", O_RDONLY
|O_CLOEXEC
);
428 r
= copy_bytes(fd
, pair
[1], (uint64_t) -1, false);
435 pair
[1] = safe_close(pair
[1]);
437 f
= fdopen(pair
[0], "re");
443 r
= load_env_file_pairs(f
, "/etc/os-release", NULL
, &l
);
447 r
= wait_for_terminate(child
, &si
);
449 return sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
450 if (si
.si_code
!= CLD_EXITED
|| si
.si_status
!= EXIT_SUCCESS
)
451 return sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
457 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Requesting OS release data is only supported on container machines.");
460 r
= sd_bus_message_new_method_return(message
, &reply
);
464 r
= sd_bus_message_open_container(reply
, 'a', "{ss}");
468 STRV_FOREACH_PAIR(k
, v
, l
) {
469 r
= sd_bus_message_append(reply
, "{ss}", *k
, *v
);
474 r
= sd_bus_message_close_container(reply
);
478 return sd_bus_send(NULL
, reply
, NULL
);
481 int bus_machine_method_open_pty(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
482 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
483 _cleanup_free_
char *pty_name
= NULL
;
484 _cleanup_close_
int master
= -1;
485 Machine
*m
= userdata
;
491 r
= bus_verify_polkit_async(
494 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-open-pty" : "org.freedesktop.machine1.open-pty",
498 &m
->manager
->polkit_registry
,
503 return 1; /* Will call us back */
505 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
509 r
= ptsname_namespace(master
, &pty_name
);
513 r
= sd_bus_message_new_method_return(message
, &reply
);
517 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
521 return sd_bus_send(NULL
, reply
, NULL
);
524 static int container_bus_new(Machine
*m
, sd_bus_error
*error
, sd_bus
**ret
) {
536 case MACHINE_CONTAINER
: {
537 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
540 r
= sd_bus_new(&bus
);
544 if (asprintf(&address
, "x-machine-kernel:pid=%1$" PID_PRI
";x-machine-unix:pid=%1$" PID_PRI
, m
->leader
) < 0)
547 bus
->address
= address
;
548 bus
->bus_client
= true;
549 bus
->trusted
= false;
550 bus
->is_system
= true;
552 r
= sd_bus_start(bus
);
554 return sd_bus_error_set_errnof(error
, r
, "There is no system bus in container %s.", m
->name
);
570 int bus_machine_method_open_login(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
571 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
572 _cleanup_free_
char *pty_name
= NULL
;
573 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
574 _cleanup_close_
int master
= -1;
575 sd_bus
*container_bus
= NULL
;
576 Machine
*m
= userdata
;
577 const char *p
, *getty
;
583 r
= bus_verify_polkit_async(
586 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-login" : "org.freedesktop.machine1.login",
590 &m
->manager
->polkit_registry
,
595 return 1; /* Will call us back */
597 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
601 r
= ptsname_namespace(master
, &pty_name
);
605 p
= path_startswith(pty_name
, "/dev/pts/");
607 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "PTS name %s is invalid", pty_name
);
609 r
= container_bus_new(m
, error
, &allocated_bus
);
613 container_bus
= allocated_bus
?: m
->manager
->bus
;
615 getty
= strjoina("container-getty@", p
, ".service");
617 r
= sd_bus_call_method(
619 "org.freedesktop.systemd1",
620 "/org/freedesktop/systemd1",
621 "org.freedesktop.systemd1.Manager",
624 "ss", getty
, "replace");
628 r
= sd_bus_message_new_method_return(message
, &reply
);
632 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
636 return sd_bus_send(NULL
, reply
, NULL
);
639 int bus_machine_method_open_shell(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
640 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
, *tm
= NULL
;
641 _cleanup_free_
char *pty_name
= NULL
;
642 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*allocated_bus
= NULL
;
643 sd_bus
*container_bus
= NULL
;
644 _cleanup_close_
int master
= -1, slave
= -1;
645 _cleanup_strv_free_
char **env
= NULL
, **args
= NULL
;
646 Machine
*m
= userdata
;
647 const char *p
, *unit
, *user
, *path
, *description
, *utmp_id
;
653 r
= sd_bus_message_read(message
, "ss", &user
, &path
);
660 if (!path_is_absolute(path
))
661 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Specified path '%s' is not absolute", path
);
663 r
= sd_bus_message_read_strv(message
, &args
);
666 if (strv_isempty(args
)) {
667 args
= strv_free(args
);
669 args
= strv_new(path
, NULL
);
673 args
[0][0] = '-'; /* Tell /bin/sh that this shall be a login shell */
676 r
= sd_bus_message_read_strv(message
, &env
);
679 if (!strv_env_is_valid(env
))
680 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid environment assignments");
682 r
= bus_verify_polkit_async(
685 m
->class == MACHINE_HOST
? "org.freedesktop.machine1.host-shell" : "org.freedesktop.machine1.shell",
689 &m
->manager
->polkit_registry
,
694 return 1; /* Will call us back */
696 master
= machine_openpt(m
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
700 r
= ptsname_namespace(master
, &pty_name
);
704 p
= path_startswith(pty_name
, "/dev/pts/");
707 slave
= machine_open_terminal(m
, pty_name
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
);
711 utmp_id
= path_startswith(pty_name
, "/dev/");
714 r
= container_bus_new(m
, error
, &allocated_bus
);
718 container_bus
= allocated_bus
?: m
->manager
->bus
;
720 r
= sd_bus_message_new_method_call(
723 "org.freedesktop.systemd1",
724 "/org/freedesktop/systemd1",
725 "org.freedesktop.systemd1.Manager",
726 "StartTransientUnit");
731 unit
= strjoina("container-shell@", p
, ".service", NULL
);
732 r
= sd_bus_message_append(tm
, "ss", unit
, "fail");
737 r
= sd_bus_message_open_container(tm
, 'a', "(sv)");
741 description
= strjoina("Shell for User ", isempty(user
) ? "root" : user
);
742 r
= sd_bus_message_append(tm
,
743 "(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)(sv)",
744 "Description", "s", description
,
745 "StandardInputFileDescriptor", "h", slave
,
746 "StandardOutputFileDescriptor", "h", slave
,
747 "StandardErrorFileDescriptor", "h", slave
,
748 "SendSIGHUP", "b", true,
749 "IgnoreSIGPIPE", "b", false,
750 "KillMode", "s", "mixed",
751 "TTYReset", "b", true,
752 "UtmpIdentifier", "s", utmp_id
,
753 "UtmpMode", "s", "user",
754 "PAMName", "s", "login",
755 "WorkingDirectory", "s", "-~");
759 r
= sd_bus_message_append(tm
, "(sv)", "User", "s", isempty(user
) ? "root" : user
);
763 if (!strv_isempty(env
)) {
764 r
= sd_bus_message_open_container(tm
, 'r', "sv");
768 r
= sd_bus_message_append(tm
, "s", "Environment");
772 r
= sd_bus_message_open_container(tm
, 'v', "as");
776 r
= sd_bus_message_append_strv(tm
, env
);
780 r
= sd_bus_message_close_container(tm
);
784 r
= sd_bus_message_close_container(tm
);
790 r
= sd_bus_message_open_container(tm
, 'r', "sv");
794 r
= sd_bus_message_append(tm
, "s", "ExecStart");
798 r
= sd_bus_message_open_container(tm
, 'v', "a(sasb)");
802 r
= sd_bus_message_open_container(tm
, 'a', "(sasb)");
806 r
= sd_bus_message_open_container(tm
, 'r', "sasb");
810 r
= sd_bus_message_append(tm
, "s", path
);
814 r
= sd_bus_message_append_strv(tm
, args
);
818 r
= sd_bus_message_append(tm
, "b", true);
822 r
= sd_bus_message_close_container(tm
);
826 r
= sd_bus_message_close_container(tm
);
830 r
= sd_bus_message_close_container(tm
);
834 r
= sd_bus_message_close_container(tm
);
838 r
= sd_bus_message_close_container(tm
);
842 /* Auxiliary units */
843 r
= sd_bus_message_append(tm
, "a(sa(sv))", 0);
847 r
= sd_bus_call(container_bus
, tm
, 0, error
, NULL
);
851 slave
= safe_close(slave
);
853 r
= sd_bus_message_new_method_return(message
, &reply
);
857 r
= sd_bus_message_append(reply
, "hs", master
, pty_name
);
861 return sd_bus_send(NULL
, reply
, NULL
);
864 int bus_machine_method_bind_mount(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
865 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
866 char mount_slave
[] = "/tmp/propagate.XXXXXX", *mount_tmp
, *mount_outside
, *p
;
867 bool mount_slave_created
= false, mount_slave_mounted
= false,
868 mount_tmp_created
= false, mount_tmp_mounted
= false,
869 mount_outside_created
= false, mount_outside_mounted
= false;
870 const char *dest
, *src
;
871 Machine
*m
= userdata
;
872 int read_only
, make_directory
;
880 if (m
->class != MACHINE_CONTAINER
)
881 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Bind mounting is only supported on container machines.");
883 r
= sd_bus_message_read(message
, "ssbb", &src
, &dest
, &read_only
, &make_directory
);
887 if (!path_is_absolute(src
) || !path_is_safe(src
))
888 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute and not contain ../.");
892 else if (!path_is_absolute(dest
) || !path_is_safe(dest
))
893 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute and not contain ../.");
895 r
= bus_verify_polkit_async(
898 "org.freedesktop.machine1.manage-machines",
902 &m
->manager
->polkit_registry
,
907 return 1; /* Will call us back */
909 /* One day, when bind mounting /proc/self/fd/n works across
910 * namespace boundaries we should rework this logic to make
913 p
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/");
914 if (laccess(p
, F_OK
) < 0)
915 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Container does not allow propagation of mount points.");
917 /* Our goal is to install a new bind mount into the container,
918 possibly read-only. This is irritatingly complex
919 unfortunately, currently.
921 First, we start by creating a private playground in /tmp,
922 that we can mount MS_SLAVE. (Which is necessary, since
923 MS_MOVE cannot be applied to mounts with MS_SHARED parent
926 if (!mkdtemp(mount_slave
))
927 return sd_bus_error_set_errnof(error
, errno
, "Failed to create playground %s: %m", mount_slave
);
929 mount_slave_created
= true;
931 if (mount(mount_slave
, mount_slave
, NULL
, MS_BIND
, NULL
) < 0) {
932 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to make bind mount %s: %m", mount_slave
);
936 mount_slave_mounted
= true;
938 if (mount(NULL
, mount_slave
, NULL
, MS_SLAVE
, NULL
) < 0) {
939 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount slave %s: %m", mount_slave
);
943 /* Second, we mount the source directory to a directory inside
944 of our MS_SLAVE playground. */
945 mount_tmp
= strjoina(mount_slave
, "/mount");
946 if (mkdir(mount_tmp
, 0700) < 0) {
947 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to create temporary mount point %s: %m", mount_tmp
);
951 mount_tmp_created
= true;
953 if (mount(src
, mount_tmp
, NULL
, MS_BIND
, NULL
) < 0) {
954 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to overmount %s: %m", mount_tmp
);
958 mount_tmp_mounted
= true;
960 /* Third, we remount the new bind mount read-only if requested. */
962 if (mount(NULL
, mount_tmp
, NULL
, MS_BIND
|MS_REMOUNT
|MS_RDONLY
, NULL
) < 0) {
963 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to remount read-only %s: %m", mount_tmp
);
967 /* Fourth, we move the new bind mount into the propagation
968 * directory. This way it will appear there read-only
971 mount_outside
= strjoina("/run/systemd/nspawn/propagate/", m
->name
, "/XXXXXX");
972 if (!mkdtemp(mount_outside
)) {
973 r
= sd_bus_error_set_errnof(error
, errno
, "Cannot create propagation directory %s: %m", mount_outside
);
977 mount_outside_created
= true;
979 if (mount(mount_tmp
, mount_outside
, NULL
, MS_MOVE
, NULL
) < 0) {
980 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to move %s to %s: %m", mount_tmp
, mount_outside
);
984 mount_outside_mounted
= true;
985 mount_tmp_mounted
= false;
987 (void) rmdir(mount_tmp
);
988 mount_tmp_created
= false;
990 (void) umount(mount_slave
);
991 mount_slave_mounted
= false;
993 (void) rmdir(mount_slave
);
994 mount_slave_created
= false;
996 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0) {
997 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1003 r
= sd_bus_error_set_errnof(error
, errno
, "Failed to fork(): %m");
1008 const char *mount_inside
;
1012 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1014 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
1015 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
1017 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
1021 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
1022 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
1027 (void) mkdir_p(dest
, 0755);
1029 /* Fifth, move the mount to the right place inside */
1030 mount_inside
= strjoina("/run/systemd/nspawn/incoming/", basename(mount_outside
));
1031 if (mount(mount_inside
, dest
, NULL
, MS_MOVE
, NULL
) < 0) {
1032 r
= log_error_errno(errno
, "Failed to mount: %m");
1036 _exit(EXIT_SUCCESS
);
1039 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1040 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1042 _exit(EXIT_FAILURE
);
1045 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1047 r
= wait_for_terminate(child
, &si
);
1049 r
= sd_bus_error_set_errnof(error
, r
, "Failed to wait for child: %m");
1052 if (si
.si_code
!= CLD_EXITED
) {
1053 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1056 if (si
.si_status
!= EXIT_SUCCESS
) {
1058 if (read(errno_pipe_fd
[0], &r
, sizeof(r
)) == sizeof(r
))
1059 r
= sd_bus_error_set_errnof(error
, r
, "Failed to mount: %m");
1061 r
= sd_bus_error_setf(error
, SD_BUS_ERROR_FAILED
, "Child failed.");
1065 r
= sd_bus_reply_method_return(message
, NULL
);
1068 if (mount_outside_mounted
)
1069 umount(mount_outside
);
1070 if (mount_outside_created
)
1071 rmdir(mount_outside
);
1073 if (mount_tmp_mounted
)
1075 if (mount_tmp_created
)
1078 if (mount_slave_mounted
)
1079 umount(mount_slave
);
1080 if (mount_slave_created
)
1086 static int machine_operation_done(sd_event_source
*s
, const siginfo_t
*si
, void *userdata
) {
1087 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1088 MachineOperation
*o
= userdata
;
1096 if (si
->si_code
!= CLD_EXITED
) {
1097 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child died abnormally.");
1101 if (si
->si_status
!= EXIT_SUCCESS
) {
1102 if (read(o
->errno_fd
, &r
, sizeof(r
)) == sizeof(r
))
1103 r
= sd_bus_error_set_errnof(&error
, r
, "%m");
1105 r
= sd_bus_error_setf(&error
, SD_BUS_ERROR_FAILED
, "Child failed.");
1110 r
= sd_bus_reply_method_return(o
->message
, NULL
);
1112 log_error_errno(r
, "Failed to reply to message: %m");
1114 machine_operation_unref(o
);
1118 r
= sd_bus_reply_method_error(o
->message
, &error
);
1120 log_error_errno(r
, "Failed to reply to message: %m");
1122 machine_operation_unref(o
);
1126 int bus_machine_method_copy(sd_bus_message
*message
, void *userdata
, sd_bus_error
*error
) {
1127 const char *src
, *dest
, *host_path
, *container_path
, *host_basename
, *host_dirname
, *container_basename
, *container_dirname
;
1128 _cleanup_close_pair_
int errno_pipe_fd
[2] = { -1, -1 };
1129 _cleanup_close_
int hostfd
= -1;
1130 Machine
*m
= userdata
;
1131 MachineOperation
*o
;
1140 if (m
->n_operations
>= MACHINE_OPERATIONS_MAX
)
1141 return sd_bus_error_setf(error
, SD_BUS_ERROR_LIMITS_EXCEEDED
, "Too many ongoing copies.");
1143 if (m
->class != MACHINE_CONTAINER
)
1144 return sd_bus_error_setf(error
, SD_BUS_ERROR_NOT_SUPPORTED
, "Copying files is only supported on container machines.");
1146 r
= sd_bus_message_read(message
, "ss", &src
, &dest
);
1150 if (!path_is_absolute(src
))
1151 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Source path must be absolute.");
1155 else if (!path_is_absolute(dest
))
1156 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Destination path must be absolute.");
1158 r
= bus_verify_polkit_async(
1161 "org.freedesktop.machine1.manage-machines",
1165 &m
->manager
->polkit_registry
,
1170 return 1; /* Will call us back */
1172 copy_from
= strstr(sd_bus_message_get_member(message
), "CopyFrom");
1175 container_path
= src
;
1179 container_path
= dest
;
1182 host_basename
= basename(host_path
);
1183 t
= strdupa(host_path
);
1184 host_dirname
= dirname(t
);
1186 container_basename
= basename(container_path
);
1187 t
= strdupa(container_path
);
1188 container_dirname
= dirname(t
);
1190 hostfd
= open(host_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1192 return sd_bus_error_set_errnof(error
, errno
, "Failed to open host directory %s: %m", host_dirname
);
1194 if (pipe2(errno_pipe_fd
, O_CLOEXEC
|O_NONBLOCK
) < 0)
1195 return sd_bus_error_set_errnof(error
, errno
, "Failed to create pipe: %m");
1199 return sd_bus_error_set_errnof(error
, errno
, "Failed to fork(): %m");
1206 errno_pipe_fd
[0] = safe_close(errno_pipe_fd
[0]);
1208 q
= procfs_file_alloca(m
->leader
, "ns/mnt");
1209 mntfd
= open(q
, O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
1211 r
= log_error_errno(errno
, "Failed to open mount namespace of leader: %m");
1215 if (setns(mntfd
, CLONE_NEWNS
) < 0) {
1216 r
= log_error_errno(errno
, "Failed to join namespace of leader: %m");
1220 containerfd
= open(container_dirname
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_DIRECTORY
);
1221 if (containerfd
< 0) {
1222 r
= log_error_errno(errno
, "Failed top open destination directory: %m");
1227 r
= copy_tree_at(containerfd
, container_basename
, hostfd
, host_basename
, true);
1229 r
= copy_tree_at(hostfd
, host_basename
, containerfd
, container_basename
, true);
1231 hostfd
= safe_close(hostfd
);
1232 containerfd
= safe_close(containerfd
);
1235 r
= log_error_errno(r
, "Failed to copy tree: %m");
1239 _exit(EXIT_SUCCESS
);
1242 (void) write(errno_pipe_fd
[1], &r
, sizeof(r
));
1243 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1245 _exit(EXIT_FAILURE
);
1248 errno_pipe_fd
[1] = safe_close(errno_pipe_fd
[1]);
1250 /* Copying might take a while, hence install a watch the
1251 * child, and return */
1253 o
= new0(MachineOperation
, 1);
1258 o
->message
= sd_bus_message_ref(message
);
1259 o
->errno_fd
= errno_pipe_fd
[0];
1260 errno_pipe_fd
[0] = -1;
1262 r
= sd_event_add_child(m
->manager
->event
, &o
->event_source
, child
, WEXITED
, machine_operation_done
, o
);
1264 machine_operation_unref(o
);
1268 LIST_PREPEND(operations
, m
->operations
, o
);
1275 const sd_bus_vtable machine_vtable
[] = {
1276 SD_BUS_VTABLE_START(0),
1277 SD_BUS_PROPERTY("Name", "s", NULL
, offsetof(Machine
, name
), SD_BUS_VTABLE_PROPERTY_CONST
),
1278 SD_BUS_PROPERTY("Id", "ay", property_get_id
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1279 BUS_PROPERTY_DUAL_TIMESTAMP("Timestamp", offsetof(Machine
, timestamp
), SD_BUS_VTABLE_PROPERTY_CONST
),
1280 SD_BUS_PROPERTY("Service", "s", NULL
, offsetof(Machine
, service
), SD_BUS_VTABLE_PROPERTY_CONST
),
1281 SD_BUS_PROPERTY("Unit", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
),
1282 SD_BUS_PROPERTY("Scope", "s", NULL
, offsetof(Machine
, unit
), SD_BUS_VTABLE_PROPERTY_CONST
|SD_BUS_VTABLE_HIDDEN
),
1283 SD_BUS_PROPERTY("Leader", "u", NULL
, offsetof(Machine
, leader
), SD_BUS_VTABLE_PROPERTY_CONST
),
1284 SD_BUS_PROPERTY("Class", "s", property_get_class
, offsetof(Machine
, class), SD_BUS_VTABLE_PROPERTY_CONST
),
1285 SD_BUS_PROPERTY("RootDirectory", "s", NULL
, offsetof(Machine
, root_directory
), SD_BUS_VTABLE_PROPERTY_CONST
),
1286 SD_BUS_PROPERTY("NetworkInterfaces", "ai", property_get_netif
, 0, SD_BUS_VTABLE_PROPERTY_CONST
),
1287 SD_BUS_PROPERTY("State", "s", property_get_state
, 0, 0),
1288 SD_BUS_METHOD("Terminate", NULL
, NULL
, bus_machine_method_terminate
, SD_BUS_VTABLE_UNPRIVILEGED
),
1289 SD_BUS_METHOD("Kill", "si", NULL
, bus_machine_method_kill
, SD_BUS_VTABLE_UNPRIVILEGED
),
1290 SD_BUS_METHOD("GetAddresses", NULL
, "a(iay)", bus_machine_method_get_addresses
, SD_BUS_VTABLE_UNPRIVILEGED
),
1291 SD_BUS_METHOD("GetOSRelease", NULL
, "a{ss}", bus_machine_method_get_os_release
, SD_BUS_VTABLE_UNPRIVILEGED
),
1292 SD_BUS_METHOD("OpenPTY", NULL
, "hs", bus_machine_method_open_pty
, SD_BUS_VTABLE_UNPRIVILEGED
),
1293 SD_BUS_METHOD("OpenLogin", NULL
, "hs", bus_machine_method_open_login
, SD_BUS_VTABLE_UNPRIVILEGED
),
1294 SD_BUS_METHOD("OpenShell", "ssasas", "hs", bus_machine_method_open_shell
, SD_BUS_VTABLE_UNPRIVILEGED
),
1295 SD_BUS_METHOD("BindMount", "ssbb", NULL
, bus_machine_method_bind_mount
, SD_BUS_VTABLE_UNPRIVILEGED
),
1296 SD_BUS_METHOD("CopyFrom", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1297 SD_BUS_METHOD("CopyTo", "ss", NULL
, bus_machine_method_copy
, SD_BUS_VTABLE_UNPRIVILEGED
),
1301 int machine_object_find(sd_bus
*bus
, const char *path
, const char *interface
, void *userdata
, void **found
, sd_bus_error
*error
) {
1302 Manager
*m
= userdata
;
1312 if (streq(path
, "/org/freedesktop/machine1/machine/self")) {
1313 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
1314 sd_bus_message
*message
;
1317 message
= sd_bus_get_current_message(bus
);
1321 r
= sd_bus_query_sender_creds(message
, SD_BUS_CREDS_PID
, &creds
);
1325 r
= sd_bus_creds_get_pid(creds
, &pid
);
1329 r
= manager_get_machine_by_pid(m
, pid
, &machine
);
1333 _cleanup_free_
char *e
= NULL
;
1336 p
= startswith(path
, "/org/freedesktop/machine1/machine/");
1340 e
= bus_label_unescape(p
);
1344 machine
= hashmap_get(m
->machines
, e
);
1353 char *machine_bus_path(Machine
*m
) {
1354 _cleanup_free_
char *e
= NULL
;
1358 e
= bus_label_escape(m
->name
);
1362 return strappend("/org/freedesktop/machine1/machine/", e
);
1365 int machine_node_enumerator(sd_bus
*bus
, const char *path
, void *userdata
, char ***nodes
, sd_bus_error
*error
) {
1366 _cleanup_strv_free_
char **l
= NULL
;
1367 Machine
*machine
= NULL
;
1368 Manager
*m
= userdata
;
1376 HASHMAP_FOREACH(machine
, m
->machines
, i
) {
1379 p
= machine_bus_path(machine
);
1383 r
= strv_consume(&l
, p
);
1394 int machine_send_signal(Machine
*m
, bool new_machine
) {
1395 _cleanup_free_
char *p
= NULL
;
1399 p
= machine_bus_path(m
);
1403 return sd_bus_emit_signal(
1405 "/org/freedesktop/machine1",
1406 "org.freedesktop.machine1.Manager",
1407 new_machine
? "MachineNew" : "MachineRemoved",
1411 int machine_send_create_reply(Machine
*m
, sd_bus_error
*error
) {
1412 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*c
= NULL
;
1413 _cleanup_free_
char *p
= NULL
;
1417 if (!m
->create_message
)
1420 c
= m
->create_message
;
1421 m
->create_message
= NULL
;
1424 return sd_bus_reply_method_error(c
, error
);
1426 /* Update the machine state file before we notify the client
1427 * about the result. */
1430 p
= machine_bus_path(m
);
1434 return sd_bus_reply_method_return(c
, "o", p
);