Blob


1 /*
2 * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
3 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/socket.h>
21 #include <sys/wait.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <signal.h>
30 #include <pwd.h>
31 #include <event.h>
32 #include <imsg.h>
34 #include "proc.h"
36 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int,
37 int, char **);
38 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
39 void proc_open(struct privsep *, int, int);
40 void proc_accept(struct privsep *, int, enum privsep_procid,
41 unsigned int);
42 void proc_close(struct privsep *);
43 int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid);
44 void proc_shutdown(struct privsep_proc *);
45 void proc_sig_handler(int, short, void *);
46 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
48 enum privsep_procid privsep_process;
50 int
51 proc_ispeer(struct privsep_proc *procs, unsigned int nproc,
52 enum privsep_procid type)
53 {
54 unsigned int i;
56 for (i = 0; i < nproc; i++)
57 if (procs[i].p_id == type)
58 return (1);
60 return (0);
61 }
63 enum privsep_procid
64 proc_getid(struct privsep_proc *procs, unsigned int nproc,
65 const char *proc_name)
66 {
67 struct privsep_proc *p;
68 unsigned int proc;
70 for (proc = 0; proc < nproc; proc++) {
71 p = &procs[proc];
72 if (strcmp(p->p_title, proc_name))
73 continue;
75 return (p->p_id);
76 }
78 return (PROC_MAX);
79 }
81 void
82 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
83 int argc, char **argv)
84 {
85 unsigned int proc, nargc, i, proc_i;
86 char **nargv;
87 struct privsep_proc *p;
88 char num[32];
89 int fd;
91 /* Prepare the new process argv. */
92 nargv = calloc(argc + 5, sizeof(char *));
93 if (nargv == NULL)
94 fatal("%s: calloc", __func__);
96 /* Copy call argument first. */
97 nargc = 0;
98 nargv[nargc++] = argv[0];
100 /* Set process name argument and save the position. */
101 nargv[nargc] = strdup("-P");
102 if (nargv[nargc] == NULL)
103 fatal("%s: strdup", __func__);
104 nargc++;
105 proc_i = nargc;
106 nargc++;
108 /* Point process instance arg to stack and copy the original args. */
109 nargv[nargc] = strdup("-I");
110 if (nargv[nargc] == NULL)
111 fatal("%s: strdup", __func__);
112 nargc++;
113 nargv[nargc++] = num;
114 for (i = 1; i < (unsigned int) argc; i++)
115 nargv[nargc++] = argv[i];
117 nargv[nargc] = NULL;
119 for (proc = 0; proc < nproc; proc++) {
120 p = &procs[proc];
122 /* Update args with process title. */
123 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
125 /* Fire children processes. */
126 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
127 /* Update the process instance number. */
128 snprintf(num, sizeof(num), "%u", i);
130 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0];
131 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0] = -1;
133 switch (fork()) {
134 case -1:
135 fatal("%s: fork", __func__);
136 break;
137 case 0:
138 /* First create a new session */
139 if (setsid() == -1)
140 fatal("setsid");
142 /* Prepare parent socket. */
143 if (fd != PROC_GOTWEBD_SOCK_FILENO) {
144 if (dup2(fd, PROC_GOTWEBD_SOCK_FILENO)
145 == -1)
146 fatal("dup2");
147 } else if (fcntl(fd, F_SETFD, 0) == -1)
148 fatal("fcntl");
150 execvp(argv[0], nargv);
151 fatal("%s: execvp", __func__);
152 break;
153 default:
154 /* Close child end. */
155 close(fd);
156 break;
161 free(nargv);
164 void
165 proc_connect(struct privsep *ps)
167 struct imsgev *iev;
168 unsigned int src, dst, inst;
170 /* Don't distribute any sockets if we are not really going to run. */
171 if (ps->ps_noaction)
172 return;
174 for (dst = 0; dst < PROC_MAX; dst++) {
175 /* We don't communicate with ourselves. */
176 if (dst == PROC_GOTWEBD)
177 continue;
179 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
180 iev = &ps->ps_ievs[dst][inst];
181 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
182 event_set(&iev->ev, iev->ibuf.fd, iev->events,
183 iev->handler, iev->data);
184 event_add(&iev->ev, NULL);
188 /* Distribute the socketpair()s for everyone. */
189 for (src = 0; src < PROC_MAX; src++)
190 for (dst = src; dst < PROC_MAX; dst++) {
191 /* Parent already distributed its fds. */
192 if (src == PROC_GOTWEBD || dst == PROC_GOTWEBD)
193 continue;
195 proc_open(ps, src, dst);
199 void
200 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
201 int argc, char **argv, enum privsep_procid proc_id)
203 struct privsep_proc *p = NULL;
204 struct privsep_pipes *pa, *pb;
205 unsigned int proc;
206 unsigned int dst;
207 int fds[2];
209 /* Don't initiate anything if we are not really going to run. */
210 if (ps->ps_noaction)
211 return;
213 if (proc_id == PROC_GOTWEBD) {
214 privsep_process = PROC_GOTWEBD;
215 proc_setup(ps, procs, nproc);
217 /*
218 * Create the children sockets so we can use them
219 * to distribute the rest of the socketpair()s using
220 * proc_connect() later.
221 */
222 for (dst = 0; dst < PROC_MAX; dst++) {
223 /* Don't create socket for ourselves. */
224 if (dst == PROC_GOTWEBD)
225 continue;
227 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
228 pa = &ps->ps_pipes[PROC_GOTWEBD][0];
229 pb = &ps->ps_pipes[dst][proc];
230 if (socketpair(AF_UNIX,
231 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
232 PF_UNSPEC, fds) == -1)
233 fatal("%s: socketpair", __func__);
235 pa->pp_pipes[dst][proc] = fds[0];
236 pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
240 /* Engage! */
241 proc_exec(ps, procs, nproc, argc, argv);
242 return;
245 /* Initialize a child */
246 for (proc = 0; proc < nproc; proc++) {
247 if (procs[proc].p_id != proc_id)
248 continue;
249 p = &procs[proc];
250 break;
252 if (p == NULL || p->p_init == NULL)
253 fatalx("%s: process %d missing process initialization",
254 __func__, proc_id);
256 p->p_init(ps, p);
258 fatalx("failed to initiate child process");
261 void
262 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
263 unsigned int n)
265 struct privsep_pipes *pp = ps->ps_pp;
266 struct imsgev *iev;
268 if (ps->ps_ievs[dst] == NULL) {
269 #if DEBUG > 1
270 log_debug("%s: %s src %d %d to dst %d %d not connected",
271 __func__, ps->ps_title[privsep_process],
272 privsep_process, ps->ps_instance + 1,
273 dst, n + 1);
274 #endif
275 close(fd);
276 return;
279 if (pp->pp_pipes[dst][n] != -1) {
280 log_warnx("%s: duplicated descriptor", __func__);
281 close(fd);
282 return;
283 } else
284 pp->pp_pipes[dst][n] = fd;
286 iev = &ps->ps_ievs[dst][n];
287 imsg_init(&iev->ibuf, fd);
288 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
289 event_add(&iev->ev, NULL);
292 void
293 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
295 unsigned int i, j, src, dst, id;
296 struct privsep_pipes *pp;
298 /* Initialize parent title, ps_instances and procs. */
299 ps->ps_title[PROC_GOTWEBD] = "parent";
301 for (src = 0; src < PROC_MAX; src++)
302 /* Default to 1 process instance */
303 if (ps->ps_instances[src] < 1)
304 ps->ps_instances[src] = 1;
306 for (src = 0; src < nproc; src++) {
307 procs[src].p_ps = ps;
308 if (procs[src].p_cb == NULL)
309 procs[src].p_cb = proc_dispatch_null;
311 id = procs[src].p_id;
312 ps->ps_title[id] = procs[src].p_title;
313 ps->ps_ievs[id] = calloc(ps->ps_instances[id],
314 sizeof(struct imsgev));
315 if (ps->ps_ievs[id] == NULL)
316 fatal("%s: calloc", __func__);
318 /* With this set up, we are ready to call imsg_init(). */
319 for (i = 0; i < ps->ps_instances[id]; i++) {
320 ps->ps_ievs[id][i].handler = proc_dispatch;
321 ps->ps_ievs[id][i].events = EV_READ;
322 ps->ps_ievs[id][i].proc = &procs[src];
323 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
327 /*
328 * Allocate pipes for all process instances (incl. parent)
330 * - ps->ps_pipes: N:M mapping
331 * N source processes connected to M destination processes:
332 * [src][instances][dst][instances], for example
333 * [PROC_RELAY][3][PROC_CA][3]
335 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
336 * Each process instance has a destination array of socketpair fds:
337 * [dst][instances], for example
338 * [PROC_GOTWEBD][0]
339 */
340 for (src = 0; src < PROC_MAX; src++) {
341 /* Allocate destination array for each process */
342 ps->ps_pipes[src] = calloc(ps->ps_instances[src],
343 sizeof(struct privsep_pipes));
344 if (ps->ps_pipes[src] == NULL)
345 fatal("%s: calloc", __func__);
347 for (i = 0; i < ps->ps_instances[src]; i++) {
348 pp = &ps->ps_pipes[src][i];
350 for (dst = 0; dst < PROC_MAX; dst++) {
351 /* Allocate maximum fd integers */
352 pp->pp_pipes[dst] =
353 calloc(ps->ps_instances[dst],
354 sizeof(int));
355 if (pp->pp_pipes[dst] == NULL)
356 fatal("%s: calloc", __func__);
358 /* Mark fd as unused */
359 for (j = 0; j < ps->ps_instances[dst]; j++)
360 pp->pp_pipes[dst][j] = -1;
365 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
368 void
369 proc_kill(struct privsep *ps)
371 char *cause;
372 pid_t pid;
373 int len, status;
375 if (privsep_process != PROC_GOTWEBD)
376 return;
378 proc_close(ps);
380 do {
381 pid = waitpid(WAIT_ANY, &status, 0);
382 if (pid <= 0)
383 continue;
385 if (WIFSIGNALED(status)) {
386 len = asprintf(&cause, "terminated; signal %d",
387 WTERMSIG(status));
388 } else if (WIFEXITED(status)) {
389 if (WEXITSTATUS(status) != 0)
390 len = asprintf(&cause, "exited abnormally");
391 else
392 len = 0;
393 } else
394 len = -1;
396 if (len == 0) {
397 /* child exited OK, don't print a warning message */
398 } else if (len != -1) {
399 log_warnx("lost child: pid %u %s", pid, cause);
400 free(cause);
401 } else
402 log_warnx("lost child: pid %u", pid);
403 } while (pid != -1 || (pid == -1 && errno == EINTR));
406 void
407 proc_open(struct privsep *ps, int src, int dst)
409 struct privsep_pipes *pa, *pb;
410 struct privsep_fd pf;
411 int fds[2];
412 unsigned int i, j;
414 /* Exchange pipes between process. */
415 for (i = 0; i < ps->ps_instances[src]; i++) {
416 for (j = 0; j < ps->ps_instances[dst]; j++) {
417 /* Don't create sockets for ourself. */
418 if (src == dst && i == j)
419 continue;
421 pa = &ps->ps_pipes[src][i];
422 pb = &ps->ps_pipes[dst][j];
423 if (socketpair(AF_UNIX,
424 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
425 PF_UNSPEC, fds) == -1)
426 fatal("%s: socketpair", __func__);
428 pa->pp_pipes[dst][j] = fds[0];
429 pb->pp_pipes[src][i] = fds[1];
431 pf.pf_procid = src;
432 pf.pf_instance = i;
433 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
434 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
435 fatal("%s: proc_compose_imsg", __func__);
437 pf.pf_procid = dst;
438 pf.pf_instance = j;
439 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
440 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
441 fatal("%s: proc_compose_imsg", __func__);
443 /*
444 * We have to flush to send the descriptors and close
445 * them to avoid the fd ramp on startup.
446 */
447 if (proc_flush_imsg(ps, src, i) == -1 ||
448 proc_flush_imsg(ps, dst, j) == -1)
449 fatal("%s: imsg_flush", __func__);
454 void
455 proc_close(struct privsep *ps)
457 unsigned int dst, n;
458 struct privsep_pipes *pp;
460 if (ps == NULL)
461 return;
463 pp = ps->ps_pp;
465 for (dst = 0; dst < PROC_MAX; dst++) {
466 if (ps->ps_ievs[dst] == NULL)
467 continue;
469 for (n = 0; n < ps->ps_instances[dst]; n++) {
470 if (pp->pp_pipes[dst][n] == -1)
471 continue;
473 /* Cancel the fd, close and invalidate the fd */
474 event_del(&(ps->ps_ievs[dst][n].ev));
475 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
476 close(pp->pp_pipes[dst][n]);
477 pp->pp_pipes[dst][n] = -1;
479 free(ps->ps_ievs[dst]);
483 void
484 proc_shutdown(struct privsep_proc *p)
486 struct privsep *ps = p->p_ps;
488 if (p->p_shutdown != NULL)
489 (*p->p_shutdown)();
491 proc_close(ps);
493 log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
495 exit(0);
498 void
499 proc_sig_handler(int sig, short event, void *arg)
501 struct privsep_proc *p = arg;
503 switch (sig) {
504 case SIGINT:
505 case SIGTERM:
506 proc_shutdown(p);
507 break;
508 case SIGCHLD:
509 case SIGHUP:
510 case SIGPIPE:
511 case SIGUSR1:
512 /* ignore */
513 break;
514 default:
515 fatalx("proc_sig_handler: unexpected signal");
516 /* NOTREACHED */
520 void
521 proc_run(struct privsep *ps, struct privsep_proc *p,
522 struct privsep_proc *procs, unsigned int nproc,
523 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
525 struct passwd *pw;
526 const char *root;
528 log_procinit(p->p_title);
530 /* Set the process group of the current process */
531 setpgid(0, 0);
533 /* Use non-standard user */
534 if (p->p_pw != NULL)
535 pw = p->p_pw;
536 else
537 pw = ps->ps_pw;
539 /* Change root directory */
540 if (p->p_chroot != NULL)
541 root = p->p_chroot;
542 else
543 root = pw->pw_dir;
545 if (chroot(root) == -1)
546 fatal("proc_run: chroot");
547 if (chdir("/") == -1)
548 fatal("proc_run: chdir(\"/\")");
550 privsep_process = p->p_id;
552 setproctitle("%s", p->p_title);
554 if (setgroups(1, &pw->pw_gid) ||
555 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
556 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
557 fatal("proc_run: cannot drop privileges");
559 event_init();
561 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
562 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
563 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
564 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
565 signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
566 signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
568 signal_add(&ps->ps_evsigint, NULL);
569 signal_add(&ps->ps_evsigterm, NULL);
570 signal_add(&ps->ps_evsigchld, NULL);
571 signal_add(&ps->ps_evsighup, NULL);
572 signal_add(&ps->ps_evsigpipe, NULL);
573 signal_add(&ps->ps_evsigusr1, NULL);
575 proc_setup(ps, procs, nproc);
576 proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
578 DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
579 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
581 if (run != NULL)
582 run(ps, p, arg);
584 event_dispatch();
586 proc_shutdown(p);
589 void
590 proc_dispatch(int fd, short event, void *arg)
592 struct imsgev *iev = arg;
593 struct privsep_proc *p = iev->proc;
594 struct privsep *ps = p->p_ps;
595 struct imsgbuf *ibuf;
596 struct imsg imsg;
597 ssize_t n;
598 int verbose;
599 const char *title;
600 struct privsep_fd pf;
602 title = ps->ps_title[privsep_process];
603 ibuf = &iev->ibuf;
605 if (event & EV_READ) {
606 n = imsg_read(ibuf);
607 if (n == -1 && errno != EAGAIN)
608 fatal("%s: imsg_read", __func__);
609 if (n == 0) {
610 /* this pipe is dead, so remove the event handler */
611 event_del(&iev->ev);
612 event_loopexit(NULL);
613 return;
617 if (event & EV_WRITE) {
618 n = msgbuf_write(&ibuf->w);
619 if (n == -1 && errno != EAGAIN)
620 fatal("%s: msgbuf_write", __func__);
621 if (n == 0) {
622 /* this pipe is dead, so remove the event handler */
623 event_del(&iev->ev);
624 event_loopexit(NULL);
625 return;
629 for (;;) {
630 n = imsg_get(ibuf, &imsg);
631 if (n == -1)
632 fatal("%s: imsg_get", __func__);
633 if (n == 0)
634 break;
636 #if DEBUG > 1
637 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
638 __func__, title, ps->ps_instance + 1,
639 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
640 #endif
642 /*
643 * Check the message with the program callback
644 */
645 if ((p->p_cb)(fd, p, &imsg) == 0) {
646 /* Message was handled by the callback, continue */
647 imsg_free(&imsg);
648 continue;
651 /*
652 * Generic message handling
653 */
654 switch (imsg.hdr.type) {
655 case IMSG_CTL_VERBOSE:
656 log_info("%s", __func__);
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 log_warnx("%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);
674 imsg_free(&imsg);
676 imsg_event_add(iev);
679 int
680 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
682 return (-1);
685 /*
686 * imsg helper functions
687 */
689 void
690 imsg_event_add(struct imsgev *iev)
692 if (iev->handler == NULL) {
693 imsg_flush(&iev->ibuf);
694 return;
697 iev->events = EV_READ;
698 if (iev->ibuf.w.queued)
699 iev->events |= EV_WRITE;
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);
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)
710 int ret;
712 ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
713 if (ret == -1)
714 return (ret);
715 imsg_event_add(iev);
716 return (ret);
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)
723 int ret;
725 ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
726 if (ret == -1)
727 return (ret);
728 imsg_event_add(iev);
729 return (ret);
732 void
733 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
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;
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)
749 int m;
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);
758 return (0);
761 int
762 proc_compose(struct privsep *ps, enum privsep_procid id,
763 uint16_t type, void *data, uint16_t datalen)
765 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
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)
772 int m;
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);
780 return (0);
783 int
784 proc_composev(struct privsep *ps, enum privsep_procid id,
785 uint16_t type, const struct iovec *iov, int iovcnt)
787 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
790 int
791 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
792 enum privsep_procid id, int n)
794 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
795 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
798 struct imsgbuf *
799 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
801 int m;
803 proc_range(ps, id, &n, &m);
804 return (&ps->ps_ievs[id][n].ibuf);
807 struct imsgev *
808 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
810 int m;
812 proc_range(ps, id, &n, &m);
813 return (&ps->ps_ievs[id][n]);
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)
820 struct imsgbuf *ibuf;
821 int m, ret = 0;
823 proc_range(ps, id, &n, &m);
824 for (; n < m; n++) {
825 ibuf = proc_ibuf(ps, id, n);
826 if (ibuf == NULL)
827 return (-1);
828 do {
829 ret = imsg_flush(ibuf);
830 } while (ret == -1 && errno == EAGAIN);
831 if (ret == -1)
832 break;
833 imsg_event_add(&ps->ps_ievs[id][n]);
836 return (ret);