1 805e39bf 2021-07-22 op /* $OpenBSD: imsg.c,v 1.16 2017/12/14 09:27:44 kettenis Exp $ */
4 805e39bf 2021-07-22 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 805e39bf 2021-07-22 op * Permission to use, copy, modify, and distribute this software for any
7 805e39bf 2021-07-22 op * purpose with or without fee is hereby granted, provided that the above
8 805e39bf 2021-07-22 op * copyright notice and this permission notice appear in all copies.
10 805e39bf 2021-07-22 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 805e39bf 2021-07-22 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 805e39bf 2021-07-22 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 805e39bf 2021-07-22 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 805e39bf 2021-07-22 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 805e39bf 2021-07-22 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 805e39bf 2021-07-22 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 805e39bf 2021-07-22 op #include "compat.h"
21 805e39bf 2021-07-22 op #include <sys/types.h>
22 805e39bf 2021-07-22 op #include <sys/socket.h>
23 805e39bf 2021-07-22 op #include <sys/uio.h>
25 805e39bf 2021-07-22 op #include <errno.h>
26 805e39bf 2021-07-22 op #include <stdlib.h>
27 805e39bf 2021-07-22 op #include <string.h>
28 805e39bf 2021-07-22 op #include <unistd.h>
30 805e39bf 2021-07-22 op #include "imsg.h"
32 805e39bf 2021-07-22 op int imsg_fd_overhead = 0;
34 805e39bf 2021-07-22 op static int imsg_get_fd(struct imsgbuf *);
37 805e39bf 2021-07-22 op imsg_init(struct imsgbuf *ibuf, int fd)
39 805e39bf 2021-07-22 op msgbuf_init(&ibuf->w);
40 805e39bf 2021-07-22 op memset(&ibuf->r, 0, sizeof(ibuf->r));
41 805e39bf 2021-07-22 op ibuf->fd = fd;
42 805e39bf 2021-07-22 op ibuf->w.fd = fd;
43 805e39bf 2021-07-22 op ibuf->pid = getpid();
44 805e39bf 2021-07-22 op TAILQ_INIT(&ibuf->fds);
48 805e39bf 2021-07-22 op imsg_read(struct imsgbuf *ibuf)
50 805e39bf 2021-07-22 op struct msghdr msg;
51 805e39bf 2021-07-22 op struct cmsghdr *cmsg;
53 805e39bf 2021-07-22 op struct cmsghdr hdr;
54 805e39bf 2021-07-22 op char buf[CMSG_SPACE(sizeof(int) * 1)];
56 805e39bf 2021-07-22 op struct iovec iov;
57 805e39bf 2021-07-22 op ssize_t n = -1;
59 805e39bf 2021-07-22 op struct imsg_fd *ifd;
61 805e39bf 2021-07-22 op memset(&msg, 0, sizeof(msg));
62 805e39bf 2021-07-22 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
64 805e39bf 2021-07-22 op iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
65 805e39bf 2021-07-22 op iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
66 805e39bf 2021-07-22 op msg.msg_iov = &iov;
67 805e39bf 2021-07-22 op msg.msg_iovlen = 1;
68 805e39bf 2021-07-22 op msg.msg_control = &cmsgbuf.buf;
69 805e39bf 2021-07-22 op msg.msg_controllen = sizeof(cmsgbuf.buf);
71 805e39bf 2021-07-22 op if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
75 805e39bf 2021-07-22 op if (getdtablecount() + imsg_fd_overhead +
76 805e39bf 2021-07-22 op (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
77 805e39bf 2021-07-22 op >= getdtablesize()) {
78 805e39bf 2021-07-22 op errno = EAGAIN;
83 805e39bf 2021-07-22 op if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
84 805e39bf 2021-07-22 op if (errno == EINTR)
89 805e39bf 2021-07-22 op ibuf->r.wpos += n;
91 805e39bf 2021-07-22 op for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
92 805e39bf 2021-07-22 op cmsg = CMSG_NXTHDR(&msg, cmsg)) {
93 805e39bf 2021-07-22 op if (cmsg->cmsg_level == SOL_SOCKET &&
94 805e39bf 2021-07-22 op cmsg->cmsg_type == SCM_RIGHTS) {
99 805e39bf 2021-07-22 op * We only accept one file descriptor. Due to C
100 805e39bf 2021-07-22 op * padding rules, our control buffer might contain
101 805e39bf 2021-07-22 op * more than one fd, and we must close them.
103 805e39bf 2021-07-22 op j = ((char *)cmsg + cmsg->cmsg_len -
104 805e39bf 2021-07-22 op (char *)CMSG_DATA(cmsg)) / sizeof(int);
105 805e39bf 2021-07-22 op for (i = 0; i < j; i++) {
106 805e39bf 2021-07-22 op fd = ((int *)CMSG_DATA(cmsg))[i];
107 805e39bf 2021-07-22 op if (ifd != NULL) {
108 805e39bf 2021-07-22 op ifd->fd = fd;
109 805e39bf 2021-07-22 op TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
116 805e39bf 2021-07-22 op /* we do not handle other ctl data level */
125 805e39bf 2021-07-22 op imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
127 805e39bf 2021-07-22 op size_t av, left, datalen;
129 805e39bf 2021-07-22 op av = ibuf->r.wpos;
131 805e39bf 2021-07-22 op if (IMSG_HEADER_SIZE > av)
134 805e39bf 2021-07-22 op memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
135 805e39bf 2021-07-22 op if (imsg->hdr.len < IMSG_HEADER_SIZE ||
136 805e39bf 2021-07-22 op imsg->hdr.len > MAX_IMSGSIZE) {
137 805e39bf 2021-07-22 op errno = ERANGE;
140 805e39bf 2021-07-22 op if (imsg->hdr.len > av)
142 805e39bf 2021-07-22 op datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
143 805e39bf 2021-07-22 op ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
144 805e39bf 2021-07-22 op if (datalen == 0)
145 805e39bf 2021-07-22 op imsg->data = NULL;
146 805e39bf 2021-07-22 op else if ((imsg->data = malloc(datalen)) == NULL)
149 805e39bf 2021-07-22 op if (imsg->hdr.flags & IMSGF_HASFD)
150 805e39bf 2021-07-22 op imsg->fd = imsg_get_fd(ibuf);
152 805e39bf 2021-07-22 op imsg->fd = -1;
154 805e39bf 2021-07-22 op memcpy(imsg->data, ibuf->r.rptr, datalen);
156 805e39bf 2021-07-22 op if (imsg->hdr.len < av) {
157 805e39bf 2021-07-22 op left = av - imsg->hdr.len;
158 805e39bf 2021-07-22 op memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
159 805e39bf 2021-07-22 op ibuf->r.wpos = left;
161 805e39bf 2021-07-22 op ibuf->r.wpos = 0;
163 805e39bf 2021-07-22 op return (datalen + IMSG_HEADER_SIZE);
167 805e39bf 2021-07-22 op imsg_compose(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
168 805e39bf 2021-07-22 op int fd, const void *data, uint16_t datalen)
170 805e39bf 2021-07-22 op struct ibuf *wbuf;
172 805e39bf 2021-07-22 op if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
175 805e39bf 2021-07-22 op if (imsg_add(wbuf, data, datalen) == -1)
178 805e39bf 2021-07-22 op wbuf->fd = fd;
180 805e39bf 2021-07-22 op imsg_close(ibuf, wbuf);
186 805e39bf 2021-07-22 op imsg_composev(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
187 805e39bf 2021-07-22 op int fd, const struct iovec *iov, int iovcnt)
189 805e39bf 2021-07-22 op struct ibuf *wbuf;
190 805e39bf 2021-07-22 op int i, datalen = 0;
192 805e39bf 2021-07-22 op for (i = 0; i < iovcnt; i++)
193 805e39bf 2021-07-22 op datalen += iov[i].iov_len;
195 805e39bf 2021-07-22 op if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
198 805e39bf 2021-07-22 op for (i = 0; i < iovcnt; i++)
199 805e39bf 2021-07-22 op if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
202 805e39bf 2021-07-22 op wbuf->fd = fd;
204 805e39bf 2021-07-22 op imsg_close(ibuf, wbuf);
209 805e39bf 2021-07-22 op /* ARGSUSED */
210 805e39bf 2021-07-22 op struct ibuf *
211 805e39bf 2021-07-22 op imsg_create(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
212 805e39bf 2021-07-22 op uint16_t datalen)
214 805e39bf 2021-07-22 op struct ibuf *wbuf;
215 805e39bf 2021-07-22 op struct imsg_hdr hdr;
217 805e39bf 2021-07-22 op datalen += IMSG_HEADER_SIZE;
218 805e39bf 2021-07-22 op if (datalen > MAX_IMSGSIZE) {
219 805e39bf 2021-07-22 op errno = ERANGE;
220 805e39bf 2021-07-22 op return (NULL);
223 805e39bf 2021-07-22 op hdr.type = type;
224 805e39bf 2021-07-22 op hdr.flags = 0;
225 805e39bf 2021-07-22 op hdr.peerid = peerid;
226 805e39bf 2021-07-22 op if ((hdr.pid = pid) == 0)
227 805e39bf 2021-07-22 op hdr.pid = ibuf->pid;
228 805e39bf 2021-07-22 op if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
229 805e39bf 2021-07-22 op return (NULL);
231 805e39bf 2021-07-22 op if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
232 805e39bf 2021-07-22 op return (NULL);
234 805e39bf 2021-07-22 op return (wbuf);
238 805e39bf 2021-07-22 op imsg_add(struct ibuf *msg, const void *data, uint16_t datalen)
241 805e39bf 2021-07-22 op if (ibuf_add(msg, data, datalen) == -1) {
242 805e39bf 2021-07-22 op ibuf_free(msg);
245 805e39bf 2021-07-22 op return (datalen);
249 805e39bf 2021-07-22 op imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
251 805e39bf 2021-07-22 op struct imsg_hdr *hdr;
253 805e39bf 2021-07-22 op hdr = (struct imsg_hdr *)msg->buf;
255 805e39bf 2021-07-22 op hdr->flags &= ~IMSGF_HASFD;
256 805e39bf 2021-07-22 op if (msg->fd != -1)
257 805e39bf 2021-07-22 op hdr->flags |= IMSGF_HASFD;
259 805e39bf 2021-07-22 op hdr->len = (uint16_t)msg->wpos;
261 805e39bf 2021-07-22 op ibuf_close(&ibuf->w, msg);
265 805e39bf 2021-07-22 op imsg_free(struct imsg *imsg)
267 805e39bf 2021-07-22 op freezero(imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);
271 805e39bf 2021-07-22 op imsg_get_fd(struct imsgbuf *ibuf)
274 805e39bf 2021-07-22 op struct imsg_fd *ifd;
276 805e39bf 2021-07-22 op if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
279 805e39bf 2021-07-22 op fd = ifd->fd;
280 805e39bf 2021-07-22 op TAILQ_REMOVE(&ibuf->fds, ifd, entry);
287 805e39bf 2021-07-22 op imsg_flush(struct imsgbuf *ibuf)
289 805e39bf 2021-07-22 op while (ibuf->w.queued)
290 805e39bf 2021-07-22 op if (msgbuf_write(&ibuf->w) <= 0)
296 805e39bf 2021-07-22 op imsg_clear(struct imsgbuf *ibuf)
300 805e39bf 2021-07-22 op msgbuf_clear(&ibuf->w);
301 805e39bf 2021-07-22 op while ((fd = imsg_get_fd(ibuf)) != -1)