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 <sys/types.h>
21 #include <sys/queue.h>
22 #include <sys/tree.h>
23 #include <sys/socket.h>
24 #include <sys/wait.h>
26 #include <fcntl.h>
27 #include <grp.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <signal.h>
35 #include <paths.h>
36 #include <pwd.h>
37 #include <event.h>
38 #include <imsg.h>
40 #include "log.h"
41 #include "proc.h"
43 #include "galileo.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 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++] = "-P";
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 execvp(argv[0], nargv);
162 fatal("%s: execvp", __func__);
163 break;
164 default:
165 /* Close child end. */
166 close(fd);
167 break;
171 free(nargv);
174 void
175 proc_connect(struct privsep *ps)
177 struct imsgev *iev;
178 unsigned int src, dst, inst;
180 /* Don't distribute any sockets if we are not really going to run. */
181 if (ps->ps_noaction)
182 return;
184 for (dst = 0; dst < PROC_MAX; dst++) {
185 /* We don't communicate with ourselves. */
186 if (dst == PROC_PARENT)
187 continue;
189 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
190 iev = &ps->ps_ievs[dst][inst];
191 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
192 event_set(&iev->ev, iev->ibuf.fd, iev->events,
193 iev->handler, iev->data);
194 event_add(&iev->ev, NULL);
198 /* Distribute the socketpair()s for everyone. */
199 for (src = 0; src < PROC_MAX; src++)
200 for (dst = src; dst < PROC_MAX; dst++) {
201 /* Parent already distributed its fds. */
202 if (src == PROC_PARENT || dst == PROC_PARENT)
203 continue;
205 proc_open(ps, src, dst);
209 void
210 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
211 int debug, int argc, char **argv, enum privsep_procid proc_id)
213 struct privsep_proc *p = NULL;
214 struct privsep_pipes *pa, *pb;
215 unsigned int proc;
216 unsigned int dst;
217 int fds[2];
219 /* Don't initiate anything if we are not really going to run. */
220 if (ps->ps_noaction)
221 return;
223 if (proc_id == PROC_PARENT) {
224 privsep_process = PROC_PARENT;
225 proc_setup(ps, procs, nproc);
227 /*
228 * Create the children sockets so we can use them
229 * to distribute the rest of the socketpair()s using
230 * proc_connect() later.
231 */
232 for (dst = 0; dst < PROC_MAX; dst++) {
233 /* Don't create socket for ourselves. */
234 if (dst == PROC_PARENT)
235 continue;
237 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
238 pa = &ps->ps_pipes[PROC_PARENT][0];
239 pb = &ps->ps_pipes[dst][proc];
240 if (socketpair(AF_UNIX,
241 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
242 PF_UNSPEC, fds) == -1)
243 fatal("%s: socketpair", __func__);
245 pa->pp_pipes[dst][proc] = fds[0];
246 pb->pp_pipes[PROC_PARENT][0] = fds[1];
250 /* Engage! */
251 proc_exec(ps, procs, nproc, debug, argc, argv);
252 return;
255 /* Initialize a child */
256 for (proc = 0; proc < nproc; proc++) {
257 if (procs[proc].p_id != proc_id)
258 continue;
259 p = &procs[proc];
260 break;
262 if (p == NULL || p->p_init == NULL)
263 fatalx("%s: process %d missing process initialization",
264 __func__, proc_id);
266 p->p_init(ps, p);
268 fatalx("failed to initiate child process");
271 void
272 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
273 unsigned int n)
275 struct privsep_pipes *pp = ps->ps_pp;
276 struct imsgev *iev;
278 if (ps->ps_ievs[dst] == NULL) {
279 #if DEBUG > 1
280 log_debug("%s: %s src %d %d to dst %d %d not connected",
281 __func__, ps->ps_title[privsep_process],
282 privsep_process, ps->ps_instance + 1,
283 dst, n + 1);
284 #endif
285 close(fd);
286 return;
289 if (pp->pp_pipes[dst][n] != -1) {
290 log_warnx("%s: duplicated descriptor", __func__);
291 close(fd);
292 return;
293 } else
294 pp->pp_pipes[dst][n] = fd;
296 iev = &ps->ps_ievs[dst][n];
297 imsg_init(&iev->ibuf, fd);
298 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
299 event_add(&iev->ev, NULL);
302 void
303 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
305 unsigned int i, j, src, dst, id;
306 struct privsep_pipes *pp;
308 /* Initialize parent title, ps_instances and procs. */
309 ps->ps_title[PROC_PARENT] = "parent";
311 for (src = 0; src < PROC_MAX; src++)
312 /* Default to 1 process instance */
313 if (ps->ps_instances[src] < 1)
314 ps->ps_instances[src] = 1;
316 for (src = 0; src < nproc; src++) {
317 procs[src].p_ps = ps;
318 if (procs[src].p_cb == NULL)
319 procs[src].p_cb = proc_dispatch_null;
321 id = procs[src].p_id;
322 ps->ps_title[id] = procs[src].p_title;
323 if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
324 sizeof(struct imsgev))) == NULL)
325 fatal("%s: calloc", __func__);
327 /* With this set up, we are ready to call imsg_init(). */
328 for (i = 0; i < ps->ps_instances[id]; i++) {
329 ps->ps_ievs[id][i].handler = proc_dispatch;
330 ps->ps_ievs[id][i].events = EV_READ;
331 ps->ps_ievs[id][i].proc = &procs[src];
332 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
336 /*
337 * Allocate pipes for all process instances (incl. parent)
339 * - ps->ps_pipes: N:M mapping
340 * N source processes connected to M destination processes:
341 * [src][instances][dst][instances], for example
342 * [PROC_RELAY][3][PROC_CA][3]
344 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
345 * Each process instance has a destination array of socketpair fds:
346 * [dst][instances], for example
347 * [PROC_PARENT][0]
348 */
349 for (src = 0; src < PROC_MAX; src++) {
350 /* Allocate destination array for each process */
351 if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
352 sizeof(struct privsep_pipes))) == NULL)
353 fatal("%s: calloc", __func__);
355 for (i = 0; i < ps->ps_instances[src]; i++) {
356 pp = &ps->ps_pipes[src][i];
358 for (dst = 0; dst < PROC_MAX; dst++) {
359 /* Allocate maximum fd integers */
360 if ((pp->pp_pipes[dst] =
361 calloc(ps->ps_instances[dst],
362 sizeof(int))) == NULL)
363 fatal("%s: calloc", __func__);
365 /* Mark fd as unused */
366 for (j = 0; j < ps->ps_instances[dst]; j++)
367 pp->pp_pipes[dst][j] = -1;
372 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
375 void
376 proc_kill(struct privsep *ps)
378 char *cause;
379 pid_t pid;
380 int len, status;
382 if (privsep_process != PROC_PARENT)
383 return;
385 proc_close(ps);
387 do {
388 pid = waitpid(WAIT_ANY, &status, 0);
389 if (pid <= 0)
390 continue;
392 if (WIFSIGNALED(status)) {
393 len = asprintf(&cause, "terminated; signal %d",
394 WTERMSIG(status));
395 } else if (WIFEXITED(status)) {
396 if (WEXITSTATUS(status) != 0)
397 len = asprintf(&cause, "exited abnormally");
398 else
399 len = 0;
400 } else
401 len = -1;
403 if (len == 0) {
404 /* child exited OK, don't print a warning message */
405 } else if (len != -1) {
406 log_warnx("lost child: pid %u %s", pid, cause);
407 free(cause);
408 } else
409 log_warnx("lost child: pid %u", pid);
410 } while (pid != -1 || errno == EINTR);
413 void
414 proc_open(struct privsep *ps, int src, int dst)
416 struct privsep_pipes *pa, *pb;
417 struct privsep_fd pf;
418 int fds[2];
419 unsigned int i, j;
421 /* Exchange pipes between process. */
422 for (i = 0; i < ps->ps_instances[src]; i++) {
423 for (j = 0; j < ps->ps_instances[dst]; j++) {
424 /* Don't create sockets for ourself. */
425 if (src == dst && i == j)
426 continue;
428 /* Proxies don't talk to each other. */
429 if (src == PROC_PROXY && dst == PROC_PROXY)
430 continue;
432 pa = &ps->ps_pipes[src][i];
433 pb = &ps->ps_pipes[dst][j];
434 if (socketpair(AF_UNIX,
435 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
436 PF_UNSPEC, fds) == -1)
437 fatal("%s: socketpair", __func__);
439 pa->pp_pipes[dst][j] = fds[0];
440 pb->pp_pipes[src][i] = fds[1];
442 pf.pf_procid = src;
443 pf.pf_instance = i;
444 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
445 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
446 fatal("%s: proc_compose_imsg", __func__);
448 pf.pf_procid = dst;
449 pf.pf_instance = j;
450 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
451 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
452 fatal("%s: proc_compose_imsg", __func__);
454 /*
455 * We have to flush to send the descriptors and close
456 * them to avoid the fd ramp on startup.
457 */
458 if (proc_flush_imsg(ps, src, i) == -1 ||
459 proc_flush_imsg(ps, dst, j) == -1)
460 fatal("%s: imsg_flush", __func__);
465 void
466 proc_close(struct privsep *ps)
468 unsigned int dst, n;
469 struct privsep_pipes *pp;
471 if (ps == NULL)
472 return;
474 pp = ps->ps_pp;
476 for (dst = 0; dst < PROC_MAX; dst++) {
477 if (ps->ps_ievs[dst] == NULL)
478 continue;
480 for (n = 0; n < ps->ps_instances[dst]; n++) {
481 if (pp->pp_pipes[dst][n] == -1)
482 continue;
484 /* Cancel the fd, close and invalidate the fd */
485 event_del(&(ps->ps_ievs[dst][n].ev));
486 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
487 close(pp->pp_pipes[dst][n]);
488 pp->pp_pipes[dst][n] = -1;
490 free(ps->ps_ievs[dst]);
494 void
495 proc_shutdown(struct privsep_proc *p)
497 struct privsep *ps = p->p_ps;
499 if (p->p_shutdown != NULL)
500 (*p->p_shutdown)();
502 proc_close(ps);
504 log_info("%s exiting, pid %d", p->p_title, getpid());
506 exit(0);
509 void
510 proc_sig_handler(int sig, short event, void *arg)
512 struct privsep_proc *p = arg;
514 switch (sig) {
515 case SIGINT:
516 case SIGTERM:
517 proc_shutdown(p);
518 break;
519 case SIGCHLD:
520 case SIGHUP:
521 /* ignore */
522 break;
523 default:
524 fatalx("%s: unexpected signal", __func__);
525 /* NOTREACHED */
529 void
530 proc_run(struct privsep *ps, struct privsep_proc *p,
531 struct privsep_proc *procs, unsigned int nproc,
532 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
534 struct passwd *pw;
535 const char *root;
537 log_procinit(p->p_title);
539 /* Set the process group of the current process */
540 setpgid(0, 0);
542 /* Use non-standard user */
543 if (p->p_pw != NULL)
544 pw = p->p_pw;
545 else
546 pw = ps->ps_pw;
548 /* Change root directory */
549 if (p->p_chroot != NULL)
550 root = p->p_chroot;
551 else
552 root = pw->pw_dir;
554 if (chroot(root) == -1)
555 fatal("%s: chroot", __func__);
556 if (chdir("/") == -1)
557 fatal("%s: chdir(\"/\")", __func__);
559 privsep_process = p->p_id;
561 setproctitle("%s", p->p_title);
563 if (setgroups(1, &pw->pw_gid) ||
564 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
565 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
566 fatal("%s: cannot drop privileges", __func__);
568 event_init();
570 signal(SIGPIPE, SIG_IGN);
572 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
573 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
574 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
575 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
577 signal_add(&ps->ps_evsigint, NULL);
578 signal_add(&ps->ps_evsigterm, NULL);
579 signal_add(&ps->ps_evsigchld, NULL);
580 signal_add(&ps->ps_evsighup, NULL);
582 proc_setup(ps, procs, nproc);
583 proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
585 log_debug("%s: %s %d/%d, pid %d", __func__, p->p_title,
586 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
588 if (run != NULL)
589 run(ps, p, arg);
591 event_dispatch();
593 proc_shutdown(p);
596 void
597 proc_dispatch(int fd, short event, void *arg)
599 struct imsgev *iev = arg;
600 struct privsep_proc *p = iev->proc;
601 struct privsep *ps = p->p_ps;
602 struct imsgbuf *ibuf;
603 struct imsg imsg;
604 ssize_t n;
605 const char *title;
606 struct privsep_fd pf;
608 title = ps->ps_title[privsep_process];
609 ibuf = &iev->ibuf;
611 if (event & EV_READ) {
612 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
613 fatal("%s: imsg_read", __func__);
614 if (n == 0) {
615 /* this pipe is dead, so remove the event handler */
616 event_del(&iev->ev);
617 event_loopexit(NULL);
618 return;
622 if (event & EV_WRITE) {
623 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
624 fatal("%s: msgbuf_write", __func__);
625 if (n == 0) {
626 /* this pipe is dead, so remove the event handler */
627 event_del(&iev->ev);
628 event_loopexit(NULL);
629 return;
633 for (;;) {
634 if ((n = imsg_get(ibuf, &imsg)) == -1)
635 fatal("%s: imsg_get", __func__);
636 if (n == 0)
637 break;
639 #if DEBUG > 1
640 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
641 __func__, title, ps->ps_instance + 1,
642 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
643 #endif
645 /*
646 * Check the message with the program callback
647 */
648 if ((p->p_cb)(fd, p, &imsg) == 0) {
649 /* Message was handled by the callback, continue */
650 imsg_free(&imsg);
651 continue;
654 /*
655 * Generic message handling
656 */
657 switch (imsg.hdr.type) {
658 case IMSG_CTL_PROCFD:
659 IMSG_SIZE_CHECK(&imsg, &pf);
660 memcpy(&pf, imsg.data, sizeof(pf));
661 proc_accept(ps, imsg.fd, pf.pf_procid,
662 pf.pf_instance);
663 break;
664 default:
665 fatalx("%s: %s %d got invalid imsg %d peerid %d "
666 "from %s %d",
667 __func__, title, ps->ps_instance + 1,
668 imsg.hdr.type, imsg.hdr.peerid,
669 p->p_title, imsg.hdr.pid);
671 imsg_free(&imsg);
673 imsg_event_add(iev);
676 int
677 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
679 return (-1);
682 /*
683 * imsg helper functions
684 */
686 void
687 imsg_event_add(struct imsgev *iev)
689 if (iev->handler == NULL) {
690 imsg_flush(&iev->ibuf);
691 return;
694 iev->events = EV_READ;
695 if (iev->ibuf.w.queued)
696 iev->events |= EV_WRITE;
698 event_del(&iev->ev);
699 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
700 event_add(&iev->ev, NULL);
703 int
704 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
705 pid_t pid, int fd, void *data, uint16_t datalen)
707 int ret;
709 if ((ret = imsg_compose(&iev->ibuf, type, peerid,
710 pid, fd, data, datalen)) == -1)
711 return (ret);
712 imsg_event_add(iev);
713 return (ret);
716 int
717 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
718 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
720 int ret;
722 if ((ret = imsg_composev(&iev->ibuf, type, peerid,
723 pid, fd, iov, iovcnt)) == -1)
724 return (ret);
725 imsg_event_add(iev);
726 return (ret);
729 void
730 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
732 if (*n == -1) {
733 /* Use a range of all target instances */
734 *n = 0;
735 *m = ps->ps_instances[id];
736 } else {
737 /* Use only a single slot of the specified peer process */
738 *m = *n + 1;
742 int
743 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
744 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
746 int m;
748 proc_range(ps, id, &n, &m);
749 for (; n < m; n++) {
750 if (imsg_compose_event(&ps->ps_ievs[id][n],
751 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
752 return (-1);
755 return (0);
758 int
759 proc_compose(struct privsep *ps, enum privsep_procid id,
760 uint16_t type, void *data, uint16_t datalen)
762 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
765 int
766 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
767 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
769 int m;
771 proc_range(ps, id, &n, &m);
772 for (; n < m; n++)
773 if (imsg_composev_event(&ps->ps_ievs[id][n],
774 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
775 return (-1);
777 return (0);
780 int
781 proc_composev(struct privsep *ps, enum privsep_procid id,
782 uint16_t type, const struct iovec *iov, int iovcnt)
784 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
787 int
788 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
789 enum privsep_procid id, int n)
791 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
792 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
795 struct imsgbuf *
796 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
798 int m;
800 proc_range(ps, id, &n, &m);
801 return (&ps->ps_ievs[id][n].ibuf);
804 struct imsgev *
805 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
807 int m;
809 proc_range(ps, id, &n, &m);
810 return (&ps->ps_ievs[id][n]);
813 /* This function should only be called with care as it breaks async I/O */
814 int
815 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
817 struct imsgbuf *ibuf;
818 int m, ret = 0;
820 proc_range(ps, id, &n, &m);
821 for (; n < m; n++) {
822 if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
823 return (-1);
824 do {
825 ret = imsg_flush(ibuf);
826 } while (ret == -1 && errno == EAGAIN);
827 if (ret == -1)
828 break;
829 imsg_event_add(&ps->ps_ievs[id][n]);
832 return (ret);