Blame


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