1 dd038bc6 2021-09-21 thomas.ad /* $OpenBSD: imsg.c,v 1.16 2017/12/14 09:27:44 kettenis Exp $ */
4 dd038bc6 2021-09-21 thomas.ad * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 dd038bc6 2021-09-21 thomas.ad * Permission to use, copy, modify, and distribute this software for any
7 dd038bc6 2021-09-21 thomas.ad * purpose with or without fee is hereby granted, provided that the above
8 dd038bc6 2021-09-21 thomas.ad * copyright notice and this permission notice appear in all copies.
10 dd038bc6 2021-09-21 thomas.ad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 dd038bc6 2021-09-21 thomas.ad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 dd038bc6 2021-09-21 thomas.ad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 dd038bc6 2021-09-21 thomas.ad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 dd038bc6 2021-09-21 thomas.ad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 dd038bc6 2021-09-21 thomas.ad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 dd038bc6 2021-09-21 thomas.ad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 dd038bc6 2021-09-21 thomas.ad #include <sys/types.h>
20 dd038bc6 2021-09-21 thomas.ad #include <sys/socket.h>
21 dd038bc6 2021-09-21 thomas.ad #include <sys/uio.h>
23 dd038bc6 2021-09-21 thomas.ad #include <errno.h>
24 dd038bc6 2021-09-21 thomas.ad #include <stdlib.h>
25 dd038bc6 2021-09-21 thomas.ad #include <string.h>
26 dd038bc6 2021-09-21 thomas.ad #include <unistd.h>
28 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
29 dd038bc6 2021-09-21 thomas.ad #include "imsg.h"
31 dd038bc6 2021-09-21 thomas.ad int imsg_fd_overhead = 0;
33 dd038bc6 2021-09-21 thomas.ad static int imsg_get_fd(struct imsgbuf *);
36 dd038bc6 2021-09-21 thomas.ad imsg_init(struct imsgbuf *ibuf, int fd)
38 dd038bc6 2021-09-21 thomas.ad msgbuf_init(&ibuf->w);
39 dd038bc6 2021-09-21 thomas.ad memset(&ibuf->r, 0, sizeof(ibuf->r));
40 dd038bc6 2021-09-21 thomas.ad ibuf->fd = fd;
41 dd038bc6 2021-09-21 thomas.ad ibuf->w.fd = fd;
42 dd038bc6 2021-09-21 thomas.ad ibuf->pid = getpid();
43 dd038bc6 2021-09-21 thomas.ad TAILQ_INIT(&ibuf->fds);
46 dd038bc6 2021-09-21 thomas.ad ssize_t
47 dd038bc6 2021-09-21 thomas.ad imsg_read(struct imsgbuf *ibuf)
49 dd038bc6 2021-09-21 thomas.ad struct msghdr msg;
50 dd038bc6 2021-09-21 thomas.ad struct cmsghdr *cmsg;
51 dd038bc6 2021-09-21 thomas.ad union {
52 dd038bc6 2021-09-21 thomas.ad struct cmsghdr hdr;
53 dd038bc6 2021-09-21 thomas.ad char buf[CMSG_SPACE(sizeof(int) * 1)];
54 dd038bc6 2021-09-21 thomas.ad } cmsgbuf;
55 dd038bc6 2021-09-21 thomas.ad struct iovec iov;
56 dd038bc6 2021-09-21 thomas.ad ssize_t n = -1;
57 dd038bc6 2021-09-21 thomas.ad int fd;
58 dd038bc6 2021-09-21 thomas.ad struct imsg_fd *ifd;
60 dd038bc6 2021-09-21 thomas.ad memset(&msg, 0, sizeof(msg));
61 dd038bc6 2021-09-21 thomas.ad memset(&cmsgbuf, 0, sizeof(cmsgbuf));
63 dd038bc6 2021-09-21 thomas.ad iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
64 dd038bc6 2021-09-21 thomas.ad iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
65 dd038bc6 2021-09-21 thomas.ad msg.msg_iov = &iov;
66 dd038bc6 2021-09-21 thomas.ad msg.msg_iovlen = 1;
67 dd038bc6 2021-09-21 thomas.ad msg.msg_control = &cmsgbuf.buf;
68 dd038bc6 2021-09-21 thomas.ad msg.msg_controllen = sizeof(cmsgbuf.buf);
70 dd038bc6 2021-09-21 thomas.ad if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
71 dd038bc6 2021-09-21 thomas.ad return (-1);
74 dd038bc6 2021-09-21 thomas.ad if (getdtablecount() + imsg_fd_overhead +
75 dd038bc6 2021-09-21 thomas.ad (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
76 dd038bc6 2021-09-21 thomas.ad >= getdtablesize()) {
77 dd038bc6 2021-09-21 thomas.ad errno = EAGAIN;
78 dd038bc6 2021-09-21 thomas.ad free(ifd);
79 dd038bc6 2021-09-21 thomas.ad return (-1);
82 dd038bc6 2021-09-21 thomas.ad if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
83 dd038bc6 2021-09-21 thomas.ad if (errno == EINTR)
84 dd038bc6 2021-09-21 thomas.ad goto again;
85 dd038bc6 2021-09-21 thomas.ad goto fail;
88 dd038bc6 2021-09-21 thomas.ad ibuf->r.wpos += n;
90 dd038bc6 2021-09-21 thomas.ad for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
91 dd038bc6 2021-09-21 thomas.ad cmsg = CMSG_NXTHDR(&msg, cmsg)) {
92 dd038bc6 2021-09-21 thomas.ad if (cmsg->cmsg_level == SOL_SOCKET &&
93 dd038bc6 2021-09-21 thomas.ad cmsg->cmsg_type == SCM_RIGHTS) {
98 dd038bc6 2021-09-21 thomas.ad * We only accept one file descriptor. Due to C
99 dd038bc6 2021-09-21 thomas.ad * padding rules, our control buffer might contain
100 dd038bc6 2021-09-21 thomas.ad * more than one fd, and we must close them.
102 dd038bc6 2021-09-21 thomas.ad j = ((char *)cmsg + cmsg->cmsg_len -
103 dd038bc6 2021-09-21 thomas.ad (char *)CMSG_DATA(cmsg)) / sizeof(int);
104 dd038bc6 2021-09-21 thomas.ad for (i = 0; i < j; i++) {
105 dd038bc6 2021-09-21 thomas.ad fd = ((int *)CMSG_DATA(cmsg))[i];
106 dd038bc6 2021-09-21 thomas.ad if (ifd != NULL) {
107 dd038bc6 2021-09-21 thomas.ad ifd->fd = fd;
108 dd038bc6 2021-09-21 thomas.ad TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
109 dd038bc6 2021-09-21 thomas.ad entry);
110 dd038bc6 2021-09-21 thomas.ad ifd = NULL;
111 dd038bc6 2021-09-21 thomas.ad } else
112 dd038bc6 2021-09-21 thomas.ad close(fd);
115 dd038bc6 2021-09-21 thomas.ad /* we do not handle other ctl data level */
119 dd038bc6 2021-09-21 thomas.ad free(ifd);
120 dd038bc6 2021-09-21 thomas.ad return (n);
123 dd038bc6 2021-09-21 thomas.ad ssize_t
124 dd038bc6 2021-09-21 thomas.ad imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
126 dd038bc6 2021-09-21 thomas.ad size_t av, left, datalen;
128 dd038bc6 2021-09-21 thomas.ad av = ibuf->r.wpos;
130 dd038bc6 2021-09-21 thomas.ad if (IMSG_HEADER_SIZE > av)
131 dd038bc6 2021-09-21 thomas.ad return (0);
133 dd038bc6 2021-09-21 thomas.ad memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
134 dd038bc6 2021-09-21 thomas.ad if (imsg->hdr.len < IMSG_HEADER_SIZE ||
135 dd038bc6 2021-09-21 thomas.ad imsg->hdr.len > MAX_IMSGSIZE) {
136 dd038bc6 2021-09-21 thomas.ad errno = ERANGE;
137 dd038bc6 2021-09-21 thomas.ad return (-1);
139 dd038bc6 2021-09-21 thomas.ad if (imsg->hdr.len > av)
140 dd038bc6 2021-09-21 thomas.ad return (0);
141 dd038bc6 2021-09-21 thomas.ad datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
142 dd038bc6 2021-09-21 thomas.ad ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
143 dd038bc6 2021-09-21 thomas.ad if (datalen == 0)
144 dd038bc6 2021-09-21 thomas.ad imsg->data = NULL;
145 dd038bc6 2021-09-21 thomas.ad else if ((imsg->data = malloc(datalen)) == NULL)
146 dd038bc6 2021-09-21 thomas.ad return (-1);
148 dd038bc6 2021-09-21 thomas.ad if (imsg->hdr.flags & IMSGF_HASFD)
149 dd038bc6 2021-09-21 thomas.ad imsg->fd = imsg_get_fd(ibuf);
151 dd038bc6 2021-09-21 thomas.ad imsg->fd = -1;
153 dd038bc6 2021-09-21 thomas.ad memcpy(imsg->data, ibuf->r.rptr, datalen);
155 dd038bc6 2021-09-21 thomas.ad if (imsg->hdr.len < av) {
156 dd038bc6 2021-09-21 thomas.ad left = av - imsg->hdr.len;
157 dd038bc6 2021-09-21 thomas.ad memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
158 dd038bc6 2021-09-21 thomas.ad ibuf->r.wpos = left;
159 dd038bc6 2021-09-21 thomas.ad } else
160 dd038bc6 2021-09-21 thomas.ad ibuf->r.wpos = 0;
162 dd038bc6 2021-09-21 thomas.ad return (datalen + IMSG_HEADER_SIZE);
166 dd038bc6 2021-09-21 thomas.ad imsg_compose(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
167 dd038bc6 2021-09-21 thomas.ad int fd, const void *data, uint16_t datalen)
169 dd038bc6 2021-09-21 thomas.ad struct ibuf *wbuf;
171 dd038bc6 2021-09-21 thomas.ad if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
172 dd038bc6 2021-09-21 thomas.ad return (-1);
174 dd038bc6 2021-09-21 thomas.ad if (imsg_add(wbuf, data, datalen) == -1)
175 dd038bc6 2021-09-21 thomas.ad return (-1);
177 dd038bc6 2021-09-21 thomas.ad wbuf->fd = fd;
179 dd038bc6 2021-09-21 thomas.ad imsg_close(ibuf, wbuf);
181 dd038bc6 2021-09-21 thomas.ad return (1);
185 dd038bc6 2021-09-21 thomas.ad imsg_composev(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
186 dd038bc6 2021-09-21 thomas.ad int fd, const struct iovec *iov, int iovcnt)
188 dd038bc6 2021-09-21 thomas.ad struct ibuf *wbuf;
189 dd038bc6 2021-09-21 thomas.ad int i, datalen = 0;
191 dd038bc6 2021-09-21 thomas.ad for (i = 0; i < iovcnt; i++)
192 dd038bc6 2021-09-21 thomas.ad datalen += iov[i].iov_len;
194 dd038bc6 2021-09-21 thomas.ad if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
195 dd038bc6 2021-09-21 thomas.ad return (-1);
197 dd038bc6 2021-09-21 thomas.ad for (i = 0; i < iovcnt; i++)
198 dd038bc6 2021-09-21 thomas.ad if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
199 dd038bc6 2021-09-21 thomas.ad return (-1);
201 dd038bc6 2021-09-21 thomas.ad wbuf->fd = fd;
203 dd038bc6 2021-09-21 thomas.ad imsg_close(ibuf, wbuf);
205 dd038bc6 2021-09-21 thomas.ad return (1);
208 dd038bc6 2021-09-21 thomas.ad /* ARGSUSED */
209 dd038bc6 2021-09-21 thomas.ad struct ibuf *
210 dd038bc6 2021-09-21 thomas.ad imsg_create(struct imsgbuf *ibuf, uint32_t type, uint32_t peerid, pid_t pid,
211 dd038bc6 2021-09-21 thomas.ad uint16_t datalen)
213 dd038bc6 2021-09-21 thomas.ad struct ibuf *wbuf;
214 dd038bc6 2021-09-21 thomas.ad struct imsg_hdr hdr;
216 dd038bc6 2021-09-21 thomas.ad datalen += IMSG_HEADER_SIZE;
217 dd038bc6 2021-09-21 thomas.ad if (datalen > MAX_IMSGSIZE) {
218 dd038bc6 2021-09-21 thomas.ad errno = ERANGE;
219 dd038bc6 2021-09-21 thomas.ad return (NULL);
222 dd038bc6 2021-09-21 thomas.ad hdr.type = type;
223 dd038bc6 2021-09-21 thomas.ad hdr.flags = 0;
224 dd038bc6 2021-09-21 thomas.ad hdr.peerid = peerid;
225 dd038bc6 2021-09-21 thomas.ad if ((hdr.pid = pid) == 0)
226 dd038bc6 2021-09-21 thomas.ad hdr.pid = ibuf->pid;
227 dd038bc6 2021-09-21 thomas.ad if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
228 dd038bc6 2021-09-21 thomas.ad return (NULL);
230 dd038bc6 2021-09-21 thomas.ad if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
231 dd038bc6 2021-09-21 thomas.ad return (NULL);
233 dd038bc6 2021-09-21 thomas.ad return (wbuf);
237 dd038bc6 2021-09-21 thomas.ad imsg_add(struct ibuf *msg, const void *data, uint16_t datalen)
239 dd038bc6 2021-09-21 thomas.ad if (datalen)
240 dd038bc6 2021-09-21 thomas.ad if (ibuf_add(msg, data, datalen) == -1) {
241 dd038bc6 2021-09-21 thomas.ad ibuf_free(msg);
242 dd038bc6 2021-09-21 thomas.ad return (-1);
244 dd038bc6 2021-09-21 thomas.ad return (datalen);
248 dd038bc6 2021-09-21 thomas.ad imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
250 dd038bc6 2021-09-21 thomas.ad struct imsg_hdr *hdr;
252 dd038bc6 2021-09-21 thomas.ad hdr = (struct imsg_hdr *)msg->buf;
254 dd038bc6 2021-09-21 thomas.ad hdr->flags &= ~IMSGF_HASFD;
255 dd038bc6 2021-09-21 thomas.ad if (msg->fd != -1)
256 dd038bc6 2021-09-21 thomas.ad hdr->flags |= IMSGF_HASFD;
258 dd038bc6 2021-09-21 thomas.ad hdr->len = (uint16_t)msg->wpos;
260 dd038bc6 2021-09-21 thomas.ad ibuf_close(&ibuf->w, msg);
264 dd038bc6 2021-09-21 thomas.ad imsg_free(struct imsg *imsg)
266 dd038bc6 2021-09-21 thomas.ad freezero(imsg->data, imsg->hdr.len - IMSG_HEADER_SIZE);
269 dd038bc6 2021-09-21 thomas.ad static int
270 dd038bc6 2021-09-21 thomas.ad imsg_get_fd(struct imsgbuf *ibuf)
272 dd038bc6 2021-09-21 thomas.ad int fd;
273 dd038bc6 2021-09-21 thomas.ad struct imsg_fd *ifd;
275 dd038bc6 2021-09-21 thomas.ad if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
276 dd038bc6 2021-09-21 thomas.ad return (-1);
278 dd038bc6 2021-09-21 thomas.ad fd = ifd->fd;
279 dd038bc6 2021-09-21 thomas.ad TAILQ_REMOVE(&ibuf->fds, ifd, entry);
280 dd038bc6 2021-09-21 thomas.ad free(ifd);
282 dd038bc6 2021-09-21 thomas.ad return (fd);
286 dd038bc6 2021-09-21 thomas.ad imsg_flush(struct imsgbuf *ibuf)
288 dd038bc6 2021-09-21 thomas.ad while (ibuf->w.queued)
289 dd038bc6 2021-09-21 thomas.ad if (msgbuf_write(&ibuf->w) <= 0)
290 dd038bc6 2021-09-21 thomas.ad return (-1);
291 dd038bc6 2021-09-21 thomas.ad return (0);
295 dd038bc6 2021-09-21 thomas.ad imsg_clear(struct imsgbuf *ibuf)
297 dd038bc6 2021-09-21 thomas.ad int fd;
299 dd038bc6 2021-09-21 thomas.ad msgbuf_clear(&ibuf->w);
300 dd038bc6 2021-09-21 thomas.ad while ((fd = imsg_get_fd(ibuf)) != -1)
301 dd038bc6 2021-09-21 thomas.ad close(fd);