Blame


1 8623abea 2024-01-20 op /* $OpenBSD: imsg.c,v 1.23 2023/12/12 15:47:41 claudio Exp $ */
2 b0003f58 2021-03-08 op
3 b0003f58 2021-03-08 op /*
4 8623abea 2024-01-20 op * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
5 b0003f58 2021-03-08 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 b0003f58 2021-03-08 op *
7 b0003f58 2021-03-08 op * Permission to use, copy, modify, and distribute this software for any
8 b0003f58 2021-03-08 op * purpose with or without fee is hereby granted, provided that the above
9 b0003f58 2021-03-08 op * copyright notice and this permission notice appear in all copies.
10 b0003f58 2021-03-08 op *
11 b0003f58 2021-03-08 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 b0003f58 2021-03-08 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 b0003f58 2021-03-08 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 b0003f58 2021-03-08 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 b0003f58 2021-03-08 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 b0003f58 2021-03-08 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 b0003f58 2021-03-08 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 b0003f58 2021-03-08 op */
19 a625907c 2021-03-19 op
20 a625907c 2021-03-19 op #include "compat.h"
21 b0003f58 2021-03-08 op
22 b0003f58 2021-03-08 op #include <sys/types.h>
23 b0003f58 2021-03-08 op #include <sys/socket.h>
24 b0003f58 2021-03-08 op #include <sys/uio.h>
25 b0003f58 2021-03-08 op
26 b0003f58 2021-03-08 op #include <errno.h>
27 b0003f58 2021-03-08 op #include <stdlib.h>
28 b0003f58 2021-03-08 op #include <string.h>
29 b0003f58 2021-03-08 op #include <unistd.h>
30 b0003f58 2021-03-08 op
31 b0003f58 2021-03-08 op #include "imsg.h"
32 b0003f58 2021-03-08 op
33 8623abea 2024-01-20 op struct imsg_fd {
34 8623abea 2024-01-20 op TAILQ_ENTRY(imsg_fd) entry;
35 8623abea 2024-01-20 op int fd;
36 8623abea 2024-01-20 op };
37 8623abea 2024-01-20 op
38 b0003f58 2021-03-08 op int imsg_fd_overhead = 0;
39 b0003f58 2021-03-08 op
40 8623abea 2024-01-20 op static int imsg_dequeue_fd(struct imsgbuf *);
41 b0003f58 2021-03-08 op
42 b0003f58 2021-03-08 op void
43 8623abea 2024-01-20 op imsg_init(struct imsgbuf *imsgbuf, int fd)
44 b0003f58 2021-03-08 op {
45 8623abea 2024-01-20 op msgbuf_init(&imsgbuf->w);
46 8623abea 2024-01-20 op memset(&imsgbuf->r, 0, sizeof(imsgbuf->r));
47 8623abea 2024-01-20 op imsgbuf->fd = fd;
48 8623abea 2024-01-20 op imsgbuf->w.fd = fd;
49 8623abea 2024-01-20 op imsgbuf->pid = getpid();
50 8623abea 2024-01-20 op TAILQ_INIT(&imsgbuf->fds);
51 b0003f58 2021-03-08 op }
52 b0003f58 2021-03-08 op
53 b0003f58 2021-03-08 op ssize_t
54 8623abea 2024-01-20 op imsg_read(struct imsgbuf *imsgbuf)
55 b0003f58 2021-03-08 op {
56 b0003f58 2021-03-08 op struct msghdr msg;
57 b0003f58 2021-03-08 op struct cmsghdr *cmsg;
58 b0003f58 2021-03-08 op union {
59 b0003f58 2021-03-08 op struct cmsghdr hdr;
60 b0003f58 2021-03-08 op char buf[CMSG_SPACE(sizeof(int) * 1)];
61 b0003f58 2021-03-08 op } cmsgbuf;
62 b0003f58 2021-03-08 op struct iovec iov;
63 b0003f58 2021-03-08 op ssize_t n = -1;
64 b0003f58 2021-03-08 op int fd;
65 b0003f58 2021-03-08 op struct imsg_fd *ifd;
66 b0003f58 2021-03-08 op
67 b0003f58 2021-03-08 op memset(&msg, 0, sizeof(msg));
68 b0003f58 2021-03-08 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
69 b0003f58 2021-03-08 op
70 8623abea 2024-01-20 op iov.iov_base = imsgbuf->r.buf + imsgbuf->r.wpos;
71 8623abea 2024-01-20 op iov.iov_len = sizeof(imsgbuf->r.buf) - imsgbuf->r.wpos;
72 b0003f58 2021-03-08 op msg.msg_iov = &iov;
73 b0003f58 2021-03-08 op msg.msg_iovlen = 1;
74 b0003f58 2021-03-08 op msg.msg_control = &cmsgbuf.buf;
75 b0003f58 2021-03-08 op msg.msg_controllen = sizeof(cmsgbuf.buf);
76 b0003f58 2021-03-08 op
77 b0003f58 2021-03-08 op if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
78 b0003f58 2021-03-08 op return (-1);
79 b0003f58 2021-03-08 op
80 b0003f58 2021-03-08 op again:
81 b0003f58 2021-03-08 op if (getdtablecount() + imsg_fd_overhead +
82 b0003f58 2021-03-08 op (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
83 b0003f58 2021-03-08 op >= getdtablesize()) {
84 b0003f58 2021-03-08 op errno = EAGAIN;
85 b0003f58 2021-03-08 op free(ifd);
86 b0003f58 2021-03-08 op return (-1);
87 b0003f58 2021-03-08 op }
88 b0003f58 2021-03-08 op
89 8623abea 2024-01-20 op if ((n = recvmsg(imsgbuf->fd, &msg, 0)) == -1) {
90 b0003f58 2021-03-08 op if (errno == EINTR)
91 b0003f58 2021-03-08 op goto again;
92 b0003f58 2021-03-08 op goto fail;
93 b0003f58 2021-03-08 op }
94 b0003f58 2021-03-08 op
95 8623abea 2024-01-20 op imsgbuf->r.wpos += n;
96 b0003f58 2021-03-08 op
97 b0003f58 2021-03-08 op for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
98 b0003f58 2021-03-08 op cmsg = CMSG_NXTHDR(&msg, cmsg)) {
99 b0003f58 2021-03-08 op if (cmsg->cmsg_level == SOL_SOCKET &&
100 b0003f58 2021-03-08 op cmsg->cmsg_type == SCM_RIGHTS) {
101 b0003f58 2021-03-08 op int i;
102 b0003f58 2021-03-08 op int j;
103 b0003f58 2021-03-08 op
104 b0003f58 2021-03-08 op /*
105 b0003f58 2021-03-08 op * We only accept one file descriptor. Due to C
106 b0003f58 2021-03-08 op * padding rules, our control buffer might contain
107 b0003f58 2021-03-08 op * more than one fd, and we must close them.
108 b0003f58 2021-03-08 op */
109 b0003f58 2021-03-08 op j = ((char *)cmsg + cmsg->cmsg_len -
110 b0003f58 2021-03-08 op (char *)CMSG_DATA(cmsg)) / sizeof(int);
111 b0003f58 2021-03-08 op for (i = 0; i < j; i++) {
112 b0003f58 2021-03-08 op fd = ((int *)CMSG_DATA(cmsg))[i];
113 b0003f58 2021-03-08 op if (ifd != NULL) {
114 b0003f58 2021-03-08 op ifd->fd = fd;
115 8623abea 2024-01-20 op TAILQ_INSERT_TAIL(&imsgbuf->fds, ifd,
116 b0003f58 2021-03-08 op entry);
117 b0003f58 2021-03-08 op ifd = NULL;
118 b0003f58 2021-03-08 op } else
119 b0003f58 2021-03-08 op close(fd);
120 b0003f58 2021-03-08 op }
121 b0003f58 2021-03-08 op }
122 b0003f58 2021-03-08 op /* we do not handle other ctl data level */
123 b0003f58 2021-03-08 op }
124 b0003f58 2021-03-08 op
125 b0003f58 2021-03-08 op fail:
126 b0003f58 2021-03-08 op free(ifd);
127 b0003f58 2021-03-08 op return (n);
128 b0003f58 2021-03-08 op }
129 b0003f58 2021-03-08 op
130 b0003f58 2021-03-08 op ssize_t
131 8623abea 2024-01-20 op imsg_get(struct imsgbuf *imsgbuf, struct imsg *imsg)
132 b0003f58 2021-03-08 op {
133 8623abea 2024-01-20 op struct imsg m;
134 b0003f58 2021-03-08 op size_t av, left, datalen;
135 b0003f58 2021-03-08 op
136 8623abea 2024-01-20 op av = imsgbuf->r.wpos;
137 b0003f58 2021-03-08 op
138 b0003f58 2021-03-08 op if (IMSG_HEADER_SIZE > av)
139 b0003f58 2021-03-08 op return (0);
140 b0003f58 2021-03-08 op
141 8623abea 2024-01-20 op memcpy(&m.hdr, imsgbuf->r.buf, sizeof(m.hdr));
142 8623abea 2024-01-20 op if (m.hdr.len < IMSG_HEADER_SIZE ||
143 8623abea 2024-01-20 op m.hdr.len > MAX_IMSGSIZE) {
144 b0003f58 2021-03-08 op errno = ERANGE;
145 b0003f58 2021-03-08 op return (-1);
146 b0003f58 2021-03-08 op }
147 8623abea 2024-01-20 op if (m.hdr.len > av)
148 b0003f58 2021-03-08 op return (0);
149 b0003f58 2021-03-08 op
150 8623abea 2024-01-20 op m.fd = -1;
151 8623abea 2024-01-20 op m.buf = NULL;
152 8623abea 2024-01-20 op m.data = NULL;
153 b0003f58 2021-03-08 op
154 8623abea 2024-01-20 op datalen = m.hdr.len - IMSG_HEADER_SIZE;
155 8623abea 2024-01-20 op imsgbuf->r.rptr = imsgbuf->r.buf + IMSG_HEADER_SIZE;
156 8623abea 2024-01-20 op if (datalen != 0) {
157 8623abea 2024-01-20 op if ((m.buf = ibuf_open(datalen)) == NULL)
158 8623abea 2024-01-20 op return (-1);
159 8623abea 2024-01-20 op if (ibuf_add(m.buf, imsgbuf->r.rptr, datalen) == -1) {
160 8623abea 2024-01-20 op /* this should never fail */
161 8623abea 2024-01-20 op ibuf_free(m.buf);
162 8623abea 2024-01-20 op return (-1);
163 8623abea 2024-01-20 op }
164 8623abea 2024-01-20 op m.data = ibuf_data(m.buf);
165 8623abea 2024-01-20 op }
166 b0003f58 2021-03-08 op
167 8623abea 2024-01-20 op if (m.hdr.flags & IMSGF_HASFD)
168 8623abea 2024-01-20 op m.fd = imsg_dequeue_fd(imsgbuf);
169 8623abea 2024-01-20 op
170 8623abea 2024-01-20 op if (m.hdr.len < av) {
171 8623abea 2024-01-20 op left = av - m.hdr.len;
172 8623abea 2024-01-20 op memmove(&imsgbuf->r.buf, imsgbuf->r.buf + m.hdr.len, left);
173 8623abea 2024-01-20 op imsgbuf->r.wpos = left;
174 b0003f58 2021-03-08 op } else
175 8623abea 2024-01-20 op imsgbuf->r.wpos = 0;
176 b0003f58 2021-03-08 op
177 8623abea 2024-01-20 op *imsg = m;
178 b0003f58 2021-03-08 op return (datalen + IMSG_HEADER_SIZE);
179 b0003f58 2021-03-08 op }
180 b0003f58 2021-03-08 op
181 b0003f58 2021-03-08 op int
182 8623abea 2024-01-20 op imsg_get_ibuf(struct imsg *imsg, struct ibuf *ibuf)
183 8623abea 2024-01-20 op {
184 8623abea 2024-01-20 op if (imsg->buf == NULL) {
185 8623abea 2024-01-20 op errno = EBADMSG;
186 8623abea 2024-01-20 op return (-1);
187 8623abea 2024-01-20 op }
188 8623abea 2024-01-20 op return ibuf_get_ibuf(imsg->buf, ibuf_size(imsg->buf), ibuf);
189 8623abea 2024-01-20 op }
190 8623abea 2024-01-20 op
191 8623abea 2024-01-20 op int
192 8623abea 2024-01-20 op imsg_get_data(struct imsg *imsg, void *data, size_t len)
193 8623abea 2024-01-20 op {
194 8623abea 2024-01-20 op if (len == 0) {
195 8623abea 2024-01-20 op errno = EINVAL;
196 8623abea 2024-01-20 op return (-1);
197 8623abea 2024-01-20 op }
198 8623abea 2024-01-20 op if (imsg->buf == NULL || ibuf_size(imsg->buf) != len) {
199 8623abea 2024-01-20 op errno = EBADMSG;
200 8623abea 2024-01-20 op return (-1);
201 8623abea 2024-01-20 op }
202 8623abea 2024-01-20 op return ibuf_get(imsg->buf, data, len);
203 8623abea 2024-01-20 op }
204 8623abea 2024-01-20 op
205 8623abea 2024-01-20 op int
206 8623abea 2024-01-20 op imsg_get_fd(struct imsg *imsg)
207 8623abea 2024-01-20 op {
208 8623abea 2024-01-20 op int fd = imsg->fd;
209 8623abea 2024-01-20 op
210 8623abea 2024-01-20 op imsg->fd = -1;
211 8623abea 2024-01-20 op return fd;
212 8623abea 2024-01-20 op }
213 8623abea 2024-01-20 op
214 8623abea 2024-01-20 op uint32_t
215 8623abea 2024-01-20 op imsg_get_id(struct imsg *imsg)
216 8623abea 2024-01-20 op {
217 8623abea 2024-01-20 op return (imsg->hdr.peerid);
218 8623abea 2024-01-20 op }
219 8623abea 2024-01-20 op
220 8623abea 2024-01-20 op size_t
221 8623abea 2024-01-20 op imsg_get_len(struct imsg *imsg)
222 8623abea 2024-01-20 op {
223 8623abea 2024-01-20 op if (imsg->buf == NULL)
224 8623abea 2024-01-20 op return 0;
225 8623abea 2024-01-20 op return ibuf_size(imsg->buf);
226 8623abea 2024-01-20 op }
227 8623abea 2024-01-20 op
228 8623abea 2024-01-20 op pid_t
229 8623abea 2024-01-20 op imsg_get_pid(struct imsg *imsg)
230 b0003f58 2021-03-08 op {
231 8623abea 2024-01-20 op return (imsg->hdr.pid);
232 8623abea 2024-01-20 op }
233 8623abea 2024-01-20 op
234 8623abea 2024-01-20 op uint32_t
235 8623abea 2024-01-20 op imsg_get_type(struct imsg *imsg)
236 8623abea 2024-01-20 op {
237 8623abea 2024-01-20 op return (imsg->hdr.type);
238 8623abea 2024-01-20 op }
239 8623abea 2024-01-20 op
240 8623abea 2024-01-20 op int
241 8623abea 2024-01-20 op imsg_compose(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
242 8623abea 2024-01-20 op int fd, const void *data, size_t datalen)
243 8623abea 2024-01-20 op {
244 b0003f58 2021-03-08 op struct ibuf *wbuf;
245 b0003f58 2021-03-08 op
246 8623abea 2024-01-20 op if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
247 b0003f58 2021-03-08 op return (-1);
248 b0003f58 2021-03-08 op
249 b0003f58 2021-03-08 op if (imsg_add(wbuf, data, datalen) == -1)
250 b0003f58 2021-03-08 op return (-1);
251 b0003f58 2021-03-08 op
252 ef16f8f6 2023-07-02 op ibuf_fd_set(wbuf, fd);
253 8623abea 2024-01-20 op imsg_close(imsgbuf, wbuf);
254 b0003f58 2021-03-08 op
255 b0003f58 2021-03-08 op return (1);
256 b0003f58 2021-03-08 op }
257 b0003f58 2021-03-08 op
258 b0003f58 2021-03-08 op int
259 8623abea 2024-01-20 op imsg_composev(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
260 b0003f58 2021-03-08 op int fd, const struct iovec *iov, int iovcnt)
261 b0003f58 2021-03-08 op {
262 b0003f58 2021-03-08 op struct ibuf *wbuf;
263 8623abea 2024-01-20 op int i;
264 8623abea 2024-01-20 op size_t datalen = 0;
265 b0003f58 2021-03-08 op
266 b0003f58 2021-03-08 op for (i = 0; i < iovcnt; i++)
267 b0003f58 2021-03-08 op datalen += iov[i].iov_len;
268 b0003f58 2021-03-08 op
269 8623abea 2024-01-20 op if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
270 b0003f58 2021-03-08 op return (-1);
271 b0003f58 2021-03-08 op
272 b0003f58 2021-03-08 op for (i = 0; i < iovcnt; i++)
273 b0003f58 2021-03-08 op if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
274 b0003f58 2021-03-08 op return (-1);
275 b0003f58 2021-03-08 op
276 ef16f8f6 2023-07-02 op ibuf_fd_set(wbuf, fd);
277 8623abea 2024-01-20 op imsg_close(imsgbuf, wbuf);
278 b0003f58 2021-03-08 op
279 b0003f58 2021-03-08 op return (1);
280 b0003f58 2021-03-08 op }
281 b0003f58 2021-03-08 op
282 8623abea 2024-01-20 op /*
283 8623abea 2024-01-20 op * Enqueue imsg with payload from ibuf buf. fd passing is not possible
284 8623abea 2024-01-20 op * with this function.
285 8623abea 2024-01-20 op */
286 ef16f8f6 2023-07-02 op int
287 8623abea 2024-01-20 op imsg_compose_ibuf(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id,
288 ef16f8f6 2023-07-02 op pid_t pid, struct ibuf *buf)
289 ef16f8f6 2023-07-02 op {
290 8623abea 2024-01-20 op struct ibuf *hdrbuf = NULL;
291 ef16f8f6 2023-07-02 op struct imsg_hdr hdr;
292 ef16f8f6 2023-07-02 op int save_errno;
293 ef16f8f6 2023-07-02 op
294 ef16f8f6 2023-07-02 op if (ibuf_size(buf) + IMSG_HEADER_SIZE > MAX_IMSGSIZE) {
295 ef16f8f6 2023-07-02 op errno = ERANGE;
296 ef16f8f6 2023-07-02 op goto fail;
297 ef16f8f6 2023-07-02 op }
298 ef16f8f6 2023-07-02 op
299 ef16f8f6 2023-07-02 op hdr.type = type;
300 ef16f8f6 2023-07-02 op hdr.len = ibuf_size(buf) + IMSG_HEADER_SIZE;
301 ef16f8f6 2023-07-02 op hdr.flags = 0;
302 8623abea 2024-01-20 op hdr.peerid = id;
303 ef16f8f6 2023-07-02 op if ((hdr.pid = pid) == 0)
304 8623abea 2024-01-20 op hdr.pid = imsgbuf->pid;
305 ef16f8f6 2023-07-02 op
306 8623abea 2024-01-20 op if ((hdrbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
307 ef16f8f6 2023-07-02 op goto fail;
308 8623abea 2024-01-20 op if (imsg_add(hdrbuf, &hdr, sizeof(hdr)) == -1)
309 ef16f8f6 2023-07-02 op goto fail;
310 ef16f8f6 2023-07-02 op
311 8623abea 2024-01-20 op ibuf_close(&imsgbuf->w, hdrbuf);
312 8623abea 2024-01-20 op ibuf_close(&imsgbuf->w, buf);
313 ef16f8f6 2023-07-02 op return (1);
314 ef16f8f6 2023-07-02 op
315 ef16f8f6 2023-07-02 op fail:
316 ef16f8f6 2023-07-02 op save_errno = errno;
317 ef16f8f6 2023-07-02 op ibuf_free(buf);
318 8623abea 2024-01-20 op ibuf_free(hdrbuf);
319 ef16f8f6 2023-07-02 op errno = save_errno;
320 ef16f8f6 2023-07-02 op return (-1);
321 ef16f8f6 2023-07-02 op }
322 ef16f8f6 2023-07-02 op
323 8623abea 2024-01-20 op /*
324 8623abea 2024-01-20 op * Forward imsg to another channel. Any attached fd is closed.
325 8623abea 2024-01-20 op */
326 8623abea 2024-01-20 op int
327 8623abea 2024-01-20 op imsg_forward(struct imsgbuf *imsgbuf, struct imsg *msg)
328 8623abea 2024-01-20 op {
329 8623abea 2024-01-20 op struct ibuf *wbuf;
330 8623abea 2024-01-20 op size_t len = 0;
331 8623abea 2024-01-20 op
332 8623abea 2024-01-20 op if (msg->fd != -1) {
333 8623abea 2024-01-20 op close(msg->fd);
334 8623abea 2024-01-20 op msg->fd = -1;
335 8623abea 2024-01-20 op }
336 8623abea 2024-01-20 op
337 8623abea 2024-01-20 op if (msg->buf != NULL) {
338 8623abea 2024-01-20 op ibuf_rewind(msg->buf);
339 8623abea 2024-01-20 op len = ibuf_size(msg->buf);
340 8623abea 2024-01-20 op }
341 8623abea 2024-01-20 op
342 8623abea 2024-01-20 op if ((wbuf = imsg_create(imsgbuf, msg->hdr.type, msg->hdr.peerid,
343 8623abea 2024-01-20 op msg->hdr.pid, len)) == NULL)
344 8623abea 2024-01-20 op return (-1);
345 8623abea 2024-01-20 op
346 8623abea 2024-01-20 op if (msg->buf != NULL) {
347 8623abea 2024-01-20 op if (ibuf_add_buf(wbuf, msg->buf) == -1) {
348 8623abea 2024-01-20 op ibuf_free(wbuf);
349 8623abea 2024-01-20 op return (-1);
350 8623abea 2024-01-20 op }
351 8623abea 2024-01-20 op }
352 8623abea 2024-01-20 op
353 8623abea 2024-01-20 op imsg_close(imsgbuf, wbuf);
354 8623abea 2024-01-20 op return (1);
355 8623abea 2024-01-20 op }
356 8623abea 2024-01-20 op
357 b0003f58 2021-03-08 op struct ibuf *
358 8623abea 2024-01-20 op imsg_create(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
359 8623abea 2024-01-20 op size_t datalen)
360 b0003f58 2021-03-08 op {
361 b0003f58 2021-03-08 op struct ibuf *wbuf;
362 b0003f58 2021-03-08 op struct imsg_hdr hdr;
363 b0003f58 2021-03-08 op
364 b0003f58 2021-03-08 op datalen += IMSG_HEADER_SIZE;
365 b0003f58 2021-03-08 op if (datalen > MAX_IMSGSIZE) {
366 b0003f58 2021-03-08 op errno = ERANGE;
367 b0003f58 2021-03-08 op return (NULL);
368 b0003f58 2021-03-08 op }
369 b0003f58 2021-03-08 op
370 b0003f58 2021-03-08 op hdr.type = type;
371 b0003f58 2021-03-08 op hdr.flags = 0;
372 8623abea 2024-01-20 op hdr.peerid = id;
373 b0003f58 2021-03-08 op if ((hdr.pid = pid) == 0)
374 8623abea 2024-01-20 op hdr.pid = imsgbuf->pid;
375 b0003f58 2021-03-08 op if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
376 b0003f58 2021-03-08 op return (NULL);
377 b0003f58 2021-03-08 op }
378 b0003f58 2021-03-08 op if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
379 b0003f58 2021-03-08 op return (NULL);
380 b0003f58 2021-03-08 op
381 b0003f58 2021-03-08 op return (wbuf);
382 b0003f58 2021-03-08 op }
383 b0003f58 2021-03-08 op
384 b0003f58 2021-03-08 op int
385 8623abea 2024-01-20 op imsg_add(struct ibuf *msg, const void *data, size_t datalen)
386 b0003f58 2021-03-08 op {
387 b0003f58 2021-03-08 op if (datalen)
388 b0003f58 2021-03-08 op if (ibuf_add(msg, data, datalen) == -1) {
389 b0003f58 2021-03-08 op ibuf_free(msg);
390 b0003f58 2021-03-08 op return (-1);
391 b0003f58 2021-03-08 op }
392 b0003f58 2021-03-08 op return (datalen);
393 b0003f58 2021-03-08 op }
394 b0003f58 2021-03-08 op
395 b0003f58 2021-03-08 op void
396 8623abea 2024-01-20 op imsg_close(struct imsgbuf *imsgbuf, struct ibuf *msg)
397 b0003f58 2021-03-08 op {
398 b0003f58 2021-03-08 op struct imsg_hdr *hdr;
399 b0003f58 2021-03-08 op
400 b0003f58 2021-03-08 op hdr = (struct imsg_hdr *)msg->buf;
401 b0003f58 2021-03-08 op
402 b0003f58 2021-03-08 op hdr->flags &= ~IMSGF_HASFD;
403 ef16f8f6 2023-07-02 op if (ibuf_fd_avail(msg))
404 b0003f58 2021-03-08 op hdr->flags |= IMSGF_HASFD;
405 ef16f8f6 2023-07-02 op hdr->len = ibuf_size(msg);
406 b0003f58 2021-03-08 op
407 8623abea 2024-01-20 op ibuf_close(&imsgbuf->w, msg);
408 b0003f58 2021-03-08 op }
409 b0003f58 2021-03-08 op
410 b0003f58 2021-03-08 op void
411 b0003f58 2021-03-08 op imsg_free(struct imsg *imsg)
412 b0003f58 2021-03-08 op {
413 8623abea 2024-01-20 op ibuf_free(imsg->buf);
414 b0003f58 2021-03-08 op }
415 b0003f58 2021-03-08 op
416 b0003f58 2021-03-08 op static int
417 8623abea 2024-01-20 op imsg_dequeue_fd(struct imsgbuf *imsgbuf)
418 b0003f58 2021-03-08 op {
419 b0003f58 2021-03-08 op int fd;
420 b0003f58 2021-03-08 op struct imsg_fd *ifd;
421 b0003f58 2021-03-08 op
422 8623abea 2024-01-20 op if ((ifd = TAILQ_FIRST(&imsgbuf->fds)) == NULL)
423 b0003f58 2021-03-08 op return (-1);
424 b0003f58 2021-03-08 op
425 b0003f58 2021-03-08 op fd = ifd->fd;
426 8623abea 2024-01-20 op TAILQ_REMOVE(&imsgbuf->fds, ifd, entry);
427 b0003f58 2021-03-08 op free(ifd);
428 b0003f58 2021-03-08 op
429 b0003f58 2021-03-08 op return (fd);
430 b0003f58 2021-03-08 op }
431 b0003f58 2021-03-08 op
432 b0003f58 2021-03-08 op int
433 8623abea 2024-01-20 op imsg_flush(struct imsgbuf *imsgbuf)
434 b0003f58 2021-03-08 op {
435 8623abea 2024-01-20 op while (imsgbuf->w.queued)
436 8623abea 2024-01-20 op if (msgbuf_write(&imsgbuf->w) <= 0)
437 b0003f58 2021-03-08 op return (-1);
438 b0003f58 2021-03-08 op return (0);
439 b0003f58 2021-03-08 op }
440 b0003f58 2021-03-08 op
441 b0003f58 2021-03-08 op void
442 8623abea 2024-01-20 op imsg_clear(struct imsgbuf *imsgbuf)
443 b0003f58 2021-03-08 op {
444 b0003f58 2021-03-08 op int fd;
445 b0003f58 2021-03-08 op
446 8623abea 2024-01-20 op msgbuf_clear(&imsgbuf->w);
447 8623abea 2024-01-20 op while ((fd = imsg_dequeue_fd(imsgbuf)) != -1)
448 b0003f58 2021-03-08 op close(fd);
449 b0003f58 2021-03-08 op }