portable: build gotsh when --enable-gotd passed
[got-portable.git] / gotwebd / proc.c
blobb1c287f143899db622e0faf4777c1a4baaadd4da
1 /*
2 * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
3 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
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.
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.
17 #include "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <pwd.h>
32 #include <event.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)
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);
64 return (PROC_MAX);
67 void
68 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
69 int argc, char **argv)
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);
204 * Create the children sockets so we can use them
205 * to distribute the rest of the socketpair()s using
206 * proc_connect() later.
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 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
217 #ifdef SOCK_CLOEXEC
218 sock_flags |= SOCK_CLOEXEC;
219 #endif
220 if (socketpair(AF_UNIX, sock_flags,
221 PF_UNSPEC, fds) == -1)
222 fatal("%s: socketpair", __func__);
224 pa->pp_pipes[dst][proc] = fds[0];
225 pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
229 /* Engage! */
230 proc_exec(ps, procs, nproc, argc, argv);
231 return;
234 /* Initialize a child */
235 for (proc = 0; proc < nproc; proc++) {
236 if (procs[proc].p_id != proc_id)
237 continue;
238 p = &procs[proc];
239 break;
241 if (p == NULL || p->p_init == NULL)
242 fatalx("%s: process %d missing process initialization",
243 __func__, proc_id);
245 p->p_init(ps, p);
247 fatalx("failed to initiate child process");
250 void
251 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
252 unsigned int n)
254 struct privsep_pipes *pp = ps->ps_pp;
255 struct imsgev *iev;
257 if (ps->ps_ievs[dst] == NULL) {
258 #if DEBUG > 1
259 log_debug("%s: %s src %d %d to dst %d %d not connected",
260 __func__, ps->ps_title[privsep_process],
261 privsep_process, ps->ps_instance + 1,
262 dst, n + 1);
263 #endif
264 close(fd);
265 return;
268 if (pp->pp_pipes[dst][n] != -1) {
269 log_warnx("%s: duplicated descriptor", __func__);
270 close(fd);
271 return;
272 } else
273 pp->pp_pipes[dst][n] = fd;
275 iev = &ps->ps_ievs[dst][n];
276 imsg_init(&iev->ibuf, fd);
277 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
278 event_add(&iev->ev, NULL);
281 void
282 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
284 unsigned int i, j, src, dst, id;
285 struct privsep_pipes *pp;
287 /* Initialize parent title, ps_instances and procs. */
288 ps->ps_title[PROC_GOTWEBD] = "parent";
290 for (src = 0; src < PROC_MAX; src++)
291 /* Default to 1 process instance */
292 if (ps->ps_instances[src] < 1)
293 ps->ps_instances[src] = 1;
295 for (src = 0; src < nproc; src++) {
296 procs[src].p_ps = ps;
297 if (procs[src].p_cb == NULL)
298 procs[src].p_cb = proc_dispatch_null;
300 id = procs[src].p_id;
301 ps->ps_title[id] = procs[src].p_title;
302 ps->ps_ievs[id] = calloc(ps->ps_instances[id],
303 sizeof(struct imsgev));
304 if (ps->ps_ievs[id] == NULL)
305 fatal("%s: calloc", __func__);
307 /* With this set up, we are ready to call imsg_init(). */
308 for (i = 0; i < ps->ps_instances[id]; i++) {
309 ps->ps_ievs[id][i].handler = proc_dispatch;
310 ps->ps_ievs[id][i].events = EV_READ;
311 ps->ps_ievs[id][i].proc = &procs[src];
312 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
317 * Allocate pipes for all process instances (incl. parent)
319 * - ps->ps_pipes: N:M mapping
320 * N source processes connected to M destination processes:
321 * [src][instances][dst][instances], for example
322 * [PROC_RELAY][3][PROC_CA][3]
324 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
325 * Each process instance has a destination array of socketpair fds:
326 * [dst][instances], for example
327 * [PROC_GOTWEBD][0]
329 for (src = 0; src < PROC_MAX; src++) {
330 /* Allocate destination array for each process */
331 ps->ps_pipes[src] = calloc(ps->ps_instances[src],
332 sizeof(struct privsep_pipes));
333 if (ps->ps_pipes[src] == NULL)
334 fatal("%s: calloc", __func__);
336 for (i = 0; i < ps->ps_instances[src]; i++) {
337 pp = &ps->ps_pipes[src][i];
339 for (dst = 0; dst < PROC_MAX; dst++) {
340 /* Allocate maximum fd integers */
341 pp->pp_pipes[dst] =
342 calloc(ps->ps_instances[dst],
343 sizeof(int));
344 if (pp->pp_pipes[dst] == NULL)
345 fatal("%s: calloc", __func__);
347 /* Mark fd as unused */
348 for (j = 0; j < ps->ps_instances[dst]; j++)
349 pp->pp_pipes[dst][j] = -1;
354 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
357 void
358 proc_kill(struct privsep *ps)
360 char *cause;
361 pid_t pid;
362 int len, status;
364 if (privsep_process != PROC_GOTWEBD)
365 return;
367 proc_close(ps);
369 do {
370 pid = waitpid(WAIT_ANY, &status, 0);
371 if (pid <= 0)
372 continue;
374 if (WIFSIGNALED(status)) {
375 len = asprintf(&cause, "terminated; signal %d",
376 WTERMSIG(status));
377 } else if (WIFEXITED(status)) {
378 if (WEXITSTATUS(status) != 0)
379 len = asprintf(&cause, "exited abnormally");
380 else
381 len = 0;
382 } else
383 len = -1;
385 if (len == 0) {
386 /* child exited OK, don't print a warning message */
387 } else if (len != -1) {
388 log_warnx("lost child: pid %u %s", pid, cause);
389 free(cause);
390 } else
391 log_warnx("lost child: pid %u", pid);
392 } while (pid != -1 || (pid == -1 && errno == EINTR));
395 void
396 proc_open(struct privsep *ps, int src, int dst)
398 struct privsep_pipes *pa, *pb;
399 struct privsep_fd pf;
400 int fds[2];
401 unsigned int i, j;
403 /* Exchange pipes between process. */
404 for (i = 0; i < ps->ps_instances[src]; i++) {
405 for (j = 0; j < ps->ps_instances[dst]; j++) {
406 /* Don't create sockets for ourself. */
407 if (src == dst && i == j)
408 continue;
410 pa = &ps->ps_pipes[src][i];
411 pb = &ps->ps_pipes[dst][j];
412 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
413 #ifdef SOCK_CLOEXEC
414 sock_flags |= SOCK_CLOEXEC;
415 #endif
416 if (socketpair(AF_UNIX, sock_flags,
417 PF_UNSPEC, fds) == -1)
418 fatal("%s: socketpair", __func__);
420 pa->pp_pipes[dst][j] = fds[0];
421 pb->pp_pipes[src][i] = fds[1];
423 pf.pf_procid = src;
424 pf.pf_instance = i;
425 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
426 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
427 fatal("%s: proc_compose_imsg", __func__);
429 pf.pf_procid = dst;
430 pf.pf_instance = j;
431 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
432 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
433 fatal("%s: proc_compose_imsg", __func__);
436 * We have to flush to send the descriptors and close
437 * them to avoid the fd ramp on startup.
439 if (proc_flush_imsg(ps, src, i) == -1 ||
440 proc_flush_imsg(ps, dst, j) == -1)
441 fatal("%s: imsg_flush", __func__);
446 void
447 proc_close(struct privsep *ps)
449 unsigned int dst, n;
450 struct privsep_pipes *pp;
452 if (ps == NULL)
453 return;
455 pp = ps->ps_pp;
457 for (dst = 0; dst < PROC_MAX; dst++) {
458 if (ps->ps_ievs[dst] == NULL)
459 continue;
461 for (n = 0; n < ps->ps_instances[dst]; n++) {
462 if (pp->pp_pipes[dst][n] == -1)
463 continue;
465 /* Cancel the fd, close and invalidate the fd */
466 event_del(&(ps->ps_ievs[dst][n].ev));
467 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
468 close(pp->pp_pipes[dst][n]);
469 pp->pp_pipes[dst][n] = -1;
471 free(ps->ps_ievs[dst]);
475 void
476 proc_shutdown(struct privsep_proc *p)
478 struct privsep *ps = p->p_ps;
480 if (p->p_shutdown != NULL)
481 (*p->p_shutdown)();
483 proc_close(ps);
485 log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
487 exit(0);
490 void
491 proc_sig_handler(int sig, short event, void *arg)
493 struct privsep_proc *p = arg;
495 switch (sig) {
496 case SIGINT:
497 case SIGTERM:
498 proc_shutdown(p);
499 break;
500 case SIGCHLD:
501 case SIGHUP:
502 case SIGPIPE:
503 case SIGUSR1:
504 /* ignore */
505 break;
506 default:
507 fatalx("proc_sig_handler: unexpected signal");
508 /* NOTREACHED */
512 void
513 proc_run(struct privsep *ps, struct privsep_proc *p,
514 struct privsep_proc *procs, unsigned int nproc,
515 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
517 struct passwd *pw;
518 const char *root;
520 log_procinit(p->p_title);
522 /* Set the process group of the current process */
523 setpgid(0, 0);
525 /* Use non-standard user */
526 if (p->p_pw != NULL)
527 pw = p->p_pw;
528 else
529 pw = ps->ps_pw;
531 /* Change root directory */
532 if (p->p_chroot != NULL)
533 root = p->p_chroot;
534 else
535 root = pw->pw_dir;
537 if (chroot(root) == -1)
538 fatal("proc_run: chroot");
539 if (chdir("/") == -1)
540 fatal("proc_run: chdir(\"/\")");
542 privsep_process = p->p_id;
544 setproctitle("%s", p->p_title);
546 if (setgroups(1, &pw->pw_gid) ||
547 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
548 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
549 fatal("proc_run: cannot drop privileges");
551 event_init();
553 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
554 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
555 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
556 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
557 signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
558 signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
560 signal_add(&ps->ps_evsigint, NULL);
561 signal_add(&ps->ps_evsigterm, NULL);
562 signal_add(&ps->ps_evsigchld, NULL);
563 signal_add(&ps->ps_evsighup, NULL);
564 signal_add(&ps->ps_evsigpipe, NULL);
565 signal_add(&ps->ps_evsigusr1, NULL);
567 proc_setup(ps, procs, nproc);
568 proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
570 DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
571 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
573 if (run != NULL)
574 run(ps, p, arg);
576 event_dispatch();
578 proc_shutdown(p);
581 void
582 proc_dispatch(int fd, short event, void *arg)
584 struct imsgev *iev = arg;
585 struct privsep_proc *p = iev->proc;
586 struct privsep *ps = p->p_ps;
587 struct imsgbuf *ibuf;
588 struct imsg imsg;
589 ssize_t n;
590 int verbose;
591 const char *title;
592 struct privsep_fd pf;
594 title = ps->ps_title[privsep_process];
595 ibuf = &iev->ibuf;
597 if (event & EV_READ) {
598 n = imsg_read(ibuf);
599 if (n == -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 n = msgbuf_write(&ibuf->w);
611 if (n == -1 && errno != EAGAIN)
612 fatal("%s: msgbuf_write", __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 for (;;) {
622 n = imsg_get(ibuf, &imsg);
623 if (n == -1)
624 fatal("%s: imsg_get", __func__);
625 if (n == 0)
626 break;
628 #if DEBUG > 1
629 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
630 __func__, title, ps->ps_instance + 1,
631 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
632 #endif
635 * Check the message with the program callback
637 if ((p->p_cb)(fd, p, &imsg) == 0) {
638 /* Message was handled by the callback, continue */
639 imsg_free(&imsg);
640 continue;
644 * Generic message handling
646 switch (imsg.hdr.type) {
647 case IMSG_CTL_VERBOSE:
648 log_info("%s", __func__);
649 IMSG_SIZE_CHECK(&imsg, &verbose);
650 memcpy(&verbose, imsg.data, sizeof(verbose));
651 log_setverbose(verbose);
652 break;
653 case IMSG_CTL_PROCFD:
654 IMSG_SIZE_CHECK(&imsg, &pf);
655 memcpy(&pf, imsg.data, sizeof(pf));
656 proc_accept(ps, imsg.fd, pf.pf_procid,
657 pf.pf_instance);
658 break;
659 default:
660 log_warnx("%s: %s %d got invalid imsg %d peerid %d "
661 "from %s %d",
662 __func__, title, ps->ps_instance + 1,
663 imsg.hdr.type, imsg.hdr.peerid,
664 p->p_title, imsg.hdr.pid);
666 imsg_free(&imsg);
668 imsg_event_add(iev);
672 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
674 return (-1);
678 * imsg helper functions
681 void
682 imsg_event_add(struct imsgev *iev)
684 if (iev->handler == NULL) {
685 imsg_flush(&iev->ibuf);
686 return;
689 iev->events = EV_READ;
690 if (iev->ibuf.w.queued)
691 iev->events |= EV_WRITE;
693 event_del(&iev->ev);
694 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
695 event_add(&iev->ev, NULL);
699 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
700 pid_t pid, int fd, void *data, uint16_t datalen)
702 int ret;
704 ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
705 if (ret == -1)
706 return (ret);
707 imsg_event_add(iev);
708 return (ret);
712 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
713 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
715 int ret;
717 ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
718 if (ret == -1)
719 return (ret);
720 imsg_event_add(iev);
721 return (ret);
724 void
725 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
727 if (*n == -1) {
728 /* Use a range of all target instances */
729 *n = 0;
730 *m = ps->ps_instances[id];
731 } else {
732 /* Use only a single slot of the specified peer process */
733 *m = *n + 1;
738 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
739 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
741 int m;
743 proc_range(ps, id, &n, &m);
744 for (; n < m; n++) {
745 if (imsg_compose_event(&ps->ps_ievs[id][n],
746 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
747 return (-1);
750 return (0);
754 proc_compose(struct privsep *ps, enum privsep_procid id,
755 uint16_t type, void *data, uint16_t datalen)
757 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
761 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
762 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
764 int m;
766 proc_range(ps, id, &n, &m);
767 for (; n < m; n++)
768 if (imsg_composev_event(&ps->ps_ievs[id][n],
769 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
770 return (-1);
772 return (0);
776 proc_composev(struct privsep *ps, enum privsep_procid id,
777 uint16_t type, const struct iovec *iov, int iovcnt)
779 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
783 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
784 enum privsep_procid id, int n)
786 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
787 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
790 struct imsgbuf *
791 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
793 int m;
795 proc_range(ps, id, &n, &m);
796 return (&ps->ps_ievs[id][n].ibuf);
799 struct imsgev *
800 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
802 int m;
804 proc_range(ps, id, &n, &m);
805 return (&ps->ps_ievs[id][n]);
808 /* This function should only be called with care as it breaks async I/O */
810 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
812 struct imsgbuf *ibuf;
813 int m, ret = 0;
815 proc_range(ps, id, &n, &m);
816 for (; n < m; n++) {
817 ibuf = proc_ibuf(ps, id, n);
818 if (ibuf == NULL)
819 return (-1);
820 do {
821 ret = imsg_flush(ibuf);
822 } while (ret == -1 && errno == EAGAIN);
823 if (ret == -1)
824 break;
825 imsg_event_add(&ps->ps_ievs[id][n]);
828 return (ret);