Blame


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