1 894e9984 2022-03-19 op /* $OpenBSD: imsg-buffer.c,v 1.13 2021/03/31 17:42:24 eric Exp $ */
4 6b191ed5 2021-02-23 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 6b191ed5 2021-02-23 op * Permission to use, copy, modify, and distribute this software for any
7 6b191ed5 2021-02-23 op * purpose with or without fee is hereby granted, provided that the above
8 6b191ed5 2021-02-23 op * copyright notice and this permission notice appear in all copies.
10 6b191ed5 2021-02-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 6b191ed5 2021-02-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 6b191ed5 2021-02-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 6b191ed5 2021-02-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 6b191ed5 2021-02-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 6b191ed5 2021-02-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 6b191ed5 2021-02-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 6b191ed5 2021-02-23 op #include "../config.h"
21 6b191ed5 2021-02-23 op #include <sys/types.h>
22 6b191ed5 2021-02-23 op #include <sys/socket.h>
23 6b191ed5 2021-02-23 op #include <sys/uio.h>
25 6b191ed5 2021-02-23 op #include <limits.h>
26 6b191ed5 2021-02-23 op #include <errno.h>
27 6b191ed5 2021-02-23 op #include <stdlib.h>
28 6b191ed5 2021-02-23 op #include <string.h>
29 6b191ed5 2021-02-23 op #include <unistd.h>
31 6b191ed5 2021-02-23 op #include "imsg.h"
33 6b191ed5 2021-02-23 op static int ibuf_realloc(struct ibuf *, size_t);
34 6b191ed5 2021-02-23 op static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
35 6b191ed5 2021-02-23 op static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
38 6b191ed5 2021-02-23 op ibuf_open(size_t len)
40 6b191ed5 2021-02-23 op struct ibuf *buf;
42 6b191ed5 2021-02-23 op if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
43 6b191ed5 2021-02-23 op return (NULL);
44 6b191ed5 2021-02-23 op if ((buf->buf = malloc(len)) == NULL) {
46 6b191ed5 2021-02-23 op return (NULL);
48 6b191ed5 2021-02-23 op buf->size = buf->max = len;
55 6b191ed5 2021-02-23 op ibuf_dynamic(size_t len, size_t max)
57 6b191ed5 2021-02-23 op struct ibuf *buf;
59 6b191ed5 2021-02-23 op if (max < len)
60 6b191ed5 2021-02-23 op return (NULL);
62 6b191ed5 2021-02-23 op if ((buf = ibuf_open(len)) == NULL)
63 6b191ed5 2021-02-23 op return (NULL);
66 6b191ed5 2021-02-23 op buf->max = max;
72 6b191ed5 2021-02-23 op ibuf_realloc(struct ibuf *buf, size_t len)
74 6b191ed5 2021-02-23 op unsigned char *b;
76 6b191ed5 2021-02-23 op /* on static buffers max is eq size and so the following fails */
77 6b191ed5 2021-02-23 op if (buf->wpos + len > buf->max) {
78 6b191ed5 2021-02-23 op errno = ERANGE;
82 6b191ed5 2021-02-23 op b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
83 6b191ed5 2021-02-23 op if (b == NULL)
86 6b191ed5 2021-02-23 op buf->size = buf->wpos + len;
92 6b191ed5 2021-02-23 op ibuf_add(struct ibuf *buf, const void *data, size_t len)
94 6b191ed5 2021-02-23 op if (buf->wpos + len > buf->size)
95 6b191ed5 2021-02-23 op if (ibuf_realloc(buf, len) == -1)
98 6b191ed5 2021-02-23 op memcpy(buf->buf + buf->wpos, data, len);
99 6b191ed5 2021-02-23 op buf->wpos += len;
104 6b191ed5 2021-02-23 op ibuf_reserve(struct ibuf *buf, size_t len)
108 6b191ed5 2021-02-23 op if (buf->wpos + len > buf->size)
109 6b191ed5 2021-02-23 op if (ibuf_realloc(buf, len) == -1)
110 6b191ed5 2021-02-23 op return (NULL);
112 6b191ed5 2021-02-23 op b = buf->buf + buf->wpos;
113 6b191ed5 2021-02-23 op buf->wpos += len;
118 6b191ed5 2021-02-23 op ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
120 6b191ed5 2021-02-23 op /* only allowed to seek in already written parts */
121 6b191ed5 2021-02-23 op if (pos + len > buf->wpos)
122 6b191ed5 2021-02-23 op return (NULL);
124 6b191ed5 2021-02-23 op return (buf->buf + pos);
128 6b191ed5 2021-02-23 op ibuf_size(struct ibuf *buf)
130 6b191ed5 2021-02-23 op return (buf->wpos);
134 6b191ed5 2021-02-23 op ibuf_left(struct ibuf *buf)
136 6b191ed5 2021-02-23 op return (buf->max - buf->wpos);
140 6b191ed5 2021-02-23 op ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
142 6b191ed5 2021-02-23 op ibuf_enqueue(msgbuf, buf);
146 6b191ed5 2021-02-23 op ibuf_write(struct msgbuf *msgbuf)
148 6b191ed5 2021-02-23 op struct iovec iov[IOV_MAX];
149 6b191ed5 2021-02-23 op struct ibuf *buf;
150 6b191ed5 2021-02-23 op unsigned int i = 0;
153 6b191ed5 2021-02-23 op memset(&iov, 0, sizeof(iov));
154 6b191ed5 2021-02-23 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
155 6b191ed5 2021-02-23 op if (i >= IOV_MAX)
157 6b191ed5 2021-02-23 op iov[i].iov_base = buf->buf + buf->rpos;
158 6b191ed5 2021-02-23 op iov[i].iov_len = buf->wpos - buf->rpos;
163 6b191ed5 2021-02-23 op if ((n = writev(msgbuf->fd, iov, i)) == -1) {
164 6b191ed5 2021-02-23 op if (errno == EINTR)
166 6b191ed5 2021-02-23 op if (errno == ENOBUFS)
167 6b191ed5 2021-02-23 op errno = EAGAIN;
171 6b191ed5 2021-02-23 op if (n == 0) { /* connection closed */
176 6b191ed5 2021-02-23 op msgbuf_drain(msgbuf, n);
182 6b191ed5 2021-02-23 op ibuf_free(struct ibuf *buf)
184 6b191ed5 2021-02-23 op if (buf == NULL)
186 6b191ed5 2021-02-23 op freezero(buf->buf, buf->size);
191 6b191ed5 2021-02-23 op msgbuf_init(struct msgbuf *msgbuf)
193 6b191ed5 2021-02-23 op msgbuf->queued = 0;
194 6b191ed5 2021-02-23 op msgbuf->fd = -1;
195 6b191ed5 2021-02-23 op TAILQ_INIT(&msgbuf->bufs);
199 6b191ed5 2021-02-23 op msgbuf_drain(struct msgbuf *msgbuf, size_t n)
201 6b191ed5 2021-02-23 op struct ibuf *buf, *next;
203 6b191ed5 2021-02-23 op for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
204 6b191ed5 2021-02-23 op buf = next) {
205 6b191ed5 2021-02-23 op next = TAILQ_NEXT(buf, entry);
206 6b191ed5 2021-02-23 op if (buf->rpos + n >= buf->wpos) {
207 6b191ed5 2021-02-23 op n -= buf->wpos - buf->rpos;
208 6b191ed5 2021-02-23 op ibuf_dequeue(msgbuf, buf);
210 6b191ed5 2021-02-23 op buf->rpos += n;
217 6b191ed5 2021-02-23 op msgbuf_clear(struct msgbuf *msgbuf)
219 6b191ed5 2021-02-23 op struct ibuf *buf;
221 6b191ed5 2021-02-23 op while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
222 6b191ed5 2021-02-23 op ibuf_dequeue(msgbuf, buf);
226 6b191ed5 2021-02-23 op msgbuf_write(struct msgbuf *msgbuf)
228 6b191ed5 2021-02-23 op struct iovec iov[IOV_MAX];
229 894e9984 2022-03-19 op struct ibuf *buf, *buf0 = NULL;
230 6b191ed5 2021-02-23 op unsigned int i = 0;
232 6b191ed5 2021-02-23 op struct msghdr msg;
233 6b191ed5 2021-02-23 op struct cmsghdr *cmsg;
235 6b191ed5 2021-02-23 op struct cmsghdr hdr;
236 6b191ed5 2021-02-23 op char buf[CMSG_SPACE(sizeof(int))];
239 6b191ed5 2021-02-23 op memset(&iov, 0, sizeof(iov));
240 6b191ed5 2021-02-23 op memset(&msg, 0, sizeof(msg));
241 6b191ed5 2021-02-23 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
242 6b191ed5 2021-02-23 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
243 6b191ed5 2021-02-23 op if (i >= IOV_MAX)
245 894e9984 2022-03-19 op if (i > 0 && buf->fd != -1)
247 6b191ed5 2021-02-23 op iov[i].iov_base = buf->buf + buf->rpos;
248 6b191ed5 2021-02-23 op iov[i].iov_len = buf->wpos - buf->rpos;
250 6b191ed5 2021-02-23 op if (buf->fd != -1)
254 6b191ed5 2021-02-23 op msg.msg_iov = iov;
255 6b191ed5 2021-02-23 op msg.msg_iovlen = i;
257 894e9984 2022-03-19 op if (buf0 != NULL) {
258 6b191ed5 2021-02-23 op msg.msg_control = (caddr_t)&cmsgbuf.buf;
259 6b191ed5 2021-02-23 op msg.msg_controllen = sizeof(cmsgbuf.buf);
260 6b191ed5 2021-02-23 op cmsg = CMSG_FIRSTHDR(&msg);
261 6b191ed5 2021-02-23 op cmsg->cmsg_len = CMSG_LEN(sizeof(int));
262 6b191ed5 2021-02-23 op cmsg->cmsg_level = SOL_SOCKET;
263 6b191ed5 2021-02-23 op cmsg->cmsg_type = SCM_RIGHTS;
264 894e9984 2022-03-19 op *(int *)CMSG_DATA(cmsg) = buf0->fd;
268 6b191ed5 2021-02-23 op if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
269 6b191ed5 2021-02-23 op if (errno == EINTR)
271 6b191ed5 2021-02-23 op if (errno == ENOBUFS)
272 6b191ed5 2021-02-23 op errno = EAGAIN;
276 6b191ed5 2021-02-23 op if (n == 0) { /* connection closed */
282 6b191ed5 2021-02-23 op * assumption: fd got sent if sendmsg sent anything
283 6b191ed5 2021-02-23 op * this works because fds are passed one at a time
285 894e9984 2022-03-19 op if (buf0 != NULL) {
286 894e9984 2022-03-19 op close(buf0->fd);
287 894e9984 2022-03-19 op buf0->fd = -1;
290 6b191ed5 2021-02-23 op msgbuf_drain(msgbuf, n);
296 6b191ed5 2021-02-23 op ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
298 6b191ed5 2021-02-23 op TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
299 6b191ed5 2021-02-23 op msgbuf->queued++;
303 6b191ed5 2021-02-23 op ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
305 6b191ed5 2021-02-23 op TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
307 6b191ed5 2021-02-23 op if (buf->fd != -1)
308 6b191ed5 2021-02-23 op close(buf->fd);
310 6b191ed5 2021-02-23 op msgbuf->queued--;
311 6b191ed5 2021-02-23 op ibuf_free(buf);