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