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/socket.h>
20 #include <sys/wait.h>
22 /* TA: FIXME */
23 #include <grp.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <signal.h>
32 #include <pwd.h>
33 #include <event.h>
35 #include "proc.h"
37 #include "got_compat.h"
39 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int,
40 int, char **);
41 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
42 void proc_open(struct privsep *, int, int);
43 void proc_accept(struct privsep *, int, enum privsep_procid,
44 unsigned int);
45 void proc_close(struct privsep *);
46 int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid);
47 void proc_shutdown(struct privsep_proc *);
48 void proc_sig_handler(int, short, void *);
49 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
51 enum privsep_procid privsep_process;
53 int
54 proc_ispeer(struct privsep_proc *procs, unsigned int nproc,
55 enum privsep_procid type)
56 {
57 unsigned int i;
59 for (i = 0; i < nproc; i++)
60 if (procs[i].p_id == type)
61 return (1);
63 return (0);
64 }
66 enum privsep_procid
67 proc_getid(struct privsep_proc *procs, unsigned int nproc,
68 const char *proc_name)
69 {
70 struct privsep_proc *p;
71 unsigned int proc;
73 for (proc = 0; proc < nproc; proc++) {
74 p = &procs[proc];
75 if (strcmp(p->p_title, proc_name))
76 continue;
78 return (p->p_id);
79 }
81 return (PROC_MAX);
82 }
84 void
85 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
86 int argc, char **argv)
87 {
88 unsigned int proc, nargc, i, proc_i;
89 char **nargv;
90 struct privsep_proc *p;
91 char num[32];
92 int fd;
94 /* Prepare the new process argv. */
95 nargv = calloc(argc + 5, sizeof(char *));
96 if (nargv == NULL)
97 fatal("%s: calloc", __func__);
99 /* Copy call argument first. */
100 nargc = 0;
101 nargv[nargc++] = argv[0];
103 /* Set process name argument and save the position. */
104 nargv[nargc] = strdup("-P");
105 if (nargv[nargc] == NULL)
106 fatal("%s: strdup", __func__);
107 nargc++;
108 proc_i = nargc;
109 nargc++;
111 /* Point process instance arg to stack and copy the original args. */
112 nargv[nargc] = strdup("-I");
113 if (nargv[nargc] == NULL)
114 fatal("%s: strdup", __func__);
115 nargc++;
116 nargv[nargc++] = num;
117 for (i = 1; i < (unsigned int) argc; i++)
118 nargv[nargc++] = argv[i];
120 nargv[nargc] = NULL;
122 for (proc = 0; proc < nproc; proc++) {
123 p = &procs[proc];
125 /* Update args with process title. */
126 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
128 /* Fire children processes. */
129 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
130 /* Update the process instance number. */
131 snprintf(num, sizeof(num), "%u", i);
133 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0];
134 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0] = -1;
136 switch (fork()) {
137 case -1:
138 fatal("%s: fork", __func__);
139 break;
140 case 0:
141 /* First create a new session */
142 if (setsid() == -1)
143 fatal("setsid");
145 /* Prepare parent socket. */
146 if (fd != PROC_GOTWEBD_SOCK_FILENO) {
147 if (dup2(fd, PROC_GOTWEBD_SOCK_FILENO)
148 == -1)
149 fatal("dup2");
150 } else if (fcntl(fd, F_SETFD, 0) == -1)
151 fatal("fcntl");
153 execvp(argv[0], nargv);
154 fatal("%s: execvp", __func__);
155 break;
156 default:
157 /* Close child end. */
158 close(fd);
159 break;
164 free(nargv);
167 void
168 proc_connect(struct privsep *ps)
170 struct imsgev *iev;
171 unsigned int src, dst, inst;
173 /* Don't distribute any sockets if we are not really going to run. */
174 if (ps->ps_noaction)
175 return;
177 for (dst = 0; dst < PROC_MAX; dst++) {
178 /* We don't communicate with ourselves. */
179 if (dst == PROC_GOTWEBD)
180 continue;
182 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
183 iev = &ps->ps_ievs[dst][inst];
184 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
185 event_set(&iev->ev, iev->ibuf.fd, iev->events,
186 iev->handler, iev->data);
187 event_add(&iev->ev, NULL);
191 /* Distribute the socketpair()s for everyone. */
192 for (src = 0; src < PROC_MAX; src++)
193 for (dst = src; dst < PROC_MAX; dst++) {
194 /* Parent already distributed its fds. */
195 if (src == PROC_GOTWEBD || dst == PROC_GOTWEBD)
196 continue;
198 proc_open(ps, src, dst);
202 void
203 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
204 int argc, char **argv, enum privsep_procid proc_id)
206 struct privsep_proc *p = NULL;
207 struct privsep_pipes *pa, *pb;
208 unsigned int proc;
209 unsigned int dst;
210 int fds[2];
212 /* Don't initiate anything if we are not really going to run. */
213 if (ps->ps_noaction)
214 return;
216 if (proc_id == PROC_GOTWEBD) {
217 privsep_process = PROC_GOTWEBD;
218 proc_setup(ps, procs, nproc);
220 /*
221 * Create the children sockets so we can use them
222 * to distribute the rest of the socketpair()s using
223 * proc_connect() later.
224 */
225 for (dst = 0; dst < PROC_MAX; dst++) {
226 /* Don't create socket for ourselves. */
227 if (dst == PROC_GOTWEBD)
228 continue;
230 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
231 pa = &ps->ps_pipes[PROC_GOTWEBD][0];
232 pb = &ps->ps_pipes[dst][proc];
233 if (socketpair(AF_UNIX,
234 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
235 PF_UNSPEC, fds) == -1)
236 fatal("%s: socketpair", __func__);
238 pa->pp_pipes[dst][proc] = fds[0];
239 pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
243 /* Engage! */
244 proc_exec(ps, procs, nproc, argc, argv);
245 return;
248 /* Initialize a child */
249 for (proc = 0; proc < nproc; proc++) {
250 if (procs[proc].p_id != proc_id)
251 continue;
252 p = &procs[proc];
253 break;
255 if (p == NULL || p->p_init == NULL)
256 fatalx("%s: process %d missing process initialization",
257 __func__, proc_id);
259 p->p_init(ps, p);
261 fatalx("failed to initiate child process");
264 void
265 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
266 unsigned int n)
268 struct privsep_pipes *pp = ps->ps_pp;
269 struct imsgev *iev;
271 if (ps->ps_ievs[dst] == NULL) {
272 #if DEBUG > 1
273 log_debug("%s: %s src %d %d to dst %d %d not connected",
274 __func__, ps->ps_title[privsep_process],
275 privsep_process, ps->ps_instance + 1,
276 dst, n + 1);
277 #endif
278 close(fd);
279 return;
282 if (pp->pp_pipes[dst][n] != -1) {
283 log_warnx("%s: duplicated descriptor", __func__);
284 close(fd);
285 return;
286 } else
287 pp->pp_pipes[dst][n] = fd;
289 iev = &ps->ps_ievs[dst][n];
290 imsg_init(&iev->ibuf, fd);
291 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
292 event_add(&iev->ev, NULL);
295 void
296 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
298 unsigned int i, j, src, dst, id;
299 struct privsep_pipes *pp;
301 /* Initialize parent title, ps_instances and procs. */
302 ps->ps_title[PROC_GOTWEBD] = "parent";
304 for (src = 0; src < PROC_MAX; src++)
305 /* Default to 1 process instance */
306 if (ps->ps_instances[src] < 1)
307 ps->ps_instances[src] = 1;
309 for (src = 0; src < nproc; src++) {
310 procs[src].p_ps = ps;
311 if (procs[src].p_cb == NULL)
312 procs[src].p_cb = proc_dispatch_null;
314 id = procs[src].p_id;
315 ps->ps_title[id] = procs[src].p_title;
316 ps->ps_ievs[id] = calloc(ps->ps_instances[id],
317 sizeof(struct imsgev));
318 if (ps->ps_ievs[id] == NULL)
319 fatal("%s: calloc", __func__);
321 /* With this set up, we are ready to call imsg_init(). */
322 for (i = 0; i < ps->ps_instances[id]; i++) {
323 ps->ps_ievs[id][i].handler = proc_dispatch;
324 ps->ps_ievs[id][i].events = EV_READ;
325 ps->ps_ievs[id][i].proc = &procs[src];
326 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
330 /*
331 * Allocate pipes for all process instances (incl. parent)
333 * - ps->ps_pipes: N:M mapping
334 * N source processes connected to M destination processes:
335 * [src][instances][dst][instances], for example
336 * [PROC_RELAY][3][PROC_CA][3]
338 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
339 * Each process instance has a destination array of socketpair fds:
340 * [dst][instances], for example
341 * [PROC_GOTWEBD][0]
342 */
343 for (src = 0; src < PROC_MAX; src++) {
344 /* Allocate destination array for each process */
345 ps->ps_pipes[src] = calloc(ps->ps_instances[src],
346 sizeof(struct privsep_pipes));
347 if (ps->ps_pipes[src] == NULL)
348 fatal("%s: calloc", __func__);
350 for (i = 0; i < ps->ps_instances[src]; i++) {
351 pp = &ps->ps_pipes[src][i];
353 for (dst = 0; dst < PROC_MAX; dst++) {
354 /* Allocate maximum fd integers */
355 pp->pp_pipes[dst] =
356 calloc(ps->ps_instances[dst],
357 sizeof(int));
358 if (pp->pp_pipes[dst] == NULL)
359 fatal("%s: calloc", __func__);
361 /* Mark fd as unused */
362 for (j = 0; j < ps->ps_instances[dst]; j++)
363 pp->pp_pipes[dst][j] = -1;
368 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
371 void
372 proc_kill(struct privsep *ps)
374 char *cause;
375 pid_t pid;
376 int len, status;
378 if (privsep_process != PROC_GOTWEBD)
379 return;
381 proc_close(ps);
383 do {
384 pid = waitpid(WAIT_ANY, &status, 0);
385 if (pid <= 0)
386 continue;
388 if (WIFSIGNALED(status)) {
389 len = asprintf(&cause, "terminated; signal %d",
390 WTERMSIG(status));
391 } else if (WIFEXITED(status)) {
392 if (WEXITSTATUS(status) != 0)
393 len = asprintf(&cause, "exited abnormally");
394 else
395 len = 0;
396 } else
397 len = -1;
399 if (len == 0) {
400 /* child exited OK, don't print a warning message */
401 } else if (len != -1) {
402 log_warnx("lost child: pid %u %s", pid, cause);
403 free(cause);
404 } else
405 log_warnx("lost child: pid %u", pid);
406 } while (pid != -1 || (pid == -1 && errno == EINTR));
409 void
410 proc_open(struct privsep *ps, int src, int dst)
412 struct privsep_pipes *pa, *pb;
413 struct privsep_fd pf;
414 int fds[2];
415 unsigned int i, j;
417 /* Exchange pipes between process. */
418 for (i = 0; i < ps->ps_instances[src]; i++) {
419 for (j = 0; j < ps->ps_instances[dst]; j++) {
420 /* Don't create sockets for ourself. */
421 if (src == dst && i == j)
422 continue;
424 pa = &ps->ps_pipes[src][i];
425 pb = &ps->ps_pipes[dst][j];
426 if (socketpair(AF_UNIX,
427 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
428 PF_UNSPEC, fds) == -1)
429 fatal("%s: socketpair", __func__);
431 pa->pp_pipes[dst][j] = fds[0];
432 pb->pp_pipes[src][i] = fds[1];
434 pf.pf_procid = src;
435 pf.pf_instance = i;
436 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
437 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
438 fatal("%s: proc_compose_imsg", __func__);
440 pf.pf_procid = dst;
441 pf.pf_instance = j;
442 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
443 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
444 fatal("%s: proc_compose_imsg", __func__);
446 /*
447 * We have to flush to send the descriptors and close
448 * them to avoid the fd ramp on startup.
449 */
450 if (proc_flush_imsg(ps, src, i) == -1 ||
451 proc_flush_imsg(ps, dst, j) == -1)
452 fatal("%s: imsg_flush", __func__);
457 void
458 proc_close(struct privsep *ps)
460 unsigned int dst, n;
461 struct privsep_pipes *pp;
463 if (ps == NULL)
464 return;
466 pp = ps->ps_pp;
468 for (dst = 0; dst < PROC_MAX; dst++) {
469 if (ps->ps_ievs[dst] == NULL)
470 continue;
472 for (n = 0; n < ps->ps_instances[dst]; n++) {
473 if (pp->pp_pipes[dst][n] == -1)
474 continue;
476 /* Cancel the fd, close and invalidate the fd */
477 event_del(&(ps->ps_ievs[dst][n].ev));
478 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
479 close(pp->pp_pipes[dst][n]);
480 pp->pp_pipes[dst][n] = -1;
482 free(ps->ps_ievs[dst]);
486 void
487 proc_shutdown(struct privsep_proc *p)
489 struct privsep *ps = p->p_ps;
491 if (p->p_shutdown != NULL)
492 (*p->p_shutdown)();
494 proc_close(ps);
496 log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
498 exit(0);
501 void
502 proc_sig_handler(int sig, short event, void *arg)
504 struct privsep_proc *p = arg;
506 switch (sig) {
507 case SIGINT:
508 case SIGTERM:
509 proc_shutdown(p);
510 break;
511 case SIGCHLD:
512 case SIGHUP:
513 case SIGPIPE:
514 case SIGUSR1:
515 /* ignore */
516 break;
517 default:
518 fatalx("proc_sig_handler: unexpected signal");
519 /* NOTREACHED */
523 void
524 proc_run(struct privsep *ps, struct privsep_proc *p,
525 struct privsep_proc *procs, unsigned int nproc,
526 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
528 struct passwd *pw;
529 const char *root;
531 log_procinit(p->p_title);
533 /* Set the process group of the current process */
534 setpgid(0, 0);
536 /* Use non-standard user */
537 if (p->p_pw != NULL)
538 pw = p->p_pw;
539 else
540 pw = ps->ps_pw;
542 /* Change root directory */
543 if (p->p_chroot != NULL)
544 root = p->p_chroot;
545 else
546 root = pw->pw_dir;
548 if (chroot(root) == -1)
549 fatal("proc_run: chroot");
550 if (chdir("/") == -1)
551 fatal("proc_run: chdir(\"/\")");
553 privsep_process = p->p_id;
555 setproctitle("%s", p->p_title);
557 if (setgroups(1, &pw->pw_gid) ||
558 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
559 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
560 fatal("proc_run: cannot drop privileges");
562 event_init();
564 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
565 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
566 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
567 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
568 signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
569 signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
571 signal_add(&ps->ps_evsigint, NULL);
572 signal_add(&ps->ps_evsigterm, NULL);
573 signal_add(&ps->ps_evsigchld, NULL);
574 signal_add(&ps->ps_evsighup, NULL);
575 signal_add(&ps->ps_evsigpipe, NULL);
576 signal_add(&ps->ps_evsigusr1, NULL);
578 proc_setup(ps, procs, nproc);
579 proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
581 DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
582 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
584 if (run != NULL)
585 run(ps, p, arg);
587 event_dispatch();
589 proc_shutdown(p);
592 void
593 proc_dispatch(int fd, short event, void *arg)
595 struct imsgev *iev = arg;
596 struct privsep_proc *p = iev->proc;
597 struct privsep *ps = p->p_ps;
598 struct imsgbuf *ibuf;
599 struct imsg imsg;
600 ssize_t n;
601 int verbose;
602 const char *title;
603 struct privsep_fd pf;
605 title = ps->ps_title[privsep_process];
606 ibuf = &iev->ibuf;
608 if (event & EV_READ) {
609 n = imsg_read(ibuf);
610 if (n == -1 && errno != EAGAIN)
611 fatal("%s: imsg_read", __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 if (event & EV_WRITE) {
621 n = msgbuf_write(&ibuf->w);
622 if (n == -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 n = imsg_get(ibuf, &imsg);
634 if (n == -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_VERBOSE:
659 log_info("%s", __func__);
660 IMSG_SIZE_CHECK(&imsg, &verbose);
661 memcpy(&verbose, imsg.data, sizeof(verbose));
662 log_setverbose(verbose);
663 break;
664 case IMSG_CTL_PROCFD:
665 IMSG_SIZE_CHECK(&imsg, &pf);
666 memcpy(&pf, imsg.data, sizeof(pf));
667 proc_accept(ps, imsg.fd, pf.pf_procid,
668 pf.pf_instance);
669 break;
670 default:
671 log_warnx("%s: %s %d got invalid imsg %d peerid %d "
672 "from %s %d",
673 __func__, title, ps->ps_instance + 1,
674 imsg.hdr.type, imsg.hdr.peerid,
675 p->p_title, imsg.hdr.pid);
677 imsg_free(&imsg);
679 imsg_event_add(iev);
682 int
683 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
685 return (-1);
688 /*
689 * imsg helper functions
690 */
692 void
693 imsg_event_add(struct imsgev *iev)
695 if (iev->handler == NULL) {
696 imsg_flush(&iev->ibuf);
697 return;
700 iev->events = EV_READ;
701 if (iev->ibuf.w.queued)
702 iev->events |= EV_WRITE;
704 event_del(&iev->ev);
705 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
706 event_add(&iev->ev, NULL);
709 int
710 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
711 pid_t pid, int fd, void *data, uint16_t datalen)
713 int ret;
715 ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
716 if (ret == -1)
717 return (ret);
718 imsg_event_add(iev);
719 return (ret);
722 int
723 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
724 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
726 int ret;
728 ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
729 if (ret == -1)
730 return (ret);
731 imsg_event_add(iev);
732 return (ret);
735 void
736 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
738 if (*n == -1) {
739 /* Use a range of all target instances */
740 *n = 0;
741 *m = ps->ps_instances[id];
742 } else {
743 /* Use only a single slot of the specified peer process */
744 *m = *n + 1;
748 int
749 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
750 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
752 int m;
754 proc_range(ps, id, &n, &m);
755 for (; n < m; n++) {
756 if (imsg_compose_event(&ps->ps_ievs[id][n],
757 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
758 return (-1);
761 return (0);
764 int
765 proc_compose(struct privsep *ps, enum privsep_procid id,
766 uint16_t type, void *data, uint16_t datalen)
768 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
771 int
772 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
773 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
775 int m;
777 proc_range(ps, id, &n, &m);
778 for (; n < m; n++)
779 if (imsg_composev_event(&ps->ps_ievs[id][n],
780 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
781 return (-1);
783 return (0);
786 int
787 proc_composev(struct privsep *ps, enum privsep_procid id,
788 uint16_t type, const struct iovec *iov, int iovcnt)
790 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
793 int
794 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
795 enum privsep_procid id, int n)
797 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
798 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
801 struct imsgbuf *
802 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
804 int m;
806 proc_range(ps, id, &n, &m);
807 return (&ps->ps_ievs[id][n].ibuf);
810 struct imsgev *
811 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
813 int m;
815 proc_range(ps, id, &n, &m);
816 return (&ps->ps_ievs[id][n]);
819 /* This function should only be called with care as it breaks async I/O */
820 int
821 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
823 struct imsgbuf *ibuf;
824 int m, ret = 0;
826 proc_range(ps, id, &n, &m);
827 for (; n < m; n++) {
828 ibuf = proc_ibuf(ps, id, n);
829 if (ibuf == NULL)
830 return (-1);
831 do {
832 ret = imsg_flush(ibuf);
833 } while (ret == -1 && errno == EAGAIN);
834 if (ret == -1)
835 break;
836 imsg_event_add(&ps->ps_ievs[id][n]);
839 return (ret);