1 12eb29d8 2024-01-07 op /* $OpenBSD: imsg.c,v 1.23 2023/12/12 15:47:41 claudio Exp $ */
4 12eb29d8 2024-01-07 op * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
5 6b191ed5 2021-02-23 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7 6b191ed5 2021-02-23 op * Permission to use, copy, modify, and distribute this software for any
8 6b191ed5 2021-02-23 op * purpose with or without fee is hereby granted, provided that the above
9 6b191ed5 2021-02-23 op * copyright notice and this permission notice appear in all copies.
11 6b191ed5 2021-02-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 6b191ed5 2021-02-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 6b191ed5 2021-02-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 6b191ed5 2021-02-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 6b191ed5 2021-02-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 6b191ed5 2021-02-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 6b191ed5 2021-02-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 6b191ed5 2021-02-23 op #include "../config.h"
22 6b191ed5 2021-02-23 op #include <sys/types.h>
23 6b191ed5 2021-02-23 op #include <sys/socket.h>
24 6b191ed5 2021-02-23 op #include <sys/uio.h>
26 6b191ed5 2021-02-23 op #include <errno.h>
27 6b191ed5 2021-02-23 op #include <stdlib.h>
28 6b191ed5 2021-02-23 op #include <string.h>
29 6b191ed5 2021-02-23 op #include <unistd.h>
31 6b191ed5 2021-02-23 op #include "imsg.h"
33 12eb29d8 2024-01-07 op struct imsg_fd {
34 12eb29d8 2024-01-07 op TAILQ_ENTRY(imsg_fd) entry;
38 6b191ed5 2021-02-23 op int imsg_fd_overhead = 0;
40 12eb29d8 2024-01-07 op static int imsg_dequeue_fd(struct imsgbuf *);
43 12eb29d8 2024-01-07 op imsg_init(struct imsgbuf *imsgbuf, int fd)
45 12eb29d8 2024-01-07 op msgbuf_init(&imsgbuf->w);
46 12eb29d8 2024-01-07 op memset(&imsgbuf->r, 0, sizeof(imsgbuf->r));
47 12eb29d8 2024-01-07 op imsgbuf->fd = fd;
48 12eb29d8 2024-01-07 op imsgbuf->w.fd = fd;
49 12eb29d8 2024-01-07 op imsgbuf->pid = getpid();
50 12eb29d8 2024-01-07 op TAILQ_INIT(&imsgbuf->fds);
54 12eb29d8 2024-01-07 op imsg_read(struct imsgbuf *imsgbuf)
56 6b191ed5 2021-02-23 op struct msghdr msg;
57 6b191ed5 2021-02-23 op struct cmsghdr *cmsg;
59 6b191ed5 2021-02-23 op struct cmsghdr hdr;
60 6b191ed5 2021-02-23 op char buf[CMSG_SPACE(sizeof(int) * 1)];
62 6b191ed5 2021-02-23 op struct iovec iov;
63 6b191ed5 2021-02-23 op ssize_t n = -1;
65 6b191ed5 2021-02-23 op struct imsg_fd *ifd;
67 6b191ed5 2021-02-23 op memset(&msg, 0, sizeof(msg));
68 6b191ed5 2021-02-23 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
70 12eb29d8 2024-01-07 op iov.iov_base = imsgbuf->r.buf + imsgbuf->r.wpos;
71 12eb29d8 2024-01-07 op iov.iov_len = sizeof(imsgbuf->r.buf) - imsgbuf->r.wpos;
72 6b191ed5 2021-02-23 op msg.msg_iov = &iov;
73 6b191ed5 2021-02-23 op msg.msg_iovlen = 1;
74 6b191ed5 2021-02-23 op msg.msg_control = &cmsgbuf.buf;
75 6b191ed5 2021-02-23 op msg.msg_controllen = sizeof(cmsgbuf.buf);
77 6b191ed5 2021-02-23 op if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
81 6b191ed5 2021-02-23 op if (getdtablecount() + imsg_fd_overhead +
82 6b191ed5 2021-02-23 op (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
83 6b191ed5 2021-02-23 op >= getdtablesize()) {
84 6b191ed5 2021-02-23 op errno = EAGAIN;
89 12eb29d8 2024-01-07 op if ((n = recvmsg(imsgbuf->fd, &msg, 0)) == -1) {
90 6b191ed5 2021-02-23 op if (errno == EINTR)
95 12eb29d8 2024-01-07 op imsgbuf->r.wpos += n;
97 6b191ed5 2021-02-23 op for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
98 6b191ed5 2021-02-23 op cmsg = CMSG_NXTHDR(&msg, cmsg)) {
99 6b191ed5 2021-02-23 op if (cmsg->cmsg_level == SOL_SOCKET &&
100 6b191ed5 2021-02-23 op cmsg->cmsg_type == SCM_RIGHTS) {
105 6b191ed5 2021-02-23 op * We only accept one file descriptor. Due to C
106 6b191ed5 2021-02-23 op * padding rules, our control buffer might contain
107 6b191ed5 2021-02-23 op * more than one fd, and we must close them.
109 6b191ed5 2021-02-23 op j = ((char *)cmsg + cmsg->cmsg_len -
110 6b191ed5 2021-02-23 op (char *)CMSG_DATA(cmsg)) / sizeof(int);
111 6b191ed5 2021-02-23 op for (i = 0; i < j; i++) {
112 6b191ed5 2021-02-23 op fd = ((int *)CMSG_DATA(cmsg))[i];
113 6b191ed5 2021-02-23 op if (ifd != NULL) {
114 6b191ed5 2021-02-23 op ifd->fd = fd;
115 12eb29d8 2024-01-07 op TAILQ_INSERT_TAIL(&imsgbuf->fds, ifd,
122 6b191ed5 2021-02-23 op /* we do not handle other ctl data level */
131 12eb29d8 2024-01-07 op imsg_get(struct imsgbuf *imsgbuf, struct imsg *imsg)
133 12eb29d8 2024-01-07 op struct imsg m;
134 6b191ed5 2021-02-23 op size_t av, left, datalen;
136 12eb29d8 2024-01-07 op av = imsgbuf->r.wpos;
138 6b191ed5 2021-02-23 op if (IMSG_HEADER_SIZE > av)
141 12eb29d8 2024-01-07 op memcpy(&m.hdr, imsgbuf->r.buf, sizeof(m.hdr));
142 12eb29d8 2024-01-07 op if (m.hdr.len < IMSG_HEADER_SIZE ||
143 12eb29d8 2024-01-07 op m.hdr.len > MAX_IMSGSIZE) {
144 6b191ed5 2021-02-23 op errno = ERANGE;
147 12eb29d8 2024-01-07 op if (m.hdr.len > av)
151 12eb29d8 2024-01-07 op m.buf = NULL;
152 12eb29d8 2024-01-07 op m.data = NULL;
154 12eb29d8 2024-01-07 op datalen = m.hdr.len - IMSG_HEADER_SIZE;
155 12eb29d8 2024-01-07 op imsgbuf->r.rptr = imsgbuf->r.buf + IMSG_HEADER_SIZE;
156 12eb29d8 2024-01-07 op if (datalen != 0) {
157 12eb29d8 2024-01-07 op if ((m.buf = ibuf_open(datalen)) == NULL)
159 12eb29d8 2024-01-07 op if (ibuf_add(m.buf, imsgbuf->r.rptr, datalen) == -1) {
160 12eb29d8 2024-01-07 op /* this should never fail */
161 12eb29d8 2024-01-07 op ibuf_free(m.buf);
164 12eb29d8 2024-01-07 op m.data = ibuf_data(m.buf);
167 12eb29d8 2024-01-07 op if (m.hdr.flags & IMSGF_HASFD)
168 12eb29d8 2024-01-07 op m.fd = imsg_dequeue_fd(imsgbuf);
170 12eb29d8 2024-01-07 op if (m.hdr.len < av) {
171 12eb29d8 2024-01-07 op left = av - m.hdr.len;
172 12eb29d8 2024-01-07 op memmove(&imsgbuf->r.buf, imsgbuf->r.buf + m.hdr.len, left);
173 12eb29d8 2024-01-07 op imsgbuf->r.wpos = left;
175 12eb29d8 2024-01-07 op imsgbuf->r.wpos = 0;
178 6b191ed5 2021-02-23 op return (datalen + IMSG_HEADER_SIZE);
182 12eb29d8 2024-01-07 op imsg_get_ibuf(struct imsg *imsg, struct ibuf *ibuf)
184 12eb29d8 2024-01-07 op if (imsg->buf == NULL) {
185 12eb29d8 2024-01-07 op errno = EBADMSG;
188 12eb29d8 2024-01-07 op return ibuf_get_ibuf(imsg->buf, ibuf_size(imsg->buf), ibuf);
192 12eb29d8 2024-01-07 op imsg_get_data(struct imsg *imsg, void *data, size_t len)
194 12eb29d8 2024-01-07 op if (len == 0) {
195 12eb29d8 2024-01-07 op errno = EINVAL;
198 12eb29d8 2024-01-07 op if (imsg->buf == NULL || ibuf_size(imsg->buf) != len) {
199 12eb29d8 2024-01-07 op errno = EBADMSG;
202 12eb29d8 2024-01-07 op return ibuf_get(imsg->buf, data, len);
206 12eb29d8 2024-01-07 op imsg_get_fd(struct imsg *imsg)
208 12eb29d8 2024-01-07 op int fd = imsg->fd;
210 12eb29d8 2024-01-07 op imsg->fd = -1;
215 12eb29d8 2024-01-07 op imsg_get_id(struct imsg *imsg)
217 12eb29d8 2024-01-07 op return (imsg->hdr.peerid);
221 12eb29d8 2024-01-07 op imsg_get_len(struct imsg *imsg)
223 12eb29d8 2024-01-07 op if (imsg->buf == NULL)
225 12eb29d8 2024-01-07 op return ibuf_size(imsg->buf);
229 12eb29d8 2024-01-07 op imsg_get_pid(struct imsg *imsg)
231 12eb29d8 2024-01-07 op return (imsg->hdr.pid);
235 12eb29d8 2024-01-07 op imsg_get_type(struct imsg *imsg)
237 12eb29d8 2024-01-07 op return (imsg->hdr.type);
241 12eb29d8 2024-01-07 op imsg_compose(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
242 12eb29d8 2024-01-07 op int fd, const void *data, size_t datalen)
244 6b191ed5 2021-02-23 op struct ibuf *wbuf;
246 12eb29d8 2024-01-07 op if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
249 6b191ed5 2021-02-23 op if (imsg_add(wbuf, data, datalen) == -1)
252 c9c44c65 2023-07-02 op ibuf_fd_set(wbuf, fd);
253 12eb29d8 2024-01-07 op imsg_close(imsgbuf, wbuf);
259 12eb29d8 2024-01-07 op imsg_composev(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
260 6b191ed5 2021-02-23 op int fd, const struct iovec *iov, int iovcnt)
262 6b191ed5 2021-02-23 op struct ibuf *wbuf;
264 12eb29d8 2024-01-07 op size_t datalen = 0;
266 6b191ed5 2021-02-23 op for (i = 0; i < iovcnt; i++)
267 6b191ed5 2021-02-23 op datalen += iov[i].iov_len;
269 12eb29d8 2024-01-07 op if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
272 6b191ed5 2021-02-23 op for (i = 0; i < iovcnt; i++)
273 6b191ed5 2021-02-23 op if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
276 c9c44c65 2023-07-02 op ibuf_fd_set(wbuf, fd);
277 12eb29d8 2024-01-07 op imsg_close(imsgbuf, wbuf);
283 12eb29d8 2024-01-07 op * Enqueue imsg with payload from ibuf buf. fd passing is not possible
284 12eb29d8 2024-01-07 op * with this function.
287 12eb29d8 2024-01-07 op imsg_compose_ibuf(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id,
288 c9c44c65 2023-07-02 op pid_t pid, struct ibuf *buf)
290 12eb29d8 2024-01-07 op struct ibuf *hdrbuf = NULL;
291 c9c44c65 2023-07-02 op struct imsg_hdr hdr;
292 c9c44c65 2023-07-02 op int save_errno;
294 c9c44c65 2023-07-02 op if (ibuf_size(buf) + IMSG_HEADER_SIZE > MAX_IMSGSIZE) {
295 c9c44c65 2023-07-02 op errno = ERANGE;
299 c9c44c65 2023-07-02 op hdr.type = type;
300 c9c44c65 2023-07-02 op hdr.len = ibuf_size(buf) + IMSG_HEADER_SIZE;
301 c9c44c65 2023-07-02 op hdr.flags = 0;
302 12eb29d8 2024-01-07 op hdr.peerid = id;
303 c9c44c65 2023-07-02 op if ((hdr.pid = pid) == 0)
304 12eb29d8 2024-01-07 op hdr.pid = imsgbuf->pid;
306 12eb29d8 2024-01-07 op if ((hdrbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
308 12eb29d8 2024-01-07 op if (imsg_add(hdrbuf, &hdr, sizeof(hdr)) == -1)
311 12eb29d8 2024-01-07 op ibuf_close(&imsgbuf->w, hdrbuf);
312 12eb29d8 2024-01-07 op ibuf_close(&imsgbuf->w, buf);
316 c9c44c65 2023-07-02 op save_errno = errno;
317 c9c44c65 2023-07-02 op ibuf_free(buf);
318 12eb29d8 2024-01-07 op ibuf_free(hdrbuf);
319 c9c44c65 2023-07-02 op errno = save_errno;
324 12eb29d8 2024-01-07 op * Forward imsg to another channel. Any attached fd is closed.
327 12eb29d8 2024-01-07 op imsg_forward(struct imsgbuf *imsgbuf, struct imsg *msg)
329 12eb29d8 2024-01-07 op struct ibuf *wbuf;
330 12eb29d8 2024-01-07 op size_t len = 0;
332 12eb29d8 2024-01-07 op if (msg->fd != -1) {
333 12eb29d8 2024-01-07 op close(msg->fd);
334 12eb29d8 2024-01-07 op msg->fd = -1;
337 12eb29d8 2024-01-07 op if (msg->buf != NULL) {
338 12eb29d8 2024-01-07 op ibuf_rewind(msg->buf);
339 12eb29d8 2024-01-07 op len = ibuf_size(msg->buf);
342 12eb29d8 2024-01-07 op if ((wbuf = imsg_create(imsgbuf, msg->hdr.type, msg->hdr.peerid,
343 12eb29d8 2024-01-07 op msg->hdr.pid, len)) == NULL)
346 12eb29d8 2024-01-07 op if (msg->buf != NULL) {
347 12eb29d8 2024-01-07 op if (ibuf_add_buf(wbuf, msg->buf) == -1) {
348 12eb29d8 2024-01-07 op ibuf_free(wbuf);
353 12eb29d8 2024-01-07 op imsg_close(imsgbuf, wbuf);
357 6b191ed5 2021-02-23 op struct ibuf *
358 12eb29d8 2024-01-07 op imsg_create(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
359 12eb29d8 2024-01-07 op size_t datalen)
361 6b191ed5 2021-02-23 op struct ibuf *wbuf;
362 6b191ed5 2021-02-23 op struct imsg_hdr hdr;
364 6b191ed5 2021-02-23 op datalen += IMSG_HEADER_SIZE;
365 6b191ed5 2021-02-23 op if (datalen > MAX_IMSGSIZE) {
366 6b191ed5 2021-02-23 op errno = ERANGE;
367 6b191ed5 2021-02-23 op return (NULL);
370 6b191ed5 2021-02-23 op hdr.type = type;
371 6b191ed5 2021-02-23 op hdr.flags = 0;
372 12eb29d8 2024-01-07 op hdr.peerid = id;
373 6b191ed5 2021-02-23 op if ((hdr.pid = pid) == 0)
374 12eb29d8 2024-01-07 op hdr.pid = imsgbuf->pid;
375 6b191ed5 2021-02-23 op if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
376 6b191ed5 2021-02-23 op return (NULL);
378 6b191ed5 2021-02-23 op if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
379 6b191ed5 2021-02-23 op return (NULL);
381 6b191ed5 2021-02-23 op return (wbuf);
385 12eb29d8 2024-01-07 op imsg_add(struct ibuf *msg, const void *data, size_t datalen)
388 6b191ed5 2021-02-23 op if (ibuf_add(msg, data, datalen) == -1) {
389 6b191ed5 2021-02-23 op ibuf_free(msg);
392 6b191ed5 2021-02-23 op return (datalen);
396 12eb29d8 2024-01-07 op imsg_close(struct imsgbuf *imsgbuf, struct ibuf *msg)
398 6b191ed5 2021-02-23 op struct imsg_hdr *hdr;
400 6b191ed5 2021-02-23 op hdr = (struct imsg_hdr *)msg->buf;
402 6b191ed5 2021-02-23 op hdr->flags &= ~IMSGF_HASFD;
403 c9c44c65 2023-07-02 op if (ibuf_fd_avail(msg))
404 6b191ed5 2021-02-23 op hdr->flags |= IMSGF_HASFD;
405 c9c44c65 2023-07-02 op hdr->len = ibuf_size(msg);
407 12eb29d8 2024-01-07 op ibuf_close(&imsgbuf->w, msg);
411 6b191ed5 2021-02-23 op imsg_free(struct imsg *imsg)
413 12eb29d8 2024-01-07 op ibuf_free(imsg->buf);
417 12eb29d8 2024-01-07 op imsg_dequeue_fd(struct imsgbuf *imsgbuf)
420 6b191ed5 2021-02-23 op struct imsg_fd *ifd;
422 12eb29d8 2024-01-07 op if ((ifd = TAILQ_FIRST(&imsgbuf->fds)) == NULL)
425 6b191ed5 2021-02-23 op fd = ifd->fd;
426 12eb29d8 2024-01-07 op TAILQ_REMOVE(&imsgbuf->fds, ifd, entry);
433 12eb29d8 2024-01-07 op imsg_flush(struct imsgbuf *imsgbuf)
435 12eb29d8 2024-01-07 op while (imsgbuf->w.queued)
436 12eb29d8 2024-01-07 op if (msgbuf_write(&imsgbuf->w) <= 0)
442 12eb29d8 2024-01-07 op imsg_clear(struct imsgbuf *imsgbuf)
446 12eb29d8 2024-01-07 op msgbuf_clear(&imsgbuf->w);
447 12eb29d8 2024-01-07 op while ((fd = imsg_dequeue_fd(imsgbuf)) != -1)