1 /* $OpenBSD: proc.c,v 1.51 2024/11/21 13:35:20 claudio Exp $ */ 2 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 */ 19 20 #include <sys/types.h> 21 #include <sys/queue.h> 22 #include <sys/socket.h> 23 #include <sys/wait.h> 24 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> 36 37 #include "iked.h" 38 39 enum privsep_procid privsep_process; 40 41 void proc_exec(struct privsep *, struct privsep_proc *, unsigned int, int, 42 char **); 43 void proc_setup(struct privsep *, struct privsep_proc *, unsigned int); 44 void proc_open(struct privsep *, int, int); 45 void proc_accept(struct privsep *, int, enum privsep_procid, 46 unsigned int); 47 void proc_close(struct privsep *); 48 void proc_shutdown(struct privsep_proc *); 49 void proc_sig_handler(int, short, void *); 50 void proc_range(struct privsep *, enum privsep_procid, int *, int *); 51 int proc_dispatch_null(int, struct privsep_proc *, struct imsg *); 52 53 enum privsep_procid 54 proc_getid(struct privsep_proc *procs, unsigned int nproc, 55 const char *proc_name) 56 { 57 struct privsep_proc *p; 58 unsigned int proc; 59 60 for (proc = 0; proc < nproc; proc++) { 61 p = &procs[proc]; 62 if (strcmp(p->p_title, proc_name)) 63 continue; 64 65 return (p->p_id); 66 } 67 68 return (PROC_MAX); 69 } 70 71 void 72 proc_exec(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc, 73 int argc, char **argv) 74 { 75 unsigned int proc, nargc, i, proc_i; 76 char **nargv; 77 struct privsep_proc *p; 78 char num[32]; 79 int fd; 80 81 /* Prepare the new process argv. */ 82 nargv = calloc(argc + 5, sizeof(char *)); 83 if (nargv == NULL) 84 fatal("%s: calloc", __func__); 85 86 /* Copy call argument first. */ 87 nargc = 0; 88 nargv[nargc++] = argv[0]; 89 90 /* Set process name argument and save the position. */ 91 nargv[nargc++] = "-P"; 92 proc_i = nargc; 93 nargc++; 94 95 /* Point process instance arg to stack and copy the original args. */ 96 nargv[nargc++] = "-I"; 97 nargv[nargc++] = num; 98 for (i = 1; i < (unsigned int) argc; i++) 99 nargv[nargc++] = argv[i]; 100 101 nargv[nargc] = NULL; 102 103 for (proc = 0; proc < nproc; proc++) { 104 p = &procs[proc]; 105 106 /* Update args with process title. */ 107 nargv[proc_i] = (char *)(uintptr_t)p->p_title; 108 109 /* Fire children processes. */ 110 for (i = 0; i < ps->ps_instances[p->p_id]; i++) { 111 /* Update the process instance number. */ 112 snprintf(num, sizeof(num), "%u", i); 113 114 fd = ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0]; 115 ps->ps_pipes[p->p_id][i].pp_pipes[PROC_PARENT][0] = -1; 116 117 switch (fork()) { 118 case -1: 119 fatal("%s: fork", __func__); 120 break; 121 case 0: 122 /* Prepare parent socket. */ 123 if (fd != PROC_PARENT_SOCK_FILENO) { 124 if (dup2(fd, PROC_PARENT_SOCK_FILENO) 125 == -1) 126 fatal("dup2"); 127 } else if (fcntl(fd, F_SETFD, 0) == -1) 128 fatal("fcntl"); 129 130 execvp(argv[0], nargv); 131 fatal("%s: execvp", __func__); 132 break; 133 default: 134 /* Close child end. */ 135 close(fd); 136 break; 137 } 138 } 139 } 140 free(nargv); 141 } 142 143 void 144 proc_connect(struct privsep *ps, void (*connected)(struct privsep *)) 145 { 146 struct imsgev *iev; 147 unsigned int src, dst, inst; 148 149 /* Don't distribute any sockets if we are not really going to run. */ 150 if (ps->ps_noaction) { 151 if (connected == NULL) 152 fatalx("%s: missing callback", __func__); 153 connected(ps); 154 return; 155 } 156 ps->ps_connected = connected; 157 158 for (dst = 0; dst < PROC_MAX; dst++) { 159 /* We don't communicate with ourselves. */ 160 if (dst == PROC_PARENT) 161 continue; 162 163 for (inst = 0; inst < ps->ps_instances[dst]; inst++) { 164 iev = &ps->ps_ievs[dst][inst]; 165 if (imsgbuf_init(&iev->ibuf, 166 ps->ps_pp->pp_pipes[dst][inst]) == -1) 167 fatal("%s: imsgbuf_init", __func__); 168 imsgbuf_allow_fdpass(&iev->ibuf); 169 event_set(&iev->ev, iev->ibuf.fd, iev->events, 170 iev->handler, iev->data); 171 event_add(&iev->ev, NULL); 172 } 173 } 174 175 /* Distribute the socketpair()s for everyone. */ 176 for (src = 0; src < PROC_MAX; src++) 177 for (dst = src; dst < PROC_MAX; dst++) { 178 /* Parent already distributed its fds. */ 179 if (src == PROC_PARENT || dst == PROC_PARENT) 180 continue; 181 182 proc_open(ps, src, dst); 183 } 184 185 /* 186 * Finally, send a ready message to everyone: 187 * When this message is processed by the receiver, it has 188 * already processed all IMSG_CTL_PROCFD messages and all 189 * pipes are ready. 190 */ 191 for (dst = 0; dst < PROC_MAX; dst++) { 192 if (dst == PROC_PARENT) 193 continue; 194 for (inst = 0; inst < ps->ps_instances[dst]; inst++) { 195 if (proc_compose_imsg(ps, dst, inst, IMSG_CTL_PROCREADY, 196 -1, -1, NULL, 0) == -1) 197 fatal("%s: proc_compose_imsg", __func__); 198 ps->ps_connecting++; 199 #if DEBUG 200 log_debug("%s: #%d %s %d", __func__, 201 ps->ps_connecting, ps->ps_title[dst], inst + 1); 202 #endif 203 } 204 } 205 } 206 207 void 208 proc_init(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc, 209 int debug, int argc, char **argv, enum privsep_procid proc_id) 210 { 211 struct privsep_proc *p = NULL; 212 struct privsep_pipes *pa, *pb; 213 unsigned int proc; 214 unsigned int dst; 215 int fds[2]; 216 217 /* Don't initiate anything if we are not really going to run. */ 218 if (ps->ps_noaction) 219 return; 220 221 if (proc_id == PROC_PARENT) { 222 privsep_process = PROC_PARENT; 223 proc_setup(ps, procs, nproc); 224 225 if (!debug && daemon(0, 0) == -1) 226 fatal("failed to daemonize"); 227 228 /* 229 * Create the children sockets so we can use them 230 * to distribute the rest of the socketpair()s using 231 * proc_connect() later. 232 */ 233 for (dst = 0; dst < PROC_MAX; dst++) { 234 /* Don't create socket for ourselves. */ 235 if (dst == PROC_PARENT) 236 continue; 237 238 for (proc = 0; proc < ps->ps_instances[dst]; proc++) { 239 pa = &ps->ps_pipes[PROC_PARENT][0]; 240 pb = &ps->ps_pipes[dst][proc]; 241 if (socketpair(AF_UNIX, 242 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 243 PF_UNSPEC, fds) == -1) 244 fatal("%s: socketpair", __func__); 245 246 pa->pp_pipes[dst][proc] = fds[0]; 247 pb->pp_pipes[PROC_PARENT][0] = fds[1]; 248 } 249 } 250 251 /* Engage! */ 252 proc_exec(ps, procs, nproc, argc, argv); 253 return; 254 } 255 256 /* Initialize a child */ 257 for (proc = 0; proc < nproc; proc++) { 258 if (procs[proc].p_id != proc_id) 259 continue; 260 p = &procs[proc]; 261 break; 262 } 263 if (p == NULL || p->p_init == NULL) 264 fatalx("%s: process %d missing process initialization", 265 __func__, proc_id); 266 267 p->p_init(ps, p); 268 269 fatalx("failed to initiate child process"); 270 } 271 272 void 273 proc_accept(struct privsep *ps, int fd, enum privsep_procid dst, 274 unsigned int n) 275 { 276 struct privsep_pipes *pp = ps->ps_pp; 277 struct imsgev *iev; 278 279 if (ps->ps_ievs[dst] == NULL) { 280 #if DEBUG > 1 281 log_debug("%s: %s src %d %d to dst %d %d not connected", 282 __func__, ps->ps_title[privsep_process], 283 privsep_process, ps->ps_instance + 1, 284 dst, n + 1); 285 #endif 286 close(fd); 287 return; 288 } 289 290 if (pp->pp_pipes[dst][n] != -1) { 291 log_warnx("%s: duplicated descriptor", __func__); 292 close(fd); 293 return; 294 } else 295 pp->pp_pipes[dst][n] = fd; 296 297 iev = &ps->ps_ievs[dst][n]; 298 if (imsgbuf_init(&iev->ibuf, fd) == -1) 299 fatal("%s: imsgbuf_init", __func__); 300 imsgbuf_allow_fdpass(&iev->ibuf); 301 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data); 302 event_add(&iev->ev, NULL); 303 } 304 305 void 306 proc_setup(struct privsep *ps, struct privsep_proc *procs, unsigned int nproc) 307 { 308 unsigned int i, j, src, dst, id; 309 struct privsep_pipes *pp; 310 311 /* Initialize parent title, ps_instances and procs. */ 312 ps->ps_title[PROC_PARENT] = "parent"; 313 314 for (src = 0; src < PROC_MAX; src++) 315 /* Default to 1 process instance */ 316 if (ps->ps_instances[src] < 1) 317 ps->ps_instances[src] = 1; 318 319 for (src = 0; src < nproc; src++) { 320 procs[src].p_ps = ps; 321 if (procs[src].p_cb == NULL) 322 procs[src].p_cb = proc_dispatch_null; 323 324 id = procs[src].p_id; 325 ps->ps_title[id] = procs[src].p_title; 326 if ((ps->ps_ievs[id] = calloc(ps->ps_instances[id], 327 sizeof(struct imsgev))) == NULL) 328 fatal("%s: calloc", __func__); 329 330 /* With this set up, we are ready to call imsgbuf_init(). */ 331 for (i = 0; i < ps->ps_instances[id]; i++) { 332 ps->ps_ievs[id][i].handler = proc_dispatch; 333 ps->ps_ievs[id][i].events = EV_READ; 334 ps->ps_ievs[id][i].proc = &procs[src]; 335 ps->ps_ievs[id][i].data = &ps->ps_ievs[id][i]; 336 } 337 } 338 339 /* 340 * Allocate pipes for all process instances (incl. parent) 341 * 342 * - ps->ps_pipes: N:M mapping 343 * N source processes connected to M destination processes: 344 * [src][instances][dst][instances], for example 345 * [PROC_RELAY][3][PROC_CA][3] 346 * 347 * - ps->ps_pp: per-process 1:M part of ps->ps_pipes 348 * Each process instance has a destination array of socketpair fds: 349 * [dst][instances], for example 350 * [PROC_PARENT][0] 351 */ 352 for (src = 0; src < PROC_MAX; src++) { 353 /* Allocate destination array for each process */ 354 if ((ps->ps_pipes[src] = calloc(ps->ps_instances[src], 355 sizeof(struct privsep_pipes))) == NULL) 356 fatal("%s: calloc", __func__); 357 358 for (i = 0; i < ps->ps_instances[src]; i++) { 359 pp = &ps->ps_pipes[src][i]; 360 361 for (dst = 0; dst < PROC_MAX; dst++) { 362 /* Allocate maximum fd integers */ 363 if ((pp->pp_pipes[dst] = 364 calloc(ps->ps_instances[dst], 365 sizeof(int))) == NULL) 366 fatal("%s: calloc", __func__); 367 368 /* Mark fd as unused */ 369 for (j = 0; j < ps->ps_instances[dst]; j++) 370 pp->pp_pipes[dst][j] = -1; 371 } 372 } 373 } 374 375 ps->ps_pp = &ps->ps_pipes[privsep_process][ps->ps_instance]; 376 } 377 378 void 379 proc_kill(struct privsep *ps) 380 { 381 char *cause; 382 pid_t pid; 383 int len, status; 384 385 if (privsep_process != PROC_PARENT) 386 return; 387 388 proc_close(ps); 389 390 do { 391 pid = waitpid(WAIT_ANY, &status, 0); 392 if (pid <= 0) 393 continue; 394 395 if (WIFSIGNALED(status)) { 396 len = asprintf(&cause, "terminated; signal %d", 397 WTERMSIG(status)); 398 } else if (WIFEXITED(status)) { 399 if (WEXITSTATUS(status) != 0) 400 len = asprintf(&cause, "exited abnormally"); 401 else 402 len = 0; 403 } else 404 len = -1; 405 406 if (len == 0) { 407 /* child exited OK, don't print a warning message */ 408 } else if (len != -1) { 409 log_warnx("lost child: pid %u %s", pid, cause); 410 free(cause); 411 } else 412 log_warnx("lost child: pid %u", pid); 413 } while (pid != -1 || (pid == -1 && errno == EINTR)); 414 } 415 416 void 417 proc_open(struct privsep *ps, int src, int dst) 418 { 419 struct privsep_pipes *pa, *pb; 420 struct privsep_fd pf; 421 int fds[2]; 422 unsigned int i, j; 423 424 /* Exchange pipes between process. */ 425 for (i = 0; i < ps->ps_instances[src]; i++) { 426 for (j = 0; j < ps->ps_instances[dst]; j++) { 427 /* Don't create sockets for ourself. */ 428 if (src == dst && i == j) 429 continue; 430 431 pa = &ps->ps_pipes[src][i]; 432 pb = &ps->ps_pipes[dst][j]; 433 if (socketpair(AF_UNIX, 434 SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 435 PF_UNSPEC, fds) == -1) 436 fatal("%s: socketpair", __func__); 437 438 pa->pp_pipes[dst][j] = fds[0]; 439 pb->pp_pipes[src][i] = fds[1]; 440 441 pf.pf_procid = src; 442 pf.pf_instance = i; 443 if (proc_compose_imsg(ps, dst, j, IMSG_CTL_PROCFD, 444 -1, pb->pp_pipes[src][i], &pf, sizeof(pf)) == -1) 445 fatal("%s: proc_compose_imsg", __func__); 446 447 pf.pf_procid = dst; 448 pf.pf_instance = j; 449 if (proc_compose_imsg(ps, src, i, IMSG_CTL_PROCFD, 450 -1, pa->pp_pipes[dst][j], &pf, sizeof(pf)) == -1) 451 fatal("%s: proc_compose_imsg", __func__); 452 453 /* 454 * We have to flush to send the descriptors and close 455 * them to avoid the fd ramp on startup. 456 */ 457 if (proc_flush_imsg(ps, src, i) == -1 || 458 proc_flush_imsg(ps, dst, j) == -1) 459 fatal("%s: proc_flush_imsg", __func__); 460 } 461 } 462 } 463 464 void 465 proc_close(struct privsep *ps) 466 { 467 unsigned int dst, n; 468 struct privsep_pipes *pp; 469 470 if (ps == NULL) 471 return; 472 473 pp = ps->ps_pp; 474 475 for (dst = 0; dst < PROC_MAX; dst++) { 476 if (ps->ps_ievs[dst] == NULL) 477 continue; 478 479 for (n = 0; n < ps->ps_instances[dst]; n++) { 480 if (pp->pp_pipes[dst][n] == -1) 481 continue; 482 483 /* Cancel the fd, close and invalidate the fd */ 484 event_del(&(ps->ps_ievs[dst][n].ev)); 485 imsgbuf_clear(&(ps->ps_ievs[dst][n].ibuf)); 486 close(pp->pp_pipes[dst][n]); 487 pp->pp_pipes[dst][n] = -1; 488 } 489 free(ps->ps_ievs[dst]); 490 } 491 } 492 493 void 494 proc_shutdown(struct privsep_proc *p) 495 { 496 struct privsep *ps = p->p_ps; 497 498 if (p->p_shutdown != NULL) 499 (*p->p_shutdown)(); 500 501 proc_close(ps); 502 503 log_info("%s exiting, pid %d", p->p_title, getpid()); 504 505 exit(0); 506 } 507 508 void 509 proc_sig_handler(int sig, short event, void *arg) 510 { 511 struct privsep_proc *p = arg; 512 513 switch (sig) { 514 case SIGINT: 515 case SIGTERM: 516 proc_shutdown(p); 517 break; 518 case SIGCHLD: 519 case SIGHUP: 520 case SIGPIPE: 521 case SIGUSR1: 522 /* ignore */ 523 break; 524 default: 525 fatalx("%s: unexpected signal", __func__); 526 /* NOTREACHED */ 527 } 528 } 529 530 void 531 proc_run(struct privsep *ps, struct privsep_proc *p, 532 struct privsep_proc *procs, unsigned int nproc, 533 void (*run)(struct privsep *, struct privsep_proc *, void *), void *arg) 534 { 535 struct passwd *pw; 536 const char *root; 537 538 log_procinit(p->p_title); 539 540 if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) { 541 if (control_init(ps, &ps->ps_csock) == -1) 542 fatalx("%s: control_init", __func__); 543 } 544 545 /* Use non-standard user */ 546 if (p->p_pw != NULL) 547 pw = p->p_pw; 548 else 549 pw = ps->ps_pw; 550 551 /* Change root directory */ 552 if (p->p_chroot != NULL) 553 root = p->p_chroot; 554 else 555 root = pw->pw_dir; 556 557 if (chroot(root) == -1) 558 fatal("%s: chroot", __func__); 559 if (chdir("/") == -1) 560 fatal("%s: chdir(\"/\")", __func__); 561 562 privsep_process = p->p_id; 563 564 setproctitle("%s", p->p_title); 565 566 if (setgroups(1, &pw->pw_gid) || 567 setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) || 568 setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid)) 569 fatal("%s: cannot drop privileges", __func__); 570 571 event_init(); 572 573 signal_set(&ps->ps_evsigint, SIGINT, proc_sig_handler, p); 574 signal_set(&ps->ps_evsigterm, SIGTERM, proc_sig_handler, p); 575 signal_set(&ps->ps_evsigchld, SIGCHLD, proc_sig_handler, p); 576 signal_set(&ps->ps_evsighup, SIGHUP, proc_sig_handler, p); 577 signal_set(&ps->ps_evsigpipe, SIGPIPE, proc_sig_handler, p); 578 signal_set(&ps->ps_evsigusr1, SIGUSR1, proc_sig_handler, p); 579 580 signal_add(&ps->ps_evsigint, NULL); 581 signal_add(&ps->ps_evsigterm, NULL); 582 signal_add(&ps->ps_evsigchld, NULL); 583 signal_add(&ps->ps_evsighup, NULL); 584 signal_add(&ps->ps_evsigpipe, NULL); 585 signal_add(&ps->ps_evsigusr1, NULL); 586 587 proc_setup(ps, procs, nproc); 588 proc_accept(ps, PROC_PARENT_SOCK_FILENO, PROC_PARENT, 0); 589 if (p->p_id == PROC_CONTROL && ps->ps_instance == 0) { 590 if (control_listen(&ps->ps_csock) == -1) 591 fatalx("%s: control_listen", __func__); 592 } 593 594 #if DEBUG 595 log_debug("%s: %s %d/%d, pid %d", __func__, p->p_title, 596 ps->ps_instance + 1, ps->ps_instances[p->p_id], getpid()); 597 #endif 598 599 if (run != NULL) 600 run(ps, p, arg); 601 602 event_dispatch(); 603 604 proc_shutdown(p); 605 } 606 607 void 608 proc_dispatch(int fd, short event, void *arg) 609 { 610 struct imsgev *iev = arg; 611 struct privsep_proc *p = iev->proc; 612 struct privsep *ps = p->p_ps; 613 struct imsgbuf *ibuf; 614 struct imsg imsg; 615 ssize_t n; 616 int verbose; 617 const char *title; 618 struct privsep_fd pf; 619 620 title = ps->ps_title[privsep_process]; 621 ibuf = &iev->ibuf; 622 623 if (event & EV_READ) { 624 if ((n = imsgbuf_read(ibuf)) == -1) 625 fatal("%s: imsgbuf_read", __func__); 626 if (n == 0) { 627 /* this pipe is dead, so remove the event handler */ 628 event_del(&iev->ev); 629 event_loopexit(NULL); 630 return; 631 } 632 } 633 634 if (event & EV_WRITE) { 635 if (imsgbuf_write(ibuf) == -1) { 636 if (errno == EPIPE) { /* Connection closed. */ 637 event_del(&iev->ev); 638 event_loopexit(NULL); 639 return; 640 } else 641 fatal("imsgbuf_write"); 642 } 643 } 644 645 for (;;) { 646 if ((n = imsg_get(ibuf, &imsg)) == -1) 647 fatal("%s: imsg_get", __func__); 648 if (n == 0) 649 break; 650 651 #if DEBUG > 1 652 log_debug("%s: %s %d got imsg %d peerid %d from %s %d", 653 __func__, title, ps->ps_instance + 1, 654 imsg.hdr.type, imsg.hdr.peerid, p->p_title, imsg.hdr.pid); 655 #endif 656 657 /* 658 * Check the message with the program callback 659 */ 660 if ((p->p_cb)(fd, p, &imsg) == 0) { 661 /* Message was handled by the callback, continue */ 662 imsg_free(&imsg); 663 continue; 664 } 665 666 /* 667 * Generic message handling 668 */ 669 switch (imsg.hdr.type) { 670 case IMSG_CTL_VERBOSE: 671 IMSG_SIZE_CHECK(&imsg, &verbose); 672 memcpy(&verbose, imsg.data, sizeof(verbose)); 673 log_setverbose(verbose); 674 break; 675 case IMSG_CTL_PROCFD: 676 IMSG_SIZE_CHECK(&imsg, &pf); 677 memcpy(&pf, imsg.data, sizeof(pf)); 678 proc_accept(ps, imsg_get_fd(&imsg), pf.pf_procid, 679 pf.pf_instance); 680 break; 681 case IMSG_CTL_PROCREADY: 682 #if DEBUG 683 log_debug("%s: ready-%s: #%d %s %d -> %s %d", __func__, 684 p->p_id == PROC_PARENT ? "req" : "ack", 685 ps->ps_connecting, p->p_title, imsg.hdr.pid, 686 title, ps->ps_instance + 1); 687 #endif 688 if (p->p_id == PROC_PARENT) { 689 /* ack that we are ready */ 690 if (proc_compose_imsg(ps, PROC_PARENT, 0, 691 IMSG_CTL_PROCREADY, -1, -1, NULL, 0) == -1) 692 fatal("%s: proc_compose_imsg", __func__); 693 } else { 694 /* parent received ack */ 695 if (ps->ps_connecting == 0) 696 fatalx("%s: wrong acks", __func__); 697 if (ps->ps_instance != 0) 698 fatalx("%s: wrong instance %d", 699 __func__, ps->ps_instance); 700 if (ps->ps_connected == NULL) 701 fatalx("%s: missing callback", __func__); 702 if (--ps->ps_connecting == 0) { 703 log_debug("%s: all connected", __func__); 704 ps->ps_connected(ps); 705 } 706 } 707 break; 708 default: 709 fatalx("%s: %s %d got invalid imsg %d peerid %d " 710 "from %s %d", 711 __func__, title, ps->ps_instance + 1, 712 imsg.hdr.type, imsg.hdr.peerid, 713 p->p_title, imsg.hdr.pid); 714 } 715 imsg_free(&imsg); 716 } 717 imsg_event_add(iev); 718 } 719 720 int 721 proc_dispatch_null(int fd, struct privsep_proc *p, struct imsg *imsg) 722 { 723 return (-1); 724 } 725 726 /* 727 * imsg helper functions 728 */ 729 730 void 731 imsg_event_add(struct imsgev *iev) 732 { 733 if (iev->handler == NULL) { 734 imsgbuf_flush(&iev->ibuf); 735 return; 736 } 737 738 iev->events = EV_READ; 739 if (imsgbuf_queuelen(&iev->ibuf) > 0) 740 iev->events |= EV_WRITE; 741 742 event_del(&iev->ev); 743 event_set(&iev->ev, iev->ibuf.fd, iev->events, iev->handler, iev->data); 744 event_add(&iev->ev, NULL); 745 } 746 747 int 748 imsg_compose_event(struct imsgev *iev, uint16_t type, uint32_t peerid, 749 pid_t pid, int fd, void *data, uint16_t datalen) 750 { 751 int ret; 752 753 if ((ret = imsg_compose(&iev->ibuf, type, peerid, 754 pid, fd, data, datalen)) == -1) 755 return (ret); 756 imsg_event_add(iev); 757 return (ret); 758 } 759 760 int 761 imsg_composev_event(struct imsgev *iev, uint16_t type, uint32_t peerid, 762 pid_t pid, int fd, const struct iovec *iov, int iovcnt) 763 { 764 int ret; 765 766 if ((ret = imsg_composev(&iev->ibuf, type, peerid, 767 pid, fd, iov, iovcnt)) == -1) 768 return (ret); 769 imsg_event_add(iev); 770 return (ret); 771 } 772 773 void 774 proc_range(struct privsep *ps, enum privsep_procid id, int *n, int *m) 775 { 776 if (*n == -1) { 777 /* Use a range of all target instances */ 778 *n = 0; 779 *m = ps->ps_instances[id]; 780 } else { 781 /* Use only a single slot of the specified peer process */ 782 *m = *n + 1; 783 } 784 } 785 786 int 787 proc_compose_imsg(struct privsep *ps, enum privsep_procid id, int n, 788 uint16_t type, uint32_t peerid, int fd, void *data, uint16_t datalen) 789 { 790 int m; 791 792 proc_range(ps, id, &n, &m); 793 for (; n < m; n++) { 794 if (imsg_compose_event(&ps->ps_ievs[id][n], 795 type, peerid, ps->ps_instance + 1, fd, data, datalen) == -1) 796 return (-1); 797 } 798 799 return (0); 800 } 801 802 int 803 proc_compose(struct privsep *ps, enum privsep_procid id, 804 uint16_t type, void *data, uint16_t datalen) 805 { 806 return (proc_compose_imsg(ps, id, -1, type, -1, -1, data, datalen)); 807 } 808 809 int 810 proc_composev_imsg(struct privsep *ps, enum privsep_procid id, int n, 811 uint16_t type, uint32_t peerid, int fd, const struct iovec *iov, int iovcnt) 812 { 813 int m; 814 815 proc_range(ps, id, &n, &m); 816 for (; n < m; n++) 817 if (imsg_composev_event(&ps->ps_ievs[id][n], 818 type, peerid, ps->ps_instance + 1, fd, iov, iovcnt) == -1) 819 return (-1); 820 821 return (0); 822 } 823 824 int 825 proc_composev(struct privsep *ps, enum privsep_procid id, 826 uint16_t type, const struct iovec *iov, int iovcnt) 827 { 828 return (proc_composev_imsg(ps, id, -1, type, -1, -1, iov, iovcnt)); 829 } 830 831 int 832 proc_forward_imsg(struct privsep *ps, struct imsg *imsg, 833 enum privsep_procid id, int n) 834 { 835 return (proc_compose_imsg(ps, id, n, imsg->hdr.type, 836 imsg->hdr.peerid, -1, imsg->data, IMSG_DATA_SIZE(imsg))); 837 } 838 839 struct imsgbuf * 840 proc_ibuf(struct privsep *ps, enum privsep_procid id, int n) 841 { 842 int m; 843 844 proc_range(ps, id, &n, &m); 845 return (&ps->ps_ievs[id][n].ibuf); 846 } 847 848 struct imsgev * 849 proc_iev(struct privsep *ps, enum privsep_procid id, int n) 850 { 851 int m; 852 853 proc_range(ps, id, &n, &m); 854 return (&ps->ps_ievs[id][n]); 855 } 856 857 /* This function should only be called with care as it breaks async I/O */ 858 int 859 proc_flush_imsg(struct privsep *ps, enum privsep_procid id, int n) 860 { 861 struct imsgbuf *ibuf; 862 int m, ret = 0; 863 864 proc_range(ps, id, &n, &m); 865 for (; n < m; n++) { 866 if ((ibuf = proc_ibuf(ps, id, n)) == NULL) 867 return (-1); 868 if ((ret = imsgbuf_flush(ibuf)) == -1) 869 break; 870 imsg_event_add(&ps->ps_ievs[id][n]); 871 } 872 873 return (ret); 874 } 875