Blame


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