Blob


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