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