Blame


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