Blame


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