Blame


1 83f0f95a 2022-09-29 op /* $OpenBSD: imsg-buffer.c,v 1.14 2022/04/23 08:57:52 tobias 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 <limits.h>
25 83f0f95a 2022-09-29 op #include <errno.h>
26 83f0f95a 2022-09-29 op #include <stdlib.h>
27 83f0f95a 2022-09-29 op #include <string.h>
28 83f0f95a 2022-09-29 op #include <unistd.h>
29 83f0f95a 2022-09-29 op
30 83f0f95a 2022-09-29 op #include "imsg.h"
31 83f0f95a 2022-09-29 op
32 83f0f95a 2022-09-29 op static int ibuf_realloc(struct ibuf *, size_t);
33 83f0f95a 2022-09-29 op static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
34 83f0f95a 2022-09-29 op static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
35 83f0f95a 2022-09-29 op
36 83f0f95a 2022-09-29 op struct ibuf *
37 83f0f95a 2022-09-29 op ibuf_open(size_t len)
38 83f0f95a 2022-09-29 op {
39 83f0f95a 2022-09-29 op struct ibuf *buf;
40 83f0f95a 2022-09-29 op
41 83f0f95a 2022-09-29 op if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
42 83f0f95a 2022-09-29 op return (NULL);
43 83f0f95a 2022-09-29 op if ((buf->buf = malloc(len)) == NULL) {
44 83f0f95a 2022-09-29 op free(buf);
45 83f0f95a 2022-09-29 op return (NULL);
46 83f0f95a 2022-09-29 op }
47 83f0f95a 2022-09-29 op buf->size = buf->max = len;
48 83f0f95a 2022-09-29 op buf->fd = -1;
49 83f0f95a 2022-09-29 op
50 83f0f95a 2022-09-29 op return (buf);
51 83f0f95a 2022-09-29 op }
52 83f0f95a 2022-09-29 op
53 83f0f95a 2022-09-29 op struct ibuf *
54 83f0f95a 2022-09-29 op ibuf_dynamic(size_t len, size_t max)
55 83f0f95a 2022-09-29 op {
56 83f0f95a 2022-09-29 op struct ibuf *buf;
57 83f0f95a 2022-09-29 op
58 83f0f95a 2022-09-29 op if (max < len)
59 83f0f95a 2022-09-29 op return (NULL);
60 83f0f95a 2022-09-29 op
61 83f0f95a 2022-09-29 op if ((buf = ibuf_open(len)) == NULL)
62 83f0f95a 2022-09-29 op return (NULL);
63 83f0f95a 2022-09-29 op
64 83f0f95a 2022-09-29 op if (max > 0)
65 83f0f95a 2022-09-29 op buf->max = max;
66 83f0f95a 2022-09-29 op
67 83f0f95a 2022-09-29 op return (buf);
68 83f0f95a 2022-09-29 op }
69 83f0f95a 2022-09-29 op
70 83f0f95a 2022-09-29 op static int
71 83f0f95a 2022-09-29 op ibuf_realloc(struct ibuf *buf, size_t len)
72 83f0f95a 2022-09-29 op {
73 83f0f95a 2022-09-29 op unsigned char *b;
74 83f0f95a 2022-09-29 op
75 83f0f95a 2022-09-29 op /* on static buffers max is eq size and so the following fails */
76 83f0f95a 2022-09-29 op if (len > SIZE_MAX - buf->wpos || buf->wpos + len > buf->max) {
77 83f0f95a 2022-09-29 op errno = ERANGE;
78 83f0f95a 2022-09-29 op return (-1);
79 83f0f95a 2022-09-29 op }
80 83f0f95a 2022-09-29 op
81 83f0f95a 2022-09-29 op b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
82 83f0f95a 2022-09-29 op if (b == NULL)
83 83f0f95a 2022-09-29 op return (-1);
84 83f0f95a 2022-09-29 op buf->buf = b;
85 83f0f95a 2022-09-29 op buf->size = buf->wpos + len;
86 83f0f95a 2022-09-29 op
87 83f0f95a 2022-09-29 op return (0);
88 83f0f95a 2022-09-29 op }
89 83f0f95a 2022-09-29 op
90 83f0f95a 2022-09-29 op int
91 83f0f95a 2022-09-29 op ibuf_add(struct ibuf *buf, const void *data, size_t len)
92 83f0f95a 2022-09-29 op {
93 83f0f95a 2022-09-29 op if (len > SIZE_MAX - buf->wpos) {
94 83f0f95a 2022-09-29 op errno = ERANGE;
95 83f0f95a 2022-09-29 op return (-1);
96 83f0f95a 2022-09-29 op }
97 83f0f95a 2022-09-29 op
98 83f0f95a 2022-09-29 op if (buf->wpos + len > buf->size)
99 83f0f95a 2022-09-29 op if (ibuf_realloc(buf, len) == -1)
100 83f0f95a 2022-09-29 op return (-1);
101 83f0f95a 2022-09-29 op
102 83f0f95a 2022-09-29 op memcpy(buf->buf + buf->wpos, data, len);
103 83f0f95a 2022-09-29 op buf->wpos += len;
104 83f0f95a 2022-09-29 op return (0);
105 83f0f95a 2022-09-29 op }
106 83f0f95a 2022-09-29 op
107 83f0f95a 2022-09-29 op void *
108 83f0f95a 2022-09-29 op ibuf_reserve(struct ibuf *buf, size_t len)
109 83f0f95a 2022-09-29 op {
110 83f0f95a 2022-09-29 op void *b;
111 83f0f95a 2022-09-29 op
112 83f0f95a 2022-09-29 op if (len > SIZE_MAX - buf->wpos) {
113 83f0f95a 2022-09-29 op errno = ERANGE;
114 83f0f95a 2022-09-29 op return (NULL);
115 83f0f95a 2022-09-29 op }
116 83f0f95a 2022-09-29 op
117 83f0f95a 2022-09-29 op if (buf->wpos + len > buf->size)
118 83f0f95a 2022-09-29 op if (ibuf_realloc(buf, len) == -1)
119 83f0f95a 2022-09-29 op return (NULL);
120 83f0f95a 2022-09-29 op
121 83f0f95a 2022-09-29 op b = buf->buf + buf->wpos;
122 83f0f95a 2022-09-29 op buf->wpos += len;
123 83f0f95a 2022-09-29 op return (b);
124 83f0f95a 2022-09-29 op }
125 83f0f95a 2022-09-29 op
126 83f0f95a 2022-09-29 op void *
127 83f0f95a 2022-09-29 op ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
128 83f0f95a 2022-09-29 op {
129 83f0f95a 2022-09-29 op /* only allowed to seek in already written parts */
130 83f0f95a 2022-09-29 op if (len > SIZE_MAX - pos || pos + len > buf->wpos)
131 83f0f95a 2022-09-29 op return (NULL);
132 83f0f95a 2022-09-29 op
133 83f0f95a 2022-09-29 op return (buf->buf + pos);
134 83f0f95a 2022-09-29 op }
135 83f0f95a 2022-09-29 op
136 83f0f95a 2022-09-29 op size_t
137 83f0f95a 2022-09-29 op ibuf_size(struct ibuf *buf)
138 83f0f95a 2022-09-29 op {
139 83f0f95a 2022-09-29 op return (buf->wpos);
140 83f0f95a 2022-09-29 op }
141 83f0f95a 2022-09-29 op
142 83f0f95a 2022-09-29 op size_t
143 83f0f95a 2022-09-29 op ibuf_left(struct ibuf *buf)
144 83f0f95a 2022-09-29 op {
145 83f0f95a 2022-09-29 op return (buf->max - buf->wpos);
146 83f0f95a 2022-09-29 op }
147 83f0f95a 2022-09-29 op
148 83f0f95a 2022-09-29 op void
149 83f0f95a 2022-09-29 op ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
150 83f0f95a 2022-09-29 op {
151 83f0f95a 2022-09-29 op ibuf_enqueue(msgbuf, buf);
152 83f0f95a 2022-09-29 op }
153 83f0f95a 2022-09-29 op
154 83f0f95a 2022-09-29 op int
155 83f0f95a 2022-09-29 op ibuf_write(struct msgbuf *msgbuf)
156 83f0f95a 2022-09-29 op {
157 83f0f95a 2022-09-29 op struct iovec iov[IOV_MAX];
158 83f0f95a 2022-09-29 op struct ibuf *buf;
159 83f0f95a 2022-09-29 op unsigned int i = 0;
160 83f0f95a 2022-09-29 op ssize_t n;
161 83f0f95a 2022-09-29 op
162 83f0f95a 2022-09-29 op memset(&iov, 0, sizeof(iov));
163 83f0f95a 2022-09-29 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
164 83f0f95a 2022-09-29 op if (i >= IOV_MAX)
165 83f0f95a 2022-09-29 op break;
166 83f0f95a 2022-09-29 op iov[i].iov_base = buf->buf + buf->rpos;
167 83f0f95a 2022-09-29 op iov[i].iov_len = buf->wpos - buf->rpos;
168 83f0f95a 2022-09-29 op i++;
169 83f0f95a 2022-09-29 op }
170 83f0f95a 2022-09-29 op
171 83f0f95a 2022-09-29 op again:
172 83f0f95a 2022-09-29 op if ((n = writev(msgbuf->fd, iov, i)) == -1) {
173 83f0f95a 2022-09-29 op if (errno == EINTR)
174 83f0f95a 2022-09-29 op goto again;
175 83f0f95a 2022-09-29 op if (errno == ENOBUFS)
176 83f0f95a 2022-09-29 op errno = EAGAIN;
177 83f0f95a 2022-09-29 op return (-1);
178 83f0f95a 2022-09-29 op }
179 83f0f95a 2022-09-29 op
180 83f0f95a 2022-09-29 op if (n == 0) { /* connection closed */
181 83f0f95a 2022-09-29 op errno = 0;
182 83f0f95a 2022-09-29 op return (0);
183 83f0f95a 2022-09-29 op }
184 83f0f95a 2022-09-29 op
185 83f0f95a 2022-09-29 op msgbuf_drain(msgbuf, n);
186 83f0f95a 2022-09-29 op
187 83f0f95a 2022-09-29 op return (1);
188 83f0f95a 2022-09-29 op }
189 83f0f95a 2022-09-29 op
190 83f0f95a 2022-09-29 op void
191 83f0f95a 2022-09-29 op ibuf_free(struct ibuf *buf)
192 83f0f95a 2022-09-29 op {
193 83f0f95a 2022-09-29 op if (buf == NULL)
194 83f0f95a 2022-09-29 op return;
195 83f0f95a 2022-09-29 op freezero(buf->buf, buf->size);
196 83f0f95a 2022-09-29 op free(buf);
197 83f0f95a 2022-09-29 op }
198 83f0f95a 2022-09-29 op
199 83f0f95a 2022-09-29 op void
200 83f0f95a 2022-09-29 op msgbuf_init(struct msgbuf *msgbuf)
201 83f0f95a 2022-09-29 op {
202 83f0f95a 2022-09-29 op msgbuf->queued = 0;
203 83f0f95a 2022-09-29 op msgbuf->fd = -1;
204 83f0f95a 2022-09-29 op TAILQ_INIT(&msgbuf->bufs);
205 83f0f95a 2022-09-29 op }
206 83f0f95a 2022-09-29 op
207 83f0f95a 2022-09-29 op void
208 83f0f95a 2022-09-29 op msgbuf_drain(struct msgbuf *msgbuf, size_t n)
209 83f0f95a 2022-09-29 op {
210 83f0f95a 2022-09-29 op struct ibuf *buf, *next;
211 83f0f95a 2022-09-29 op
212 83f0f95a 2022-09-29 op for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
213 83f0f95a 2022-09-29 op buf = next) {
214 83f0f95a 2022-09-29 op next = TAILQ_NEXT(buf, entry);
215 83f0f95a 2022-09-29 op if (n >= buf->wpos - buf->rpos) {
216 83f0f95a 2022-09-29 op n -= buf->wpos - buf->rpos;
217 83f0f95a 2022-09-29 op ibuf_dequeue(msgbuf, buf);
218 83f0f95a 2022-09-29 op } else {
219 83f0f95a 2022-09-29 op buf->rpos += n;
220 83f0f95a 2022-09-29 op n = 0;
221 83f0f95a 2022-09-29 op }
222 83f0f95a 2022-09-29 op }
223 83f0f95a 2022-09-29 op }
224 83f0f95a 2022-09-29 op
225 83f0f95a 2022-09-29 op void
226 83f0f95a 2022-09-29 op msgbuf_clear(struct msgbuf *msgbuf)
227 83f0f95a 2022-09-29 op {
228 83f0f95a 2022-09-29 op struct ibuf *buf;
229 83f0f95a 2022-09-29 op
230 83f0f95a 2022-09-29 op while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
231 83f0f95a 2022-09-29 op ibuf_dequeue(msgbuf, buf);
232 83f0f95a 2022-09-29 op }
233 83f0f95a 2022-09-29 op
234 83f0f95a 2022-09-29 op int
235 83f0f95a 2022-09-29 op msgbuf_write(struct msgbuf *msgbuf)
236 83f0f95a 2022-09-29 op {
237 83f0f95a 2022-09-29 op struct iovec iov[IOV_MAX];
238 83f0f95a 2022-09-29 op struct ibuf *buf, *buf0 = NULL;
239 83f0f95a 2022-09-29 op unsigned int i = 0;
240 83f0f95a 2022-09-29 op ssize_t n;
241 83f0f95a 2022-09-29 op struct msghdr msg;
242 83f0f95a 2022-09-29 op struct cmsghdr *cmsg;
243 83f0f95a 2022-09-29 op union {
244 83f0f95a 2022-09-29 op struct cmsghdr hdr;
245 83f0f95a 2022-09-29 op char buf[CMSG_SPACE(sizeof(int))];
246 83f0f95a 2022-09-29 op } cmsgbuf;
247 83f0f95a 2022-09-29 op
248 83f0f95a 2022-09-29 op memset(&iov, 0, sizeof(iov));
249 83f0f95a 2022-09-29 op memset(&msg, 0, sizeof(msg));
250 83f0f95a 2022-09-29 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
251 83f0f95a 2022-09-29 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
252 83f0f95a 2022-09-29 op if (i >= IOV_MAX)
253 83f0f95a 2022-09-29 op break;
254 83f0f95a 2022-09-29 op if (i > 0 && buf->fd != -1)
255 83f0f95a 2022-09-29 op break;
256 83f0f95a 2022-09-29 op iov[i].iov_base = buf->buf + buf->rpos;
257 83f0f95a 2022-09-29 op iov[i].iov_len = buf->wpos - buf->rpos;
258 83f0f95a 2022-09-29 op i++;
259 83f0f95a 2022-09-29 op if (buf->fd != -1)
260 83f0f95a 2022-09-29 op buf0 = buf;
261 83f0f95a 2022-09-29 op }
262 83f0f95a 2022-09-29 op
263 83f0f95a 2022-09-29 op msg.msg_iov = iov;
264 83f0f95a 2022-09-29 op msg.msg_iovlen = i;
265 83f0f95a 2022-09-29 op
266 83f0f95a 2022-09-29 op if (buf0 != NULL) {
267 83f0f95a 2022-09-29 op msg.msg_control = (caddr_t)&cmsgbuf.buf;
268 83f0f95a 2022-09-29 op msg.msg_controllen = sizeof(cmsgbuf.buf);
269 83f0f95a 2022-09-29 op cmsg = CMSG_FIRSTHDR(&msg);
270 83f0f95a 2022-09-29 op cmsg->cmsg_len = CMSG_LEN(sizeof(int));
271 83f0f95a 2022-09-29 op cmsg->cmsg_level = SOL_SOCKET;
272 83f0f95a 2022-09-29 op cmsg->cmsg_type = SCM_RIGHTS;
273 83f0f95a 2022-09-29 op *(int *)CMSG_DATA(cmsg) = buf0->fd;
274 83f0f95a 2022-09-29 op }
275 83f0f95a 2022-09-29 op
276 83f0f95a 2022-09-29 op again:
277 83f0f95a 2022-09-29 op if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
278 83f0f95a 2022-09-29 op if (errno == EINTR)
279 83f0f95a 2022-09-29 op goto again;
280 83f0f95a 2022-09-29 op if (errno == ENOBUFS)
281 83f0f95a 2022-09-29 op errno = EAGAIN;
282 83f0f95a 2022-09-29 op return (-1);
283 83f0f95a 2022-09-29 op }
284 83f0f95a 2022-09-29 op
285 83f0f95a 2022-09-29 op if (n == 0) { /* connection closed */
286 83f0f95a 2022-09-29 op errno = 0;
287 83f0f95a 2022-09-29 op return (0);
288 83f0f95a 2022-09-29 op }
289 83f0f95a 2022-09-29 op
290 83f0f95a 2022-09-29 op /*
291 83f0f95a 2022-09-29 op * assumption: fd got sent if sendmsg sent anything
292 83f0f95a 2022-09-29 op * this works because fds are passed one at a time
293 83f0f95a 2022-09-29 op */
294 83f0f95a 2022-09-29 op if (buf0 != NULL) {
295 83f0f95a 2022-09-29 op close(buf0->fd);
296 83f0f95a 2022-09-29 op buf0->fd = -1;
297 83f0f95a 2022-09-29 op }
298 83f0f95a 2022-09-29 op
299 83f0f95a 2022-09-29 op msgbuf_drain(msgbuf, n);
300 83f0f95a 2022-09-29 op
301 83f0f95a 2022-09-29 op return (1);
302 83f0f95a 2022-09-29 op }
303 83f0f95a 2022-09-29 op
304 83f0f95a 2022-09-29 op static void
305 83f0f95a 2022-09-29 op ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
306 83f0f95a 2022-09-29 op {
307 83f0f95a 2022-09-29 op TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
308 83f0f95a 2022-09-29 op msgbuf->queued++;
309 83f0f95a 2022-09-29 op }
310 83f0f95a 2022-09-29 op
311 83f0f95a 2022-09-29 op static void
312 83f0f95a 2022-09-29 op ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
313 83f0f95a 2022-09-29 op {
314 83f0f95a 2022-09-29 op TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
315 83f0f95a 2022-09-29 op
316 83f0f95a 2022-09-29 op if (buf->fd != -1)
317 83f0f95a 2022-09-29 op close(buf->fd);
318 83f0f95a 2022-09-29 op
319 83f0f95a 2022-09-29 op msgbuf->queued--;
320 83f0f95a 2022-09-29 op ibuf_free(buf);
321 83f0f95a 2022-09-29 op }