xref: /openbsd-src/lib/libutil/imsg.h (revision ff59764deb7c720c75d582b75dac4b924ec7c8a6)
1 /*	$OpenBSD: imsg.h,v 1.19 2024/11/26 13:57:31 claudio 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 
27 #define IBUF_READ_SIZE		65535
28 #define IMSG_HEADER_SIZE	sizeof(struct imsg_hdr)
29 #define MAX_IMSGSIZE		16384
30 
31 struct ibuf {
32 	TAILQ_ENTRY(ibuf)	 entry;
33 	unsigned char		*buf;
34 	size_t			 size;
35 	size_t			 max;
36 	size_t			 wpos;
37 	size_t			 rpos;
38 	int			 fd;
39 };
40 
41 struct msgbuf;
42 
43 struct imsgbuf {
44 	struct msgbuf		*w;
45 	pid_t			 pid;
46 	uint32_t		 maxsize;
47 	int			 fd;
48 	int			 flags;
49 };
50 
51 struct imsg_hdr {
52 	uint32_t	 type;
53 	uint32_t	 len;
54 	uint32_t	 peerid;
55 	uint32_t	 pid;
56 };
57 
58 struct imsg {
59 	struct imsg_hdr	 hdr;
60 	void		*data;
61 	struct ibuf	*buf;
62 };
63 
64 struct iovec;
65 
66 /* imsg-buffer.c */
67 struct ibuf	*ibuf_open(size_t);
68 struct ibuf	*ibuf_dynamic(size_t, size_t);
69 int		 ibuf_add(struct ibuf *, const void *, size_t);
70 int		 ibuf_add_ibuf(struct ibuf *, const struct ibuf *);
71 int		 ibuf_add_zero(struct ibuf *, size_t);
72 int		 ibuf_add_n8(struct ibuf *, uint64_t);
73 int		 ibuf_add_n16(struct ibuf *, uint64_t);
74 int		 ibuf_add_n32(struct ibuf *, uint64_t);
75 int		 ibuf_add_n64(struct ibuf *, uint64_t);
76 int		 ibuf_add_h16(struct ibuf *, uint64_t);
77 int		 ibuf_add_h32(struct ibuf *, uint64_t);
78 int		 ibuf_add_h64(struct ibuf *, uint64_t);
79 void		*ibuf_reserve(struct ibuf *, size_t);
80 void		*ibuf_seek(struct ibuf *, size_t, size_t);
81 int		 ibuf_set(struct ibuf *, size_t, const void *, size_t);
82 int		 ibuf_set_n8(struct ibuf *, size_t, uint64_t);
83 int		 ibuf_set_n16(struct ibuf *, size_t, uint64_t);
84 int		 ibuf_set_n32(struct ibuf *, size_t, uint64_t);
85 int		 ibuf_set_n64(struct ibuf *, size_t, uint64_t);
86 int		 ibuf_set_h16(struct ibuf *, size_t, uint64_t);
87 int		 ibuf_set_h32(struct ibuf *, size_t, uint64_t);
88 int		 ibuf_set_h64(struct ibuf *, size_t, uint64_t);
89 void		*ibuf_data(const struct ibuf *);
90 size_t		 ibuf_size(const struct ibuf *);
91 size_t		 ibuf_left(const struct ibuf *);
92 int		 ibuf_truncate(struct ibuf *, size_t);
93 void		 ibuf_rewind(struct ibuf *);
94 void		 ibuf_close(struct msgbuf *, struct ibuf *);
95 void		 ibuf_from_buffer(struct ibuf *, void *, size_t);
96 void		 ibuf_from_ibuf(struct ibuf *, const struct ibuf *);
97 int		 ibuf_get(struct ibuf *, void *, size_t);
98 int		 ibuf_get_ibuf(struct ibuf *, size_t, struct ibuf *);
99 int		 ibuf_get_n8(struct ibuf *, uint8_t *);
100 int		 ibuf_get_n16(struct ibuf *, uint16_t *);
101 int		 ibuf_get_n32(struct ibuf *, uint32_t *);
102 int		 ibuf_get_n64(struct ibuf *, uint64_t *);
103 int		 ibuf_get_h16(struct ibuf *, uint16_t *);
104 int		 ibuf_get_h32(struct ibuf *, uint32_t *);
105 int		 ibuf_get_h64(struct ibuf *, uint64_t *);
106 char		*ibuf_get_string(struct ibuf *, size_t);
107 int		 ibuf_skip(struct ibuf *, size_t);
108 void		 ibuf_free(struct ibuf *);
109 int		 ibuf_fd_avail(struct ibuf *);
110 int		 ibuf_fd_get(struct ibuf *);
111 void		 ibuf_fd_set(struct ibuf *, int);
112 struct msgbuf	*msgbuf_new(void);
113 struct msgbuf	*msgbuf_new_reader(size_t,
114 		    struct ibuf *(*)(struct ibuf *, void *, int *), void *);
115 void		 msgbuf_free(struct msgbuf *);
116 void		 msgbuf_clear(struct msgbuf *);
117 uint32_t	 msgbuf_queuelen(struct msgbuf *);
118 int		 ibuf_write(int, struct msgbuf *);
119 int		 msgbuf_write(int, struct msgbuf *);
120 int		 ibuf_read(int, struct msgbuf *);
121 int		 msgbuf_read(int, struct msgbuf *);
122 struct ibuf	*msgbuf_get(struct msgbuf *);
123 
124 /* imsg.c */
125 int	 imsgbuf_init(struct imsgbuf *, int);
126 void	 imsgbuf_allow_fdpass(struct imsgbuf *imsgbuf);
127 int	 imsgbuf_set_maxsize(struct imsgbuf *, uint32_t);
128 int	 imsgbuf_read(struct imsgbuf *);
129 int	 imsgbuf_write(struct imsgbuf *);
130 int	 imsgbuf_flush(struct imsgbuf *);
131 void	 imsgbuf_clear(struct imsgbuf *);
132 uint32_t imsgbuf_queuelen(struct imsgbuf *);
133 ssize_t	 imsg_get(struct imsgbuf *, struct imsg *);
134 int	 imsg_get_ibuf(struct imsg *, struct ibuf *);
135 int	 imsg_get_data(struct imsg *, void *, size_t);
136 int	 imsg_get_fd(struct imsg *);
137 uint32_t imsg_get_id(struct imsg *);
138 size_t	 imsg_get_len(struct imsg *);
139 pid_t	 imsg_get_pid(struct imsg *);
140 uint32_t imsg_get_type(struct imsg *);
141 int	 imsg_forward(struct imsgbuf *, struct imsg *);
142 int	 imsg_compose(struct imsgbuf *, uint32_t, uint32_t, pid_t, int,
143 	    const void *, size_t);
144 int	 imsg_composev(struct imsgbuf *, uint32_t, uint32_t,  pid_t, int,
145 	    const struct iovec *, int);
146 int	 imsg_compose_ibuf(struct imsgbuf *, uint32_t, uint32_t, pid_t,
147 	    struct ibuf *);
148 struct ibuf *imsg_create(struct imsgbuf *, uint32_t, uint32_t, pid_t, size_t);
149 int	 imsg_add(struct ibuf *, const void *, size_t);
150 void	 imsg_close(struct imsgbuf *, struct ibuf *);
151 void	 imsg_free(struct imsg *);
152 
153 #endif
154