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