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 <limits.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <signal.h>
34 #include <paths.h>
35 #include <pwd.h>
36 #include <event.h>
37 #include <imsg.h>
39 #include "log.h"
40 #include "proc.h"
42 #include "galileo.h"
44 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int,
45 int, char **);
46 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
47 void proc_open(struct privsep *, int, int);
48 void proc_accept(struct privsep *, int, enum privsep_procid,
49 unsigned int);
50 void proc_close(struct privsep *);
51 int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid);
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 int
58 proc_ispeer(struct privsep_proc *procs, unsigned int nproc,
59 enum privsep_procid type)
60 {
61 unsigned int i;
63 for (i = 0; i < nproc; i++)
64 if (procs[i].p_id == type)
65 return (1);
66 return (0);
67 }
69 enum privsep_procid
70 proc_getid(struct privsep_proc *procs, unsigned int nproc,
71 const char *proc_name)
72 {
73 struct privsep_proc *p;
74 unsigned int proc;
76 for (proc = 0; proc < nproc; proc++) {
77 p = &procs[proc];
78 if (strcmp(p->p_title, proc_name))
79 continue;
81 return (p->p_id);
82 }
84 return (PROC_MAX);
85 }
87 void
88 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
89 int debug, int argc, char **argv)
90 {
91 unsigned int proc, nargc, i, proc_i;
92 char **nargv;
93 struct privsep_proc *p;
94 char num[32];
95 int fd;
97 /* Prepare the new process argv. */
98 nargv = calloc(argc + 5, sizeof(char *));
99 if (nargv == NULL)
100 fatal("%s: calloc", __func__);
102 /* Copy call argument first. */
103 nargc = 0;
104 nargv[nargc++] = argv[0];
106 /* Set process name argument and save the position. */
107 nargv[nargc++] = "-P";
108 proc_i = nargc;
109 nargc++;
111 /* Point process instance arg to stack and copy the original args. */
112 nargv[nargc++] = "-I";
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_PARENT][0];
131 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][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_PARENT_SOCK_FILENO) {
144 if (dup2(fd, PROC_PARENT_SOCK_FILENO)
145 == -1)
146 fatal("dup2");
147 } else if (fcntl(fd, F_SETFD, 0) == -1)
148 fatal("fcntl");
150 /* Daemons detach from terminal. */
151 if (!debug && (fd =
152 open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
153 (void)dup2(fd, STDIN_FILENO);
154 (void)dup2(fd, STDOUT_FILENO);
155 (void)dup2(fd, STDERR_FILENO);
156 if (fd > 2)
157 (void)close(fd);
160 execvp(argv[0], nargv);
161 fatal("%s: execvp", __func__);
162 break;
163 default:
164 /* Close child end. */
165 close(fd);
166 break;
170 free(nargv);
173 void
174 proc_connect(struct privsep *ps)
176 struct imsgev *iev;
177 unsigned int src, dst, inst;
179 /* Don't distribute any sockets if we are not really going to run. */
180 if (ps->ps_noaction)
181 return;
183 for (dst = 0; dst < PROC_MAX; dst++) {
184 /* We don't communicate with ourselves. */
185 if (dst == PROC_PARENT)
186 continue;
188 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
189 iev = &ps->ps_ievs[dst][inst];
190 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
191 event_set(&iev->ev, iev->ibuf.fd, iev->events,
192 iev->handler, iev->data);
193 event_add(&iev->ev, NULL);
197 /* Distribute the socketpair()s for everyone. */
198 for (src = 0; src < PROC_MAX; src++)
199 for (dst = src; dst < PROC_MAX; dst++) {
200 /* Parent already distributed its fds. */
201 if (src == PROC_PARENT || dst == PROC_PARENT)
202 continue;
204 proc_open(ps, src, dst);
208 void
209 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
210 int debug, int argc, char **argv, enum privsep_procid proc_id)
212 struct privsep_proc *p = NULL;
213 struct privsep_pipes *pa, *pb;
214 unsigned int proc;
215 unsigned int dst;
216 int fds[2];
218 /* Don't initiate anything if we are not really going to run. */
219 if (ps->ps_noaction)
220 return;
222 if (proc_id == PROC_PARENT) {
223 privsep_process = PROC_PARENT;
224 proc_setup(ps, procs, nproc);
226 /*
227 * Create the children sockets so we can use them
228 * to distribute the rest of the socketpair()s using
229 * proc_connect() later.
230 */
231 for (dst = 0; dst < PROC_MAX; dst++) {
232 /* Don't create socket for ourselves. */
233 if (dst == PROC_PARENT)
234 continue;
236 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
237 pa = &ps->ps_pipes[PROC_PARENT][0];
238 pb = &ps->ps_pipes[dst][proc];
239 if (socketpair(AF_UNIX,
240 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
241 PF_UNSPEC, fds) == -1)
242 fatal("%s: socketpair", __func__);
244 pa->pp_pipes[dst][proc] = fds[0];
245 pb->pp_pipes[PROC_PARENT][0] = fds[1];
249 /* Engage! */
250 proc_exec(ps, procs, nproc, debug, argc, argv);
251 return;
254 /* Initialize a child */
255 for (proc = 0; proc < nproc; proc++) {
256 if (procs[proc].p_id != proc_id)
257 continue;
258 p = &procs[proc];
259 break;
261 if (p == NULL || p->p_init == NULL)
262 fatalx("%s: process %d missing process initialization",
263 __func__, proc_id);
265 p->p_init(ps, p);
267 fatalx("failed to initiate child process");
270 void
271 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
272 unsigned int n)
274 struct privsep_pipes *pp = ps->ps_pp;
275 struct imsgev *iev;
277 if (ps->ps_ievs[dst] == NULL) {
278 #if DEBUG > 1
279 log_debug("%s: %s src %d %d to dst %d %d not connected",
280 __func__, ps->ps_title[privsep_process],
281 privsep_process, ps->ps_instance + 1,
282 dst, n + 1);
283 #endif
284 close(fd);
285 return;
288 if (pp->pp_pipes[dst][n] != -1) {
289 log_warnx("%s: duplicated descriptor", __func__);
290 close(fd);
291 return;
292 } else
293 pp->pp_pipes[dst][n] = fd;
295 iev = &ps->ps_ievs[dst][n];
296 imsg_init(&iev->ibuf, fd);
297 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
298 event_add(&iev->ev, NULL);
301 void
302 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
304 unsigned int i, j, src, dst, id;
305 struct privsep_pipes *pp;
307 /* Initialize parent title, ps_instances and procs. */
308 ps->ps_title[PROC_PARENT] = "parent";
310 for (src = 0; src < PROC_MAX; src++)
311 /* Default to 1 process instance */
312 if (ps->ps_instances[src] < 1)
313 ps->ps_instances[src] = 1;
315 for (src = 0; src < nproc; src++) {
316 procs[src].p_ps = ps;
317 if (procs[src].p_cb == NULL)
318 procs[src].p_cb = proc_dispatch_null;
320 id = procs[src].p_id;
321 ps->ps_title[id] = procs[src].p_title;
322 if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
323 sizeof(struct imsgev))) == NULL)
324 fatal("%s: calloc", __func__);
326 /* With this set up, we are ready to call imsg_init(). */
327 for (i = 0; i < ps->ps_instances[id]; i++) {
328 ps->ps_ievs[id][i].handler = proc_dispatch;
329 ps->ps_ievs[id][i].events = EV_READ;
330 ps->ps_ievs[id][i].proc = &procs[src];
331 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
335 /*
336 * Allocate pipes for all process instances (incl. parent)
338 * - ps->ps_pipes: N:M mapping
339 * N source processes connected to M destination processes:
340 * [src][instances][dst][instances], for example
341 * [PROC_RELAY][3][PROC_CA][3]
343 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
344 * Each process instance has a destination array of socketpair fds:
345 * [dst][instances], for example
346 * [PROC_PARENT][0]
347 */
348 for (src = 0; src < PROC_MAX; src++) {
349 /* Allocate destination array for each process */
350 if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
351 sizeof(struct privsep_pipes))) == NULL)
352 fatal("%s: calloc", __func__);
354 for (i = 0; i < ps->ps_instances[src]; i++) {
355 pp = &ps->ps_pipes[src][i];
357 for (dst = 0; dst < PROC_MAX; dst++) {
358 /* Allocate maximum fd integers */
359 if ((pp->pp_pipes[dst] =
360 calloc(ps->ps_instances[dst],
361 sizeof(int))) == NULL)
362 fatal("%s: calloc", __func__);
364 /* Mark fd as unused */
365 for (j = 0; j < ps->ps_instances[dst]; j++)
366 pp->pp_pipes[dst][j] = -1;
371 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
374 void
375 proc_kill(struct privsep *ps)
377 char *cause;
378 pid_t pid;
379 int len, status;
381 if (privsep_process != PROC_PARENT)
382 return;
384 proc_close(ps);
386 do {
387 pid = waitpid(WAIT_ANY, &status, 0);
388 if (pid <= 0)
389 continue;
391 if (WIFSIGNALED(status)) {
392 len = asprintf(&cause, "terminated; signal %d",
393 WTERMSIG(status));
394 } else if (WIFEXITED(status)) {
395 if (WEXITSTATUS(status) != 0)
396 len = asprintf(&cause, "exited abnormally");
397 else
398 len = 0;
399 } else
400 len = -1;
402 if (len == 0) {
403 /* child exited OK, don't print a warning message */
404 } else if (len != -1) {
405 log_warnx("lost child: pid %u %s", pid, cause);
406 free(cause);
407 } else
408 log_warnx("lost child: pid %u", pid);
409 } while (pid != -1 || errno == EINTR);
412 void
413 proc_open(struct privsep *ps, int src, int dst)
415 struct privsep_pipes *pa, *pb;
416 struct privsep_fd pf;
417 int fds[2];
418 unsigned int i, j;
420 /* Exchange pipes between process. */
421 for (i = 0; i < ps->ps_instances[src]; i++) {
422 for (j = 0; j < ps->ps_instances[dst]; j++) {
423 /* Don't create sockets for ourself. */
424 if (src == dst && i == j)
425 continue;
427 /* Proxies don't talk to each other. */
428 if (src == PROC_PROXY && dst == PROC_PROXY)
429 continue;
431 pa = &ps->ps_pipes[src][i];
432 pb = &ps->ps_pipes[dst][j];
433 if (socketpair(AF_UNIX,
434 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
435 PF_UNSPEC, fds) == -1)
436 fatal("%s: socketpair", __func__);
438 pa->pp_pipes[dst][j] = fds[0];
439 pb->pp_pipes[src][i] = fds[1];
441 pf.pf_procid = src;
442 pf.pf_instance = i;
443 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
444 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
445 fatal("%s: proc_compose_imsg", __func__);
447 pf.pf_procid = dst;
448 pf.pf_instance = j;
449 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
450 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
451 fatal("%s: proc_compose_imsg", __func__);
453 /*
454 * We have to flush to send the descriptors and close
455 * them to avoid the fd ramp on startup.
456 */
457 if (proc_flush_imsg(ps, src, i) == -1 ||
458 proc_flush_imsg(ps, dst, j) == -1)
459 fatal("%s: imsg_flush", __func__);
464 void
465 proc_close(struct privsep *ps)
467 unsigned int dst, n;
468 struct privsep_pipes *pp;
470 if (ps == NULL)
471 return;
473 pp = ps->ps_pp;
475 for (dst = 0; dst < PROC_MAX; dst++) {
476 if (ps->ps_ievs[dst] == NULL)
477 continue;
479 for (n = 0; n < ps->ps_instances[dst]; n++) {
480 if (pp->pp_pipes[dst][n] == -1)
481 continue;
483 /* Cancel the fd, close and invalidate the fd */
484 event_del(&(ps->ps_ievs[dst][n].ev));
485 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
486 close(pp->pp_pipes[dst][n]);
487 pp->pp_pipes[dst][n] = -1;
489 free(ps->ps_ievs[dst]);
493 void
494 proc_shutdown(struct privsep_proc *p)
496 struct privsep *ps = p->p_ps;
498 if (p->p_shutdown != NULL)
499 (*p->p_shutdown)();
501 proc_close(ps);
503 log_info("%s exiting, pid %d", p->p_title, getpid());
505 exit(0);
508 void
509 proc_sig_handler(int sig, short event, void *arg)
511 struct privsep_proc *p = arg;
513 switch (sig) {
514 case SIGINT:
515 case SIGTERM:
516 proc_shutdown(p);
517 break;
518 case SIGCHLD:
519 case SIGHUP:
520 /* ignore */
521 break;
522 default:
523 fatalx("%s: unexpected signal", __func__);
524 /* NOTREACHED */
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)
533 struct passwd *pw;
534 const char *root;
536 log_procinit(p->p_title);
538 /* Set the process group of the current process */
539 setpgid(0, 0);
541 /* Use non-standard user */
542 if (p->p_pw != NULL)
543 pw = p->p_pw;
544 else
545 pw = ps->ps_pw;
547 /* Change root directory */
548 if (p->p_chroot != NULL)
549 root = p->p_chroot;
550 else
551 root = pw->pw_dir;
553 if (chroot(root) == -1)
554 fatal("%s: chroot", __func__);
555 if (chdir("/") == -1)
556 fatal("%s: chdir(\"/\")", __func__);
558 privsep_process = p->p_id;
560 setproctitle("%s", p->p_title);
562 if (setgroups(1, &pw->pw_gid) ||
563 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
564 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
565 fatal("%s: cannot drop privileges", __func__);
567 event_init();
569 signal(SIGPIPE, SIG_IGN);
571 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
572 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
573 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
574 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
576 signal_add(&ps->ps_evsigint, NULL);
577 signal_add(&ps->ps_evsigterm, NULL);
578 signal_add(&ps->ps_evsigchld, NULL);
579 signal_add(&ps->ps_evsighup, NULL);
581 proc_setup(ps, procs, nproc);
582 proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
584 log_debug("%s: %s %d/%d, pid %d", __func__, p->p_title,
585 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
587 if (run != NULL)
588 run(ps, p, arg);
590 event_dispatch();
592 proc_shutdown(p);
595 void
596 proc_dispatch(int fd, short event, void *arg)
598 struct imsgev *iev = arg;
599 struct privsep_proc *p = iev->proc;
600 struct privsep *ps = p->p_ps;
601 struct imsgbuf *ibuf;
602 struct imsg imsg;
603 ssize_t n;
604 const char *title;
605 struct privsep_fd pf;
607 title = ps->ps_title[privsep_process];
608 ibuf = &iev->ibuf;
610 if (event & EV_READ) {
611 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
612 fatal("%s: imsg_read", __func__);
613 if (n == 0) {
614 /* this pipe is dead, so remove the event handler */
615 event_del(&iev->ev);
616 event_loopexit(NULL);
617 return;
621 if (event & EV_WRITE) {
622 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
623 fatal("%s: msgbuf_write", __func__);
624 if (n == 0) {
625 /* this pipe is dead, so remove the event handler */
626 event_del(&iev->ev);
627 event_loopexit(NULL);
628 return;
632 for (;;) {
633 if ((n = imsg_get(ibuf, &imsg)) == -1)
634 fatal("%s: imsg_get", __func__);
635 if (n == 0)
636 break;
638 #if DEBUG > 1
639 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
640 __func__, title, ps->ps_instance + 1,
641 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
642 #endif
644 /*
645 * Check the message with the program callback
646 */
647 if ((p->p_cb)(fd, p, &imsg) == 0) {
648 /* Message was handled by the callback, continue */
649 imsg_free(&imsg);
650 continue;
653 /*
654 * Generic message handling
655 */
656 switch (imsg.hdr.type) {
657 case IMSG_CTL_PROCFD:
658 IMSG_SIZE_CHECK(&imsg, &pf);
659 memcpy(&pf, imsg.data, sizeof(pf));
660 proc_accept(ps, imsg.fd, pf.pf_procid,
661 pf.pf_instance);
662 break;
663 default:
664 fatalx("%s: %s %d got invalid imsg %d peerid %d "
665 "from %s %d",
666 __func__, title, ps->ps_instance + 1,
667 imsg.hdr.type, imsg.hdr.peerid,
668 p->p_title, imsg.hdr.pid);
670 imsg_free(&imsg);
672 imsg_event_add(iev);
675 int
676 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
678 return (-1);
681 /*
682 * imsg helper functions
683 */
685 void
686 imsg_event_add(struct imsgev *iev)
688 if (iev->handler == NULL) {
689 imsg_flush(&iev->ibuf);
690 return;
693 iev->events = EV_READ;
694 if (iev->ibuf.w.queued)
695 iev->events |= EV_WRITE;
697 event_del(&iev->ev);
698 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
699 event_add(&iev->ev, NULL);
702 int
703 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
704 pid_t pid, int fd, void *data, uint16_t datalen)
706 int ret;
708 if ((ret = imsg_compose(&iev->ibuf, type, peerid,
709 pid, fd, data, datalen)) == -1)
710 return (ret);
711 imsg_event_add(iev);
712 return (ret);
715 int
716 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
717 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
719 int ret;
721 if ((ret = imsg_composev(&iev->ibuf, type, peerid,
722 pid, fd, iov, iovcnt)) == -1)
723 return (ret);
724 imsg_event_add(iev);
725 return (ret);
728 void
729 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
731 if (*n == -1) {
732 /* Use a range of all target instances */
733 *n = 0;
734 *m = ps->ps_instances[id];
735 } else {
736 /* Use only a single slot of the specified peer process */
737 *m = *n + 1;
741 int
742 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
743 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
745 int m;
747 proc_range(ps, id, &n, &m);
748 for (; n < m; n++) {
749 if (imsg_compose_event(&ps->ps_ievs[id][n],
750 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
751 return (-1);
754 return (0);
757 int
758 proc_compose(struct privsep *ps, enum privsep_procid id,
759 uint16_t type, void *data, uint16_t datalen)
761 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
764 int
765 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
766 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
768 int m;
770 proc_range(ps, id, &n, &m);
771 for (; n < m; n++)
772 if (imsg_composev_event(&ps->ps_ievs[id][n],
773 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
774 return (-1);
776 return (0);
779 int
780 proc_composev(struct privsep *ps, enum privsep_procid id,
781 uint16_t type, const struct iovec *iov, int iovcnt)
783 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
786 int
787 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
788 enum privsep_procid id, int n)
790 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
791 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
794 struct imsgbuf *
795 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
797 int m;
799 proc_range(ps, id, &n, &m);
800 return (&ps->ps_ievs[id][n].ibuf);
803 struct imsgev *
804 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
806 int m;
808 proc_range(ps, id, &n, &m);
809 return (&ps->ps_ievs[id][n]);
812 /* This function should only be called with care as it breaks async I/O */
813 int
814 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
816 struct imsgbuf *ibuf;
817 int m, ret = 0;
819 proc_range(ps, id, &n, &m);
820 for (; n < m; n++) {
821 if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
822 return (-1);
823 do {
824 ret = imsg_flush(ibuf);
825 } while (ret == -1 && errno == EAGAIN);
826 if (ret == -1)
827 break;
828 imsg_event_add(&ps->ps_ievs[id][n]);
831 return (ret);