Blame


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