jcs's openbsd hax
openbsd
1/* $OpenBSD: imsg.h,v 1.24 2025/06/05 08:55:07 tb Exp $ */
2
3/*
4 * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
5 * Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org>
6 * Copyright (c) 2006, 2007, 2008 Reyk Floeter <reyk@openbsd.org>
7 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 */
21
22#ifndef _IMSG_H_
23#define _IMSG_H_
24
25#include <sys/types.h>
26#include <sys/queue.h>
27#include <stddef.h>
28#include <stdint.h>
29
30#define IBUF_READ_SIZE 65535
31#define IMSG_HEADER_SIZE sizeof(struct imsg_hdr)
32#define MAX_IMSGSIZE 16384
33
34struct ibuf {
35 TAILQ_ENTRY(ibuf) entry;
36 unsigned char *buf;
37 size_t size;
38 size_t max;
39 size_t wpos;
40 size_t rpos;
41 int fd;
42};
43
44struct ibufqueue;
45struct msgbuf;
46
47struct imsgbuf {
48 struct msgbuf *w;
49 pid_t pid;
50 uint32_t maxsize;
51 int fd;
52 int flags;
53};
54
55struct imsg_hdr {
56 uint32_t type;
57 uint32_t len;
58 uint32_t peerid;
59 uint32_t pid;
60};
61
62struct imsg {
63 struct imsg_hdr hdr;
64 void *data;
65 struct ibuf *buf;
66};
67
68struct iovec;
69
70/* imsg-buffer.c */
71struct ibuf *ibuf_open(size_t);
72struct ibuf *ibuf_dynamic(size_t, size_t);
73int ibuf_add(struct ibuf *, const void *, size_t);
74int ibuf_add_ibuf(struct ibuf *, const struct ibuf *);
75int ibuf_add_zero(struct ibuf *, size_t);
76int ibuf_add_n8(struct ibuf *, uint64_t);
77int ibuf_add_n16(struct ibuf *, uint64_t);
78int ibuf_add_n32(struct ibuf *, uint64_t);
79int ibuf_add_n64(struct ibuf *, uint64_t);
80int ibuf_add_h16(struct ibuf *, uint64_t);
81int ibuf_add_h32(struct ibuf *, uint64_t);
82int ibuf_add_h64(struct ibuf *, uint64_t);
83int ibuf_add_strbuf(struct ibuf *, const char *, size_t);
84void *ibuf_reserve(struct ibuf *, size_t);
85void *ibuf_seek(struct ibuf *, size_t, size_t);
86int ibuf_set(struct ibuf *, size_t, const void *, size_t);
87int ibuf_set_n8(struct ibuf *, size_t, uint64_t);
88int ibuf_set_n16(struct ibuf *, size_t, uint64_t);
89int ibuf_set_n32(struct ibuf *, size_t, uint64_t);
90int ibuf_set_n64(struct ibuf *, size_t, uint64_t);
91int ibuf_set_h16(struct ibuf *, size_t, uint64_t);
92int ibuf_set_h32(struct ibuf *, size_t, uint64_t);
93int ibuf_set_h64(struct ibuf *, size_t, uint64_t);
94int ibuf_set_maxsize(struct ibuf *, size_t);
95void *ibuf_data(const struct ibuf *);
96size_t ibuf_size(const struct ibuf *);
97size_t ibuf_left(const struct ibuf *);
98int ibuf_truncate(struct ibuf *, size_t);
99void ibuf_rewind(struct ibuf *);
100void ibuf_close(struct msgbuf *, struct ibuf *);
101void ibuf_from_buffer(struct ibuf *, void *, size_t);
102void ibuf_from_ibuf(struct ibuf *, const struct ibuf *);
103int ibuf_get(struct ibuf *, void *, size_t);
104int ibuf_get_ibuf(struct ibuf *, size_t, struct ibuf *);
105int ibuf_get_n8(struct ibuf *, uint8_t *);
106int ibuf_get_n16(struct ibuf *, uint16_t *);
107int ibuf_get_n32(struct ibuf *, uint32_t *);
108int ibuf_get_n64(struct ibuf *, uint64_t *);
109int ibuf_get_h16(struct ibuf *, uint16_t *);
110int ibuf_get_h32(struct ibuf *, uint32_t *);
111int ibuf_get_h64(struct ibuf *, uint64_t *);
112char *ibuf_get_string(struct ibuf *, size_t);
113int ibuf_get_strbuf(struct ibuf *, char *, size_t);
114int ibuf_skip(struct ibuf *, size_t);
115void ibuf_free(struct ibuf *);
116int ibuf_fd_avail(struct ibuf *);
117int ibuf_fd_get(struct ibuf *);
118void ibuf_fd_set(struct ibuf *, int);
119struct msgbuf *msgbuf_new(void);
120struct msgbuf *msgbuf_new_reader(size_t,
121 struct ibuf *(*)(struct ibuf *, void *, int *), void *);
122void msgbuf_free(struct msgbuf *);
123void msgbuf_clear(struct msgbuf *);
124void msgbuf_concat(struct msgbuf *, struct ibufqueue *);
125uint32_t msgbuf_queuelen(struct msgbuf *);
126int ibuf_write(int, struct msgbuf *);
127int msgbuf_write(int, struct msgbuf *);
128int ibuf_read(int, struct msgbuf *);
129int msgbuf_read(int, struct msgbuf *);
130struct ibuf *msgbuf_get(struct msgbuf *);
131
132struct ibufqueue *ibufq_new(void);
133void ibufq_free(struct ibufqueue *);
134struct ibuf *ibufq_pop(struct ibufqueue *bufq);
135void ibufq_push(struct ibufqueue *, struct ibuf *);
136uint32_t ibufq_queuelen(struct ibufqueue *);
137void ibufq_concat(struct ibufqueue *, struct ibufqueue *);
138void ibufq_flush(struct ibufqueue *);
139
140/* imsg.c */
141int imsgbuf_init(struct imsgbuf *, int);
142void imsgbuf_allow_fdpass(struct imsgbuf *imsgbuf);
143int imsgbuf_set_maxsize(struct imsgbuf *, uint32_t);
144int imsgbuf_read(struct imsgbuf *);
145int imsgbuf_write(struct imsgbuf *);
146int imsgbuf_flush(struct imsgbuf *);
147void imsgbuf_clear(struct imsgbuf *);
148uint32_t imsgbuf_queuelen(struct imsgbuf *);
149int imsgbuf_get(struct imsgbuf *, struct imsg *);
150ssize_t imsg_get(struct imsgbuf *, struct imsg *);
151int imsg_ibufq_pop(struct ibufqueue *, struct imsg *);
152void imsg_ibufq_push(struct ibufqueue *, struct imsg *);
153int imsg_get_ibuf(struct imsg *, struct ibuf *);
154int imsg_get_data(struct imsg *, void *, size_t);
155int imsg_get_buf(struct imsg *, void *, size_t);
156int imsg_get_strbuf(struct imsg *, char *, size_t);
157int imsg_get_fd(struct imsg *);
158uint32_t imsg_get_id(struct imsg *);
159size_t imsg_get_len(struct imsg *);
160pid_t imsg_get_pid(struct imsg *);
161uint32_t imsg_get_type(struct imsg *);
162int imsg_forward(struct imsgbuf *, struct imsg *);
163int imsg_compose(struct imsgbuf *, uint32_t, uint32_t, pid_t, int,
164 const void *, size_t);
165int imsg_composev(struct imsgbuf *, uint32_t, uint32_t, pid_t, int,
166 const struct iovec *, int);
167int imsg_compose_ibuf(struct imsgbuf *, uint32_t, uint32_t, pid_t,
168 struct ibuf *);
169struct ibuf *imsg_create(struct imsgbuf *, uint32_t, uint32_t, pid_t, size_t);
170int imsg_add(struct ibuf *, const void *, size_t);
171void imsg_close(struct imsgbuf *, struct ibuf *);
172void imsg_free(struct imsg *);
173int imsg_set_maxsize(struct ibuf *, size_t);
174
175#endif