Blob


1 /* $OpenBSD: proc.c,v 1.53 2026/03/02 19:28:01 rsadowski 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 <sys/types.h>
21 #include <sys/queue.h>
22 #include <sys/socket.h>
23 #include <sys/wait.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 <paths.h>
33 #include <pwd.h>
34 #include <event.h>
35 #include <imsg.h>
37 #include "relayd.h"
38 #include "log.h"
40 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int,
41 char **);
42 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
43 void proc_open(struct privsep *, int, int);
44 void proc_accept(struct privsep *, int, enum privsep_procid,
45 unsigned int);
46 void proc_close(struct privsep *);
47 void proc_shutdown(struct privsep_proc *);
48 void proc_sig_handler(int, short, void *);
49 void proc_range(struct privsep *, enum privsep_procid, int *, int *);
50 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
52 enum privsep_procid
53 proc_getid(struct privsep_proc *procs, unsigned int nproc,
54 const char *proc_name)
55 {
56 struct privsep_proc *p;
57 unsigned int proc;
59 for (proc = 0; proc < nproc; proc++) {
60 p = &procs[proc];
61 if (strcmp(p->p_title, proc_name))
62 continue;
64 return (p->p_id);
65 }
67 return (PROC_MAX);
68 }
70 void
71 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
72 int argc, char **argv)
73 {
74 unsigned int proc, nargc, i, proc_i;
75 char **nargv;
76 struct privsep_proc *p;
77 char num[32];
78 int fd;
80 /* Prepare the new process argv. */
81 nargv = calloc(argc + 5, sizeof(char *));
82 if (nargv == NULL)
83 fatal("%s: calloc", __func__);
85 /* Copy call argument first. */
86 nargc = 0;
87 nargv[nargc++] = argv[0];
89 /* Set process name argument and save the position. */
90 nargv[nargc++] = "-P";
91 proc_i = nargc;
92 nargc++;
94 /* Point process instance arg to stack and copy the original args. */
95 nargv[nargc++] = "-I";
96 nargv[nargc++] = num;
97 for (i = 1; i < (unsigned int) argc; i++)
98 nargv[nargc++] = argv[i];
100 nargv[nargc] = NULL;
102 for (proc = 0; proc < nproc; proc++) {
103 p = &procs[proc];
105 /* Update args with process title. */
106 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
108 /* Fire children processes. */
109 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
110 /* Update the process instance number. */
111 snprintf(num, sizeof(num), "%u", i);
113 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0];
114 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0] = -1;
116 switch (fork()) {
117 case -1:
118 fatal("%s: fork", __func__);
119 break;
120 case 0:
121 /* Prepare parent socket. */
122 if (fd != PROC_PARENT_SOCK_FILENO) {
123 if (dup2(fd, PROC_PARENT_SOCK_FILENO)
124 == -1)
125 fatal("dup2");
126 } else if (fcntl(fd, F_SETFD, 0) == -1)
127 fatal("fcntl");
129 execvp(argv[0], nargv);
130 fatal("%s: execvp", __func__);
131 break;
132 default:
133 /* Close child end. */
134 close(fd);
135 break;
139 free(nargv);
142 void
143 proc_connect(struct privsep *ps)
145 struct imsgev *iev;
146 unsigned int src, dst, inst;
148 /* Don't distribute any sockets if we are not really going to run. */
149 if (ps->ps_noaction)
150 return;
152 for (dst = 0; dst < PROC_MAX; dst++) {
153 /* We don't communicate with ourselves. */
154 if (dst == PROC_PARENT)
155 continue;
157 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
158 iev = &ps->ps_ievs[dst][inst];
159 if (imsgbuf_init(&iev->ibuf,
160 ps->ps_pp->pp_pipes[dst][inst]) == -1)
161 fatal("imsgbuf_init");
162 imsgbuf_allow_fdpass(&iev->ibuf);
163 event_set(&iev->ev, iev->ibuf.fd, iev->events,
164 iev->handler, iev->data);
165 event_add(&iev->ev, NULL);
169 /* Distribute the socketpair()s for everyone. */
170 for (src = 0; src < PROC_MAX; src++)
171 for (dst = src; dst < PROC_MAX; dst++) {
172 /* Parent already distributed its fds. */
173 if (src == PROC_PARENT || dst == PROC_PARENT)
174 continue;
176 proc_open(ps, src, dst);
180 void
181 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
182 int debug, int argc, char **argv, enum privsep_procid proc_id)
184 struct privsep_proc *p = NULL;
185 struct privsep_pipes *pa, *pb;
186 unsigned int proc;
187 unsigned int dst;
188 int fds[2];
190 /* Don't initiate anything if we are not really going to run. */
191 if (ps->ps_noaction)
192 return;
194 if (proc_id == PROC_PARENT) {
195 privsep_process = PROC_PARENT;
196 proc_setup(ps, procs, nproc);
198 if (!debug && daemon(1, 0) == -1)
199 fatal("failed to daemonize");
201 /*
202 * Create the children sockets so we can use them
203 * to distribute the rest of the socketpair()s using
204 * proc_connect() later.
205 */
206 for (dst = 0; dst < PROC_MAX; dst++) {
207 /* Don't create socket for ourselves. */
208 if (dst == PROC_PARENT)
209 continue;
211 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
212 pa = &ps->ps_pipes[PROC_PARENT][0];
213 pb = &ps->ps_pipes[dst][proc];
214 if (socketpair(AF_UNIX,
215 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
216 PF_UNSPEC, fds) == -1)
217 fatal("%s: socketpair", __func__);
219 pa->pp_pipes[dst][proc] = fds[0];
220 pb->pp_pipes[PROC_PARENT][0] = fds[1];
224 /* Engage! */
225 proc_exec(ps, procs, nproc, argc, argv);
226 return;
229 /* Initialize a child */
230 for (proc = 0; proc < nproc; proc++) {
231 if (procs[proc].p_id != proc_id)
232 continue;
233 p = &procs[proc];
234 break;
236 if (p == NULL || p->p_init == NULL)
237 fatalx("%s: process %d missing process initialization",
238 __func__, proc_id);
240 p->p_init(ps, p);
242 fatalx("failed to initiate child process");
245 void
246 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
247 unsigned int n)
249 struct privsep_pipes *pp = ps->ps_pp;
250 struct imsgev *iev;
252 if (ps->ps_ievs[dst] == NULL) {
253 #if DEBUG > 1
254 log_debug("%s: %s src %d %d to dst %d %d not connected",
255 __func__, ps->ps_title[privsep_process],
256 privsep_process, ps->ps_instance + 1,
257 dst, n + 1);
258 #endif
259 close(fd);
260 return;
263 if (pp->pp_pipes[dst][n] != -1) {
264 log_warnx("%s: duplicated descriptor", __func__);
265 close(fd);
266 return;
267 } else
268 pp->pp_pipes[dst][n] = fd;
270 iev = &ps->ps_ievs[dst][n];
271 if (imsgbuf_init(&iev->ibuf, fd) == -1)
272 fatal("imsgbuf_init");
273 imsgbuf_allow_fdpass(&iev->ibuf);
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_PARENT] = "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 if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
300 sizeof(struct imsgev))) == NULL)
301 fatal("%s: calloc", __func__);
303 /* With this set up, we are ready to call imsgbuf_init(). */
304 for (i = 0; i < ps->ps_instances[id]; i++) {
305 ps->ps_ievs[id][i].handler = proc_dispatch;
306 ps->ps_ievs[id][i].events = EV_READ;
307 ps->ps_ievs[id][i].proc = &procs[src];
308 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
312 /*
313 * Allocate pipes for all process instances (incl. parent)
315 * - ps->ps_pipes: N:M mapping
316 * N source processes connected to M destination processes:
317 * [src][instances][dst][instances], for example
318 * [PROC_RELAY][3][PROC_CA][3]
320 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
321 * Each process instance has a destination array of socketpair fds:
322 * [dst][instances], for example
323 * [PROC_PARENT][0]
324 */
325 for (src = 0; src < PROC_MAX; src++) {
326 /* Allocate destination array for each process */
327 if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
328 sizeof(struct privsep_pipes))) == NULL)
329 fatal("%s: calloc", __func__);
331 for (i = 0; i < ps->ps_instances[src]; i++) {
332 pp = &ps->ps_pipes[src][i];
334 for (dst = 0; dst < PROC_MAX; dst++) {
335 /* Allocate maximum fd integers */
336 if ((pp->pp_pipes[dst] =
337 calloc(ps->ps_instances[dst],
338 sizeof(int))) == NULL)
339 fatal("%s: calloc", __func__);
341 /* Mark fd as unused */
342 for (j = 0; j < ps->ps_instances[dst]; j++)
343 pp->pp_pipes[dst][j] = -1;
348 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
351 void
352 proc_kill(struct privsep *ps)
354 char *cause;
355 pid_t pid;
356 int len, status;
358 if (privsep_process != PROC_PARENT)
359 return;
361 proc_close(ps);
363 do {
364 pid = waitpid(WAIT_ANY, &status, 0);
365 if (pid <= 0)
366 continue;
368 if (WIFSIGNALED(status)) {
369 len = asprintf(&cause, "terminated; signal %d",
370 WTERMSIG(status));
371 } else if (WIFEXITED(status)) {
372 if (WEXITSTATUS(status) != 0)
373 len = asprintf(&cause, "exited abnormally");
374 else
375 len = 0;
376 } else
377 len = -1;
379 if (len == 0) {
380 /* child exited OK, don't print a warning message */
381 } else if (len != -1) {
382 log_warnx("lost child: pid %u %s", pid, cause);
383 free(cause);
384 } else
385 log_warnx("lost child: pid %u", pid);
386 } while (pid != -1 || (pid == -1 && errno == EINTR));
389 void
390 proc_open(struct privsep *ps, int src, int dst)
392 struct privsep_pipes *pa, *pb;
393 struct privsep_fd pf;
394 int fds[2];
395 unsigned int i, j;
397 /* Exchange pipes between process. */
398 for (i = 0; i < ps->ps_instances[src]; i++) {
399 for (j = 0; j < ps->ps_instances[dst]; j++) {
400 /* Don't create sockets for ourself. */
401 if (src == dst && i == j)
402 continue;
404 /* No need for CA to CA or RELAY to RELAY sockets. */
405 if ((src == PROC_CA && dst == PROC_CA) ||
406 (src == PROC_RELAY && dst == PROC_RELAY))
407 continue;
409 pa = &ps->ps_pipes[src][i];
410 pb = &ps->ps_pipes[dst][j];
411 if (socketpair(AF_UNIX,
412 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC,
413 PF_UNSPEC, fds) == -1)
414 fatal("%s: socketpair", __func__);
416 pa->pp_pipes[dst][j] = fds[0];
417 pb->pp_pipes[src][i] = fds[1];
419 pf.pf_procid = src;
420 pf.pf_instance = i;
421 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
422 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
423 fatal("%s: proc_compose_imsg", __func__);
425 pf.pf_procid = dst;
426 pf.pf_instance = j;
427 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
428 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
429 fatal("%s: proc_compose_imsg", __func__);
431 /*
432 * We have to flush to send the descriptors and close
433 * them to avoid the fd ramp on startup.
434 */
435 if (proc_flush_imsg(ps, src, i) == -1 ||
436 proc_flush_imsg(ps, dst, j) == -1)
437 fatal("%s: proc_flush_imsg", __func__);
442 void
443 proc_close(struct privsep *ps)
445 unsigned int dst, n;
446 struct privsep_pipes *pp;
448 if (ps == NULL)
449 return;
451 pp = ps->ps_pp;
453 for (dst = 0; dst < PROC_MAX; dst++) {
454 if (ps->ps_ievs[dst] == NULL)
455 continue;
457 for (n = 0; n < ps->ps_instances[dst]; n++) {
458 if (pp->pp_pipes[dst][n] == -1)
459 continue;
461 /* Cancel the fd, close and invalidate the fd */
462 event_del(&(ps->ps_ievs[dst][n].ev));
463 imsgbuf_clear(&(ps->ps_ievs[dst][n].ibuf));
464 close(pp->pp_pipes[dst][n]);
465 pp->pp_pipes[dst][n] = -1;
467 free(ps->ps_ievs[dst]);
471 void
472 proc_shutdown(struct privsep_proc *p)
474 struct privsep *ps = p->p_ps;
476 if (p->p_id == PROC_CONTROL && ps)
477 control_cleanup(&ps->ps_csock);
479 if (p->p_shutdown != NULL)
480 (*p->p_shutdown)();
482 proc_close(ps);
484 log_info("%s exiting, pid %d", p->p_title, getpid());
486 exit(0);
489 void
490 proc_sig_handler(int sig, short event, void *arg)
492 struct privsep_proc *p = arg;
494 switch (sig) {
495 case SIGINT:
496 case SIGTERM:
497 proc_shutdown(p);
498 break;
499 case SIGCHLD:
500 case SIGHUP:
501 case SIGPIPE:
502 case SIGUSR1:
503 /* ignore */
504 break;
505 default:
506 fatalx("%s: unexpected signal", __func__);
507 /* NOTREACHED */
511 void
512 proc_run(struct privsep *ps, struct privsep_proc *p,
513 struct privsep_proc *procs, unsigned int nproc,
514 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
516 struct passwd *pw;
517 const char *root;
518 struct control_sock *rcs;
520 log_procinit(p->p_title);
522 if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) {
523 if (control_init(ps, &ps->ps_csock) == -1)
524 fatalx("%s: control_init", __func__);
525 TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry)
526 if (control_init(ps, rcs) == -1)
527 fatalx("%s: control_init", __func__);
530 /* Use non-standard user */
531 if (p->p_pw != NULL)
532 pw = p->p_pw;
533 else
534 pw = ps->ps_pw;
536 /* Change root directory */
537 if (p->p_chroot != NULL)
538 root = p->p_chroot;
539 else
540 root = pw->pw_dir;
542 if (chroot(root) == -1)
543 fatal("%s: chroot", __func__);
544 if (chdir("/") == -1)
545 fatal("%s: chdir(\"/\")", __func__);
547 privsep_process = p->p_id;
549 setproctitle("%s", p->p_title);
551 if (setgroups(1, &pw->pw_gid) ||
552 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
553 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
554 fatal("%s: cannot drop privileges", __func__);
556 event_init();
558 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
559 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
560 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
561 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
562 signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
563 signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
565 signal_add(&ps->ps_evsigint, NULL);
566 signal_add(&ps->ps_evsigterm, NULL);
567 signal_add(&ps->ps_evsigchld, NULL);
568 signal_add(&ps->ps_evsighup, NULL);
569 signal_add(&ps->ps_evsigpipe, NULL);
570 signal_add(&ps->ps_evsigusr1, NULL);
572 proc_setup(ps, procs, nproc);
573 proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
574 if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) {
575 if (control_listen(&ps->ps_csock) == -1)
576 fatalx("%s: control_listen", __func__);
577 TAILQ_FOREACH(rcs, &ps->ps_rcsocks, cs_entry)
578 if (control_listen(rcs) == -1)
579 fatalx("%s: control_listen", __func__);
582 DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
583 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
585 if (run != NULL)
586 run(ps, p, arg);
588 event_dispatch();
590 proc_shutdown(p);
593 void
594 proc_dispatch(int fd, short event, void *arg)
596 struct imsgev *iev = arg;
597 struct privsep_proc *p = iev->proc;
598 struct privsep *ps = p->p_ps;
599 struct imsgbuf *ibuf;
600 struct imsg imsg;
601 ssize_t n;
602 int verbose;
603 const char *title;
604 struct privsep_fd pf;
606 title = ps->ps_title[privsep_process];
607 ibuf = &iev->ibuf;
609 if (event & EV_READ) {
610 if ((n = imsgbuf_read(ibuf)) == -1)
611 fatal("%s: imsgbuf_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 if (imsgbuf_write(ibuf) == -1) {
622 if (errno == EPIPE) {
623 /* this pipe is dead, remove the handler */
624 event_del(&iev->ev);
625 event_loopexit(NULL);
626 return;
628 fatal("%s: imsgbuf_write", __func__);
632 for (;;) {
633 if ((n = imsg_get(ibuf, &imsg)) == -1)
634 fatal("%s: imsg_get", __func__);
635 if (n == 0)
636 break;
638 #if DEBUG > 1
639 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
640 __func__, title, ps->ps_instance + 1,
641 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
642 #endif
644 /*
645 * Check the message with the program callback
646 */
647 if ((p->p_cb)(fd, p, &imsg) == 0) {
648 /* Message was handled by the callback, continue */
649 imsg_free(&imsg);
650 continue;
653 /*
654 * Generic message handling
655 */
656 switch (imsg.hdr.type) {
657 case IMSG_CTL_VERBOSE:
658 IMSG_SIZE_CHECK(&imsg, &verbose);
659 memcpy(&verbose, imsg.data, sizeof(verbose));
660 log_setverbose(verbose);
661 break;
662 case IMSG_CTL_PROCFD:
663 IMSG_SIZE_CHECK(&imsg, &pf);
664 memcpy(&pf, imsg.data, sizeof(pf));
665 proc_accept(ps, imsg_get_fd(&imsg), pf.pf_procid,
666 pf.pf_instance);
667 break;
668 default:
669 fatalx("%s: %s %d got invalid imsg %d peerid %d "
670 "from %s %d",
671 __func__, title, ps->ps_instance + 1,
672 imsg.hdr.type, imsg.hdr.peerid,
673 p->p_title, imsg.hdr.pid);
675 imsg_free(&imsg);
677 imsg_event_add(iev);
680 int
681 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
683 return (-1);
686 /*
687 * imsg helper functions
688 */
690 void
691 imsg_event_add(struct imsgev *iev)
693 if (iev->handler == NULL) {
694 imsgbuf_flush(&iev->ibuf);
695 return;
698 iev->events = EV_READ;
699 if (imsgbuf_queuelen(&iev->ibuf) > 0)
700 iev->events |= EV_WRITE;
702 event_del(&iev->ev);
703 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
704 event_add(&iev->ev, NULL);
707 int
708 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
709 pid_t pid, int fd, void *data, uint16_t datalen)
711 int ret;
713 if ((ret = imsg_compose(&iev->ibuf, type, peerid,
714 pid, fd, data, datalen)) == -1)
715 return (ret);
716 imsg_event_add(iev);
717 return (ret);
720 int
721 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
722 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
724 int ret;
726 if ((ret = imsg_composev(&iev->ibuf, type, peerid,
727 pid, fd, iov, iovcnt)) == -1)
728 return (ret);
729 imsg_event_add(iev);
730 return (ret);
733 void
734 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
736 if (*n == -1) {
737 /* Use a range of all target instances */
738 *n = 0;
739 *m = ps->ps_instances[id];
740 } else {
741 /* Use only a single slot of the specified peer process */
742 *m = *n + 1;
746 int
747 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
748 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
750 int m;
752 proc_range(ps, id, &n, &m);
753 for (; n < m; n++) {
754 if (imsg_compose_event(&ps->ps_ievs[id][n],
755 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
756 return (-1);
759 return (0);
762 int
763 proc_compose(struct privsep *ps, enum privsep_procid id,
764 uint16_t type, void *data, uint16_t datalen)
766 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
769 int
770 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
771 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
773 int m;
775 proc_range(ps, id, &n, &m);
776 for (; n < m; n++)
777 if (imsg_composev_event(&ps->ps_ievs[id][n],
778 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
779 return (-1);
781 return (0);
784 int
785 proc_composev(struct privsep *ps, enum privsep_procid id,
786 uint16_t type, const struct iovec *iov, int iovcnt)
788 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
791 int
792 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
793 enum privsep_procid id, int n)
795 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
796 imsg->hdr.peerid, -1, imsg->data, IMSG_DATA_SIZE(imsg)));
799 struct imsgbuf *
800 proc_ibuf(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].ibuf);
808 struct imsgev *
809 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
811 int m;
813 proc_range(ps, id, &n, &m);
814 return (&ps->ps_ievs[id][n]);
817 /* This function should only be called with care as it breaks async I/O */
818 int
819 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
821 struct imsgbuf *ibuf;
822 int m, ret = 0;
824 proc_range(ps, id, &n, &m);
825 for (; n < m; n++) {
826 if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
827 return (-1);
828 if ((ret = imsgbuf_flush(ibuf)) == -1)
829 break;
830 imsg_event_add(&ps->ps_ievs[id][n]);
833 return (ret);