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