Merge branch 'obsd-master'
[tmux.git] / compat / imsg.h
blobdd47b1889da577ce63769035f0e4bd0b1d8a7c32
1 /* $OpenBSD: imsg.h,v 1.8 2023/12/12 15:47:41 claudio Exp $ */
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>
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.
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.
22 #ifndef _IMSG_H_
23 #define _IMSG_H_
25 #include <sys/types.h>
27 #define IBUF_READ_SIZE 65535
28 #define IMSG_HEADER_SIZE sizeof(struct imsg_hdr)
29 #define MAX_IMSGSIZE 16384
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;
41 struct msgbuf {
42 TAILQ_HEAD(, ibuf) bufs;
43 uint32_t queued;
44 int fd;
47 struct ibuf_read {
48 unsigned char buf[IBUF_READ_SIZE];
49 unsigned char *rptr;
50 size_t wpos;
53 struct imsg_fd;
54 struct imsgbuf {
55 TAILQ_HEAD(, imsg_fd) fds;
56 struct ibuf_read r;
57 struct msgbuf w;
58 int fd;
59 pid_t pid;
62 #define IMSGF_HASFD 1
64 struct imsg_hdr {
65 uint32_t type;
66 uint16_t len;
67 uint16_t flags;
68 uint32_t peerid;
69 uint32_t pid;
72 struct imsg {
73 struct imsg_hdr hdr;
74 int fd;
75 void *data;
76 struct ibuf *buf;
79 struct iovec;
81 /* imsg-buffer.c */
82 struct ibuf *ibuf_open(size_t);
83 struct ibuf *ibuf_dynamic(size_t, size_t);
84 int ibuf_add(struct ibuf *, const void *, size_t);
85 int ibuf_add_buf(struct ibuf *, const struct ibuf *);
86 int ibuf_add_ibuf(struct ibuf *, const struct ibuf *);
87 int ibuf_add_zero(struct ibuf *, size_t);
88 int ibuf_add_n8(struct ibuf *, uint64_t);
89 int ibuf_add_n16(struct ibuf *, uint64_t);
90 int ibuf_add_n32(struct ibuf *, uint64_t);
91 int ibuf_add_n64(struct ibuf *, uint64_t);
92 int ibuf_add_h16(struct ibuf *, uint64_t);
93 int ibuf_add_h32(struct ibuf *, uint64_t);
94 int ibuf_add_h64(struct ibuf *, uint64_t);
95 void *ibuf_reserve(struct ibuf *, size_t);
96 void *ibuf_seek(struct ibuf *, size_t, size_t);
97 int ibuf_set(struct ibuf *, size_t, const void *, size_t);
98 int ibuf_set_n8(struct ibuf *, size_t, uint64_t);
99 int ibuf_set_n16(struct ibuf *, size_t, uint64_t);
100 int ibuf_set_n32(struct ibuf *, size_t, uint64_t);
101 int ibuf_set_n64(struct ibuf *, size_t, uint64_t);
102 int ibuf_set_h16(struct ibuf *, size_t, uint64_t);
103 int ibuf_set_h32(struct ibuf *, size_t, uint64_t);
104 int ibuf_set_h64(struct ibuf *, size_t, uint64_t);
105 void *ibuf_data(const struct ibuf *);
106 size_t ibuf_size(const struct ibuf *);
107 size_t ibuf_left(const struct ibuf *);
108 int ibuf_truncate(struct ibuf *, size_t);
109 void ibuf_rewind(struct ibuf *);
110 void ibuf_close(struct msgbuf *, struct ibuf *);
111 void ibuf_from_buffer(struct ibuf *, void *, size_t);
112 void ibuf_from_ibuf(struct ibuf *, const struct ibuf *);
113 int ibuf_get(struct ibuf *, void *, size_t);
114 int ibuf_get_ibuf(struct ibuf *, size_t, struct ibuf *);
115 int ibuf_get_n8(struct ibuf *, uint8_t *);
116 int ibuf_get_n16(struct ibuf *, uint16_t *);
117 int ibuf_get_n32(struct ibuf *, uint32_t *);
118 int ibuf_get_n64(struct ibuf *, uint64_t *);
119 int ibuf_get_h16(struct ibuf *, uint16_t *);
120 int ibuf_get_h32(struct ibuf *, uint32_t *);
121 int ibuf_get_h64(struct ibuf *, uint64_t *);
122 int ibuf_skip(struct ibuf *, size_t);
123 void ibuf_free(struct ibuf *);
124 int ibuf_fd_avail(struct ibuf *);
125 int ibuf_fd_get(struct ibuf *);
126 void ibuf_fd_set(struct ibuf *, int);
127 int ibuf_write(struct msgbuf *);
128 void msgbuf_init(struct msgbuf *);
129 void msgbuf_clear(struct msgbuf *);
130 uint32_t msgbuf_queuelen(struct msgbuf *);
131 int msgbuf_write(struct msgbuf *);
133 /* imsg.c */
134 void imsg_init(struct imsgbuf *, int);
135 ssize_t imsg_read(struct imsgbuf *);
136 ssize_t imsg_get(struct imsgbuf *, struct imsg *);
137 int imsg_get_ibuf(struct imsg *, struct ibuf *);
138 int imsg_get_data(struct imsg *, void *, size_t);
139 int imsg_get_fd(struct imsg *);
140 uint32_t imsg_get_id(struct imsg *);
141 size_t imsg_get_len(struct imsg *);
142 pid_t imsg_get_pid(struct imsg *);
143 uint32_t imsg_get_type(struct imsg *);
144 int imsg_forward(struct imsgbuf *, struct imsg *);
145 int imsg_compose(struct imsgbuf *, uint32_t, uint32_t, pid_t, int,
146 const void *, size_t);
147 int imsg_composev(struct imsgbuf *, uint32_t, uint32_t, pid_t, int,
148 const struct iovec *, int);
149 int imsg_compose_ibuf(struct imsgbuf *, uint32_t, uint32_t, pid_t,
150 struct ibuf *);
151 struct ibuf *imsg_create(struct imsgbuf *, uint32_t, uint32_t, pid_t, size_t);
152 int imsg_add(struct ibuf *, const void *, size_t);
153 void imsg_close(struct imsgbuf *, struct ibuf *);
154 void imsg_free(struct imsg *);
155 int imsg_flush(struct imsgbuf *);
156 void imsg_clear(struct imsgbuf *);
158 #endif