1 /* $OpenBSD: imsg-buffer.c,v 1.14 2022/04/23 08:57:52 tobias Exp $ */
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
32 static int ibuf_realloc(struct ibuf *, size_t);
33 static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
34 static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
41 if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
43 if ((buf->buf = malloc(len)) == NULL) {
47 buf->size = buf->max = len;
54 ibuf_dynamic(size_t len, size_t max)
61 if ((buf = ibuf_open(len)) == NULL)
71 ibuf_realloc(struct ibuf *buf, size_t len)
75 /* on static buffers max is eq size and so the following fails */
76 if (len > SIZE_MAX - buf->wpos || buf->wpos + len > buf->max) {
81 b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
85 buf->size = buf->wpos + len;
91 ibuf_add(struct ibuf *buf, const void *data, size_t len)
93 if (len > SIZE_MAX - buf->wpos) {
98 if (buf->wpos + len > buf->size)
99 if (ibuf_realloc(buf, len) == -1)
102 memcpy(buf->buf + buf->wpos, data, len);
108 ibuf_reserve(struct ibuf *buf, size_t len)
112 if (len > SIZE_MAX - buf->wpos) {
117 if (buf->wpos + len > buf->size)
118 if (ibuf_realloc(buf, len) == -1)
121 b = buf->buf + buf->wpos;
127 ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
129 /* only allowed to seek in already written parts */
130 if (len > SIZE_MAX - pos || pos + len > buf->wpos)
133 return (buf->buf + pos);
137 ibuf_size(struct ibuf *buf)
143 ibuf_left(struct ibuf *buf)
145 return (buf->max - buf->wpos);
149 ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
151 ibuf_enqueue(msgbuf, buf);
155 ibuf_write(struct msgbuf *msgbuf)
157 struct iovec iov[IOV_MAX];
162 memset(&iov, 0, sizeof(iov));
163 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
166 iov[i].iov_base = buf->buf + buf->rpos;
167 iov[i].iov_len = buf->wpos - buf->rpos;
172 if ((n = writev(msgbuf->fd, iov, i)) == -1) {
175 if (errno == ENOBUFS)
180 if (n == 0) { /* connection closed */
185 msgbuf_drain(msgbuf, n);
191 ibuf_free(struct ibuf *buf)
195 freezero(buf->buf, buf->size);
200 msgbuf_init(struct msgbuf *msgbuf)
204 TAILQ_INIT(&msgbuf->bufs);
208 msgbuf_drain(struct msgbuf *msgbuf, size_t n)
210 struct ibuf *buf, *next;
212 for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
214 next = TAILQ_NEXT(buf, entry);
215 if (n >= buf->wpos - buf->rpos) {
216 n -= buf->wpos - buf->rpos;
217 ibuf_dequeue(msgbuf, buf);
226 msgbuf_clear(struct msgbuf *msgbuf)
230 while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
231 ibuf_dequeue(msgbuf, buf);
235 msgbuf_write(struct msgbuf *msgbuf)
237 struct iovec iov[IOV_MAX];
238 struct ibuf *buf, *buf0 = NULL;
242 struct cmsghdr *cmsg;
245 char buf[CMSG_SPACE(sizeof(int))];
248 memset(&iov, 0, sizeof(iov));
249 memset(&msg, 0, sizeof(msg));
250 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
251 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
254 if (i > 0 && buf->fd != -1)
256 iov[i].iov_base = buf->buf + buf->rpos;
257 iov[i].iov_len = buf->wpos - buf->rpos;
267 msg.msg_control = (caddr_t)&cmsgbuf.buf;
268 msg.msg_controllen = sizeof(cmsgbuf.buf);
269 cmsg = CMSG_FIRSTHDR(&msg);
270 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
271 cmsg->cmsg_level = SOL_SOCKET;
272 cmsg->cmsg_type = SCM_RIGHTS;
273 *(int *)CMSG_DATA(cmsg) = buf0->fd;
277 if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
280 if (errno == ENOBUFS)
285 if (n == 0) { /* connection closed */
291 * assumption: fd got sent if sendmsg sent anything
292 * this works because fds are passed one at a time
299 msgbuf_drain(msgbuf, n);
305 ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
307 TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
312 ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
314 TAILQ_REMOVE(&msgbuf->bufs, buf, entry);