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 void proc_shutdown(struct privsep_proc *);
53 void proc_sig_handler(int, short, void *);
54 void proc_range(struct privsep *, enum privsep_procid, int *, int *);
55 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
57 enum privsep_procid
58 proc_getid(struct privsep_proc *procs, unsigned int nproc,
59 const char *proc_name)
60 {
61 struct privsep_proc *p;
62 unsigned int proc;
64 for (proc = 0; proc < nproc; proc++) {
65 p = &procs[proc];
66 if (strcmp(p->p_title, proc_name))
67 continue;
69 return (p->p_id);
70 }
72 return (PROC_MAX);
73 }
75 void
76 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
77 int debug, int argc, char **argv)
78 {
79 unsigned int proc, nargc, i, proc_i;
80 char **nargv;
81 struct privsep_proc *p;
82 char num[32];
83 int fd;
85 /* Prepare the new process argv. */
86 nargv = calloc(argc + 5, sizeof(char *));
87 if (nargv == NULL)
88 fatal("%s: calloc", __func__);
90 /* Copy call argument first. */
91 nargc = 0;
92 nargv[nargc++] = argv[0];
94 /* Set process name argument and save the position. */
95 nargv[nargc++] = "-P";
96 proc_i = nargc;
97 nargc++;
99 /* Point process instance arg to stack and copy the original args. */
100 nargv[nargc++] = "-I";
101 nargv[nargc++] = num;
102 for (i = 1; i < (unsigned int) argc; i++)
103 nargv[nargc++] = argv[i];
105 nargv[nargc] = NULL;
107 for (proc = 0; proc < nproc; proc++) {
108 p = &procs[proc];
110 /* Update args with process title. */
111 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
113 /* Fire children processes. */
114 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
115 /* Update the process instance number. */
116 snprintf(num, sizeof(num), "%u", i);
118 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0];
119 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0] = -1;
121 switch (fork()) {
122 case -1:
123 fatal("%s: fork", __func__);
124 break;
125 case 0:
126 /* First create a new session */
127 if (setsid() == -1)
128 fatal("setsid");
130 /* Prepare parent socket. */
131 if (fd != PROC_PARENT_SOCK_FILENO) {
132 if (dup2(fd, PROC_PARENT_SOCK_FILENO)
133 == -1)
134 fatal("dup2");
135 } else if (fcntl(fd, F_SETFD, 0) == -1)
136 fatal("fcntl");
138 /* Daemons detach from terminal. */
139 if (!debug && (fd =
140 open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
141 (void)dup2(fd, STDIN_FILENO);
142 (void)dup2(fd, STDOUT_FILENO);
143 (void)dup2(fd, STDERR_FILENO);
144 if (fd > 2)
145 (void)close(fd);
148 execvp(argv[0], nargv);
149 fatal("%s: execvp", __func__);
150 break;
151 default:
152 /* Close child end. */
153 close(fd);
154 break;
158 free(nargv);
161 void
162 proc_connect(struct privsep *ps)
164 struct imsgev *iev;
165 unsigned int src, dst, inst;
167 /* Don't distribute any sockets if we are not really going to run. */
168 if (ps->ps_noaction)
169 return;
171 for (dst = 0; dst < PROC_MAX; dst++) {
172 /* We don't communicate with ourselves. */
173 if (dst == PROC_PARENT)
174 continue;
176 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
177 iev = &ps->ps_ievs[dst][inst];
178 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
179 event_set(&iev->ev, iev->ibuf.fd, iev->events,
180 iev->handler, iev->data);
181 event_add(&iev->ev, NULL);
185 /* Distribute the socketpair()s for everyone. */
186 for (src = 0; src < PROC_MAX; src++)
187 for (dst = src; dst < PROC_MAX; dst++) {
188 /* Parent already distributed its fds. */
189 if (src == PROC_PARENT || dst == PROC_PARENT)
190 continue;
192 proc_open(ps, src, dst);
196 void
197 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
198 int debug, int argc, char **argv, enum privsep_procid proc_id)
200 struct privsep_proc *p = NULL;
201 struct privsep_pipes *pa, *pb;
202 unsigned int proc;
203 unsigned int dst;
204 int fds[2];
206 /* Don't initiate anything if we are not really going to run. */
207 if (ps->ps_noaction)
208 return;
210 if (proc_id == PROC_PARENT) {
211 privsep_process = PROC_PARENT;
212 proc_setup(ps, procs, nproc);
214 /*
215 * Create the children sockets so we can use them
216 * to distribute the rest of the socketpair()s using
217 * proc_connect() later.
218 */
219 for (dst = 0; dst < PROC_MAX; dst++) {
220 /* Don't create socket for ourselves. */
221 if (dst == PROC_PARENT)
222 continue;
224 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
225 pa = &ps->ps_pipes[PROC_PARENT][0];
226 pb = &ps->ps_pipes[dst][proc];
227 if (socketpair(AF_UNIX,
228 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
229 PF_UNSPEC, fds) == -1)
230 fatal("%s: socketpair", __func__);
232 pa->pp_pipes[dst][proc] = fds[0];
233 pb->pp_pipes[PROC_PARENT][0] = fds[1];
237 /* Engage! */
238 proc_exec(ps, procs, nproc, debug, argc, argv);
239 return;
242 /* Initialize a child */
243 for (proc = 0; proc < nproc; proc++) {
244 if (procs[proc].p_id != proc_id)
245 continue;
246 p = &procs[proc];
247 break;
249 if (p == NULL || p->p_init == NULL)
250 fatalx("%s: process %d missing process initialization",
251 __func__, proc_id);
253 p->p_init(ps, p);
255 fatalx("failed to initiate child process");
258 void
259 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
260 unsigned int n)
262 struct privsep_pipes *pp = ps->ps_pp;
263 struct imsgev *iev;
265 if (ps->ps_ievs[dst] == NULL) {
266 #if DEBUG > 1
267 log_debug("%s: %s src %d %d to dst %d %d not connected",
268 __func__, ps->ps_title[privsep_process],
269 privsep_process, ps->ps_instance + 1,
270 dst, n + 1);
271 #endif
272 close(fd);
273 return;
276 if (pp->pp_pipes[dst][n] != -1) {
277 log_warnx("%s: duplicated descriptor", __func__);
278 close(fd);
279 return;
280 } else
281 pp->pp_pipes[dst][n] = fd;
283 iev = &ps->ps_ievs[dst][n];
284 imsg_init(&iev->ibuf, fd);
285 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
286 event_add(&iev->ev, NULL);
289 void
290 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
292 unsigned int i, j, src, dst, id;
293 struct privsep_pipes *pp;
295 /* Initialize parent title, ps_instances and procs. */
296 ps->ps_title[PROC_PARENT] = "parent";
298 for (src = 0; src < PROC_MAX; src++)
299 /* Default to 1 process instance */
300 if (ps->ps_instances[src] < 1)
301 ps->ps_instances[src] = 1;
303 for (src = 0; src < nproc; src++) {
304 procs[src].p_ps = ps;
305 if (procs[src].p_cb == NULL)
306 procs[src].p_cb = proc_dispatch_null;
308 id = procs[src].p_id;
309 ps->ps_title[id] = procs[src].p_title;
310 if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
311 sizeof(struct imsgev))) == NULL)
312 fatal("%s: calloc", __func__);
314 /* With this set up, we are ready to call imsg_init(). */
315 for (i = 0; i < ps->ps_instances[id]; i++) {
316 ps->ps_ievs[id][i].handler = proc_dispatch;
317 ps->ps_ievs[id][i].events = EV_READ;
318 ps->ps_ievs[id][i].proc = &procs[src];
319 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
323 /*
324 * Allocate pipes for all process instances (incl. parent)
326 * - ps->ps_pipes: N:M mapping
327 * N source processes connected to M destination processes:
328 * [src][instances][dst][instances], for example
329 * [PROC_RELAY][3][PROC_CA][3]
331 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
332 * Each process instance has a destination array of socketpair fds:
333 * [dst][instances], for example
334 * [PROC_PARENT][0]
335 */
336 for (src = 0; src < PROC_MAX; src++) {
337 /* Allocate destination array for each process */
338 if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
339 sizeof(struct privsep_pipes))) == NULL)
340 fatal("%s: calloc", __func__);
342 for (i = 0; i < ps->ps_instances[src]; i++) {
343 pp = &ps->ps_pipes[src][i];
345 for (dst = 0; dst < PROC_MAX; dst++) {
346 /* Allocate maximum fd integers */
347 if ((pp->pp_pipes[dst] =
348 calloc(ps->ps_instances[dst],
349 sizeof(int))) == NULL)
350 fatal("%s: calloc", __func__);
352 /* Mark fd as unused */
353 for (j = 0; j < ps->ps_instances[dst]; j++)
354 pp->pp_pipes[dst][j] = -1;
359 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
362 void
363 proc_kill(struct privsep *ps)
365 char *cause;
366 pid_t pid;
367 int len, status;
369 if (privsep_process != PROC_PARENT)
370 return;
372 proc_close(ps);
374 do {
375 pid = waitpid(WAIT_ANY, &status, 0);
376 if (pid <= 0)
377 continue;
379 if (WIFSIGNALED(status)) {
380 len = asprintf(&cause, "terminated; signal %d",
381 WTERMSIG(status));
382 } else if (WIFEXITED(status)) {
383 if (WEXITSTATUS(status) != 0)
384 len = asprintf(&cause, "exited abnormally");
385 else
386 len = 0;
387 } else
388 len = -1;
390 if (len == 0) {
391 /* child exited OK, don't print a warning message */
392 } else if (len != -1) {
393 log_warnx("lost child: pid %u %s", pid, cause);
394 free(cause);
395 } else
396 log_warnx("lost child: pid %u", pid);
397 } while (pid != -1 || errno == EINTR);
400 void
401 proc_open(struct privsep *ps, int src, int dst)
403 struct privsep_pipes *pa, *pb;
404 struct privsep_fd pf;
405 int fds[2];
406 unsigned int i, j;
408 /* Exchange pipes between process. */
409 for (i = 0; i < ps->ps_instances[src]; i++) {
410 for (j = 0; j < ps->ps_instances[dst]; j++) {
411 /* Don't create sockets for ourself. */
412 if (src == dst && i == j)
413 continue;
415 /* Proxies don't talk to each other. */
416 if (src == PROC_PROXY && dst == PROC_PROXY)
417 continue;
419 pa = &ps->ps_pipes[src][i];
420 pb = &ps->ps_pipes[dst][j];
421 if (socketpair(AF_UNIX,
422 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
423 PF_UNSPEC, fds) == -1)
424 fatal("%s: socketpair", __func__);
426 pa->pp_pipes[dst][j] = fds[0];
427 pb->pp_pipes[src][i] = fds[1];
429 pf.pf_procid = src;
430 pf.pf_instance = i;
431 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
432 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
433 fatal("%s: proc_compose_imsg", __func__);
435 pf.pf_procid = dst;
436 pf.pf_instance = j;
437 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
438 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
439 fatal("%s: proc_compose_imsg", __func__);
441 /*
442 * We have to flush to send the descriptors and close
443 * them to avoid the fd ramp on startup.
444 */
445 if (proc_flush_imsg(ps, src, i) == -1 ||
446 proc_flush_imsg(ps, dst, j) == -1)
447 fatal("%s: imsg_flush", __func__);
452 void
453 proc_close(struct privsep *ps)
455 unsigned int dst, n;
456 struct privsep_pipes *pp;
458 if (ps == NULL)
459 return;
461 pp = ps->ps_pp;
463 for (dst = 0; dst < PROC_MAX; dst++) {
464 if (ps->ps_ievs[dst] == NULL)
465 continue;
467 for (n = 0; n < ps->ps_instances[dst]; n++) {
468 if (pp->pp_pipes[dst][n] == -1)
469 continue;
471 /* Cancel the fd, close and invalidate the fd */
472 event_del(&(ps->ps_ievs[dst][n].ev));
473 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
474 close(pp->pp_pipes[dst][n]);
475 pp->pp_pipes[dst][n] = -1;
477 free(ps->ps_ievs[dst]);
481 void
482 proc_shutdown(struct privsep_proc *p)
484 struct privsep *ps = p->p_ps;
486 if (p->p_shutdown != NULL)
487 (*p->p_shutdown)();
489 proc_close(ps);
491 log_info("%s exiting, pid %d", p->p_title, getpid());
493 exit(0);
496 void
497 proc_sig_handler(int sig, short event, void *arg)
499 struct privsep_proc *p = arg;
501 switch (sig) {
502 case SIGINT:
503 case SIGTERM:
504 proc_shutdown(p);
505 break;
506 case SIGCHLD:
507 case SIGHUP:
508 /* ignore */
509 break;
510 default:
511 fatalx("%s: unexpected signal", __func__);
512 /* NOTREACHED */
516 void
517 proc_run(struct privsep *ps, struct privsep_proc *p,
518 struct privsep_proc *procs, unsigned int nproc,
519 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
521 struct passwd *pw;
522 const char *root;
524 log_procinit(p->p_title);
526 /* Set the process group of the current process */
527 setpgid(0, 0);
529 /* Use non-standard user */
530 if (p->p_pw != NULL)
531 pw = p->p_pw;
532 else
533 pw = ps->ps_pw;
535 /* Change root directory */
536 if (p->p_chroot != NULL)
537 root = p->p_chroot;
538 else
539 root = pw->pw_dir;
541 if (chroot(root) == -1)
542 fatal("%s: chroot", __func__);
543 if (chdir("/") == -1)
544 fatal("%s: chdir(\"/\")", __func__);
546 privsep_process = p->p_id;
548 setproctitle("%s", p->p_title);
550 if (setgroups(1, &pw->pw_gid) ||
551 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
552 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
553 fatal("%s: cannot drop privileges", __func__);
555 event_init();
557 signal(SIGPIPE, SIG_IGN);
559 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
560 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
561 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
562 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
564 signal_add(&ps->ps_evsigint, NULL);
565 signal_add(&ps->ps_evsigterm, NULL);
566 signal_add(&ps->ps_evsigchld, NULL);
567 signal_add(&ps->ps_evsighup, NULL);
569 proc_setup(ps, procs, nproc);
570 proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
572 log_debug("%s: %s %d/%d, pid %d", __func__, p->p_title,
573 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
575 if (run != NULL)
576 run(ps, p, arg);
578 event_dispatch();
580 proc_shutdown(p);
583 void
584 proc_dispatch(int fd, short event, void *arg)
586 struct imsgev *iev = arg;
587 struct privsep_proc *p = iev->proc;
588 struct privsep *ps = p->p_ps;
589 struct imsgbuf *ibuf;
590 struct imsg imsg;
591 ssize_t n;
592 const char *title;
593 struct privsep_fd pf;
595 title = ps->ps_title[privsep_process];
596 ibuf = &iev->ibuf;
598 if (event & EV_READ) {
599 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
600 fatal("%s: imsg_read", __func__);
601 if (n == 0) {
602 /* this pipe is dead, so remove the event handler */
603 event_del(&iev->ev);
604 event_loopexit(NULL);
605 return;
609 if (event & EV_WRITE) {
610 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
611 fatal("%s: msgbuf_write", __func__);
612 if (n == 0) {
613 /* this pipe is dead, so remove the event handler */
614 event_del(&iev->ev);
615 event_loopexit(NULL);
616 return;
620 for (;;) {
621 if ((n = imsg_get(ibuf, &imsg)) == -1)
622 fatal("%s: imsg_get", __func__);
623 if (n == 0)
624 break;
626 #if DEBUG > 1
627 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
628 __func__, title, ps->ps_instance + 1,
629 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
630 #endif
632 /*
633 * Check the message with the program callback
634 */
635 if ((p->p_cb)(fd, p, &imsg) == 0) {
636 /* Message was handled by the callback, continue */
637 imsg_free(&imsg);
638 continue;
641 /*
642 * Generic message handling
643 */
644 switch (imsg.hdr.type) {
645 case IMSG_CTL_PROCFD:
646 IMSG_SIZE_CHECK(&imsg, &pf);
647 memcpy(&pf, imsg.data, sizeof(pf));
648 proc_accept(ps, imsg.fd, pf.pf_procid,
649 pf.pf_instance);
650 break;
651 default:
652 fatalx("%s: %s %d got invalid imsg %d peerid %d "
653 "from %s %d",
654 __func__, title, ps->ps_instance + 1,
655 imsg.hdr.type, imsg.hdr.peerid,
656 p->p_title, imsg.hdr.pid);
658 imsg_free(&imsg);
660 imsg_event_add(iev);
663 int
664 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
666 return (-1);
669 /*
670 * imsg helper functions
671 */
673 void
674 imsg_event_add(struct imsgev *iev)
676 if (iev->handler == NULL) {
677 imsg_flush(&iev->ibuf);
678 return;
681 iev->events = EV_READ;
682 if (iev->ibuf.w.queued)
683 iev->events |= EV_WRITE;
685 event_del(&iev->ev);
686 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
687 event_add(&iev->ev, NULL);
690 int
691 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
692 pid_t pid, int fd, void *data, uint16_t datalen)
694 int ret;
696 if ((ret = imsg_compose(&iev->ibuf, type, peerid,
697 pid, fd, data, datalen)) == -1)
698 return (ret);
699 imsg_event_add(iev);
700 return (ret);
703 int
704 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
705 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
707 int ret;
709 if ((ret = imsg_composev(&iev->ibuf, type, peerid,
710 pid, fd, iov, iovcnt)) == -1)
711 return (ret);
712 imsg_event_add(iev);
713 return (ret);
716 void
717 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
719 if (*n == -1) {
720 /* Use a range of all target instances */
721 *n = 0;
722 *m = ps->ps_instances[id];
723 } else {
724 /* Use only a single slot of the specified peer process */
725 *m = *n + 1;
729 int
730 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
731 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
733 int m;
735 proc_range(ps, id, &n, &m);
736 for (; n < m; n++) {
737 if (imsg_compose_event(&ps->ps_ievs[id][n],
738 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
739 return (-1);
742 return (0);
745 int
746 proc_compose(struct privsep *ps, enum privsep_procid id,
747 uint16_t type, void *data, uint16_t datalen)
749 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
752 int
753 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
754 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
756 int m;
758 proc_range(ps, id, &n, &m);
759 for (; n < m; n++)
760 if (imsg_composev_event(&ps->ps_ievs[id][n],
761 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
762 return (-1);
764 return (0);
767 int
768 proc_composev(struct privsep *ps, enum privsep_procid id,
769 uint16_t type, const struct iovec *iov, int iovcnt)
771 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
774 int
775 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
776 enum privsep_procid id, int n)
778 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
779 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
782 struct imsgbuf *
783 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
785 int m;
787 proc_range(ps, id, &n, &m);
788 return (&ps->ps_ievs[id][n].ibuf);
791 struct imsgev *
792 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
794 int m;
796 proc_range(ps, id, &n, &m);
797 return (&ps->ps_ievs[id][n]);
800 /* This function should only be called with care as it breaks async I/O */
801 int
802 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
804 struct imsgbuf *ibuf;
805 int m, ret = 0;
807 proc_range(ps, id, &n, &m);
808 for (; n < m; n++) {
809 if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
810 return (-1);
811 do {
812 ret = imsg_flush(ibuf);
813 } while (ret == -1 && errno == EAGAIN);
814 if (ret == -1)
815 break;
816 imsg_event_add(&ps->ps_ievs[id][n]);
819 return (ret);