xref: /openbsd-src/usr.sbin/vmd/proc.c (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1 /*	$OpenBSD: proc.c,v 1.21 2023/09/26 01:53:54 dv Exp $	*/
2 
3 /*
4  * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
5  * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/types.h>
21 #include <sys/queue.h>
22 #include <sys/socket.h>
23 #include <sys/wait.h>
24 
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <paths.h>
33 #include <pwd.h>
34 #include <event.h>
35 #include <imsg.h>
36 
37 #include "proc.h"
38 
39 void	 proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int,
40 	    int, char **);
41 void	 proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
42 void	 proc_open(struct privsep *, int, int);
43 void	 proc_accept(struct privsep *, int, enum privsep_procid,
44 	    unsigned int);
45 void	 proc_close(struct privsep *);
46 void	 proc_shutdown(struct privsep_proc *);
47 void	 proc_sig_handler(int, short, void *);
48 void	 proc_range(struct privsep *, enum privsep_procid, int *, int *);
49 int	 proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
50 
51 enum privsep_procid
52 proc_getid(struct privsep_proc *procs, unsigned int nproc,
53     const char *proc_name)
54 {
55 	struct privsep_proc	*p;
56 	unsigned int		 proc;
57 
58 	for (proc = 0; proc < nproc; proc++) {
59 		p = &procs[proc];
60 		if (strcmp(p->p_title, proc_name))
61 			continue;
62 
63 		return (p->p_id);
64 	}
65 
66 	return (PROC_MAX);
67 }
68 
69 void
70 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
71     int debug, int argc, char **argv)
72 {
73 	unsigned int		 proc, nargc, i, proc_i;
74 	char			**nargv;
75 	struct privsep_proc	*p;
76 	char			 num[32];
77 	int			 fd;
78 
79 	/* Prepare the new process argv. */
80 	nargv = calloc(argc + 5, sizeof(char *));
81 	if (nargv == NULL)
82 		fatal("%s: calloc", __func__);
83 
84 	/* Copy call argument first. */
85 	nargc = 0;
86 	nargv[nargc++] = argv[0];
87 
88 	/* Set process name argument and save the position. */
89 	nargv[nargc++] = "-P";
90 	proc_i = nargc;
91 	nargc++;
92 
93 	/* Point process instance arg to stack and copy the original args. */
94 	nargv[nargc++] = "-I";
95 	nargv[nargc++] = num;
96 	for (i = 1; i < (unsigned int) argc; i++)
97 		nargv[nargc++] = argv[i];
98 
99 	nargv[nargc] = NULL;
100 
101 	for (proc = 0; proc < nproc; proc++) {
102 		p = &procs[proc];
103 
104 		/* Update args with process title. */
105 		nargv[proc_i] = (char *)(uintptr_t)p->p_title;
106 
107 		/* Fire children processes. */
108 		for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
109 			/* Update the process instance number. */
110 			snprintf(num, sizeof(num), "%u", i);
111 
112 			fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0];
113 			ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0] = -1;
114 
115 			switch (fork()) {
116 			case -1:
117 				fatal("%s: fork", __func__);
118 				break;
119 			case 0:
120 				/* First create a new session */
121 				if (setsid() == -1)
122 					fatal("setsid");
123 
124 				/* Prepare parent socket. */
125 				if (fd != PROC_PARENT_SOCK_FILENO) {
126 					if (dup2(fd, PROC_PARENT_SOCK_FILENO)
127 					    == -1)
128 						fatal("dup2");
129 				} else if (fcntl(fd, F_SETFD, 0) == -1)
130 					fatal("fcntl");
131 
132 				/* Daemons detach from terminal. */
133 				if (!debug && (fd =
134 				    open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
135 					(void)dup2(fd, STDIN_FILENO);
136 					(void)dup2(fd, STDOUT_FILENO);
137 					(void)dup2(fd, STDERR_FILENO);
138 					if (fd > 2)
139 						(void)close(fd);
140 				}
141 
142 				execvp(argv[0], nargv);
143 				fatal("%s: execvp", __func__);
144 				break;
145 			default:
146 				/* Close child end. */
147 				close(fd);
148 				break;
149 			}
150 		}
151 	}
152 	free(nargv);
153 }
154 
155 void
156 proc_connect(struct privsep *ps)
157 {
158 	struct imsgev		*iev;
159 	unsigned int		 src, dst, inst;
160 
161 	/* Don't distribute any sockets if we are not really going to run. */
162 	if (ps->ps_noaction)
163 		return;
164 
165 	for (dst = 0; dst < PROC_MAX; dst++) {
166 		/* We don't communicate with ourselves. */
167 		if (dst == PROC_PARENT)
168 			continue;
169 
170 		for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
171 			iev = &ps->ps_ievs[dst][inst];
172 			imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
173 			event_set(&iev->ev, iev->ibuf.fd, iev->events,
174 			    iev->handler, iev->data);
175 			event_add(&iev->ev, NULL);
176 		}
177 	}
178 
179 	/* Distribute the socketpair()s for everyone. */
180 	for (src = 0; src < PROC_MAX; src++)
181 		for (dst = src; dst < PROC_MAX; dst++) {
182 			/* Parent already distributed its fds. */
183 			if (src == PROC_PARENT || dst == PROC_PARENT)
184 				continue;
185 
186 			proc_open(ps, src, dst);
187 		}
188 }
189 
190 void
191 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
192     int debug, int argc, char **argv, enum privsep_procid proc_id)
193 {
194 	struct privsep_proc	*p = NULL;
195 	struct privsep_pipes	*pa, *pb;
196 	unsigned int		 proc;
197 	unsigned int		 dst;
198 	int			 fds[2];
199 
200 	/* Don't initiate anything if we are not really going to run. */
201 	if (ps->ps_noaction)
202 		return;
203 
204 	if (proc_id == PROC_PARENT) {
205 		privsep_process = PROC_PARENT;
206 		proc_setup(ps, procs, nproc);
207 
208 		/*
209 		 * Create the children sockets so we can use them
210 		 * to distribute the rest of the socketpair()s using
211 		 * proc_connect() later.
212 		 */
213 		for (dst = 0; dst < PROC_MAX; dst++) {
214 			/* Don't create socket for ourselves. */
215 			if (dst == PROC_PARENT)
216 				continue;
217 
218 			for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
219 				pa = &ps->ps_pipes[PROC_PARENT][0];
220 				pb = &ps->ps_pipes[dst][proc];
221 				if (socketpair(AF_UNIX,
222 				    SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
223 				    PF_UNSPEC, fds) == -1)
224 					fatal("%s: socketpair", __func__);
225 
226 				pa->pp_pipes[dst][proc] = fds[0];
227 				pb->pp_pipes[PROC_PARENT][0] = fds[1];
228 			}
229 		}
230 
231 		/* Engage! */
232 		proc_exec(ps, procs, nproc, debug, argc, argv);
233 		return;
234 	}
235 
236 	/* Initialize a child */
237 	for (proc = 0; proc < nproc; proc++) {
238 		if (procs[proc].p_id != proc_id)
239 			continue;
240 		p = &procs[proc];
241 		break;
242 	}
243 	if (p == NULL || p->p_init == NULL)
244 		fatalx("%s: process %d missing process initialization",
245 		    __func__, proc_id);
246 
247 	p->p_init(ps, p);
248 
249 	fatalx("failed to initiate child process");
250 }
251 
252 void
253 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
254     unsigned int n)
255 {
256 	struct privsep_pipes	*pp = ps->ps_pp;
257 	struct imsgev		*iev;
258 
259 	if (ps->ps_ievs[dst] == NULL) {
260 #if DEBUG > 1
261 		log_debug("%s: %s src %d %d to dst %d %d not connected",
262 		    __func__, ps->ps_title[privsep_process],
263 		    privsep_process, ps->ps_instance + 1,
264 		    dst, n + 1);
265 #endif
266 		close(fd);
267 		return;
268 	}
269 
270 	if (pp->pp_pipes[dst][n] != -1) {
271 		log_warnx("%s: duplicated descriptor", __func__);
272 		close(fd);
273 		return;
274 	} else
275 		pp->pp_pipes[dst][n] = fd;
276 
277 	iev = &ps->ps_ievs[dst][n];
278 	imsg_init(&iev->ibuf, fd);
279 	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
280 	event_add(&iev->ev, NULL);
281 }
282 
283 void
284 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
285 {
286 	unsigned int		 i, j, src, dst, id;
287 	struct privsep_pipes	*pp;
288 
289 	/* Initialize parent title, ps_instances and procs. */
290 	ps->ps_title[PROC_PARENT] = "vmd";
291 
292 	for (src = 0; src < PROC_MAX; src++)
293 		/* Default to 1 process instance */
294 		if (ps->ps_instances[src] < 1)
295 			ps->ps_instances[src] = 1;
296 
297 	for (src = 0; src < nproc; src++) {
298 		procs[src].p_ps = ps;
299 		if (procs[src].p_cb == NULL)
300 			procs[src].p_cb = proc_dispatch_null;
301 
302 		id = procs[src].p_id;
303 		ps->ps_title[id] = procs[src].p_title;
304 		if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
305 		    sizeof(struct imsgev))) == NULL)
306 			fatal("%s: calloc", __func__);
307 
308 		/* With this set up, we are ready to call imsg_init(). */
309 		for (i = 0; i < ps->ps_instances[id]; i++) {
310 			ps->ps_ievs[id][i].handler = proc_dispatch;
311 			ps->ps_ievs[id][i].events = EV_READ;
312 			ps->ps_ievs[id][i].proc = &procs[src];
313 			ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
314 		}
315 	}
316 
317 	/*
318 	 * Allocate pipes for all process instances (incl. parent)
319 	 *
320 	 * - ps->ps_pipes: N:M mapping
321 	 * N source processes connected to M destination processes:
322 	 * [src][instances][dst][instances], for example
323 	 * [PROC_RELAY][3][PROC_CA][3]
324 	 *
325 	 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
326 	 * Each process instance has a destination array of socketpair fds:
327 	 * [dst][instances], for example
328 	 * [PROC_PARENT][0]
329 	 */
330 	for (src = 0; src < PROC_MAX; src++) {
331 		/* Allocate destination array for each process */
332 		if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
333 		    sizeof(struct privsep_pipes))) == NULL)
334 			fatal("%s: calloc", __func__);
335 
336 		for (i = 0; i < ps->ps_instances[src]; i++) {
337 			pp = &ps->ps_pipes[src][i];
338 
339 			for (dst = 0; dst < PROC_MAX; dst++) {
340 				/* Allocate maximum fd integers */
341 				if ((pp->pp_pipes[dst] =
342 				    calloc(ps->ps_instances[dst],
343 				    sizeof(int))) == NULL)
344 					fatal("%s: calloc", __func__);
345 
346 				/* Mark fd as unused */
347 				for (j = 0; j < ps->ps_instances[dst]; j++)
348 					pp->pp_pipes[dst][j] = -1;
349 			}
350 		}
351 	}
352 
353 	ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
354 }
355 
356 void
357 proc_kill(struct privsep *ps)
358 {
359 	char		*cause;
360 	pid_t		 pid;
361 	int		 len, status;
362 
363 	if (privsep_process != PROC_PARENT)
364 		return;
365 
366 	proc_close(ps);
367 
368 	do {
369 		pid = waitpid(WAIT_ANY, &status, 0);
370 		if (pid <= 0)
371 			continue;
372 
373 		if (WIFSIGNALED(status)) {
374 			len = asprintf(&cause, "terminated; signal %d",
375 			    WTERMSIG(status));
376 		} else if (WIFEXITED(status)) {
377 			if (WEXITSTATUS(status) != 0)
378 				len = asprintf(&cause, "exited abnormally");
379 			else
380 				len = 0;
381 		} else
382 			len = -1;
383 
384 		if (len == 0) {
385 			/* child exited OK, don't print a warning message */
386 		} else if (len != -1) {
387 			log_warnx("lost child: pid %u %s", pid, cause);
388 			free(cause);
389 		} else
390 			log_warnx("lost child: pid %u", pid);
391 	} while (pid != -1 || (pid == -1 && errno == EINTR));
392 }
393 
394 void
395 proc_open(struct privsep *ps, int src, int dst)
396 {
397 	struct privsep_pipes	*pa, *pb;
398 	struct privsep_fd	 pf;
399 	int			 fds[2];
400 	unsigned int		 i, j;
401 
402 	/* Exchange pipes between process. */
403 	for (i = 0; i < ps->ps_instances[src]; i++) {
404 		for (j = 0; j < ps->ps_instances[dst]; j++) {
405 			/* Don't create sockets for ourself. */
406 			if (src == dst && i == j)
407 				continue;
408 
409 			pa = &ps->ps_pipes[src][i];
410 			pb = &ps->ps_pipes[dst][j];
411 			if (socketpair(AF_UNIX,
412 			    SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
413 			    PF_UNSPEC, fds) == -1)
414 				fatal("%s: socketpair", __func__);
415 
416 			pa->pp_pipes[dst][j] = fds[0];
417 			pb->pp_pipes[src][i] = fds[1];
418 
419 			pf.pf_procid = src;
420 			pf.pf_instance = i;
421 			if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
422 			    -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
423 				fatal("%s: proc_compose_imsg", __func__);
424 
425 			pf.pf_procid = dst;
426 			pf.pf_instance = j;
427 			if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
428 			    -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
429 				fatal("%s: proc_compose_imsg", __func__);
430 
431 			/*
432 			 * We have to flush to send the descriptors and close
433 			 * them to avoid the fd ramp on startup.
434 			 */
435 			if (proc_flush_imsg(ps, src, i) == -1 ||
436 			    proc_flush_imsg(ps, dst, j) == -1)
437 				fatal("%s: imsg_flush", __func__);
438 		}
439 	}
440 }
441 
442 void
443 proc_close(struct privsep *ps)
444 {
445 	unsigned int		 dst, n;
446 	struct privsep_pipes	*pp;
447 
448 	if (ps == NULL)
449 		return;
450 
451 	pp = ps->ps_pp;
452 
453 	for (dst = 0; dst < PROC_MAX; dst++) {
454 		if (ps->ps_ievs[dst] == NULL)
455 			continue;
456 
457 		for (n = 0; n < ps->ps_instances[dst]; n++) {
458 			if (pp->pp_pipes[dst][n] == -1)
459 				continue;
460 
461 			/* Cancel the fd, close and invalidate the fd */
462 			event_del(&(ps->ps_ievs[dst][n].ev));
463 			imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
464 			close(pp->pp_pipes[dst][n]);
465 			pp->pp_pipes[dst][n] = -1;
466 		}
467 		free(ps->ps_ievs[dst]);
468 	}
469 }
470 
471 void
472 proc_shutdown(struct privsep_proc *p)
473 {
474 	struct privsep	*ps = p->p_ps;
475 
476 	if (p->p_shutdown != NULL)
477 		(*p->p_shutdown)();
478 
479 	proc_close(ps);
480 
481 	log_info("%s exiting, pid %d", p->p_title, getpid());
482 
483 	exit(0);
484 }
485 
486 void
487 proc_sig_handler(int sig, short event, void *arg)
488 {
489 	struct privsep_proc	*p = arg;
490 
491 	switch (sig) {
492 	case SIGINT:
493 	case SIGTERM:
494 		proc_shutdown(p);
495 		break;
496 	case SIGCHLD:
497 	case SIGHUP:
498 	case SIGPIPE:
499 	case SIGUSR1:
500 		/* ignore */
501 		break;
502 	default:
503 		fatalx("%s: unexpected signal", __func__);
504 		/* NOTREACHED */
505 	}
506 }
507 
508 void
509 proc_run(struct privsep *ps, struct privsep_proc *p,
510     struct privsep_proc *procs, unsigned int nproc,
511     void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
512 {
513 	struct passwd		*pw;
514 	const char		*root;
515 	struct control_sock	*rcs;
516 
517 	log_procinit("%s", p->p_title);
518 
519 	/* Set the process group of the current process */
520 	setpgid(0, 0);
521 
522 	if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) {
523 		if (control_init(ps, &ps->ps_csock) == -1)
524 			fatalx("%s: control_init", __func__);
525 		TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry)
526 			if (control_init(ps, rcs) == -1)
527 				fatalx("%s: control_init", __func__);
528 	}
529 
530 	/* Use non-standard user */
531 	if (p->p_pw != NULL)
532 		pw = p->p_pw;
533 	else
534 		pw = ps->ps_pw;
535 
536 	/* Change root directory */
537 	if (p->p_chroot != NULL)
538 		root = p->p_chroot;
539 	else
540 		root = pw->pw_dir;
541 
542 	if (chroot(root) == -1)
543 		fatal("%s: chroot", __func__);
544 	if (chdir("/") == -1)
545 		fatal("%s: chdir(\"/\")", __func__);
546 
547 	privsep_process = p->p_id;
548 
549 	setproctitle("%s", p->p_title);
550 
551 	if (setgroups(1, &pw->pw_gid) ||
552 	    setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
553 	    setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
554 		fatal("%s: cannot drop privileges", __func__);
555 
556 	event_init();
557 
558 	signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
559 	signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
560 	signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
561 	signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
562 	signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
563 	signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
564 
565 	signal_add(&ps->ps_evsigint, NULL);
566 	signal_add(&ps->ps_evsigterm, NULL);
567 	signal_add(&ps->ps_evsigchld, NULL);
568 	signal_add(&ps->ps_evsighup, NULL);
569 	signal_add(&ps->ps_evsigpipe, NULL);
570 	signal_add(&ps->ps_evsigusr1, NULL);
571 
572 	proc_setup(ps, procs, nproc);
573 	proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
574 	if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) {
575 		if (control_listen(&ps->ps_csock) == -1)
576 			fatalx("%s: control_listen", __func__);
577 		TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry)
578 			if (control_listen(rcs) == -1)
579 				fatalx("%s: control_listen", __func__);
580 	}
581 
582 	DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
583 	    ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
584 
585 	if (run != NULL)
586 		run(ps, p, arg);
587 
588 	event_dispatch();
589 
590 	proc_shutdown(p);
591 }
592 
593 void
594 proc_dispatch(int fd, short event, void *arg)
595 {
596 	struct imsgev		*iev = arg;
597 	struct privsep_proc	*p = iev->proc;
598 	struct privsep		*ps = p->p_ps;
599 	struct imsgbuf		*ibuf;
600 	struct imsg		 imsg;
601 	ssize_t			 n;
602 	int			 verbose;
603 	const char		*title;
604 	struct privsep_fd	 pf;
605 
606 	title = ps->ps_title[privsep_process];
607 	ibuf = &iev->ibuf;
608 
609 	if (event & EV_READ) {
610 		if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
611 			fatal("%s: imsg_read", __func__);
612 		if (n == 0) {
613 			/* this pipe is dead, so remove the event handler */
614 			event_del(&iev->ev);
615 			event_loopexit(NULL);
616 			return;
617 		}
618 	}
619 
620 	if (event & EV_WRITE) {
621 		if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
622 			fatal("%s: msgbuf_write", __func__);
623 		if (n == 0) {
624 			/* this pipe is dead, so remove the event handler */
625 			event_del(&iev->ev);
626 			event_loopexit(NULL);
627 			return;
628 		}
629 	}
630 
631 	for (;;) {
632 		if ((n = imsg_get(ibuf, &imsg)) == -1)
633 			fatal("%s: imsg_get", __func__);
634 		if (n == 0)
635 			break;
636 
637 #if DEBUG > 1
638 		log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
639 		    __func__, title, ps->ps_instance + 1,
640 		    imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
641 #endif
642 
643 		/*
644 		 * Check the message with the program callback
645 		 */
646 		if ((p->p_cb)(fd, p, &imsg) == 0) {
647 			/* Message was handled by the callback, continue */
648 			imsg_free(&imsg);
649 			continue;
650 		}
651 
652 		/*
653 		 * Generic message handling
654 		 */
655 		switch (imsg.hdr.type) {
656 		case IMSG_CTL_VERBOSE:
657 			IMSG_SIZE_CHECK(&imsg, &verbose);
658 			memcpy(&verbose, imsg.data, sizeof(verbose));
659 			log_setverbose(verbose);
660 			break;
661 		case IMSG_CTL_PROCFD:
662 			IMSG_SIZE_CHECK(&imsg, &pf);
663 			memcpy(&pf, imsg.data, sizeof(pf));
664 			proc_accept(ps, imsg.fd, pf.pf_procid,
665 			    pf.pf_instance);
666 			break;
667 		default:
668 			fatalx("%s: %s %d got invalid imsg %d peerid %d "
669 			    "from %s %d",
670 			    __func__, title, ps->ps_instance + 1,
671 			    imsg.hdr.type, imsg.hdr.peerid,
672 			    p->p_title, imsg.hdr.pid);
673 		}
674 		imsg_free(&imsg);
675 	}
676 	imsg_event_add(iev);
677 }
678 
679 int
680 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
681 {
682 	return (-1);
683 }
684 
685 /*
686  * imsg helper functions
687  */
688 
689 void
690 imsg_event_add(struct imsgev *iev)
691 {
692 	if (iev->handler == NULL) {
693 		imsg_flush(&iev->ibuf);
694 		return;
695 	}
696 
697 	iev->events = EV_READ;
698 	if (iev->ibuf.w.queued)
699 		iev->events |= EV_WRITE;
700 
701 	event_del(&iev->ev);
702 	event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
703 	event_add(&iev->ev, NULL);
704 }
705 
706 int
707 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
708     pid_t pid, int fd, void *data, uint16_t datalen)
709 {
710 	int	ret;
711 
712 	if ((ret = imsg_compose(&iev->ibuf, type, peerid,
713 	    pid, fd, data, datalen)) == -1)
714 		return (ret);
715 	imsg_event_add(iev);
716 	return (ret);
717 }
718 
719 int
720 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
721     pid_t pid, int fd, const struct iovec *iov, int iovcnt)
722 {
723 	int	ret;
724 
725 	if ((ret = imsg_composev(&iev->ibuf, type, peerid,
726 	    pid, fd, iov, iovcnt)) == -1)
727 		return (ret);
728 	imsg_event_add(iev);
729 	return (ret);
730 }
731 
732 void
733 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
734 {
735 	if (*n == -1) {
736 		/* Use a range of all target instances */
737 		*n = 0;
738 		*m = ps->ps_instances[id];
739 	} else {
740 		/* Use only a single slot of the specified peer process */
741 		*m = *n + 1;
742 	}
743 }
744 
745 int
746 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
747     uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
748 {
749 	int	 m;
750 
751 	proc_range(ps, id, &n, &m);
752 	for (; n < m; n++) {
753 		if (imsg_compose_event(&ps->ps_ievs[id][n],
754 		    type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
755 			return (-1);
756 	}
757 
758 	return (0);
759 }
760 
761 int
762 proc_compose(struct privsep *ps, enum privsep_procid id,
763     uint16_t type, void *data, uint16_t datalen)
764 {
765 	return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
766 }
767 
768 int
769 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
770     uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
771 {
772 	int	 m;
773 
774 	proc_range(ps, id, &n, &m);
775 	for (; n < m; n++)
776 		if (imsg_composev_event(&ps->ps_ievs[id][n],
777 		    type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
778 			return (-1);
779 
780 	return (0);
781 }
782 
783 int
784 proc_composev(struct privsep *ps, enum privsep_procid id,
785     uint16_t type, const struct iovec *iov, int iovcnt)
786 {
787 	return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
788 }
789 
790 int
791 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
792     enum privsep_procid id, int n)
793 {
794 	return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
795 	    imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
796 }
797 
798 struct imsgbuf *
799 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
800 {
801 	int	 m;
802 
803 	proc_range(ps, id, &n, &m);
804 	return (&ps->ps_ievs[id][n].ibuf);
805 }
806 
807 struct imsgev *
808 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
809 {
810 	int	 m;
811 
812 	proc_range(ps, id, &n, &m);
813 	return (&ps->ps_ievs[id][n]);
814 }
815 
816 /* This function should only be called with care as it breaks async I/O */
817 int
818 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
819 {
820 	struct imsgbuf	*ibuf;
821 	int		 m, ret = 0;
822 
823 	proc_range(ps, id, &n, &m);
824 	for (; n < m; n++) {
825 		if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
826 			return (-1);
827 		do {
828 			ret = imsg_flush(ibuf);
829 		} while (ret == -1 && errno == EAGAIN);
830 		if (ret == -1)
831 			break;
832 		imsg_event_add(&ps->ps_ievs[id][n]);
833 	}
834 
835 	return (ret);
836 }
837