xref: /openbsd/usr.sbin/vmd/proc.h (revision fc61954a)
1 /*	$OpenBSD: proc.h,v 1.9 2016/10/05 17:30:13 reyk Exp $	*/
2 
3 /*
4  * Copyright (c) 2010-2015 Reyk Floeter <reyk@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/socket.h>
20 #include <sys/queue.h>
21 #include <sys/uio.h>
22 
23 #include <imsg.h>
24 #include <event.h>
25 
26 #ifndef _PROC_H
27 #define _PROC_H
28 
29 enum {
30 	IMSG_NONE,
31 	IMSG_CTL_OK,
32 	IMSG_CTL_FAIL,
33 	IMSG_CTL_VERBOSE,
34 	IMSG_CTL_END,
35 	IMSG_CTL_NOTIFY,
36 	IMSG_CTL_RESET,
37 	IMSG_CTL_PROCFD,
38 	IMSG_PROC_MAX
39 };
40 
41 /* imsg */
42 struct imsgev {
43 	struct imsgbuf		 ibuf;
44 	void			(*handler)(int, short, void *);
45 	struct event		 ev;
46 	struct privsep_proc	*proc;
47 	void			*data;
48 	short			 events;
49 };
50 
51 #define IMSG_SIZE_CHECK(imsg, p) do {					\
52 	if (IMSG_DATA_SIZE(imsg) < sizeof(*p))				\
53 		fatalx("bad length imsg received (%s)",	#p);		\
54 } while (0)
55 #define IMSG_DATA_SIZE(imsg)	((imsg)->hdr.len - IMSG_HEADER_SIZE)
56 
57 /* control socket */
58 struct control_sock {
59 	const char	*cs_name;
60 	struct event	 cs_ev;
61 	struct event	 cs_evt;
62 	int		 cs_fd;
63 	int		 cs_restricted;
64 	void		*cs_env;
65 
66 	TAILQ_ENTRY(control_sock) cs_entry;
67 };
68 TAILQ_HEAD(control_socks, control_sock);
69 
70 struct {
71 	struct event	 ev;
72 	int		 fd;
73 } control_state;
74 
75 struct ctl_conn {
76 	TAILQ_ENTRY(ctl_conn)	 entry;
77 	uint8_t			 flags;
78 	unsigned int		 waiting;
79 #define CTL_CONN_NOTIFY		 0x01
80 	struct imsgev		 iev;
81 	struct sockpeercred	 peercred;
82 
83 };
84 TAILQ_HEAD(ctl_connlist, ctl_conn);
85 extern  struct ctl_connlist ctl_conns;
86 
87 /* privsep */
88 enum privsep_procid {
89 	PROC_PARENT	= 0,
90 	PROC_CONTROL,
91 	PROC_VMM,
92 	PROC_PRIV,
93 	PROC_MAX,
94 } privsep_process;
95 
96 #define CONFIG_RELOAD		0x00
97 #define CONFIG_VMS		0x01
98 #define CONFIG_SWITCHES		0x02
99 #define CONFIG_ALL		0xff
100 
101 struct privsep_pipes {
102 	int				*pp_pipes[PROC_MAX];
103 };
104 
105 struct privsep {
106 	struct privsep_pipes		*ps_pipes[PROC_MAX];
107 	struct privsep_pipes		*ps_pp;
108 
109 	struct imsgev			*ps_ievs[PROC_MAX];
110 	const char			*ps_title[PROC_MAX];
111 	uint8_t				 ps_what[PROC_MAX];
112 
113 	struct passwd			*ps_pw;
114 	int				 ps_noaction;
115 
116 	struct control_sock		 ps_csock;
117 	struct control_socks		 ps_rcsocks;
118 
119 	unsigned int			 ps_instances[PROC_MAX];
120 	unsigned int			 ps_instance;
121 
122 	/* Event and signal handlers */
123 	struct event			 ps_evsigint;
124 	struct event			 ps_evsigterm;
125 	struct event			 ps_evsigchld;
126 	struct event			 ps_evsighup;
127 	struct event			 ps_evsigpipe;
128 	struct event			 ps_evsigusr1;
129 
130 	void				*ps_env;
131 };
132 
133 struct privsep_proc {
134 	const char		*p_title;
135 	enum privsep_procid	 p_id;
136 	int			(*p_cb)(int, struct privsep_proc *,
137 				    struct imsg *);
138 	void			(*p_init)(struct privsep *,
139 				    struct privsep_proc *);
140 	void			(*p_shutdown)(void);
141 	const char		*p_chroot;
142 	struct passwd		*p_pw;
143 	struct privsep		*p_ps;
144 };
145 
146 struct privsep_fd {
147 	enum privsep_procid		 pf_procid;
148 	unsigned int			 pf_instance;
149 };
150 
151 #if DEBUG
152 #define DPRINTF		log_debug
153 #else
154 #define DPRINTF(x...)	do {} while(0)
155 #endif
156 
157 #define PROC_PARENT_SOCK_FILENO 3
158 #define PROC_MAX_INSTANCES      32
159 
160 /* proc.c */
161 void	 proc_init(struct privsep *, struct privsep_proc *, unsigned int,
162 	    int, char **, enum privsep_procid);
163 void	 proc_kill(struct privsep *);
164 void	 proc_connect(struct privsep *ps);
165 void	 proc_dispatch(int, short event, void *);
166 void	 proc_run(struct privsep *, struct privsep_proc *,
167 	    struct privsep_proc *, unsigned int,
168 	    void (*)(struct privsep *, struct privsep_proc *, void *), void *);
169 void	 imsg_event_add(struct imsgev *);
170 int	 imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
171 	    pid_t, int, void *, uint16_t);
172 int	 imsg_composev_event(struct imsgev *, uint16_t, uint32_t,
173 	    pid_t, int, const struct iovec *, int);
174 int	 proc_compose_imsg(struct privsep *, enum privsep_procid, int,
175 	    uint16_t, uint32_t, int, void *, uint16_t);
176 int	 proc_compose(struct privsep *, enum privsep_procid,
177 	    uint16_t, void *data, uint16_t);
178 int	 proc_composev_imsg(struct privsep *, enum privsep_procid, int,
179 	    uint16_t, uint32_t, int, const struct iovec *, int);
180 int	 proc_composev(struct privsep *, enum privsep_procid,
181 	    uint16_t, const struct iovec *, int);
182 int	 proc_forward_imsg(struct privsep *, struct imsg *,
183 	    enum privsep_procid, int);
184 struct imsgbuf *
185 	 proc_ibuf(struct privsep *, enum privsep_procid, int);
186 struct imsgev *
187 	 proc_iev(struct privsep *, enum privsep_procid, int);
188 enum privsep_procid
189 	 proc_getid(struct privsep_proc *, unsigned int, const char *);
190 
191 /* control.c */
192 void	 control(struct privsep *, struct privsep_proc *);
193 int	 control_init(struct privsep *, struct control_sock *);
194 int	 control_listen(struct control_sock *);
195 void	 control_cleanup(struct control_sock *);
196 
197 /* log.c */
198 void	log_init(int, int);
199 void	log_procinit(const char *);
200 void	log_verbose(int);
201 void	log_warn(const char *, ...)
202 	    __attribute__((__format__ (printf, 1, 2)));
203 void	log_warnx(const char *, ...)
204 	    __attribute__((__format__ (printf, 1, 2)));
205 void	log_info(const char *, ...)
206 	    __attribute__((__format__ (printf, 1, 2)));
207 void	log_debug(const char *, ...)
208 	    __attribute__((__format__ (printf, 1, 2)));
209 void	logit(int, const char *, ...)
210 	    __attribute__((__format__ (printf, 2, 3)));
211 void	vlog(int, const char *, va_list)
212 	    __attribute__((__format__ (printf, 2, 0)));
213 __dead void fatal(const char *, ...)
214 	    __attribute__((__format__ (printf, 1, 2)));
215 __dead void fatalx(const char *, ...)
216 	    __attribute__((__format__ (printf, 1, 2)));
217 
218 #endif /* _PROC_H */
219