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