Blame


1 3bb9eb8b 2024-01-18 thomas /* $OpenBSD: imsg.c,v 1.23 2023/12/12 15:47:41 claudio Exp $ */
2 dd038bc6 2021-09-21 thomas.ad
3 dd038bc6 2021-09-21 thomas.ad /*
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>
6 dd038bc6 2021-09-21 thomas.ad *
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.
10 dd038bc6 2021-09-21 thomas.ad *
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.
18 dd038bc6 2021-09-21 thomas.ad */
19 dd038bc6 2021-09-21 thomas.ad
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>
23 dd038bc6 2021-09-21 thomas.ad
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>
28 dd038bc6 2021-09-21 thomas.ad
29 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
30 dd038bc6 2021-09-21 thomas.ad #include "imsg.h"
31 dd038bc6 2021-09-21 thomas.ad
32 3bb9eb8b 2024-01-18 thomas struct imsg_fd {
33 3bb9eb8b 2024-01-18 thomas TAILQ_ENTRY(imsg_fd) entry;
34 3bb9eb8b 2024-01-18 thomas int fd;
35 3bb9eb8b 2024-01-18 thomas };
36 3bb9eb8b 2024-01-18 thomas
37 dd038bc6 2021-09-21 thomas.ad int imsg_fd_overhead = 0;
38 dd038bc6 2021-09-21 thomas.ad
39 b69b73ef 2023-11-25 thomas static int imsg_dequeue_fd(struct imsgbuf *);
40 dd038bc6 2021-09-21 thomas.ad
41 dd038bc6 2021-09-21 thomas.ad void
42 b69b73ef 2023-11-25 thomas imsg_init(struct imsgbuf *imsgbuf, int fd)
43 dd038bc6 2021-09-21 thomas.ad {
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);
50 dd038bc6 2021-09-21 thomas.ad }
51 dd038bc6 2021-09-21 thomas.ad
52 dd038bc6 2021-09-21 thomas.ad ssize_t
53 b69b73ef 2023-11-25 thomas imsg_read(struct imsgbuf *imsgbuf)
54 dd038bc6 2021-09-21 thomas.ad {
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;
65 dd038bc6 2021-09-21 thomas.ad
66 dd038bc6 2021-09-21 thomas.ad memset(&msg, 0, sizeof(msg));
67 dd038bc6 2021-09-21 thomas.ad memset(&cmsgbuf, 0, sizeof(cmsgbuf));
68 dd038bc6 2021-09-21 thomas.ad
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);
75 dd038bc6 2021-09-21 thomas.ad
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);
78 dd038bc6 2021-09-21 thomas.ad
79 dd038bc6 2021-09-21 thomas.ad again:
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);
86 dd038bc6 2021-09-21 thomas.ad }
87 dd038bc6 2021-09-21 thomas.ad
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;
92 dd038bc6 2021-09-21 thomas.ad }
93 dd038bc6 2021-09-21 thomas.ad
94 b69b73ef 2023-11-25 thomas imsgbuf->r.wpos += n;
95 dd038bc6 2021-09-21 thomas.ad
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;
102 dd038bc6 2021-09-21 thomas.ad
103 dd038bc6 2021-09-21 thomas.ad /*
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.
107 dd038bc6 2021-09-21 thomas.ad */
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);
119 dd038bc6 2021-09-21 thomas.ad }
120 dd038bc6 2021-09-21 thomas.ad }
121 dd038bc6 2021-09-21 thomas.ad /* we do not handle other ctl data level */
122 dd038bc6 2021-09-21 thomas.ad }
123 dd038bc6 2021-09-21 thomas.ad
124 dd038bc6 2021-09-21 thomas.ad fail:
125 dd038bc6 2021-09-21 thomas.ad free(ifd);
126 dd038bc6 2021-09-21 thomas.ad return (n);
127 dd038bc6 2021-09-21 thomas.ad }
128 dd038bc6 2021-09-21 thomas.ad
129 dd038bc6 2021-09-21 thomas.ad ssize_t
130 b69b73ef 2023-11-25 thomas imsg_get(struct imsgbuf *imsgbuf, struct imsg *imsg)
131 dd038bc6 2021-09-21 thomas.ad {
132 3bb9eb8b 2024-01-18 thomas struct imsg m;
133 dd038bc6 2021-09-21 thomas.ad size_t av, left, datalen;
134 dd038bc6 2021-09-21 thomas.ad
135 b69b73ef 2023-11-25 thomas av = imsgbuf->r.wpos;
136 dd038bc6 2021-09-21 thomas.ad
137 dd038bc6 2021-09-21 thomas.ad if (IMSG_HEADER_SIZE > av)
138 dd038bc6 2021-09-21 thomas.ad return (0);
139 dd038bc6 2021-09-21 thomas.ad
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);
145 dd038bc6 2021-09-21 thomas.ad }
146 3bb9eb8b 2024-01-18 thomas if (m.hdr.len > av)
147 dd038bc6 2021-09-21 thomas.ad return (0);
148 dd038bc6 2021-09-21 thomas.ad
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;
152 dd038bc6 2021-09-21 thomas.ad
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);
162 3bb9eb8b 2024-01-18 thomas }
163 3bb9eb8b 2024-01-18 thomas m.data = ibuf_data(m.buf);
164 3bb9eb8b 2024-01-18 thomas }
165 dd038bc6 2021-09-21 thomas.ad
166 3bb9eb8b 2024-01-18 thomas if (m.hdr.flags & IMSGF_HASFD)
167 3bb9eb8b 2024-01-18 thomas m.fd = imsg_dequeue_fd(imsgbuf);
168 3bb9eb8b 2024-01-18 thomas
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;
175 dd038bc6 2021-09-21 thomas.ad
176 3bb9eb8b 2024-01-18 thomas *imsg = m;
177 dd038bc6 2021-09-21 thomas.ad return (datalen + IMSG_HEADER_SIZE);
178 3bb9eb8b 2024-01-18 thomas }
179 3bb9eb8b 2024-01-18 thomas
180 3bb9eb8b 2024-01-18 thomas int
181 3bb9eb8b 2024-01-18 thomas imsg_get_ibuf(struct imsg *imsg, struct ibuf *ibuf)
182 3bb9eb8b 2024-01-18 thomas {
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);
186 3bb9eb8b 2024-01-18 thomas }
187 3bb9eb8b 2024-01-18 thomas return ibuf_get_ibuf(imsg->buf, ibuf_size(imsg->buf), ibuf);
188 3bb9eb8b 2024-01-18 thomas }
189 3bb9eb8b 2024-01-18 thomas
190 3bb9eb8b 2024-01-18 thomas int
191 3bb9eb8b 2024-01-18 thomas imsg_get_data(struct imsg *imsg, void *data, size_t len)
192 3bb9eb8b 2024-01-18 thomas {
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);
196 3bb9eb8b 2024-01-18 thomas }
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);
200 3bb9eb8b 2024-01-18 thomas }
201 3bb9eb8b 2024-01-18 thomas return ibuf_get(imsg->buf, data, len);
202 3bb9eb8b 2024-01-18 thomas }
203 3bb9eb8b 2024-01-18 thomas
204 3bb9eb8b 2024-01-18 thomas int
205 3bb9eb8b 2024-01-18 thomas imsg_get_fd(struct imsg *imsg)
206 3bb9eb8b 2024-01-18 thomas {
207 3bb9eb8b 2024-01-18 thomas int fd = imsg->fd;
208 3bb9eb8b 2024-01-18 thomas
209 3bb9eb8b 2024-01-18 thomas imsg->fd = -1;
210 3bb9eb8b 2024-01-18 thomas return fd;
211 3bb9eb8b 2024-01-18 thomas }
212 3bb9eb8b 2024-01-18 thomas
213 3bb9eb8b 2024-01-18 thomas uint32_t
214 3bb9eb8b 2024-01-18 thomas imsg_get_id(struct imsg *imsg)
215 3bb9eb8b 2024-01-18 thomas {
216 3bb9eb8b 2024-01-18 thomas return (imsg->hdr.peerid);
217 dd038bc6 2021-09-21 thomas.ad }
218 dd038bc6 2021-09-21 thomas.ad
219 3bb9eb8b 2024-01-18 thomas size_t
220 3bb9eb8b 2024-01-18 thomas imsg_get_len(struct imsg *imsg)
221 3bb9eb8b 2024-01-18 thomas {
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);
225 3bb9eb8b 2024-01-18 thomas }
226 3bb9eb8b 2024-01-18 thomas
227 3bb9eb8b 2024-01-18 thomas pid_t
228 3bb9eb8b 2024-01-18 thomas imsg_get_pid(struct imsg *imsg)
229 3bb9eb8b 2024-01-18 thomas {
230 3bb9eb8b 2024-01-18 thomas return (imsg->hdr.pid);
231 3bb9eb8b 2024-01-18 thomas }
232 3bb9eb8b 2024-01-18 thomas
233 3bb9eb8b 2024-01-18 thomas uint32_t
234 3bb9eb8b 2024-01-18 thomas imsg_get_type(struct imsg *imsg)
235 3bb9eb8b 2024-01-18 thomas {
236 3bb9eb8b 2024-01-18 thomas return (imsg->hdr.type);
237 3bb9eb8b 2024-01-18 thomas }
238 3bb9eb8b 2024-01-18 thomas
239 dd038bc6 2021-09-21 thomas.ad int
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)
242 dd038bc6 2021-09-21 thomas.ad {
243 dd038bc6 2021-09-21 thomas.ad struct ibuf *wbuf;
244 dd038bc6 2021-09-21 thomas.ad
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);
247 dd038bc6 2021-09-21 thomas.ad
248 dd038bc6 2021-09-21 thomas.ad if (imsg_add(wbuf, data, datalen) == -1)
249 dd038bc6 2021-09-21 thomas.ad return (-1);
250 dd038bc6 2021-09-21 thomas.ad
251 3ef3f36a 2023-07-05 op ibuf_fd_set(wbuf, fd);
252 b69b73ef 2023-11-25 thomas imsg_close(imsgbuf, wbuf);
253 dd038bc6 2021-09-21 thomas.ad
254 dd038bc6 2021-09-21 thomas.ad return (1);
255 dd038bc6 2021-09-21 thomas.ad }
256 dd038bc6 2021-09-21 thomas.ad
257 dd038bc6 2021-09-21 thomas.ad int
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)
260 dd038bc6 2021-09-21 thomas.ad {
261 dd038bc6 2021-09-21 thomas.ad struct ibuf *wbuf;
262 3bb9eb8b 2024-01-18 thomas int i;
263 3bb9eb8b 2024-01-18 thomas size_t datalen = 0;
264 dd038bc6 2021-09-21 thomas.ad
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;
267 dd038bc6 2021-09-21 thomas.ad
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);
270 dd038bc6 2021-09-21 thomas.ad
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);
274 dd038bc6 2021-09-21 thomas.ad
275 3ef3f36a 2023-07-05 op ibuf_fd_set(wbuf, fd);
276 b69b73ef 2023-11-25 thomas imsg_close(imsgbuf, wbuf);
277 dd038bc6 2021-09-21 thomas.ad
278 dd038bc6 2021-09-21 thomas.ad return (1);
279 dd038bc6 2021-09-21 thomas.ad }
280 dd038bc6 2021-09-21 thomas.ad
281 3bb9eb8b 2024-01-18 thomas /*
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.
284 3bb9eb8b 2024-01-18 thomas */
285 3ef3f36a 2023-07-05 op int
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)
288 3ef3f36a 2023-07-05 op {
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;
292 3ef3f36a 2023-07-05 op
293 3ef3f36a 2023-07-05 op if (ibuf_size(buf) + IMSG_HEADER_SIZE > MAX_IMSGSIZE) {
294 3ef3f36a 2023-07-05 op errno = ERANGE;
295 3ef3f36a 2023-07-05 op goto fail;
296 3ef3f36a 2023-07-05 op }
297 3ef3f36a 2023-07-05 op
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;
304 3ef3f36a 2023-07-05 op
305 3bb9eb8b 2024-01-18 thomas if ((hdrbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
306 3ef3f36a 2023-07-05 op goto fail;
307 3bb9eb8b 2024-01-18 thomas if (imsg_add(hdrbuf, &hdr, sizeof(hdr)) == -1)
308 3ef3f36a 2023-07-05 op goto fail;
309 3ef3f36a 2023-07-05 op
310 3bb9eb8b 2024-01-18 thomas ibuf_close(&imsgbuf->w, hdrbuf);
311 b69b73ef 2023-11-25 thomas ibuf_close(&imsgbuf->w, buf);
312 3ef3f36a 2023-07-05 op return (1);
313 3ef3f36a 2023-07-05 op
314 3ef3f36a 2023-07-05 op fail:
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;
319 3ef3f36a 2023-07-05 op return (-1);
320 3ef3f36a 2023-07-05 op }
321 3ef3f36a 2023-07-05 op
322 3bb9eb8b 2024-01-18 thomas /*
323 3bb9eb8b 2024-01-18 thomas * Forward imsg to another channel. Any attached fd is closed.
324 3bb9eb8b 2024-01-18 thomas */
325 3bb9eb8b 2024-01-18 thomas int
326 3bb9eb8b 2024-01-18 thomas imsg_forward(struct imsgbuf *imsgbuf, struct imsg *msg)
327 3bb9eb8b 2024-01-18 thomas {
328 3bb9eb8b 2024-01-18 thomas struct ibuf *wbuf;
329 3bb9eb8b 2024-01-18 thomas size_t len = 0;
330 3bb9eb8b 2024-01-18 thomas
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;
334 3bb9eb8b 2024-01-18 thomas }
335 3bb9eb8b 2024-01-18 thomas
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);
339 3bb9eb8b 2024-01-18 thomas }
340 3bb9eb8b 2024-01-18 thomas
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);
344 3bb9eb8b 2024-01-18 thomas
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);
349 3bb9eb8b 2024-01-18 thomas }
350 3bb9eb8b 2024-01-18 thomas }
351 3bb9eb8b 2024-01-18 thomas
352 3bb9eb8b 2024-01-18 thomas imsg_close(imsgbuf, wbuf);
353 3bb9eb8b 2024-01-18 thomas return (1);
354 3bb9eb8b 2024-01-18 thomas }
355 3bb9eb8b 2024-01-18 thomas
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)
359 dd038bc6 2021-09-21 thomas.ad {
360 dd038bc6 2021-09-21 thomas.ad struct ibuf *wbuf;
361 dd038bc6 2021-09-21 thomas.ad struct imsg_hdr hdr;
362 dd038bc6 2021-09-21 thomas.ad
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);
367 dd038bc6 2021-09-21 thomas.ad }
368 dd038bc6 2021-09-21 thomas.ad
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);
376 dd038bc6 2021-09-21 thomas.ad }
377 dd038bc6 2021-09-21 thomas.ad if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
378 dd038bc6 2021-09-21 thomas.ad return (NULL);
379 dd038bc6 2021-09-21 thomas.ad
380 dd038bc6 2021-09-21 thomas.ad return (wbuf);
381 dd038bc6 2021-09-21 thomas.ad }
382 dd038bc6 2021-09-21 thomas.ad
383 dd038bc6 2021-09-21 thomas.ad int
384 3bb9eb8b 2024-01-18 thomas imsg_add(struct ibuf *msg, const void *data, size_t datalen)
385 dd038bc6 2021-09-21 thomas.ad {
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);
390 dd038bc6 2021-09-21 thomas.ad }
391 dd038bc6 2021-09-21 thomas.ad return (datalen);
392 dd038bc6 2021-09-21 thomas.ad }
393 dd038bc6 2021-09-21 thomas.ad
394 dd038bc6 2021-09-21 thomas.ad void
395 b69b73ef 2023-11-25 thomas imsg_close(struct imsgbuf *imsgbuf, struct ibuf *msg)
396 dd038bc6 2021-09-21 thomas.ad {
397 dd038bc6 2021-09-21 thomas.ad struct imsg_hdr *hdr;
398 dd038bc6 2021-09-21 thomas.ad
399 dd038bc6 2021-09-21 thomas.ad hdr = (struct imsg_hdr *)msg->buf;
400 dd038bc6 2021-09-21 thomas.ad
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);
405 dd038bc6 2021-09-21 thomas.ad
406 b69b73ef 2023-11-25 thomas ibuf_close(&imsgbuf->w, msg);
407 dd038bc6 2021-09-21 thomas.ad }
408 dd038bc6 2021-09-21 thomas.ad
409 dd038bc6 2021-09-21 thomas.ad void
410 dd038bc6 2021-09-21 thomas.ad imsg_free(struct imsg *imsg)
411 dd038bc6 2021-09-21 thomas.ad {
412 3bb9eb8b 2024-01-18 thomas ibuf_free(imsg->buf);
413 dd038bc6 2021-09-21 thomas.ad }
414 dd038bc6 2021-09-21 thomas.ad
415 dd038bc6 2021-09-21 thomas.ad static int
416 b69b73ef 2023-11-25 thomas imsg_dequeue_fd(struct imsgbuf *imsgbuf)
417 dd038bc6 2021-09-21 thomas.ad {
418 dd038bc6 2021-09-21 thomas.ad int fd;
419 dd038bc6 2021-09-21 thomas.ad struct imsg_fd *ifd;
420 dd038bc6 2021-09-21 thomas.ad
421 b69b73ef 2023-11-25 thomas if ((ifd = TAILQ_FIRST(&imsgbuf->fds)) == NULL)
422 dd038bc6 2021-09-21 thomas.ad return (-1);
423 dd038bc6 2021-09-21 thomas.ad
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);
427 dd038bc6 2021-09-21 thomas.ad
428 dd038bc6 2021-09-21 thomas.ad return (fd);
429 dd038bc6 2021-09-21 thomas.ad }
430 dd038bc6 2021-09-21 thomas.ad
431 dd038bc6 2021-09-21 thomas.ad int
432 b69b73ef 2023-11-25 thomas imsg_flush(struct imsgbuf *imsgbuf)
433 dd038bc6 2021-09-21 thomas.ad {
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);
438 dd038bc6 2021-09-21 thomas.ad }
439 dd038bc6 2021-09-21 thomas.ad
440 dd038bc6 2021-09-21 thomas.ad void
441 b69b73ef 2023-11-25 thomas imsg_clear(struct imsgbuf *imsgbuf)
442 dd038bc6 2021-09-21 thomas.ad {
443 dd038bc6 2021-09-21 thomas.ad int fd;
444 dd038bc6 2021-09-21 thomas.ad
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);
448 dd038bc6 2021-09-21 thomas.ad }