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