1 3bb9eb8b 2024-01-18 thomas /* $OpenBSD: imsg.h,v 1.8 2023/12/12 15:47:41 claudio Exp $ */
4 3bb9eb8b 2024-01-18 thomas * Copyright (c) 2023 Claudio Jeker <claudio@openbsd.org>
5 dd038bc6 2021-09-21 thomas.ad * Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org>
6 dd038bc6 2021-09-21 thomas.ad * Copyright (c) 2006, 2007, 2008 Reyk Floeter <reyk@openbsd.org>
7 dd038bc6 2021-09-21 thomas.ad * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
9 dd038bc6 2021-09-21 thomas.ad * Permission to use, copy, modify, and distribute this software for any
10 dd038bc6 2021-09-21 thomas.ad * purpose with or without fee is hereby granted, provided that the above
11 dd038bc6 2021-09-21 thomas.ad * copyright notice and this permission notice appear in all copies.
13 dd038bc6 2021-09-21 thomas.ad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 dd038bc6 2021-09-21 thomas.ad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 dd038bc6 2021-09-21 thomas.ad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 dd038bc6 2021-09-21 thomas.ad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 dd038bc6 2021-09-21 thomas.ad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 dd038bc6 2021-09-21 thomas.ad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 dd038bc6 2021-09-21 thomas.ad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 dd038bc6 2021-09-21 thomas.ad #ifndef _IMSG_H_
23 dd038bc6 2021-09-21 thomas.ad #define _IMSG_H_
25 3bb9eb8b 2024-01-18 thomas #include <sys/types.h>
27 dd038bc6 2021-09-21 thomas.ad #define IBUF_READ_SIZE 65535
28 dd038bc6 2021-09-21 thomas.ad #define IMSG_HEADER_SIZE sizeof(struct imsg_hdr)
29 dd038bc6 2021-09-21 thomas.ad #define MAX_IMSGSIZE 16384
31 dd038bc6 2021-09-21 thomas.ad struct ibuf {
32 dd038bc6 2021-09-21 thomas.ad TAILQ_ENTRY(ibuf) entry;
33 3ef3f36a 2023-07-05 op unsigned char *buf;
34 dd038bc6 2021-09-21 thomas.ad size_t size;
35 dd038bc6 2021-09-21 thomas.ad size_t max;
36 dd038bc6 2021-09-21 thomas.ad size_t wpos;
37 dd038bc6 2021-09-21 thomas.ad size_t rpos;
38 dd038bc6 2021-09-21 thomas.ad int fd;
41 dd038bc6 2021-09-21 thomas.ad struct msgbuf {
42 dd038bc6 2021-09-21 thomas.ad TAILQ_HEAD(, ibuf) bufs;
43 dd038bc6 2021-09-21 thomas.ad uint32_t queued;
44 dd038bc6 2021-09-21 thomas.ad int fd;
47 dd038bc6 2021-09-21 thomas.ad struct ibuf_read {
48 3ef3f36a 2023-07-05 op unsigned char buf[IBUF_READ_SIZE];
49 3ef3f36a 2023-07-05 op unsigned char *rptr;
50 dd038bc6 2021-09-21 thomas.ad size_t wpos;
53 3bb9eb8b 2024-01-18 thomas struct imsg_fd;
54 dd038bc6 2021-09-21 thomas.ad struct imsgbuf {
55 dd038bc6 2021-09-21 thomas.ad TAILQ_HEAD(, imsg_fd) fds;
56 dd038bc6 2021-09-21 thomas.ad struct ibuf_read r;
57 dd038bc6 2021-09-21 thomas.ad struct msgbuf w;
58 dd038bc6 2021-09-21 thomas.ad int fd;
59 dd038bc6 2021-09-21 thomas.ad pid_t pid;
62 dd038bc6 2021-09-21 thomas.ad #define IMSGF_HASFD 1
64 dd038bc6 2021-09-21 thomas.ad struct imsg_hdr {
65 dd038bc6 2021-09-21 thomas.ad uint32_t type;
66 dd038bc6 2021-09-21 thomas.ad uint16_t len;
67 dd038bc6 2021-09-21 thomas.ad uint16_t flags;
68 dd038bc6 2021-09-21 thomas.ad uint32_t peerid;
69 dd038bc6 2021-09-21 thomas.ad uint32_t pid;
72 dd038bc6 2021-09-21 thomas.ad struct imsg {
73 dd038bc6 2021-09-21 thomas.ad struct imsg_hdr hdr;
74 dd038bc6 2021-09-21 thomas.ad int fd;
75 dd038bc6 2021-09-21 thomas.ad void *data;
76 3bb9eb8b 2024-01-18 thomas struct ibuf *buf;
81 3ef3f36a 2023-07-05 op /* imsg-buffer.c */
82 dd038bc6 2021-09-21 thomas.ad struct ibuf *ibuf_open(size_t);
83 dd038bc6 2021-09-21 thomas.ad struct ibuf *ibuf_dynamic(size_t, size_t);
84 dd038bc6 2021-09-21 thomas.ad int ibuf_add(struct ibuf *, const void *, size_t);
85 3ef3f36a 2023-07-05 op int ibuf_add_buf(struct ibuf *, const struct ibuf *);
86 3bb9eb8b 2024-01-18 thomas int ibuf_add_ibuf(struct ibuf *, const struct ibuf *);
87 3ef3f36a 2023-07-05 op int ibuf_add_zero(struct ibuf *, size_t);
88 3ef3f36a 2023-07-05 op int ibuf_add_n8(struct ibuf *, uint64_t);
89 3ef3f36a 2023-07-05 op int ibuf_add_n16(struct ibuf *, uint64_t);
90 3ef3f36a 2023-07-05 op int ibuf_add_n32(struct ibuf *, uint64_t);
91 3ef3f36a 2023-07-05 op int ibuf_add_n64(struct ibuf *, uint64_t);
92 3bb9eb8b 2024-01-18 thomas int ibuf_add_h16(struct ibuf *, uint64_t);
93 3bb9eb8b 2024-01-18 thomas int ibuf_add_h32(struct ibuf *, uint64_t);
94 3bb9eb8b 2024-01-18 thomas int ibuf_add_h64(struct ibuf *, uint64_t);
95 dd038bc6 2021-09-21 thomas.ad void *ibuf_reserve(struct ibuf *, size_t);
96 dd038bc6 2021-09-21 thomas.ad void *ibuf_seek(struct ibuf *, size_t, size_t);
97 3ef3f36a 2023-07-05 op int ibuf_set(struct ibuf *, size_t, const void *, size_t);
98 3ef3f36a 2023-07-05 op int ibuf_set_n8(struct ibuf *, size_t, uint64_t);
99 3ef3f36a 2023-07-05 op int ibuf_set_n16(struct ibuf *, size_t, uint64_t);
100 3ef3f36a 2023-07-05 op int ibuf_set_n32(struct ibuf *, size_t, uint64_t);
101 3ef3f36a 2023-07-05 op int ibuf_set_n64(struct ibuf *, size_t, uint64_t);
102 3bb9eb8b 2024-01-18 thomas int ibuf_set_h16(struct ibuf *, size_t, uint64_t);
103 3bb9eb8b 2024-01-18 thomas int ibuf_set_h32(struct ibuf *, size_t, uint64_t);
104 3bb9eb8b 2024-01-18 thomas int ibuf_set_h64(struct ibuf *, size_t, uint64_t);
105 3bb9eb8b 2024-01-18 thomas void *ibuf_data(const struct ibuf *);
106 3bb9eb8b 2024-01-18 thomas size_t ibuf_size(const struct ibuf *);
107 3bb9eb8b 2024-01-18 thomas size_t ibuf_left(const struct ibuf *);
108 3bb9eb8b 2024-01-18 thomas int ibuf_truncate(struct ibuf *, size_t);
109 3bb9eb8b 2024-01-18 thomas void ibuf_rewind(struct ibuf *);
110 dd038bc6 2021-09-21 thomas.ad void ibuf_close(struct msgbuf *, struct ibuf *);
111 3bb9eb8b 2024-01-18 thomas void ibuf_from_buffer(struct ibuf *, void *, size_t);
112 3bb9eb8b 2024-01-18 thomas void ibuf_from_ibuf(struct ibuf *, const struct ibuf *);
113 3bb9eb8b 2024-01-18 thomas int ibuf_get(struct ibuf *, void *, size_t);
114 3bb9eb8b 2024-01-18 thomas int ibuf_get_ibuf(struct ibuf *, size_t, struct ibuf *);
115 3bb9eb8b 2024-01-18 thomas int ibuf_get_n8(struct ibuf *, uint8_t *);
116 3bb9eb8b 2024-01-18 thomas int ibuf_get_n16(struct ibuf *, uint16_t *);
117 3bb9eb8b 2024-01-18 thomas int ibuf_get_n32(struct ibuf *, uint32_t *);
118 3bb9eb8b 2024-01-18 thomas int ibuf_get_n64(struct ibuf *, uint64_t *);
119 3bb9eb8b 2024-01-18 thomas int ibuf_get_h16(struct ibuf *, uint16_t *);
120 3bb9eb8b 2024-01-18 thomas int ibuf_get_h32(struct ibuf *, uint32_t *);
121 3bb9eb8b 2024-01-18 thomas int ibuf_get_h64(struct ibuf *, uint64_t *);
122 3bb9eb8b 2024-01-18 thomas int ibuf_skip(struct ibuf *, size_t);
123 dd038bc6 2021-09-21 thomas.ad void ibuf_free(struct ibuf *);
124 3ef3f36a 2023-07-05 op int ibuf_fd_avail(struct ibuf *);
125 3ef3f36a 2023-07-05 op int ibuf_fd_get(struct ibuf *);
126 3ef3f36a 2023-07-05 op void ibuf_fd_set(struct ibuf *, int);
127 3ef3f36a 2023-07-05 op int ibuf_write(struct msgbuf *);
128 dd038bc6 2021-09-21 thomas.ad void msgbuf_init(struct msgbuf *);
129 dd038bc6 2021-09-21 thomas.ad void msgbuf_clear(struct msgbuf *);
130 3bb9eb8b 2024-01-18 thomas uint32_t msgbuf_queuelen(struct msgbuf *);
131 dd038bc6 2021-09-21 thomas.ad int msgbuf_write(struct msgbuf *);
133 dd038bc6 2021-09-21 thomas.ad /* imsg.c */
134 dd038bc6 2021-09-21 thomas.ad void imsg_init(struct imsgbuf *, int);
135 dd038bc6 2021-09-21 thomas.ad ssize_t imsg_read(struct imsgbuf *);
136 dd038bc6 2021-09-21 thomas.ad ssize_t imsg_get(struct imsgbuf *, struct imsg *);
137 3bb9eb8b 2024-01-18 thomas int imsg_get_ibuf(struct imsg *, struct ibuf *);
138 3bb9eb8b 2024-01-18 thomas int imsg_get_data(struct imsg *, void *, size_t);
139 3bb9eb8b 2024-01-18 thomas int imsg_get_fd(struct imsg *);
140 3bb9eb8b 2024-01-18 thomas uint32_t imsg_get_id(struct imsg *);
141 3bb9eb8b 2024-01-18 thomas size_t imsg_get_len(struct imsg *);
142 3bb9eb8b 2024-01-18 thomas pid_t imsg_get_pid(struct imsg *);
143 3bb9eb8b 2024-01-18 thomas uint32_t imsg_get_type(struct imsg *);
144 3bb9eb8b 2024-01-18 thomas int imsg_forward(struct imsgbuf *, struct imsg *);
145 dd038bc6 2021-09-21 thomas.ad int imsg_compose(struct imsgbuf *, uint32_t, uint32_t, pid_t, int,
146 3bb9eb8b 2024-01-18 thomas const void *, size_t);
147 dd038bc6 2021-09-21 thomas.ad int imsg_composev(struct imsgbuf *, uint32_t, uint32_t, pid_t, int,
148 dd038bc6 2021-09-21 thomas.ad const struct iovec *, int);
149 3ef3f36a 2023-07-05 op int imsg_compose_ibuf(struct imsgbuf *, uint32_t, uint32_t, pid_t,
150 3ef3f36a 2023-07-05 op struct ibuf *);
151 3bb9eb8b 2024-01-18 thomas struct ibuf *imsg_create(struct imsgbuf *, uint32_t, uint32_t, pid_t, size_t);
152 3bb9eb8b 2024-01-18 thomas int imsg_add(struct ibuf *, const void *, size_t);
153 dd038bc6 2021-09-21 thomas.ad void imsg_close(struct imsgbuf *, struct ibuf *);
154 dd038bc6 2021-09-21 thomas.ad void imsg_free(struct imsg *);
155 dd038bc6 2021-09-21 thomas.ad int imsg_flush(struct imsgbuf *);
156 dd038bc6 2021-09-21 thomas.ad void imsg_clear(struct imsgbuf *);
158 dd038bc6 2021-09-21 thomas.ad #endif