Blame


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