| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "bus-container.h" |
| 6 |
#include "bus-internal.h" |
| 7 |
#include "bus-socket.h" |
| 8 |
#include "env-file.h" |
| 9 |
#include "errno-util.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "format-util.h" |
| 12 |
#include "hostname-util.h" |
| 13 |
#include "log.h" |
| 14 |
#include "namespace-util.h" |
| 15 |
#include "parse-util.h" |
| 16 |
#include "path-lookup.h" |
| 17 |
#include "path-util.h" |
| 18 |
#include "process-util.h" |
| 19 |
#include "string-util.h" |
| 20 |
|
| 21 |
int container_get_leader(RuntimeScope scope, const char *machine, pid_t *ret) { |
| 22 |
_cleanup_free_ char *p = NULL, *s = NULL, *class = NULL; |
| 23 |
pid_t leader; |
| 24 |
int r; |
| 25 |
|
| 26 |
assert(machine); |
| 27 |
assert(ret); |
| 28 |
|
| 29 |
if (streq(machine, ".host")) { |
| 30 |
if (scope == RUNTIME_SCOPE_USER) |
| 31 |
return -EHOSTDOWN; |
| 32 |
|
| 33 |
*ret = 1; |
| 34 |
return 0; |
| 35 |
} |
| 36 |
|
| 37 |
if (!hostname_is_valid(machine, 0)) |
| 38 |
return -EINVAL; |
| 39 |
|
| 40 |
r = runtime_directory_generic(scope, "systemd/machines", &p); |
| 41 |
if (r < 0) |
| 42 |
return r; |
| 43 |
|
| 44 |
if (!path_extend(&p, machine)) |
| 45 |
return -ENOMEM; |
| 46 |
|
| 47 |
r = parse_env_file(NULL, p, |
| 48 |
"LEADER", &s, |
| 49 |
"CLASS", &class); |
| 50 |
if (r == -ENOENT) |
| 51 |
return -EHOSTDOWN; |
| 52 |
if (r < 0) |
| 53 |
return r; |
| 54 |
if (!s) |
| 55 |
return -ESRCH; |
| 56 |
|
| 57 |
if (!streq_ptr(class, "container")) |
| 58 |
return -EMEDIUMTYPE; |
| 59 |
|
| 60 |
r = parse_pid(s, &leader); |
| 61 |
if (r < 0) |
| 62 |
return r; |
| 63 |
if (leader <= 1) |
| 64 |
return -EBADMSG; |
| 65 |
|
| 66 |
*ret = leader; |
| 67 |
return 0; |
| 68 |
} |
| 69 |
|
| 70 |
static int bus_container_connect_namespace(sd_bus *b, int pidnsfd, int mntnsfd, int usernsfd, int rootfd) { |
| 71 |
_cleanup_close_pair_ int errno_pipe_fd[2] = EBADF_PAIR; |
| 72 |
int r; |
| 73 |
|
| 74 |
if (pipe2(errno_pipe_fd, O_CLOEXEC) < 0) |
| 75 |
return log_debug_errno(errno, "Failed to create pipe: %m"); |
| 76 |
|
| 77 |
r = namespace_fork("(sd-buscntrns)", "(sd-buscntr)", FORK_RESET_SIGNALS|FORK_DEATHSIG_SIGKILL|FORK_WAIT, |
| 78 |
pidnsfd, mntnsfd, /* netns_fd= */ -EBADF, usernsfd, rootfd, /* ret= */ NULL); |
| 79 |
if (r == -EPROTO) { |
| 80 |
errno_pipe_fd[1] = safe_close(errno_pipe_fd[1]); |
| 81 |
|
| 82 |
int k = read_errno(errno_pipe_fd[0]); |
| 83 |
if (k < 0 && k != -EIO) |
| 84 |
return k; |
| 85 |
} |
| 86 |
if (r < 0) |
| 87 |
return log_debug_errno(r, "Failed to create namespace for (sd-buscntr): %m"); |
| 88 |
if (r == 0) { |
| 89 |
errno_pipe_fd[0] = safe_close(errno_pipe_fd[0]); |
| 90 |
|
| 91 |
r = RET_NERRNO(connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size)); |
| 92 |
report_errno_and_exit(errno_pipe_fd[1], r); |
| 93 |
} |
| 94 |
|
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
int bus_container_connect_socket(sd_bus *b) { |
| 99 |
_cleanup_close_ int pidnsfd = -EBADF, mntnsfd = -EBADF, usernsfd = -EBADF, rootfd = -EBADF; |
| 100 |
int r; |
| 101 |
|
| 102 |
assert(b); |
| 103 |
assert(b->input_fd < 0); |
| 104 |
assert(b->output_fd < 0); |
| 105 |
assert(b->nspid > 0 || b->machine); |
| 106 |
|
| 107 |
if (b->nspid <= 0) { |
| 108 |
log_debug("sd-bus: connecting bus%s%s to machine %s...", |
| 109 |
b->description ? " " : "", strempty(b->description), b->machine); |
| 110 |
|
| 111 |
r = container_get_leader(RUNTIME_SCOPE_USER, b->machine, &b->nspid); |
| 112 |
if (IN_SET(r, -EHOSTDOWN, -ENXIO)) |
| 113 |
r = container_get_leader(RUNTIME_SCOPE_SYSTEM, b->machine, &b->nspid); |
| 114 |
if (r < 0) |
| 115 |
return r; |
| 116 |
} else |
| 117 |
log_debug("sd-bus: connecting bus%s%s to namespace of PID "PID_FMT"...", |
| 118 |
b->description ? " " : "", strempty(b->description), b->nspid); |
| 119 |
|
| 120 |
r = namespace_open(b->nspid, &pidnsfd, &mntnsfd, /* ret_netns_fd= */ NULL, &usernsfd, &rootfd); |
| 121 |
if (r < 0) |
| 122 |
return log_debug_errno(r, "Failed to open namespace of PID "PID_FMT": %m", b->nspid); |
| 123 |
|
| 124 |
b->input_fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
| 125 |
if (b->input_fd < 0) |
| 126 |
return log_debug_errno(errno, "Failed to create a socket: %m"); |
| 127 |
|
| 128 |
b->input_fd = fd_move_above_stdio(b->input_fd); |
| 129 |
|
| 130 |
b->output_fd = b->input_fd; |
| 131 |
|
| 132 |
bus_socket_setup(b); |
| 133 |
|
| 134 |
r = are_our_namespaces(pidnsfd, mntnsfd, /* netns_fd= */ -EBADF, usernsfd, rootfd); |
| 135 |
if (r < 0) |
| 136 |
return log_debug_errno(r, "Failed to check if already in PID "PID_FMT" namespaces: %m", b->nspid); |
| 137 |
if (r > 0) |
| 138 |
r = RET_NERRNO(connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size)); |
| 139 |
else |
| 140 |
r = bus_container_connect_namespace(b, pidnsfd, mntnsfd, usernsfd, rootfd); |
| 141 |
if (r == -EINPROGRESS) |
| 142 |
return 1; |
| 143 |
if (r < 0) |
| 144 |
return log_debug_errno(r, "Failed to connect to D-Bus socket in namespaces of PID "PID_FMT": %m", b->nspid); |
| 145 |
|
| 146 |
return bus_socket_start_auth(b); |
| 147 |
} |