Skip to content

Commit 80b0d3e

Browse files
committed
sd-bus: when connecting to a kdbus container bus pass error up
We rely on the correct error used when opening the kdbus device node, hence let's make sure we pass it up from the namespaced child process to the process which actually wants to connect.
1 parent a07c35c commit 80b0d3e

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

src/libsystemd/sd-bus/bus-container.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,22 @@ int bus_container_connect_kernel(sd_bus *b) {
125125
struct cmsghdr cmsghdr;
126126
uint8_t buf[CMSG_SPACE(sizeof(int))];
127127
} control = {};
128+
int error_buf = 0;
129+
struct iovec iov = {
130+
.iov_base = &error_buf,
131+
.iov_len = sizeof(error_buf),
132+
};
128133
struct msghdr mh = {
129134
.msg_control = &control,
130135
.msg_controllen = sizeof(control),
136+
.msg_iov = &iov,
137+
.msg_iovlen = 1,
131138
};
132139
struct cmsghdr *cmsg;
133140
pid_t child;
134141
siginfo_t si;
135-
int r;
136-
_cleanup_close_ int fd = -1;
142+
int r, fd = -1;
143+
ssize_t n;
137144

138145
assert(b);
139146
assert(b->input_fd < 0);
@@ -178,10 +185,13 @@ int bus_container_connect_kernel(sd_bus *b) {
178185
_exit(EXIT_FAILURE);
179186

180187
if (grandchild == 0) {
181-
182188
fd = open(b->kernel, O_RDWR|O_NOCTTY|O_CLOEXEC);
183-
if (fd < 0)
189+
if (fd < 0) {
190+
/* Try to send error up */
191+
error_buf = errno;
192+
(void) write(pair[1], &error_buf, sizeof(error_buf));
184193
_exit(EXIT_FAILURE);
194+
}
185195

186196
cmsg = CMSG_FIRSTHDR(&mh);
187197
cmsg->cmsg_level = SOL_SOCKET;
@@ -213,20 +223,17 @@ int bus_container_connect_kernel(sd_bus *b) {
213223
if (r < 0)
214224
return r;
215225

216-
if (si.si_code != CLD_EXITED)
217-
return -EIO;
218-
219-
if (si.si_status != EXIT_SUCCESS)
220-
return -EIO;
221-
222-
if (recvmsg(pair[0], &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC) < 0)
226+
n = recvmsg(pair[0], &mh, MSG_NOSIGNAL|MSG_CMSG_CLOEXEC);
227+
if (n < 0)
223228
return -errno;
224229

225-
CMSG_FOREACH(cmsg, &mh)
230+
CMSG_FOREACH(cmsg, &mh) {
226231
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
227232
int *fds;
228233
unsigned n_fds;
229234

235+
assert(fd < 0);
236+
230237
fds = (int*) CMSG_DATA(cmsg);
231238
n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
232239

@@ -237,9 +244,18 @@ int bus_container_connect_kernel(sd_bus *b) {
237244

238245
fd = fds[0];
239246
}
247+
}
248+
249+
/* If there's an fd passed, we are good. */
250+
if (fd >= 0) {
251+
b->input_fd = b->output_fd = fd;
252+
return bus_kernel_take_fd(b);
253+
}
240254

241-
b->input_fd = b->output_fd = fd;
242-
fd = -1;
255+
/* If there's an error passed, use it */
256+
if (n == sizeof(error_buf) && error_buf > 0)
257+
return -error_buf;
243258

244-
return bus_kernel_take_fd(b);
259+
/* Otherwise, we have no clue */
260+
return -EIO;
245261
}

0 commit comments

Comments
 (0)