1 83f0f95a 2022-09-29 op /* $OpenBSD: imsg-buffer.c,v 1.14 2022/04/23 08:57:52 tobias Exp $ */
4 83f0f95a 2022-09-29 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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.
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>
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>
30 83f0f95a 2022-09-29 op #include "imsg.h"
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 *);
37 83f0f95a 2022-09-29 op ibuf_open(size_t len)
39 83f0f95a 2022-09-29 op struct ibuf *buf;
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) {
45 83f0f95a 2022-09-29 op return (NULL);
47 83f0f95a 2022-09-29 op buf->size = buf->max = len;
54 83f0f95a 2022-09-29 op ibuf_dynamic(size_t len, size_t max)
56 83f0f95a 2022-09-29 op struct ibuf *buf;
58 83f0f95a 2022-09-29 op if (max < len)
59 83f0f95a 2022-09-29 op return (NULL);
61 83f0f95a 2022-09-29 op if ((buf = ibuf_open(len)) == NULL)
62 83f0f95a 2022-09-29 op return (NULL);
65 83f0f95a 2022-09-29 op buf->max = max;
71 83f0f95a 2022-09-29 op ibuf_realloc(struct ibuf *buf, size_t len)
73 83f0f95a 2022-09-29 op unsigned char *b;
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;
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)
85 83f0f95a 2022-09-29 op buf->size = buf->wpos + len;
91 83f0f95a 2022-09-29 op ibuf_add(struct ibuf *buf, const void *data, size_t len)
93 83f0f95a 2022-09-29 op if (len > SIZE_MAX - buf->wpos) {
94 83f0f95a 2022-09-29 op errno = ERANGE;
98 83f0f95a 2022-09-29 op if (buf->wpos + len > buf->size)
99 83f0f95a 2022-09-29 op if (ibuf_realloc(buf, len) == -1)
102 83f0f95a 2022-09-29 op memcpy(buf->buf + buf->wpos, data, len);
103 83f0f95a 2022-09-29 op buf->wpos += len;
108 83f0f95a 2022-09-29 op ibuf_reserve(struct ibuf *buf, size_t len)
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);
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);
121 83f0f95a 2022-09-29 op b = buf->buf + buf->wpos;
122 83f0f95a 2022-09-29 op buf->wpos += len;
127 83f0f95a 2022-09-29 op ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
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);
133 83f0f95a 2022-09-29 op return (buf->buf + pos);
137 83f0f95a 2022-09-29 op ibuf_size(struct ibuf *buf)
139 83f0f95a 2022-09-29 op return (buf->wpos);
143 83f0f95a 2022-09-29 op ibuf_left(struct ibuf *buf)
145 83f0f95a 2022-09-29 op return (buf->max - buf->wpos);
149 83f0f95a 2022-09-29 op ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
151 83f0f95a 2022-09-29 op ibuf_enqueue(msgbuf, buf);
155 83f0f95a 2022-09-29 op ibuf_write(struct msgbuf *msgbuf)
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;
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)
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;
172 83f0f95a 2022-09-29 op if ((n = writev(msgbuf->fd, iov, i)) == -1) {
173 83f0f95a 2022-09-29 op if (errno == EINTR)
175 83f0f95a 2022-09-29 op if (errno == ENOBUFS)
176 83f0f95a 2022-09-29 op errno = EAGAIN;
180 83f0f95a 2022-09-29 op if (n == 0) { /* connection closed */
185 83f0f95a 2022-09-29 op msgbuf_drain(msgbuf, n);
191 83f0f95a 2022-09-29 op ibuf_free(struct ibuf *buf)
193 83f0f95a 2022-09-29 op if (buf == NULL)
195 83f0f95a 2022-09-29 op freezero(buf->buf, buf->size);
200 83f0f95a 2022-09-29 op msgbuf_init(struct msgbuf *msgbuf)
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);
208 83f0f95a 2022-09-29 op msgbuf_drain(struct msgbuf *msgbuf, size_t n)
210 83f0f95a 2022-09-29 op struct ibuf *buf, *next;
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);
219 83f0f95a 2022-09-29 op buf->rpos += n;
226 83f0f95a 2022-09-29 op msgbuf_clear(struct msgbuf *msgbuf)
228 83f0f95a 2022-09-29 op struct ibuf *buf;
230 83f0f95a 2022-09-29 op while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
231 83f0f95a 2022-09-29 op ibuf_dequeue(msgbuf, buf);
235 83f0f95a 2022-09-29 op msgbuf_write(struct msgbuf *msgbuf)
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;
241 83f0f95a 2022-09-29 op struct msghdr msg;
242 83f0f95a 2022-09-29 op struct cmsghdr *cmsg;
244 83f0f95a 2022-09-29 op struct cmsghdr hdr;
245 83f0f95a 2022-09-29 op char buf[CMSG_SPACE(sizeof(int))];
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)
254 83f0f95a 2022-09-29 op if (i > 0 && buf->fd != -1)
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;
259 83f0f95a 2022-09-29 op if (buf->fd != -1)
263 83f0f95a 2022-09-29 op msg.msg_iov = iov;
264 83f0f95a 2022-09-29 op msg.msg_iovlen = i;
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;
277 83f0f95a 2022-09-29 op if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
278 83f0f95a 2022-09-29 op if (errno == EINTR)
280 83f0f95a 2022-09-29 op if (errno == ENOBUFS)
281 83f0f95a 2022-09-29 op errno = EAGAIN;
285 83f0f95a 2022-09-29 op if (n == 0) { /* connection closed */
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
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;
299 83f0f95a 2022-09-29 op msgbuf_drain(msgbuf, n);
305 83f0f95a 2022-09-29 op ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
307 83f0f95a 2022-09-29 op TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
308 83f0f95a 2022-09-29 op msgbuf->queued++;
312 83f0f95a 2022-09-29 op ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
314 83f0f95a 2022-09-29 op TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
316 83f0f95a 2022-09-29 op if (buf->fd != -1)
317 83f0f95a 2022-09-29 op close(buf->fd);
319 83f0f95a 2022-09-29 op msgbuf->queued--;
320 83f0f95a 2022-09-29 op ibuf_free(buf);