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 void proc_shutdown(struct privsep_proc *);
44 void proc_sig_handler(int, short, void *);
45 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
47 enum privsep_procid privsep_process;
49 enum privsep_procid
50 proc_getid(struct privsep_proc *procs, unsigned int nproc,
51 const char *proc_name)
52 {
53 struct privsep_proc *p;
54 unsigned int proc;
56 for (proc = 0; proc < nproc; proc++) {
57 p = &procs[proc];
58 if (strcmp(p->p_title, proc_name))
59 continue;
61 return (p->p_id);
62 }
64 return (PROC_MAX);
65 }
67 void
68 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
69 int argc, char **argv)
70 {
71 unsigned int proc, nargc, i, proc_i;
72 char **nargv;
73 struct privsep_proc *p;
74 char num[32];
75 int fd;
77 /* Prepare the new process argv. */
78 nargv = calloc(argc + 5, sizeof(char *));
79 if (nargv == NULL)
80 fatal("%s: calloc", __func__);
82 /* Copy call argument first. */
83 nargc = 0;
84 nargv[nargc++] = argv[0];
86 /* Set process name argument and save the position. */
87 nargv[nargc] = strdup("-P");
88 if (nargv[nargc] == NULL)
89 fatal("%s: strdup", __func__);
90 nargc++;
91 proc_i = nargc;
92 nargc++;
94 /* Point process instance arg to stack and copy the original args. */
95 nargv[nargc] = strdup("-I");
96 if (nargv[nargc] == NULL)
97 fatal("%s: strdup", __func__);
98 nargc++;
99 nargv[nargc++] = num;
100 for (i = 1; i < (unsigned int) argc; i++)
101 nargv[nargc++] = argv[i];
103 nargv[nargc] = NULL;
105 for (proc = 0; proc < nproc; proc++) {
106 p = &procs[proc];
108 /* Update args with process title. */
109 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
111 /* Fire children processes. */
112 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
113 /* Update the process instance number. */
114 snprintf(num, sizeof(num), "%u", i);
116 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0];
117 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0] = -1;
119 switch (fork()) {
120 case -1:
121 fatal("%s: fork", __func__);
122 break;
123 case 0:
124 /* First create a new session */
125 if (setsid() == -1)
126 fatal("setsid");
128 /* Prepare parent socket. */
129 if (fd != PROC_GOTWEBD_SOCK_FILENO) {
130 if (dup2(fd, PROC_GOTWEBD_SOCK_FILENO)
131 == -1)
132 fatal("dup2");
133 } else if (fcntl(fd, F_SETFD, 0) == -1)
134 fatal("fcntl");
136 execvp(argv[0], nargv);
137 fatal("%s: execvp", __func__);
138 break;
139 default:
140 /* Close child end. */
141 close(fd);
142 break;
147 free(nargv);
150 void
151 proc_connect(struct privsep *ps)
153 struct imsgev *iev;
154 unsigned int src, dst, inst;
156 /* Don't distribute any sockets if we are not really going to run. */
157 if (ps->ps_noaction)
158 return;
160 for (dst = 0; dst < PROC_MAX; dst++) {
161 /* We don't communicate with ourselves. */
162 if (dst == PROC_GOTWEBD)
163 continue;
165 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
166 iev = &ps->ps_ievs[dst][inst];
167 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
168 event_set(&iev->ev, iev->ibuf.fd, iev->events,
169 iev->handler, iev->data);
170 event_add(&iev->ev, NULL);
174 /* Distribute the socketpair()s for everyone. */
175 for (src = 0; src < PROC_MAX; src++)
176 for (dst = src; dst < PROC_MAX; dst++) {
177 /* Parent already distributed its fds. */
178 if (src == PROC_GOTWEBD || dst == PROC_GOTWEBD)
179 continue;
181 proc_open(ps, src, dst);
185 void
186 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
187 int argc, char **argv, enum privsep_procid proc_id)
189 struct privsep_proc *p = NULL;
190 struct privsep_pipes *pa, *pb;
191 unsigned int proc;
192 unsigned int dst;
193 int fds[2];
195 /* Don't initiate anything if we are not really going to run. */
196 if (ps->ps_noaction)
197 return;
199 if (proc_id == PROC_GOTWEBD) {
200 privsep_process = PROC_GOTWEBD;
201 proc_setup(ps, procs, nproc);
203 /*
204 * Create the children sockets so we can use them
205 * to distribute the rest of the socketpair()s using
206 * proc_connect() later.
207 */
208 for (dst = 0; dst < PROC_MAX; dst++) {
209 /* Don't create socket for ourselves. */
210 if (dst == PROC_GOTWEBD)
211 continue;
213 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
214 pa = &ps->ps_pipes[PROC_GOTWEBD][0];
215 pb = &ps->ps_pipes[dst][proc];
216 if (socketpair(AF_UNIX,
217 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
218 PF_UNSPEC, fds) == -1)
219 fatal("%s: socketpair", __func__);
221 pa->pp_pipes[dst][proc] = fds[0];
222 pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
226 /* Engage! */
227 proc_exec(ps, procs, nproc, argc, argv);
228 return;
231 /* Initialize a child */
232 for (proc = 0; proc < nproc; proc++) {
233 if (procs[proc].p_id != proc_id)
234 continue;
235 p = &procs[proc];
236 break;
238 if (p == NULL || p->p_init == NULL)
239 fatalx("%s: process %d missing process initialization",
240 __func__, proc_id);
242 p->p_init(ps, p);
244 fatalx("failed to initiate child process");
247 void
248 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
249 unsigned int n)
251 struct privsep_pipes *pp = ps->ps_pp;
252 struct imsgev *iev;
254 if (ps->ps_ievs[dst] == NULL) {
255 #if DEBUG > 1
256 log_debug("%s: %s src %d %d to dst %d %d not connected",
257 __func__, ps->ps_title[privsep_process],
258 privsep_process, ps->ps_instance + 1,
259 dst, n + 1);
260 #endif
261 close(fd);
262 return;
265 if (pp->pp_pipes[dst][n] != -1) {
266 log_warnx("%s: duplicated descriptor", __func__);
267 close(fd);
268 return;
269 } else
270 pp->pp_pipes[dst][n] = fd;
272 iev = &ps->ps_ievs[dst][n];
273 imsg_init(&iev->ibuf, fd);
274 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
275 event_add(&iev->ev, NULL);
278 void
279 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
281 unsigned int i, j, src, dst, id;
282 struct privsep_pipes *pp;
284 /* Initialize parent title, ps_instances and procs. */
285 ps->ps_title[PROC_GOTWEBD] = "parent";
287 for (src = 0; src < PROC_MAX; src++)
288 /* Default to 1 process instance */
289 if (ps->ps_instances[src] < 1)
290 ps->ps_instances[src] = 1;
292 for (src = 0; src < nproc; src++) {
293 procs[src].p_ps = ps;
294 if (procs[src].p_cb == NULL)
295 procs[src].p_cb = proc_dispatch_null;
297 id = procs[src].p_id;
298 ps->ps_title[id] = procs[src].p_title;
299 ps->ps_ievs[id] = calloc(ps->ps_instances[id],
300 sizeof(struct imsgev));
301 if (ps->ps_ievs[id] == NULL)
302 fatal("%s: calloc", __func__);
304 /* With this set up, we are ready to call imsg_init(). */
305 for (i = 0; i < ps->ps_instances[id]; i++) {
306 ps->ps_ievs[id][i].handler = proc_dispatch;
307 ps->ps_ievs[id][i].events = EV_READ;
308 ps->ps_ievs[id][i].proc = &procs[src];
309 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
313 /*
314 * Allocate pipes for all process instances (incl. parent)
316 * - ps->ps_pipes: N:M mapping
317 * N source processes connected to M destination processes:
318 * [src][instances][dst][instances], for example
319 * [PROC_RELAY][3][PROC_CA][3]
321 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
322 * Each process instance has a destination array of socketpair fds:
323 * [dst][instances], for example
324 * [PROC_GOTWEBD][0]
325 */
326 for (src = 0; src < PROC_MAX; src++) {
327 /* Allocate destination array for each process */
328 ps->ps_pipes[src] = calloc(ps->ps_instances[src],
329 sizeof(struct privsep_pipes));
330 if (ps->ps_pipes[src] == NULL)
331 fatal("%s: calloc", __func__);
333 for (i = 0; i < ps->ps_instances[src]; i++) {
334 pp = &ps->ps_pipes[src][i];
336 for (dst = 0; dst < PROC_MAX; dst++) {
337 /* Allocate maximum fd integers */
338 pp->pp_pipes[dst] =
339 calloc(ps->ps_instances[dst],
340 sizeof(int));
341 if (pp->pp_pipes[dst] == NULL)
342 fatal("%s: calloc", __func__);
344 /* Mark fd as unused */
345 for (j = 0; j < ps->ps_instances[dst]; j++)
346 pp->pp_pipes[dst][j] = -1;
351 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
354 void
355 proc_kill(struct privsep *ps)
357 char *cause;
358 pid_t pid;
359 int len, status;
361 if (privsep_process != PROC_GOTWEBD)
362 return;
364 proc_close(ps);
366 do {
367 pid = waitpid(WAIT_ANY, &status, 0);
368 if (pid <= 0)
369 continue;
371 if (WIFSIGNALED(status)) {
372 len = asprintf(&cause, "terminated; signal %d",
373 WTERMSIG(status));
374 } else if (WIFEXITED(status)) {
375 if (WEXITSTATUS(status) != 0)
376 len = asprintf(&cause, "exited abnormally");
377 else
378 len = 0;
379 } else
380 len = -1;
382 if (len == 0) {
383 /* child exited OK, don't print a warning message */
384 } else if (len != -1) {
385 log_warnx("lost child: pid %u %s", pid, cause);
386 free(cause);
387 } else
388 log_warnx("lost child: pid %u", pid);
389 } while (pid != -1 || (pid == -1 && errno == EINTR));
392 void
393 proc_open(struct privsep *ps, int src, int dst)
395 struct privsep_pipes *pa, *pb;
396 struct privsep_fd pf;
397 int fds[2];
398 unsigned int i, j;
400 /* Exchange pipes between process. */
401 for (i = 0; i < ps->ps_instances[src]; i++) {
402 for (j = 0; j < ps->ps_instances[dst]; j++) {
403 /* Don't create sockets for ourself. */
404 if (src == dst && i == j)
405 continue;
407 pa = &ps->ps_pipes[src][i];
408 pb = &ps->ps_pipes[dst][j];
409 if (socketpair(AF_UNIX,
410 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
411 PF_UNSPEC, fds) == -1)
412 fatal("%s: socketpair", __func__);
414 pa->pp_pipes[dst][j] = fds[0];
415 pb->pp_pipes[src][i] = fds[1];
417 pf.pf_procid = src;
418 pf.pf_instance = i;
419 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
420 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
421 fatal("%s: proc_compose_imsg", __func__);
423 pf.pf_procid = dst;
424 pf.pf_instance = j;
425 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
426 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
427 fatal("%s: proc_compose_imsg", __func__);
429 /*
430 * We have to flush to send the descriptors and close
431 * them to avoid the fd ramp on startup.
432 */
433 if (proc_flush_imsg(ps, src, i) == -1 ||
434 proc_flush_imsg(ps, dst, j) == -1)
435 fatal("%s: imsg_flush", __func__);
440 void
441 proc_close(struct privsep *ps)
443 unsigned int dst, n;
444 struct privsep_pipes *pp;
446 if (ps == NULL)
447 return;
449 pp = ps->ps_pp;
451 for (dst = 0; dst < PROC_MAX; dst++) {
452 if (ps->ps_ievs[dst] == NULL)
453 continue;
455 for (n = 0; n < ps->ps_instances[dst]; n++) {
456 if (pp->pp_pipes[dst][n] == -1)
457 continue;
459 /* Cancel the fd, close and invalidate the fd */
460 event_del(&(ps->ps_ievs[dst][n].ev));
461 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
462 close(pp->pp_pipes[dst][n]);
463 pp->pp_pipes[dst][n] = -1;
465 free(ps->ps_ievs[dst]);
469 void
470 proc_shutdown(struct privsep_proc *p)
472 struct privsep *ps = p->p_ps;
474 if (p->p_shutdown != NULL)
475 (*p->p_shutdown)();
477 proc_close(ps);
479 log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
481 exit(0);
484 void
485 proc_sig_handler(int sig, short event, void *arg)
487 struct privsep_proc *p = arg;
489 switch (sig) {
490 case SIGINT:
491 case SIGTERM:
492 proc_shutdown(p);
493 break;
494 case SIGCHLD:
495 case SIGHUP:
496 case SIGPIPE:
497 case SIGUSR1:
498 /* ignore */
499 break;
500 default:
501 fatalx("proc_sig_handler: unexpected signal");
502 /* NOTREACHED */
506 void
507 proc_run(struct privsep *ps, struct privsep_proc *p,
508 struct privsep_proc *procs, unsigned int nproc,
509 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
511 struct passwd *pw;
512 const char *root;
514 log_procinit(p->p_title);
516 /* Set the process group of the current process */
517 setpgid(0, 0);
519 /* Use non-standard user */
520 if (p->p_pw != NULL)
521 pw = p->p_pw;
522 else
523 pw = ps->ps_pw;
525 /* Change root directory */
526 if (p->p_chroot != NULL)
527 root = p->p_chroot;
528 else
529 root = pw->pw_dir;
531 if (chroot(root) == -1)
532 fatal("proc_run: chroot");
533 if (chdir("/") == -1)
534 fatal("proc_run: chdir(\"/\")");
536 privsep_process = p->p_id;
538 setproctitle("%s", p->p_title);
540 if (setgroups(1, &pw->pw_gid) ||
541 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
542 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
543 fatal("proc_run: cannot drop privileges");
545 event_init();
547 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
548 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
549 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
550 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
551 signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
552 signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
554 signal_add(&ps->ps_evsigint, NULL);
555 signal_add(&ps->ps_evsigterm, NULL);
556 signal_add(&ps->ps_evsigchld, NULL);
557 signal_add(&ps->ps_evsighup, NULL);
558 signal_add(&ps->ps_evsigpipe, NULL);
559 signal_add(&ps->ps_evsigusr1, NULL);
561 proc_setup(ps, procs, nproc);
562 proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
564 DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
565 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
567 if (run != NULL)
568 run(ps, p, arg);
570 event_dispatch();
572 proc_shutdown(p);
575 void
576 proc_dispatch(int fd, short event, void *arg)
578 struct imsgev *iev = arg;
579 struct privsep_proc *p = iev->proc;
580 struct privsep *ps = p->p_ps;
581 struct imsgbuf *ibuf;
582 struct imsg imsg;
583 ssize_t n;
584 int verbose;
585 const char *title;
586 struct privsep_fd pf;
588 title = ps->ps_title[privsep_process];
589 ibuf = &iev->ibuf;
591 if (event & EV_READ) {
592 n = imsg_read(ibuf);
593 if (n == -1 && errno != EAGAIN)
594 fatal("%s: imsg_read", __func__);
595 if (n == 0) {
596 /* this pipe is dead, so remove the event handler */
597 event_del(&iev->ev);
598 event_loopexit(NULL);
599 return;
603 if (event & EV_WRITE) {
604 n = msgbuf_write(&ibuf->w);
605 if (n == -1 && errno != EAGAIN)
606 fatal("%s: msgbuf_write", __func__);
607 if (n == 0) {
608 /* this pipe is dead, so remove the event handler */
609 event_del(&iev->ev);
610 event_loopexit(NULL);
611 return;
615 for (;;) {
616 n = imsg_get(ibuf, &imsg);
617 if (n == -1)
618 fatal("%s: imsg_get", __func__);
619 if (n == 0)
620 break;
622 #if DEBUG > 1
623 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
624 __func__, title, ps->ps_instance + 1,
625 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
626 #endif
628 /*
629 * Check the message with the program callback
630 */
631 if ((p->p_cb)(fd, p, &imsg) == 0) {
632 /* Message was handled by the callback, continue */
633 imsg_free(&imsg);
634 continue;
637 /*
638 * Generic message handling
639 */
640 switch (imsg.hdr.type) {
641 case IMSG_CTL_VERBOSE:
642 log_info("%s", __func__);
643 IMSG_SIZE_CHECK(&imsg, &verbose);
644 memcpy(&verbose, imsg.data, sizeof(verbose));
645 log_setverbose(verbose);
646 break;
647 case IMSG_CTL_PROCFD:
648 IMSG_SIZE_CHECK(&imsg, &pf);
649 memcpy(&pf, imsg.data, sizeof(pf));
650 proc_accept(ps, imsg.fd, pf.pf_procid,
651 pf.pf_instance);
652 break;
653 default:
654 log_warnx("%s: %s %d got invalid imsg %d peerid %d "
655 "from %s %d",
656 __func__, title, ps->ps_instance + 1,
657 imsg.hdr.type, imsg.hdr.peerid,
658 p->p_title, imsg.hdr.pid);
660 imsg_free(&imsg);
662 imsg_event_add(iev);
665 int
666 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
668 return (-1);
671 /*
672 * imsg helper functions
673 */
675 void
676 imsg_event_add(struct imsgev *iev)
678 if (iev->handler == NULL) {
679 imsg_flush(&iev->ibuf);
680 return;
683 iev->events = EV_READ;
684 if (iev->ibuf.w.queued)
685 iev->events |= EV_WRITE;
687 event_del(&iev->ev);
688 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
689 event_add(&iev->ev, NULL);
692 int
693 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
694 pid_t pid, int fd, void *data, uint16_t datalen)
696 int ret;
698 ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
699 if (ret == -1)
700 return (ret);
701 imsg_event_add(iev);
702 return (ret);
705 int
706 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
707 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
709 int ret;
711 ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
712 if (ret == -1)
713 return (ret);
714 imsg_event_add(iev);
715 return (ret);
718 void
719 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
721 if (*n == -1) {
722 /* Use a range of all target instances */
723 *n = 0;
724 *m = ps->ps_instances[id];
725 } else {
726 /* Use only a single slot of the specified peer process */
727 *m = *n + 1;
731 int
732 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
733 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
735 int m;
737 proc_range(ps, id, &n, &m);
738 for (; n < m; n++) {
739 if (imsg_compose_event(&ps->ps_ievs[id][n],
740 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
741 return (-1);
744 return (0);
747 int
748 proc_compose(struct privsep *ps, enum privsep_procid id,
749 uint16_t type, void *data, uint16_t datalen)
751 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
754 int
755 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
756 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
758 int m;
760 proc_range(ps, id, &n, &m);
761 for (; n < m; n++)
762 if (imsg_composev_event(&ps->ps_ievs[id][n],
763 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
764 return (-1);
766 return (0);
769 int
770 proc_composev(struct privsep *ps, enum privsep_procid id,
771 uint16_t type, const struct iovec *iov, int iovcnt)
773 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
776 int
777 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
778 enum privsep_procid id, int n)
780 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
781 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
784 struct imsgbuf *
785 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
787 int m;
789 proc_range(ps, id, &n, &m);
790 return (&ps->ps_ievs[id][n].ibuf);
793 struct imsgev *
794 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
796 int m;
798 proc_range(ps, id, &n, &m);
799 return (&ps->ps_ievs[id][n]);
802 /* This function should only be called with care as it breaks async I/O */
803 int
804 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
806 struct imsgbuf *ibuf;
807 int m, ret = 0;
809 proc_range(ps, id, &n, &m);
810 for (; n < m; n++) {
811 ibuf = proc_ibuf(ps, id, n);
812 if (ibuf == NULL)
813 return (-1);
814 do {
815 ret = imsg_flush(ibuf);
816 } while (ret == -1 && errno == EAGAIN);
817 if (ret == -1)
818 break;
819 imsg_event_add(&ps->ps_ievs[id][n]);
822 return (ret);