1 3bb9eb8b 2024-01-18 thomas /* $OpenBSD: imsg.c,v 1.23 2023/12/12 15:47:41 claudio Exp $ */
4 3bb9eb8b 2024-01-18 thomas * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
5 dd038bc6 2021-09-21 thomas.ad * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
7 dd038bc6 2021-09-21 thomas.ad * Permission to use, copy, modify, and distribute this software for any
8 dd038bc6 2021-09-21 thomas.ad * purpose with or without fee is hereby granted, provided that the above
9 dd038bc6 2021-09-21 thomas.ad * copyright notice and this permission notice appear in all copies.
11 dd038bc6 2021-09-21 thomas.ad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 dd038bc6 2021-09-21 thomas.ad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 dd038bc6 2021-09-21 thomas.ad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 dd038bc6 2021-09-21 thomas.ad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 dd038bc6 2021-09-21 thomas.ad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 dd038bc6 2021-09-21 thomas.ad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 dd038bc6 2021-09-21 thomas.ad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 dd038bc6 2021-09-21 thomas.ad #include <sys/types.h>
21 dd038bc6 2021-09-21 thomas.ad #include <sys/socket.h>
22 dd038bc6 2021-09-21 thomas.ad #include <sys/uio.h>
24 dd038bc6 2021-09-21 thomas.ad #include <errno.h>
25 dd038bc6 2021-09-21 thomas.ad #include <stdlib.h>
26 dd038bc6 2021-09-21 thomas.ad #include <string.h>
27 dd038bc6 2021-09-21 thomas.ad #include <unistd.h>
29 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
30 dd038bc6 2021-09-21 thomas.ad #include "imsg.h"
32 3bb9eb8b 2024-01-18 thomas struct imsg_fd {
33 3bb9eb8b 2024-01-18 thomas TAILQ_ENTRY(imsg_fd) entry;
37 dd038bc6 2021-09-21 thomas.ad int imsg_fd_overhead = 0;
39 b69b73ef 2023-11-25 thomas static int imsg_dequeue_fd(struct imsgbuf *);
42 b69b73ef 2023-11-25 thomas imsg_init(struct imsgbuf *imsgbuf, int fd)
44 b69b73ef 2023-11-25 thomas msgbuf_init(&imsgbuf->w);
45 b69b73ef 2023-11-25 thomas memset(&imsgbuf->r, 0, sizeof(imsgbuf->r));
46 b69b73ef 2023-11-25 thomas imsgbuf->fd = fd;
47 b69b73ef 2023-11-25 thomas imsgbuf->w.fd = fd;
48 b69b73ef 2023-11-25 thomas imsgbuf->pid = getpid();
49 b69b73ef 2023-11-25 thomas TAILQ_INIT(&imsgbuf->fds);
52 dd038bc6 2021-09-21 thomas.ad ssize_t
53 b69b73ef 2023-11-25 thomas imsg_read(struct imsgbuf *imsgbuf)
55 dd038bc6 2021-09-21 thomas.ad struct msghdr msg;
56 dd038bc6 2021-09-21 thomas.ad struct cmsghdr *cmsg;
57 dd038bc6 2021-09-21 thomas.ad union {
58 dd038bc6 2021-09-21 thomas.ad struct cmsghdr hdr;
59 dd038bc6 2021-09-21 thomas.ad char buf[CMSG_SPACE(sizeof(int) * 1)];
60 dd038bc6 2021-09-21 thomas.ad } cmsgbuf;
61 dd038bc6 2021-09-21 thomas.ad struct iovec iov;
62 dd038bc6 2021-09-21 thomas.ad ssize_t n = -1;
63 dd038bc6 2021-09-21 thomas.ad int fd;
64 dd038bc6 2021-09-21 thomas.ad struct imsg_fd *ifd;
66 dd038bc6 2021-09-21 thomas.ad memset(&msg, 0, sizeof(msg));
67 dd038bc6 2021-09-21 thomas.ad memset(&cmsgbuf, 0, sizeof(cmsgbuf));
69 b69b73ef 2023-11-25 thomas iov.iov_base = imsgbuf->r.buf + imsgbuf->r.wpos;
70 b69b73ef 2023-11-25 thomas iov.iov_len = sizeof(imsgbuf->r.buf) - imsgbuf->r.wpos;
71 dd038bc6 2021-09-21 thomas.ad msg.msg_iov = &iov;
72 dd038bc6 2021-09-21 thomas.ad msg.msg_iovlen = 1;
73 dd038bc6 2021-09-21 thomas.ad msg.msg_control = &cmsgbuf.buf;
74 dd038bc6 2021-09-21 thomas.ad msg.msg_controllen = sizeof(cmsgbuf.buf);
76 dd038bc6 2021-09-21 thomas.ad if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
77 dd038bc6 2021-09-21 thomas.ad return (-1);
80 dd038bc6 2021-09-21 thomas.ad if (getdtablecount() + imsg_fd_overhead +
81 dd038bc6 2021-09-21 thomas.ad (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
82 dd038bc6 2021-09-21 thomas.ad >= getdtablesize()) {
83 dd038bc6 2021-09-21 thomas.ad errno = EAGAIN;
84 dd038bc6 2021-09-21 thomas.ad free(ifd);
85 dd038bc6 2021-09-21 thomas.ad return (-1);
88 b69b73ef 2023-11-25 thomas if ((n = recvmsg(imsgbuf->fd, &msg, 0)) == -1) {
89 dd038bc6 2021-09-21 thomas.ad if (errno == EINTR)
90 dd038bc6 2021-09-21 thomas.ad goto again;
91 dd038bc6 2021-09-21 thomas.ad goto fail;
94 b69b73ef 2023-11-25 thomas imsgbuf->r.wpos += n;
96 dd038bc6 2021-09-21 thomas.ad for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
97 dd038bc6 2021-09-21 thomas.ad cmsg = CMSG_NXTHDR(&msg, cmsg)) {
98 dd038bc6 2021-09-21 thomas.ad if (cmsg->cmsg_level == SOL_SOCKET &&
99 dd038bc6 2021-09-21 thomas.ad cmsg->cmsg_type == SCM_RIGHTS) {
100 dd038bc6 2021-09-21 thomas.ad int i;
101 dd038bc6 2021-09-21 thomas.ad int j;
104 dd038bc6 2021-09-21 thomas.ad * We only accept one file descriptor. Due to C
105 dd038bc6 2021-09-21 thomas.ad * padding rules, our control buffer might contain
106 dd038bc6 2021-09-21 thomas.ad * more than one fd, and we must close them.
108 dd038bc6 2021-09-21 thomas.ad j = ((char *)cmsg + cmsg->cmsg_len -
109 dd038bc6 2021-09-21 thomas.ad (char *)CMSG_DATA(cmsg)) / sizeof(int);
110 dd038bc6 2021-09-21 thomas.ad for (i = 0; i < j; i++) {
111 dd038bc6 2021-09-21 thomas.ad fd = ((int *)CMSG_DATA(cmsg))[i];
112 dd038bc6 2021-09-21 thomas.ad if (ifd != NULL) {
113 dd038bc6 2021-09-21 thomas.ad ifd->fd = fd;
114 b69b73ef 2023-11-25 thomas TAILQ_INSERT_TAIL(&imsgbuf->fds, ifd,
115 dd038bc6 2021-09-21 thomas.ad entry);
116 dd038bc6 2021-09-21 thomas.ad ifd = NULL;
117 dd038bc6 2021-09-21 thomas.ad } else
118 dd038bc6 2021-09-21 thomas.ad close(fd);
121 dd038bc6 2021-09-21 thomas.ad /* we do not handle other ctl data level */
125 dd038bc6 2021-09-21 thomas.ad free(ifd);
126 dd038bc6 2021-09-21 thomas.ad return (n);
129 dd038bc6 2021-09-21 thomas.ad ssize_t
130 b69b73ef 2023-11-25 thomas imsg_get(struct imsgbuf *imsgbuf, struct imsg *imsg)
132 3bb9eb8b 2024-01-18 thomas struct imsg m;
133 dd038bc6 2021-09-21 thomas.ad size_t av, left, datalen;
135 b69b73ef 2023-11-25 thomas av = imsgbuf->r.wpos;
137 dd038bc6 2021-09-21 thomas.ad if (IMSG_HEADER_SIZE > av)
138 dd038bc6 2021-09-21 thomas.ad return (0);
140 3bb9eb8b 2024-01-18 thomas memcpy(&m.hdr, imsgbuf->r.buf, sizeof(m.hdr));
141 3bb9eb8b 2024-01-18 thomas if (m.hdr.len < IMSG_HEADER_SIZE ||
142 3bb9eb8b 2024-01-18 thomas m.hdr.len > MAX_IMSGSIZE) {
143 dd038bc6 2021-09-21 thomas.ad errno = ERANGE;
144 dd038bc6 2021-09-21 thomas.ad return (-1);
146 3bb9eb8b 2024-01-18 thomas if (m.hdr.len > av)
147 dd038bc6 2021-09-21 thomas.ad return (0);
149 3bb9eb8b 2024-01-18 thomas m.fd = -1;
150 3bb9eb8b 2024-01-18 thomas m.buf = NULL;
151 3bb9eb8b 2024-01-18 thomas m.data = NULL;
153 3bb9eb8b 2024-01-18 thomas datalen = m.hdr.len - IMSG_HEADER_SIZE;
154 3bb9eb8b 2024-01-18 thomas imsgbuf->r.rptr = imsgbuf->r.buf + IMSG_HEADER_SIZE;
155 3bb9eb8b 2024-01-18 thomas if (datalen != 0) {
156 3bb9eb8b 2024-01-18 thomas if ((m.buf = ibuf_open(datalen)) == NULL)
157 3bb9eb8b 2024-01-18 thomas return (-1);
158 3bb9eb8b 2024-01-18 thomas if (ibuf_add(m.buf, imsgbuf->r.rptr, datalen) == -1) {
159 3bb9eb8b 2024-01-18 thomas /* this should never fail */
160 3bb9eb8b 2024-01-18 thomas ibuf_free(m.buf);
161 3bb9eb8b 2024-01-18 thomas return (-1);
163 3bb9eb8b 2024-01-18 thomas m.data = ibuf_data(m.buf);
166 3bb9eb8b 2024-01-18 thomas if (m.hdr.flags & IMSGF_HASFD)
167 3bb9eb8b 2024-01-18 thomas m.fd = imsg_dequeue_fd(imsgbuf);
169 3bb9eb8b 2024-01-18 thomas if (m.hdr.len < av) {
170 3bb9eb8b 2024-01-18 thomas left = av - m.hdr.len;
171 3bb9eb8b 2024-01-18 thomas memmove(&imsgbuf->r.buf, imsgbuf->r.buf + m.hdr.len, left);
172 b69b73ef 2023-11-25 thomas imsgbuf->r.wpos = left;
173 dd038bc6 2021-09-21 thomas.ad } else
174 b69b73ef 2023-11-25 thomas imsgbuf->r.wpos = 0;
176 3bb9eb8b 2024-01-18 thomas *imsg = m;
177 dd038bc6 2021-09-21 thomas.ad return (datalen + IMSG_HEADER_SIZE);
181 3bb9eb8b 2024-01-18 thomas imsg_get_ibuf(struct imsg *imsg, struct ibuf *ibuf)
183 3bb9eb8b 2024-01-18 thomas if (imsg->buf == NULL) {
184 3bb9eb8b 2024-01-18 thomas errno = EBADMSG;
185 3bb9eb8b 2024-01-18 thomas return (-1);
187 3bb9eb8b 2024-01-18 thomas return ibuf_get_ibuf(imsg->buf, ibuf_size(imsg->buf), ibuf);
191 3bb9eb8b 2024-01-18 thomas imsg_get_data(struct imsg *imsg, void *data, size_t len)
193 3bb9eb8b 2024-01-18 thomas if (len == 0) {
194 3bb9eb8b 2024-01-18 thomas errno = EINVAL;
195 3bb9eb8b 2024-01-18 thomas return (-1);
197 3bb9eb8b 2024-01-18 thomas if (imsg->buf == NULL || ibuf_size(imsg->buf) != len) {
198 3bb9eb8b 2024-01-18 thomas errno = EBADMSG;
199 3bb9eb8b 2024-01-18 thomas return (-1);
201 3bb9eb8b 2024-01-18 thomas return ibuf_get(imsg->buf, data, len);
205 3bb9eb8b 2024-01-18 thomas imsg_get_fd(struct imsg *imsg)
207 3bb9eb8b 2024-01-18 thomas int fd = imsg->fd;
209 3bb9eb8b 2024-01-18 thomas imsg->fd = -1;
210 3bb9eb8b 2024-01-18 thomas return fd;
214 3bb9eb8b 2024-01-18 thomas imsg_get_id(struct imsg *imsg)
216 3bb9eb8b 2024-01-18 thomas return (imsg->hdr.peerid);
220 3bb9eb8b 2024-01-18 thomas imsg_get_len(struct imsg *imsg)
222 3bb9eb8b 2024-01-18 thomas if (imsg->buf == NULL)
223 3bb9eb8b 2024-01-18 thomas return 0;
224 3bb9eb8b 2024-01-18 thomas return ibuf_size(imsg->buf);
228 3bb9eb8b 2024-01-18 thomas imsg_get_pid(struct imsg *imsg)
230 3bb9eb8b 2024-01-18 thomas return (imsg->hdr.pid);
234 3bb9eb8b 2024-01-18 thomas imsg_get_type(struct imsg *imsg)
236 3bb9eb8b 2024-01-18 thomas return (imsg->hdr.type);
240 b69b73ef 2023-11-25 thomas imsg_compose(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
241 3bb9eb8b 2024-01-18 thomas int fd, const void *data, size_t datalen)
243 dd038bc6 2021-09-21 thomas.ad struct ibuf *wbuf;
245 b69b73ef 2023-11-25 thomas if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
246 dd038bc6 2021-09-21 thomas.ad return (-1);
248 dd038bc6 2021-09-21 thomas.ad if (imsg_add(wbuf, data, datalen) == -1)
249 dd038bc6 2021-09-21 thomas.ad return (-1);
251 3ef3f36a 2023-07-05 op ibuf_fd_set(wbuf, fd);
252 b69b73ef 2023-11-25 thomas imsg_close(imsgbuf, wbuf);
254 dd038bc6 2021-09-21 thomas.ad return (1);
258 b69b73ef 2023-11-25 thomas imsg_composev(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
259 dd038bc6 2021-09-21 thomas.ad int fd, const struct iovec *iov, int iovcnt)
261 dd038bc6 2021-09-21 thomas.ad struct ibuf *wbuf;
263 3bb9eb8b 2024-01-18 thomas size_t datalen = 0;
265 dd038bc6 2021-09-21 thomas.ad for (i = 0; i < iovcnt; i++)
266 dd038bc6 2021-09-21 thomas.ad datalen += iov[i].iov_len;
268 b69b73ef 2023-11-25 thomas if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
269 dd038bc6 2021-09-21 thomas.ad return (-1);
271 dd038bc6 2021-09-21 thomas.ad for (i = 0; i < iovcnt; i++)
272 dd038bc6 2021-09-21 thomas.ad if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
273 dd038bc6 2021-09-21 thomas.ad return (-1);
275 3ef3f36a 2023-07-05 op ibuf_fd_set(wbuf, fd);
276 b69b73ef 2023-11-25 thomas imsg_close(imsgbuf, wbuf);
278 dd038bc6 2021-09-21 thomas.ad return (1);
282 3bb9eb8b 2024-01-18 thomas * Enqueue imsg with payload from ibuf buf. fd passing is not possible
283 3bb9eb8b 2024-01-18 thomas * with this function.
286 b69b73ef 2023-11-25 thomas imsg_compose_ibuf(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id,
287 3ef3f36a 2023-07-05 op pid_t pid, struct ibuf *buf)
289 3bb9eb8b 2024-01-18 thomas struct ibuf *hdrbuf = NULL;
290 3ef3f36a 2023-07-05 op struct imsg_hdr hdr;
291 3ef3f36a 2023-07-05 op int save_errno;
293 3ef3f36a 2023-07-05 op if (ibuf_size(buf) + IMSG_HEADER_SIZE > MAX_IMSGSIZE) {
294 3ef3f36a 2023-07-05 op errno = ERANGE;
298 3ef3f36a 2023-07-05 op hdr.type = type;
299 3ef3f36a 2023-07-05 op hdr.len = ibuf_size(buf) + IMSG_HEADER_SIZE;
300 3ef3f36a 2023-07-05 op hdr.flags = 0;
301 b69b73ef 2023-11-25 thomas hdr.peerid = id;
302 3ef3f36a 2023-07-05 op if ((hdr.pid = pid) == 0)
303 b69b73ef 2023-11-25 thomas hdr.pid = imsgbuf->pid;
305 3bb9eb8b 2024-01-18 thomas if ((hdrbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
307 3bb9eb8b 2024-01-18 thomas if (imsg_add(hdrbuf, &hdr, sizeof(hdr)) == -1)
310 3bb9eb8b 2024-01-18 thomas ibuf_close(&imsgbuf->w, hdrbuf);
311 b69b73ef 2023-11-25 thomas ibuf_close(&imsgbuf->w, buf);
315 3ef3f36a 2023-07-05 op save_errno = errno;
316 3ef3f36a 2023-07-05 op ibuf_free(buf);
317 3bb9eb8b 2024-01-18 thomas ibuf_free(hdrbuf);
318 3ef3f36a 2023-07-05 op errno = save_errno;
323 3bb9eb8b 2024-01-18 thomas * Forward imsg to another channel. Any attached fd is closed.
326 3bb9eb8b 2024-01-18 thomas imsg_forward(struct imsgbuf *imsgbuf, struct imsg *msg)
328 3bb9eb8b 2024-01-18 thomas struct ibuf *wbuf;
329 3bb9eb8b 2024-01-18 thomas size_t len = 0;
331 3bb9eb8b 2024-01-18 thomas if (msg->fd != -1) {
332 3bb9eb8b 2024-01-18 thomas close(msg->fd);
333 3bb9eb8b 2024-01-18 thomas msg->fd = -1;
336 3bb9eb8b 2024-01-18 thomas if (msg->buf != NULL) {
337 3bb9eb8b 2024-01-18 thomas ibuf_rewind(msg->buf);
338 3bb9eb8b 2024-01-18 thomas len = ibuf_size(msg->buf);
341 3bb9eb8b 2024-01-18 thomas if ((wbuf = imsg_create(imsgbuf, msg->hdr.type, msg->hdr.peerid,
342 3bb9eb8b 2024-01-18 thomas msg->hdr.pid, len)) == NULL)
343 3bb9eb8b 2024-01-18 thomas return (-1);
345 3bb9eb8b 2024-01-18 thomas if (msg->buf != NULL) {
346 3bb9eb8b 2024-01-18 thomas if (ibuf_add_buf(wbuf, msg->buf) == -1) {
347 3bb9eb8b 2024-01-18 thomas ibuf_free(wbuf);
348 3bb9eb8b 2024-01-18 thomas return (-1);
352 3bb9eb8b 2024-01-18 thomas imsg_close(imsgbuf, wbuf);
353 3bb9eb8b 2024-01-18 thomas return (1);
356 dd038bc6 2021-09-21 thomas.ad struct ibuf *
357 b69b73ef 2023-11-25 thomas imsg_create(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
358 3bb9eb8b 2024-01-18 thomas size_t datalen)
360 dd038bc6 2021-09-21 thomas.ad struct ibuf *wbuf;
361 dd038bc6 2021-09-21 thomas.ad struct imsg_hdr hdr;
363 dd038bc6 2021-09-21 thomas.ad datalen += IMSG_HEADER_SIZE;
364 dd038bc6 2021-09-21 thomas.ad if (datalen > MAX_IMSGSIZE) {
365 dd038bc6 2021-09-21 thomas.ad errno = ERANGE;
366 dd038bc6 2021-09-21 thomas.ad return (NULL);
369 dd038bc6 2021-09-21 thomas.ad hdr.type = type;
370 dd038bc6 2021-09-21 thomas.ad hdr.flags = 0;
371 b69b73ef 2023-11-25 thomas hdr.peerid = id;
372 dd038bc6 2021-09-21 thomas.ad if ((hdr.pid = pid) == 0)
373 b69b73ef 2023-11-25 thomas hdr.pid = imsgbuf->pid;
374 dd038bc6 2021-09-21 thomas.ad if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
375 dd038bc6 2021-09-21 thomas.ad return (NULL);
377 dd038bc6 2021-09-21 thomas.ad if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
378 dd038bc6 2021-09-21 thomas.ad return (NULL);
380 dd038bc6 2021-09-21 thomas.ad return (wbuf);
384 3bb9eb8b 2024-01-18 thomas imsg_add(struct ibuf *msg, const void *data, size_t datalen)
386 dd038bc6 2021-09-21 thomas.ad if (datalen)
387 dd038bc6 2021-09-21 thomas.ad if (ibuf_add(msg, data, datalen) == -1) {
388 dd038bc6 2021-09-21 thomas.ad ibuf_free(msg);
389 dd038bc6 2021-09-21 thomas.ad return (-1);
391 dd038bc6 2021-09-21 thomas.ad return (datalen);
395 b69b73ef 2023-11-25 thomas imsg_close(struct imsgbuf *imsgbuf, struct ibuf *msg)
397 dd038bc6 2021-09-21 thomas.ad struct imsg_hdr *hdr;
399 dd038bc6 2021-09-21 thomas.ad hdr = (struct imsg_hdr *)msg->buf;
401 dd038bc6 2021-09-21 thomas.ad hdr->flags &= ~IMSGF_HASFD;
402 3ef3f36a 2023-07-05 op if (ibuf_fd_avail(msg))
403 dd038bc6 2021-09-21 thomas.ad hdr->flags |= IMSGF_HASFD;
404 3ef3f36a 2023-07-05 op hdr->len = ibuf_size(msg);
406 b69b73ef 2023-11-25 thomas ibuf_close(&imsgbuf->w, msg);
410 dd038bc6 2021-09-21 thomas.ad imsg_free(struct imsg *imsg)
412 3bb9eb8b 2024-01-18 thomas ibuf_free(imsg->buf);
415 dd038bc6 2021-09-21 thomas.ad static int
416 b69b73ef 2023-11-25 thomas imsg_dequeue_fd(struct imsgbuf *imsgbuf)
418 dd038bc6 2021-09-21 thomas.ad int fd;
419 dd038bc6 2021-09-21 thomas.ad struct imsg_fd *ifd;
421 b69b73ef 2023-11-25 thomas if ((ifd = TAILQ_FIRST(&imsgbuf->fds)) == NULL)
422 dd038bc6 2021-09-21 thomas.ad return (-1);
424 dd038bc6 2021-09-21 thomas.ad fd = ifd->fd;
425 b69b73ef 2023-11-25 thomas TAILQ_REMOVE(&imsgbuf->fds, ifd, entry);
426 dd038bc6 2021-09-21 thomas.ad free(ifd);
428 dd038bc6 2021-09-21 thomas.ad return (fd);
432 b69b73ef 2023-11-25 thomas imsg_flush(struct imsgbuf *imsgbuf)
434 b69b73ef 2023-11-25 thomas while (imsgbuf->w.queued)
435 b69b73ef 2023-11-25 thomas if (msgbuf_write(&imsgbuf->w) <= 0)
436 dd038bc6 2021-09-21 thomas.ad return (-1);
437 dd038bc6 2021-09-21 thomas.ad return (0);
441 b69b73ef 2023-11-25 thomas imsg_clear(struct imsgbuf *imsgbuf)
443 dd038bc6 2021-09-21 thomas.ad int fd;
445 b69b73ef 2023-11-25 thomas msgbuf_clear(&imsgbuf->w);
446 b69b73ef 2023-11-25 thomas while ((fd = imsg_dequeue_fd(imsgbuf)) != -1)
447 dd038bc6 2021-09-21 thomas.ad close(fd);