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 a625907c 2021-03-19 op #include "compat.h"
21 b0003f58 2021-03-08 op #include <sys/types.h>
22 b0003f58 2021-03-08 op #include <sys/socket.h>
23 b0003f58 2021-03-08 op #include <sys/uio.h>
25 b0003f58 2021-03-08 op #include <limits.h>
26 b0003f58 2021-03-08 op #include <errno.h>
27 b0003f58 2021-03-08 op #include <stdlib.h>
28 b0003f58 2021-03-08 op #include <string.h>
29 b0003f58 2021-03-08 op #include <unistd.h>
31 b0003f58 2021-03-08 op #include "imsg.h"
33 b0003f58 2021-03-08 op static int ibuf_realloc(struct ibuf *, size_t);
34 b0003f58 2021-03-08 op static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
35 b0003f58 2021-03-08 op static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
38 b0003f58 2021-03-08 op ibuf_open(size_t len)
40 b0003f58 2021-03-08 op struct ibuf *buf;
42 b0003f58 2021-03-08 op if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
43 b0003f58 2021-03-08 op return (NULL);
44 b0003f58 2021-03-08 op if ((buf->buf = malloc(len)) == NULL) {
46 b0003f58 2021-03-08 op return (NULL);
48 b0003f58 2021-03-08 op buf->size = buf->max = len;
55 b0003f58 2021-03-08 op ibuf_dynamic(size_t len, size_t max)
57 b0003f58 2021-03-08 op struct ibuf *buf;
59 b0003f58 2021-03-08 op if (max < len)
60 b0003f58 2021-03-08 op return (NULL);
62 b0003f58 2021-03-08 op if ((buf = ibuf_open(len)) == NULL)
63 b0003f58 2021-03-08 op return (NULL);
66 b0003f58 2021-03-08 op buf->max = max;
72 b0003f58 2021-03-08 op ibuf_realloc(struct ibuf *buf, size_t len)
74 b0003f58 2021-03-08 op unsigned char *b;
76 b0003f58 2021-03-08 op /* on static buffers max is eq size and so the following fails */
77 b0003f58 2021-03-08 op if (buf->wpos + len > buf->max) {
78 b0003f58 2021-03-08 op errno = ERANGE;
82 b0003f58 2021-03-08 op b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
83 b0003f58 2021-03-08 op if (b == NULL)
86 b0003f58 2021-03-08 op buf->size = buf->wpos + len;
92 b0003f58 2021-03-08 op ibuf_add(struct ibuf *buf, const void *data, size_t len)
94 b0003f58 2021-03-08 op if (buf->wpos + len > buf->size)
95 b0003f58 2021-03-08 op if (ibuf_realloc(buf, len) == -1)
98 b0003f58 2021-03-08 op memcpy(buf->buf + buf->wpos, data, len);
99 b0003f58 2021-03-08 op buf->wpos += len;
104 b0003f58 2021-03-08 op ibuf_reserve(struct ibuf *buf, size_t len)
108 b0003f58 2021-03-08 op if (buf->wpos + len > buf->size)
109 b0003f58 2021-03-08 op if (ibuf_realloc(buf, len) == -1)
110 b0003f58 2021-03-08 op return (NULL);
112 b0003f58 2021-03-08 op b = buf->buf + buf->wpos;
113 b0003f58 2021-03-08 op buf->wpos += len;
118 b0003f58 2021-03-08 op ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
120 b0003f58 2021-03-08 op /* only allowed to seek in already written parts */
121 b0003f58 2021-03-08 op if (pos + len > buf->wpos)
122 b0003f58 2021-03-08 op return (NULL);
124 b0003f58 2021-03-08 op return (buf->buf + pos);
128 b0003f58 2021-03-08 op ibuf_size(struct ibuf *buf)
130 b0003f58 2021-03-08 op return (buf->wpos);
134 b0003f58 2021-03-08 op ibuf_left(struct ibuf *buf)
136 b0003f58 2021-03-08 op return (buf->max - buf->wpos);
140 b0003f58 2021-03-08 op ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
142 b0003f58 2021-03-08 op ibuf_enqueue(msgbuf, buf);
146 b0003f58 2021-03-08 op ibuf_write(struct msgbuf *msgbuf)
148 b0003f58 2021-03-08 op struct iovec iov[IOV_MAX];
149 b0003f58 2021-03-08 op struct ibuf *buf;
150 b0003f58 2021-03-08 op unsigned int i = 0;
153 b0003f58 2021-03-08 op memset(&iov, 0, sizeof(iov));
154 b0003f58 2021-03-08 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
155 b0003f58 2021-03-08 op if (i >= IOV_MAX)
157 b0003f58 2021-03-08 op iov[i].iov_base = buf->buf + buf->rpos;
158 b0003f58 2021-03-08 op iov[i].iov_len = buf->wpos - buf->rpos;
163 b0003f58 2021-03-08 op if ((n = writev(msgbuf->fd, iov, i)) == -1) {
164 b0003f58 2021-03-08 op if (errno == EINTR)
166 b0003f58 2021-03-08 op if (errno == ENOBUFS)
167 b0003f58 2021-03-08 op errno = EAGAIN;
171 b0003f58 2021-03-08 op if (n == 0) { /* connection closed */
176 b0003f58 2021-03-08 op msgbuf_drain(msgbuf, n);
182 b0003f58 2021-03-08 op ibuf_free(struct ibuf *buf)
184 b0003f58 2021-03-08 op if (buf == NULL)
186 b0003f58 2021-03-08 op freezero(buf->buf, buf->size);
191 b0003f58 2021-03-08 op msgbuf_init(struct msgbuf *msgbuf)
193 b0003f58 2021-03-08 op msgbuf->queued = 0;
194 b0003f58 2021-03-08 op msgbuf->fd = -1;
195 b0003f58 2021-03-08 op TAILQ_INIT(&msgbuf->bufs);
199 b0003f58 2021-03-08 op msgbuf_drain(struct msgbuf *msgbuf, size_t n)
201 b0003f58 2021-03-08 op struct ibuf *buf, *next;
203 b0003f58 2021-03-08 op for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
204 b0003f58 2021-03-08 op buf = next) {
205 b0003f58 2021-03-08 op next = TAILQ_NEXT(buf, entry);
206 b0003f58 2021-03-08 op if (buf->rpos + n >= buf->wpos) {
207 b0003f58 2021-03-08 op n -= buf->wpos - buf->rpos;
208 b0003f58 2021-03-08 op ibuf_dequeue(msgbuf, buf);
210 b0003f58 2021-03-08 op buf->rpos += n;
217 b0003f58 2021-03-08 op msgbuf_clear(struct msgbuf *msgbuf)
219 b0003f58 2021-03-08 op struct ibuf *buf;
221 b0003f58 2021-03-08 op while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
222 b0003f58 2021-03-08 op ibuf_dequeue(msgbuf, buf);
226 b0003f58 2021-03-08 op msgbuf_write(struct msgbuf *msgbuf)
228 b0003f58 2021-03-08 op struct iovec iov[IOV_MAX];
229 b0003f58 2021-03-08 op struct ibuf *buf;
230 b0003f58 2021-03-08 op unsigned int i = 0;
232 b0003f58 2021-03-08 op struct msghdr msg;
233 b0003f58 2021-03-08 op struct cmsghdr *cmsg;
235 b0003f58 2021-03-08 op struct cmsghdr hdr;
236 b0003f58 2021-03-08 op char buf[CMSG_SPACE(sizeof(int))];
239 b0003f58 2021-03-08 op memset(&iov, 0, sizeof(iov));
240 b0003f58 2021-03-08 op memset(&msg, 0, sizeof(msg));
241 b0003f58 2021-03-08 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
242 b0003f58 2021-03-08 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
243 b0003f58 2021-03-08 op if (i >= IOV_MAX)
245 b0003f58 2021-03-08 op iov[i].iov_base = buf->buf + buf->rpos;
246 b0003f58 2021-03-08 op iov[i].iov_len = buf->wpos - buf->rpos;
248 b0003f58 2021-03-08 op if (buf->fd != -1)
252 b0003f58 2021-03-08 op msg.msg_iov = iov;
253 b0003f58 2021-03-08 op msg.msg_iovlen = i;
255 b0003f58 2021-03-08 op if (buf != NULL && buf->fd != -1) {
256 b0003f58 2021-03-08 op msg.msg_control = (caddr_t)&cmsgbuf.buf;
257 b0003f58 2021-03-08 op msg.msg_controllen = sizeof(cmsgbuf.buf);
258 b0003f58 2021-03-08 op cmsg = CMSG_FIRSTHDR(&msg);
259 b0003f58 2021-03-08 op cmsg->cmsg_len = CMSG_LEN(sizeof(int));
260 b0003f58 2021-03-08 op cmsg->cmsg_level = SOL_SOCKET;
261 b0003f58 2021-03-08 op cmsg->cmsg_type = SCM_RIGHTS;
262 b0003f58 2021-03-08 op *(int *)CMSG_DATA(cmsg) = buf->fd;
266 b0003f58 2021-03-08 op if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
267 b0003f58 2021-03-08 op if (errno == EINTR)
269 b0003f58 2021-03-08 op if (errno == ENOBUFS)
270 b0003f58 2021-03-08 op errno = EAGAIN;
274 b0003f58 2021-03-08 op if (n == 0) { /* connection closed */
280 b0003f58 2021-03-08 op * assumption: fd got sent if sendmsg sent anything
281 b0003f58 2021-03-08 op * this works because fds are passed one at a time
283 b0003f58 2021-03-08 op if (buf != NULL && buf->fd != -1) {
284 b0003f58 2021-03-08 op close(buf->fd);
285 b0003f58 2021-03-08 op buf->fd = -1;
288 b0003f58 2021-03-08 op msgbuf_drain(msgbuf, n);
294 b0003f58 2021-03-08 op ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
296 b0003f58 2021-03-08 op TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
297 b0003f58 2021-03-08 op msgbuf->queued++;
301 b0003f58 2021-03-08 op ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
303 b0003f58 2021-03-08 op TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
305 b0003f58 2021-03-08 op if (buf->fd != -1)
306 b0003f58 2021-03-08 op close(buf->fd);
308 b0003f58 2021-03-08 op msgbuf->queued--;
309 b0003f58 2021-03-08 op ibuf_free(buf);