1 /* $OpenBSD: client.c,v 1.131 2019/07/26 20:08:40 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/un.h> 22 #include <sys/wait.h> 23 24 #include <errno.h> 25 #include <event.h> 26 #include <fcntl.h> 27 #include <imsg.h> 28 #include <signal.h> 29 #include <stdlib.h> 30 #include <string.h> 31 #include <unistd.h> 32 33 #include "tmux.h" 34 35 static struct tmuxproc *client_proc; 36 static struct tmuxpeer *client_peer; 37 static int client_flags; 38 static struct event client_stdin; 39 static enum { 40 CLIENT_EXIT_NONE, 41 CLIENT_EXIT_DETACHED, 42 CLIENT_EXIT_DETACHED_HUP, 43 CLIENT_EXIT_LOST_TTY, 44 CLIENT_EXIT_TERMINATED, 45 CLIENT_EXIT_LOST_SERVER, 46 CLIENT_EXIT_EXITED, 47 CLIENT_EXIT_SERVER_EXITED, 48 } client_exitreason = CLIENT_EXIT_NONE; 49 static int client_exitval; 50 static enum msgtype client_exittype; 51 static const char *client_exitsession; 52 static const char *client_execshell; 53 static const char *client_execcmd; 54 static int client_attached; 55 56 static __dead void client_exec(const char *,const char *); 57 static int client_get_lock(char *); 58 static int client_connect(struct event_base *, const char *, int); 59 static void client_send_identify(const char *, const char *); 60 static void client_stdin_callback(int, short, void *); 61 static void client_write(int, const char *, size_t); 62 static void client_signal(int); 63 static void client_dispatch(struct imsg *, void *); 64 static void client_dispatch_attached(struct imsg *); 65 static void client_dispatch_wait(struct imsg *); 66 static const char *client_exit_message(void); 67 68 /* 69 * Get server create lock. If already held then server start is happening in 70 * another client, so block until the lock is released and return -2 to 71 * retry. Return -1 on failure to continue and start the server anyway. 72 */ 73 static int 74 client_get_lock(char *lockfile) 75 { 76 int lockfd; 77 78 log_debug("lock file is %s", lockfile); 79 80 if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1) { 81 log_debug("open failed: %s", strerror(errno)); 82 return (-1); 83 } 84 85 if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) { 86 log_debug("flock failed: %s", strerror(errno)); 87 if (errno != EAGAIN) 88 return (lockfd); 89 while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR) 90 /* nothing */; 91 close(lockfd); 92 return (-2); 93 } 94 log_debug("flock succeeded"); 95 96 return (lockfd); 97 } 98 99 /* Connect client to server. */ 100 static int 101 client_connect(struct event_base *base, const char *path, int start_server) 102 { 103 struct sockaddr_un sa; 104 size_t size; 105 int fd, lockfd = -1, locked = 0; 106 char *lockfile = NULL; 107 108 memset(&sa, 0, sizeof sa); 109 sa.sun_family = AF_UNIX; 110 size = strlcpy(sa.sun_path, path, sizeof sa.sun_path); 111 if (size >= sizeof sa.sun_path) { 112 errno = ENAMETOOLONG; 113 return (-1); 114 } 115 log_debug("socket is %s", path); 116 117 retry: 118 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 119 return (-1); 120 121 log_debug("trying connect"); 122 if (connect(fd, (struct sockaddr *)&sa, sizeof sa) == -1) { 123 log_debug("connect failed: %s", strerror(errno)); 124 if (errno != ECONNREFUSED && errno != ENOENT) 125 goto failed; 126 if (!start_server) 127 goto failed; 128 close(fd); 129 130 if (!locked) { 131 xasprintf(&lockfile, "%s.lock", path); 132 if ((lockfd = client_get_lock(lockfile)) < 0) { 133 log_debug("didn't get lock (%d)", lockfd); 134 135 free(lockfile); 136 lockfile = NULL; 137 138 if (lockfd == -2) 139 goto retry; 140 } 141 log_debug("got lock (%d)", lockfd); 142 143 /* 144 * Always retry at least once, even if we got the lock, 145 * because another client could have taken the lock, 146 * started the server and released the lock between our 147 * connect() and flock(). 148 */ 149 locked = 1; 150 goto retry; 151 } 152 153 if (lockfd >= 0 && unlink(path) != 0 && errno != ENOENT) { 154 free(lockfile); 155 close(lockfd); 156 return (-1); 157 } 158 fd = server_start(client_proc, base, lockfd, lockfile); 159 } 160 161 if (locked && lockfd >= 0) { 162 free(lockfile); 163 close(lockfd); 164 } 165 setblocking(fd, 0); 166 return (fd); 167 168 failed: 169 if (locked) { 170 free(lockfile); 171 close(lockfd); 172 } 173 close(fd); 174 return (-1); 175 } 176 177 /* Get exit string from reason number. */ 178 const char * 179 client_exit_message(void) 180 { 181 static char msg[256]; 182 183 switch (client_exitreason) { 184 case CLIENT_EXIT_NONE: 185 break; 186 case CLIENT_EXIT_DETACHED: 187 if (client_exitsession != NULL) { 188 xsnprintf(msg, sizeof msg, "detached " 189 "(from session %s)", client_exitsession); 190 return (msg); 191 } 192 return ("detached"); 193 case CLIENT_EXIT_DETACHED_HUP: 194 if (client_exitsession != NULL) { 195 xsnprintf(msg, sizeof msg, "detached and SIGHUP " 196 "(from session %s)", client_exitsession); 197 return (msg); 198 } 199 return ("detached and SIGHUP"); 200 case CLIENT_EXIT_LOST_TTY: 201 return ("lost tty"); 202 case CLIENT_EXIT_TERMINATED: 203 return ("terminated"); 204 case CLIENT_EXIT_LOST_SERVER: 205 return ("server exited unexpectedly"); 206 case CLIENT_EXIT_EXITED: 207 return ("exited"); 208 case CLIENT_EXIT_SERVER_EXITED: 209 return ("server exited"); 210 } 211 return ("unknown reason"); 212 } 213 214 /* Client main loop. */ 215 int 216 client_main(struct event_base *base, int argc, char **argv, int flags) 217 { 218 struct cmd_parse_result *pr; 219 struct cmd *cmd; 220 struct msg_command_data *data; 221 int cmdflags, fd, i; 222 const char *ttynam, *cwd; 223 pid_t ppid; 224 enum msgtype msg; 225 struct termios tio, saved_tio; 226 size_t size; 227 228 /* Ignore SIGCHLD now or daemon() in the server will leave a zombie. */ 229 signal(SIGCHLD, SIG_IGN); 230 231 /* Save the flags. */ 232 client_flags = flags; 233 234 /* Set up the initial command. */ 235 cmdflags = 0; 236 if (shell_command != NULL) { 237 msg = MSG_SHELL; 238 cmdflags = CMD_STARTSERVER; 239 } else if (argc == 0) { 240 msg = MSG_COMMAND; 241 cmdflags = CMD_STARTSERVER; 242 } else { 243 msg = MSG_COMMAND; 244 245 /* 246 * It sucks parsing the command string twice (in client and 247 * later in server) but it is necessary to get the start server 248 * flag. 249 */ 250 pr = cmd_parse_from_arguments(argc, argv, NULL); 251 if (pr->status == CMD_PARSE_SUCCESS) { 252 TAILQ_FOREACH(cmd, &pr->cmdlist->list, qentry) { 253 if (cmd->entry->flags & CMD_STARTSERVER) 254 cmdflags |= CMD_STARTSERVER; 255 } 256 cmd_list_free(pr->cmdlist); 257 } else 258 free(pr->error); 259 } 260 261 /* Create client process structure (starts logging). */ 262 client_proc = proc_start("client"); 263 proc_set_signals(client_proc, client_signal); 264 265 /* Initialize the client socket and start the server. */ 266 fd = client_connect(base, socket_path, cmdflags & CMD_STARTSERVER); 267 if (fd == -1) { 268 if (errno == ECONNREFUSED) { 269 fprintf(stderr, "no server running on %s\n", 270 socket_path); 271 } else { 272 fprintf(stderr, "error connecting to %s (%s)\n", 273 socket_path, strerror(errno)); 274 } 275 return (1); 276 } 277 client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL); 278 279 /* Save these before pledge(). */ 280 if ((cwd = find_cwd()) == NULL && (cwd = find_home()) == NULL) 281 cwd = "/"; 282 if ((ttynam = ttyname(STDIN_FILENO)) == NULL) 283 ttynam = ""; 284 285 /* 286 * Drop privileges for client. "proc exec" is needed for -c and for 287 * locking (which uses system(3)). 288 * 289 * "tty" is needed to restore termios(4) and also for some reason -CC 290 * does not work properly without it (input is not recognised). 291 * 292 * "sendfd" is dropped later in client_dispatch_wait(). 293 */ 294 if (pledge("stdio unix sendfd proc exec tty", NULL) != 0) 295 fatal("pledge failed"); 296 297 /* Free stuff that is not used in the client. */ 298 if (ptm_fd != -1) 299 close(ptm_fd); 300 options_free(global_options); 301 options_free(global_s_options); 302 options_free(global_w_options); 303 environ_free(global_environ); 304 305 /* Create stdin handler. */ 306 setblocking(STDIN_FILENO, 0); 307 event_set(&client_stdin, STDIN_FILENO, EV_READ|EV_PERSIST, 308 client_stdin_callback, NULL); 309 if (client_flags & CLIENT_CONTROLCONTROL) { 310 if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) { 311 fprintf(stderr, "tcgetattr failed: %s\n", 312 strerror(errno)); 313 return (1); 314 } 315 cfmakeraw(&tio); 316 tio.c_iflag = ICRNL|IXANY; 317 tio.c_oflag = OPOST|ONLCR; 318 tio.c_lflag = NOKERNINFO; 319 tio.c_cflag = CREAD|CS8|HUPCL; 320 tio.c_cc[VMIN] = 1; 321 tio.c_cc[VTIME] = 0; 322 cfsetispeed(&tio, cfgetispeed(&saved_tio)); 323 cfsetospeed(&tio, cfgetospeed(&saved_tio)); 324 tcsetattr(STDIN_FILENO, TCSANOW, &tio); 325 } 326 327 /* Send identify messages. */ 328 client_send_identify(ttynam, cwd); 329 330 /* Send first command. */ 331 if (msg == MSG_COMMAND) { 332 /* How big is the command? */ 333 size = 0; 334 for (i = 0; i < argc; i++) 335 size += strlen(argv[i]) + 1; 336 if (size > MAX_IMSGSIZE - (sizeof *data)) { 337 fprintf(stderr, "command too long\n"); 338 return (1); 339 } 340 data = xmalloc((sizeof *data) + size); 341 342 /* Prepare command for server. */ 343 data->argc = argc; 344 if (cmd_pack_argv(argc, argv, (char *)(data + 1), size) != 0) { 345 fprintf(stderr, "command too long\n"); 346 free(data); 347 return (1); 348 } 349 size += sizeof *data; 350 351 /* Send the command. */ 352 if (proc_send(client_peer, msg, -1, data, size) != 0) { 353 fprintf(stderr, "failed to send command\n"); 354 free(data); 355 return (1); 356 } 357 free(data); 358 } else if (msg == MSG_SHELL) 359 proc_send(client_peer, msg, -1, NULL, 0); 360 361 /* Start main loop. */ 362 proc_loop(client_proc, NULL); 363 364 /* Run command if user requested exec, instead of exiting. */ 365 if (client_exittype == MSG_EXEC) { 366 if (client_flags & CLIENT_CONTROLCONTROL) 367 tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio); 368 client_exec(client_execshell, client_execcmd); 369 } 370 371 /* Print the exit message, if any, and exit. */ 372 if (client_attached) { 373 if (client_exitreason != CLIENT_EXIT_NONE) 374 printf("[%s]\n", client_exit_message()); 375 376 ppid = getppid(); 377 if (client_exittype == MSG_DETACHKILL && ppid > 1) 378 kill(ppid, SIGHUP); 379 } else if (client_flags & CLIENT_CONTROLCONTROL) { 380 if (client_exitreason != CLIENT_EXIT_NONE) 381 printf("%%exit %s\n", client_exit_message()); 382 else 383 printf("%%exit\n"); 384 printf("\033\\"); 385 tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio); 386 } else if (client_exitreason != CLIENT_EXIT_NONE) 387 fprintf(stderr, "%s\n", client_exit_message()); 388 setblocking(STDIN_FILENO, 1); 389 return (client_exitval); 390 } 391 392 /* Send identify messages to server. */ 393 static void 394 client_send_identify(const char *ttynam, const char *cwd) 395 { 396 const char *s; 397 char **ss; 398 size_t sslen; 399 int fd, flags = client_flags; 400 pid_t pid; 401 402 proc_send(client_peer, MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags); 403 404 if ((s = getenv("TERM")) == NULL) 405 s = ""; 406 proc_send(client_peer, MSG_IDENTIFY_TERM, -1, s, strlen(s) + 1); 407 408 proc_send(client_peer, MSG_IDENTIFY_TTYNAME, -1, ttynam, 409 strlen(ttynam) + 1); 410 proc_send(client_peer, MSG_IDENTIFY_CWD, -1, cwd, strlen(cwd) + 1); 411 412 if ((fd = dup(STDIN_FILENO)) == -1) 413 fatal("dup failed"); 414 proc_send(client_peer, MSG_IDENTIFY_STDIN, fd, NULL, 0); 415 416 pid = getpid(); 417 proc_send(client_peer, MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid); 418 419 for (ss = environ; *ss != NULL; ss++) { 420 sslen = strlen(*ss) + 1; 421 if (sslen > MAX_IMSGSIZE - IMSG_HEADER_SIZE) 422 continue; 423 proc_send(client_peer, MSG_IDENTIFY_ENVIRON, -1, *ss, sslen); 424 } 425 426 proc_send(client_peer, MSG_IDENTIFY_DONE, -1, NULL, 0); 427 } 428 429 /* Callback for client stdin read events. */ 430 static void 431 client_stdin_callback(__unused int fd, __unused short events, 432 __unused void *arg) 433 { 434 struct msg_stdin_data data; 435 436 data.size = read(STDIN_FILENO, data.data, sizeof data.data); 437 if (data.size == -1 && (errno == EINTR || errno == EAGAIN)) 438 return; 439 440 proc_send(client_peer, MSG_STDIN, -1, &data, sizeof data); 441 if (data.size <= 0) 442 event_del(&client_stdin); 443 } 444 445 /* Force write to file descriptor. */ 446 static void 447 client_write(int fd, const char *data, size_t size) 448 { 449 ssize_t used; 450 451 log_debug("%s: %.*s", __func__, (int)size, data); 452 while (size != 0) { 453 used = write(fd, data, size); 454 if (used == -1) { 455 if (errno == EINTR || errno == EAGAIN) 456 continue; 457 break; 458 } 459 data += used; 460 size -= used; 461 } 462 } 463 464 /* Run command in shell; used for -c. */ 465 static __dead void 466 client_exec(const char *shell, const char *shellcmd) 467 { 468 const char *name, *ptr; 469 char *argv0; 470 471 log_debug("shell %s, command %s", shell, shellcmd); 472 473 ptr = strrchr(shell, '/'); 474 if (ptr != NULL && *(ptr + 1) != '\0') 475 name = ptr + 1; 476 else 477 name = shell; 478 if (client_flags & CLIENT_LOGIN) 479 xasprintf(&argv0, "-%s", name); 480 else 481 xasprintf(&argv0, "%s", name); 482 setenv("SHELL", shell, 1); 483 484 proc_clear_signals(client_proc, 1); 485 486 setblocking(STDIN_FILENO, 1); 487 setblocking(STDOUT_FILENO, 1); 488 setblocking(STDERR_FILENO, 1); 489 closefrom(STDERR_FILENO + 1); 490 491 execl(shell, argv0, "-c", shellcmd, (char *) NULL); 492 fatal("execl failed"); 493 } 494 495 /* Callback to handle signals in the client. */ 496 static void 497 client_signal(int sig) 498 { 499 struct sigaction sigact; 500 int status; 501 502 if (sig == SIGCHLD) 503 waitpid(WAIT_ANY, &status, WNOHANG); 504 else if (!client_attached) { 505 if (sig == SIGTERM) 506 proc_exit(client_proc); 507 } else { 508 switch (sig) { 509 case SIGHUP: 510 client_exitreason = CLIENT_EXIT_LOST_TTY; 511 client_exitval = 1; 512 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 513 break; 514 case SIGTERM: 515 client_exitreason = CLIENT_EXIT_TERMINATED; 516 client_exitval = 1; 517 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 518 break; 519 case SIGWINCH: 520 proc_send(client_peer, MSG_RESIZE, -1, NULL, 0); 521 break; 522 case SIGCONT: 523 memset(&sigact, 0, sizeof sigact); 524 sigemptyset(&sigact.sa_mask); 525 sigact.sa_flags = SA_RESTART; 526 sigact.sa_handler = SIG_IGN; 527 if (sigaction(SIGTSTP, &sigact, NULL) != 0) 528 fatal("sigaction failed"); 529 proc_send(client_peer, MSG_WAKEUP, -1, NULL, 0); 530 break; 531 } 532 } 533 } 534 535 /* Callback for client read events. */ 536 static void 537 client_dispatch(struct imsg *imsg, __unused void *arg) 538 { 539 if (imsg == NULL) { 540 client_exitreason = CLIENT_EXIT_LOST_SERVER; 541 client_exitval = 1; 542 proc_exit(client_proc); 543 return; 544 } 545 546 if (client_attached) 547 client_dispatch_attached(imsg); 548 else 549 client_dispatch_wait(imsg); 550 } 551 552 /* Dispatch imsgs when in wait state (before MSG_READY). */ 553 static void 554 client_dispatch_wait(struct imsg *imsg) 555 { 556 char *data; 557 ssize_t datalen; 558 struct msg_stdout_data stdoutdata; 559 struct msg_stderr_data stderrdata; 560 int retval; 561 static int pledge_applied; 562 563 /* 564 * "sendfd" is no longer required once all of the identify messages 565 * have been sent. We know the server won't send us anything until that 566 * point (because we don't ask it to), so we can drop "sendfd" once we 567 * get the first message from the server. 568 */ 569 if (!pledge_applied) { 570 if (pledge("stdio unix proc exec tty", NULL) != 0) 571 fatal("pledge failed"); 572 pledge_applied = 1; 573 }; 574 575 data = imsg->data; 576 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 577 578 switch (imsg->hdr.type) { 579 case MSG_EXIT: 580 case MSG_SHUTDOWN: 581 if (datalen != sizeof retval && datalen != 0) 582 fatalx("bad MSG_EXIT size"); 583 if (datalen == sizeof retval) { 584 memcpy(&retval, data, sizeof retval); 585 client_exitval = retval; 586 } 587 proc_exit(client_proc); 588 break; 589 case MSG_READY: 590 if (datalen != 0) 591 fatalx("bad MSG_READY size"); 592 593 event_del(&client_stdin); 594 client_attached = 1; 595 proc_send(client_peer, MSG_RESIZE, -1, NULL, 0); 596 break; 597 case MSG_STDIN: 598 if (datalen != 0) 599 fatalx("bad MSG_STDIN size"); 600 601 event_add(&client_stdin, NULL); 602 break; 603 case MSG_STDOUT: 604 if (datalen != sizeof stdoutdata) 605 fatalx("bad MSG_STDOUT size"); 606 memcpy(&stdoutdata, data, sizeof stdoutdata); 607 608 client_write(STDOUT_FILENO, stdoutdata.data, 609 stdoutdata.size); 610 break; 611 case MSG_STDERR: 612 if (datalen != sizeof stderrdata) 613 fatalx("bad MSG_STDERR size"); 614 memcpy(&stderrdata, data, sizeof stderrdata); 615 616 client_write(STDERR_FILENO, stderrdata.data, 617 stderrdata.size); 618 break; 619 case MSG_VERSION: 620 if (datalen != 0) 621 fatalx("bad MSG_VERSION size"); 622 623 fprintf(stderr, "protocol version mismatch " 624 "(client %d, server %u)\n", PROTOCOL_VERSION, 625 imsg->hdr.peerid & 0xff); 626 client_exitval = 1; 627 proc_exit(client_proc); 628 break; 629 case MSG_SHELL: 630 if (datalen == 0 || data[datalen - 1] != '\0') 631 fatalx("bad MSG_SHELL string"); 632 633 client_exec(data, shell_command); 634 /* NOTREACHED */ 635 case MSG_DETACH: 636 case MSG_DETACHKILL: 637 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 638 break; 639 case MSG_EXITED: 640 proc_exit(client_proc); 641 break; 642 } 643 } 644 645 /* Dispatch imsgs in attached state (after MSG_READY). */ 646 static void 647 client_dispatch_attached(struct imsg *imsg) 648 { 649 struct sigaction sigact; 650 char *data; 651 ssize_t datalen; 652 653 data = imsg->data; 654 datalen = imsg->hdr.len - IMSG_HEADER_SIZE; 655 656 switch (imsg->hdr.type) { 657 case MSG_DETACH: 658 case MSG_DETACHKILL: 659 if (datalen == 0 || data[datalen - 1] != '\0') 660 fatalx("bad MSG_DETACH string"); 661 662 client_exitsession = xstrdup(data); 663 client_exittype = imsg->hdr.type; 664 if (imsg->hdr.type == MSG_DETACHKILL) 665 client_exitreason = CLIENT_EXIT_DETACHED_HUP; 666 else 667 client_exitreason = CLIENT_EXIT_DETACHED; 668 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 669 break; 670 case MSG_EXEC: 671 if (datalen == 0 || data[datalen - 1] != '\0' || 672 strlen(data) + 1 == (size_t)datalen) 673 fatalx("bad MSG_EXEC string"); 674 client_execcmd = xstrdup(data); 675 client_execshell = xstrdup(data + strlen(data) + 1); 676 677 client_exittype = imsg->hdr.type; 678 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 679 break; 680 case MSG_EXIT: 681 if (datalen != 0 && datalen != sizeof (int)) 682 fatalx("bad MSG_EXIT size"); 683 684 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 685 client_exitreason = CLIENT_EXIT_EXITED; 686 break; 687 case MSG_EXITED: 688 if (datalen != 0) 689 fatalx("bad MSG_EXITED size"); 690 691 proc_exit(client_proc); 692 break; 693 case MSG_SHUTDOWN: 694 if (datalen != 0) 695 fatalx("bad MSG_SHUTDOWN size"); 696 697 proc_send(client_peer, MSG_EXITING, -1, NULL, 0); 698 client_exitreason = CLIENT_EXIT_SERVER_EXITED; 699 client_exitval = 1; 700 break; 701 case MSG_SUSPEND: 702 if (datalen != 0) 703 fatalx("bad MSG_SUSPEND size"); 704 705 memset(&sigact, 0, sizeof sigact); 706 sigemptyset(&sigact.sa_mask); 707 sigact.sa_flags = SA_RESTART; 708 sigact.sa_handler = SIG_DFL; 709 if (sigaction(SIGTSTP, &sigact, NULL) != 0) 710 fatal("sigaction failed"); 711 kill(getpid(), SIGTSTP); 712 break; 713 case MSG_LOCK: 714 if (datalen == 0 || data[datalen - 1] != '\0') 715 fatalx("bad MSG_LOCK string"); 716 717 system(data); 718 proc_send(client_peer, MSG_UNLOCK, -1, NULL, 0); 719 break; 720 } 721 } 722