]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7893c6b | 2 | |
a7893c6b | 3 | #include <fcntl.h> |
cf0fbc49 | 4 | #include <unistd.h> |
a7893c6b | 5 | |
3ffd4af2 | 6 | #include "bus-container.h" |
a7893c6b LP |
7 | #include "bus-internal.h" |
8 | #include "bus-socket.h" | |
3ffd4af2 | 9 | #include "fd-util.h" |
0cb8e3d1 | 10 | #include "namespace-util.h" |
3ffd4af2 | 11 | #include "process-util.h" |
165fee86 | 12 | #include "string-util.h" |
3ffd4af2 | 13 | #include "util.h" |
a7893c6b | 14 | |
bc9fd78c | 15 | int bus_container_connect_socket(sd_bus *b) { |
2b7d6d33 | 16 | _cleanup_close_pair_ int pair[2] = { -1, -1 }; |
671c3419 | 17 | _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1; |
2b7d6d33 | 18 | int r, error_buf = 0; |
2e87a1fd | 19 | pid_t child; |
2b7d6d33 | 20 | ssize_t n; |
a7893c6b LP |
21 | |
22 | assert(b); | |
23 | assert(b->input_fd < 0); | |
24 | assert(b->output_fd < 0); | |
ee502e0c | 25 | assert(b->nspid > 0 || b->machine); |
a7893c6b | 26 | |
ee502e0c | 27 | if (b->nspid <= 0) { |
165fee86 ZJS |
28 | log_debug("sd-bus: connecting bus%s%s to machine %s...", |
29 | b->description ? " " : "", strempty(b->description), b->machine); | |
30 | ||
ee502e0c LP |
31 | r = container_get_leader(b->machine, &b->nspid); |
32 | if (r < 0) | |
33 | return r; | |
165fee86 ZJS |
34 | } else |
35 | log_debug("sd-bus: connecting bus%s%s to namespace of PID "PID_FMT"...", | |
36 | b->description ? " " : "", strempty(b->description), b->nspid); | |
a7893c6b | 37 | |
671c3419 | 38 | r = namespace_open(b->nspid, &pidnsfd, &mntnsfd, NULL, &usernsfd, &rootfd); |
a7893c6b LP |
39 | if (r < 0) |
40 | return r; | |
a7893c6b LP |
41 | |
42 | b->input_fd = socket(b->sockaddr.sa.sa_family, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); | |
43 | if (b->input_fd < 0) | |
44 | return -errno; | |
45 | ||
7fe2903c LP |
46 | b->input_fd = fd_move_above_stdio(b->input_fd); |
47 | ||
a7893c6b LP |
48 | b->output_fd = b->input_fd; |
49 | ||
8f04d2eb | 50 | bus_socket_setup(b); |
a7893c6b | 51 | |
2b7d6d33 LP |
52 | if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, pair) < 0) |
53 | return -errno; | |
54 | ||
0a885dd0 LP |
55 | r = namespace_fork("(sd-buscntrns)", "(sd-buscntr)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG, |
56 | pidnsfd, mntnsfd, -1, usernsfd, rootfd, &child); | |
4c253ed1 LP |
57 | if (r < 0) |
58 | return r; | |
59 | if (r == 0) { | |
2b7d6d33 LP |
60 | pair[0] = safe_close(pair[0]); |
61 | ||
0a885dd0 LP |
62 | r = connect(b->input_fd, &b->sockaddr.sa, b->sockaddr_size); |
63 | if (r < 0) { | |
64 | /* Try to send error up */ | |
65 | error_buf = errno; | |
66 | (void) write(pair[1], &error_buf, sizeof(error_buf)); | |
2b7d6d33 | 67 | _exit(EXIT_FAILURE); |
a7893c6b LP |
68 | } |
69 | ||
0a885dd0 | 70 | _exit(EXIT_SUCCESS); |
a7893c6b LP |
71 | } |
72 | ||
2b7d6d33 LP |
73 | pair[1] = safe_close(pair[1]); |
74 | ||
0a885dd0 | 75 | r = wait_for_terminate_and_check("(sd-buscntrns)", child, 0); |
a7893c6b LP |
76 | if (r < 0) |
77 | return r; | |
2e87a1fd LP |
78 | if (r != EXIT_SUCCESS) |
79 | return -EPROTO; | |
a7893c6b | 80 | |
2b7d6d33 LP |
81 | n = read(pair[0], &error_buf, sizeof(error_buf)); |
82 | if (n < 0) | |
83 | return -errno; | |
84 | ||
85 | if (n > 0) { | |
86 | if (n != sizeof(error_buf)) | |
87 | return -EIO; | |
88 | ||
89 | if (error_buf < 0) | |
90 | return -EIO; | |
91 | ||
92 | if (error_buf == EINPROGRESS) | |
93 | return 1; | |
94 | ||
95 | if (error_buf > 0) | |
96 | return -error_buf; | |
97 | } | |
98 | ||
a7893c6b LP |
99 | return bus_socket_start_auth(b); |
100 | } |