Blame


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