Blame


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