tmux/compat/imsg.c

379 lines
7.3 KiB
C
Raw Normal View History

2024-11-27 10:30:52 +00:00
/* $OpenBSD: imsg.c,v 1.37 2024/11/26 13:57:31 claudio Exp $ */
/*
2024-01-17 09:41:53 +00:00
* Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <errno.h>
2024-11-27 10:30:52 +00:00
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2017-01-25 13:49:01 +00:00
#include "compat.h"
#include "imsg.h"
2024-11-22 08:03:55 +00:00
#define IMSG_ALLOW_FDPASS 0x01
2024-11-27 10:30:52 +00:00
#define IMSG_FD_MARK 0x80000000U
2024-01-17 09:41:53 +00:00
2024-11-27 10:30:52 +00:00
static struct ibuf *imsg_parse_hdr(struct ibuf *, void *, int *);
2024-11-22 08:03:55 +00:00
int
imsgbuf_init(struct imsgbuf *imsgbuf, int fd)
{
imsgbuf->w = msgbuf_new_reader(IMSG_HEADER_SIZE, imsg_parse_hdr,
imsgbuf);
if (imsgbuf->w == NULL)
return (-1);
imsgbuf->pid = getpid();
imsgbuf->maxsize = MAX_IMSGSIZE;
imsgbuf->fd = fd;
imsgbuf->flags = 0;
return (0);
}
void
2024-11-22 08:03:55 +00:00
imsgbuf_allow_fdpass(struct imsgbuf *imsgbuf)
{
2024-11-22 08:03:55 +00:00
imsgbuf->flags |= IMSG_ALLOW_FDPASS;
}
2024-11-27 10:30:52 +00:00
int
2024-11-22 08:03:55 +00:00
imsgbuf_set_maxsize(struct imsgbuf *imsgbuf, uint32_t maxsize)
{
2024-11-27 10:30:52 +00:00
if (maxsize < IMSG_HEADER_SIZE || maxsize & IMSG_FD_MARK) {
errno = EINVAL;
return (-1);
}
2024-11-22 08:03:55 +00:00
imsgbuf->maxsize = maxsize;
2024-11-27 10:30:52 +00:00
return (0);
2024-11-22 08:03:55 +00:00
}
2024-11-22 08:03:55 +00:00
int
imsgbuf_read(struct imsgbuf *imsgbuf)
{
if (imsgbuf->flags & IMSG_ALLOW_FDPASS)
return msgbuf_read(imsgbuf->fd, imsgbuf->w);
else
return ibuf_read(imsgbuf->fd, imsgbuf->w);
}
2024-11-22 08:03:55 +00:00
int
imsgbuf_write(struct imsgbuf *imsgbuf)
{
if (imsgbuf->flags & IMSG_ALLOW_FDPASS)
return msgbuf_write(imsgbuf->fd, imsgbuf->w);
else
return ibuf_write(imsgbuf->fd, imsgbuf->w);
}
2024-11-22 08:03:55 +00:00
int
imsgbuf_flush(struct imsgbuf *imsgbuf)
{
while (imsgbuf_queuelen(imsgbuf) > 0) {
if (imsgbuf_write(imsgbuf) == -1)
return (-1);
}
2024-11-22 08:03:55 +00:00
return (0);
}
void
imsgbuf_clear(struct imsgbuf *imsgbuf)
{
msgbuf_free(imsgbuf->w);
imsgbuf->w = NULL;
}
2024-11-22 08:03:55 +00:00
uint32_t
imsgbuf_queuelen(struct imsgbuf *imsgbuf)
{
return msgbuf_queuelen(imsgbuf->w);
}
ssize_t
2024-01-17 09:41:53 +00:00
imsg_get(struct imsgbuf *imsgbuf, struct imsg *imsg)
{
2024-01-17 09:41:53 +00:00
struct imsg m;
2024-11-22 08:03:55 +00:00
struct ibuf *buf;
2024-11-22 08:03:55 +00:00
if ((buf = msgbuf_get(imsgbuf->w)) == NULL)
return (0);
2024-11-22 08:03:55 +00:00
if (ibuf_get(buf, &m.hdr, sizeof(m.hdr)) == -1)
return (-1);
2024-11-22 08:03:55 +00:00
if (ibuf_size(buf))
m.data = ibuf_data(buf);
else
m.data = NULL;
m.buf = buf;
2024-11-27 10:30:52 +00:00
m.hdr.len &= ~IMSG_FD_MARK;
2024-01-17 09:41:53 +00:00
*imsg = m;
2024-11-22 08:03:55 +00:00
return (ibuf_size(buf) + IMSG_HEADER_SIZE);
}
int
2024-01-17 09:41:53 +00:00
imsg_get_ibuf(struct imsg *imsg, struct ibuf *ibuf)
{
2024-11-22 08:03:55 +00:00
if (ibuf_size(imsg->buf) == 0) {
2024-01-17 09:41:53 +00:00
errno = EBADMSG;
return (-1);
}
return ibuf_get_ibuf(imsg->buf, ibuf_size(imsg->buf), ibuf);
}
int
imsg_get_data(struct imsg *imsg, void *data, size_t len)
{
if (len == 0) {
errno = EINVAL;
return (-1);
}
2024-11-22 08:03:55 +00:00
if (ibuf_size(imsg->buf) != len) {
2024-01-17 09:41:53 +00:00
errno = EBADMSG;
return (-1);
}
return ibuf_get(imsg->buf, data, len);
}
int
imsg_get_fd(struct imsg *imsg)
{
2024-11-22 08:03:55 +00:00
return ibuf_fd_get(imsg->buf);
2024-01-17 09:41:53 +00:00
}
uint32_t
imsg_get_id(struct imsg *imsg)
{
return (imsg->hdr.peerid);
}
size_t
imsg_get_len(struct imsg *imsg)
{
return ibuf_size(imsg->buf);
}
pid_t
imsg_get_pid(struct imsg *imsg)
{
return (imsg->hdr.pid);
}
uint32_t
imsg_get_type(struct imsg *imsg)
{
return (imsg->hdr.type);
}
int
imsg_compose(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
int fd, const void *data, size_t datalen)
{
struct ibuf *wbuf;
2024-01-17 09:41:53 +00:00
if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
return (-1);
if (imsg_add(wbuf, data, datalen) == -1)
return (-1);
2024-01-17 09:41:53 +00:00
ibuf_fd_set(wbuf, fd);
imsg_close(imsgbuf, wbuf);
return (1);
}
int
2024-01-17 09:41:53 +00:00
imsg_composev(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
int fd, const struct iovec *iov, int iovcnt)
{
struct ibuf *wbuf;
2024-01-17 09:41:53 +00:00
int i;
size_t datalen = 0;
for (i = 0; i < iovcnt; i++)
datalen += iov[i].iov_len;
2024-01-17 09:41:53 +00:00
if ((wbuf = imsg_create(imsgbuf, type, id, pid, datalen)) == NULL)
return (-1);
for (i = 0; i < iovcnt; i++)
if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
return (-1);
2024-01-17 09:41:53 +00:00
ibuf_fd_set(wbuf, fd);
imsg_close(imsgbuf, wbuf);
2024-01-17 09:41:53 +00:00
return (1);
}
/*
* Enqueue imsg with payload from ibuf buf. fd passing is not possible
* with this function.
*/
int
imsg_compose_ibuf(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id,
pid_t pid, struct ibuf *buf)
{
struct ibuf *hdrbuf = NULL;
struct imsg_hdr hdr;
int save_errno;
2024-11-22 08:03:55 +00:00
if (ibuf_size(buf) + IMSG_HEADER_SIZE > imsgbuf->maxsize) {
2024-01-17 09:41:53 +00:00
errno = ERANGE;
goto fail;
}
hdr.type = type;
hdr.len = ibuf_size(buf) + IMSG_HEADER_SIZE;
hdr.peerid = id;
if ((hdr.pid = pid) == 0)
hdr.pid = imsgbuf->pid;
2024-01-17 09:41:53 +00:00
if ((hdrbuf = ibuf_open(IMSG_HEADER_SIZE)) == NULL)
goto fail;
if (imsg_add(hdrbuf, &hdr, sizeof(hdr)) == -1)
goto fail;
2024-11-22 08:03:55 +00:00
ibuf_close(imsgbuf->w, hdrbuf);
ibuf_close(imsgbuf->w, buf);
2024-01-17 09:41:53 +00:00
return (1);
fail:
save_errno = errno;
ibuf_free(buf);
ibuf_free(hdrbuf);
errno = save_errno;
return (-1);
}
/*
* Forward imsg to another channel. Any attached fd is closed.
*/
int
imsg_forward(struct imsgbuf *imsgbuf, struct imsg *msg)
{
struct ibuf *wbuf;
2024-11-27 10:30:52 +00:00
size_t len;
2024-01-17 09:41:53 +00:00
2024-11-22 08:03:55 +00:00
ibuf_rewind(msg->buf);
ibuf_skip(msg->buf, sizeof(msg->hdr));
len = ibuf_size(msg->buf);
2024-01-17 09:41:53 +00:00
if ((wbuf = imsg_create(imsgbuf, msg->hdr.type, msg->hdr.peerid,
msg->hdr.pid, len)) == NULL)
return (-1);
2024-11-27 10:30:52 +00:00
if (len != 0) {
2024-11-22 08:03:55 +00:00
if (ibuf_add_ibuf(wbuf, msg->buf) == -1) {
2024-01-17 09:41:53 +00:00
ibuf_free(wbuf);
return (-1);
}
}
imsg_close(imsgbuf, wbuf);
return (1);
}
struct ibuf *
2024-01-17 09:41:53 +00:00
imsg_create(struct imsgbuf *imsgbuf, uint32_t type, uint32_t id, pid_t pid,
size_t datalen)
{
struct ibuf *wbuf;
struct imsg_hdr hdr;
datalen += IMSG_HEADER_SIZE;
2024-11-22 08:03:55 +00:00
if (datalen > imsgbuf->maxsize) {
errno = ERANGE;
return (NULL);
}
hdr.type = type;
2024-01-17 09:41:53 +00:00
hdr.peerid = id;
if ((hdr.pid = pid) == 0)
2024-01-17 09:41:53 +00:00
hdr.pid = imsgbuf->pid;
2024-11-22 08:03:55 +00:00
if ((wbuf = ibuf_dynamic(datalen, imsgbuf->maxsize)) == NULL) {
return (NULL);
}
if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
return (NULL);
return (wbuf);
}
int
2024-01-17 09:41:53 +00:00
imsg_add(struct ibuf *msg, const void *data, size_t datalen)
{
if (datalen)
if (ibuf_add(msg, data, datalen) == -1) {
ibuf_free(msg);
return (-1);
}
return (datalen);
}
void
2024-01-17 09:41:53 +00:00
imsg_close(struct imsgbuf *imsgbuf, struct ibuf *msg)
{
struct imsg_hdr *hdr;
2024-11-27 10:30:52 +00:00
uint32_t len;
2024-11-27 10:30:52 +00:00
len = ibuf_size(msg);
if (ibuf_fd_avail(msg))
len |= IMSG_FD_MARK;
(void)ibuf_set_h32(msg, offsetof(struct imsg_hdr, len), len);
2024-11-22 08:03:55 +00:00
ibuf_close(imsgbuf->w, msg);
}
void
imsg_free(struct imsg *imsg)
{
2024-01-17 09:41:53 +00:00
ibuf_free(imsg->buf);
}
2024-11-27 10:30:52 +00:00
static struct ibuf *
imsg_parse_hdr(struct ibuf *buf, void *arg, int *fd)
{
2024-11-22 08:03:55 +00:00
struct imsgbuf *imsgbuf = arg;
struct imsg_hdr hdr;
2024-11-27 10:30:52 +00:00
struct ibuf *b;
uint32_t len;
2024-11-22 08:03:55 +00:00
if (ibuf_get(buf, &hdr, sizeof(hdr)) == -1)
2024-11-27 10:30:52 +00:00
return (NULL);
len = hdr.len & ~IMSG_FD_MARK;
if (len < IMSG_HEADER_SIZE || len > imsgbuf->maxsize) {
2024-11-22 08:03:55 +00:00
errno = ERANGE;
2024-11-27 10:30:52 +00:00
return (NULL);
2024-11-22 08:03:55 +00:00
}
2024-11-27 10:30:52 +00:00
if ((b = ibuf_open(len)) == NULL)
return (NULL);
if (hdr.len & IMSG_FD_MARK) {
ibuf_fd_set(b, *fd);
*fd = -1;
}
return b;
}