Blame


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