add some missing libraries to DPADD and fix up some outdated deps
[got-portable.git] / gotwebd / proc.c
blob208d96c56bcaa3315c1baddc10a24b5adf8a38dd
1 /*
2 * Copyright (c) 2010 - 2016 Reyk Floeter <reyk@openbsd.org>
3 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
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.
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.
17 #include "got_compat.h"
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <signal.h>
31 #include <pwd.h>
32 #include <event.h>
34 #include "proc.h"
36 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int,
37 int, char **);
38 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int);
39 void proc_open(struct privsep *, int, int);
40 void proc_accept(struct privsep *, int, enum privsep_procid,
41 unsigned int);
42 void proc_close(struct privsep *);
43 int proc_ispeer(struct privsep_proc *, unsigned int, enum privsep_procid);
44 void proc_shutdown(struct privsep_proc *);
45 void proc_sig_handler(int, short, void *);
46 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *);
48 enum privsep_procid privsep_process;
50 int
51 proc_ispeer(struct privsep_proc *procs, unsigned int nproc,
52 enum privsep_procid type)
54 unsigned int i;
56 for (i = 0; i < nproc; i++)
57 if (procs[i].p_id == type)
58 return (1);
60 return (0);
63 enum privsep_procid
64 proc_getid(struct privsep_proc *procs, unsigned int nproc,
65 const char *proc_name)
67 struct privsep_proc *p;
68 unsigned int proc;
70 for (proc = 0; proc < nproc; proc++) {
71 p = &procs[proc];
72 if (strcmp(p->p_title, proc_name))
73 continue;
75 return (p->p_id);
78 return (PROC_MAX);
81 void
82 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
83 int argc, char **argv)
85 unsigned int proc, nargc, i, proc_i;
86 char **nargv;
87 struct privsep_proc *p;
88 char num[32];
89 int fd;
91 /* Prepare the new process argv. */
92 nargv = calloc(argc + 5, sizeof(char *));
93 if (nargv == NULL)
94 fatal("%s: calloc", __func__);
96 /* Copy call argument first. */
97 nargc = 0;
98 nargv[nargc++] = argv[0];
100 /* Set process name argument and save the position. */
101 nargv[nargc] = strdup("-P");
102 if (nargv[nargc] == NULL)
103 fatal("%s: strdup", __func__);
104 nargc++;
105 proc_i = nargc;
106 nargc++;
108 /* Point process instance arg to stack and copy the original args. */
109 nargv[nargc] = strdup("-I");
110 if (nargv[nargc] == NULL)
111 fatal("%s: strdup", __func__);
112 nargc++;
113 nargv[nargc++] = num;
114 for (i = 1; i < (unsigned int) argc; i++)
115 nargv[nargc++] = argv[i];
117 nargv[nargc] = NULL;
119 for (proc = 0; proc < nproc; proc++) {
120 p = &procs[proc];
122 /* Update args with process title. */
123 nargv[proc_i] = (char *)(uintptr_t)p->p_title;
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_GOTWEBD][0];
131 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_GOTWEBD][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_GOTWEBD_SOCK_FILENO) {
144 if (dup2(fd, PROC_GOTWEBD_SOCK_FILENO)
145 == -1)
146 fatal("dup2");
147 } else if (fcntl(fd, F_SETFD, 0) == -1)
148 fatal("fcntl");
150 execvp(argv[0], nargv);
151 fatal("%s: execvp", __func__);
152 break;
153 default:
154 /* Close child end. */
155 close(fd);
156 break;
161 free(nargv);
164 void
165 proc_connect(struct privsep *ps)
167 struct imsgev *iev;
168 unsigned int src, dst, inst;
170 /* Don't distribute any sockets if we are not really going to run. */
171 if (ps->ps_noaction)
172 return;
174 for (dst = 0; dst < PROC_MAX; dst++) {
175 /* We don't communicate with ourselves. */
176 if (dst == PROC_GOTWEBD)
177 continue;
179 for (inst = 0; inst < ps->ps_instances[dst]; inst++) {
180 iev = &ps->ps_ievs[dst][inst];
181 imsg_init(&iev->ibuf, ps->ps_pp->pp_pipes[dst][inst]);
182 event_set(&iev->ev, iev->ibuf.fd, iev->events,
183 iev->handler, iev->data);
184 event_add(&iev->ev, NULL);
188 /* Distribute the socketpair()s for everyone. */
189 for (src = 0; src < PROC_MAX; src++)
190 for (dst = src; dst < PROC_MAX; dst++) {
191 /* Parent already distributed its fds. */
192 if (src == PROC_GOTWEBD || dst == PROC_GOTWEBD)
193 continue;
195 proc_open(ps, src, dst);
199 void
200 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc,
201 int argc, char **argv, enum privsep_procid proc_id)
203 struct privsep_proc *p = NULL;
204 struct privsep_pipes *pa, *pb;
205 unsigned int proc;
206 unsigned int dst;
207 int fds[2];
209 /* Don't initiate anything if we are not really going to run. */
210 if (ps->ps_noaction)
211 return;
213 if (proc_id == PROC_GOTWEBD) {
214 privsep_process = PROC_GOTWEBD;
215 proc_setup(ps, procs, nproc);
218 * Create the children sockets so we can use them
219 * to distribute the rest of the socketpair()s using
220 * proc_connect() later.
222 for (dst = 0; dst < PROC_MAX; dst++) {
223 /* Don't create socket for ourselves. */
224 if (dst == PROC_GOTWEBD)
225 continue;
227 for (proc = 0; proc < ps->ps_instances[dst]; proc++) {
228 pa = &ps->ps_pipes[PROC_GOTWEBD][0];
229 pb = &ps->ps_pipes[dst][proc];
230 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
231 #ifdef SOCK_CLOEXEC
232 sock_flags |= SOCK_CLOEXEC;
233 #endif
234 if (socketpair(AF_UNIX, sock_flags,
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];
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]
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 int sock_flags = SOCK_STREAM | SOCK_NONBLOCK;
427 #ifdef SOCK_CLOEXEC
428 sock_flags |= SOCK_CLOEXEC;
429 #endif
430 if (socketpair(AF_UNIX, sock_flags,
431 PF_UNSPEC, fds) == -1)
432 fatal("%s: socketpair", __func__);
434 pa->pp_pipes[dst][j] = fds[0];
435 pb->pp_pipes[src][i] = fds[1];
437 pf.pf_procid = src;
438 pf.pf_instance = i;
439 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD,
440 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1)
441 fatal("%s: proc_compose_imsg", __func__);
443 pf.pf_procid = dst;
444 pf.pf_instance = j;
445 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD,
446 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1)
447 fatal("%s: proc_compose_imsg", __func__);
450 * We have to flush to send the descriptors and close
451 * them to avoid the fd ramp on startup.
453 if (proc_flush_imsg(ps, src, i) == -1 ||
454 proc_flush_imsg(ps, dst, j) == -1)
455 fatal("%s: imsg_flush", __func__);
460 void
461 proc_close(struct privsep *ps)
463 unsigned int dst, n;
464 struct privsep_pipes *pp;
466 if (ps == NULL)
467 return;
469 pp = ps->ps_pp;
471 for (dst = 0; dst < PROC_MAX; dst++) {
472 if (ps->ps_ievs[dst] == NULL)
473 continue;
475 for (n = 0; n < ps->ps_instances[dst]; n++) {
476 if (pp->pp_pipes[dst][n] == -1)
477 continue;
479 /* Cancel the fd, close and invalidate the fd */
480 event_del(&(ps->ps_ievs[dst][n].ev));
481 imsg_clear(&(ps->ps_ievs[dst][n].ibuf));
482 close(pp->pp_pipes[dst][n]);
483 pp->pp_pipes[dst][n] = -1;
485 free(ps->ps_ievs[dst]);
489 void
490 proc_shutdown(struct privsep_proc *p)
492 struct privsep *ps = p->p_ps;
494 if (p->p_shutdown != NULL)
495 (*p->p_shutdown)();
497 proc_close(ps);
499 log_info("%s, %s exiting, pid %d", getprogname(), p->p_title, getpid());
501 exit(0);
504 void
505 proc_sig_handler(int sig, short event, void *arg)
507 struct privsep_proc *p = arg;
509 switch (sig) {
510 case SIGINT:
511 case SIGTERM:
512 proc_shutdown(p);
513 break;
514 case SIGCHLD:
515 case SIGHUP:
516 case SIGPIPE:
517 case SIGUSR1:
518 /* ignore */
519 break;
520 default:
521 fatalx("proc_sig_handler: unexpected signal");
522 /* NOTREACHED */
526 void
527 proc_run(struct privsep *ps, struct privsep_proc *p,
528 struct privsep_proc *procs, unsigned int nproc,
529 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg)
531 struct passwd *pw;
532 const char *root;
534 log_procinit(p->p_title);
536 /* Set the process group of the current process */
537 setpgid(0, 0);
539 /* Use non-standard user */
540 if (p->p_pw != NULL)
541 pw = p->p_pw;
542 else
543 pw = ps->ps_pw;
545 /* Change root directory */
546 if (p->p_chroot != NULL)
547 root = p->p_chroot;
548 else
549 root = pw->pw_dir;
551 if (chroot(root) == -1)
552 fatal("proc_run: chroot");
553 if (chdir("/") == -1)
554 fatal("proc_run: chdir(\"/\")");
556 privsep_process = p->p_id;
558 setproctitle("%s", p->p_title);
560 if (setgroups(1, &pw->pw_gid) ||
561 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
562 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
563 fatal("proc_run: cannot drop privileges");
565 event_init();
567 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p);
568 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p);
569 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p);
570 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p);
571 signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p);
572 signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p);
574 signal_add(&ps->ps_evsigint, NULL);
575 signal_add(&ps->ps_evsigterm, NULL);
576 signal_add(&ps->ps_evsigchld, NULL);
577 signal_add(&ps->ps_evsighup, NULL);
578 signal_add(&ps->ps_evsigpipe, NULL);
579 signal_add(&ps->ps_evsigusr1, NULL);
581 proc_setup(ps, procs, nproc);
582 proc_accept(ps, PROC_GOTWEBD_SOCK_FILENO, PROC_GOTWEBD, 0);
584 DPRINTF("%s: %s %d/%d, pid %d", __func__, p->p_title,
585 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid());
587 if (run != NULL)
588 run(ps, p, arg);
590 event_dispatch();
592 proc_shutdown(p);
595 void
596 proc_dispatch(int fd, short event, void *arg)
598 struct imsgev *iev = arg;
599 struct privsep_proc *p = iev->proc;
600 struct privsep *ps = p->p_ps;
601 struct imsgbuf *ibuf;
602 struct imsg imsg;
603 ssize_t n;
604 int verbose;
605 const char *title;
606 struct privsep_fd pf;
608 title = ps->ps_title[privsep_process];
609 ibuf = &iev->ibuf;
611 if (event & EV_READ) {
612 n = imsg_read(ibuf);
613 if (n == -1 && errno != EAGAIN)
614 fatal("%s: imsg_read", __func__);
615 if (n == 0) {
616 /* this pipe is dead, so remove the event handler */
617 event_del(&iev->ev);
618 event_loopexit(NULL);
619 return;
623 if (event & EV_WRITE) {
624 n = msgbuf_write(&ibuf->w);
625 if (n == -1 && errno != EAGAIN)
626 fatal("%s: msgbuf_write", __func__);
627 if (n == 0) {
628 /* this pipe is dead, so remove the event handler */
629 event_del(&iev->ev);
630 event_loopexit(NULL);
631 return;
635 for (;;) {
636 n = imsg_get(ibuf, &imsg);
637 if (n == -1)
638 fatal("%s: imsg_get", __func__);
639 if (n == 0)
640 break;
642 #if DEBUG > 1
643 log_debug("%s: %s %d got imsg %d peerid %d from %s %d",
644 __func__, title, ps->ps_instance + 1,
645 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid);
646 #endif
649 * Check the message with the program callback
651 if ((p->p_cb)(fd, p, &imsg) == 0) {
652 /* Message was handled by the callback, continue */
653 imsg_free(&imsg);
654 continue;
658 * Generic message handling
660 switch (imsg.hdr.type) {
661 case IMSG_CTL_VERBOSE:
662 log_info("%s", __func__);
663 IMSG_SIZE_CHECK(&imsg, &verbose);
664 memcpy(&verbose, imsg.data, sizeof(verbose));
665 log_setverbose(verbose);
666 break;
667 case IMSG_CTL_PROCFD:
668 IMSG_SIZE_CHECK(&imsg, &pf);
669 memcpy(&pf, imsg.data, sizeof(pf));
670 proc_accept(ps, imsg.fd, pf.pf_procid,
671 pf.pf_instance);
672 break;
673 default:
674 log_warnx("%s: %s %d got invalid imsg %d peerid %d "
675 "from %s %d",
676 __func__, title, ps->ps_instance + 1,
677 imsg.hdr.type, imsg.hdr.peerid,
678 p->p_title, imsg.hdr.pid);
680 imsg_free(&imsg);
682 imsg_event_add(iev);
686 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg)
688 return (-1);
692 * imsg helper functions
695 void
696 imsg_event_add(struct imsgev *iev)
698 if (iev->handler == NULL) {
699 imsg_flush(&iev->ibuf);
700 return;
703 iev->events = EV_READ;
704 if (iev->ibuf.w.queued)
705 iev->events |= EV_WRITE;
707 event_del(&iev->ev);
708 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data);
709 event_add(&iev->ev, NULL);
713 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
714 pid_t pid, int fd, void *data, uint16_t datalen)
716 int ret;
718 ret = imsg_compose(&iev->ibuf, type, peerid, pid, fd, data, datalen);
719 if (ret == -1)
720 return (ret);
721 imsg_event_add(iev);
722 return (ret);
726 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid,
727 pid_t pid, int fd, const struct iovec *iov, int iovcnt)
729 int ret;
731 ret = imsg_composev(&iev->ibuf, type, peerid, pid, fd, iov, iovcnt);
732 if (ret == -1)
733 return (ret);
734 imsg_event_add(iev);
735 return (ret);
738 void
739 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m)
741 if (*n == -1) {
742 /* Use a range of all target instances */
743 *n = 0;
744 *m = ps->ps_instances[id];
745 } else {
746 /* Use only a single slot of the specified peer process */
747 *m = *n + 1;
752 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n,
753 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen)
755 int m;
757 proc_range(ps, id, &n, &m);
758 for (; n < m; n++) {
759 if (imsg_compose_event(&ps->ps_ievs[id][n],
760 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1)
761 return (-1);
764 return (0);
768 proc_compose(struct privsep *ps, enum privsep_procid id,
769 uint16_t type, void *data, uint16_t datalen)
771 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen));
775 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n,
776 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt)
778 int m;
780 proc_range(ps, id, &n, &m);
781 for (; n < m; n++)
782 if (imsg_composev_event(&ps->ps_ievs[id][n],
783 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1)
784 return (-1);
786 return (0);
790 proc_composev(struct privsep *ps, enum privsep_procid id,
791 uint16_t type, const struct iovec *iov, int iovcnt)
793 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt));
797 proc_forward_imsg(struct privsep *ps, struct imsg *imsg,
798 enum privsep_procid id, int n)
800 return (proc_compose_imsg(ps, id, n, imsg->hdr.type,
801 imsg->hdr.peerid, imsg->fd, imsg->data, IMSG_DATA_SIZE(imsg)));
804 struct imsgbuf *
805 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n)
807 int m;
809 proc_range(ps, id, &n, &m);
810 return (&ps->ps_ievs[id][n].ibuf);
813 struct imsgev *
814 proc_iev(struct privsep *ps, enum privsep_procid id, int n)
816 int m;
818 proc_range(ps, id, &n, &m);
819 return (&ps->ps_ievs[id][n]);
822 /* This function should only be called with care as it breaks async I/O */
824 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n)
826 struct imsgbuf *ibuf;
827 int m, ret = 0;
829 proc_range(ps, id, &n, &m);
830 for (; n < m; n++) {
831 ibuf = proc_ibuf(ps, id, n);
832 if (ibuf == NULL)
833 return (-1);
834 do {
835 ret = imsg_flush(ibuf);
836 } while (ret == -1 && errno == EAGAIN);
837 if (ret == -1)
838 break;
839 imsg_event_add(&ps->ps_ievs[id][n]);
842 return (ret);