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