Blame


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