Blame


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