Blame


1 3ef3f36a 2023-07-05 op /* $OpenBSD: imsg.c,v 1.19 2023/06/19 17:19:50 claudio Exp $ */
2 dd038bc6 2021-09-21 thomas.ad
3 dd038bc6 2021-09-21 thomas.ad /*
4 dd038bc6 2021-09-21 thomas.ad * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5 dd038bc6 2021-09-21 thomas.ad *
6 dd038bc6 2021-09-21 thomas.ad * Permission to use, copy, modify, and distribute this software for any
7 dd038bc6 2021-09-21 thomas.ad * purpose with or without fee is hereby granted, provided that the above
8 dd038bc6 2021-09-21 thomas.ad * copyright notice and this permission notice appear in all copies.
9 dd038bc6 2021-09-21 thomas.ad *
10 dd038bc6 2021-09-21 thomas.ad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 dd038bc6 2021-09-21 thomas.ad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 dd038bc6 2021-09-21 thomas.ad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 dd038bc6 2021-09-21 thomas.ad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 dd038bc6 2021-09-21 thomas.ad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 dd038bc6 2021-09-21 thomas.ad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 dd038bc6 2021-09-21 thomas.ad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 dd038bc6 2021-09-21 thomas.ad */
18 dd038bc6 2021-09-21 thomas.ad
19 dd038bc6 2021-09-21 thomas.ad #include <sys/types.h>
20 dd038bc6 2021-09-21 thomas.ad #include <sys/socket.h>
21 dd038bc6 2021-09-21 thomas.ad #include <sys/uio.h>
22 dd038bc6 2021-09-21 thomas.ad
23 dd038bc6 2021-09-21 thomas.ad #include <errno.h>
24 dd038bc6 2021-09-21 thomas.ad #include <stdlib.h>
25 dd038bc6 2021-09-21 thomas.ad #include <string.h>
26 dd038bc6 2021-09-21 thomas.ad #include <unistd.h>
27 dd038bc6 2021-09-21 thomas.ad
28 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
29 dd038bc6 2021-09-21 thomas.ad #include "imsg.h"
30 dd038bc6 2021-09-21 thomas.ad
31 dd038bc6 2021-09-21 thomas.ad int imsg_fd_overhead = 0;
32 dd038bc6 2021-09-21 thomas.ad
33 dd038bc6 2021-09-21 thomas.ad static int imsg_get_fd(struct imsgbuf *);
34 dd038bc6 2021-09-21 thomas.ad
35 dd038bc6 2021-09-21 thomas.ad void
36 dd038bc6 2021-09-21 thomas.ad imsg_init(struct imsgbuf *ibuf, int fd)
37 dd038bc6 2021-09-21 thomas.ad {
38 dd038bc6 2021-09-21 thomas.ad msgbuf_init(&ibuf->w);
39 dd038bc6 2021-09-21 thomas.ad memset(&ibuf->r, 0, sizeof(ibuf->r));
40 dd038bc6 2021-09-21 thomas.ad ibuf->fd = fd;
41 dd038bc6 2021-09-21 thomas.ad ibuf->w.fd = fd;
42 dd038bc6 2021-09-21 thomas.ad ibuf->pid = getpid();
43 dd038bc6 2021-09-21 thomas.ad TAILQ_INIT(&ibuf->fds);
44 dd038bc6 2021-09-21 thomas.ad }
45 dd038bc6 2021-09-21 thomas.ad
46 dd038bc6 2021-09-21 thomas.ad ssize_t
47 dd038bc6 2021-09-21 thomas.ad imsg_read(struct imsgbuf *ibuf)
48 dd038bc6 2021-09-21 thomas.ad {
49 dd038bc6 2021-09-21 thomas.ad struct msghdr msg;
50 dd038bc6 2021-09-21 thomas.ad struct cmsghdr *cmsg;
51 dd038bc6 2021-09-21 thomas.ad union {
52 dd038bc6 2021-09-21 thomas.ad struct cmsghdr hdr;
53 dd038bc6 2021-09-21 thomas.ad char buf[CMSG_SPACE(sizeof(int) * 1)];
54 dd038bc6 2021-09-21 thomas.ad } cmsgbuf;
55 dd038bc6 2021-09-21 thomas.ad struct iovec iov;
56 dd038bc6 2021-09-21 thomas.ad ssize_t n = -1;
57 dd038bc6 2021-09-21 thomas.ad int fd;
58 dd038bc6 2021-09-21 thomas.ad struct imsg_fd *ifd;
59 dd038bc6 2021-09-21 thomas.ad
60 dd038bc6 2021-09-21 thomas.ad memset(&msg, 0, sizeof(msg));
61 dd038bc6 2021-09-21 thomas.ad memset(&cmsgbuf, 0, sizeof(cmsgbuf));
62 dd038bc6 2021-09-21 thomas.ad
63 dd038bc6 2021-09-21 thomas.ad iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
64 dd038bc6 2021-09-21 thomas.ad iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
65 dd038bc6 2021-09-21 thomas.ad msg.msg_iov = &iov;
66 dd038bc6 2021-09-21 thomas.ad msg.msg_iovlen = 1;
67 dd038bc6 2021-09-21 thomas.ad msg.msg_control = &cmsgbuf.buf;
68 dd038bc6 2021-09-21 thomas.ad msg.msg_controllen = sizeof(cmsgbuf.buf);
69 dd038bc6 2021-09-21 thomas.ad
70 dd038bc6 2021-09-21 thomas.ad if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
71 dd038bc6 2021-09-21 thomas.ad return (-1);
72 dd038bc6 2021-09-21 thomas.ad
73 dd038bc6 2021-09-21 thomas.ad again:
74 dd038bc6 2021-09-21 thomas.ad if (getdtablecount() + imsg_fd_overhead +
75 dd038bc6 2021-09-21 thomas.ad (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
76 dd038bc6 2021-09-21 thomas.ad >= getdtablesize()) {
77 dd038bc6 2021-09-21 thomas.ad errno = EAGAIN;
78 dd038bc6 2021-09-21 thomas.ad free(ifd);
79 dd038bc6 2021-09-21 thomas.ad return (-1);
80 dd038bc6 2021-09-21 thomas.ad }
81 dd038bc6 2021-09-21 thomas.ad
82 dd038bc6 2021-09-21 thomas.ad if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
83 dd038bc6 2021-09-21 thomas.ad if (errno == EINTR)
84 dd038bc6 2021-09-21 thomas.ad goto again;
85 dd038bc6 2021-09-21 thomas.ad goto fail;
86 dd038bc6 2021-09-21 thomas.ad }
87 dd038bc6 2021-09-21 thomas.ad
88 dd038bc6 2021-09-21 thomas.ad ibuf->r.wpos += n;
89 dd038bc6 2021-09-21 thomas.ad
90 dd038bc6 2021-09-21 thomas.ad for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
91 dd038bc6 2021-09-21 thomas.ad cmsg = CMSG_NXTHDR(&msg, cmsg)) {
92 dd038bc6 2021-09-21 thomas.ad if (cmsg->cmsg_level == SOL_SOCKET &&
93 dd038bc6 2021-09-21 thomas.ad cmsg->cmsg_type == SCM_RIGHTS) {
94 dd038bc6 2021-09-21 thomas.ad int i;
95 dd038bc6 2021-09-21 thomas.ad int j;
96 dd038bc6 2021-09-21 thomas.ad
97 dd038bc6 2021-09-21 thomas.ad /*
98 dd038bc6 2021-09-21 thomas.ad * We only accept one file descriptor. Due to C
99 dd038bc6 2021-09-21 thomas.ad * padding rules, our control buffer might contain
100 dd038bc6 2021-09-21 thomas.ad * more than one fd, and we must close them.
101 dd038bc6 2021-09-21 thomas.ad */
102 dd038bc6 2021-09-21 thomas.ad j = ((char *)cmsg + cmsg->cmsg_len -
103 dd038bc6 2021-09-21 thomas.ad (char *)CMSG_DATA(cmsg)) / sizeof(int);
104 dd038bc6 2021-09-21 thomas.ad for (i = 0; i < j; i++) {
105 dd038bc6 2021-09-21 thomas.ad fd = ((int *)CMSG_DATA(cmsg))[i];
106 dd038bc6 2021-09-21 thomas.ad if (ifd != NULL) {
107 dd038bc6 2021-09-21 thomas.ad ifd->fd = fd;
108 dd038bc6 2021-09-21 thomas.ad TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
109 dd038bc6 2021-09-21 thomas.ad entry);
110 dd038bc6 2021-09-21 thomas.ad ifd = NULL;
111 dd038bc6 2021-09-21 thomas.ad } else
112 dd038bc6 2021-09-21 thomas.ad close(fd);
113 dd038bc6 2021-09-21 thomas.ad }
114 dd038bc6 2021-09-21 thomas.ad }
115 dd038bc6 2021-09-21 thomas.ad /* we do not handle other ctl data level */
116 dd038bc6 2021-09-21 thomas.ad }
117 dd038bc6 2021-09-21 thomas.ad
118 dd038bc6 2021-09-21 thomas.ad fail:
119 dd038bc6 2021-09-21 thomas.ad free(ifd);
120 dd038bc6 2021-09-21 thomas.ad return (n);
121 dd038bc6 2021-09-21 thomas.ad }
122 dd038bc6 2021-09-21 thomas.ad
123 dd038bc6 2021-09-21 thomas.ad ssize_t
124 dd038bc6 2021-09-21 thomas.ad imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
125 dd038bc6 2021-09-21 thomas.ad {
126 dd038bc6 2021-09-21 thomas.ad size_t av, left, datalen;
127 dd038bc6 2021-09-21 thomas.ad
128 dd038bc6 2021-09-21 thomas.ad av = ibuf->r.wpos;
129 dd038bc6 2021-09-21 thomas.ad
130 dd038bc6 2021-09-21 thomas.ad if (IMSG_HEADER_SIZE > av)
131 dd038bc6 2021-09-21 thomas.ad return (0);
132 dd038bc6 2021-09-21 thomas.ad
133 dd038bc6 2021-09-21 thomas.ad memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
134 dd038bc6 2021-09-21 thomas.ad if (imsg->hdr.len < IMSG_HEADER_SIZE ||
135 dd038bc6 2021-09-21 thomas.ad imsg->hdr.len > MAX_IMSGSIZE) {
136 dd038bc6 2021-09-21 thomas.ad errno = ERANGE;
137 dd038bc6 2021-09-21 thomas.ad return (-1);
138 dd038bc6 2021-09-21 thomas.ad }
139 dd038bc6 2021-09-21 thomas.ad if (imsg->hdr.len > av)
140 dd038bc6 2021-09-21 thomas.ad return (0);
141 dd038bc6 2021-09-21 thomas.ad datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
142 dd038bc6 2021-09-21 thomas.ad ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
143 dd038bc6 2021-09-21 thomas.ad if (datalen == 0)
144 dd038bc6 2021-09-21 thomas.ad imsg->data = NULL;
145 dd038bc6 2021-09-21 thomas.ad else if ((imsg->data = malloc(datalen)) == NULL)
146 dd038bc6 2021-09-21 thomas.ad return (-1);
147 dd038bc6 2021-09-21 thomas.ad
148 dd038bc6 2021-09-21 thomas.ad if (imsg->hdr.flags & IMSGF_HASFD)
149 dd038bc6 2021-09-21 thomas.ad imsg->fd = imsg_get_fd(ibuf);
150 dd038bc6 2021-09-21 thomas.ad else
151 dd038bc6 2021-09-21 thomas.ad imsg->fd = -1;
152 dd038bc6 2021-09-21 thomas.ad
153 3ef3f36a 2023-07-05 op if (datalen != 0)
154 3ef3f36a 2023-07-05 op memcpy(imsg->data, ibuf->r.rptr, datalen);
155 dd038bc6 2021-09-21 thomas.ad
156 dd038bc6 2021-09-21 thomas.ad if (imsg->hdr.len < av) {
157 dd038bc6 2021-09-21 thomas.ad left = av - imsg->hdr.len;
158 dd038bc6 2021-09-21 thomas.ad memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
159 dd038bc6 2021-09-21 thomas.ad ibuf->r.wpos = left;
160 dd038bc6 2021-09-21 thomas.ad } else
161 dd038bc6 2021-09-21 thomas.ad ibuf->r.wpos = 0;
162 dd038bc6 2021-09-21 thomas.ad
163 dd038bc6 2021-09-21 thomas.ad return (datalen + IMSG_HEADER_SIZE);
164 dd038bc6 2021-09-21 thomas.ad }
165 dd038bc6 2021-09-21 thomas.ad
166 dd038bc6 2021-09-21 thomas.ad int
167 dd038bc6 2021-09-21 thomas.ad imsg_compose(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
168 dd038bc6 2021-09-21 thomas.ad int fd, const void *data, uint16_t datalen)
169 dd038bc6 2021-09-21 thomas.ad {
170 dd038bc6 2021-09-21 thomas.ad struct ibuf *wbuf;
171 dd038bc6 2021-09-21 thomas.ad
172 dd038bc6 2021-09-21 thomas.ad if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
173 dd038bc6 2021-09-21 thomas.ad return (-1);
174 dd038bc6 2021-09-21 thomas.ad
175 dd038bc6 2021-09-21 thomas.ad if (imsg_add(wbuf, data, datalen) == -1)
176 dd038bc6 2021-09-21 thomas.ad return (-1);
177 dd038bc6 2021-09-21 thomas.ad
178 3ef3f36a 2023-07-05 op ibuf_fd_set(wbuf, fd);
179 dd038bc6 2021-09-21 thomas.ad imsg_close(ibuf, wbuf);
180 dd038bc6 2021-09-21 thomas.ad
181 dd038bc6 2021-09-21 thomas.ad return (1);
182 dd038bc6 2021-09-21 thomas.ad }
183 dd038bc6 2021-09-21 thomas.ad
184 dd038bc6 2021-09-21 thomas.ad int
185 dd038bc6 2021-09-21 thomas.ad imsg_composev(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
186 dd038bc6 2021-09-21 thomas.ad int fd, const struct iovec *iov, int iovcnt)
187 dd038bc6 2021-09-21 thomas.ad {
188 dd038bc6 2021-09-21 thomas.ad struct ibuf *wbuf;
189 dd038bc6 2021-09-21 thomas.ad int i, datalen = 0;
190 dd038bc6 2021-09-21 thomas.ad
191 dd038bc6 2021-09-21 thomas.ad for (i = 0; i < iovcnt; i++)
192 dd038bc6 2021-09-21 thomas.ad datalen += iov[i].iov_len;
193 dd038bc6 2021-09-21 thomas.ad
194 dd038bc6 2021-09-21 thomas.ad if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
195 dd038bc6 2021-09-21 thomas.ad return (-1);
196 dd038bc6 2021-09-21 thomas.ad
197 dd038bc6 2021-09-21 thomas.ad for (i = 0; i < iovcnt; i++)
198 dd038bc6 2021-09-21 thomas.ad if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
199 dd038bc6 2021-09-21 thomas.ad return (-1);
200 dd038bc6 2021-09-21 thomas.ad
201 3ef3f36a 2023-07-05 op ibuf_fd_set(wbuf, fd);
202 dd038bc6 2021-09-21 thomas.ad imsg_close(ibuf, wbuf);
203 dd038bc6 2021-09-21 thomas.ad
204 dd038bc6 2021-09-21 thomas.ad return (1);
205 dd038bc6 2021-09-21 thomas.ad }
206 dd038bc6 2021-09-21 thomas.ad
207 3ef3f36a 2023-07-05 op int
208 3ef3f36a 2023-07-05 op imsg_compose_ibuf(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid,
209 3ef3f36a 2023-07-05 op pid_t pid, struct ibuf *buf)
210 3ef3f36a 2023-07-05 op {
211 3ef3f36a 2023-07-05 op struct ibuf *wbuf = NULL;
212 3ef3f36a 2023-07-05 op struct imsg_hdr hdr;
213 3ef3f36a 2023-07-05 op int save_errno;
214 3ef3f36a 2023-07-05 op
215 3ef3f36a 2023-07-05 op if (ibuf_size(buf) + IMSG_HEADER_SIZE > MAX_IMSGSIZE) {
216 3ef3f36a 2023-07-05 op errno = ERANGE;
217 3ef3f36a 2023-07-05 op goto fail;
218 3ef3f36a 2023-07-05 op }
219 3ef3f36a 2023-07-05 op
220 3ef3f36a 2023-07-05 op hdr.type = type;
221 3ef3f36a 2023-07-05 op hdr.len = ibuf_size(buf) + IMSG_HEADER_SIZE;
222 3ef3f36a 2023-07-05 op hdr.flags = 0;
223 3ef3f36a 2023-07-05 op hdr.peerid = peerid;
224 3ef3f36a 2023-07-05 op if ((hdr.pid = pid) == 0)
225 3ef3f36a 2023-07-05 op hdr.pid = ibuf->pid;
226 3ef3f36a 2023-07-05 op
227 3ef3f36a 2023-07-05 op if ((wbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
228 3ef3f36a 2023-07-05 op goto fail;
229 3ef3f36a 2023-07-05 op if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
230 3ef3f36a 2023-07-05 op goto fail;
231 3ef3f36a 2023-07-05 op
232 3ef3f36a 2023-07-05 op ibuf_close(&ibuf->w, wbuf);
233 3ef3f36a 2023-07-05 op ibuf_close(&ibuf->w, buf);
234 3ef3f36a 2023-07-05 op return (1);
235 3ef3f36a 2023-07-05 op
236 3ef3f36a 2023-07-05 op fail:
237 3ef3f36a 2023-07-05 op save_errno = errno;
238 3ef3f36a 2023-07-05 op ibuf_free(buf);
239 3ef3f36a 2023-07-05 op ibuf_free(wbuf);
240 3ef3f36a 2023-07-05 op errno = save_errno;
241 3ef3f36a 2023-07-05 op return (-1);
242 3ef3f36a 2023-07-05 op }
243 3ef3f36a 2023-07-05 op
244 dd038bc6 2021-09-21 thomas.ad struct ibuf *
245 dd038bc6 2021-09-21 thomas.ad imsg_create(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
246 dd038bc6 2021-09-21 thomas.ad uint16_t datalen)
247 dd038bc6 2021-09-21 thomas.ad {
248 dd038bc6 2021-09-21 thomas.ad struct ibuf *wbuf;
249 dd038bc6 2021-09-21 thomas.ad struct imsg_hdr hdr;
250 dd038bc6 2021-09-21 thomas.ad
251 dd038bc6 2021-09-21 thomas.ad datalen += IMSG_HEADER_SIZE;
252 dd038bc6 2021-09-21 thomas.ad if (datalen > MAX_IMSGSIZE) {
253 dd038bc6 2021-09-21 thomas.ad errno = ERANGE;
254 dd038bc6 2021-09-21 thomas.ad return (NULL);
255 dd038bc6 2021-09-21 thomas.ad }
256 dd038bc6 2021-09-21 thomas.ad
257 dd038bc6 2021-09-21 thomas.ad hdr.type = type;
258 dd038bc6 2021-09-21 thomas.ad hdr.flags = 0;
259 dd038bc6 2021-09-21 thomas.ad hdr.peerid = peerid;
260 dd038bc6 2021-09-21 thomas.ad if ((hdr.pid = pid) == 0)
261 dd038bc6 2021-09-21 thomas.ad hdr.pid = ibuf->pid;
262 dd038bc6 2021-09-21 thomas.ad if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
263 dd038bc6 2021-09-21 thomas.ad return (NULL);
264 dd038bc6 2021-09-21 thomas.ad }
265 dd038bc6 2021-09-21 thomas.ad if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
266 dd038bc6 2021-09-21 thomas.ad return (NULL);
267 dd038bc6 2021-09-21 thomas.ad
268 dd038bc6 2021-09-21 thomas.ad return (wbuf);
269 dd038bc6 2021-09-21 thomas.ad }
270 dd038bc6 2021-09-21 thomas.ad
271 dd038bc6 2021-09-21 thomas.ad int
272 dd038bc6 2021-09-21 thomas.ad imsg_add(struct ibuf *msg, const void *data, uint16_t datalen)
273 dd038bc6 2021-09-21 thomas.ad {
274 dd038bc6 2021-09-21 thomas.ad if (datalen)
275 dd038bc6 2021-09-21 thomas.ad if (ibuf_add(msg, data, datalen) == -1) {
276 dd038bc6 2021-09-21 thomas.ad ibuf_free(msg);
277 dd038bc6 2021-09-21 thomas.ad return (-1);
278 dd038bc6 2021-09-21 thomas.ad }
279 dd038bc6 2021-09-21 thomas.ad return (datalen);
280 dd038bc6 2021-09-21 thomas.ad }
281 dd038bc6 2021-09-21 thomas.ad
282 dd038bc6 2021-09-21 thomas.ad void
283 dd038bc6 2021-09-21 thomas.ad imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
284 dd038bc6 2021-09-21 thomas.ad {
285 dd038bc6 2021-09-21 thomas.ad struct imsg_hdr *hdr;
286 dd038bc6 2021-09-21 thomas.ad
287 dd038bc6 2021-09-21 thomas.ad hdr = (struct imsg_hdr *)msg->buf;
288 dd038bc6 2021-09-21 thomas.ad
289 dd038bc6 2021-09-21 thomas.ad hdr->flags &= ~IMSGF_HASFD;
290 3ef3f36a 2023-07-05 op if (ibuf_fd_avail(msg))
291 dd038bc6 2021-09-21 thomas.ad hdr->flags |= IMSGF_HASFD;
292 3ef3f36a 2023-07-05 op hdr->len = ibuf_size(msg);
293 dd038bc6 2021-09-21 thomas.ad
294 dd038bc6 2021-09-21 thomas.ad ibuf_close(&ibuf->w, msg);
295 dd038bc6 2021-09-21 thomas.ad }
296 dd038bc6 2021-09-21 thomas.ad
297 dd038bc6 2021-09-21 thomas.ad void
298 dd038bc6 2021-09-21 thomas.ad imsg_free(struct imsg *imsg)
299 dd038bc6 2021-09-21 thomas.ad {
300 dd038bc6 2021-09-21 thomas.ad freezero(imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);
301 dd038bc6 2021-09-21 thomas.ad }
302 dd038bc6 2021-09-21 thomas.ad
303 dd038bc6 2021-09-21 thomas.ad static int
304 dd038bc6 2021-09-21 thomas.ad imsg_get_fd(struct imsgbuf *ibuf)
305 dd038bc6 2021-09-21 thomas.ad {
306 dd038bc6 2021-09-21 thomas.ad int fd;
307 dd038bc6 2021-09-21 thomas.ad struct imsg_fd *ifd;
308 dd038bc6 2021-09-21 thomas.ad
309 dd038bc6 2021-09-21 thomas.ad if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
310 dd038bc6 2021-09-21 thomas.ad return (-1);
311 dd038bc6 2021-09-21 thomas.ad
312 dd038bc6 2021-09-21 thomas.ad fd = ifd->fd;
313 dd038bc6 2021-09-21 thomas.ad TAILQ_REMOVE(&ibuf->fds, ifd, entry);
314 dd038bc6 2021-09-21 thomas.ad free(ifd);
315 dd038bc6 2021-09-21 thomas.ad
316 dd038bc6 2021-09-21 thomas.ad return (fd);
317 dd038bc6 2021-09-21 thomas.ad }
318 dd038bc6 2021-09-21 thomas.ad
319 dd038bc6 2021-09-21 thomas.ad int
320 dd038bc6 2021-09-21 thomas.ad imsg_flush(struct imsgbuf *ibuf)
321 dd038bc6 2021-09-21 thomas.ad {
322 dd038bc6 2021-09-21 thomas.ad while (ibuf->w.queued)
323 dd038bc6 2021-09-21 thomas.ad if (msgbuf_write(&ibuf->w) <= 0)
324 dd038bc6 2021-09-21 thomas.ad return (-1);
325 dd038bc6 2021-09-21 thomas.ad return (0);
326 dd038bc6 2021-09-21 thomas.ad }
327 dd038bc6 2021-09-21 thomas.ad
328 dd038bc6 2021-09-21 thomas.ad void
329 dd038bc6 2021-09-21 thomas.ad imsg_clear(struct imsgbuf *ibuf)
330 dd038bc6 2021-09-21 thomas.ad {
331 dd038bc6 2021-09-21 thomas.ad int fd;
332 dd038bc6 2021-09-21 thomas.ad
333 dd038bc6 2021-09-21 thomas.ad msgbuf_clear(&ibuf->w);
334 dd038bc6 2021-09-21 thomas.ad while ((fd = imsg_get_fd(ibuf)) != -1)
335 dd038bc6 2021-09-21 thomas.ad close(fd);
336 dd038bc6 2021-09-21 thomas.ad }