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