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>
33 #include "proc.h"
35 #include "got_compat.h"
37 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int,
38 int, char **);
39 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
40 void proc_open(struct privsep *, int, int);
41 void proc_accept(struct privsep *, int, enum privsep_procid,
42 unsigned int);
43 void proc_close(struct privsep *);
44 int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid);
45 void proc_shutdown(struct privsep_proc *);
46 void proc_sig_handler(int, short, void *);
47 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
49 enum privsep_procid privsep_process;
51 int
52 proc_ispeer(struct privsep_proc *procs, unsigned int nproc,
53 enum privsep_procid type)
54 {
55 unsigned int i;
57 for (i = 0; i < nproc; i++)
58 if (procs[i].p_id == type)
59 return (1);
61 return (0);
62 }
64 enum privsep_procid
65 proc_getid(struct privsep_proc *procs, unsigned int nproc,
66 const char *proc_name)
67 {
68 struct privsep_proc *p;
69 unsigned int proc;
71 for (proc = 0; proc < nproc; proc++) {
72 p = &procs[proc];
73 if (strcmp(p->p_title, proc_name))
74 continue;
76 return (p->p_id);
77 }
79 return (PROC_MAX);
80 }
82 void
83 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
84 int argc, char **argv)
85 {
86 unsigned int proc, nargc, i, proc_i;
87 char **nargv;
88 struct privsep_proc *p;
89 char num[32];
90 int fd;
92 /* Prepare the new process argv. */
93 nargv = calloc(argc + 5, sizeof(char *));
94 if (nargv == NULL)
95 fatal("%s: calloc", __func__);
97 /* Copy call argument first. */
98 nargc = 0;
99 nargv[nargc++] = argv[0];
101 /* Set process name argument and save the position. */
102 nargv[nargc] = strdup("-P");
103 if (nargv[nargc] == NULL)
104 fatal("%s: strdup", __func__);
105 nargc++;
106 proc_i = nargc;
107 nargc++;
109 /* Point process instance arg to stack and copy the original args. */
110 nargv[nargc] = strdup("-I");
111 if (nargv[nargc] == NULL)
112 fatal("%s: strdup", __func__);
113 nargc++;
114 nargv[nargc++] = num;
115 for (i = 1; i < (unsigned int) argc; i++)
116 nargv[nargc++] = argv[i];
118 nargv[nargc] = NULL;
120 for (proc = 0; proc < nproc; proc++) {
121 p = &procs[proc];
123 /* Update args with process title. */
124 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
126 /* Fire children processes. */
127 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
128 /* Update the process instance number. */
129 snprintf(num, sizeof(num), "%u", i);
131 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0];
132 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][0] = -1;
134 switch (fork()) {
135 case -1:
136 fatal("%s: fork", __func__);
137 break;
138 case 0:
139 /* First create a new session */
140 if (setsid() == -1)
141 fatal("setsid");
143 /* Prepare parent socket. */
144 if (fd != PROC_GOTWEBD_SOCK_FILENO) {
145 if (dup2(fd, PROC_GOTWEBD_SOCK_FILENO)
146 == -1)
147 fatal("dup2");
148 } else if (fcntl(fd, F_SETFD, 0) == -1)
149 fatal("fcntl");
151 execvp(argv[0], nargv);
152 fatal("%s: execvp", __func__);
153 break;
154 default:
155 /* Close child end. */
156 close(fd);
157 break;
162 free(nargv);
165 void
166 proc_connect(struct privsep *ps)
168 struct imsgev *iev;
169 unsigned int src, dst, inst;
171 /* Don't distribute any sockets if we are not really going to run. */
172 if (ps->ps_noaction)
173 return;
175 for (dst = 0; dst < PROC_MAX; dst++) {
176 /* We don't communicate with ourselves. */
177 if (dst == PROC_GOTWEBD)
178 continue;
180 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
181 iev = &ps->ps_ievs[dst][inst];
182 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
183 event_set(&iev->ev, iev->ibuf.fd, iev->events,
184 iev->handler, iev->data);
185 event_add(&iev->ev, NULL);
189 /* Distribute the socketpair()s for everyone. */
190 for (src = 0; src < PROC_MAX; src++)
191 for (dst = src; dst < PROC_MAX; dst++) {
192 /* Parent already distributed its fds. */
193 if (src == PROC_GOTWEBD || dst == PROC_GOTWEBD)
194 continue;
196 proc_open(ps, src, dst);
200 void
201 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
202 int argc, char **argv, enum privsep_procid proc_id)
204 struct privsep_proc *p = NULL;
205 struct privsep_pipes *pa, *pb;
206 unsigned int proc;
207 unsigned int dst;
208 int fds[2];
210 /* Don't initiate anything if we are not really going to run. */
211 if (ps->ps_noaction)
212 return;
214 if (proc_id == PROC_GOTWEBD) {
215 privsep_process = PROC_GOTWEBD;
216 proc_setup(ps, procs, nproc);
218 /*
219 * Create the children sockets so we can use them
220 * to distribute the rest of the socketpair()s using
221 * proc_connect() later.
222 */
223 for (dst = 0; dst < PROC_MAX; dst++) {
224 /* Don't create socket for ourselves. */
225 if (dst == PROC_GOTWEBD)
226 continue;
228 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
229 pa = &ps->ps_pipes[PROC_GOTWEBD][0];
230 pb = &ps->ps_pipes[dst][proc];
231 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
232 #ifdef SOCK_CLOEXEC
233 sock_flags |= SOCK_CLOEXEC;
234 #endif
235 if (socketpair(AF_UNIX, sock_flags,
236 PF_UNSPEC, fds) == -1)
237 fatal("%s: socketpair", __func__);
239 pa->pp_pipes[dst][proc] = fds[0];
240 pb->pp_pipes[PROC_GOTWEBD][0] = fds[1];
244 /* Engage! */
245 proc_exec(ps, procs, nproc, argc, argv);
246 return;
249 /* Initialize a child */
250 for (proc = 0; proc < nproc; proc++) {
251 if (procs[proc].p_id != proc_id)
252 continue;
253 p = &procs[proc];
254 break;
256 if (p == NULL || p->p_init == NULL)
257 fatalx("%s: process %d missing process initialization",
258 __func__, proc_id);
260 p->p_init(ps, p);
262 fatalx("failed to initiate child process");
265 void
266 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
267 unsigned int n)
269 struct privsep_pipes *pp = ps->ps_pp;
270 struct imsgev *iev;
272 if (ps->ps_ievs[dst] == NULL) {
273 #if DEBUG > 1
274 log_debug("%s: %s src %d %d to dst %d %d not connected",
275 __func__, ps->ps_title[privsep_process],
276 privsep_process, ps->ps_instance + 1,
277 dst, n + 1);
278 #endif
279 close(fd);
280 return;
283 if (pp->pp_pipes[dst][n] != -1) {
284 log_warnx("%s: duplicated descriptor", __func__);
285 close(fd);
286 return;
287 } else
288 pp->pp_pipes[dst][n] = fd;
290 iev = &ps->ps_ievs[dst][n];
291 imsg_init(&iev->ibuf, fd);
292 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
293 event_add(&iev->ev, NULL);
296 void
297 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
299 unsigned int i, j, src, dst, id;
300 struct privsep_pipes *pp;
302 /* Initialize parent title, ps_instances and procs. */
303 ps->ps_title[PROC_GOTWEBD] = "parent";
305 for (src = 0; src < PROC_MAX; src++)
306 /* Default to 1 process instance */
307 if (ps->ps_instances[src] < 1)
308 ps->ps_instances[src] = 1;
310 for (src = 0; src < nproc; src++) {
311 procs[src].p_ps = ps;
312 if (procs[src].p_cb == NULL)
313 procs[src].p_cb = proc_dispatch_null;
315 id = procs[src].p_id;
316 ps->ps_title[id] = procs[src].p_title;
317 ps->ps_ievs[id] = calloc(ps->ps_instances[id],
318 sizeof(struct imsgev));
319 if (ps->ps_ievs[id] == NULL)
320 fatal("%s: calloc", __func__);
322 /* With this set up, we are ready to call imsg_init(). */
323 for (i = 0; i < ps->ps_instances[id]; i++) {
324 ps->ps_ievs[id][i].handler = proc_dispatch;
325 ps->ps_ievs[id][i].events = EV_READ;
326 ps->ps_ievs[id][i].proc = &procs[src];
327 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
331 /*
332 * Allocate pipes for all process instances (incl. parent)
334 * - ps->ps_pipes: N:M mapping
335 * N source processes connected to M destination processes:
336 * [src][instances][dst][instances], for example
337 * [PROC_RELAY][3][PROC_CA][3]
339 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
340 * Each process instance has a destination array of socketpair fds:
341 * [dst][instances], for example
342 * [PROC_GOTWEBD][0]
343 */
344 for (src = 0; src < PROC_MAX; src++) {
345 /* Allocate destination array for each process */
346 ps->ps_pipes[src] = calloc(ps->ps_instances[src],
347 sizeof(struct privsep_pipes));
348 if (ps->ps_pipes[src] == NULL)
349 fatal("%s: calloc", __func__);
351 for (i = 0; i < ps->ps_instances[src]; i++) {
352 pp = &ps->ps_pipes[src][i];
354 for (dst = 0; dst < PROC_MAX; dst++) {
355 /* Allocate maximum fd integers */
356 pp->pp_pipes[dst] =
357 calloc(ps->ps_instances[dst],
358 sizeof(int));
359 if (pp->pp_pipes[dst] == NULL)
360 fatal("%s: calloc", __func__);
362 /* Mark fd as unused */
363 for (j = 0; j < ps->ps_instances[dst]; j++)
364 pp->pp_pipes[dst][j] = -1;
369 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
372 void
373 proc_kill(struct privsep *ps)
375 char *cause;
376 pid_t pid;
377 int len, status;
379 if (privsep_process != PROC_GOTWEBD)
380 return;
382 proc_close(ps);
384 do {
385 pid = waitpid(WAIT_ANY, &status, 0);
386 if (pid <= 0)
387 continue;
389 if (WIFSIGNALED(status)) {
390 len = asprintf(&cause, "terminated; signal %d",
391 WTERMSIG(status));
392 } else if (WIFEXITED(status)) {
393 if (WEXITSTATUS(status) != 0)
394 len = asprintf(&cause, "exited abnormally");
395 else
396 len = 0;
397 } else
398 len = -1;
400 if (len == 0) {
401 /* child exited OK, don't print a warning message */
402 } else if (len != -1) {
403 log_warnx("lost child: pid %u %s", pid, cause);
404 free(cause);
405 } else
406 log_warnx("lost child: pid %u", pid);
407 } while (pid != -1 || (pid == -1 && errno == EINTR));
410 void
411 proc_open(struct privsep *ps, int src, int dst)
413 struct privsep_pipes *pa, *pb;
414 struct privsep_fd pf;
415 int fds[2];
416 unsigned int i, j;
418 /* Exchange pipes between process. */
419 for (i = 0; i < ps->ps_instances[src]; i++) {
420 for (j = 0; j < ps->ps_instances[dst]; j++) {
421 /* Don't create sockets for ourself. */
422 if (src == dst && i == j)
423 continue;
425 pa = &ps->ps_pipes[src][i];
426 pb = &ps->ps_pipes[dst][j];
427 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
428 #ifdef SOCK_CLOEXEC
429 sock_flags |= SOCK_CLOEXEC;
430 #endif
431 if (socketpair(AF_UNIX, sock_flags,
432 PF_UNSPEC, fds) == -1)
433 fatal("%s: socketpair", __func__);
435 pa->pp_pipes[dst][j] = fds[0];
436 pb->pp_pipes[src][i] = fds[1];
438 pf.pf_procid = src;
439 pf.pf_instance = i;
440 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
441 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
442 fatal("%s: proc_compose_imsg", __func__);
444 pf.pf_procid = dst;
445 pf.pf_instance = j;
446 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
447 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
448 fatal("%s: proc_compose_imsg", __func__);
450 /*
451 * We have to flush to send the descriptors and close
452 * them to avoid the fd ramp on startup.
453 */
454 if (proc_flush_imsg(ps, src, i) == -1 ||
455 proc_flush_imsg(ps, dst, j) == -1)
456 fatal("%s: imsg_flush", __func__);
461 void
462 proc_close(struct privsep *ps)
464 unsigned int dst, n;
465 struct privsep_pipes *pp;
467 if (ps == NULL)
468 return;
470 pp = ps->ps_pp;
472 for (dst = 0; dst < PROC_MAX; dst++) {
473 if (ps->ps_ievs[dst] == NULL)
474 continue;
476 for (n = 0; n < ps->ps_instances[dst]; n++) {
477 if (pp->pp_pipes[dst][n] == -1)
478 continue;
480 /* Cancel the fd, close and invalidate the fd */
481 event_del(&(ps->ps_ievs[dst][n].ev));
482 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
483 close(pp->pp_pipes[dst][n]);
484 pp->pp_pipes[dst][n] = -1;
486 free(ps->ps_ievs[dst]);
490 void
491 proc_shutdown(struct privsep_proc *p)
493 struct privsep *ps = p->p_ps;
495 if (p->p_shutdown != NULL)
496 (*p->p_shutdown)();
498 proc_close(ps);
500 log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
502 exit(0);
505 void
506 proc_sig_handler(int sig, short event, void *arg)
508 struct privsep_proc *p = arg;
510 switch (sig) {
511 case SIGINT:
512 case SIGTERM:
513 proc_shutdown(p);
514 break;
515 case SIGCHLD:
516 case SIGHUP:
517 case SIGPIPE:
518 case SIGUSR1:
519 /* ignore */
520 break;
521 default:
522 fatalx("proc_sig_handler: unexpected signal");
523 /* NOTREACHED */
527 void
528 proc_run(struct privsep *ps, struct privsep_proc *p,
529 struct privsep_proc *procs, unsigned int nproc,
530 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
532 struct passwd *pw;
533 const char *root;
535 log_procinit(p->p_title);
537 /* Set the process group of the current process */
538 setpgid(0, 0);
540 /* Use non-standard user */
541 if (p->p_pw != NULL)
542 pw = p->p_pw;
543 else
544 pw = ps->ps_pw;
546 /* Change root directory */
547 if (p->p_chroot != NULL)
548 root = p->p_chroot;
549 else
550 root = pw->pw_dir;
552 if (chroot(root) == -1)
553 fatal("proc_run: chroot");
554 if (chdir("/") == -1)
555 fatal("proc_run: chdir(\"/\")");
557 privsep_process = p->p_id;
559 setproctitle("%s", p->p_title);
561 if (setgroups(1, &pw->pw_gid) ||
562 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
563 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
564 fatal("proc_run: cannot drop privileges");
566 event_init();
568 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
569 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
570 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
571 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
572 signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
573 signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
575 signal_add(&ps->ps_evsigint, NULL);
576 signal_add(&ps->ps_evsigterm, NULL);
577 signal_add(&ps->ps_evsigchld, NULL);
578 signal_add(&ps->ps_evsighup, NULL);
579 signal_add(&ps->ps_evsigpipe, NULL);
580 signal_add(&ps->ps_evsigusr1, NULL);
582 proc_setup(ps, procs, nproc);
583 proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
585 DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
586 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
588 if (run != NULL)
589 run(ps, p, arg);
591 event_dispatch();
593 proc_shutdown(p);
596 void
597 proc_dispatch(int fd, short event, void *arg)
599 struct imsgev *iev = arg;
600 struct privsep_proc *p = iev->proc;
601 struct privsep *ps = p->p_ps;
602 struct imsgbuf *ibuf;
603 struct imsg imsg;
604 ssize_t n;
605 int verbose;
606 const char *title;
607 struct privsep_fd pf;
609 title = ps->ps_title[privsep_process];
610 ibuf = &iev->ibuf;
612 if (event & EV_READ) {
613 n = imsg_read(ibuf);
614 if (n == -1 && errno != EAGAIN)
615 fatal("%s: imsg_read", __func__);
616 if (n == 0) {
617 /* this pipe is dead, so remove the event handler */
618 event_del(&iev->ev);
619 event_loopexit(NULL);
620 return;
624 if (event & EV_WRITE) {
625 n = msgbuf_write(&ibuf->w);
626 if (n == -1 && errno != EAGAIN)
627 fatal("%s: msgbuf_write", __func__);
628 if (n == 0) {
629 /* this pipe is dead, so remove the event handler */
630 event_del(&iev->ev);
631 event_loopexit(NULL);
632 return;
636 for (;;) {
637 n = imsg_get(ibuf, &imsg);
638 if (n == -1)
639 fatal("%s: imsg_get", __func__);
640 if (n == 0)
641 break;
643 #if DEBUG > 1
644 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
645 __func__, title, ps->ps_instance + 1,
646 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
647 #endif
649 /*
650 * Check the message with the program callback
651 */
652 if ((p->p_cb)(fd, p, &imsg) == 0) {
653 /* Message was handled by the callback, continue */
654 imsg_free(&imsg);
655 continue;
658 /*
659 * Generic message handling
660 */
661 switch (imsg.hdr.type) {
662 case IMSG_CTL_VERBOSE:
663 log_info("%s", __func__);
664 IMSG_SIZE_CHECK(&imsg, &verbose);
665 memcpy(&verbose, imsg.data, sizeof(verbose));
666 log_setverbose(verbose);
667 break;
668 case IMSG_CTL_PROCFD:
669 IMSG_SIZE_CHECK(&imsg, &pf);
670 memcpy(&pf, imsg.data, sizeof(pf));
671 proc_accept(ps, imsg.fd, pf.pf_procid,
672 pf.pf_instance);
673 break;
674 default:
675 log_warnx("%s: %s %d got invalid imsg %d peerid %d "
676 "from %s %d",
677 __func__, title, ps->ps_instance + 1,
678 imsg.hdr.type, imsg.hdr.peerid,
679 p->p_title, imsg.hdr.pid);
681 imsg_free(&imsg);
683 imsg_event_add(iev);
686 int
687 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
689 return (-1);
692 /*
693 * imsg helper functions
694 */
696 void
697 imsg_event_add(struct imsgev *iev)
699 if (iev->handler == NULL) {
700 imsg_flush(&iev->ibuf);
701 return;
704 iev->events = EV_READ;
705 if (iev->ibuf.w.queued)
706 iev->events |= EV_WRITE;
708 event_del(&iev->ev);
709 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
710 event_add(&iev->ev, NULL);
713 int
714 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
715 pid_t pid, int fd, void *data, uint16_t datalen)
717 int ret;
719 ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
720 if (ret == -1)
721 return (ret);
722 imsg_event_add(iev);
723 return (ret);
726 int
727 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
728 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
730 int ret;
732 ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
733 if (ret == -1)
734 return (ret);
735 imsg_event_add(iev);
736 return (ret);
739 void
740 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
742 if (*n == -1) {
743 /* Use a range of all target instances */
744 *n = 0;
745 *m = ps->ps_instances[id];
746 } else {
747 /* Use only a single slot of the specified peer process */
748 *m = *n + 1;
752 int
753 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
754 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
756 int m;
758 proc_range(ps, id, &n, &m);
759 for (; n < m; n++) {
760 if (imsg_compose_event(&ps->ps_ievs[id][n],
761 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
762 return (-1);
765 return (0);
768 int
769 proc_compose(struct privsep *ps, enum privsep_procid id,
770 uint16_t type, void *data, uint16_t datalen)
772 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
775 int
776 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
777 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
779 int m;
781 proc_range(ps, id, &n, &m);
782 for (; n < m; n++)
783 if (imsg_composev_event(&ps->ps_ievs[id][n],
784 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
785 return (-1);
787 return (0);
790 int
791 proc_composev(struct privsep *ps, enum privsep_procid id,
792 uint16_t type, const struct iovec *iov, int iovcnt)
794 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
797 int
798 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
799 enum privsep_procid id, int n)
801 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
802 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
805 struct imsgbuf *
806 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
808 int m;
810 proc_range(ps, id, &n, &m);
811 return (&ps->ps_ievs[id][n].ibuf);
814 struct imsgev *
815 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
817 int m;
819 proc_range(ps, id, &n, &m);
820 return (&ps->ps_ievs[id][n]);
823 /* This function should only be called with care as it breaks async I/O */
824 int
825 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
827 struct imsgbuf *ibuf;
828 int m, ret = 0;
830 proc_range(ps, id, &n, &m);
831 for (; n < m; n++) {
832 ibuf = proc_ibuf(ps, id, n);
833 if (ibuf == NULL)
834 return (-1);
835 do {
836 ret = imsg_flush(ibuf);
837 } while (ret == -1 && errno == EAGAIN);
838 if (ret == -1)
839 break;
840 imsg_event_add(&ps->ps_ievs[id][n]);
843 return (ret);