Blame


1 894e9984 2022-03-19 op /* $OpenBSD: imsg-buffer.c,v 1.13 2021/03/31 17:42:24 eric Exp $ */
2 6b191ed5 2021-02-23 op
3 6b191ed5 2021-02-23 op /*
4 6b191ed5 2021-02-23 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5 6b191ed5 2021-02-23 op *
6 6b191ed5 2021-02-23 op * Permission to use, copy, modify, and distribute this software for any
7 6b191ed5 2021-02-23 op * purpose with or without fee is hereby granted, provided that the above
8 6b191ed5 2021-02-23 op * copyright notice and this permission notice appear in all copies.
9 6b191ed5 2021-02-23 op *
10 6b191ed5 2021-02-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 6b191ed5 2021-02-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 6b191ed5 2021-02-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 6b191ed5 2021-02-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 6b191ed5 2021-02-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 6b191ed5 2021-02-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 6b191ed5 2021-02-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 6b191ed5 2021-02-23 op */
18 6b191ed5 2021-02-23 op
19 6b191ed5 2021-02-23 op #include "../config.h"
20 6b191ed5 2021-02-23 op
21 6b191ed5 2021-02-23 op #include <sys/types.h>
22 6b191ed5 2021-02-23 op #include <sys/socket.h>
23 6b191ed5 2021-02-23 op #include <sys/uio.h>
24 6b191ed5 2021-02-23 op
25 6b191ed5 2021-02-23 op #include <limits.h>
26 6b191ed5 2021-02-23 op #include <errno.h>
27 6b191ed5 2021-02-23 op #include <stdlib.h>
28 6b191ed5 2021-02-23 op #include <string.h>
29 6b191ed5 2021-02-23 op #include <unistd.h>
30 6b191ed5 2021-02-23 op
31 6b191ed5 2021-02-23 op #include "imsg.h"
32 6b191ed5 2021-02-23 op
33 6b191ed5 2021-02-23 op static int ibuf_realloc(struct ibuf *, size_t);
34 6b191ed5 2021-02-23 op static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
35 6b191ed5 2021-02-23 op static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
36 6b191ed5 2021-02-23 op
37 6b191ed5 2021-02-23 op struct ibuf *
38 6b191ed5 2021-02-23 op ibuf_open(size_t len)
39 6b191ed5 2021-02-23 op {
40 6b191ed5 2021-02-23 op struct ibuf *buf;
41 6b191ed5 2021-02-23 op
42 6b191ed5 2021-02-23 op if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
43 6b191ed5 2021-02-23 op return (NULL);
44 6b191ed5 2021-02-23 op if ((buf->buf = malloc(len)) == NULL) {
45 6b191ed5 2021-02-23 op free(buf);
46 6b191ed5 2021-02-23 op return (NULL);
47 6b191ed5 2021-02-23 op }
48 6b191ed5 2021-02-23 op buf->size = buf->max = len;
49 6b191ed5 2021-02-23 op buf->fd = -1;
50 6b191ed5 2021-02-23 op
51 6b191ed5 2021-02-23 op return (buf);
52 6b191ed5 2021-02-23 op }
53 6b191ed5 2021-02-23 op
54 6b191ed5 2021-02-23 op struct ibuf *
55 6b191ed5 2021-02-23 op ibuf_dynamic(size_t len, size_t max)
56 6b191ed5 2021-02-23 op {
57 6b191ed5 2021-02-23 op struct ibuf *buf;
58 6b191ed5 2021-02-23 op
59 6b191ed5 2021-02-23 op if (max < len)
60 6b191ed5 2021-02-23 op return (NULL);
61 6b191ed5 2021-02-23 op
62 6b191ed5 2021-02-23 op if ((buf = ibuf_open(len)) == NULL)
63 6b191ed5 2021-02-23 op return (NULL);
64 6b191ed5 2021-02-23 op
65 6b191ed5 2021-02-23 op if (max > 0)
66 6b191ed5 2021-02-23 op buf->max = max;
67 6b191ed5 2021-02-23 op
68 6b191ed5 2021-02-23 op return (buf);
69 6b191ed5 2021-02-23 op }
70 6b191ed5 2021-02-23 op
71 6b191ed5 2021-02-23 op static int
72 6b191ed5 2021-02-23 op ibuf_realloc(struct ibuf *buf, size_t len)
73 6b191ed5 2021-02-23 op {
74 6b191ed5 2021-02-23 op unsigned char *b;
75 6b191ed5 2021-02-23 op
76 6b191ed5 2021-02-23 op /* on static buffers max is eq size and so the following fails */
77 6b191ed5 2021-02-23 op if (buf->wpos + len > buf->max) {
78 6b191ed5 2021-02-23 op errno = ERANGE;
79 6b191ed5 2021-02-23 op return (-1);
80 6b191ed5 2021-02-23 op }
81 6b191ed5 2021-02-23 op
82 6b191ed5 2021-02-23 op b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
83 6b191ed5 2021-02-23 op if (b == NULL)
84 6b191ed5 2021-02-23 op return (-1);
85 6b191ed5 2021-02-23 op buf->buf = b;
86 6b191ed5 2021-02-23 op buf->size = buf->wpos + len;
87 6b191ed5 2021-02-23 op
88 6b191ed5 2021-02-23 op return (0);
89 6b191ed5 2021-02-23 op }
90 6b191ed5 2021-02-23 op
91 6b191ed5 2021-02-23 op int
92 6b191ed5 2021-02-23 op ibuf_add(struct ibuf *buf, const void *data, size_t len)
93 6b191ed5 2021-02-23 op {
94 6b191ed5 2021-02-23 op if (buf->wpos + len > buf->size)
95 6b191ed5 2021-02-23 op if (ibuf_realloc(buf, len) == -1)
96 6b191ed5 2021-02-23 op return (-1);
97 6b191ed5 2021-02-23 op
98 6b191ed5 2021-02-23 op memcpy(buf->buf + buf->wpos, data, len);
99 6b191ed5 2021-02-23 op buf->wpos += len;
100 6b191ed5 2021-02-23 op return (0);
101 6b191ed5 2021-02-23 op }
102 6b191ed5 2021-02-23 op
103 6b191ed5 2021-02-23 op void *
104 6b191ed5 2021-02-23 op ibuf_reserve(struct ibuf *buf, size_t len)
105 6b191ed5 2021-02-23 op {
106 6b191ed5 2021-02-23 op void *b;
107 6b191ed5 2021-02-23 op
108 6b191ed5 2021-02-23 op if (buf->wpos + len > buf->size)
109 6b191ed5 2021-02-23 op if (ibuf_realloc(buf, len) == -1)
110 6b191ed5 2021-02-23 op return (NULL);
111 6b191ed5 2021-02-23 op
112 6b191ed5 2021-02-23 op b = buf->buf + buf->wpos;
113 6b191ed5 2021-02-23 op buf->wpos += len;
114 6b191ed5 2021-02-23 op return (b);
115 6b191ed5 2021-02-23 op }
116 6b191ed5 2021-02-23 op
117 6b191ed5 2021-02-23 op void *
118 6b191ed5 2021-02-23 op ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
119 6b191ed5 2021-02-23 op {
120 6b191ed5 2021-02-23 op /* only allowed to seek in already written parts */
121 6b191ed5 2021-02-23 op if (pos + len > buf->wpos)
122 6b191ed5 2021-02-23 op return (NULL);
123 6b191ed5 2021-02-23 op
124 6b191ed5 2021-02-23 op return (buf->buf + pos);
125 6b191ed5 2021-02-23 op }
126 6b191ed5 2021-02-23 op
127 6b191ed5 2021-02-23 op size_t
128 6b191ed5 2021-02-23 op ibuf_size(struct ibuf *buf)
129 6b191ed5 2021-02-23 op {
130 6b191ed5 2021-02-23 op return (buf->wpos);
131 6b191ed5 2021-02-23 op }
132 6b191ed5 2021-02-23 op
133 6b191ed5 2021-02-23 op size_t
134 6b191ed5 2021-02-23 op ibuf_left(struct ibuf *buf)
135 6b191ed5 2021-02-23 op {
136 6b191ed5 2021-02-23 op return (buf->max - buf->wpos);
137 6b191ed5 2021-02-23 op }
138 6b191ed5 2021-02-23 op
139 6b191ed5 2021-02-23 op void
140 6b191ed5 2021-02-23 op ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
141 6b191ed5 2021-02-23 op {
142 6b191ed5 2021-02-23 op ibuf_enqueue(msgbuf, buf);
143 6b191ed5 2021-02-23 op }
144 6b191ed5 2021-02-23 op
145 6b191ed5 2021-02-23 op int
146 6b191ed5 2021-02-23 op ibuf_write(struct msgbuf *msgbuf)
147 6b191ed5 2021-02-23 op {
148 6b191ed5 2021-02-23 op struct iovec iov[IOV_MAX];
149 6b191ed5 2021-02-23 op struct ibuf *buf;
150 6b191ed5 2021-02-23 op unsigned int i = 0;
151 6b191ed5 2021-02-23 op ssize_t n;
152 6b191ed5 2021-02-23 op
153 6b191ed5 2021-02-23 op memset(&iov, 0, sizeof(iov));
154 6b191ed5 2021-02-23 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
155 6b191ed5 2021-02-23 op if (i >= IOV_MAX)
156 6b191ed5 2021-02-23 op break;
157 6b191ed5 2021-02-23 op iov[i].iov_base = buf->buf + buf->rpos;
158 6b191ed5 2021-02-23 op iov[i].iov_len = buf->wpos - buf->rpos;
159 6b191ed5 2021-02-23 op i++;
160 6b191ed5 2021-02-23 op }
161 6b191ed5 2021-02-23 op
162 6b191ed5 2021-02-23 op again:
163 6b191ed5 2021-02-23 op if ((n = writev(msgbuf->fd, iov, i)) == -1) {
164 6b191ed5 2021-02-23 op if (errno == EINTR)
165 6b191ed5 2021-02-23 op goto again;
166 6b191ed5 2021-02-23 op if (errno == ENOBUFS)
167 6b191ed5 2021-02-23 op errno = EAGAIN;
168 6b191ed5 2021-02-23 op return (-1);
169 6b191ed5 2021-02-23 op }
170 6b191ed5 2021-02-23 op
171 6b191ed5 2021-02-23 op if (n == 0) { /* connection closed */
172 6b191ed5 2021-02-23 op errno = 0;
173 6b191ed5 2021-02-23 op return (0);
174 6b191ed5 2021-02-23 op }
175 6b191ed5 2021-02-23 op
176 6b191ed5 2021-02-23 op msgbuf_drain(msgbuf, n);
177 6b191ed5 2021-02-23 op
178 6b191ed5 2021-02-23 op return (1);
179 6b191ed5 2021-02-23 op }
180 6b191ed5 2021-02-23 op
181 6b191ed5 2021-02-23 op void
182 6b191ed5 2021-02-23 op ibuf_free(struct ibuf *buf)
183 6b191ed5 2021-02-23 op {
184 6b191ed5 2021-02-23 op if (buf == NULL)
185 6b191ed5 2021-02-23 op return;
186 6b191ed5 2021-02-23 op freezero(buf->buf, buf->size);
187 6b191ed5 2021-02-23 op free(buf);
188 6b191ed5 2021-02-23 op }
189 6b191ed5 2021-02-23 op
190 6b191ed5 2021-02-23 op void
191 6b191ed5 2021-02-23 op msgbuf_init(struct msgbuf *msgbuf)
192 6b191ed5 2021-02-23 op {
193 6b191ed5 2021-02-23 op msgbuf->queued = 0;
194 6b191ed5 2021-02-23 op msgbuf->fd = -1;
195 6b191ed5 2021-02-23 op TAILQ_INIT(&msgbuf->bufs);
196 6b191ed5 2021-02-23 op }
197 6b191ed5 2021-02-23 op
198 6b191ed5 2021-02-23 op void
199 6b191ed5 2021-02-23 op msgbuf_drain(struct msgbuf *msgbuf, size_t n)
200 6b191ed5 2021-02-23 op {
201 6b191ed5 2021-02-23 op struct ibuf *buf, *next;
202 6b191ed5 2021-02-23 op
203 6b191ed5 2021-02-23 op for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
204 6b191ed5 2021-02-23 op buf = next) {
205 6b191ed5 2021-02-23 op next = TAILQ_NEXT(buf, entry);
206 6b191ed5 2021-02-23 op if (buf->rpos + n >= buf->wpos) {
207 6b191ed5 2021-02-23 op n -= buf->wpos - buf->rpos;
208 6b191ed5 2021-02-23 op ibuf_dequeue(msgbuf, buf);
209 6b191ed5 2021-02-23 op } else {
210 6b191ed5 2021-02-23 op buf->rpos += n;
211 6b191ed5 2021-02-23 op n = 0;
212 6b191ed5 2021-02-23 op }
213 6b191ed5 2021-02-23 op }
214 6b191ed5 2021-02-23 op }
215 6b191ed5 2021-02-23 op
216 6b191ed5 2021-02-23 op void
217 6b191ed5 2021-02-23 op msgbuf_clear(struct msgbuf *msgbuf)
218 6b191ed5 2021-02-23 op {
219 6b191ed5 2021-02-23 op struct ibuf *buf;
220 6b191ed5 2021-02-23 op
221 6b191ed5 2021-02-23 op while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
222 6b191ed5 2021-02-23 op ibuf_dequeue(msgbuf, buf);
223 6b191ed5 2021-02-23 op }
224 6b191ed5 2021-02-23 op
225 6b191ed5 2021-02-23 op int
226 6b191ed5 2021-02-23 op msgbuf_write(struct msgbuf *msgbuf)
227 6b191ed5 2021-02-23 op {
228 6b191ed5 2021-02-23 op struct iovec iov[IOV_MAX];
229 894e9984 2022-03-19 op struct ibuf *buf, *buf0 = NULL;
230 6b191ed5 2021-02-23 op unsigned int i = 0;
231 6b191ed5 2021-02-23 op ssize_t n;
232 6b191ed5 2021-02-23 op struct msghdr msg;
233 6b191ed5 2021-02-23 op struct cmsghdr *cmsg;
234 6b191ed5 2021-02-23 op union {
235 6b191ed5 2021-02-23 op struct cmsghdr hdr;
236 6b191ed5 2021-02-23 op char buf[CMSG_SPACE(sizeof(int))];
237 6b191ed5 2021-02-23 op } cmsgbuf;
238 6b191ed5 2021-02-23 op
239 6b191ed5 2021-02-23 op memset(&iov, 0, sizeof(iov));
240 6b191ed5 2021-02-23 op memset(&msg, 0, sizeof(msg));
241 6b191ed5 2021-02-23 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
242 6b191ed5 2021-02-23 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
243 6b191ed5 2021-02-23 op if (i >= IOV_MAX)
244 6b191ed5 2021-02-23 op break;
245 894e9984 2022-03-19 op if (i > 0 && buf->fd != -1)
246 894e9984 2022-03-19 op break;
247 6b191ed5 2021-02-23 op iov[i].iov_base = buf->buf + buf->rpos;
248 6b191ed5 2021-02-23 op iov[i].iov_len = buf->wpos - buf->rpos;
249 6b191ed5 2021-02-23 op i++;
250 6b191ed5 2021-02-23 op if (buf->fd != -1)
251 894e9984 2022-03-19 op buf0 = buf;
252 6b191ed5 2021-02-23 op }
253 6b191ed5 2021-02-23 op
254 6b191ed5 2021-02-23 op msg.msg_iov = iov;
255 6b191ed5 2021-02-23 op msg.msg_iovlen = i;
256 6b191ed5 2021-02-23 op
257 894e9984 2022-03-19 op if (buf0 != NULL) {
258 6b191ed5 2021-02-23 op msg.msg_control = (caddr_t)&cmsgbuf.buf;
259 6b191ed5 2021-02-23 op msg.msg_controllen = sizeof(cmsgbuf.buf);
260 6b191ed5 2021-02-23 op cmsg = CMSG_FIRSTHDR(&msg);
261 6b191ed5 2021-02-23 op cmsg->cmsg_len = CMSG_LEN(sizeof(int));
262 6b191ed5 2021-02-23 op cmsg->cmsg_level = SOL_SOCKET;
263 6b191ed5 2021-02-23 op cmsg->cmsg_type = SCM_RIGHTS;
264 894e9984 2022-03-19 op *(int *)CMSG_DATA(cmsg) = buf0->fd;
265 6b191ed5 2021-02-23 op }
266 6b191ed5 2021-02-23 op
267 6b191ed5 2021-02-23 op again:
268 6b191ed5 2021-02-23 op if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
269 6b191ed5 2021-02-23 op if (errno == EINTR)
270 6b191ed5 2021-02-23 op goto again;
271 6b191ed5 2021-02-23 op if (errno == ENOBUFS)
272 6b191ed5 2021-02-23 op errno = EAGAIN;
273 6b191ed5 2021-02-23 op return (-1);
274 6b191ed5 2021-02-23 op }
275 6b191ed5 2021-02-23 op
276 6b191ed5 2021-02-23 op if (n == 0) { /* connection closed */
277 6b191ed5 2021-02-23 op errno = 0;
278 6b191ed5 2021-02-23 op return (0);
279 6b191ed5 2021-02-23 op }
280 6b191ed5 2021-02-23 op
281 6b191ed5 2021-02-23 op /*
282 6b191ed5 2021-02-23 op * assumption: fd got sent if sendmsg sent anything
283 6b191ed5 2021-02-23 op * this works because fds are passed one at a time
284 6b191ed5 2021-02-23 op */
285 894e9984 2022-03-19 op if (buf0 != NULL) {
286 894e9984 2022-03-19 op close(buf0->fd);
287 894e9984 2022-03-19 op buf0->fd = -1;
288 6b191ed5 2021-02-23 op }
289 6b191ed5 2021-02-23 op
290 6b191ed5 2021-02-23 op msgbuf_drain(msgbuf, n);
291 6b191ed5 2021-02-23 op
292 6b191ed5 2021-02-23 op return (1);
293 6b191ed5 2021-02-23 op }
294 6b191ed5 2021-02-23 op
295 6b191ed5 2021-02-23 op static void
296 6b191ed5 2021-02-23 op ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
297 6b191ed5 2021-02-23 op {
298 6b191ed5 2021-02-23 op TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
299 6b191ed5 2021-02-23 op msgbuf->queued++;
300 6b191ed5 2021-02-23 op }
301 6b191ed5 2021-02-23 op
302 6b191ed5 2021-02-23 op static void
303 6b191ed5 2021-02-23 op ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
304 6b191ed5 2021-02-23 op {
305 6b191ed5 2021-02-23 op TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
306 6b191ed5 2021-02-23 op
307 6b191ed5 2021-02-23 op if (buf->fd != -1)
308 6b191ed5 2021-02-23 op close(buf->fd);
309 6b191ed5 2021-02-23 op
310 6b191ed5 2021-02-23 op msgbuf->queued--;
311 6b191ed5 2021-02-23 op ibuf_free(buf);
312 6b191ed5 2021-02-23 op }