Blame


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