1 f229e009 2023-07-02 op /* $OpenBSD: imsg.c,v 1.19 2023/06/19 17:19:50 claudio Exp $ */
4 bbcba3ed 2022-01-10 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 bbcba3ed 2022-01-10 op * Permission to use, copy, modify, and distribute this software for any
7 bbcba3ed 2022-01-10 op * purpose with or without fee is hereby granted, provided that the above
8 bbcba3ed 2022-01-10 op * copyright notice and this permission notice appear in all copies.
10 bbcba3ed 2022-01-10 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 bbcba3ed 2022-01-10 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 bbcba3ed 2022-01-10 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 bbcba3ed 2022-01-10 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 bbcba3ed 2022-01-10 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 bbcba3ed 2022-01-10 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 bbcba3ed 2022-01-10 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 bbcba3ed 2022-01-10 op #include "compat.h"
21 bbcba3ed 2022-01-10 op #include <sys/types.h>
22 bbcba3ed 2022-01-10 op #include <sys/socket.h>
23 bbcba3ed 2022-01-10 op #include <sys/uio.h>
25 bbcba3ed 2022-01-10 op #include <errno.h>
26 bbcba3ed 2022-01-10 op #include <stdlib.h>
27 bbcba3ed 2022-01-10 op #include <string.h>
28 bbcba3ed 2022-01-10 op #include <unistd.h>
30 bbcba3ed 2022-01-10 op #include "imsg.h"
32 bbcba3ed 2022-01-10 op int imsg_fd_overhead = 0;
34 bbcba3ed 2022-01-10 op static int imsg_get_fd(struct imsgbuf *);
37 bbcba3ed 2022-01-10 op imsg_init(struct imsgbuf *ibuf, int fd)
39 bbcba3ed 2022-01-10 op msgbuf_init(&ibuf->w);
40 bbcba3ed 2022-01-10 op memset(&ibuf->r, 0, sizeof(ibuf->r));
41 bbcba3ed 2022-01-10 op ibuf->fd = fd;
42 bbcba3ed 2022-01-10 op ibuf->w.fd = fd;
43 bbcba3ed 2022-01-10 op ibuf->pid = getpid();
44 bbcba3ed 2022-01-10 op TAILQ_INIT(&ibuf->fds);
48 bbcba3ed 2022-01-10 op imsg_read(struct imsgbuf *ibuf)
50 bbcba3ed 2022-01-10 op struct msghdr msg;
51 bbcba3ed 2022-01-10 op struct cmsghdr *cmsg;
53 bbcba3ed 2022-01-10 op struct cmsghdr hdr;
54 bbcba3ed 2022-01-10 op char buf[CMSG_SPACE(sizeof(int) * 1)];
56 bbcba3ed 2022-01-10 op struct iovec iov;
57 bbcba3ed 2022-01-10 op ssize_t n = -1;
59 bbcba3ed 2022-01-10 op struct imsg_fd *ifd;
61 bbcba3ed 2022-01-10 op memset(&msg, 0, sizeof(msg));
62 bbcba3ed 2022-01-10 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
64 bbcba3ed 2022-01-10 op iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
65 bbcba3ed 2022-01-10 op iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
66 bbcba3ed 2022-01-10 op msg.msg_iov = &iov;
67 bbcba3ed 2022-01-10 op msg.msg_iovlen = 1;
68 bbcba3ed 2022-01-10 op msg.msg_control = &cmsgbuf.buf;
69 bbcba3ed 2022-01-10 op msg.msg_controllen = sizeof(cmsgbuf.buf);
71 bbcba3ed 2022-01-10 op if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
75 bbcba3ed 2022-01-10 op if (getdtablecount() + imsg_fd_overhead +
76 bbcba3ed 2022-01-10 op (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
77 bbcba3ed 2022-01-10 op >= getdtablesize()) {
78 bbcba3ed 2022-01-10 op errno = EAGAIN;
83 bbcba3ed 2022-01-10 op if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
84 bbcba3ed 2022-01-10 op if (errno == EINTR)
89 bbcba3ed 2022-01-10 op ibuf->r.wpos += n;
91 bbcba3ed 2022-01-10 op for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
92 bbcba3ed 2022-01-10 op cmsg = CMSG_NXTHDR(&msg, cmsg)) {
93 bbcba3ed 2022-01-10 op if (cmsg->cmsg_level == SOL_SOCKET &&
94 bbcba3ed 2022-01-10 op cmsg->cmsg_type == SCM_RIGHTS) {
99 bbcba3ed 2022-01-10 op * We only accept one file descriptor. Due to C
100 bbcba3ed 2022-01-10 op * padding rules, our control buffer might contain
101 bbcba3ed 2022-01-10 op * more than one fd, and we must close them.
103 bbcba3ed 2022-01-10 op j = ((char *)cmsg + cmsg->cmsg_len -
104 bbcba3ed 2022-01-10 op (char *)CMSG_DATA(cmsg)) / sizeof(int);
105 bbcba3ed 2022-01-10 op for (i = 0; i < j; i++) {
106 bbcba3ed 2022-01-10 op fd = ((int *)CMSG_DATA(cmsg))[i];
107 bbcba3ed 2022-01-10 op if (ifd != NULL) {
108 bbcba3ed 2022-01-10 op ifd->fd = fd;
109 bbcba3ed 2022-01-10 op TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
116 bbcba3ed 2022-01-10 op /* we do not handle other ctl data level */
125 bbcba3ed 2022-01-10 op imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
127 bbcba3ed 2022-01-10 op size_t av, left, datalen;
129 bbcba3ed 2022-01-10 op av = ibuf->r.wpos;
131 bbcba3ed 2022-01-10 op if (IMSG_HEADER_SIZE > av)
134 bbcba3ed 2022-01-10 op memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
135 bbcba3ed 2022-01-10 op if (imsg->hdr.len < IMSG_HEADER_SIZE ||
136 bbcba3ed 2022-01-10 op imsg->hdr.len > MAX_IMSGSIZE) {
137 bbcba3ed 2022-01-10 op errno = ERANGE;
140 bbcba3ed 2022-01-10 op if (imsg->hdr.len > av)
142 bbcba3ed 2022-01-10 op datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
143 bbcba3ed 2022-01-10 op ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
144 bbcba3ed 2022-01-10 op if (datalen == 0)
145 bbcba3ed 2022-01-10 op imsg->data = NULL;
146 bbcba3ed 2022-01-10 op else if ((imsg->data = malloc(datalen)) == NULL)
149 bbcba3ed 2022-01-10 op if (imsg->hdr.flags & IMSGF_HASFD)
150 bbcba3ed 2022-01-10 op imsg->fd = imsg_get_fd(ibuf);
152 bbcba3ed 2022-01-10 op imsg->fd = -1;
154 f6df3e85 2022-01-28 op if (datalen != 0)
155 f6df3e85 2022-01-28 op memcpy(imsg->data, ibuf->r.rptr, datalen);
157 bbcba3ed 2022-01-10 op if (imsg->hdr.len < av) {
158 bbcba3ed 2022-01-10 op left = av - imsg->hdr.len;
159 bbcba3ed 2022-01-10 op memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
160 bbcba3ed 2022-01-10 op ibuf->r.wpos = left;
162 bbcba3ed 2022-01-10 op ibuf->r.wpos = 0;
164 bbcba3ed 2022-01-10 op return (datalen + IMSG_HEADER_SIZE);
168 bbcba3ed 2022-01-10 op imsg_compose(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
169 bbcba3ed 2022-01-10 op int fd, const void *data, uint16_t datalen)
171 bbcba3ed 2022-01-10 op struct ibuf *wbuf;
173 bbcba3ed 2022-01-10 op if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
176 bbcba3ed 2022-01-10 op if (imsg_add(wbuf, data, datalen) == -1)
179 f229e009 2023-07-02 op ibuf_fd_set(wbuf, fd);
180 bbcba3ed 2022-01-10 op imsg_close(ibuf, wbuf);
186 bbcba3ed 2022-01-10 op imsg_composev(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
187 bbcba3ed 2022-01-10 op int fd, const struct iovec *iov, int iovcnt)
189 bbcba3ed 2022-01-10 op struct ibuf *wbuf;
190 bbcba3ed 2022-01-10 op int i, datalen = 0;
192 bbcba3ed 2022-01-10 op for (i = 0; i < iovcnt; i++)
193 bbcba3ed 2022-01-10 op datalen += iov[i].iov_len;
195 bbcba3ed 2022-01-10 op if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
198 bbcba3ed 2022-01-10 op for (i = 0; i < iovcnt; i++)
199 bbcba3ed 2022-01-10 op if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
202 f229e009 2023-07-02 op ibuf_fd_set(wbuf, fd);
203 bbcba3ed 2022-01-10 op imsg_close(ibuf, wbuf);
209 f229e009 2023-07-02 op imsg_compose_ibuf(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid,
210 f229e009 2023-07-02 op pid_t pid, struct ibuf *buf)
212 f229e009 2023-07-02 op struct ibuf *wbuf = NULL;
213 f229e009 2023-07-02 op struct imsg_hdr hdr;
214 f229e009 2023-07-02 op int save_errno;
216 f229e009 2023-07-02 op if (ibuf_size(buf) + IMSG_HEADER_SIZE > MAX_IMSGSIZE) {
217 f229e009 2023-07-02 op errno = ERANGE;
221 f229e009 2023-07-02 op hdr.type = type;
222 f229e009 2023-07-02 op hdr.len = ibuf_size(buf) + IMSG_HEADER_SIZE;
223 f229e009 2023-07-02 op hdr.flags = 0;
224 f229e009 2023-07-02 op hdr.peerid = peerid;
225 f229e009 2023-07-02 op if ((hdr.pid = pid) == 0)
226 f229e009 2023-07-02 op hdr.pid = ibuf->pid;
228 f229e009 2023-07-02 op if ((wbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
230 f229e009 2023-07-02 op if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
233 f229e009 2023-07-02 op ibuf_close(&ibuf->w, wbuf);
234 f229e009 2023-07-02 op ibuf_close(&ibuf->w, buf);
238 f229e009 2023-07-02 op save_errno = errno;
239 f229e009 2023-07-02 op ibuf_free(buf);
240 f229e009 2023-07-02 op ibuf_free(wbuf);
241 f229e009 2023-07-02 op errno = save_errno;
245 bbcba3ed 2022-01-10 op struct ibuf *
246 bbcba3ed 2022-01-10 op imsg_create(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
247 bbcba3ed 2022-01-10 op uint16_t datalen)
249 bbcba3ed 2022-01-10 op struct ibuf *wbuf;
250 bbcba3ed 2022-01-10 op struct imsg_hdr hdr;
252 bbcba3ed 2022-01-10 op datalen += IMSG_HEADER_SIZE;
253 bbcba3ed 2022-01-10 op if (datalen > MAX_IMSGSIZE) {
254 bbcba3ed 2022-01-10 op errno = ERANGE;
255 bbcba3ed 2022-01-10 op return (NULL);
258 bbcba3ed 2022-01-10 op hdr.type = type;
259 bbcba3ed 2022-01-10 op hdr.flags = 0;
260 bbcba3ed 2022-01-10 op hdr.peerid = peerid;
261 bbcba3ed 2022-01-10 op if ((hdr.pid = pid) == 0)
262 bbcba3ed 2022-01-10 op hdr.pid = ibuf->pid;
263 bbcba3ed 2022-01-10 op if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
264 bbcba3ed 2022-01-10 op return (NULL);
266 bbcba3ed 2022-01-10 op if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
267 bbcba3ed 2022-01-10 op return (NULL);
269 bbcba3ed 2022-01-10 op return (wbuf);
273 bbcba3ed 2022-01-10 op imsg_add(struct ibuf *msg, const void *data, uint16_t datalen)
276 bbcba3ed 2022-01-10 op if (ibuf_add(msg, data, datalen) == -1) {
277 bbcba3ed 2022-01-10 op ibuf_free(msg);
280 bbcba3ed 2022-01-10 op return (datalen);
284 bbcba3ed 2022-01-10 op imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
286 bbcba3ed 2022-01-10 op struct imsg_hdr *hdr;
288 bbcba3ed 2022-01-10 op hdr = (struct imsg_hdr *)msg->buf;
290 bbcba3ed 2022-01-10 op hdr->flags &= ~IMSGF_HASFD;
291 f229e009 2023-07-02 op if (ibuf_fd_avail(msg))
292 bbcba3ed 2022-01-10 op hdr->flags |= IMSGF_HASFD;
293 f229e009 2023-07-02 op hdr->len = ibuf_size(msg);
295 bbcba3ed 2022-01-10 op ibuf_close(&ibuf->w, msg);
299 bbcba3ed 2022-01-10 op imsg_free(struct imsg *imsg)
301 bbcba3ed 2022-01-10 op freezero(imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);
305 bbcba3ed 2022-01-10 op imsg_get_fd(struct imsgbuf *ibuf)
308 bbcba3ed 2022-01-10 op struct imsg_fd *ifd;
310 bbcba3ed 2022-01-10 op if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
313 bbcba3ed 2022-01-10 op fd = ifd->fd;
314 bbcba3ed 2022-01-10 op TAILQ_REMOVE(&ibuf->fds, ifd, entry);
321 bbcba3ed 2022-01-10 op imsg_flush(struct imsgbuf *ibuf)
323 bbcba3ed 2022-01-10 op while (ibuf->w.queued)
324 bbcba3ed 2022-01-10 op if (msgbuf_write(&ibuf->w) <= 0)
330 bbcba3ed 2022-01-10 op imsg_clear(struct imsgbuf *ibuf)
334 bbcba3ed 2022-01-10 op msgbuf_clear(&ibuf->w);
335 bbcba3ed 2022-01-10 op while ((fd = imsg_get_fd(ibuf)) != -1)