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