Blame


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