Blame


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