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