1 291ecc47 2023-07-02 op /* $OpenBSD: imsg-buffer.c,v 1.16 2023/06/19 17:19:50 claudio Exp $ */
4 83f0f95a 2022-09-29 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 83f0f95a 2022-09-29 op * Permission to use, copy, modify, and distribute this software for any
7 83f0f95a 2022-09-29 op * purpose with or without fee is hereby granted, provided that the above
8 83f0f95a 2022-09-29 op * copyright notice and this permission notice appear in all copies.
10 83f0f95a 2022-09-29 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 83f0f95a 2022-09-29 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 83f0f95a 2022-09-29 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 83f0f95a 2022-09-29 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 83f0f95a 2022-09-29 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 83f0f95a 2022-09-29 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 83f0f95a 2022-09-29 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 83f0f95a 2022-09-29 op #include <sys/types.h>
20 83f0f95a 2022-09-29 op #include <sys/queue.h>
21 83f0f95a 2022-09-29 op #include <sys/socket.h>
22 83f0f95a 2022-09-29 op #include <sys/uio.h>
24 83f0f95a 2022-09-29 op #include <limits.h>
25 83f0f95a 2022-09-29 op #include <errno.h>
26 291ecc47 2023-07-02 op #include <endian.h>
27 83f0f95a 2022-09-29 op #include <stdlib.h>
28 83f0f95a 2022-09-29 op #include <string.h>
29 83f0f95a 2022-09-29 op #include <unistd.h>
31 83f0f95a 2022-09-29 op #include "imsg.h"
33 83f0f95a 2022-09-29 op static int ibuf_realloc(struct ibuf *, size_t);
34 83f0f95a 2022-09-29 op static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
35 83f0f95a 2022-09-29 op static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
36 291ecc47 2023-07-02 op static void msgbuf_drain(struct msgbuf *, size_t);
39 83f0f95a 2022-09-29 op ibuf_open(size_t len)
41 83f0f95a 2022-09-29 op struct ibuf *buf;
43 291ecc47 2023-07-02 op if (len == 0) {
44 291ecc47 2023-07-02 op errno = EINVAL;
45 291ecc47 2023-07-02 op return (NULL);
47 83f0f95a 2022-09-29 op if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
48 83f0f95a 2022-09-29 op return (NULL);
49 291ecc47 2023-07-02 op if ((buf->buf = calloc(len, 1)) == NULL) {
51 83f0f95a 2022-09-29 op return (NULL);
53 83f0f95a 2022-09-29 op buf->size = buf->max = len;
60 83f0f95a 2022-09-29 op ibuf_dynamic(size_t len, size_t max)
62 83f0f95a 2022-09-29 op struct ibuf *buf;
64 291ecc47 2023-07-02 op if (max < len) {
65 291ecc47 2023-07-02 op errno = EINVAL;
66 83f0f95a 2022-09-29 op return (NULL);
69 291ecc47 2023-07-02 op if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
70 83f0f95a 2022-09-29 op return (NULL);
71 291ecc47 2023-07-02 op if (len > 0) {
72 291ecc47 2023-07-02 op if ((buf->buf = calloc(len, 1)) == NULL) {
74 291ecc47 2023-07-02 op return (NULL);
77 291ecc47 2023-07-02 op buf->size = len;
78 291ecc47 2023-07-02 op buf->max = max;
85 83f0f95a 2022-09-29 op ibuf_realloc(struct ibuf *buf, size_t len)
87 83f0f95a 2022-09-29 op unsigned char *b;
89 83f0f95a 2022-09-29 op /* on static buffers max is eq size and so the following fails */
90 83f0f95a 2022-09-29 op if (len > SIZE_MAX - buf->wpos || buf->wpos + len > buf->max) {
91 83f0f95a 2022-09-29 op errno = ERANGE;
95 83f0f95a 2022-09-29 op b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
96 83f0f95a 2022-09-29 op if (b == NULL)
99 83f0f95a 2022-09-29 op buf->size = buf->wpos + len;
105 83f0f95a 2022-09-29 op ibuf_reserve(struct ibuf *buf, size_t len)
109 83f0f95a 2022-09-29 op if (len > SIZE_MAX - buf->wpos) {
110 83f0f95a 2022-09-29 op errno = ERANGE;
111 83f0f95a 2022-09-29 op return (NULL);
114 83f0f95a 2022-09-29 op if (buf->wpos + len > buf->size)
115 83f0f95a 2022-09-29 op if (ibuf_realloc(buf, len) == -1)
116 83f0f95a 2022-09-29 op return (NULL);
118 83f0f95a 2022-09-29 op b = buf->buf + buf->wpos;
119 83f0f95a 2022-09-29 op buf->wpos += len;
120 291ecc47 2023-07-02 op memset(b, 0, len);
125 291ecc47 2023-07-02 op ibuf_add(struct ibuf *buf, const void *data, size_t len)
129 291ecc47 2023-07-02 op if ((b = ibuf_reserve(buf, len)) == NULL)
132 291ecc47 2023-07-02 op memcpy(b, data, len);
137 291ecc47 2023-07-02 op ibuf_add_buf(struct ibuf *buf, const struct ibuf *from)
139 291ecc47 2023-07-02 op return ibuf_add(buf, from->buf, from->wpos);
143 291ecc47 2023-07-02 op ibuf_add_n8(struct ibuf *buf, uint64_t value)
147 291ecc47 2023-07-02 op if (value > UINT8_MAX) {
148 291ecc47 2023-07-02 op errno = EINVAL;
152 291ecc47 2023-07-02 op return ibuf_add(buf, &v, sizeof(v));
156 291ecc47 2023-07-02 op ibuf_add_n16(struct ibuf *buf, uint64_t value)
160 291ecc47 2023-07-02 op if (value > UINT16_MAX) {
161 291ecc47 2023-07-02 op errno = EINVAL;
164 291ecc47 2023-07-02 op v = htobe16(value);
165 291ecc47 2023-07-02 op return ibuf_add(buf, &v, sizeof(v));
169 291ecc47 2023-07-02 op ibuf_add_n32(struct ibuf *buf, uint64_t value)
173 291ecc47 2023-07-02 op if (value > UINT32_MAX) {
174 291ecc47 2023-07-02 op errno = EINVAL;
177 291ecc47 2023-07-02 op v = htobe32(value);
178 291ecc47 2023-07-02 op return ibuf_add(buf, &v, sizeof(v));
182 291ecc47 2023-07-02 op ibuf_add_n64(struct ibuf *buf, uint64_t value)
184 291ecc47 2023-07-02 op value = htobe64(value);
185 291ecc47 2023-07-02 op return ibuf_add(buf, &value, sizeof(value));
189 291ecc47 2023-07-02 op ibuf_add_zero(struct ibuf *buf, size_t len)
193 291ecc47 2023-07-02 op if ((b = ibuf_reserve(buf, len)) == NULL)
199 83f0f95a 2022-09-29 op ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
201 83f0f95a 2022-09-29 op /* only allowed to seek in already written parts */
202 291ecc47 2023-07-02 op if (len > SIZE_MAX - pos || pos + len > buf->wpos) {
203 291ecc47 2023-07-02 op errno = ERANGE;
204 83f0f95a 2022-09-29 op return (NULL);
207 83f0f95a 2022-09-29 op return (buf->buf + pos);
211 291ecc47 2023-07-02 op ibuf_set(struct ibuf *buf, size_t pos, const void *data, size_t len)
215 291ecc47 2023-07-02 op if ((b = ibuf_seek(buf, pos, len)) == NULL)
218 291ecc47 2023-07-02 op memcpy(b, data, len);
223 291ecc47 2023-07-02 op ibuf_set_n8(struct ibuf *buf, size_t pos, uint64_t value)
227 291ecc47 2023-07-02 op if (value > UINT8_MAX) {
228 291ecc47 2023-07-02 op errno = EINVAL;
232 291ecc47 2023-07-02 op return (ibuf_set(buf, pos, &v, sizeof(v)));
236 291ecc47 2023-07-02 op ibuf_set_n16(struct ibuf *buf, size_t pos, uint64_t value)
240 291ecc47 2023-07-02 op if (value > UINT16_MAX) {
241 291ecc47 2023-07-02 op errno = EINVAL;
244 291ecc47 2023-07-02 op v = htobe16(value);
245 291ecc47 2023-07-02 op return (ibuf_set(buf, pos, &v, sizeof(v)));
249 291ecc47 2023-07-02 op ibuf_set_n32(struct ibuf *buf, size_t pos, uint64_t value)
253 291ecc47 2023-07-02 op if (value > UINT32_MAX) {
254 291ecc47 2023-07-02 op errno = EINVAL;
257 291ecc47 2023-07-02 op v = htobe32(value);
258 291ecc47 2023-07-02 op return (ibuf_set(buf, pos, &v, sizeof(v)));
262 291ecc47 2023-07-02 op ibuf_set_n64(struct ibuf *buf, size_t pos, uint64_t value)
264 291ecc47 2023-07-02 op value = htobe64(value);
265 291ecc47 2023-07-02 op return (ibuf_set(buf, pos, &value, sizeof(value)));
269 291ecc47 2023-07-02 op ibuf_data(struct ibuf *buf)
271 291ecc47 2023-07-02 op return (buf->buf);
275 83f0f95a 2022-09-29 op ibuf_size(struct ibuf *buf)
277 83f0f95a 2022-09-29 op return (buf->wpos);
281 83f0f95a 2022-09-29 op ibuf_left(struct ibuf *buf)
283 83f0f95a 2022-09-29 op return (buf->max - buf->wpos);
287 83f0f95a 2022-09-29 op ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
289 83f0f95a 2022-09-29 op ibuf_enqueue(msgbuf, buf);
293 291ecc47 2023-07-02 op ibuf_free(struct ibuf *buf)
295 291ecc47 2023-07-02 op if (buf == NULL)
297 291ecc47 2023-07-02 op #ifdef NOTYET
298 291ecc47 2023-07-02 op if (buf->fd != -1)
299 291ecc47 2023-07-02 op close(buf->fd);
301 291ecc47 2023-07-02 op freezero(buf->buf, buf->size);
306 291ecc47 2023-07-02 op ibuf_fd_avail(struct ibuf *buf)
308 291ecc47 2023-07-02 op return (buf->fd != -1);
312 291ecc47 2023-07-02 op ibuf_fd_get(struct ibuf *buf)
316 291ecc47 2023-07-02 op fd = buf->fd;
317 291ecc47 2023-07-02 op #ifdef NOTYET
318 291ecc47 2023-07-02 op buf->fd = -1;
324 291ecc47 2023-07-02 op ibuf_fd_set(struct ibuf *buf, int fd)
326 291ecc47 2023-07-02 op if (buf->fd != -1)
327 291ecc47 2023-07-02 op close(buf->fd);
328 291ecc47 2023-07-02 op buf->fd = fd;
332 83f0f95a 2022-09-29 op ibuf_write(struct msgbuf *msgbuf)
334 83f0f95a 2022-09-29 op struct iovec iov[IOV_MAX];
335 83f0f95a 2022-09-29 op struct ibuf *buf;
336 83f0f95a 2022-09-29 op unsigned int i = 0;
339 83f0f95a 2022-09-29 op memset(&iov, 0, sizeof(iov));
340 83f0f95a 2022-09-29 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
341 83f0f95a 2022-09-29 op if (i >= IOV_MAX)
343 83f0f95a 2022-09-29 op iov[i].iov_base = buf->buf + buf->rpos;
344 83f0f95a 2022-09-29 op iov[i].iov_len = buf->wpos - buf->rpos;
349 83f0f95a 2022-09-29 op if ((n = writev(msgbuf->fd, iov, i)) == -1) {
350 83f0f95a 2022-09-29 op if (errno == EINTR)
352 83f0f95a 2022-09-29 op if (errno == ENOBUFS)
353 83f0f95a 2022-09-29 op errno = EAGAIN;
357 83f0f95a 2022-09-29 op if (n == 0) { /* connection closed */
362 83f0f95a 2022-09-29 op msgbuf_drain(msgbuf, n);
368 83f0f95a 2022-09-29 op msgbuf_init(struct msgbuf *msgbuf)
370 83f0f95a 2022-09-29 op msgbuf->queued = 0;
371 83f0f95a 2022-09-29 op msgbuf->fd = -1;
372 83f0f95a 2022-09-29 op TAILQ_INIT(&msgbuf->bufs);
376 83f0f95a 2022-09-29 op msgbuf_drain(struct msgbuf *msgbuf, size_t n)
378 83f0f95a 2022-09-29 op struct ibuf *buf, *next;
380 83f0f95a 2022-09-29 op for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
381 83f0f95a 2022-09-29 op buf = next) {
382 83f0f95a 2022-09-29 op next = TAILQ_NEXT(buf, entry);
383 83f0f95a 2022-09-29 op if (n >= buf->wpos - buf->rpos) {
384 83f0f95a 2022-09-29 op n -= buf->wpos - buf->rpos;
385 83f0f95a 2022-09-29 op ibuf_dequeue(msgbuf, buf);
387 83f0f95a 2022-09-29 op buf->rpos += n;
394 83f0f95a 2022-09-29 op msgbuf_clear(struct msgbuf *msgbuf)
396 83f0f95a 2022-09-29 op struct ibuf *buf;
398 83f0f95a 2022-09-29 op while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
399 83f0f95a 2022-09-29 op ibuf_dequeue(msgbuf, buf);
403 83f0f95a 2022-09-29 op msgbuf_write(struct msgbuf *msgbuf)
405 83f0f95a 2022-09-29 op struct iovec iov[IOV_MAX];
406 83f0f95a 2022-09-29 op struct ibuf *buf, *buf0 = NULL;
407 83f0f95a 2022-09-29 op unsigned int i = 0;
409 83f0f95a 2022-09-29 op struct msghdr msg;
410 83f0f95a 2022-09-29 op struct cmsghdr *cmsg;
412 83f0f95a 2022-09-29 op struct cmsghdr hdr;
413 83f0f95a 2022-09-29 op char buf[CMSG_SPACE(sizeof(int))];
416 83f0f95a 2022-09-29 op memset(&iov, 0, sizeof(iov));
417 83f0f95a 2022-09-29 op memset(&msg, 0, sizeof(msg));
418 83f0f95a 2022-09-29 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
419 83f0f95a 2022-09-29 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
420 83f0f95a 2022-09-29 op if (i >= IOV_MAX)
422 83f0f95a 2022-09-29 op if (i > 0 && buf->fd != -1)
424 83f0f95a 2022-09-29 op iov[i].iov_base = buf->buf + buf->rpos;
425 83f0f95a 2022-09-29 op iov[i].iov_len = buf->wpos - buf->rpos;
427 83f0f95a 2022-09-29 op if (buf->fd != -1)
431 83f0f95a 2022-09-29 op msg.msg_iov = iov;
432 83f0f95a 2022-09-29 op msg.msg_iovlen = i;
434 83f0f95a 2022-09-29 op if (buf0 != NULL) {
435 83f0f95a 2022-09-29 op msg.msg_control = (caddr_t)&cmsgbuf.buf;
436 83f0f95a 2022-09-29 op msg.msg_controllen = sizeof(cmsgbuf.buf);
437 83f0f95a 2022-09-29 op cmsg = CMSG_FIRSTHDR(&msg);
438 83f0f95a 2022-09-29 op cmsg->cmsg_len = CMSG_LEN(sizeof(int));
439 83f0f95a 2022-09-29 op cmsg->cmsg_level = SOL_SOCKET;
440 83f0f95a 2022-09-29 op cmsg->cmsg_type = SCM_RIGHTS;
441 83f0f95a 2022-09-29 op *(int *)CMSG_DATA(cmsg) = buf0->fd;
445 83f0f95a 2022-09-29 op if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
446 83f0f95a 2022-09-29 op if (errno == EINTR)
448 83f0f95a 2022-09-29 op if (errno == ENOBUFS)
449 83f0f95a 2022-09-29 op errno = EAGAIN;
453 83f0f95a 2022-09-29 op if (n == 0) { /* connection closed */
459 83f0f95a 2022-09-29 op * assumption: fd got sent if sendmsg sent anything
460 83f0f95a 2022-09-29 op * this works because fds are passed one at a time
462 83f0f95a 2022-09-29 op if (buf0 != NULL) {
463 83f0f95a 2022-09-29 op close(buf0->fd);
464 83f0f95a 2022-09-29 op buf0->fd = -1;
467 83f0f95a 2022-09-29 op msgbuf_drain(msgbuf, n);
473 83f0f95a 2022-09-29 op ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
475 83f0f95a 2022-09-29 op TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
476 83f0f95a 2022-09-29 op msgbuf->queued++;
480 83f0f95a 2022-09-29 op ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
482 83f0f95a 2022-09-29 op TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
484 291ecc47 2023-07-02 op if (buf->fd != -1) {
485 83f0f95a 2022-09-29 op close(buf->fd);
486 291ecc47 2023-07-02 op buf->fd = -1;
489 83f0f95a 2022-09-29 op msgbuf->queued--;
490 83f0f95a 2022-09-29 op ibuf_free(buf);