1 /* $OpenBSD: client.c,v 1.155 2021/02/17 07:18:36 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 248 /* Ignore SIGCHLD now or daemon() in the server will leave a zombie. */ 249 signal(SIGCHLD, SIG_IGN); 250 251 /* Set up the initial command. */ 252 if (shell_command != NULL) { 253 msg = MSG_SHELL; 254 flags |= CLIENT_STARTSERVER; 255 } else if (argc == 0) { 256 msg = MSG_COMMAND; 257 flags |= CLIENT_STARTSERVER; 258 } else { 259 msg = MSG_COMMAND; 260 261 /* 262 * It sucks parsing the command string twice (in client and 263 * later in server) but it is necessary to get the start server 264 * flag. 265 */ 266 pr = cmd_parse_from_arguments(argc, argv, NULL); 267 if (pr->status == CMD_PARSE_SUCCESS) { 268 if (cmd_list_any_have(pr->cmdlist, CMD_STARTSERVER)) 269 flags |= CLIENT_STARTSERVER; 270 cmd_list_free(pr->cmdlist); 271 } else 272 free(pr->error); 273 } 274 275 /* Create client process structure (starts logging). */ 276 client_proc = proc_start("client"); 277 proc_set_signals(client_proc, client_signal); 278 279 /* Save the flags. */ 280 client_flags = flags; 281 log_debug("flags are %#llx", (unsigned long long)client_flags); 282 283 /* Initialize the client socket and start the server. */ 284 fd = client_connect(base, socket_path, client_flags); 285 if (fd == -1) { 286 if (errno == ECONNREFUSED) { 287 fprintf(stderr, "no server running on %s\n", 288 socket_path); 289 } else { 290 fprintf(stderr, "error connecting to %s (%s)\n", 291 socket_path, strerror(errno)); 292 } 293 return (1); 294 } 295 client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL); 296 297 /* Save these before pledge(). */ 298 if ((cwd = find_cwd()) == NULL && (cwd = find_home()) == NULL) 299 cwd = "/"; 300 if ((ttynam = ttyname(STDIN_FILENO)) == NULL) 301 ttynam = ""; 302 if ((termname = getenv("TERM")) == NULL) 303 termname = ""; 304 305 /* 306 * Drop privileges for client. "proc exec" is needed for -c and for 307 * locking (which uses system(3)). 308 * 309 * "tty" is needed to restore termios(4) and also for some reason -CC 310 * does not work properly without it (input is not recognised). 311 * 312 * "sendfd" is dropped later in client_dispatch_wait(). 313 */ 314 if (pledge( 315 "stdio rpath wpath cpath unix sendfd proc exec tty", 316 NULL) != 0) 317 fatal("pledge failed"); 318 319 /* Load terminfo entry if any. */ 320 if (isatty(STDIN_FILENO) && 321 *termname != '\0' && 322 tty_term_read_list(termname, STDIN_FILENO, &caps, &ncaps, 323 &cause) != 0) { 324 fprintf(stderr, "%s\n", cause); 325 free(cause); 326 return (1); 327 } 328 329 /* Free stuff that is not used in the client. */ 330 if (ptm_fd != -1) 331 close(ptm_fd); 332 options_free(global_options); 333 options_free(global_s_options); 334 options_free(global_w_options); 335 environ_free(global_environ); 336 337 /* Set up control mode. */ 338 if (client_flags & CLIENT_CONTROLCONTROL) { 339 if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) { 340 fprintf(stderr, "tcgetattr failed: %s\n", 341 strerror(errno)); 342 return (1); 343 } 344 cfmakeraw(&tio); 345 tio.c_iflag = ICRNL|IXANY; 346 tio.c_oflag = OPOST|ONLCR; 347 tio.c_lflag = NOKERNINFO; 348 tio.c_cflag = CREAD|CS8|HUPCL; 349 tio.c_cc[VMIN] = 1; 350 tio.c_cc[VTIME] = 0; 351 cfsetispeed(&tio, cfgetispeed(&saved_tio)); 352 cfsetospeed(&tio, cfgetospeed(&saved_tio)); 353 tcsetattr(STDIN_FILENO, TCSANOW, &tio); 354 } 355 356 /* Send identify messages. */ 357 client_send_identify(ttynam, termname, caps, ncaps, cwd, feat); 358 tty_term_free_list(caps, ncaps); 359 360 /* Send first command. */ 361 if (msg == MSG_COMMAND) { 362 /* How big is the command? */ 363 size = 0; 364 for (i = 0; i < argc; i++) 365 size += strlen(argv[i]) + 1; 366 if (size > MAX_IMSGSIZE - (sizeof *data)) { 367 fprintf(stderr, "command too long\n"); 368 return (1); 369 } 370 data = xmalloc((sizeof *data) + size); 371 372 /* Prepare command for server. */ 373 data->argc = argc; 374 if (cmd_pack_argv(argc, argv, (char *)(data + 1), size) != 0) { 375 fprintf(stderr, "command too long\n"); 376 free(data); 377 return (1); 378 } 379 size += sizeof *data; 380 381 /* Send the command. */ 382 if (proc_send(client_peer, msg, -1, data, size) != 0) { 383 fprintf(stderr, "failed to send command\n"); 384 free(data); 385 return (1); 386 } 387 free(data); 388 } else if (msg == MSG_SHELL) 389 proc_send(client_peer, msg, -1, NULL, 0); 390 391 /* Start main loop. */ 392 proc_loop(client_proc, NULL); 393 394 /* Run command if user requested exec, instead of exiting. */ 395 if (client_exittype == MSG_EXEC) { 396 if (client_flags & CLIENT_CONTROLCONTROL) 397 tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio); 398 client_exec(client_execshell, client_execcmd); 399 } 400 401 /* Restore streams to blocking. */ 402 setblocking(STDIN_FILENO, 1); 403 setblocking(STDOUT_FILENO, 1); 404 setblocking(STDERR_FILENO, 1); 405 406 /* Print the exit message, if any, and exit. */ 407 if (client_attached) { 408 if (client_exitreason != CLIENT_EXIT_NONE) 409 printf("[%s]\n", client_exit_message()); 410 411 ppid = getppid(); 412 if (client_exittype == MSG_DETACHKILL && ppid > 1) 413 kill(ppid, SIGHUP); 414 } else if (client_flags & CLIENT_CONTROL) { 415 if (client_exitreason != CLIENT_EXIT_NONE) 416 printf("%%exit %s\n", client_exit_message()); 417 else 418 printf("%%exit\n"); 419 fflush(stdout); 420 if (client_flags & CLIENT_CONTROL_WAITEXIT) { 421 setvbuf(stdin, NULL, _IOLBF, 0); 422 for (;;) { 423 linelen = getline(&line, &linesize, stdin); 424 if (linelen <= 1) 425 break; 426 } 427 free(line); 428 } 429 if (client_flags & CLIENT_CONTROLCONTROL) { 430 printf("\033\\"); 431 fflush(stdout); 432 tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio); 433 } 434 } else if (client_exitreason != CLIENT_EXIT_NONE) 435 fprintf(stderr, "%s\n", client_exit_message()); 436 return (client_exitval); 437 } 438 439 /* Send identify messages to server. */ 440 static void 441 client_send_identify(const char *ttynam, const char *termname, char **caps, 442 u_int ncaps, const char *cwd, int feat) 443 { 444 char **ss; 445 size_t sslen; 446 int fd, flags = client_flags; 447 pid_t pid; 448 u_int i; 449 450 proc_send(client_peer, MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags); 451 proc_send(client_peer, MSG_IDENTIFY_LONGFLAGS, -1, &client_flags, 452 sizeof client_flags); 453 454 proc_send(client_peer, MSG_IDENTIFY_TERM, -1, termname, 455 strlen(termname) + 1); 456 proc_send(client_peer, MSG_IDENTIFY_FEATURES, -1, &feat, sizeof feat); 457 458 proc_send(client_peer, MSG_IDENTIFY_TTYNAME, -1, ttynam, 459 strlen(ttynam) + 1); 460 proc_send(client_peer, MSG_IDENTIFY_CWD, -1, cwd, strlen(cwd) + 1); 461 462 for (i = 0; i < ncaps; i++) { 463 proc_send(client_peer, MSG_IDENTIFY_TERMINFO, -1, 464 caps[i], strlen(caps[i]) + 1); 465 } 466 467 if ((fd = dup(STDIN_FILENO)) == -1) 468 fatal("dup failed"); 469 proc_send(client_peer, MSG_IDENTIFY_STDIN, fd, NULL, 0); 470 if ((fd = dup(STDOUT_FILENO)) == -1) 471 fatal("dup failed"); 472 proc_send(client_peer, MSG_IDENTIFY_STDOUT, fd, NULL, 0); 473 474 pid = getpid(); 475 proc_send(client_peer, MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid); 476 477 for (ss = environ; *ss != NULL; ss++) { 478 sslen = strlen(*ss) + 1; 479 if (sslen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) 480 continue; 481 proc_send(client_peer, MSG_IDENTIFY_ENVIRON, -1, *ss, sslen); 482 } 483 484 proc_send(client_peer, MSG_IDENTIFY_DONE, -1, NULL, 0); 485 } 486 487 /* Run command in shell; used for -c. */ 488 static __dead void 489 client_exec(const char *shell, const char *shellcmd) 490 { 491 const char *name, *ptr; 492 char *argv0; 493 494 log_debug("shell %s, command %s", shell, shellcmd); 495 496 ptr = strrchr(shell, '/'); 497 if (ptr != NULL && *(ptr + 1) != '\0') 498 name = ptr + 1; 499 else 500 name = shell; 501 if (client_flags & CLIENT_LOGIN) 502 xasprintf(&argv0, "-%s", name); 503 else 504 xasprintf(&argv0, "%s", name); 505 setenv("SHELL", shell, 1); 506 507 proc_clear_signals(client_proc, 1); 508 509 setblocking(STDIN_FILENO, 1); 510 setblocking(STDOUT_FILENO, 1); 511 setblocking(STDERR_FILENO, 1); 512 closefrom(STDERR_FILENO + 1); 513 514 execl(shell, argv0, "-c", shellcmd, (char *) NULL); 515 fatal("execl failed"); 516 } 517 518 /* Callback to handle signals in the client. */ 519 static void 520 client_signal(int sig) 521 { 522 struct sigaction sigact; 523 int status; 524 525 log_debug("%s: %s", __func__, strsignal(sig)); 526 if (sig == SIGCHLD) 527 waitpid(WAIT_ANY, &status, WNOHANG); 528 else if (!client_attached) { 529 if (sig == SIGTERM) 530 proc_exit(client_proc); 531 } else { 532 switch (sig) { 533 case SIGHUP: 534 client_exitreason = CLIENT_EXIT_LOST_TTY; 535 client_exitval = 1; 536 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 537 break; 538 case SIGTERM: 539 if (!client_suspended) 540 client_exitreason = CLIENT_EXIT_TERMINATED; 541 client_exitval = 1; 542 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 543 break; 544 case SIGWINCH: 545 proc_send(client_peer, MSG_RESIZE, -1, NULL, 0); 546 break; 547 case SIGCONT: 548 memset(&sigact, 0, sizeof sigact); 549 sigemptyset(&sigact.sa_mask); 550 sigact.sa_flags = SA_RESTART; 551 sigact.sa_handler = SIG_IGN; 552 if (sigaction(SIGTSTP, &sigact, NULL) != 0) 553 fatal("sigaction failed"); 554 proc_send(client_peer, MSG_WAKEUP, -1, NULL, 0); 555 client_suspended = 0; 556 break; 557 } 558 } 559 } 560 561 /* Callback for file write error or close. */ 562 static void 563 client_file_check_cb(__unused struct client *c, __unused const char *path, 564 __unused int error, __unused int closed, __unused struct evbuffer *buffer, 565 __unused void *data) 566 { 567 if (client_exitflag) 568 client_exit(); 569 } 570 571 /* Callback for client read events. */ 572 static void 573 client_dispatch(struct imsg *imsg, __unused void *arg) 574 { 575 if (imsg == NULL) { 576 if (!client_exitflag) { 577 client_exitreason = CLIENT_EXIT_LOST_SERVER; 578 client_exitval = 1; 579 } 580 proc_exit(client_proc); 581 return; 582 } 583 584 if (client_attached) 585 client_dispatch_attached(imsg); 586 else 587 client_dispatch_wait(imsg); 588 } 589 590 /* Process an exit message. */ 591 static void 592 client_dispatch_exit_message(char *data, size_t datalen) 593 { 594 int retval; 595 596 if (datalen < sizeof retval && datalen != 0) 597 fatalx("bad MSG_EXIT size"); 598 599 if (datalen >= sizeof retval) { 600 memcpy(&retval, data, sizeof retval); 601 client_exitval = retval; 602 } 603 604 if (datalen > sizeof retval) { 605 datalen -= sizeof retval; 606 data += sizeof retval; 607 608 client_exitmessage = xmalloc(datalen); 609 memcpy(client_exitmessage, data, datalen); 610 client_exitmessage[datalen - 1] = '\0'; 611 612 client_exitreason = CLIENT_EXIT_MESSAGE_PROVIDED; 613 } 614 } 615 616 /* Dispatch imsgs when in wait state (before MSG_READY). */ 617 static void 618 client_dispatch_wait(struct imsg *imsg) 619 { 620 char *data; 621 ssize_t datalen; 622 static int pledge_applied; 623 624 /* 625 * "sendfd" is no longer required once all of the identify messages 626 * have been sent. We know the server won't send us anything until that 627 * point (because we don't ask it to), so we can drop "sendfd" once we 628 * get the first message from the server. 629 */ 630 if (!pledge_applied) { 631 if (pledge( 632 "stdio rpath wpath cpath unix proc exec tty", 633 NULL) != 0) 634 fatal("pledge failed"); 635 pledge_applied = 1; 636 } 637 638 data = imsg->data; 639 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 640 641 switch (imsg->hdr.type) { 642 case MSG_EXIT: 643 case MSG_SHUTDOWN: 644 client_dispatch_exit_message(data, datalen); 645 client_exitflag = 1; 646 client_exit(); 647 break; 648 case MSG_READY: 649 if (datalen != 0) 650 fatalx("bad MSG_READY size"); 651 652 client_attached = 1; 653 proc_send(client_peer, MSG_RESIZE, -1, NULL, 0); 654 break; 655 case MSG_VERSION: 656 if (datalen != 0) 657 fatalx("bad MSG_VERSION size"); 658 659 fprintf(stderr, "protocol version mismatch " 660 "(client %d, server %u)\n", PROTOCOL_VERSION, 661 imsg->hdr.peerid & 0xff); 662 client_exitval = 1; 663 proc_exit(client_proc); 664 break; 665 case MSG_FLAGS: 666 if (datalen != sizeof client_flags) 667 fatalx("bad MSG_FLAGS string"); 668 669 memcpy(&client_flags, data, sizeof client_flags); 670 log_debug("new flags are %#llx", 671 (unsigned long long)client_flags); 672 break; 673 case MSG_SHELL: 674 if (datalen == 0 || data[datalen - 1] != '\0') 675 fatalx("bad MSG_SHELL string"); 676 677 client_exec(data, shell_command); 678 /* NOTREACHED */ 679 case MSG_DETACH: 680 case MSG_DETACHKILL: 681 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 682 break; 683 case MSG_EXITED: 684 proc_exit(client_proc); 685 break; 686 case MSG_READ_OPEN: 687 file_read_open(&client_files, client_peer, imsg, 1, 688 !(client_flags & CLIENT_CONTROL), client_file_check_cb, 689 NULL); 690 break; 691 case MSG_WRITE_OPEN: 692 file_write_open(&client_files, client_peer, imsg, 1, 693 !(client_flags & CLIENT_CONTROL), client_file_check_cb, 694 NULL); 695 break; 696 case MSG_WRITE: 697 file_write_data(&client_files, imsg); 698 break; 699 case MSG_WRITE_CLOSE: 700 file_write_close(&client_files, imsg); 701 break; 702 case MSG_OLDSTDERR: 703 case MSG_OLDSTDIN: 704 case MSG_OLDSTDOUT: 705 fprintf(stderr, "server version is too old for client\n"); 706 proc_exit(client_proc); 707 break; 708 } 709 } 710 711 /* Dispatch imsgs in attached state (after MSG_READY). */ 712 static void 713 client_dispatch_attached(struct imsg *imsg) 714 { 715 struct sigaction sigact; 716 char *data; 717 ssize_t datalen; 718 719 data = imsg->data; 720 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 721 722 switch (imsg->hdr.type) { 723 case MSG_FLAGS: 724 if (datalen != sizeof client_flags) 725 fatalx("bad MSG_FLAGS string"); 726 727 memcpy(&client_flags, data, sizeof client_flags); 728 log_debug("new flags are %#llx", 729 (unsigned long long)client_flags); 730 break; 731 case MSG_DETACH: 732 case MSG_DETACHKILL: 733 if (datalen == 0 || data[datalen - 1] != '\0') 734 fatalx("bad MSG_DETACH string"); 735 736 client_exitsession = xstrdup(data); 737 client_exittype = imsg->hdr.type; 738 if (imsg->hdr.type == MSG_DETACHKILL) 739 client_exitreason = CLIENT_EXIT_DETACHED_HUP; 740 else 741 client_exitreason = CLIENT_EXIT_DETACHED; 742 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 743 break; 744 case MSG_EXEC: 745 if (datalen == 0 || data[datalen - 1] != '\0' || 746 strlen(data) + 1 == (size_t)datalen) 747 fatalx("bad MSG_EXEC string"); 748 client_execcmd = xstrdup(data); 749 client_execshell = xstrdup(data + strlen(data) + 1); 750 751 client_exittype = imsg->hdr.type; 752 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 753 break; 754 case MSG_EXIT: 755 client_dispatch_exit_message(data, datalen); 756 if (client_exitreason == CLIENT_EXIT_NONE) 757 client_exitreason = CLIENT_EXIT_EXITED; 758 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 759 break; 760 case MSG_EXITED: 761 if (datalen != 0) 762 fatalx("bad MSG_EXITED size"); 763 764 proc_exit(client_proc); 765 break; 766 case MSG_SHUTDOWN: 767 if (datalen != 0) 768 fatalx("bad MSG_SHUTDOWN size"); 769 770 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 771 client_exitreason = CLIENT_EXIT_SERVER_EXITED; 772 client_exitval = 1; 773 break; 774 case MSG_SUSPEND: 775 if (datalen != 0) 776 fatalx("bad MSG_SUSPEND size"); 777 778 memset(&sigact, 0, sizeof sigact); 779 sigemptyset(&sigact.sa_mask); 780 sigact.sa_flags = SA_RESTART; 781 sigact.sa_handler = SIG_DFL; 782 if (sigaction(SIGTSTP, &sigact, NULL) != 0) 783 fatal("sigaction failed"); 784 client_suspended = 1; 785 kill(getpid(), SIGTSTP); 786 break; 787 case MSG_LOCK: 788 if (datalen == 0 || data[datalen - 1] != '\0') 789 fatalx("bad MSG_LOCK string"); 790 791 system(data); 792 proc_send(client_peer, MSG_UNLOCK, -1, NULL, 0); 793 break; 794 } 795 } 796