1 /* $OpenBSD: imsg.c,v 1.23 2023/12/12 15:47:41 claudio Exp $ */
4 * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
5 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
21 #include <sys/socket.h>
29 #include "got_compat.h"
33 TAILQ_ENTRY(imsg_fd) entry;
37 int imsg_fd_overhead = 0;
39 static int imsg_dequeue_fd(struct imsgbuf *);
42 imsg_init(struct imsgbuf *imsgbuf, int fd)
44 msgbuf_init(&imsgbuf->w);
45 memset(&imsgbuf->r, 0, sizeof(imsgbuf->r));
48 imsgbuf->pid = getpid();
49 TAILQ_INIT(&imsgbuf->fds);
53 imsg_read(struct imsgbuf *imsgbuf)
59 char buf[CMSG_SPACE(sizeof(int) * 1)];
66 memset(&msg, 0, sizeof(msg));
67 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
69 iov.iov_base = imsgbuf->r.buf + imsgbuf->r.wpos;
70 iov.iov_len = sizeof(imsgbuf->r.buf) - imsgbuf->r.wpos;
73 msg.msg_control = &cmsgbuf.buf;
74 msg.msg_controllen = sizeof(cmsgbuf.buf);
76 if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
80 if (getdtablecount() + imsg_fd_overhead +
81 (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
88 if ((n = recvmsg(imsgbuf->fd, &msg, 0)) == -1) {
96 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
97 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
98 if (cmsg->cmsg_level == SOL_SOCKET &&
99 cmsg->cmsg_type == SCM_RIGHTS) {
104 * We only accept one file descriptor. Due to C
105 * padding rules, our control buffer might contain
106 * more than one fd, and we must close them.
108 j = ((char *)cmsg + cmsg->cmsg_len -
109 (char *)CMSG_DATA(cmsg)) / sizeof(int);
110 for (i = 0; i < j; i++) {
111 fd = ((int *)CMSG_DATA(cmsg))[i];
114 TAILQ_INSERT_TAIL(&imsgbuf->fds, ifd,
121 /* we do not handle other ctl data level */
130 imsg_get(struct imsgbuf *imsgbuf, struct imsg *imsg)
133 size_t av, left, datalen;
135 av = imsgbuf->r.wpos;
137 if (IMSG_HEADER_SIZE > av)
140 memcpy(&m.hdr, imsgbuf->r.buf, sizeof(m.hdr));
141 if (m.hdr.len < IMSG_HEADER_SIZE ||
142 m.hdr.len > MAX_IMSGSIZE) {
153 datalen = m.hdr.len - IMSG_HEADER_SIZE;
154 imsgbuf->r.rptr = imsgbuf->r.buf + IMSG_HEADER_SIZE;
156 if ((m.buf = ibuf_open(datalen)) == NULL)
158 if (ibuf_add(m.buf, imsgbuf->r.rptr, datalen) == -1) {
159 /* this should never fail */
163 m.data = ibuf_data(m.buf);
166 if (m.hdr.flags & IMSGF_HASFD)
167 m.fd = imsg_dequeue_fd(imsgbuf);
169 if (m.hdr.len < av) {
170 left = av - m.hdr.len;
171 memmove(&imsgbuf->r.buf, imsgbuf->r.buf + m.hdr.len, left);
172 imsgbuf->r.wpos = left;
177 return (datalen + IMSG_HEADER_SIZE);
181 imsg_get_ibuf(struct imsg *imsg, struct ibuf *ibuf)
183 if (imsg->buf == NULL) {
187 return ibuf_get_ibuf(imsg->buf, ibuf_size(imsg->buf), ibuf);
191 imsg_get_data(struct imsg *imsg, void *data, size_t len)
197 if (imsg->buf == NULL || ibuf_size(imsg->buf) != len) {
201 return ibuf_get(imsg->buf, data, len);
205 imsg_get_fd(struct imsg *imsg)
214 imsg_get_id(struct imsg *imsg)
216 return (imsg->hdr.peerid);
220 imsg_get_len(struct imsg *imsg)
222 if (imsg->buf == NULL)
224 return ibuf_size(imsg->buf);
228 imsg_get_pid(struct imsg *imsg)
230 return (imsg->hdr.pid);
234 imsg_get_type(struct imsg *imsg)
236 return (imsg->hdr.type);
240 imsg_compose(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
241 int fd, const void *data, size_t datalen)
245 if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
248 if (imsg_add(wbuf, data, datalen) == -1)
251 ibuf_fd_set(wbuf, fd);
252 imsg_close(imsgbuf, wbuf);
258 imsg_composev(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
259 int fd, const struct iovec *iov, int iovcnt)
265 for (i = 0; i < iovcnt; i++)
266 datalen += iov[i].iov_len;
268 if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
271 for (i = 0; i < iovcnt; i++)
272 if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
275 ibuf_fd_set(wbuf, fd);
276 imsg_close(imsgbuf, wbuf);
282 * Enqueue imsg with payload from ibuf buf. fd passing is not possible
283 * with this function.
286 imsg_compose_ibuf(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id,
287 pid_t pid, struct ibuf *buf)
289 struct ibuf *hdrbuf = NULL;
293 if (ibuf_size(buf) + IMSG_HEADER_SIZE > MAX_IMSGSIZE) {
299 hdr.len = ibuf_size(buf) + IMSG_HEADER_SIZE;
302 if ((hdr.pid = pid) == 0)
303 hdr.pid = imsgbuf->pid;
305 if ((hdrbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
307 if (imsg_add(hdrbuf, &hdr, sizeof(hdr)) == -1)
310 ibuf_close(&imsgbuf->w, hdrbuf);
311 ibuf_close(&imsgbuf->w, buf);
323 * Forward imsg to another channel. Any attached fd is closed.
326 imsg_forward(struct imsgbuf *imsgbuf, struct imsg *msg)
336 if (msg->buf != NULL) {
337 ibuf_rewind(msg->buf);
338 len = ibuf_size(msg->buf);
341 if ((wbuf = imsg_create(imsgbuf, msg->hdr.type, msg->hdr.peerid,
342 msg->hdr.pid, len)) == NULL)
345 if (msg->buf != NULL) {
346 if (ibuf_add_buf(wbuf, msg->buf) == -1) {
352 imsg_close(imsgbuf, wbuf);
357 imsg_create(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
363 datalen += IMSG_HEADER_SIZE;
364 if (datalen > MAX_IMSGSIZE) {
372 if ((hdr.pid = pid) == 0)
373 hdr.pid = imsgbuf->pid;
374 if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
377 if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
384 imsg_add(struct ibuf *msg, const void *data, size_t datalen)
387 if (ibuf_add(msg, data, datalen) == -1) {
395 imsg_close(struct imsgbuf *imsgbuf, struct ibuf *msg)
397 struct imsg_hdr *hdr;
399 hdr = (struct imsg_hdr *)msg->buf;
401 hdr->flags &= ~IMSGF_HASFD;
402 if (ibuf_fd_avail(msg))
403 hdr->flags |= IMSGF_HASFD;
404 hdr->len = ibuf_size(msg);
406 ibuf_close(&imsgbuf->w, msg);
410 imsg_free(struct imsg *imsg)
412 ibuf_free(imsg->buf);
416 imsg_dequeue_fd(struct imsgbuf *imsgbuf)
421 if ((ifd = TAILQ_FIRST(&imsgbuf->fds)) == NULL)
425 TAILQ_REMOVE(&imsgbuf->fds, ifd, entry);
432 imsg_flush(struct imsgbuf *imsgbuf)
434 while (imsgbuf->w.queued)
435 if (msgbuf_write(&imsgbuf->w) <= 0)
441 imsg_clear(struct imsgbuf *imsgbuf)
445 msgbuf_clear(&imsgbuf->w);
446 while ((fd = imsg_dequeue_fd(imsgbuf)) != -1)