1 /* $OpenBSD: client.c,v 1.163 2024/08/26 07:30:46 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/socket.h> 21 #include <sys/uio.h> 22 #include <sys/un.h> 23 #include <sys/wait.h> 24 25 #include <errno.h> 26 #include <event.h> 27 #include <fcntl.h> 28 #include <imsg.h> 29 #include <signal.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <unistd.h> 33 34 #include "tmux.h" 35 36 static struct tmuxproc *client_proc; 37 static struct tmuxpeer *client_peer; 38 static uint64_t client_flags; 39 static int client_suspended; 40 static enum { 41 CLIENT_EXIT_NONE, 42 CLIENT_EXIT_DETACHED, 43 CLIENT_EXIT_DETACHED_HUP, 44 CLIENT_EXIT_LOST_TTY, 45 CLIENT_EXIT_TERMINATED, 46 CLIENT_EXIT_LOST_SERVER, 47 CLIENT_EXIT_EXITED, 48 CLIENT_EXIT_SERVER_EXITED, 49 CLIENT_EXIT_MESSAGE_PROVIDED 50 } client_exitreason = CLIENT_EXIT_NONE; 51 static int client_exitflag; 52 static int client_exitval; 53 static enum msgtype client_exittype; 54 static const char *client_exitsession; 55 static char *client_exitmessage; 56 static const char *client_execshell; 57 static const char *client_execcmd; 58 static int client_attached; 59 static struct client_files client_files = RB_INITIALIZER(&client_files); 60 61 static __dead void client_exec(const char *,const char *); 62 static int client_get_lock(char *); 63 static int client_connect(struct event_base *, const char *, 64 uint64_t); 65 static void client_send_identify(const char *, const char *, 66 char **, u_int, const char *, int); 67 static void client_signal(int); 68 static void client_dispatch(struct imsg *, void *); 69 static void client_dispatch_attached(struct imsg *); 70 static void client_dispatch_wait(struct imsg *); 71 static const char *client_exit_message(void); 72 73 /* 74 * Get server create lock. If already held then server start is happening in 75 * another client, so block until the lock is released and return -2 to 76 * retry. Return -1 on failure to continue and start the server anyway. 77 */ 78 static int 79 client_get_lock(char *lockfile) 80 { 81 int lockfd; 82 83 log_debug("lock file is %s", lockfile); 84 85 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) { 86 log_debug("open failed: %s", strerror(errno)); 87 return (-1); 88 } 89 90 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) { 91 log_debug("flock failed: %s", strerror(errno)); 92 if (errno != EAGAIN) 93 return (lockfd); 94 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR) 95 /* nothing */; 96 close(lockfd); 97 return (-2); 98 } 99 log_debug("flock succeeded"); 100 101 return (lockfd); 102 } 103 104 /* Connect client to server. */ 105 static int 106 client_connect(struct event_base *base, const char *path, uint64_t flags) 107 { 108 struct sockaddr_un sa; 109 size_t size; 110 int fd, lockfd = -1, locked = 0; 111 char *lockfile = NULL; 112 113 memset(&sa, 0, sizeof sa); 114 sa.sun_family = AF_UNIX; 115 size = strlcpy(sa.sun_path, path, sizeof sa.sun_path); 116 if (size >= sizeof sa.sun_path) { 117 errno = ENAMETOOLONG; 118 return (-1); 119 } 120 log_debug("socket is %s", path); 121 122 retry: 123 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 124 return (-1); 125 126 log_debug("trying connect"); 127 if (connect(fd, (struct sockaddr *)&sa, sizeof sa) == -1) { 128 log_debug("connect failed: %s", strerror(errno)); 129 if (errno != ECONNREFUSED && errno != ENOENT) 130 goto failed; 131 if (flags & CLIENT_NOSTARTSERVER) 132 goto failed; 133 if (~flags & CLIENT_STARTSERVER) 134 goto failed; 135 close(fd); 136 137 if (!locked) { 138 xasprintf(&lockfile, "%s.lock", path); 139 if ((lockfd = client_get_lock(lockfile)) < 0) { 140 log_debug("didn't get lock (%d)", lockfd); 141 142 free(lockfile); 143 lockfile = NULL; 144 145 if (lockfd == -2) 146 goto retry; 147 } 148 log_debug("got lock (%d)", lockfd); 149 150 /* 151 * Always retry at least once, even if we got the lock, 152 * because another client could have taken the lock, 153 * started the server and released the lock between our 154 * connect() and flock(). 155 */ 156 locked = 1; 157 goto retry; 158 } 159 160 if (lockfd >= 0 && unlink(path) != 0 && errno != ENOENT) { 161 free(lockfile); 162 close(lockfd); 163 return (-1); 164 } 165 fd = server_start(client_proc, flags, base, lockfd, lockfile); 166 } 167 168 if (locked && lockfd >= 0) { 169 free(lockfile); 170 close(lockfd); 171 } 172 setblocking(fd, 0); 173 return (fd); 174 175 failed: 176 if (locked) { 177 free(lockfile); 178 close(lockfd); 179 } 180 close(fd); 181 return (-1); 182 } 183 184 /* Get exit string from reason number. */ 185 const char * 186 client_exit_message(void) 187 { 188 static char msg[256]; 189 190 switch (client_exitreason) { 191 case CLIENT_EXIT_NONE: 192 break; 193 case CLIENT_EXIT_DETACHED: 194 if (client_exitsession != NULL) { 195 xsnprintf(msg, sizeof msg, "detached " 196 "(from session %s)", client_exitsession); 197 return (msg); 198 } 199 return ("detached"); 200 case CLIENT_EXIT_DETACHED_HUP: 201 if (client_exitsession != NULL) { 202 xsnprintf(msg, sizeof msg, "detached and SIGHUP " 203 "(from session %s)", client_exitsession); 204 return (msg); 205 } 206 return ("detached and SIGHUP"); 207 case CLIENT_EXIT_LOST_TTY: 208 return ("lost tty"); 209 case CLIENT_EXIT_TERMINATED: 210 return ("terminated"); 211 case CLIENT_EXIT_LOST_SERVER: 212 return ("server exited unexpectedly"); 213 case CLIENT_EXIT_EXITED: 214 return ("exited"); 215 case CLIENT_EXIT_SERVER_EXITED: 216 return ("server exited"); 217 case CLIENT_EXIT_MESSAGE_PROVIDED: 218 return (client_exitmessage); 219 } 220 return ("unknown reason"); 221 } 222 223 /* Exit if all streams flushed. */ 224 static void 225 client_exit(void) 226 { 227 if (!file_write_left(&client_files)) 228 proc_exit(client_proc); 229 } 230 231 /* Client main loop. */ 232 int 233 client_main(struct event_base *base, int argc, char **argv, uint64_t flags, 234 int feat) 235 { 236 struct cmd_parse_result *pr; 237 struct msg_command *data; 238 int fd, i; 239 const char *ttynam, *termname, *cwd; 240 pid_t ppid; 241 enum msgtype msg; 242 struct termios tio, saved_tio; 243 size_t size, linesize = 0; 244 ssize_t linelen; 245 char *line = NULL, **caps = NULL, *cause; 246 u_int ncaps = 0; 247 struct args_value *values; 248 249 /* Set up the initial command. */ 250 if (shell_command != NULL) { 251 msg = MSG_SHELL; 252 flags |= CLIENT_STARTSERVER; 253 } else if (argc == 0) { 254 msg = MSG_COMMAND; 255 flags |= CLIENT_STARTSERVER; 256 } else { 257 msg = MSG_COMMAND; 258 259 /* 260 * It's annoying parsing the command string twice (in client 261 * and later in server) but it is necessary to get the start 262 * server flag. 263 */ 264 values = args_from_vector(argc, argv); 265 pr = cmd_parse_from_arguments(values, argc, NULL); 266 if (pr->status == CMD_PARSE_SUCCESS) { 267 if (cmd_list_any_have(pr->cmdlist, CMD_STARTSERVER)) 268 flags |= CLIENT_STARTSERVER; 269 cmd_list_free(pr->cmdlist); 270 } else 271 free(pr->error); 272 args_free_values(values, argc); 273 free(values); 274 } 275 276 /* Create client process structure (starts logging). */ 277 client_proc = proc_start("client"); 278 proc_set_signals(client_proc, client_signal); 279 280 /* Save the flags. */ 281 client_flags = flags; 282 log_debug("flags are %#llx", (unsigned long long)client_flags); 283 284 /* Initialize the client socket and start the server. */ 285 fd = client_connect(base, socket_path, client_flags); 286 if (fd == -1) { 287 if (errno == ECONNREFUSED) { 288 fprintf(stderr, "no server running on %s\n", 289 socket_path); 290 } else { 291 fprintf(stderr, "error connecting to %s (%s)\n", 292 socket_path, strerror(errno)); 293 } 294 return (1); 295 } 296 client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL); 297 298 /* Save these before pledge(). */ 299 if ((cwd = find_cwd()) == NULL && (cwd = find_home()) == NULL) 300 cwd = "/"; 301 if ((ttynam = ttyname(STDIN_FILENO)) == NULL) 302 ttynam = ""; 303 if ((termname = getenv("TERM")) == NULL) 304 termname = ""; 305 306 /* 307 * Drop privileges for client. "proc exec" is needed for -c and for 308 * locking (which uses system(3)). 309 * 310 * "tty" is needed to restore termios(4) and also for some reason -CC 311 * does not work properly without it (input is not recognised). 312 * 313 * "sendfd" is dropped later in client_dispatch_wait(). 314 */ 315 if (pledge( 316 "stdio rpath wpath cpath unix sendfd proc exec tty", 317 NULL) != 0) 318 fatal("pledge failed"); 319 320 /* Load terminfo entry if any. */ 321 if (isatty(STDIN_FILENO) && 322 *termname != '\0' && 323 tty_term_read_list(termname, STDIN_FILENO, &caps, &ncaps, 324 &cause) != 0) { 325 fprintf(stderr, "%s\n", cause); 326 free(cause); 327 return (1); 328 } 329 330 /* Free stuff that is not used in the client. */ 331 if (ptm_fd != -1) 332 close(ptm_fd); 333 options_free(global_options); 334 options_free(global_s_options); 335 options_free(global_w_options); 336 environ_free(global_environ); 337 338 /* Set up control mode. */ 339 if (client_flags & CLIENT_CONTROLCONTROL) { 340 if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) { 341 fprintf(stderr, "tcgetattr failed: %s\n", 342 strerror(errno)); 343 return (1); 344 } 345 cfmakeraw(&tio); 346 tio.c_iflag = ICRNL|IXANY; 347 tio.c_oflag = OPOST|ONLCR; 348 tio.c_lflag = NOKERNINFO; 349 tio.c_cflag = CREAD|CS8|HUPCL; 350 tio.c_cc[VMIN] = 1; 351 tio.c_cc[VTIME] = 0; 352 cfsetispeed(&tio, cfgetispeed(&saved_tio)); 353 cfsetospeed(&tio, cfgetospeed(&saved_tio)); 354 tcsetattr(STDIN_FILENO, TCSANOW, &tio); 355 } 356 357 /* Send identify messages. */ 358 client_send_identify(ttynam, termname, caps, ncaps, cwd, feat); 359 tty_term_free_list(caps, ncaps); 360 proc_flush_peer(client_peer); 361 362 /* Send first command. */ 363 if (msg == MSG_COMMAND) { 364 /* How big is the command? */ 365 size = 0; 366 for (i = 0; i < argc; i++) 367 size += strlen(argv[i]) + 1; 368 if (size > MAX_IMSGSIZE - (sizeof *data)) { 369 fprintf(stderr, "command too long\n"); 370 return (1); 371 } 372 data = xmalloc((sizeof *data) + size); 373 374 /* Prepare command for server. */ 375 data->argc = argc; 376 if (cmd_pack_argv(argc, argv, (char *)(data + 1), size) != 0) { 377 fprintf(stderr, "command too long\n"); 378 free(data); 379 return (1); 380 } 381 size += sizeof *data; 382 383 /* Send the command. */ 384 if (proc_send(client_peer, msg, -1, data, size) != 0) { 385 fprintf(stderr, "failed to send command\n"); 386 free(data); 387 return (1); 388 } 389 free(data); 390 } else if (msg == MSG_SHELL) 391 proc_send(client_peer, msg, -1, NULL, 0); 392 393 /* Start main loop. */ 394 proc_loop(client_proc, NULL); 395 396 /* Run command if user requested exec, instead of exiting. */ 397 if (client_exittype == MSG_EXEC) { 398 if (client_flags & CLIENT_CONTROLCONTROL) 399 tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio); 400 client_exec(client_execshell, client_execcmd); 401 } 402 403 /* Restore streams to blocking. */ 404 setblocking(STDIN_FILENO, 1); 405 setblocking(STDOUT_FILENO, 1); 406 setblocking(STDERR_FILENO, 1); 407 408 /* Print the exit message, if any, and exit. */ 409 if (client_attached) { 410 if (client_exitreason != CLIENT_EXIT_NONE) 411 printf("[%s]\n", client_exit_message()); 412 413 ppid = getppid(); 414 if (client_exittype == MSG_DETACHKILL && ppid > 1) 415 kill(ppid, SIGHUP); 416 } else if (client_flags & CLIENT_CONTROL) { 417 if (client_exitreason != CLIENT_EXIT_NONE) 418 printf("%%exit %s\n", client_exit_message()); 419 else 420 printf("%%exit\n"); 421 fflush(stdout); 422 if (client_flags & CLIENT_CONTROL_WAITEXIT) { 423 setvbuf(stdin, NULL, _IOLBF, 0); 424 for (;;) { 425 linelen = getline(&line, &linesize, stdin); 426 if (linelen <= 1) 427 break; 428 } 429 free(line); 430 } 431 if (client_flags & CLIENT_CONTROLCONTROL) { 432 printf("\033\\"); 433 fflush(stdout); 434 tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio); 435 } 436 } else if (client_exitreason != CLIENT_EXIT_NONE) 437 fprintf(stderr, "%s\n", client_exit_message()); 438 return (client_exitval); 439 } 440 441 /* Send identify messages to server. */ 442 static void 443 client_send_identify(const char *ttynam, const char *termname, char **caps, 444 u_int ncaps, const char *cwd, int feat) 445 { 446 char **ss; 447 size_t sslen; 448 int fd; 449 uint64_t flags = client_flags; 450 pid_t pid; 451 u_int i; 452 453 proc_send(client_peer, MSG_IDENTIFY_LONGFLAGS, -1, &flags, sizeof flags); 454 proc_send(client_peer, MSG_IDENTIFY_LONGFLAGS, -1, &client_flags, 455 sizeof client_flags); 456 457 proc_send(client_peer, MSG_IDENTIFY_TERM, -1, termname, 458 strlen(termname) + 1); 459 proc_send(client_peer, MSG_IDENTIFY_FEATURES, -1, &feat, sizeof feat); 460 461 proc_send(client_peer, MSG_IDENTIFY_TTYNAME, -1, ttynam, 462 strlen(ttynam) + 1); 463 proc_send(client_peer, MSG_IDENTIFY_CWD, -1, cwd, strlen(cwd) + 1); 464 465 for (i = 0; i < ncaps; i++) { 466 proc_send(client_peer, MSG_IDENTIFY_TERMINFO, -1, 467 caps[i], strlen(caps[i]) + 1); 468 } 469 470 if ((fd = dup(STDIN_FILENO)) == -1) 471 fatal("dup failed"); 472 proc_send(client_peer, MSG_IDENTIFY_STDIN, fd, NULL, 0); 473 if ((fd = dup(STDOUT_FILENO)) == -1) 474 fatal("dup failed"); 475 proc_send(client_peer, MSG_IDENTIFY_STDOUT, fd, NULL, 0); 476 477 pid = getpid(); 478 proc_send(client_peer, MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid); 479 480 for (ss = environ; *ss != NULL; ss++) { 481 sslen = strlen(*ss) + 1; 482 if (sslen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) 483 continue; 484 proc_send(client_peer, MSG_IDENTIFY_ENVIRON, -1, *ss, sslen); 485 } 486 487 proc_send(client_peer, MSG_IDENTIFY_DONE, -1, NULL, 0); 488 } 489 490 /* Run command in shell; used for -c. */ 491 static __dead void 492 client_exec(const char *shell, const char *shellcmd) 493 { 494 char *argv0; 495 496 log_debug("shell %s, command %s", shell, shellcmd); 497 argv0 = shell_argv0(shell, !!(client_flags & CLIENT_LOGIN)); 498 setenv("SHELL", shell, 1); 499 500 proc_clear_signals(client_proc, 1); 501 502 setblocking(STDIN_FILENO, 1); 503 setblocking(STDOUT_FILENO, 1); 504 setblocking(STDERR_FILENO, 1); 505 closefrom(STDERR_FILENO + 1); 506 507 execl(shell, argv0, "-c", shellcmd, (char *) NULL); 508 fatal("execl failed"); 509 } 510 511 /* Callback to handle signals in the client. */ 512 static void 513 client_signal(int sig) 514 { 515 struct sigaction sigact; 516 int status; 517 pid_t pid; 518 519 log_debug("%s: %s", __func__, strsignal(sig)); 520 if (sig == SIGCHLD) { 521 for (;;) { 522 pid = waitpid(WAIT_ANY, &status, WNOHANG); 523 if (pid == 0) 524 break; 525 if (pid == -1) { 526 if (errno == ECHILD) 527 break; 528 log_debug("waitpid failed: %s", 529 strerror(errno)); 530 } 531 } 532 } else if (!client_attached) { 533 if (sig == SIGTERM || sig == SIGHUP) 534 proc_exit(client_proc); 535 } else { 536 switch (sig) { 537 case SIGHUP: 538 client_exitreason = CLIENT_EXIT_LOST_TTY; 539 client_exitval = 1; 540 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 541 break; 542 case SIGTERM: 543 if (!client_suspended) 544 client_exitreason = CLIENT_EXIT_TERMINATED; 545 client_exitval = 1; 546 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 547 break; 548 case SIGWINCH: 549 proc_send(client_peer, MSG_RESIZE, -1, NULL, 0); 550 break; 551 case SIGCONT: 552 memset(&sigact, 0, sizeof sigact); 553 sigemptyset(&sigact.sa_mask); 554 sigact.sa_flags = SA_RESTART; 555 sigact.sa_handler = SIG_IGN; 556 if (sigaction(SIGTSTP, &sigact, NULL) != 0) 557 fatal("sigaction failed"); 558 proc_send(client_peer, MSG_WAKEUP, -1, NULL, 0); 559 client_suspended = 0; 560 break; 561 } 562 } 563 } 564 565 /* Callback for file write error or close. */ 566 static void 567 client_file_check_cb(__unused struct client *c, __unused const char *path, 568 __unused int error, __unused int closed, __unused struct evbuffer *buffer, 569 __unused void *data) 570 { 571 if (client_exitflag) 572 client_exit(); 573 } 574 575 /* Callback for client read events. */ 576 static void 577 client_dispatch(struct imsg *imsg, __unused void *arg) 578 { 579 if (imsg == NULL) { 580 if (!client_exitflag) { 581 client_exitreason = CLIENT_EXIT_LOST_SERVER; 582 client_exitval = 1; 583 } 584 proc_exit(client_proc); 585 return; 586 } 587 588 if (client_attached) 589 client_dispatch_attached(imsg); 590 else 591 client_dispatch_wait(imsg); 592 } 593 594 /* Process an exit message. */ 595 static void 596 client_dispatch_exit_message(char *data, size_t datalen) 597 { 598 int retval; 599 600 if (datalen < sizeof retval && datalen != 0) 601 fatalx("bad MSG_EXIT size"); 602 603 if (datalen >= sizeof retval) { 604 memcpy(&retval, data, sizeof retval); 605 client_exitval = retval; 606 } 607 608 if (datalen > sizeof retval) { 609 datalen -= sizeof retval; 610 data += sizeof retval; 611 612 client_exitmessage = xmalloc(datalen); 613 memcpy(client_exitmessage, data, datalen); 614 client_exitmessage[datalen - 1] = '\0'; 615 616 client_exitreason = CLIENT_EXIT_MESSAGE_PROVIDED; 617 } 618 } 619 620 /* Dispatch imsgs when in wait state (before MSG_READY). */ 621 static void 622 client_dispatch_wait(struct imsg *imsg) 623 { 624 char *data; 625 ssize_t datalen; 626 static int pledge_applied; 627 628 /* 629 * "sendfd" is no longer required once all of the identify messages 630 * have been sent. We know the server won't send us anything until that 631 * point (because we don't ask it to), so we can drop "sendfd" once we 632 * get the first message from the server. 633 */ 634 if (!pledge_applied) { 635 if (pledge( 636 "stdio rpath wpath cpath unix proc exec tty", 637 NULL) != 0) 638 fatal("pledge failed"); 639 pledge_applied = 1; 640 } 641 642 data = imsg->data; 643 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 644 645 switch (imsg->hdr.type) { 646 case MSG_EXIT: 647 case MSG_SHUTDOWN: 648 client_dispatch_exit_message(data, datalen); 649 client_exitflag = 1; 650 client_exit(); 651 break; 652 case MSG_READY: 653 if (datalen != 0) 654 fatalx("bad MSG_READY size"); 655 656 client_attached = 1; 657 proc_send(client_peer, MSG_RESIZE, -1, NULL, 0); 658 break; 659 case MSG_VERSION: 660 if (datalen != 0) 661 fatalx("bad MSG_VERSION size"); 662 663 fprintf(stderr, "protocol version mismatch " 664 "(client %d, server %u)\n", PROTOCOL_VERSION, 665 imsg->hdr.peerid & 0xff); 666 client_exitval = 1; 667 proc_exit(client_proc); 668 break; 669 case MSG_FLAGS: 670 if (datalen != sizeof client_flags) 671 fatalx("bad MSG_FLAGS string"); 672 673 memcpy(&client_flags, data, sizeof client_flags); 674 log_debug("new flags are %#llx", 675 (unsigned long long)client_flags); 676 break; 677 case MSG_SHELL: 678 if (datalen == 0 || data[datalen - 1] != '\0') 679 fatalx("bad MSG_SHELL string"); 680 681 client_exec(data, shell_command); 682 /* NOTREACHED */ 683 case MSG_DETACH: 684 case MSG_DETACHKILL: 685 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 686 break; 687 case MSG_EXITED: 688 proc_exit(client_proc); 689 break; 690 case MSG_READ_OPEN: 691 file_read_open(&client_files, client_peer, imsg, 1, 692 !(client_flags & CLIENT_CONTROL), client_file_check_cb, 693 NULL); 694 break; 695 case MSG_READ_CANCEL: 696 file_read_cancel(&client_files, imsg); 697 break; 698 case MSG_WRITE_OPEN: 699 file_write_open(&client_files, client_peer, imsg, 1, 700 !(client_flags & CLIENT_CONTROL), client_file_check_cb, 701 NULL); 702 break; 703 case MSG_WRITE: 704 file_write_data(&client_files, imsg); 705 break; 706 case MSG_WRITE_CLOSE: 707 file_write_close(&client_files, imsg); 708 break; 709 case MSG_OLDSTDERR: 710 case MSG_OLDSTDIN: 711 case MSG_OLDSTDOUT: 712 fprintf(stderr, "server version is too old for client\n"); 713 proc_exit(client_proc); 714 break; 715 } 716 } 717 718 /* Dispatch imsgs in attached state (after MSG_READY). */ 719 static void 720 client_dispatch_attached(struct imsg *imsg) 721 { 722 struct sigaction sigact; 723 char *data; 724 ssize_t datalen; 725 726 data = imsg->data; 727 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 728 729 switch (imsg->hdr.type) { 730 case MSG_FLAGS: 731 if (datalen != sizeof client_flags) 732 fatalx("bad MSG_FLAGS string"); 733 734 memcpy(&client_flags, data, sizeof client_flags); 735 log_debug("new flags are %#llx", 736 (unsigned long long)client_flags); 737 break; 738 case MSG_DETACH: 739 case MSG_DETACHKILL: 740 if (datalen == 0 || data[datalen - 1] != '\0') 741 fatalx("bad MSG_DETACH string"); 742 743 client_exitsession = xstrdup(data); 744 client_exittype = imsg->hdr.type; 745 if (imsg->hdr.type == MSG_DETACHKILL) 746 client_exitreason = CLIENT_EXIT_DETACHED_HUP; 747 else 748 client_exitreason = CLIENT_EXIT_DETACHED; 749 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 750 break; 751 case MSG_EXEC: 752 if (datalen == 0 || data[datalen - 1] != '\0' || 753 strlen(data) + 1 == (size_t)datalen) 754 fatalx("bad MSG_EXEC string"); 755 client_execcmd = xstrdup(data); 756 client_execshell = xstrdup(data + strlen(data) + 1); 757 758 client_exittype = imsg->hdr.type; 759 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 760 break; 761 case MSG_EXIT: 762 client_dispatch_exit_message(data, datalen); 763 if (client_exitreason == CLIENT_EXIT_NONE) 764 client_exitreason = CLIENT_EXIT_EXITED; 765 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 766 break; 767 case MSG_EXITED: 768 if (datalen != 0) 769 fatalx("bad MSG_EXITED size"); 770 771 proc_exit(client_proc); 772 break; 773 case MSG_SHUTDOWN: 774 if (datalen != 0) 775 fatalx("bad MSG_SHUTDOWN size"); 776 777 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 778 client_exitreason = CLIENT_EXIT_SERVER_EXITED; 779 client_exitval = 1; 780 break; 781 case MSG_SUSPEND: 782 if (datalen != 0) 783 fatalx("bad MSG_SUSPEND size"); 784 785 memset(&sigact, 0, sizeof sigact); 786 sigemptyset(&sigact.sa_mask); 787 sigact.sa_flags = SA_RESTART; 788 sigact.sa_handler = SIG_DFL; 789 if (sigaction(SIGTSTP, &sigact, NULL) != 0) 790 fatal("sigaction failed"); 791 client_suspended = 1; 792 kill(getpid(), SIGTSTP); 793 break; 794 case MSG_LOCK: 795 if (datalen == 0 || data[datalen - 1] != '\0') 796 fatalx("bad MSG_LOCK string"); 797 798 system(data); 799 proc_send(client_peer, MSG_UNLOCK, -1, NULL, 0); 800 break; 801 } 802 } 803