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], prefork[32];
83 int fd;
85 /* Prepare the new process argv. */
86 nargv = calloc(argc + 11, sizeof(char *));
87 if (nargv == NULL)
88 fatal("%s: calloc", __func__);
90 /* Update prefork number */
91 snprintf(prefork, sizeof(prefork), "%d", ps->ps_instances[PROC_SERVER]);
93 /* Copy call argument first. */
94 nargc = 0;
95 nargv[nargc++] = argv[0];
97 /* Set process name argument and save the position. */
98 nargv[nargc++] = "-T";
99 proc_i = nargc;
100 nargc++;
102 /* Set user and chroot */
103 if (ps->ps_pw != NULL) {
104 nargv[nargc++] = "-U";
105 nargv[nargc++] = ps->ps_pw->pw_name;
107 nargv[nargc++] = "-X";
108 proc_X = nargc;
109 nargc++;
112 /* Set server prefork number */
113 nargv[nargc++] = "-J";
114 nargv[nargc++] = prefork;
116 /* Point process instance arg to stack and copy the original args. */
117 nargv[nargc++] = "-I";
118 nargv[nargc++] = num;
119 for (i = 1; i < (unsigned int) argc; i++)
120 nargv[nargc++] = argv[i];
122 nargv[nargc] = NULL;
124 for (proc = 0; proc < nproc; proc++) {
125 p = &procs[proc];
127 /* Update args with process title and chroot. */
128 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
129 if (proc_X)
130 nargv[proc_X] = p->p_chroot;
132 /* Fire children processes. */
133 for (i = 0; i < ps->ps_instances[p->p_id]; i++) {
134 /* Update the process instance number. */
135 snprintf(num, sizeof(num), "%u", i);
137 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0];
138 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0] = -1;
140 switch (fork()) {
141 case -1:
142 fatal("%s: fork", __func__);
143 break;
144 case 0:
145 /* First create a new session */
146 if (setsid() == -1)
147 fatal("setsid");
149 /* Prepare parent socket. */
150 if (fd != PROC_PARENT_SOCK_FILENO) {
151 if (dup2(fd, PROC_PARENT_SOCK_FILENO)
152 == -1)
153 fatal("dup2");
154 } else if (fcntl(fd, F_SETFD, 0) == -1)
155 fatal("fcntl");
157 /* Daemons detach from terminal. */
158 if (!debug && (fd =
159 open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
160 (void)dup2(fd, STDIN_FILENO);
161 (void)dup2(fd, STDOUT_FILENO);
162 (void)dup2(fd, STDERR_FILENO);
163 if (fd > 2)
164 (void)close(fd);
167 /* obnoxious casts */
168 execvp(argv[0], (char *const *)nargv);
169 fatal("%s: execvp", __func__);
170 break;
171 default:
172 /* Close child end. */
173 close(fd);
174 break;
178 free(nargv);
181 void
182 proc_connect(struct privsep *ps)
184 struct imsgev *iev;
185 unsigned int src, dst, inst;
187 /* Don't distribute any sockets if we are not really going to run. */
188 if (ps->ps_noaction)
189 return;
191 for (dst = 0; dst < PROC_MAX; dst++) {
192 /* We don't communicate with ourselves. */
193 if (dst == PROC_PARENT)
194 continue;
196 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
197 iev = &ps->ps_ievs[dst][inst];
198 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
199 event_set(&iev->ev, iev->ibuf.fd, iev->events,
200 iev->handler, iev->data);
201 event_add(&iev->ev, NULL);
205 /* Distribute the socketpair()s for everyone. */
206 for (src = 0; src < PROC_MAX; src++)
207 for (dst = src; dst < PROC_MAX; dst++) {
208 /* Parent already distributed its fds. */
209 if (src == PROC_PARENT || dst == PROC_PARENT)
210 continue;
212 proc_open(ps, src, dst);
216 void
217 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
218 int debug, int argc, char **argv, enum privsep_procid proc_id)
220 struct privsep_proc *p = NULL;
221 struct privsep_pipes *pa, *pb;
222 unsigned int proc;
223 unsigned int dst;
224 int fds[2];
226 /* Don't initiate anything if we are not really going to run. */
227 if (ps->ps_noaction)
228 return;
230 if (proc_id == PROC_PARENT) {
231 privsep_process = PROC_PARENT;
232 proc_setup(ps, procs, nproc);
234 /*
235 * Create the children sockets so we can use them
236 * to distribute the rest of the socketpair()s using
237 * proc_connect() later.
238 */
239 for (dst = 0; dst < PROC_MAX; dst++) {
240 /* Don't create socket for ourselves. */
241 if (dst == PROC_PARENT)
242 continue;
244 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
245 pa = &ps->ps_pipes[PROC_PARENT][0];
246 pb = &ps->ps_pipes[dst][proc];
247 if (socketpair(AF_UNIX, SOCK_STREAM,
248 PF_UNSPEC, fds) == -1)
249 fatal("%s: socketpair", __func__);
251 mark_nonblock(fds[0]);
252 mark_nonblock(fds[1]);
253 if (fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
254 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1)
255 fatal("%s: fcntl F_SETFD", __func__);
257 pa->pp_pipes[dst][proc] = fds[0];
258 pb->pp_pipes[PROC_PARENT][0] = fds[1];
262 /* Engage! */
263 proc_exec(ps, procs, nproc, debug, argc, argv);
264 return;
267 /* Initialize a child */
268 for (proc = 0; proc < nproc; proc++) {
269 if (procs[proc].p_id != proc_id)
270 continue;
271 p = &procs[proc];
272 break;
274 if (p == NULL || p->p_init == NULL)
275 fatalx("%s: process %d missing process initialization",
276 __func__, proc_id);
278 p->p_init(ps, p);
280 fatalx("failed to initiate child process");
283 void
284 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst,
285 unsigned int n)
287 struct privsep_pipes *pp = ps->ps_pp;
288 struct imsgev *iev;
290 if (ps->ps_ievs[dst] == NULL) {
291 #if DEBUG > 1
292 log_debug("%s: %s src %d %d to dst %d %d not connected",
293 __func__, ps->ps_title[privsep_process],
294 privsep_process, ps->ps_instance + 1,
295 dst, n + 1);
296 #endif
297 close(fd);
298 return;
301 if (pp->pp_pipes[dst][n] != -1) {
302 log_warnx("%s: duplicated descriptor", __func__);
303 close(fd);
304 return;
305 } else
306 pp->pp_pipes[dst][n] = fd;
308 iev = &ps->ps_ievs[dst][n];
309 imsg_init(&iev->ibuf, fd);
310 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
311 event_add(&iev->ev, NULL);
314 void
315 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc)
317 unsigned int i, j, src, dst, id;
318 struct privsep_pipes *pp;
320 /* Initialize parent title, ps_instances and procs. */
321 ps->ps_title[PROC_PARENT] = "parent";
323 for (src = 0; src < PROC_MAX; src++)
324 /* Default to 1 process instance */
325 if (ps->ps_instances[src] < 1)
326 ps->ps_instances[src] = 1;
328 for (src = 0; src < nproc; src++) {
329 procs[src].p_ps = ps;
330 if (procs[src].p_cb == NULL)
331 procs[src].p_cb = proc_dispatch_null;
333 id = procs[src].p_id;
334 ps->ps_title[id] = procs[src].p_title;
335 if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id],
336 sizeof(struct imsgev))) == NULL)
337 fatal("%s: calloc", __func__);
339 /* With this set up, we are ready to call imsg_init(). */
340 for (i = 0; i < ps->ps_instances[id]; i++) {
341 ps->ps_ievs[id][i].handler = proc_dispatch;
342 ps->ps_ievs[id][i].events = EV_READ;
343 ps->ps_ievs[id][i].proc = &procs[src];
344 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i];
348 /*
349 * Allocate pipes for all process instances (incl. parent)
351 * - ps->ps_pipes: N:M mapping
352 * N source processes connected to M destination processes:
353 * [src][instances][dst][instances], for example
354 * [PROC_RELAY][3][PROC_CA][3]
356 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes
357 * Each process instance has a destination array of socketpair fds:
358 * [dst][instances], for example
359 * [PROC_PARENT][0]
360 */
361 for (src = 0; src < PROC_MAX; src++) {
362 /* Allocate destination array for each process */
363 log_debug("allocating %d for proc %d/%d",
364 ps->ps_instances[src], src, PROC_MAX);
365 if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src],
366 sizeof(struct privsep_pipes))) == NULL)
367 fatal("%s: calloc", __func__);
369 for (i = 0; i < ps->ps_instances[src]; i++) {
370 pp = &ps->ps_pipes[src][i];
372 for (dst = 0; dst < PROC_MAX; dst++) {
373 /* Allocate maximum fd integers */
374 if ((pp->pp_pipes[dst] =
375 calloc(ps->ps_instances[dst],
376 sizeof(int))) == NULL)
377 fatal("%s: calloc", __func__);
379 /* Mark fd as unused */
380 for (j = 0; j < ps->ps_instances[dst]; j++)
381 pp->pp_pipes[dst][j] = -1;
386 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance];
389 void
390 proc_kill(struct privsep *ps)
392 char *cause;
393 pid_t pid;
394 int len, status;
396 if (privsep_process != PROC_PARENT)
397 return;
399 proc_close(ps);
401 do {
402 pid = waitpid(WAIT_ANY, &status, 0);
403 if (pid <= 0)
404 continue;
406 if (WIFSIGNALED(status)) {
407 len = asprintf(&cause, "terminated; signal %d",
408 WTERMSIG(status));
409 } else if (WIFEXITED(status)) {
410 if (WEXITSTATUS(status) != 0)
411 len = asprintf(&cause, "exited abnormally");
412 else
413 len = 0;
414 } else
415 len = -1;
417 if (len == 0) {
418 /* child exited OK, don't print a warning message */
419 } else if (len != -1) {
420 log_warnx("lost child: pid %u %s", pid, cause);
421 free(cause);
422 } else
423 log_warnx("lost child: pid %u", pid);
424 } while (pid != -1 || errno == EINTR);
427 void
428 proc_open(struct privsep *ps, int src, int dst)
430 struct privsep_pipes *pa, *pb;
431 struct privsep_fd pf;
432 int fds[2];
433 unsigned int i, j;
435 /* Exchange pipes between process. */
436 for (i = 0; i < ps->ps_instances[src]; i++) {
437 for (j = 0; j < ps->ps_instances[dst]; j++) {
438 /* Don't create sockets for ourself. */
439 if (src == dst && i == j)
440 continue;
442 /* Servers don't talk to each other. */
443 if (src == PROC_SERVER && dst == PROC_SERVER)
444 continue;
446 pa = &ps->ps_pipes[src][i];
447 pb = &ps->ps_pipes[dst][j];
448 if (socketpair(AF_UNIX,
449 SOCK_STREAM,
450 PF_UNSPEC, fds) == -1)
451 fatal("%s: socketpair", __func__);
453 mark_nonblock(fds[0]);
454 mark_nonblock(fds[1]);
455 if (fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
456 fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1)
457 fatal("%s: fcntl F_SETFD", __func__);
459 pa->pp_pipes[dst][j] = fds[0];
460 pb->pp_pipes[src][i] = fds[1];
462 pf.pf_procid = src;
463 pf.pf_instance = i;
464 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
465 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
466 fatal("%s: proc_compose_imsg", __func__);
468 pf.pf_procid = dst;
469 pf.pf_instance = j;
470 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
471 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
472 fatal("%s: proc_compose_imsg", __func__);
474 /*
475 * We have to flush to send the descriptors and close
476 * them to avoid the fd ramp on startup.
477 */
478 if (proc_flush_imsg(ps, src, i) == -1 ||
479 proc_flush_imsg(ps, dst, j) == -1)
480 fatal("%s: imsg_flush", __func__);
485 void
486 proc_close(struct privsep *ps)
488 unsigned int dst, n;
489 struct privsep_pipes *pp;
491 if (ps == NULL)
492 return;
494 pp = ps->ps_pp;
496 for (dst = 0; dst < PROC_MAX; dst++) {
497 if (ps->ps_ievs[dst] == NULL)
498 continue;
500 for (n = 0; n < ps->ps_instances[dst]; n++) {
501 if (pp->pp_pipes[dst][n] == -1)
502 continue;
504 /* Cancel the fd, close and invalidate the fd */
505 event_del(&(ps->ps_ievs[dst][n].ev));
506 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
507 close(pp->pp_pipes[dst][n]);
508 pp->pp_pipes[dst][n] = -1;
510 free(ps->ps_ievs[dst]);
514 void
515 proc_shutdown(struct privsep_proc *p)
517 struct privsep *ps = p->p_ps;
519 if (p->p_shutdown != NULL)
520 (*p->p_shutdown)();
522 proc_close(ps);
524 log_info("%s exiting, pid %d", p->p_title, getpid());
526 exit(0);
529 void
530 proc_sig_handler(int sig, short event, void *arg)
532 struct privsep_proc *p = arg;
534 switch (sig) {
535 case SIGINT:
536 case SIGTERM:
537 proc_shutdown(p);
538 break;
539 case SIGCHLD:
540 case SIGHUP:
541 /* ignore */
542 break;
543 default:
544 fatalx("%s: unexpected signal", __func__);
545 /* NOTREACHED */
549 void
550 proc_run(struct privsep *ps, struct privsep_proc *p,
551 struct privsep_proc *procs, unsigned int nproc,
552 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
554 struct passwd *pw;
555 const char *root;
557 log_procinit(p->p_title);
558 setproctitle("%s", p->p_title);
560 privsep_process = p->p_id;
562 if (ps->ps_pw == NULL)
563 goto init;
565 /* Set the process group of the current process */
566 setpgid(0, 0);
568 /* Use non-standard user */
569 if (p->p_pw != NULL)
570 pw = p->p_pw;
571 else
572 pw = ps->ps_pw;
574 /* Change root directory */
575 if (p->p_chroot != NULL)
576 root = p->p_chroot;
577 else
578 root = pw->pw_dir;
580 if (chroot(root) == -1)
581 fatal("%s: chroot", __func__);
582 if (chdir("/") == -1)
583 fatal("%s: chdir(\"/\")", __func__);
585 if (setgroups(1, &pw->pw_gid) ||
586 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
587 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
588 fatal("%s: cannot drop privileges", __func__);
590 init:
591 event_init();
593 signal(SIGPIPE, SIG_IGN);
595 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
596 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
597 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
598 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
600 signal_add(&ps->ps_evsigint, NULL);
601 signal_add(&ps->ps_evsigterm, NULL);
602 signal_add(&ps->ps_evsigchld, NULL);
603 signal_add(&ps->ps_evsighup, NULL);
605 proc_setup(ps, procs, nproc);
606 proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0);
608 log_debug("%s: %s %d/%d, pid %d", __func__, p->p_title,
609 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
611 if (run != NULL)
612 run(ps, p, arg);
614 event_dispatch();
616 proc_shutdown(p);
619 void
620 proc_dispatch(int fd, short event, void *arg)
622 struct imsgev *iev = arg;
623 struct privsep_proc *p = iev->proc;
624 struct privsep *ps = p->p_ps;
625 struct imsgbuf *ibuf;
626 struct imsg imsg;
627 ssize_t n;
628 const char *title;
629 struct privsep_fd pf;
631 title = ps->ps_title[privsep_process];
632 ibuf = &iev->ibuf;
634 if (event & EV_READ) {
635 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
636 fatal("%s: imsg_read", __func__);
637 if (n == 0) {
638 /* this pipe is dead, so remove the event handler */
639 event_del(&iev->ev);
640 event_loopexit(NULL);
641 return;
645 if (event & EV_WRITE) {
646 if ((n = msgbuf_write(&ibuf->w)) == -1 && errno != EAGAIN)
647 fatal("%s: msgbuf_write", __func__);
648 if (n == 0) {
649 /* this pipe is dead, so remove the event handler */
650 event_del(&iev->ev);
651 event_loopexit(NULL);
652 return;
656 for (;;) {
657 if ((n = imsg_get(ibuf, &imsg)) == -1)
658 fatal("%s: imsg_get", __func__);
659 if (n == 0)
660 break;
662 #if DEBUG > 1
663 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
664 __func__, title, ps->ps_instance + 1,
665 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
666 #endif
668 /*
669 * Check the message with the program callback
670 */
671 if ((p->p_cb)(fd, p, &imsg) == 0) {
672 /* Message was handled by the callback, continue */
673 imsg_free(&imsg);
674 continue;
677 /*
678 * Generic message handling
679 */
680 switch (imsg.hdr.type) {
681 case IMSG_CTL_PROCFD:
682 if (imsg_get_data(&imsg, &pf, sizeof(pf)))
683 fatalx("bad length imsg CTL_PROCFD");
684 proc_accept(ps, imsg_get_fd(&imsg), pf.pf_procid,
685 pf.pf_instance);
686 break;
687 default:
688 fatalx("%s: %s %d got invalid imsg %d peerid %d "
689 "from %s %d",
690 __func__, title, ps->ps_instance + 1,
691 imsg.hdr.type, imsg.hdr.peerid,
692 p->p_title, imsg.hdr.pid);
694 imsg_free(&imsg);
696 imsg_event_add(iev);
699 int
700 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
702 return (-1);
705 /*
706 * imsg helper functions
707 */
709 void
710 imsg_event_add(struct imsgev *iev)
712 if (iev->handler == NULL) {
713 imsg_flush(&iev->ibuf);
714 return;
717 iev->events = EV_READ;
718 if (iev->ibuf.w.queued)
719 iev->events |= EV_WRITE;
721 event_del(&iev->ev);
722 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
723 event_add(&iev->ev, NULL);
726 int
727 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
728 pid_t pid, int fd, void *data, uint16_t datalen)
730 int ret;
732 if ((ret = imsg_compose(&iev->ibuf, type, peerid,
733 pid, fd, data, datalen)) == -1)
734 return (ret);
735 imsg_event_add(iev);
736 return (ret);
739 int
740 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
741 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
743 int ret;
745 if ((ret = imsg_composev(&iev->ibuf, type, peerid,
746 pid, fd, iov, iovcnt)) == -1)
747 return (ret);
748 imsg_event_add(iev);
749 return (ret);
752 void
753 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
755 if (*n == -1) {
756 /* Use a range of all target instances */
757 *n = 0;
758 *m = ps->ps_instances[id];
759 } else {
760 /* Use only a single slot of the specified peer process */
761 *m = *n + 1;
765 int
766 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
767 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
769 int m;
771 proc_range(ps, id, &n, &m);
772 for (; n < m; n++) {
773 if (imsg_compose_event(&ps->ps_ievs[id][n],
774 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
775 return (-1);
778 return (0);
781 int
782 proc_compose(struct privsep *ps, enum privsep_procid id,
783 uint16_t type, void *data, uint16_t datalen)
785 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
788 int
789 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
790 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
792 int m;
794 proc_range(ps, id, &n, &m);
795 for (; n < m; n++)
796 if (imsg_composev_event(&ps->ps_ievs[id][n],
797 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
798 return (-1);
800 return (0);
803 int
804 proc_composev(struct privsep *ps, enum privsep_procid id,
805 uint16_t type, const struct iovec *iov, int iovcnt)
807 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
810 struct imsgbuf *
811 proc_ibuf(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].ibuf);
819 struct imsgev *
820 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
822 int m;
824 proc_range(ps, id, &n, &m);
825 return (&ps->ps_ievs[id][n]);
828 /* This function should only be called with care as it breaks async I/O */
829 int
830 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
832 struct imsgbuf *ibuf;
833 int m, ret = 0;
835 proc_range(ps, id, &n, &m);
836 for (; n < m; n++) {
837 if ((ibuf = proc_ibuf(ps, id, n)) == NULL)
838 return (-1);
840 do {
841 ret = imsg_flush(ibuf);
842 } while (ret == -1 && errno == EAGAIN);
843 if (ret == -1)
844 break;
845 imsg_event_add(&ps->ps_ievs[id][n]);
848 return (ret);