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 "gmid.h"
22 #include <sys/types.h>
23 /* #include <sys/queue.h> XXX provided by gmid.h */
24 /* #include <sys/tree.h> XXX provided by gmid.h */
25 #include <sys/socket.h>
26 #include <sys/wait.h>
28 #include <fcntl.h>
29 #include <grp.h>
30 #include <limits.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <signal.h>
37 #include <paths.h>
38 #include <pwd.h>
39 /* #include <event.h> XXX provided by gmid.h */
40 /* #include <imsg.h> XXX provided by gmid.h */
42 #include "log.h"
43 #include "proc.h"
45 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int,
46 int, char **);
47 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
48 void proc_open(struct privsep *, int, int);
49 void proc_accept(struct privsep *, int, enum privsep_procid,
50 unsigned int);
51 void proc_close(struct privsep *);
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 enum privsep_procid
58 proc_getid(struct privsep_proc *procs, unsigned int nproc,
59 const char *proc_name)
60 {
61 struct privsep_proc *p;
62 unsigned int proc;
64 for (proc = 0; proc < nproc; proc++) {
65 p = &procs[proc];
66 if (strcmp(p->p_title, proc_name))
67 continue;
69 return (p->p_id);
70 }
72 return (PROC_MAX);
73 }
75 void
76 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
77 int debug, int argc, char **argv)
78 {
79 unsigned int proc, nargc, i, proc_i, proc_X = 0;
80 const char **nargv;
81 struct privsep_proc *p;
82 char num[32];
83 int fd;
85 /* Prepare the new process argv. */
86 nargv = calloc(argc + 9, sizeof(char *));
87 if (nargv == NULL)
88 fatal("%s: calloc", __func__);
90 /* Copy call argument first. */
91 nargc = 0;
92 nargv[nargc++] = argv[0];
94 /* Set process name argument and save the position. */
95 nargv[nargc++] = "-T";
96 proc_i = nargc;
97 nargc++;
99 /* Set user and chroot */
100 if (ps->ps_pw != NULL) {
101 nargv[nargc++] = "-U";
102 nargv[nargc++] = ps->ps_pw->pw_name;
104 nargv[nargc++] = "-X";
105 proc_X = nargc;
106 nargc++;
109 /* Point process instance arg to stack and copy the original args. */
110 nargv[nargc++] = "-I";
111 nargv[nargc++] = num;
112 for (i = 1; i < (unsigned int) argc; i++)
113 nargv[nargc++] = argv[i];
115 nargv[nargc] = NULL;
117 for (proc = 0; proc < nproc; proc++) {
118 p = &procs[proc];
120 /* Update args with process title and chroot. */
121 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
122 if (proc_X)
123 nargv[proc_X] = p->p_chroot;
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 /* obnoxious casts */
161 execvp(argv[0], (char *const *)nargv);
162 fatal("%s: execvp", __func__);
163 break;
164 default:
165 /* Close child end. */
166 close(fd);
167 break;
171 free(nargv);
174 void
175 proc_connect(struct privsep *ps)
177 struct imsgev *iev;
178 unsigned int src, dst, inst;
180 /* Don't distribute any sockets if we are not really going to run. */
181 if (ps->ps_noaction)
182 return;
184 for (dst = 0; dst < PROC_MAX; dst++) {
185 /* We don't communicate with ourselves. */
186 if (dst == PROC_PARENT)
187 continue;
189 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
190 iev = &ps->ps_ievs[dst][inst];
191 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
192 event_set(&iev->ev, iev->ibuf.fd, iev->events,
193 iev->handler, iev->data);
194 event_add(&iev->ev, NULL);
198 /* Distribute the socketpair()s for everyone. */
199 for (src = 0; src < PROC_MAX; src++)
200 for (dst = src; dst < PROC_MAX; dst++) {
201 /* Parent already distributed its fds. */
202 if (src == PROC_PARENT || dst == PROC_PARENT)
203 continue;
205 proc_open(ps, src, dst);
209 void
210 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
211 int debug, int argc, char **argv, enum privsep_procid proc_id)
213 struct privsep_proc *p = NULL;
214 struct privsep_pipes *pa, *pb;
215 unsigned int proc;
216 unsigned int dst;
217 int fds[2];
219 /* Don't initiate anything if we are not really going to run. */
220 if (ps->ps_noaction)
221 return;
223 if (proc_id == PROC_PARENT) {
224 privsep_process = PROC_PARENT;
225 proc_setup(ps, procs, nproc);
227 /*
228 * Create the children sockets so we can use them
229 * to distribute the rest of the socketpair()s using
230 * proc_connect() later.
231 */
232 for (dst = 0; dst < PROC_MAX; dst++) {
233 /* Don't create socket for ourselves. */
234 if (dst == PROC_PARENT)
235 continue;
237 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
238 pa = &ps->ps_pipes[PROC_PARENT][0];
239 pb = &ps->ps_pipes[dst][proc];
240 if (socketpair(AF_UNIX,
241 SOCK_STREAM,
242 PF_UNSPEC, fds) == -1)
243 fatal("%s: socketpair", __func__);
245 mark_nonblock(fds[0]);
246 mark_nonblock(fds[1]);
247 if (fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
248 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1)
249 fatal("%s: fcntl F_SETFD", __func__);
251 pa->pp_pipes[dst][proc] = fds[0];
252 pb->pp_pipes[PROC_PARENT][0] = fds[1];
256 /* Engage! */
257 proc_exec(ps, procs, nproc, debug, argc, argv);
258 return;
261 /* Initialize a child */
262 for (proc = 0; proc < nproc; proc++) {
263 if (procs[proc].p_id != proc_id)
264 continue;
265 p = &procs[proc];
266 break;
268 if (p == NULL || p->p_init == NULL)
269 fatalx("%s: process %d missing process initialization",
270 __func__, proc_id);
272 p->p_init(ps, p);
274 fatalx("failed to initiate child process");
277 void
278 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
279 unsigned int n)
281 struct privsep_pipes *pp = ps->ps_pp;
282 struct imsgev *iev;
284 if (ps->ps_ievs[dst] == NULL) {
285 #if DEBUG > 1
286 log_debug("%s: %s src %d %d to dst %d %d not connected",
287 __func__, ps->ps_title[privsep_process],
288 privsep_process, ps->ps_instance + 1,
289 dst, n + 1);
290 #endif
291 close(fd);
292 return;
295 if (pp->pp_pipes[dst][n] != -1) {
296 log_warnx("%s: duplicated descriptor", __func__);
297 close(fd);
298 return;
299 } else
300 pp->pp_pipes[dst][n] = fd;
302 iev = &ps->ps_ievs[dst][n];
303 imsg_init(&iev->ibuf, fd);
304 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
305 event_add(&iev->ev, NULL);
308 void
309 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
311 unsigned int i, j, src, dst, id;
312 struct privsep_pipes *pp;
314 /* Initialize parent title, ps_instances and procs. */
315 ps->ps_title[PROC_PARENT] = "parent";
317 for (src = 0; src < PROC_MAX; src++)
318 /* Default to 1 process instance */
319 if (ps->ps_instances[src] < 1)
320 ps->ps_instances[src] = 1;
322 for (src = 0; src < nproc; src++) {
323 procs[src].p_ps = ps;
324 if (procs[src].p_cb == NULL)
325 procs[src].p_cb = proc_dispatch_null;
327 id = procs[src].p_id;
328 ps->ps_title[id] = procs[src].p_title;
329 if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
330 sizeof(struct imsgev))) == NULL)
331 fatal("%s: calloc", __func__);
333 /* With this set up, we are ready to call imsg_init(). */
334 for (i = 0; i < ps->ps_instances[id]; i++) {
335 ps->ps_ievs[id][i].handler = proc_dispatch;
336 ps->ps_ievs[id][i].events = EV_READ;
337 ps->ps_ievs[id][i].proc = &procs[src];
338 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
342 /*
343 * Allocate pipes for all process instances (incl. parent)
345 * - ps->ps_pipes: N:M mapping
346 * N source processes connected to M destination processes:
347 * [src][instances][dst][instances], for example
348 * [PROC_RELAY][3][PROC_CA][3]
350 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
351 * Each process instance has a destination array of socketpair fds:
352 * [dst][instances], for example
353 * [PROC_PARENT][0]
354 */
355 for (src = 0; src < PROC_MAX; src++) {
356 /* Allocate destination array for each process */
357 if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
358 sizeof(struct privsep_pipes))) == NULL)
359 fatal("%s: calloc", __func__);
361 for (i = 0; i < ps->ps_instances[src]; i++) {
362 pp = &ps->ps_pipes[src][i];
364 for (dst = 0; dst < PROC_MAX; dst++) {
365 /* Allocate maximum fd integers */
366 if ((pp->pp_pipes[dst] =
367 calloc(ps->ps_instances[dst],
368 sizeof(int))) == NULL)
369 fatal("%s: calloc", __func__);
371 /* Mark fd as unused */
372 for (j = 0; j < ps->ps_instances[dst]; j++)
373 pp->pp_pipes[dst][j] = -1;
378 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
381 void
382 proc_kill(struct privsep *ps)
384 char *cause;
385 pid_t pid;
386 int len, status;
388 if (privsep_process != PROC_PARENT)
389 return;
391 proc_close(ps);
393 do {
394 pid = waitpid(WAIT_ANY, &status, 0);
395 if (pid <= 0)
396 continue;
398 if (WIFSIGNALED(status)) {
399 len = asprintf(&cause, "terminated; signal %d",
400 WTERMSIG(status));
401 } else if (WIFEXITED(status)) {
402 if (WEXITSTATUS(status) != 0)
403 len = asprintf(&cause, "exited abnormally");
404 else
405 len = 0;
406 } else
407 len = -1;
409 if (len == 0) {
410 /* child exited OK, don't print a warning message */
411 } else if (len != -1) {
412 log_warnx("lost child: pid %u %s", pid, cause);
413 free(cause);
414 } else
415 log_warnx("lost child: pid %u", pid);
416 } while (pid != -1 || errno == EINTR);
419 void
420 proc_open(struct privsep *ps, int src, int dst)
422 struct privsep_pipes *pa, *pb;
423 struct privsep_fd pf;
424 int fds[2];
425 unsigned int i, j;
427 /* Exchange pipes between process. */
428 for (i = 0; i < ps->ps_instances[src]; i++) {
429 for (j = 0; j < ps->ps_instances[dst]; j++) {
430 /* Don't create sockets for ourself. */
431 if (src == dst && i == j)
432 continue;
434 /* Servers don't talk to each other. */
435 if (src == PROC_SERVER && dst == PROC_SERVER)
436 continue;
438 pa = &ps->ps_pipes[src][i];
439 pb = &ps->ps_pipes[dst][j];
440 if (socketpair(AF_UNIX,
441 SOCK_STREAM,
442 PF_UNSPEC, fds) == -1)
443 fatal("%s: socketpair", __func__);
445 mark_nonblock(fds[0]);
446 mark_nonblock(fds[1]);
447 if (fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
448 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1)
449 fatal("%s: fcntl F_SETFD", __func__);
451 pa->pp_pipes[dst][j] = fds[0];
452 pb->pp_pipes[src][i] = fds[1];
454 pf.pf_procid = src;
455 pf.pf_instance = i;
456 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
457 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
458 fatal("%s: proc_compose_imsg", __func__);
460 pf.pf_procid = dst;
461 pf.pf_instance = j;
462 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
463 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
464 fatal("%s: proc_compose_imsg", __func__);
466 /*
467 * We have to flush to send the descriptors and close
468 * them to avoid the fd ramp on startup.
469 */
470 if (proc_flush_imsg(ps, src, i) == -1 ||
471 proc_flush_imsg(ps, dst, j) == -1)
472 fatal("%s: imsg_flush", __func__);
477 void
478 proc_close(struct privsep *ps)
480 unsigned int dst, n;
481 struct privsep_pipes *pp;
483 if (ps == NULL)
484 return;
486 pp = ps->ps_pp;
488 for (dst = 0; dst < PROC_MAX; dst++) {
489 if (ps->ps_ievs[dst] == NULL)
490 continue;
492 for (n = 0; n < ps->ps_instances[dst]; n++) {
493 if (pp->pp_pipes[dst][n] == -1)
494 continue;
496 /* Cancel the fd, close and invalidate the fd */
497 event_del(&(ps->ps_ievs[dst][n].ev));
498 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
499 close(pp->pp_pipes[dst][n]);
500 pp->pp_pipes[dst][n] = -1;
502 free(ps->ps_ievs[dst]);
506 void
507 proc_shutdown(struct privsep_proc *p)
509 struct privsep *ps = p->p_ps;
511 if (p->p_shutdown != NULL)
512 (*p->p_shutdown)();
514 proc_close(ps);
516 log_info("%s exiting, pid %d", p->p_title, getpid());
518 exit(0);
521 void
522 proc_sig_handler(int sig, short event, void *arg)
524 struct privsep_proc *p = arg;
526 switch (sig) {
527 case SIGINT:
528 case SIGTERM:
529 proc_shutdown(p);
530 break;
531 case SIGCHLD:
532 case SIGHUP:
533 /* ignore */
534 break;
535 default:
536 fatalx("%s: unexpected signal", __func__);
537 /* NOTREACHED */
541 void
542 proc_run(struct privsep *ps, struct privsep_proc *p,
543 struct privsep_proc *procs, unsigned int nproc,
544 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
546 struct passwd *pw;
547 const char *root;
549 log_procinit(p->p_title);
550 setproctitle("%s", p->p_title);
552 privsep_process = p->p_id;
554 if (ps->ps_pw == NULL)
555 goto init;
557 /* Set the process group of the current process */
558 setpgid(0, 0);
560 /* Use non-standard user */
561 if (p->p_pw != NULL)
562 pw = p->p_pw;
563 else
564 pw = ps->ps_pw;
566 /* Change root directory */
567 if (p->p_chroot != NULL)
568 root = p->p_chroot;
569 else
570 root = pw->pw_dir;
572 if (chroot(root) == -1)
573 fatal("%s: chroot", __func__);
574 if (chdir("/") == -1)
575 fatal("%s: chdir(\"/\")", __func__);
577 if (setgroups(1, &pw->pw_gid) ||
578 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
579 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
580 fatal("%s: cannot drop privileges", __func__);
582 init:
583 event_init();
585 signal(SIGPIPE, SIG_IGN);
587 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
588 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
589 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
590 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
592 signal_add(&ps->ps_evsigint, NULL);
593 signal_add(&ps->ps_evsigterm, NULL);
594 signal_add(&ps->ps_evsigchld, NULL);
595 signal_add(&ps->ps_evsighup, NULL);
597 proc_setup(ps, procs, nproc);
598 proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
600 log_debug("%s: %s %d/%d, pid %d", __func__, p->p_title,
601 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
603 if (run != NULL)
604 run(ps, p, arg);
606 event_dispatch();
608 proc_shutdown(p);
611 void
612 proc_dispatch(int fd, short event, void *arg)
614 struct imsgev *iev = arg;
615 struct privsep_proc *p = iev->proc;
616 struct privsep *ps = p->p_ps;
617 struct imsgbuf *ibuf;
618 struct imsg imsg;
619 ssize_t n;
620 const char *title;
621 struct privsep_fd pf;
623 title = ps->ps_title[privsep_process];
624 ibuf = &iev->ibuf;
626 if (event & EV_READ) {
627 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
628 fatal("%s: imsg_read", __func__);
629 if (n == 0) {
630 /* this pipe is dead, so remove the event handler */
631 event_del(&iev->ev);
632 event_loopexit(NULL);
633 return;
637 if (event & EV_WRITE) {
638 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
639 fatal("%s: msgbuf_write", __func__);
640 if (n == 0) {
641 /* this pipe is dead, so remove the event handler */
642 event_del(&iev->ev);
643 event_loopexit(NULL);
644 return;
648 for (;;) {
649 if ((n = imsg_get(ibuf, &imsg)) == -1)
650 fatal("%s: imsg_get", __func__);
651 if (n == 0)
652 break;
654 #if DEBUG > 1
655 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
656 __func__, title, ps->ps_instance + 1,
657 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
658 #endif
660 /*
661 * Check the message with the program callback
662 */
663 if ((p->p_cb)(fd, p, &imsg) == 0) {
664 /* Message was handled by the callback, continue */
665 imsg_free(&imsg);
666 continue;
669 /*
670 * Generic message handling
671 */
672 switch (imsg.hdr.type) {
673 case IMSG_CTL_PROCFD:
674 if (imsg_get_data(&imsg, &pf, sizeof(pf)))
675 fatalx("bad length imsg CTL_PROCFD");
676 proc_accept(ps, imsg_get_fd(&imsg), pf.pf_procid,
677 pf.pf_instance);
678 break;
679 default:
680 fatalx("%s: %s %d got invalid imsg %d peerid %d "
681 "from %s %d",
682 __func__, title, ps->ps_instance + 1,
683 imsg.hdr.type, imsg.hdr.peerid,
684 p->p_title, imsg.hdr.pid);
686 imsg_free(&imsg);
688 imsg_event_add(iev);
691 int
692 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
694 return (-1);
697 /*
698 * imsg helper functions
699 */
701 void
702 imsg_event_add(struct imsgev *iev)
704 if (iev->handler == NULL) {
705 imsg_flush(&iev->ibuf);
706 return;
709 iev->events = EV_READ;
710 if (iev->ibuf.w.queued)
711 iev->events |= EV_WRITE;
713 event_del(&iev->ev);
714 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
715 event_add(&iev->ev, NULL);
718 int
719 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
720 pid_t pid, int fd, void *data, uint16_t datalen)
722 int ret;
724 if ((ret = imsg_compose(&iev->ibuf, type, peerid,
725 pid, fd, data, datalen)) == -1)
726 return (ret);
727 imsg_event_add(iev);
728 return (ret);
731 int
732 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
733 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
735 int ret;
737 if ((ret = imsg_composev(&iev->ibuf, type, peerid,
738 pid, fd, iov, iovcnt)) == -1)
739 return (ret);
740 imsg_event_add(iev);
741 return (ret);
744 void
745 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
747 if (*n == -1) {
748 /* Use a range of all target instances */
749 *n = 0;
750 *m = ps->ps_instances[id];
751 } else {
752 /* Use only a single slot of the specified peer process */
753 *m = *n + 1;
757 int
758 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
759 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
761 int m;
763 proc_range(ps, id, &n, &m);
764 for (; n < m; n++) {
765 if (imsg_compose_event(&ps->ps_ievs[id][n],
766 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
767 return (-1);
770 return (0);
773 int
774 proc_compose(struct privsep *ps, enum privsep_procid id,
775 uint16_t type, void *data, uint16_t datalen)
777 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
780 int
781 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
782 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
784 int m;
786 proc_range(ps, id, &n, &m);
787 for (; n < m; n++)
788 if (imsg_composev_event(&ps->ps_ievs[id][n],
789 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
790 return (-1);
792 return (0);
795 int
796 proc_composev(struct privsep *ps, enum privsep_procid id,
797 uint16_t type, const struct iovec *iov, int iovcnt)
799 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
802 struct imsgbuf *
803 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
805 int m;
807 proc_range(ps, id, &n, &m);
808 return (&ps->ps_ievs[id][n].ibuf);
811 struct imsgev *
812 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
814 int m;
816 proc_range(ps, id, &n, &m);
817 return (&ps->ps_ievs[id][n]);
820 /* This function should only be called with care as it breaks async I/O */
821 int
822 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
824 struct imsgbuf *ibuf;
825 int m, ret = 0;
827 proc_range(ps, id, &n, &m);
828 for (; n < m; n++) {
829 if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
830 return (-1);
832 do {
833 ret = imsg_flush(ibuf);
834 } while (ret == -1 && errno == EAGAIN);
835 if (ret == -1)
836 break;
837 imsg_event_add(&ps->ps_ievs[id][n]);
840 return (ret);