Blob


1 /* $OpenBSD: proc.c,v 1.41 2021/12/04 06:52:58 florian Exp $ */
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 */
20 #include "gmid.h"
22 #include <sys/types.h>
23 /* #include <sys/queue.h> XXX provided by gmid.h */
24 /* #include <sys/tree.h> XXX provided by gmid.h */
25 #include <sys/socket.h>
26 #include <sys/wait.h>
28 #include <fcntl.h>
29 #include <grp.h>
30 #include <limits.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <signal.h>
37 #include <paths.h>
38 #include <pwd.h>
39 /* #include <event.h> XXX provided by gmid.h */
40 /* #include <imsg.h> XXX provided by gmid.h */
42 #include "log.h"
43 #include "proc.h"
45 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int,
46 int, char **);
47 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
48 void proc_open(struct privsep *, int, int);
49 void proc_accept(struct privsep *, int, enum privsep_procid,
50 unsigned int);
51 void proc_close(struct privsep *);
52 int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid);
53 void proc_shutdown(struct privsep_proc *);
54 void proc_sig_handler(int, short, void *);
55 void proc_range(struct privsep *, enum privsep_procid, int *, int *);
56 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
58 int
59 proc_ispeer(struct privsep_proc *procs, unsigned int nproc,
60 enum privsep_procid type)
61 {
62 unsigned int i;
64 for (i = 0; i < nproc; i++)
65 if (procs[i].p_id == type)
66 return (1);
67 return (0);
68 }
70 enum privsep_procid
71 proc_getid(struct privsep_proc *procs, unsigned int nproc,
72 const char *proc_name)
73 {
74 struct privsep_proc *p;
75 unsigned int proc;
77 for (proc = 0; proc < nproc; proc++) {
78 p = &procs[proc];
79 if (strcmp(p->p_title, proc_name))
80 continue;
82 return (p->p_id);
83 }
85 return (PROC_MAX);
86 }
88 void
89 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
90 int debug, int argc, char **argv)
91 {
92 unsigned int proc, nargc, i, proc_i;
93 const char **nargv;
94 struct privsep_proc *p;
95 char num[32];
96 int fd;
98 /* Prepare the new process argv. */
99 nargv = calloc(argc + 5, sizeof(char *));
100 if (nargv == NULL)
101 fatal("%s: calloc", __func__);
103 /* Copy call argument first. */
104 nargc = 0;
105 nargv[nargc++] = argv[0];
107 /* Set process name argument and save the position. */
108 nargv[nargc++] = "-T";
109 proc_i = nargc;
110 nargc++;
112 /* Point process instance arg to stack and copy the original args. */
113 nargv[nargc++] = "-I";
114 nargv[nargc++] = num;
115 for (i = 1; i < (unsigned int) argc; i++)
116 nargv[nargc++] = argv[i];
118 nargv[nargc] = NULL;
120 for (proc = 0; proc < nproc; proc++) {
121 p = &procs[proc];
123 /* Update args with process title. */
124 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
126 /* Fire children processes. */
127 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
128 /* Update the process instance number. */
129 snprintf(num, sizeof(num), "%u", i);
131 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0];
132 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0] = -1;
134 switch (fork()) {
135 case -1:
136 fatal("%s: fork", __func__);
137 break;
138 case 0:
139 /* First create a new session */
140 if (setsid() == -1)
141 fatal("setsid");
143 /* Prepare parent socket. */
144 if (fd != PROC_PARENT_SOCK_FILENO) {
145 if (dup2(fd, PROC_PARENT_SOCK_FILENO)
146 == -1)
147 fatal("dup2");
148 } else if (fcntl(fd, F_SETFD, 0) == -1)
149 fatal("fcntl");
151 /* Daemons detach from terminal. */
152 if (!debug && (fd =
153 open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
154 (void)dup2(fd, STDIN_FILENO);
155 (void)dup2(fd, STDOUT_FILENO);
156 (void)dup2(fd, STDERR_FILENO);
157 if (fd > 2)
158 (void)close(fd);
161 /* obnoxious casts */
162 execvp(argv[0], (char *const *)nargv);
163 fatal("%s: execvp", __func__);
164 break;
165 default:
166 /* Close child end. */
167 close(fd);
168 break;
172 free(nargv);
175 void
176 proc_connect(struct privsep *ps)
178 struct imsgev *iev;
179 unsigned int src, dst, inst;
181 /* Don't distribute any sockets if we are not really going to run. */
182 if (ps->ps_noaction)
183 return;
185 for (dst = 0; dst < PROC_MAX; dst++) {
186 /* We don't communicate with ourselves. */
187 if (dst == PROC_PARENT)
188 continue;
190 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
191 iev = &ps->ps_ievs[dst][inst];
192 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
193 event_set(&iev->ev, iev->ibuf.fd, iev->events,
194 iev->handler, iev->data);
195 event_add(&iev->ev, NULL);
199 /* Distribute the socketpair()s for everyone. */
200 for (src = 0; src < PROC_MAX; src++)
201 for (dst = src; dst < PROC_MAX; dst++) {
202 /* Parent already distributed its fds. */
203 if (src == PROC_PARENT || dst == PROC_PARENT)
204 continue;
206 proc_open(ps, src, dst);
210 void
211 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
212 int debug, int argc, char **argv, enum privsep_procid proc_id)
214 struct privsep_proc *p = NULL;
215 struct privsep_pipes *pa, *pb;
216 unsigned int proc;
217 unsigned int dst;
218 int fds[2];
220 /* Don't initiate anything if we are not really going to run. */
221 if (ps->ps_noaction)
222 return;
224 if (proc_id == PROC_PARENT) {
225 privsep_process = PROC_PARENT;
226 proc_setup(ps, procs, nproc);
228 /*
229 * Create the children sockets so we can use them
230 * to distribute the rest of the socketpair()s using
231 * proc_connect() later.
232 */
233 for (dst = 0; dst < PROC_MAX; dst++) {
234 /* Don't create socket for ourselves. */
235 if (dst == PROC_PARENT)
236 continue;
238 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
239 pa = &ps->ps_pipes[PROC_PARENT][0];
240 pb = &ps->ps_pipes[dst][proc];
241 if (socketpair(AF_UNIX,
242 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
243 PF_UNSPEC, fds) == -1)
244 fatal("%s: socketpair", __func__);
246 pa->pp_pipes[dst][proc] = fds[0];
247 pb->pp_pipes[PROC_PARENT][0] = fds[1];
251 /* Engage! */
252 proc_exec(ps, procs, nproc, debug, argc, argv);
253 return;
256 /* Initialize a child */
257 for (proc = 0; proc < nproc; proc++) {
258 if (procs[proc].p_id != proc_id)
259 continue;
260 p = &procs[proc];
261 break;
263 if (p == NULL || p->p_init == NULL)
264 fatalx("%s: process %d missing process initialization",
265 __func__, proc_id);
267 p->p_init(ps, p);
269 fatalx("failed to initiate child process");
272 void
273 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
274 unsigned int n)
276 struct privsep_pipes *pp = ps->ps_pp;
277 struct imsgev *iev;
279 if (ps->ps_ievs[dst] == NULL) {
280 #if DEBUG > 1
281 log_debug("%s: %s src %d %d to dst %d %d not connected",
282 __func__, ps->ps_title[privsep_process],
283 privsep_process, ps->ps_instance + 1,
284 dst, n + 1);
285 #endif
286 close(fd);
287 return;
290 if (pp->pp_pipes[dst][n] != -1) {
291 log_warnx("%s: duplicated descriptor", __func__);
292 close(fd);
293 return;
294 } else
295 pp->pp_pipes[dst][n] = fd;
297 iev = &ps->ps_ievs[dst][n];
298 imsg_init(&iev->ibuf, fd);
299 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
300 event_add(&iev->ev, NULL);
303 void
304 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
306 unsigned int i, j, src, dst, id;
307 struct privsep_pipes *pp;
309 /* Initialize parent title, ps_instances and procs. */
310 ps->ps_title[PROC_PARENT] = "parent";
312 for (src = 0; src < PROC_MAX; src++)
313 /* Default to 1 process instance */
314 if (ps->ps_instances[src] < 1)
315 ps->ps_instances[src] = 1;
317 for (src = 0; src < nproc; src++) {
318 procs[src].p_ps = ps;
319 if (procs[src].p_cb == NULL)
320 procs[src].p_cb = proc_dispatch_null;
322 id = procs[src].p_id;
323 ps->ps_title[id] = procs[src].p_title;
324 if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
325 sizeof(struct imsgev))) == NULL)
326 fatal("%s: calloc", __func__);
328 /* With this set up, we are ready to call imsg_init(). */
329 for (i = 0; i < ps->ps_instances[id]; i++) {
330 ps->ps_ievs[id][i].handler = proc_dispatch;
331 ps->ps_ievs[id][i].events = EV_READ;
332 ps->ps_ievs[id][i].proc = &procs[src];
333 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
337 /*
338 * Allocate pipes for all process instances (incl. parent)
340 * - ps->ps_pipes: N:M mapping
341 * N source processes connected to M destination processes:
342 * [src][instances][dst][instances], for example
343 * [PROC_RELAY][3][PROC_CA][3]
345 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
346 * Each process instance has a destination array of socketpair fds:
347 * [dst][instances], for example
348 * [PROC_PARENT][0]
349 */
350 for (src = 0; src < PROC_MAX; src++) {
351 /* Allocate destination array for each process */
352 if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
353 sizeof(struct privsep_pipes))) == NULL)
354 fatal("%s: calloc", __func__);
356 for (i = 0; i < ps->ps_instances[src]; i++) {
357 pp = &ps->ps_pipes[src][i];
359 for (dst = 0; dst < PROC_MAX; dst++) {
360 /* Allocate maximum fd integers */
361 if ((pp->pp_pipes[dst] =
362 calloc(ps->ps_instances[dst],
363 sizeof(int))) == NULL)
364 fatal("%s: calloc", __func__);
366 /* Mark fd as unused */
367 for (j = 0; j < ps->ps_instances[dst]; j++)
368 pp->pp_pipes[dst][j] = -1;
373 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
376 void
377 proc_kill(struct privsep *ps)
379 char *cause;
380 pid_t pid;
381 int len, status;
383 if (privsep_process != PROC_PARENT)
384 return;
386 proc_close(ps);
388 do {
389 pid = waitpid(WAIT_ANY, &status, 0);
390 if (pid <= 0)
391 continue;
393 if (WIFSIGNALED(status)) {
394 len = asprintf(&cause, "terminated; signal %d",
395 WTERMSIG(status));
396 } else if (WIFEXITED(status)) {
397 if (WEXITSTATUS(status) != 0)
398 len = asprintf(&cause, "exited abnormally");
399 else
400 len = 0;
401 } else
402 len = -1;
404 if (len == 0) {
405 /* child exited OK, don't print a warning message */
406 } else if (len != -1) {
407 log_warnx("lost child: pid %u %s", pid, cause);
408 free(cause);
409 } else
410 log_warnx("lost child: pid %u", pid);
411 } while (pid != -1 || errno == EINTR);
414 void
415 proc_open(struct privsep *ps, int src, int dst)
417 struct privsep_pipes *pa, *pb;
418 struct privsep_fd pf;
419 int fds[2];
420 unsigned int i, j;
422 /* Exchange pipes between process. */
423 for (i = 0; i < ps->ps_instances[src]; i++) {
424 for (j = 0; j < ps->ps_instances[dst]; j++) {
425 /* Don't create sockets for ourself. */
426 if (src == dst && i == j)
427 continue;
429 /* Servers don't talk to each other. */
430 if (src == PROC_SERVER && dst == PROC_SERVER)
431 continue;
433 pa = &ps->ps_pipes[src][i];
434 pb = &ps->ps_pipes[dst][j];
435 if (socketpair(AF_UNIX,
436 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
437 PF_UNSPEC, fds) == -1)
438 fatal("%s: socketpair", __func__);
440 pa->pp_pipes[dst][j] = fds[0];
441 pb->pp_pipes[src][i] = fds[1];
443 pf.pf_procid = src;
444 pf.pf_instance = i;
445 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
446 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
447 fatal("%s: proc_compose_imsg", __func__);
449 pf.pf_procid = dst;
450 pf.pf_instance = j;
451 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
452 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
453 fatal("%s: proc_compose_imsg", __func__);
455 /*
456 * We have to flush to send the descriptors and close
457 * them to avoid the fd ramp on startup.
458 */
459 if (proc_flush_imsg(ps, src, i) == -1 ||
460 proc_flush_imsg(ps, dst, j) == -1)
461 fatal("%s: imsg_flush", __func__);
466 void
467 proc_close(struct privsep *ps)
469 unsigned int dst, n;
470 struct privsep_pipes *pp;
472 if (ps == NULL)
473 return;
475 pp = ps->ps_pp;
477 for (dst = 0; dst < PROC_MAX; dst++) {
478 if (ps->ps_ievs[dst] == NULL)
479 continue;
481 for (n = 0; n < ps->ps_instances[dst]; n++) {
482 if (pp->pp_pipes[dst][n] == -1)
483 continue;
485 /* Cancel the fd, close and invalidate the fd */
486 event_del(&(ps->ps_ievs[dst][n].ev));
487 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
488 close(pp->pp_pipes[dst][n]);
489 pp->pp_pipes[dst][n] = -1;
491 free(ps->ps_ievs[dst]);
495 void
496 proc_shutdown(struct privsep_proc *p)
498 struct privsep *ps = p->p_ps;
500 if (p->p_shutdown != NULL)
501 (*p->p_shutdown)();
503 proc_close(ps);
505 log_info("%s exiting, pid %d", p->p_title, getpid());
507 exit(0);
510 void
511 proc_sig_handler(int sig, short event, void *arg)
513 struct privsep_proc *p = arg;
515 switch (sig) {
516 case SIGINT:
517 case SIGTERM:
518 proc_shutdown(p);
519 break;
520 case SIGCHLD:
521 case SIGHUP:
522 /* ignore */
523 break;
524 default:
525 fatalx("%s: unexpected signal", __func__);
526 /* NOTREACHED */
530 void
531 proc_run(struct privsep *ps, struct privsep_proc *p,
532 struct privsep_proc *procs, unsigned int nproc,
533 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
535 struct passwd *pw;
536 const char *root;
538 log_procinit(p->p_title);
540 if (ps->ps_pw == NULL)
541 goto init;
543 /* Set the process group of the current process */
544 setpgid(0, 0);
546 /* Use non-standard user */
547 if (p->p_pw != NULL)
548 pw = p->p_pw;
549 else
550 pw = ps->ps_pw;
552 /* Change root directory */
553 if (p->p_chroot != NULL)
554 root = p->p_chroot;
555 else
556 root = pw->pw_dir;
558 if (chroot(root) == -1)
559 fatal("%s: chroot", __func__);
560 if (chdir("/") == -1)
561 fatal("%s: chdir(\"/\")", __func__);
563 privsep_process = p->p_id;
565 setproctitle("%s", p->p_title);
567 if (setgroups(1, &pw->pw_gid) ||
568 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
569 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
570 fatal("%s: cannot drop privileges", __func__);
572 init:
573 event_init();
575 signal(SIGPIPE, SIG_IGN);
577 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
578 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
579 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
580 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
582 signal_add(&ps->ps_evsigint, NULL);
583 signal_add(&ps->ps_evsigterm, NULL);
584 signal_add(&ps->ps_evsigchld, NULL);
585 signal_add(&ps->ps_evsighup, NULL);
587 proc_setup(ps, procs, nproc);
588 proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
590 log_debug("%s: %s %d/%d, pid %d", __func__, p->p_title,
591 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
593 if (run != NULL)
594 run(ps, p, arg);
596 event_dispatch();
598 proc_shutdown(p);
601 void
602 proc_dispatch(int fd, short event, void *arg)
604 struct imsgev *iev = arg;
605 struct privsep_proc *p = iev->proc;
606 struct privsep *ps = p->p_ps;
607 struct imsgbuf *ibuf;
608 struct imsg imsg;
609 ssize_t n;
610 const char *title;
611 struct privsep_fd pf;
613 title = ps->ps_title[privsep_process];
614 ibuf = &iev->ibuf;
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;
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;
638 for (;;) {
639 if ((n = imsg_get(ibuf, &imsg)) == -1)
640 fatal("%s: imsg_get", __func__);
641 if (n == 0)
642 break;
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
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;
659 /*
660 * Generic message handling
661 */
662 switch (imsg.hdr.type) {
663 case IMSG_CTL_PROCFD:
664 IMSG_SIZE_CHECK(&imsg, &pf);
665 memcpy(&pf, imsg.data, sizeof(pf));
666 proc_accept(ps, imsg.fd, pf.pf_procid,
667 pf.pf_instance);
668 break;
669 default:
670 fatalx("%s: %s %d got invalid imsg %d peerid %d "
671 "from %s %d",
672 __func__, title, ps->ps_instance + 1,
673 imsg.hdr.type, imsg.hdr.peerid,
674 p->p_title, imsg.hdr.pid);
676 imsg_free(&imsg);
678 imsg_event_add(iev);
681 int
682 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
684 return (-1);
687 /*
688 * imsg helper functions
689 */
691 void
692 imsg_event_add(struct imsgev *iev)
694 if (iev->handler == NULL) {
695 imsg_flush(&iev->ibuf);
696 return;
699 iev->events = EV_READ;
700 if (iev->ibuf.w.queued)
701 iev->events |= EV_WRITE;
703 event_del(&iev->ev);
704 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
705 event_add(&iev->ev, NULL);
708 int
709 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
710 pid_t pid, int fd, void *data, uint16_t datalen)
712 int ret;
714 if ((ret = imsg_compose(&iev->ibuf, type, peerid,
715 pid, fd, data, datalen)) == -1)
716 return (ret);
717 imsg_event_add(iev);
718 return (ret);
721 int
722 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
723 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
725 int ret;
727 if ((ret = imsg_composev(&iev->ibuf, type, peerid,
728 pid, fd, iov, iovcnt)) == -1)
729 return (ret);
730 imsg_event_add(iev);
731 return (ret);
734 void
735 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
737 if (*n == -1) {
738 /* Use a range of all target instances */
739 *n = 0;
740 *m = ps->ps_instances[id];
741 } else {
742 /* Use only a single slot of the specified peer process */
743 *m = *n + 1;
747 int
748 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
749 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
751 int m;
753 proc_range(ps, id, &n, &m);
754 for (; n < m; n++) {
755 if (imsg_compose_event(&ps->ps_ievs[id][n],
756 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
757 return (-1);
760 return (0);
763 int
764 proc_compose(struct privsep *ps, enum privsep_procid id,
765 uint16_t type, void *data, uint16_t datalen)
767 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
770 int
771 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
772 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
774 int m;
776 proc_range(ps, id, &n, &m);
777 for (; n < m; n++)
778 if (imsg_composev_event(&ps->ps_ievs[id][n],
779 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
780 return (-1);
782 return (0);
785 int
786 proc_composev(struct privsep *ps, enum privsep_procid id,
787 uint16_t type, const struct iovec *iov, int iovcnt)
789 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
792 int
793 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
794 enum privsep_procid id, int n)
796 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
797 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
800 struct imsgbuf *
801 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
803 int m;
805 proc_range(ps, id, &n, &m);
806 return (&ps->ps_ievs[id][n].ibuf);
809 struct imsgev *
810 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
812 int m;
814 proc_range(ps, id, &n, &m);
815 return (&ps->ps_ievs[id][n]);
818 /* This function should only be called with care as it breaks async I/O */
819 int
820 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
822 struct imsgbuf *ibuf;
823 int m, ret = 0;
825 proc_range(ps, id, &n, &m);
826 for (; n < m; n++) {
827 if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
828 return (-1);
830 do {
831 ret = imsg_flush(ibuf);
832 } while (ret == -1 && errno == EAGAIN);
833 if (ret == -1)
834 break;
835 imsg_event_add(&ps->ps_ievs[id][n]);
838 return (ret);