/src/systemd/src/libsystemd/sd-bus/bus-container.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | | |
3 | | #include <fcntl.h> |
4 | | #include <unistd.h> |
5 | | |
6 | | #include "bus-container.h" |
7 | | #include "bus-internal.h" |
8 | | #include "bus-socket.h" |
9 | | #include "fd-util.h" |
10 | | #include "namespace-util.h" |
11 | | #include "process-util.h" |
12 | | #include "util.h" |
13 | | |
14 | 0 | int bus_container_connect_socket(sd_bus *b) { |
15 | 0 | _cleanup_close_pair_ int pair[2] = { -1, -1 }; |
16 | 0 | _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1; |
17 | 0 | int r, error_buf = 0; |
18 | 0 | pid_t child; |
19 | 0 | ssize_t n; |
20 | 0 |
|
21 | 0 | assert(b); |
22 | 0 | assert(b->input_fd < 0); |
23 | 0 | assert(b->output_fd < 0); |
24 | 0 | assert(b->nspid > 0 || b->machine); |
25 | 0 |
|
26 | 0 | if (b->nspid <= 0) { |
27 | 0 | r = container_get_leader(b->machine, &b->nspid); |
28 | 0 | if (r < 0) |
29 | 0 | return r; |
30 | 0 | } |
31 | 0 | |
32 | 0 | r = namespace_open(b->nspid, &pidnsfd, &mntnsfd, NULL, &usernsfd, &rootfd); |
33 | 0 | if (r < 0) |
34 | 0 | return r; |
35 | 0 | |
36 | 0 | b->input_fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); |
37 | 0 | if (b->input_fd < 0) |
38 | 0 | return -errno; |
39 | 0 | |
40 | 0 | b->input_fd = fd_move_above_stdio(b->input_fd); |
41 | 0 |
|
42 | 0 | b->output_fd = b->input_fd; |
43 | 0 |
|
44 | 0 | bus_socket_setup(b); |
45 | 0 |
|
46 | 0 | if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0) |
47 | 0 | return -errno; |
48 | 0 | |
49 | 0 | r = namespace_fork("(sd-buscntrns)", "(sd-buscntr)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG, |
50 | 0 | pidnsfd, mntnsfd, -1, usernsfd, rootfd, &child); |
51 | 0 | if (r < 0) |
52 | 0 | return r; |
53 | 0 | if (r == 0) { |
54 | 0 | pair[0] = safe_close(pair[0]); |
55 | 0 |
|
56 | 0 | r = connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size); |
57 | 0 | if (r < 0) { |
58 | 0 | /* Try to send error up */ |
59 | 0 | error_buf = errno; |
60 | 0 | (void) write(pair[1], &error_buf, sizeof(error_buf)); |
61 | 0 | _exit(EXIT_FAILURE); |
62 | 0 | } |
63 | 0 | |
64 | 0 | _exit(EXIT_SUCCESS); |
65 | 0 | } |
66 | 0 | |
67 | 0 | pair[1] = safe_close(pair[1]); |
68 | 0 |
|
69 | 0 | r = wait_for_terminate_and_check("(sd-buscntrns)", child, 0); |
70 | 0 | if (r < 0) |
71 | 0 | return r; |
72 | 0 | if (r != EXIT_SUCCESS) |
73 | 0 | return -EPROTO; |
74 | 0 | |
75 | 0 | n = read(pair[0], &error_buf, sizeof(error_buf)); |
76 | 0 | if (n < 0) |
77 | 0 | return -errno; |
78 | 0 | |
79 | 0 | if (n > 0) { |
80 | 0 | if (n != sizeof(error_buf)) |
81 | 0 | return -EIO; |
82 | 0 | |
83 | 0 | if (error_buf < 0) |
84 | 0 | return -EIO; |
85 | 0 | |
86 | 0 | if (error_buf == EINPROGRESS) |
87 | 0 | return 1; |
88 | 0 | |
89 | 0 | if (error_buf > 0) |
90 | 0 | return -error_buf; |
91 | 0 | } |
92 | 0 | |
93 | 0 | return bus_socket_start_auth(b); |
94 | 0 | } |