1 dd038bc6 2021-09-21 thomas.ad /* $OpenBSD: imsg-buffer.c,v 1.11 2017/12/14 09:27:44 kettenis Exp $ */
4 dd038bc6 2021-09-21 thomas.ad * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 dd038bc6 2021-09-21 thomas.ad * Permission to use, copy, modify, and distribute this software for any
7 dd038bc6 2021-09-21 thomas.ad * purpose with or without fee is hereby granted, provided that the above
8 dd038bc6 2021-09-21 thomas.ad * copyright notice and this permission notice appear in all copies.
10 dd038bc6 2021-09-21 thomas.ad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 dd038bc6 2021-09-21 thomas.ad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 dd038bc6 2021-09-21 thomas.ad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 dd038bc6 2021-09-21 thomas.ad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 dd038bc6 2021-09-21 thomas.ad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 dd038bc6 2021-09-21 thomas.ad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 dd038bc6 2021-09-21 thomas.ad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 dd038bc6 2021-09-21 thomas.ad #include <sys/types.h>
20 dd038bc6 2021-09-21 thomas.ad #include <sys/socket.h>
21 dd038bc6 2021-09-21 thomas.ad #include <sys/uio.h>
23 dd038bc6 2021-09-21 thomas.ad #include <limits.h>
24 dd038bc6 2021-09-21 thomas.ad #include <errno.h>
25 dd038bc6 2021-09-21 thomas.ad #include <stdlib.h>
26 dd038bc6 2021-09-21 thomas.ad #include <string.h>
27 dd038bc6 2021-09-21 thomas.ad #include <unistd.h>
29 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
31 dd038bc6 2021-09-21 thomas.ad static int ibuf_realloc(struct ibuf *, size_t);
32 dd038bc6 2021-09-21 thomas.ad static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
33 dd038bc6 2021-09-21 thomas.ad static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
35 dd038bc6 2021-09-21 thomas.ad struct ibuf *
36 dd038bc6 2021-09-21 thomas.ad ibuf_open(size_t len)
38 dd038bc6 2021-09-21 thomas.ad struct ibuf *buf;
40 dd038bc6 2021-09-21 thomas.ad if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
41 dd038bc6 2021-09-21 thomas.ad return (NULL);
42 dd038bc6 2021-09-21 thomas.ad if ((buf->buf = malloc(len)) == NULL) {
43 dd038bc6 2021-09-21 thomas.ad free(buf);
44 dd038bc6 2021-09-21 thomas.ad return (NULL);
46 dd038bc6 2021-09-21 thomas.ad buf->size = buf->max = len;
47 dd038bc6 2021-09-21 thomas.ad buf->fd = -1;
49 dd038bc6 2021-09-21 thomas.ad return (buf);
52 dd038bc6 2021-09-21 thomas.ad struct ibuf *
53 dd038bc6 2021-09-21 thomas.ad ibuf_dynamic(size_t len, size_t max)
55 dd038bc6 2021-09-21 thomas.ad struct ibuf *buf;
57 dd038bc6 2021-09-21 thomas.ad if (max < len)
58 dd038bc6 2021-09-21 thomas.ad return (NULL);
60 dd038bc6 2021-09-21 thomas.ad if ((buf = ibuf_open(len)) == NULL)
61 dd038bc6 2021-09-21 thomas.ad return (NULL);
63 dd038bc6 2021-09-21 thomas.ad if (max > 0)
64 dd038bc6 2021-09-21 thomas.ad buf->max = max;
66 dd038bc6 2021-09-21 thomas.ad return (buf);
69 dd038bc6 2021-09-21 thomas.ad static int
70 dd038bc6 2021-09-21 thomas.ad ibuf_realloc(struct ibuf *buf, size_t len)
72 dd038bc6 2021-09-21 thomas.ad u_char *b;
74 dd038bc6 2021-09-21 thomas.ad /* on static buffers max is eq size and so the following fails */
75 dd038bc6 2021-09-21 thomas.ad if (buf->wpos + len > buf->max) {
76 dd038bc6 2021-09-21 thomas.ad errno = ERANGE;
77 dd038bc6 2021-09-21 thomas.ad return (-1);
80 c0faa645 2021-09-21 thomas.ad b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
81 dd038bc6 2021-09-21 thomas.ad if (b == NULL)
82 dd038bc6 2021-09-21 thomas.ad return (-1);
83 dd038bc6 2021-09-21 thomas.ad buf->buf = b;
84 dd038bc6 2021-09-21 thomas.ad buf->size = buf->wpos + len;
86 dd038bc6 2021-09-21 thomas.ad return (0);
90 dd038bc6 2021-09-21 thomas.ad ibuf_add(struct ibuf *buf, const void *data, size_t len)
92 dd038bc6 2021-09-21 thomas.ad if (buf->wpos + len > buf->size)
93 dd038bc6 2021-09-21 thomas.ad if (ibuf_realloc(buf, len) == -1)
94 dd038bc6 2021-09-21 thomas.ad return (-1);
96 dd038bc6 2021-09-21 thomas.ad memcpy(buf->buf + buf->wpos, data, len);
97 dd038bc6 2021-09-21 thomas.ad buf->wpos += len;
98 dd038bc6 2021-09-21 thomas.ad return (0);
101 dd038bc6 2021-09-21 thomas.ad void *
102 dd038bc6 2021-09-21 thomas.ad ibuf_reserve(struct ibuf *buf, size_t len)
104 dd038bc6 2021-09-21 thomas.ad void *b;
106 dd038bc6 2021-09-21 thomas.ad if (buf->wpos + len > buf->size)
107 dd038bc6 2021-09-21 thomas.ad if (ibuf_realloc(buf, len) == -1)
108 dd038bc6 2021-09-21 thomas.ad return (NULL);
110 dd038bc6 2021-09-21 thomas.ad b = buf->buf + buf->wpos;
111 dd038bc6 2021-09-21 thomas.ad buf->wpos += len;
112 dd038bc6 2021-09-21 thomas.ad return (b);
115 dd038bc6 2021-09-21 thomas.ad void *
116 dd038bc6 2021-09-21 thomas.ad ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
118 dd038bc6 2021-09-21 thomas.ad /* only allowed to seek in already written parts */
119 dd038bc6 2021-09-21 thomas.ad if (pos + len > buf->wpos)
120 dd038bc6 2021-09-21 thomas.ad return (NULL);
122 dd038bc6 2021-09-21 thomas.ad return (buf->buf + pos);
125 dd038bc6 2021-09-21 thomas.ad size_t
126 dd038bc6 2021-09-21 thomas.ad ibuf_size(struct ibuf *buf)
128 dd038bc6 2021-09-21 thomas.ad return (buf->wpos);
131 dd038bc6 2021-09-21 thomas.ad size_t
132 dd038bc6 2021-09-21 thomas.ad ibuf_left(struct ibuf *buf)
134 dd038bc6 2021-09-21 thomas.ad return (buf->max - buf->wpos);
138 dd038bc6 2021-09-21 thomas.ad ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
140 dd038bc6 2021-09-21 thomas.ad ibuf_enqueue(msgbuf, buf);
144 dd038bc6 2021-09-21 thomas.ad ibuf_write(struct msgbuf *msgbuf)
146 dd038bc6 2021-09-21 thomas.ad struct iovec iov[IOV_MAX];
147 dd038bc6 2021-09-21 thomas.ad struct ibuf *buf;
148 dd038bc6 2021-09-21 thomas.ad unsigned int i = 0;
149 dd038bc6 2021-09-21 thomas.ad ssize_t n;
151 dd038bc6 2021-09-21 thomas.ad memset(&iov, 0, sizeof(iov));
152 dd038bc6 2021-09-21 thomas.ad TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
153 dd038bc6 2021-09-21 thomas.ad if (i >= IOV_MAX)
154 dd038bc6 2021-09-21 thomas.ad break;
155 dd038bc6 2021-09-21 thomas.ad iov[i].iov_base = buf->buf + buf->rpos;
156 dd038bc6 2021-09-21 thomas.ad iov[i].iov_len = buf->wpos - buf->rpos;
160 dd038bc6 2021-09-21 thomas.ad again:
161 dd038bc6 2021-09-21 thomas.ad if ((n = writev(msgbuf->fd, iov, i)) == -1) {
162 dd038bc6 2021-09-21 thomas.ad if (errno == EINTR)
163 dd038bc6 2021-09-21 thomas.ad goto again;
164 dd038bc6 2021-09-21 thomas.ad if (errno == ENOBUFS)
165 dd038bc6 2021-09-21 thomas.ad errno = EAGAIN;
166 dd038bc6 2021-09-21 thomas.ad return (-1);
169 dd038bc6 2021-09-21 thomas.ad if (n == 0) { /* connection closed */
170 dd038bc6 2021-09-21 thomas.ad errno = 0;
171 dd038bc6 2021-09-21 thomas.ad return (0);
174 dd038bc6 2021-09-21 thomas.ad msgbuf_drain(msgbuf, n);
176 dd038bc6 2021-09-21 thomas.ad return (1);
180 dd038bc6 2021-09-21 thomas.ad ibuf_free(struct ibuf *buf)
182 dd038bc6 2021-09-21 thomas.ad if (buf == NULL)
183 dd038bc6 2021-09-21 thomas.ad return;
184 dd038bc6 2021-09-21 thomas.ad freezero(buf->buf, buf->size);
185 dd038bc6 2021-09-21 thomas.ad free(buf);
189 dd038bc6 2021-09-21 thomas.ad msgbuf_init(struct msgbuf *msgbuf)
191 dd038bc6 2021-09-21 thomas.ad msgbuf->queued = 0;
192 dd038bc6 2021-09-21 thomas.ad msgbuf->fd = -1;
193 dd038bc6 2021-09-21 thomas.ad TAILQ_INIT(&msgbuf->bufs);
197 dd038bc6 2021-09-21 thomas.ad msgbuf_drain(struct msgbuf *msgbuf, size_t n)
199 dd038bc6 2021-09-21 thomas.ad struct ibuf *buf, *next;
201 dd038bc6 2021-09-21 thomas.ad for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
202 dd038bc6 2021-09-21 thomas.ad buf = next) {
203 dd038bc6 2021-09-21 thomas.ad next = TAILQ_NEXT(buf, entry);
204 dd038bc6 2021-09-21 thomas.ad if (buf->rpos + n >= buf->wpos) {
205 dd038bc6 2021-09-21 thomas.ad n -= buf->wpos - buf->rpos;
206 dd038bc6 2021-09-21 thomas.ad ibuf_dequeue(msgbuf, buf);
207 dd038bc6 2021-09-21 thomas.ad } else {
208 dd038bc6 2021-09-21 thomas.ad buf->rpos += n;
209 dd038bc6 2021-09-21 thomas.ad n = 0;
215 dd038bc6 2021-09-21 thomas.ad msgbuf_clear(struct msgbuf *msgbuf)
217 dd038bc6 2021-09-21 thomas.ad struct ibuf *buf;
219 dd038bc6 2021-09-21 thomas.ad while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
220 dd038bc6 2021-09-21 thomas.ad ibuf_dequeue(msgbuf, buf);
224 dd038bc6 2021-09-21 thomas.ad msgbuf_write(struct msgbuf *msgbuf)
226 dd038bc6 2021-09-21 thomas.ad struct iovec iov[IOV_MAX];
227 dd038bc6 2021-09-21 thomas.ad struct ibuf *buf;
228 dd038bc6 2021-09-21 thomas.ad unsigned int i = 0;
229 dd038bc6 2021-09-21 thomas.ad ssize_t n;
230 dd038bc6 2021-09-21 thomas.ad struct msghdr msg;
231 dd038bc6 2021-09-21 thomas.ad struct cmsghdr *cmsg;
232 dd038bc6 2021-09-21 thomas.ad union {
233 dd038bc6 2021-09-21 thomas.ad struct cmsghdr hdr;
234 dd038bc6 2021-09-21 thomas.ad char buf[CMSG_SPACE(sizeof(int))];
235 dd038bc6 2021-09-21 thomas.ad } cmsgbuf;
237 dd038bc6 2021-09-21 thomas.ad memset(&iov, 0, sizeof(iov));
238 dd038bc6 2021-09-21 thomas.ad memset(&msg, 0, sizeof(msg));
239 dd038bc6 2021-09-21 thomas.ad memset(&cmsgbuf, 0, sizeof(cmsgbuf));
240 dd038bc6 2021-09-21 thomas.ad TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
241 dd038bc6 2021-09-21 thomas.ad if (i >= IOV_MAX)
242 dd038bc6 2021-09-21 thomas.ad break;
243 dd038bc6 2021-09-21 thomas.ad iov[i].iov_base = buf->buf + buf->rpos;
244 dd038bc6 2021-09-21 thomas.ad iov[i].iov_len = buf->wpos - buf->rpos;
246 dd038bc6 2021-09-21 thomas.ad if (buf->fd != -1)
247 dd038bc6 2021-09-21 thomas.ad break;
250 dd038bc6 2021-09-21 thomas.ad msg.msg_iov = iov;
251 dd038bc6 2021-09-21 thomas.ad msg.msg_iovlen = i;
253 dd038bc6 2021-09-21 thomas.ad if (buf != NULL && buf->fd != -1) {
254 dd038bc6 2021-09-21 thomas.ad msg.msg_control = (caddr_t)&cmsgbuf.buf;
255 dd038bc6 2021-09-21 thomas.ad msg.msg_controllen = sizeof(cmsgbuf.buf);
256 dd038bc6 2021-09-21 thomas.ad cmsg = CMSG_FIRSTHDR(&msg);
257 dd038bc6 2021-09-21 thomas.ad cmsg->cmsg_len = CMSG_LEN(sizeof(int));
258 dd038bc6 2021-09-21 thomas.ad cmsg->cmsg_level = SOL_SOCKET;
259 dd038bc6 2021-09-21 thomas.ad cmsg->cmsg_type = SCM_RIGHTS;
260 dd038bc6 2021-09-21 thomas.ad *(int *)CMSG_DATA(cmsg) = buf->fd;
263 dd038bc6 2021-09-21 thomas.ad again:
264 dd038bc6 2021-09-21 thomas.ad if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
265 dd038bc6 2021-09-21 thomas.ad if (errno == EINTR)
266 dd038bc6 2021-09-21 thomas.ad goto again;
267 dd038bc6 2021-09-21 thomas.ad if (errno == ENOBUFS)
268 dd038bc6 2021-09-21 thomas.ad errno = EAGAIN;
269 dd038bc6 2021-09-21 thomas.ad return (-1);
272 dd038bc6 2021-09-21 thomas.ad if (n == 0) { /* connection closed */
273 dd038bc6 2021-09-21 thomas.ad errno = 0;
274 dd038bc6 2021-09-21 thomas.ad return (0);
278 dd038bc6 2021-09-21 thomas.ad * assumption: fd got sent if sendmsg sent anything
279 dd038bc6 2021-09-21 thomas.ad * this works because fds are passed one at a time
281 dd038bc6 2021-09-21 thomas.ad if (buf != NULL && buf->fd != -1) {
282 dd038bc6 2021-09-21 thomas.ad close(buf->fd);
283 dd038bc6 2021-09-21 thomas.ad buf->fd = -1;
286 dd038bc6 2021-09-21 thomas.ad msgbuf_drain(msgbuf, n);
288 dd038bc6 2021-09-21 thomas.ad return (1);
291 dd038bc6 2021-09-21 thomas.ad static void
292 dd038bc6 2021-09-21 thomas.ad ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
294 dd038bc6 2021-09-21 thomas.ad TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
295 dd038bc6 2021-09-21 thomas.ad msgbuf->queued++;
298 dd038bc6 2021-09-21 thomas.ad static void
299 dd038bc6 2021-09-21 thomas.ad ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
301 dd038bc6 2021-09-21 thomas.ad TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
303 dd038bc6 2021-09-21 thomas.ad if (buf->fd != -1)
304 dd038bc6 2021-09-21 thomas.ad close(buf->fd);
306 dd038bc6 2021-09-21 thomas.ad msgbuf->queued--;
307 dd038bc6 2021-09-21 thomas.ad ibuf_free(buf);