xref: /openbsd/usr.sbin/vmd/proc.h (revision 65bbee46)
1 /*	$OpenBSD: proc.h,v 1.25 2024/09/26 01:45:13 jsg 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/queue.h>
20 #include <sys/socket.h>
21 
22 #include <event.h>
23 #include <imsg.h>
24 
25 #ifndef _PROC_H
26 #define _PROC_H
27 
28 enum {
29 	IMSG_NONE,
30 	IMSG_CTL_OK,
31 	IMSG_CTL_FAIL,
32 	IMSG_CTL_VERBOSE,
33 	IMSG_CTL_END,
34 	IMSG_CTL_RESET,
35 	IMSG_CTL_PROCFD,
36 	IMSG_PROC_MAX
37 };
38 
39 /* imsg */
40 struct imsgev {
41 	struct imsgbuf		 ibuf;
42 	void			(*handler)(int, short, void *);
43 	struct event		 ev;
44 	struct privsep_proc	*proc;
45 	void			*data;
46 	short			 events;
47 };
48 
49 #define IMSG_SIZE_CHECK(imsg, p) do {					\
50 	if (IMSG_DATA_SIZE(imsg) < sizeof(*p))				\
51 		fatalx("bad length imsg received (%s)",	#p);		\
52 } while (0)
53 #define IMSG_DATA_SIZE(imsg)	((imsg)->hdr.len - IMSG_HEADER_SIZE)
54 
55 /* control socket */
56 struct control_sock {
57 	const char	*cs_name;
58 	struct event	 cs_ev;
59 	struct event	 cs_evt;
60 	int		 cs_fd;
61 	int		 cs_restricted;
62 	void		*cs_env;
63 	uid_t		 cs_uid;
64 	gid_t		 cs_gid;
65 
66 	TAILQ_ENTRY(control_sock) cs_entry;
67 };
68 TAILQ_HEAD(control_socks, control_sock);
69 
70 struct ctl_conn {
71 	TAILQ_ENTRY(ctl_conn)	 entry;
72 	struct imsgev		 iev;
73 	struct sockpeercred	 peercred;
74 };
75 TAILQ_HEAD(ctl_connlist, ctl_conn);
76 
77 /* privsep */
78 enum privsep_procid {
79 	PROC_PARENT	= 0,
80 	PROC_CONTROL,
81 	PROC_AGENTX,
82 	PROC_VMM,
83 	PROC_PRIV,
84 	PROC_MAX,
85 };
86 extern enum privsep_procid privsep_process;
87 
88 #define CONFIG_RELOAD		0x00
89 #define CONFIG_VMS		0x01
90 #define CONFIG_SWITCHES		0x02
91 #define CONFIG_ALL		0xff
92 
93 struct privsep_pipes {
94 	int				*pp_pipes[PROC_MAX];
95 };
96 
97 struct privsep {
98 	struct privsep_pipes		*ps_pipes[PROC_MAX];
99 	struct privsep_pipes		*ps_pp;
100 
101 	struct imsgev			*ps_ievs[PROC_MAX];
102 	const char			*ps_title[PROC_MAX];
103 	uint8_t				 ps_what[PROC_MAX];
104 
105 	struct passwd			*ps_pw;
106 	int				 ps_noaction;
107 
108 	struct control_sock		 ps_csock;
109 	struct control_socks		 ps_rcsocks;
110 
111 	unsigned int			 ps_instances[PROC_MAX];
112 	unsigned int			 ps_instance;
113 
114 	/* Event and signal handlers */
115 	struct event			 ps_evsigint;
116 	struct event			 ps_evsigterm;
117 	struct event			 ps_evsigchld;
118 	struct event			 ps_evsighup;
119 	struct event			 ps_evsigpipe;
120 	struct event			 ps_evsigusr1;
121 
122 	void				*ps_env;
123 };
124 
125 struct privsep_proc {
126 	const char		*p_title;
127 	enum privsep_procid	 p_id;
128 	int			(*p_cb)(int, struct privsep_proc *,
129 				    struct imsg *);
130 	void			(*p_init)(struct privsep *,
131 				    struct privsep_proc *);
132 	void			(*p_shutdown)(void);
133 	const char		*p_chroot;
134 	struct passwd		*p_pw;
135 	struct privsep		*p_ps;
136 };
137 
138 struct privsep_fd {
139 	enum privsep_procid		 pf_procid;
140 	unsigned int			 pf_instance;
141 };
142 
143 #if DEBUG
144 #define DPRINTF		log_debug
145 #else
146 #define DPRINTF(x...)	do {} while(0)
147 #endif
148 
149 #define PROC_PARENT_SOCK_FILENO	3
150 #define PROC_MAX_INSTANCES	32
151 
152 /* proc.c */
153 void	 proc_init(struct privsep *, struct privsep_proc *, unsigned int, int,
154 	    int, char **, enum privsep_procid);
155 void	 proc_kill(struct privsep *);
156 void	 proc_connect(struct privsep *ps);
157 void	 proc_dispatch(int, short event, void *);
158 void	 proc_run(struct privsep *, struct privsep_proc *,
159 	    struct privsep_proc *, unsigned int,
160 	    void (*)(struct privsep *, struct privsep_proc *, void *), void *);
161 void	 imsg_event_add(struct imsgev *);
162 void	 imsg_event_add2(struct imsgev *, struct event_base *);
163 int	 imsg_compose_event(struct imsgev *, uint16_t, uint32_t,
164 	    pid_t, int, void *, uint16_t);
165 int	 imsg_compose_event2(struct imsgev *, uint16_t, uint32_t,
166     	    pid_t, int, void *, uint16_t, struct event_base *);
167 int	 imsg_composev_event(struct imsgev *, uint16_t, uint32_t,
168 	    pid_t, int, const struct iovec *, int);
169 int	 proc_compose_imsg(struct privsep *, enum privsep_procid, int,
170 	    uint16_t, uint32_t, int, void *, uint16_t);
171 int	 proc_compose(struct privsep *, enum privsep_procid,
172 	    uint16_t, void *data, uint16_t);
173 int	 proc_composev_imsg(struct privsep *, enum privsep_procid, int,
174 	    uint16_t, uint32_t, int, const struct iovec *, int);
175 int	 proc_composev(struct privsep *, enum privsep_procid,
176 	    uint16_t, const struct iovec *, int);
177 int	 proc_forward_imsg(struct privsep *, struct imsg *,
178 	    enum privsep_procid, int);
179 struct imsgbuf *
180 	 proc_ibuf(struct privsep *, enum privsep_procid, int);
181 struct imsgev *
182 	 proc_iev(struct privsep *, enum privsep_procid, int);
183 enum privsep_procid
184 	 proc_getid(struct privsep_proc *, unsigned int, const char *);
185 int	 proc_flush_imsg(struct privsep *, enum privsep_procid, int);
186 
187 /* control.c */
188 void	 control(struct privsep *, struct privsep_proc *);
189 int	 control_init(struct privsep *, struct control_sock *);
190 int	 control_reset(struct control_sock *);
191 int	 control_listen(struct control_sock *);
192 
193 /* log.c */
194 void	log_init(int, int);
195 void	log_procinit(const char *, ...);
196 void	log_setverbose(int);
197 int	log_getverbose(void);
198 void	log_warn(const char *, ...)
199 	    __attribute__((__format__ (printf, 1, 2)));
200 void	log_warnx(const char *, ...)
201 	    __attribute__((__format__ (printf, 1, 2)));
202 void	log_info(const char *, ...)
203 	    __attribute__((__format__ (printf, 1, 2)));
204 void	log_debug(const char *, ...)
205 	    __attribute__((__format__ (printf, 1, 2)));
206 void	logit(int, const char *, ...)
207 	    __attribute__((__format__ (printf, 2, 3)));
208 void	vlog(int, const char *, va_list)
209 	    __attribute__((__format__ (printf, 2, 0)));
210 __dead void fatal(const char *, ...)
211 	    __attribute__((__format__ (printf, 1, 2)));
212 __dead void fatalx(const char *, ...)
213 	    __attribute__((__format__ (printf, 1, 2)));
214 
215 #endif /* _PROC_H */
216