Blame


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