Blame


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