mirror of
https://github.com/tmux/tmux.git
synced 2025-09-02 13:37:12 +00:00
Update imsg from OpenBSD.
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
/* $OpenBSD: imsg-buffer.c,v 1.31 2024/11/26 13:57:31 claudio Exp $ */
|
/* $OpenBSD: imsg-buffer.c,v 1.35 2025/06/04 09:06:56 claudio Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
|
* Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
|
||||||
@ -20,7 +20,6 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/uio.h>
|
#include <sys/uio.h>
|
||||||
#include <arpa/inet.h>
|
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
@ -45,10 +44,14 @@
|
|||||||
#undef be64toh
|
#undef be64toh
|
||||||
#define be64toh ntohll
|
#define be64toh ntohll
|
||||||
|
|
||||||
|
struct ibufqueue {
|
||||||
|
TAILQ_HEAD(, ibuf) bufs;
|
||||||
|
uint32_t queued;
|
||||||
|
};
|
||||||
|
|
||||||
struct msgbuf {
|
struct msgbuf {
|
||||||
TAILQ_HEAD(, ibuf) bufs;
|
struct ibufqueue bufs;
|
||||||
TAILQ_HEAD(, ibuf) rbufs;
|
struct ibufqueue rbufs;
|
||||||
uint32_t queued;
|
|
||||||
char *rbuf;
|
char *rbuf;
|
||||||
struct ibuf *rpmsg;
|
struct ibuf *rpmsg;
|
||||||
struct ibuf *(*readhdr)(struct ibuf *, void *, int *);
|
struct ibuf *(*readhdr)(struct ibuf *, void *, int *);
|
||||||
@ -57,10 +60,8 @@ struct msgbuf {
|
|||||||
size_t hdrsize;
|
size_t hdrsize;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void msgbuf_read_enqueue(struct msgbuf *, struct ibuf *);
|
|
||||||
static void msgbuf_enqueue(struct msgbuf *, struct ibuf *);
|
|
||||||
static void msgbuf_dequeue(struct msgbuf *, struct ibuf *);
|
|
||||||
static void msgbuf_drain(struct msgbuf *, size_t);
|
static void msgbuf_drain(struct msgbuf *, size_t);
|
||||||
|
static void ibufq_init(struct ibufqueue *);
|
||||||
|
|
||||||
#define IBUF_FD_MARK_ON_STACK -2
|
#define IBUF_FD_MARK_ON_STACK -2
|
||||||
|
|
||||||
@ -149,6 +150,9 @@ ibuf_add(struct ibuf *buf, const void *data, size_t len)
|
|||||||
{
|
{
|
||||||
void *b;
|
void *b;
|
||||||
|
|
||||||
|
if (len == 0)
|
||||||
|
return (0);
|
||||||
|
|
||||||
if ((b = ibuf_reserve(buf, len)) == NULL)
|
if ((b = ibuf_reserve(buf, len)) == NULL)
|
||||||
return (-1);
|
return (-1);
|
||||||
|
|
||||||
@ -245,12 +249,34 @@ ibuf_add_zero(struct ibuf *buf, size_t len)
|
|||||||
{
|
{
|
||||||
void *b;
|
void *b;
|
||||||
|
|
||||||
|
if (len == 0)
|
||||||
|
return (0);
|
||||||
|
|
||||||
if ((b = ibuf_reserve(buf, len)) == NULL)
|
if ((b = ibuf_reserve(buf, len)) == NULL)
|
||||||
return (-1);
|
return (-1);
|
||||||
memset(b, 0, len);
|
memset(b, 0, len);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
ibuf_add_strbuf(struct ibuf *buf, const char *str, size_t len)
|
||||||
|
{
|
||||||
|
char *b;
|
||||||
|
size_t n;
|
||||||
|
|
||||||
|
if ((b = ibuf_reserve(buf, len)) == NULL)
|
||||||
|
return (-1);
|
||||||
|
|
||||||
|
n = strlcpy(b, str, len);
|
||||||
|
if (n >= len) {
|
||||||
|
/* also covers the case where len == 0 */
|
||||||
|
errno = EOVERFLOW;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
memset(b + n, 0, len - n);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
void *
|
void *
|
||||||
ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
|
ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
|
||||||
{
|
{
|
||||||
@ -272,6 +298,8 @@ ibuf_set(struct ibuf *buf, size_t pos, const void *data, size_t len)
|
|||||||
if ((b = ibuf_seek(buf, pos, len)) == NULL)
|
if ((b = ibuf_seek(buf, pos, len)) == NULL)
|
||||||
return (-1);
|
return (-1);
|
||||||
|
|
||||||
|
if (len == 0)
|
||||||
|
return (0);
|
||||||
memcpy(b, data, len);
|
memcpy(b, data, len);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
@ -354,6 +382,22 @@ ibuf_set_h64(struct ibuf *buf, size_t pos, uint64_t value)
|
|||||||
return (ibuf_set(buf, pos, &value, sizeof(value)));
|
return (ibuf_set(buf, pos, &value, sizeof(value)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
ibuf_set_maxsize(struct ibuf *buf, size_t max)
|
||||||
|
{
|
||||||
|
if (buf->fd == IBUF_FD_MARK_ON_STACK) {
|
||||||
|
/* can't fiddle with stack buffers */
|
||||||
|
errno = EINVAL;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
if (max > buf->max) {
|
||||||
|
errno = ERANGE;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
buf->max = max;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
void *
|
void *
|
||||||
ibuf_data(const struct ibuf *buf)
|
ibuf_data(const struct ibuf *buf)
|
||||||
{
|
{
|
||||||
@ -399,7 +443,7 @@ ibuf_rewind(struct ibuf *buf)
|
|||||||
void
|
void
|
||||||
ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
|
ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
|
||||||
{
|
{
|
||||||
msgbuf_enqueue(msgbuf, buf);
|
ibufq_push(&msgbuf->bufs, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -514,6 +558,24 @@ ibuf_get_string(struct ibuf *buf, size_t len)
|
|||||||
return (str);
|
return (str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
ibuf_get_strbuf(struct ibuf *buf, char *str, size_t len)
|
||||||
|
{
|
||||||
|
if (len == 0) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ibuf_get(buf, str, len) == -1)
|
||||||
|
return -1;
|
||||||
|
if (str[len - 1] != '\0') {
|
||||||
|
str[len - 1] = '\0';
|
||||||
|
errno = EOVERFLOW;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
ibuf_skip(struct ibuf *buf, size_t len)
|
ibuf_skip(struct ibuf *buf, size_t len)
|
||||||
{
|
{
|
||||||
@ -529,6 +591,8 @@ ibuf_skip(struct ibuf *buf, size_t len)
|
|||||||
void
|
void
|
||||||
ibuf_free(struct ibuf *buf)
|
ibuf_free(struct ibuf *buf)
|
||||||
{
|
{
|
||||||
|
int save_errno = errno;
|
||||||
|
|
||||||
if (buf == NULL)
|
if (buf == NULL)
|
||||||
return;
|
return;
|
||||||
/* if buf lives on the stack abort before causing more harm */
|
/* if buf lives on the stack abort before causing more harm */
|
||||||
@ -538,6 +602,7 @@ ibuf_free(struct ibuf *buf)
|
|||||||
close(buf->fd);
|
close(buf->fd);
|
||||||
freezero(buf->buf, buf->size);
|
freezero(buf->buf, buf->size);
|
||||||
free(buf);
|
free(buf);
|
||||||
|
errno = save_errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -579,9 +644,8 @@ msgbuf_new(void)
|
|||||||
|
|
||||||
if ((msgbuf = calloc(1, sizeof(*msgbuf))) == NULL)
|
if ((msgbuf = calloc(1, sizeof(*msgbuf))) == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
msgbuf->queued = 0;
|
ibufq_init(&msgbuf->bufs);
|
||||||
TAILQ_INIT(&msgbuf->bufs);
|
ibufq_init(&msgbuf->rbufs);
|
||||||
TAILQ_INIT(&msgbuf->rbufs);
|
|
||||||
|
|
||||||
return msgbuf;
|
return msgbuf;
|
||||||
}
|
}
|
||||||
@ -628,7 +692,7 @@ msgbuf_free(struct msgbuf *msgbuf)
|
|||||||
uint32_t
|
uint32_t
|
||||||
msgbuf_queuelen(struct msgbuf *msgbuf)
|
msgbuf_queuelen(struct msgbuf *msgbuf)
|
||||||
{
|
{
|
||||||
return (msgbuf->queued);
|
return ibufq_queuelen(&msgbuf->bufs);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -637,15 +701,10 @@ msgbuf_clear(struct msgbuf *msgbuf)
|
|||||||
struct ibuf *buf;
|
struct ibuf *buf;
|
||||||
|
|
||||||
/* write side */
|
/* write side */
|
||||||
while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
|
ibufq_flush(&msgbuf->bufs);
|
||||||
msgbuf_dequeue(msgbuf, buf);
|
|
||||||
msgbuf->queued = 0;
|
|
||||||
|
|
||||||
/* read side */
|
/* read side */
|
||||||
while ((buf = TAILQ_FIRST(&msgbuf->rbufs)) != NULL) {
|
ibufq_flush(&msgbuf->rbufs);
|
||||||
TAILQ_REMOVE(&msgbuf->rbufs, buf, entry);
|
|
||||||
ibuf_free(buf);
|
|
||||||
}
|
|
||||||
msgbuf->roff = 0;
|
msgbuf->roff = 0;
|
||||||
ibuf_free(msgbuf->rpmsg);
|
ibuf_free(msgbuf->rpmsg);
|
||||||
msgbuf->rpmsg = NULL;
|
msgbuf->rpmsg = NULL;
|
||||||
@ -654,11 +713,13 @@ msgbuf_clear(struct msgbuf *msgbuf)
|
|||||||
struct ibuf *
|
struct ibuf *
|
||||||
msgbuf_get(struct msgbuf *msgbuf)
|
msgbuf_get(struct msgbuf *msgbuf)
|
||||||
{
|
{
|
||||||
struct ibuf *buf;
|
return ibufq_pop(&msgbuf->rbufs);
|
||||||
|
}
|
||||||
|
|
||||||
if ((buf = TAILQ_FIRST(&msgbuf->rbufs)) != NULL)
|
void
|
||||||
TAILQ_REMOVE(&msgbuf->rbufs, buf, entry);
|
msgbuf_concat(struct msgbuf *msgbuf, struct ibufqueue *from)
|
||||||
return buf;
|
{
|
||||||
|
ibufq_concat(&msgbuf->bufs, from);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -670,7 +731,7 @@ ibuf_write(int fd, struct msgbuf *msgbuf)
|
|||||||
ssize_t n;
|
ssize_t n;
|
||||||
|
|
||||||
memset(&iov, 0, sizeof(iov));
|
memset(&iov, 0, sizeof(iov));
|
||||||
TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
|
TAILQ_FOREACH(buf, &msgbuf->bufs.bufs, entry) {
|
||||||
if (i >= IOV_MAX)
|
if (i >= IOV_MAX)
|
||||||
break;
|
break;
|
||||||
iov[i].iov_base = ibuf_data(buf);
|
iov[i].iov_base = ibuf_data(buf);
|
||||||
@ -711,7 +772,7 @@ msgbuf_write(int fd, struct msgbuf *msgbuf)
|
|||||||
memset(&iov, 0, sizeof(iov));
|
memset(&iov, 0, sizeof(iov));
|
||||||
memset(&msg, 0, sizeof(msg));
|
memset(&msg, 0, sizeof(msg));
|
||||||
memset(&cmsgbuf, 0, sizeof(cmsgbuf));
|
memset(&cmsgbuf, 0, sizeof(cmsgbuf));
|
||||||
TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
|
TAILQ_FOREACH(buf, &msgbuf->bufs.bufs, entry) {
|
||||||
if (i >= IOV_MAX)
|
if (i >= IOV_MAX)
|
||||||
break;
|
break;
|
||||||
if (i > 0 && buf->fd != -1)
|
if (i > 0 && buf->fd != -1)
|
||||||
@ -794,7 +855,7 @@ ibuf_read_process(struct msgbuf *msgbuf, int fd)
|
|||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
if (ibuf_left(msgbuf->rpmsg) == 0) {
|
if (ibuf_left(msgbuf->rpmsg) == 0) {
|
||||||
msgbuf_read_enqueue(msgbuf, msgbuf->rpmsg);
|
ibufq_push(&msgbuf->rbufs, msgbuf->rpmsg);
|
||||||
msgbuf->rpmsg = NULL;
|
msgbuf->rpmsg = NULL;
|
||||||
}
|
}
|
||||||
} while (ibuf_size(&rbuf) > 0);
|
} while (ibuf_size(&rbuf) > 0);
|
||||||
@ -922,47 +983,95 @@ again:
|
|||||||
return (ibuf_read_process(msgbuf, fdpass));
|
return (ibuf_read_process(msgbuf, fdpass));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
msgbuf_read_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
|
|
||||||
{
|
|
||||||
/* if buf lives on the stack abort before causing more harm */
|
|
||||||
if (buf->fd == IBUF_FD_MARK_ON_STACK)
|
|
||||||
abort();
|
|
||||||
TAILQ_INSERT_TAIL(&msgbuf->rbufs, buf, entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
msgbuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
|
|
||||||
{
|
|
||||||
/* if buf lives on the stack abort before causing more harm */
|
|
||||||
if (buf->fd == IBUF_FD_MARK_ON_STACK)
|
|
||||||
abort();
|
|
||||||
TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
|
|
||||||
msgbuf->queued++;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
msgbuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
|
|
||||||
{
|
|
||||||
TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
|
|
||||||
msgbuf->queued--;
|
|
||||||
ibuf_free(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
msgbuf_drain(struct msgbuf *msgbuf, size_t n)
|
msgbuf_drain(struct msgbuf *msgbuf, size_t n)
|
||||||
{
|
{
|
||||||
struct ibuf *buf, *next;
|
struct ibuf *buf;
|
||||||
|
|
||||||
for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
|
while ((buf = TAILQ_FIRST(&msgbuf->bufs.bufs)) != NULL) {
|
||||||
buf = next) {
|
|
||||||
next = TAILQ_NEXT(buf, entry);
|
|
||||||
if (n >= ibuf_size(buf)) {
|
if (n >= ibuf_size(buf)) {
|
||||||
n -= ibuf_size(buf);
|
n -= ibuf_size(buf);
|
||||||
msgbuf_dequeue(msgbuf, buf);
|
TAILQ_REMOVE(&msgbuf->bufs.bufs, buf, entry);
|
||||||
|
msgbuf->bufs.queued--;
|
||||||
|
ibuf_free(buf);
|
||||||
} else {
|
} else {
|
||||||
buf->rpos += n;
|
buf->rpos += n;
|
||||||
n = 0;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
ibufq_init(struct ibufqueue *bufq)
|
||||||
|
{
|
||||||
|
TAILQ_INIT(&bufq->bufs);
|
||||||
|
bufq->queued = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ibufqueue *
|
||||||
|
ibufq_new(void)
|
||||||
|
{
|
||||||
|
struct ibufqueue *bufq;
|
||||||
|
|
||||||
|
if ((bufq = calloc(1, sizeof(*bufq))) == NULL)
|
||||||
|
return NULL;
|
||||||
|
ibufq_init(bufq);
|
||||||
|
return bufq;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ibufq_free(struct ibufqueue *bufq)
|
||||||
|
{
|
||||||
|
if (bufq == NULL)
|
||||||
|
return;
|
||||||
|
ibufq_flush(bufq);
|
||||||
|
free(bufq);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ibuf *
|
||||||
|
ibufq_pop(struct ibufqueue *bufq)
|
||||||
|
{
|
||||||
|
struct ibuf *buf;
|
||||||
|
|
||||||
|
if ((buf = TAILQ_FIRST(&bufq->bufs)) == NULL)
|
||||||
|
return NULL;
|
||||||
|
TAILQ_REMOVE(&bufq->bufs, buf, entry);
|
||||||
|
bufq->queued--;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ibufq_push(struct ibufqueue *bufq, struct ibuf *buf)
|
||||||
|
{
|
||||||
|
/* if buf lives on the stack abort before causing more harm */
|
||||||
|
if (buf->fd == IBUF_FD_MARK_ON_STACK)
|
||||||
|
abort();
|
||||||
|
TAILQ_INSERT_TAIL(&bufq->bufs, buf, entry);
|
||||||
|
bufq->queued++;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t
|
||||||
|
ibufq_queuelen(struct ibufqueue *bufq)
|
||||||
|
{
|
||||||
|
return (bufq->queued);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ibufq_concat(struct ibufqueue *to, struct ibufqueue *from)
|
||||||
|
{
|
||||||
|
to->queued += from->queued;
|
||||||
|
TAILQ_CONCAT(&to->bufs, &from->bufs, entry);
|
||||||
|
from->queued = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ibufq_flush(struct ibufqueue *bufq)
|
||||||
|
{
|
||||||
|
struct ibuf *buf;
|
||||||
|
|
||||||
|
while ((buf = TAILQ_FIRST(&bufq->bufs)) != NULL) {
|
||||||
|
TAILQ_REMOVE(&bufq->bufs, buf, entry);
|
||||||
|
ibuf_free(buf);
|
||||||
|
}
|
||||||
|
bufq->queued = 0;
|
||||||
|
}
|
||||||
|
126
compat/imsg.c
126
compat/imsg.c
@ -1,4 +1,4 @@
|
|||||||
/* $OpenBSD: imsg.c,v 1.37 2024/11/26 13:57:31 claudio Exp $ */
|
/* $OpenBSD: imsg.c,v 1.42 2025/06/16 13:56:11 claudio Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
|
* Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
|
||||||
@ -56,13 +56,18 @@ imsgbuf_allow_fdpass(struct imsgbuf *imsgbuf)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
imsgbuf_set_maxsize(struct imsgbuf *imsgbuf, uint32_t maxsize)
|
imsgbuf_set_maxsize(struct imsgbuf *imsgbuf, uint32_t max)
|
||||||
{
|
{
|
||||||
if (maxsize < IMSG_HEADER_SIZE || maxsize & IMSG_FD_MARK) {
|
if (max > UINT32_MAX - IMSG_HEADER_SIZE) {
|
||||||
|
errno = ERANGE;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
max += IMSG_HEADER_SIZE;
|
||||||
|
if (max & IMSG_FD_MARK) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
imsgbuf->maxsize = maxsize;
|
imsgbuf->maxsize = max;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,11 +112,11 @@ imsgbuf_queuelen(struct imsgbuf *imsgbuf)
|
|||||||
return msgbuf_queuelen(imsgbuf->w);
|
return msgbuf_queuelen(imsgbuf->w);
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t
|
int
|
||||||
imsg_get(struct imsgbuf *imsgbuf, struct imsg *imsg)
|
imsgbuf_get(struct imsgbuf *imsgbuf, struct imsg *imsg)
|
||||||
{
|
{
|
||||||
struct imsg m;
|
struct imsg m;
|
||||||
struct ibuf *buf;
|
struct ibuf *buf;
|
||||||
|
|
||||||
if ((buf = msgbuf_get(imsgbuf->w)) == NULL)
|
if ((buf = msgbuf_get(imsgbuf->w)) == NULL)
|
||||||
return (0);
|
return (0);
|
||||||
@ -127,7 +132,48 @@ imsg_get(struct imsgbuf *imsgbuf, struct imsg *imsg)
|
|||||||
m.hdr.len &= ~IMSG_FD_MARK;
|
m.hdr.len &= ~IMSG_FD_MARK;
|
||||||
|
|
||||||
*imsg = m;
|
*imsg = m;
|
||||||
return (ibuf_size(buf) + IMSG_HEADER_SIZE);
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t
|
||||||
|
imsg_get(struct imsgbuf *imsgbuf, struct imsg *imsg)
|
||||||
|
{
|
||||||
|
int rv;
|
||||||
|
|
||||||
|
if ((rv = imsgbuf_get(imsgbuf, imsg)) != 1)
|
||||||
|
return rv;
|
||||||
|
return (imsg_get_len(imsg) + IMSG_HEADER_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
imsg_ibufq_pop(struct ibufqueue *bufq, struct imsg *imsg)
|
||||||
|
{
|
||||||
|
struct imsg m;
|
||||||
|
struct ibuf *buf;
|
||||||
|
|
||||||
|
if ((buf = ibufq_pop(bufq)) == NULL)
|
||||||
|
return (0);
|
||||||
|
|
||||||
|
if (ibuf_get(buf, &m.hdr, sizeof(m.hdr)) == -1)
|
||||||
|
return (-1);
|
||||||
|
|
||||||
|
if (ibuf_size(buf))
|
||||||
|
m.data = ibuf_data(buf);
|
||||||
|
else
|
||||||
|
m.data = NULL;
|
||||||
|
m.buf = buf;
|
||||||
|
m.hdr.len &= ~IMSG_FD_MARK;
|
||||||
|
|
||||||
|
*imsg = m;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
imsg_ibufq_push(struct ibufqueue *bufq, struct imsg *imsg)
|
||||||
|
{
|
||||||
|
ibuf_rewind(imsg->buf);
|
||||||
|
ibufq_push(bufq, imsg->buf);
|
||||||
|
memset(imsg, 0, sizeof(*imsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -154,6 +200,18 @@ imsg_get_data(struct imsg *imsg, void *data, size_t len)
|
|||||||
return ibuf_get(imsg->buf, data, len);
|
return ibuf_get(imsg->buf, data, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
imsg_get_buf(struct imsg *imsg, void *data, size_t len)
|
||||||
|
{
|
||||||
|
return ibuf_get(imsg->buf, data, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
imsg_get_strbuf(struct imsg *imsg, char *str, size_t len)
|
||||||
|
{
|
||||||
|
return ibuf_get_strbuf(imsg->buf, str, len);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
imsg_get_fd(struct imsg *imsg)
|
imsg_get_fd(struct imsg *imsg)
|
||||||
{
|
{
|
||||||
@ -191,15 +249,19 @@ imsg_compose(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
|
|||||||
struct ibuf *wbuf;
|
struct ibuf *wbuf;
|
||||||
|
|
||||||
if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
|
if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
|
||||||
return (-1);
|
goto fail;
|
||||||
|
|
||||||
if (imsg_add(wbuf, data, datalen) == -1)
|
if (ibuf_add(wbuf, data, datalen) == -1)
|
||||||
return (-1);
|
goto fail;
|
||||||
|
|
||||||
ibuf_fd_set(wbuf, fd);
|
ibuf_fd_set(wbuf, fd);
|
||||||
imsg_close(imsgbuf, wbuf);
|
imsg_close(imsgbuf, wbuf);
|
||||||
|
|
||||||
return (1);
|
return (1);
|
||||||
|
|
||||||
|
fail:
|
||||||
|
ibuf_free(wbuf);
|
||||||
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -214,16 +276,20 @@ imsg_composev(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
|
|||||||
datalen += iov[i].iov_len;
|
datalen += iov[i].iov_len;
|
||||||
|
|
||||||
if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
|
if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
|
||||||
return (-1);
|
goto fail;
|
||||||
|
|
||||||
for (i = 0; i < iovcnt; i++)
|
for (i = 0; i < iovcnt; i++)
|
||||||
if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
|
if (ibuf_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
|
||||||
return (-1);
|
goto fail;
|
||||||
|
|
||||||
ibuf_fd_set(wbuf, fd);
|
ibuf_fd_set(wbuf, fd);
|
||||||
imsg_close(imsgbuf, wbuf);
|
imsg_close(imsgbuf, wbuf);
|
||||||
|
|
||||||
return (1);
|
return (1);
|
||||||
|
|
||||||
|
fail:
|
||||||
|
ibuf_free(wbuf);
|
||||||
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -236,7 +302,6 @@ imsg_compose_ibuf(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id,
|
|||||||
{
|
{
|
||||||
struct ibuf *hdrbuf = NULL;
|
struct ibuf *hdrbuf = NULL;
|
||||||
struct imsg_hdr hdr;
|
struct imsg_hdr hdr;
|
||||||
int save_errno;
|
|
||||||
|
|
||||||
if (ibuf_size(buf) + IMSG_HEADER_SIZE > imsgbuf->maxsize) {
|
if (ibuf_size(buf) + IMSG_HEADER_SIZE > imsgbuf->maxsize) {
|
||||||
errno = ERANGE;
|
errno = ERANGE;
|
||||||
@ -251,7 +316,7 @@ imsg_compose_ibuf(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id,
|
|||||||
|
|
||||||
if ((hdrbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
|
if ((hdrbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
|
||||||
goto fail;
|
goto fail;
|
||||||
if (imsg_add(hdrbuf, &hdr, sizeof(hdr)) == -1)
|
if (ibuf_add(hdrbuf, &hdr, sizeof(hdr)) == -1)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
ibuf_close(imsgbuf->w, hdrbuf);
|
ibuf_close(imsgbuf->w, hdrbuf);
|
||||||
@ -259,10 +324,8 @@ imsg_compose_ibuf(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id,
|
|||||||
return (1);
|
return (1);
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
save_errno = errno;
|
|
||||||
ibuf_free(buf);
|
ibuf_free(buf);
|
||||||
ibuf_free(hdrbuf);
|
ibuf_free(hdrbuf);
|
||||||
errno = save_errno;
|
|
||||||
return (-1);
|
return (-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -307,17 +370,21 @@ imsg_create(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
|
|||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hdr.len = 0;
|
||||||
hdr.type = type;
|
hdr.type = type;
|
||||||
hdr.peerid = id;
|
hdr.peerid = id;
|
||||||
if ((hdr.pid = pid) == 0)
|
if ((hdr.pid = pid) == 0)
|
||||||
hdr.pid = imsgbuf->pid;
|
hdr.pid = imsgbuf->pid;
|
||||||
if ((wbuf = ibuf_dynamic(datalen, imsgbuf->maxsize)) == NULL) {
|
if ((wbuf = ibuf_dynamic(datalen, imsgbuf->maxsize)) == NULL)
|
||||||
return (NULL);
|
goto fail;
|
||||||
}
|
if (ibuf_add(wbuf, &hdr, sizeof(hdr)) == -1)
|
||||||
if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
|
goto fail;
|
||||||
return (NULL);
|
|
||||||
|
|
||||||
return (wbuf);
|
return (wbuf);
|
||||||
|
|
||||||
|
fail:
|
||||||
|
ibuf_free(wbuf);
|
||||||
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -334,7 +401,6 @@ imsg_add(struct ibuf *msg, const void *data, size_t datalen)
|
|||||||
void
|
void
|
||||||
imsg_close(struct imsgbuf *imsgbuf, struct ibuf *msg)
|
imsg_close(struct imsgbuf *imsgbuf, struct ibuf *msg)
|
||||||
{
|
{
|
||||||
struct imsg_hdr *hdr;
|
|
||||||
uint32_t len;
|
uint32_t len;
|
||||||
|
|
||||||
len = ibuf_size(msg);
|
len = ibuf_size(msg);
|
||||||
@ -350,6 +416,16 @@ imsg_free(struct imsg *imsg)
|
|||||||
ibuf_free(imsg->buf);
|
ibuf_free(imsg->buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
imsg_set_maxsize(struct ibuf *msg, size_t max)
|
||||||
|
{
|
||||||
|
if (max > UINT32_MAX - IMSG_HEADER_SIZE) {
|
||||||
|
errno = ERANGE;
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
return ibuf_set_maxsize(msg, max + IMSG_HEADER_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
static struct ibuf *
|
static struct ibuf *
|
||||||
imsg_parse_hdr(struct ibuf *buf, void *arg, int *fd)
|
imsg_parse_hdr(struct ibuf *buf, void *arg, int *fd)
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/* $OpenBSD: imsg.h,v 1.19 2024/11/26 13:57:31 claudio Exp $ */
|
/* $OpenBSD: imsg.h,v 1.24 2025/06/05 08:55:07 tb Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
|
* Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
|
||||||
@ -23,6 +23,7 @@
|
|||||||
#define _IMSG_H_
|
#define _IMSG_H_
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#define IBUF_READ_SIZE 65535
|
#define IBUF_READ_SIZE 65535
|
||||||
#define IMSG_HEADER_SIZE sizeof(struct imsg_hdr)
|
#define IMSG_HEADER_SIZE sizeof(struct imsg_hdr)
|
||||||
@ -38,6 +39,7 @@ struct ibuf {
|
|||||||
int fd;
|
int fd;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct ibufqueue;
|
||||||
struct msgbuf;
|
struct msgbuf;
|
||||||
|
|
||||||
struct imsgbuf {
|
struct imsgbuf {
|
||||||
@ -76,6 +78,7 @@ int ibuf_add_n64(struct ibuf *, uint64_t);
|
|||||||
int ibuf_add_h16(struct ibuf *, uint64_t);
|
int ibuf_add_h16(struct ibuf *, uint64_t);
|
||||||
int ibuf_add_h32(struct ibuf *, uint64_t);
|
int ibuf_add_h32(struct ibuf *, uint64_t);
|
||||||
int ibuf_add_h64(struct ibuf *, uint64_t);
|
int ibuf_add_h64(struct ibuf *, uint64_t);
|
||||||
|
int ibuf_add_strbuf(struct ibuf *, const char *, size_t);
|
||||||
void *ibuf_reserve(struct ibuf *, size_t);
|
void *ibuf_reserve(struct ibuf *, size_t);
|
||||||
void *ibuf_seek(struct ibuf *, size_t, size_t);
|
void *ibuf_seek(struct ibuf *, size_t, size_t);
|
||||||
int ibuf_set(struct ibuf *, size_t, const void *, size_t);
|
int ibuf_set(struct ibuf *, size_t, const void *, size_t);
|
||||||
@ -86,6 +89,7 @@ int ibuf_set_n64(struct ibuf *, size_t, uint64_t);
|
|||||||
int ibuf_set_h16(struct ibuf *, size_t, uint64_t);
|
int ibuf_set_h16(struct ibuf *, size_t, uint64_t);
|
||||||
int ibuf_set_h32(struct ibuf *, size_t, uint64_t);
|
int ibuf_set_h32(struct ibuf *, size_t, uint64_t);
|
||||||
int ibuf_set_h64(struct ibuf *, size_t, uint64_t);
|
int ibuf_set_h64(struct ibuf *, size_t, uint64_t);
|
||||||
|
int ibuf_set_maxsize(struct ibuf *, size_t);
|
||||||
void *ibuf_data(const struct ibuf *);
|
void *ibuf_data(const struct ibuf *);
|
||||||
size_t ibuf_size(const struct ibuf *);
|
size_t ibuf_size(const struct ibuf *);
|
||||||
size_t ibuf_left(const struct ibuf *);
|
size_t ibuf_left(const struct ibuf *);
|
||||||
@ -104,6 +108,7 @@ int ibuf_get_h16(struct ibuf *, uint16_t *);
|
|||||||
int ibuf_get_h32(struct ibuf *, uint32_t *);
|
int ibuf_get_h32(struct ibuf *, uint32_t *);
|
||||||
int ibuf_get_h64(struct ibuf *, uint64_t *);
|
int ibuf_get_h64(struct ibuf *, uint64_t *);
|
||||||
char *ibuf_get_string(struct ibuf *, size_t);
|
char *ibuf_get_string(struct ibuf *, size_t);
|
||||||
|
int ibuf_get_strbuf(struct ibuf *, char *, size_t);
|
||||||
int ibuf_skip(struct ibuf *, size_t);
|
int ibuf_skip(struct ibuf *, size_t);
|
||||||
void ibuf_free(struct ibuf *);
|
void ibuf_free(struct ibuf *);
|
||||||
int ibuf_fd_avail(struct ibuf *);
|
int ibuf_fd_avail(struct ibuf *);
|
||||||
@ -114,6 +119,7 @@ struct msgbuf *msgbuf_new_reader(size_t,
|
|||||||
struct ibuf *(*)(struct ibuf *, void *, int *), void *);
|
struct ibuf *(*)(struct ibuf *, void *, int *), void *);
|
||||||
void msgbuf_free(struct msgbuf *);
|
void msgbuf_free(struct msgbuf *);
|
||||||
void msgbuf_clear(struct msgbuf *);
|
void msgbuf_clear(struct msgbuf *);
|
||||||
|
void msgbuf_concat(struct msgbuf *, struct ibufqueue *);
|
||||||
uint32_t msgbuf_queuelen(struct msgbuf *);
|
uint32_t msgbuf_queuelen(struct msgbuf *);
|
||||||
int ibuf_write(int, struct msgbuf *);
|
int ibuf_write(int, struct msgbuf *);
|
||||||
int msgbuf_write(int, struct msgbuf *);
|
int msgbuf_write(int, struct msgbuf *);
|
||||||
@ -121,6 +127,14 @@ int ibuf_read(int, struct msgbuf *);
|
|||||||
int msgbuf_read(int, struct msgbuf *);
|
int msgbuf_read(int, struct msgbuf *);
|
||||||
struct ibuf *msgbuf_get(struct msgbuf *);
|
struct ibuf *msgbuf_get(struct msgbuf *);
|
||||||
|
|
||||||
|
struct ibufqueue *ibufq_new(void);
|
||||||
|
void ibufq_free(struct ibufqueue *);
|
||||||
|
struct ibuf *ibufq_pop(struct ibufqueue *bufq);
|
||||||
|
void ibufq_push(struct ibufqueue *, struct ibuf *);
|
||||||
|
uint32_t ibufq_queuelen(struct ibufqueue *);
|
||||||
|
void ibufq_concat(struct ibufqueue *, struct ibufqueue *);
|
||||||
|
void ibufq_flush(struct ibufqueue *);
|
||||||
|
|
||||||
/* imsg.c */
|
/* imsg.c */
|
||||||
int imsgbuf_init(struct imsgbuf *, int);
|
int imsgbuf_init(struct imsgbuf *, int);
|
||||||
void imsgbuf_allow_fdpass(struct imsgbuf *imsgbuf);
|
void imsgbuf_allow_fdpass(struct imsgbuf *imsgbuf);
|
||||||
@ -130,9 +144,14 @@ int imsgbuf_write(struct imsgbuf *);
|
|||||||
int imsgbuf_flush(struct imsgbuf *);
|
int imsgbuf_flush(struct imsgbuf *);
|
||||||
void imsgbuf_clear(struct imsgbuf *);
|
void imsgbuf_clear(struct imsgbuf *);
|
||||||
uint32_t imsgbuf_queuelen(struct imsgbuf *);
|
uint32_t imsgbuf_queuelen(struct imsgbuf *);
|
||||||
|
int imsgbuf_get(struct imsgbuf *, struct imsg *);
|
||||||
ssize_t imsg_get(struct imsgbuf *, struct imsg *);
|
ssize_t imsg_get(struct imsgbuf *, struct imsg *);
|
||||||
|
int imsg_ibufq_pop(struct ibufqueue *, struct imsg *);
|
||||||
|
void imsg_ibufq_push(struct ibufqueue *, struct imsg *);
|
||||||
int imsg_get_ibuf(struct imsg *, struct ibuf *);
|
int imsg_get_ibuf(struct imsg *, struct ibuf *);
|
||||||
int imsg_get_data(struct imsg *, void *, size_t);
|
int imsg_get_data(struct imsg *, void *, size_t);
|
||||||
|
int imsg_get_buf(struct imsg *, void *, size_t);
|
||||||
|
int imsg_get_strbuf(struct imsg *, char *, size_t);
|
||||||
int imsg_get_fd(struct imsg *);
|
int imsg_get_fd(struct imsg *);
|
||||||
uint32_t imsg_get_id(struct imsg *);
|
uint32_t imsg_get_id(struct imsg *);
|
||||||
size_t imsg_get_len(struct imsg *);
|
size_t imsg_get_len(struct imsg *);
|
||||||
@ -149,5 +168,6 @@ struct ibuf *imsg_create(struct imsgbuf *, uint32_t, uint32_t, pid_t, size_t);
|
|||||||
int imsg_add(struct ibuf *, const void *, size_t);
|
int imsg_add(struct ibuf *, const void *, size_t);
|
||||||
void imsg_close(struct imsgbuf *, struct ibuf *);
|
void imsg_close(struct imsgbuf *, struct ibuf *);
|
||||||
void imsg_free(struct imsg *);
|
void imsg_free(struct imsg *);
|
||||||
|
int imsg_set_maxsize(struct ibuf *, size_t);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user