1 /* $OpenBSD: server.c,v 1.59 2009/10/13 06:14:08 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> 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/ioctl.h> 21 #include <sys/socket.h> 22 #include <sys/stat.h> 23 #include <sys/un.h> 24 #include <sys/wait.h> 25 26 #include <errno.h> 27 #include <fcntl.h> 28 #include <paths.h> 29 #include <signal.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <syslog.h> 34 #include <termios.h> 35 #include <time.h> 36 #include <unistd.h> 37 38 #include "tmux.h" 39 40 /* 41 * Main server functions. 42 */ 43 44 /* Client list. */ 45 struct clients clients; 46 struct clients dead_clients; 47 48 /* Mapping of a pollfd to an fd independent of its position in the array. */ 49 struct poll_item { 50 struct pollfd pfd; 51 52 RB_ENTRY(poll_item) entry; 53 }; 54 RB_HEAD(poll_items, poll_item) poll_items; 55 56 int server_poll_cmp(struct poll_item *, struct poll_item *); 57 struct pollfd *server_poll_lookup(int); 58 void server_poll_add(int, int); 59 struct pollfd *server_poll_flatten(int *); 60 void server_poll_parse(struct pollfd *); 61 void server_poll_reset(void); 62 RB_PROTOTYPE(poll_items, poll_item, entry, server_poll_cmp); 63 RB_GENERATE(poll_items, poll_item, entry, server_poll_cmp); 64 65 void server_create_client(int); 66 int server_create_socket(void); 67 int server_main(int); 68 void server_shutdown(void); 69 int server_should_shutdown(void); 70 void server_child_signal(void); 71 void server_fill_windows(void); 72 void server_handle_windows(void); 73 void server_fill_clients(void); 74 void server_handle_clients(void); 75 void server_fill_jobs(void); 76 void server_handle_jobs(void); 77 void server_accept_client(int); 78 void server_handle_client(struct client *); 79 void server_handle_window(struct window *, struct window_pane *); 80 int server_check_window_bell(struct session *, struct window *); 81 int server_check_window_activity(struct session *, 82 struct window *); 83 int server_check_window_content(struct session *, struct window *, 84 struct window_pane *); 85 void server_clean_dead(void); 86 void server_lost_client(struct client *); 87 void server_check_window(struct window *); 88 void server_check_redraw(struct client *); 89 void server_set_title(struct client *); 90 void server_check_timers(struct client *); 91 void server_check_jobs(void); 92 void server_lock_server(void); 93 void server_lock_sessions(void); 94 void server_check_clients(void); 95 void server_second_timers(void); 96 int server_update_socket(void); 97 98 int 99 server_poll_cmp(struct poll_item *pitem1, struct poll_item *pitem2) 100 { 101 return (pitem1->pfd.fd - pitem2->pfd.fd); 102 } 103 104 struct pollfd * 105 server_poll_lookup(int fd) 106 { 107 struct poll_item pitem; 108 109 pitem.pfd.fd = fd; 110 return (&RB_FIND(poll_items, &poll_items, &pitem)->pfd); 111 } 112 113 void 114 server_poll_add(int fd, int events) 115 { 116 struct poll_item *pitem; 117 118 pitem = xmalloc(sizeof *pitem); 119 pitem->pfd.fd = fd; 120 pitem->pfd.events = events; 121 RB_INSERT(poll_items, &poll_items, pitem); 122 } 123 124 struct pollfd * 125 server_poll_flatten(int *nfds) 126 { 127 struct poll_item *pitem; 128 struct pollfd *pfds; 129 130 pfds = NULL; 131 *nfds = 0; 132 RB_FOREACH(pitem, poll_items, &poll_items) { 133 pfds = xrealloc(pfds, (*nfds) + 1, sizeof *pfds); 134 pfds[*nfds].fd = pitem->pfd.fd; 135 pfds[*nfds].events = pitem->pfd.events; 136 (*nfds)++; 137 } 138 return (pfds); 139 } 140 141 void 142 server_poll_parse(struct pollfd *pfds) 143 { 144 struct poll_item *pitem; 145 int nfds; 146 147 nfds = 0; 148 RB_FOREACH(pitem, poll_items, &poll_items) { 149 pitem->pfd.revents = pfds[nfds].revents; 150 nfds++; 151 } 152 xfree(pfds); 153 } 154 155 void 156 server_poll_reset(void) 157 { 158 struct poll_item *pitem; 159 160 while (!RB_EMPTY(&poll_items)) { 161 pitem = RB_ROOT(&poll_items); 162 RB_REMOVE(poll_items, &poll_items, pitem); 163 xfree(pitem); 164 } 165 } 166 167 /* Create a new client. */ 168 void 169 server_create_client(int fd) 170 { 171 struct client *c; 172 int mode; 173 u_int i; 174 175 if ((mode = fcntl(fd, F_GETFL)) == -1) 176 fatal("fcntl failed"); 177 if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1) 178 fatal("fcntl failed"); 179 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) 180 fatal("fcntl failed"); 181 182 c = xcalloc(1, sizeof *c); 183 c->references = 0; 184 imsg_init(&c->ibuf, fd); 185 186 if (gettimeofday(&c->tv, NULL) != 0) 187 fatal("gettimeofday failed"); 188 189 ARRAY_INIT(&c->prompt_hdata); 190 191 c->tty.fd = -1; 192 c->title = NULL; 193 194 c->session = NULL; 195 c->tty.sx = 80; 196 c->tty.sy = 24; 197 198 screen_init(&c->status, c->tty.sx, 1, 0); 199 job_tree_init(&c->status_jobs); 200 201 c->message_string = NULL; 202 203 c->prompt_string = NULL; 204 c->prompt_buffer = NULL; 205 c->prompt_index = 0; 206 207 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 208 if (ARRAY_ITEM(&clients, i) == NULL) { 209 ARRAY_SET(&clients, i, c); 210 return; 211 } 212 } 213 ARRAY_ADD(&clients, c); 214 log_debug("new client %d", fd); 215 } 216 217 /* Fork new server. */ 218 int 219 server_start(char *path) 220 { 221 struct client *c; 222 int pair[2], srv_fd; 223 char *cause; 224 char rpathbuf[MAXPATHLEN]; 225 226 /* The first client is special and gets a socketpair; create it. */ 227 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0) 228 fatal("socketpair failed"); 229 230 switch (fork()) { 231 case -1: 232 fatal("fork failed"); 233 case 0: 234 break; 235 default: 236 close(pair[1]); 237 return (pair[0]); 238 } 239 close(pair[0]); 240 241 /* 242 * Must daemonise before loading configuration as the PID changes so 243 * $TMUX would be wrong for sessions created in the config file. 244 */ 245 if (daemon(1, 0) != 0) 246 fatal("daemon failed"); 247 248 logfile("server"); 249 log_debug("server started, pid %ld", (long) getpid()); 250 251 ARRAY_INIT(&windows); 252 ARRAY_INIT(&clients); 253 ARRAY_INIT(&dead_clients); 254 ARRAY_INIT(&sessions); 255 ARRAY_INIT(&dead_sessions); 256 TAILQ_INIT(&session_groups); 257 mode_key_init_trees(); 258 key_bindings_init(); 259 utf8_build(); 260 261 start_time = time(NULL); 262 socket_path = path; 263 264 if (realpath(socket_path, rpathbuf) == NULL) 265 strlcpy(rpathbuf, socket_path, sizeof rpathbuf); 266 log_debug("socket path %s", socket_path); 267 setproctitle("server (%s)", rpathbuf); 268 269 srv_fd = server_create_socket(); 270 server_create_client(pair[1]); 271 272 if (access(SYSTEM_CFG, R_OK) != 0) { 273 if (errno != ENOENT) { 274 xasprintf( 275 &cause, "%s: %s", strerror(errno), SYSTEM_CFG); 276 goto error; 277 } 278 } else if (load_cfg(SYSTEM_CFG, NULL, &cause) != 0) 279 goto error; 280 if (cfg_file != NULL && load_cfg(cfg_file, NULL, &cause) != 0) 281 goto error; 282 283 exit(server_main(srv_fd)); 284 285 error: 286 /* Write the error and shutdown the server. */ 287 c = ARRAY_FIRST(&clients); 288 289 server_write_error(c, cause); 290 xfree(cause); 291 292 sigterm = 1; 293 server_shutdown(); 294 295 exit(server_main(srv_fd)); 296 } 297 298 /* Create server socket. */ 299 int 300 server_create_socket(void) 301 { 302 struct sockaddr_un sa; 303 size_t size; 304 mode_t mask; 305 int fd, mode; 306 307 memset(&sa, 0, sizeof sa); 308 sa.sun_family = AF_UNIX; 309 size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path); 310 if (size >= sizeof sa.sun_path) { 311 errno = ENAMETOOLONG; 312 fatal("socket failed"); 313 } 314 unlink(sa.sun_path); 315 316 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 317 fatal("socket failed"); 318 319 mask = umask(S_IXUSR|S_IRWXG|S_IRWXO); 320 if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) 321 fatal("bind failed"); 322 umask(mask); 323 324 if (listen(fd, 16) == -1) 325 fatal("listen failed"); 326 327 if ((mode = fcntl(fd, F_GETFL)) == -1) 328 fatal("fcntl failed"); 329 if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1) 330 fatal("fcntl failed"); 331 if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) 332 fatal("fcntl failed"); 333 334 return (fd); 335 } 336 337 /* Main server loop. */ 338 int 339 server_main(int srv_fd) 340 { 341 struct pollfd *pfds, *pfd; 342 int nfds, xtimeout; 343 u_int i; 344 time_t now, last; 345 346 siginit(); 347 log_debug("server socket is %d", srv_fd); 348 349 last = time(NULL); 350 351 pfds = NULL; 352 for (;;) { 353 /* If sigterm, kill all windows and clients. */ 354 if (sigterm) 355 server_shutdown(); 356 357 /* Stop if no sessions or clients left. */ 358 if (server_should_shutdown()) 359 break; 360 361 /* Handle child exit. */ 362 if (sigchld) { 363 server_child_signal(); 364 sigchld = 0; 365 } 366 367 /* Recreate socket on SIGUSR1. */ 368 if (sigusr1) { 369 close(srv_fd); 370 srv_fd = server_create_socket(); 371 sigusr1 = 0; 372 } 373 374 /* Collect any jobs that have died and process clients. */ 375 server_check_jobs(); 376 server_check_clients(); 377 378 /* Initialise pollfd array and add server socket. */ 379 server_poll_reset(); 380 server_poll_add(srv_fd, POLLIN); 381 382 /* Fill window and client sockets. */ 383 server_fill_jobs(); 384 server_fill_windows(); 385 server_fill_clients(); 386 387 /* Update socket permissions. */ 388 xtimeout = INFTIM; 389 if (server_update_socket() != 0) 390 xtimeout = POLL_TIMEOUT; 391 392 /* Do the poll. */ 393 pfds = server_poll_flatten(&nfds); 394 if (poll(pfds, nfds, xtimeout) == -1) { 395 if (errno == EAGAIN || errno == EINTR) 396 continue; 397 fatal("poll failed"); 398 } 399 server_poll_parse(pfds); 400 401 /* Handle server socket. */ 402 pfd = server_poll_lookup(srv_fd); 403 if (pfd->revents & (POLLERR|POLLNVAL|POLLHUP)) 404 fatalx("lost server socket"); 405 if (pfd->revents & POLLIN) { 406 server_accept_client(srv_fd); 407 continue; 408 } 409 410 /* Call second-based timers. */ 411 now = time(NULL); 412 if (now != last) { 413 last = now; 414 server_second_timers(); 415 } 416 417 /* Set window names. */ 418 set_window_names(); 419 420 /* Handle window and client sockets. */ 421 server_handle_jobs(); 422 server_handle_windows(); 423 server_handle_clients(); 424 425 /* Collect any unset key bindings. */ 426 key_bindings_clean(); 427 428 /* Collect dead clients and sessions. */ 429 server_clean_dead(); 430 } 431 server_poll_reset(); 432 433 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { 434 if (ARRAY_ITEM(&sessions, i) != NULL) 435 session_destroy(ARRAY_ITEM(&sessions, i)); 436 } 437 ARRAY_FREE(&sessions); 438 439 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 440 if (ARRAY_ITEM(&clients, i) != NULL) 441 server_lost_client(ARRAY_ITEM(&clients, i)); 442 } 443 ARRAY_FREE(&clients); 444 445 mode_key_free_trees(); 446 key_bindings_free(); 447 448 close(srv_fd); 449 450 unlink(socket_path); 451 xfree(socket_path); 452 453 options_free(&global_s_options); 454 options_free(&global_w_options); 455 456 return (0); 457 } 458 459 /* Kill all clients. */ 460 void 461 server_shutdown(void) 462 { 463 struct session *s; 464 struct client *c; 465 u_int i, j; 466 467 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 468 c = ARRAY_ITEM(&clients, i); 469 if (c != NULL) { 470 if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED)) 471 server_lost_client(c); 472 else 473 server_write_client(c, MSG_SHUTDOWN, NULL, 0); 474 } 475 } 476 477 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { 478 s = ARRAY_ITEM(&sessions, i); 479 for (j = 0; j < ARRAY_LENGTH(&clients); j++) { 480 c = ARRAY_ITEM(&clients, j); 481 if (c != NULL && c->session == s) { 482 s = NULL; 483 break; 484 } 485 } 486 if (s != NULL) 487 session_destroy(s); 488 } 489 } 490 491 /* Check if the server should be shutting down (no more clients or windows). */ 492 int 493 server_should_shutdown(void) 494 { 495 u_int i; 496 497 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { 498 if (ARRAY_ITEM(&sessions, i) != NULL) 499 return (0); 500 } 501 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 502 if (ARRAY_ITEM(&clients, i) != NULL) 503 return (0); 504 } 505 return (1); 506 } 507 508 /* Handle SIGCHLD. */ 509 void 510 server_child_signal(void) 511 { 512 struct window *w; 513 struct window_pane *wp; 514 struct job *job; 515 int status; 516 pid_t pid; 517 u_int i; 518 519 for (;;) { 520 switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) { 521 case -1: 522 if (errno == ECHILD) 523 return; 524 fatal("waitpid failed"); 525 case 0: 526 return; 527 } 528 if (!WIFSTOPPED(status)) { 529 SLIST_FOREACH(job, &all_jobs, lentry) { 530 if (pid == job->pid) { 531 job->pid = -1; 532 job->status = status; 533 } 534 } 535 continue; 536 } 537 if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU) 538 continue; 539 540 for (i = 0; i < ARRAY_LENGTH(&windows); i++) { 541 w = ARRAY_ITEM(&windows, i); 542 if (w == NULL) 543 continue; 544 TAILQ_FOREACH(wp, &w->panes, entry) { 545 if (wp->pid == pid) { 546 if (killpg(pid, SIGCONT) != 0) 547 kill(pid, SIGCONT); 548 } 549 } 550 } 551 } 552 } 553 554 /* Fill window pollfds. */ 555 void 556 server_fill_windows(void) 557 { 558 struct window *w; 559 struct window_pane *wp; 560 u_int i; 561 int events; 562 563 for (i = 0; i < ARRAY_LENGTH(&windows); i++) { 564 w = ARRAY_ITEM(&windows, i); 565 if (w == NULL) 566 continue; 567 568 TAILQ_FOREACH(wp, &w->panes, entry) { 569 if (wp->fd == -1) 570 continue; 571 events = POLLIN; 572 if (BUFFER_USED(wp->out) > 0) 573 events |= POLLOUT; 574 server_poll_add(wp->fd, events); 575 576 if (wp->pipe_fd == -1) 577 continue; 578 events = 0; 579 if (BUFFER_USED(wp->pipe_buf) > 0) 580 events |= POLLOUT; 581 server_poll_add(wp->pipe_fd, events); 582 } 583 } 584 } 585 586 /* Handle window pollfds. */ 587 void 588 server_handle_windows(void) 589 { 590 struct window *w; 591 struct window_pane *wp; 592 struct pollfd *pfd; 593 u_int i; 594 595 for (i = 0; i < ARRAY_LENGTH(&windows); i++) { 596 w = ARRAY_ITEM(&windows, i); 597 if (w == NULL) 598 continue; 599 600 TAILQ_FOREACH(wp, &w->panes, entry) { 601 if (wp->fd == -1) 602 continue; 603 if ((pfd = server_poll_lookup(wp->fd)) == NULL) 604 continue; 605 if (buffer_poll(pfd, wp->in, wp->out) != 0) { 606 close(wp->fd); 607 wp->fd = -1; 608 } else 609 server_handle_window(w, wp); 610 611 if (wp->pipe_fd == -1) 612 continue; 613 if ((pfd = server_poll_lookup(wp->pipe_fd)) == NULL) 614 continue; 615 if (buffer_poll(pfd, NULL, wp->pipe_buf) != 0) { 616 buffer_destroy(wp->pipe_buf); 617 close(wp->pipe_fd); 618 wp->pipe_fd = -1; 619 } 620 } 621 622 server_check_window(w); 623 } 624 } 625 626 /* Check clients for redraw and timers. */ 627 void 628 server_check_clients(void) 629 { 630 struct client *c; 631 struct window *w; 632 struct window_pane *wp; 633 u_int i; 634 635 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 636 c = ARRAY_ITEM(&clients, i); 637 if (c == NULL || c->session == NULL) 638 continue; 639 640 server_check_timers(c); 641 server_check_redraw(c); 642 } 643 644 /* 645 * Clear any window redraw flags (will have been redrawn as part of 646 * client). 647 */ 648 for (i = 0; i < ARRAY_LENGTH(&windows); i++) { 649 w = ARRAY_ITEM(&windows, i); 650 if (w == NULL) 651 continue; 652 653 w->flags &= ~WINDOW_REDRAW; 654 TAILQ_FOREACH(wp, &w->panes, entry) 655 wp->flags &= ~PANE_REDRAW; 656 } 657 } 658 659 /* Check for general redraw on client. */ 660 void 661 server_check_redraw(struct client *c) 662 { 663 struct session *s = c->session; 664 struct window_pane *wp; 665 int flags, redraw; 666 667 flags = c->tty.flags & TTY_FREEZE; 668 c->tty.flags &= ~TTY_FREEZE; 669 670 if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) { 671 if (options_get_number(&s->options, "set-titles")) 672 server_set_title(c); 673 674 if (c->message_string != NULL) 675 redraw = status_message_redraw(c); 676 else if (c->prompt_string != NULL) 677 redraw = status_prompt_redraw(c); 678 else 679 redraw = status_redraw(c); 680 if (!redraw) 681 c->flags &= ~CLIENT_STATUS; 682 } 683 684 if (c->flags & CLIENT_REDRAW) { 685 screen_redraw_screen(c, 0); 686 c->flags &= ~CLIENT_STATUS; 687 } else { 688 TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) { 689 if (wp->flags & PANE_REDRAW) 690 screen_redraw_pane(c, wp); 691 } 692 } 693 694 if (c->flags & CLIENT_STATUS) 695 screen_redraw_screen(c, 1); 696 697 c->tty.flags |= flags; 698 699 c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS); 700 } 701 702 /* Set client title. */ 703 void 704 server_set_title(struct client *c) 705 { 706 struct session *s = c->session; 707 const char *template; 708 char *title; 709 710 template = options_get_string(&s->options, "set-titles-string"); 711 712 title = status_replace(c, template, time(NULL)); 713 if (c->title == NULL || strcmp(title, c->title) != 0) { 714 if (c->title != NULL) 715 xfree(c->title); 716 c->title = xstrdup(title); 717 tty_set_title(&c->tty, c->title); 718 } 719 xfree(title); 720 } 721 722 /* Check for timers on client. */ 723 void 724 server_check_timers(struct client *c) 725 { 726 struct session *s = c->session; 727 struct job *job; 728 struct timeval tv; 729 u_int interval; 730 731 if (gettimeofday(&tv, NULL) != 0) 732 fatal("gettimeofday failed"); 733 734 if (c->flags & CLIENT_IDENTIFY && timercmp(&tv, &c->identify_timer, >)) 735 server_clear_identify(c); 736 737 if (c->message_string != NULL && timercmp(&tv, &c->message_timer, >)) 738 status_message_clear(c); 739 740 if (c->message_string != NULL || c->prompt_string != NULL) { 741 /* 742 * Don't need timed redraw for messages/prompts so bail now. 743 * The status timer isn't reset when they are redrawn anyway. 744 */ 745 return; 746 } 747 if (!options_get_number(&s->options, "status")) 748 return; 749 750 /* Check timer; resolution is only a second so don't be too clever. */ 751 interval = options_get_number(&s->options, "status-interval"); 752 if (interval == 0) 753 return; 754 if (tv.tv_sec < c->status_timer.tv_sec || 755 ((u_int) tv.tv_sec) - c->status_timer.tv_sec >= interval) { 756 /* Run the jobs for this client and schedule for redraw. */ 757 RB_FOREACH(job, jobs, &c->status_jobs) 758 job_run(job); 759 c->flags |= CLIENT_STATUS; 760 } 761 } 762 763 /* Fill client pollfds. */ 764 void 765 server_fill_clients(void) 766 { 767 struct client *c; 768 u_int i; 769 int events; 770 771 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 772 c = ARRAY_ITEM(&clients, i); 773 774 if (c != NULL) { 775 events = 0; 776 if (!(c->flags & CLIENT_BAD)) 777 events |= POLLIN; 778 if (c->ibuf.w.queued > 0) 779 events |= POLLOUT; 780 server_poll_add(c->ibuf.fd, events); 781 } 782 783 if (c != NULL && !(c->flags & CLIENT_SUSPENDED) && 784 c->tty.fd != -1 && c->session != NULL) { 785 events = POLLIN; 786 if (BUFFER_USED(c->tty.out) > 0) 787 events |= POLLOUT; 788 server_poll_add(c->tty.fd, events); 789 } 790 } 791 } 792 793 /* Fill in job fds. */ 794 void 795 server_fill_jobs(void) 796 { 797 struct job *job; 798 799 SLIST_FOREACH(job, &all_jobs, lentry) { 800 if (job->fd == -1) 801 continue; 802 server_poll_add(job->fd, POLLIN); 803 } 804 } 805 806 /* Handle job fds. */ 807 void 808 server_handle_jobs(void) 809 { 810 struct job *job; 811 struct pollfd *pfd; 812 813 SLIST_FOREACH(job, &all_jobs, lentry) { 814 if (job->fd == -1) 815 continue; 816 if ((pfd = server_poll_lookup(job->fd)) == NULL) 817 continue; 818 if (buffer_poll(pfd, job->out, NULL) != 0) { 819 close(job->fd); 820 job->fd = -1; 821 } 822 } 823 } 824 825 /* Handle job fds. */ 826 void 827 server_check_jobs(void) 828 { 829 struct job *job; 830 831 restart: 832 SLIST_FOREACH(job, &all_jobs, lentry) { 833 if (job->flags & JOB_DONE || job->fd != -1 || job->pid != -1) 834 continue; 835 job->flags |= JOB_DONE; 836 837 if (job->callbackfn != NULL) { 838 job->callbackfn(job); 839 goto restart; /* could be freed by callback */ 840 } 841 } 842 } 843 844 /* Handle client pollfds. */ 845 void 846 server_handle_clients(void) 847 { 848 struct client *c; 849 struct pollfd *pfd; 850 u_int i; 851 852 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 853 c = ARRAY_ITEM(&clients, i); 854 855 if (c != NULL) { 856 if ((pfd = server_poll_lookup(c->ibuf.fd)) == NULL) 857 continue; 858 if (pfd->revents & (POLLERR|POLLNVAL|POLLHUP)) { 859 server_lost_client(c); 860 continue; 861 } 862 863 if (pfd->revents & POLLOUT) { 864 if (msgbuf_write(&c->ibuf.w) < 0) { 865 server_lost_client(c); 866 continue; 867 } 868 } 869 870 if (c->flags & CLIENT_BAD) { 871 if (c->ibuf.w.queued == 0) 872 server_lost_client(c); 873 continue; 874 } else if (pfd->revents & POLLIN) { 875 if (server_msg_dispatch(c) != 0) { 876 server_lost_client(c); 877 continue; 878 } 879 } 880 } 881 882 if (c != NULL && !(c->flags & CLIENT_SUSPENDED) && 883 c->tty.fd != -1 && c->session != NULL) { 884 if ((pfd = server_poll_lookup(c->tty.fd)) == NULL) 885 continue; 886 if (buffer_poll(pfd, c->tty.in, c->tty.out) != 0) 887 server_lost_client(c); 888 else 889 server_handle_client(c); 890 } 891 } 892 } 893 894 /* accept(2) and create new client. */ 895 void 896 server_accept_client(int srv_fd) 897 { 898 struct sockaddr_storage sa; 899 socklen_t slen = sizeof sa; 900 int fd; 901 902 fd = accept(srv_fd, (struct sockaddr *) &sa, &slen); 903 if (fd == -1) { 904 if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED) 905 return; 906 fatal("accept failed"); 907 } 908 if (sigterm) { 909 close(fd); 910 return; 911 } 912 server_create_client(fd); 913 } 914 915 /* Input data from client. */ 916 void 917 server_handle_client(struct client *c) 918 { 919 struct window *w; 920 struct window_pane *wp; 921 struct screen *s; 922 struct options *oo; 923 struct timeval tv; 924 struct key_binding *bd; 925 struct keylist *keylist; 926 struct mouse_event mouse; 927 int key, status, xtimeout, mode, isprefix; 928 u_int i; 929 930 xtimeout = options_get_number(&c->session->options, "repeat-time"); 931 if (xtimeout != 0 && c->flags & CLIENT_REPEAT) { 932 if (gettimeofday(&tv, NULL) != 0) 933 fatal("gettimeofday failed"); 934 if (timercmp(&tv, &c->repeat_timer, >)) 935 c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT); 936 } 937 938 /* Process keys. */ 939 keylist = options_get_data(&c->session->options, "prefix"); 940 while (tty_keys_next(&c->tty, &key, &mouse) == 0) { 941 if (c->session == NULL) 942 return; 943 944 c->session->activity = time(NULL); 945 w = c->session->curw->window; 946 wp = w->active; /* could die */ 947 oo = &c->session->options; 948 949 /* Special case: number keys jump to pane in identify mode. */ 950 if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') { 951 wp = window_pane_at_index(w, key - '0'); 952 if (wp != NULL && window_pane_visible(wp)) 953 window_set_active_pane(w, wp); 954 server_clear_identify(c); 955 continue; 956 } 957 958 status_message_clear(c); 959 server_clear_identify(c); 960 if (c->prompt_string != NULL) { 961 status_prompt_key(c, key); 962 continue; 963 } 964 965 /* Check for mouse keys. */ 966 if (key == KEYC_MOUSE) { 967 if (options_get_number(oo, "mouse-select-pane")) { 968 window_set_active_at(w, mouse.x, mouse.y); 969 wp = w->active; 970 } 971 window_pane_mouse(wp, c, &mouse); 972 continue; 973 } 974 975 /* Is this a prefix key? */ 976 isprefix = 0; 977 for (i = 0; i < ARRAY_LENGTH(keylist); i++) { 978 if (key == ARRAY_ITEM(keylist, i)) { 979 isprefix = 1; 980 break; 981 } 982 } 983 984 /* No previous prefix key. */ 985 if (!(c->flags & CLIENT_PREFIX)) { 986 if (isprefix) 987 c->flags |= CLIENT_PREFIX; 988 else { 989 /* Try as a non-prefix key binding. */ 990 if ((bd = key_bindings_lookup(key)) == NULL) 991 window_pane_key(wp, c, key); 992 else 993 key_bindings_dispatch(bd, c); 994 } 995 continue; 996 } 997 998 /* Prefix key already pressed. Reset prefix and lookup key. */ 999 c->flags &= ~CLIENT_PREFIX; 1000 if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) { 1001 /* If repeating, treat this as a key, else ignore. */ 1002 if (c->flags & CLIENT_REPEAT) { 1003 c->flags &= ~CLIENT_REPEAT; 1004 if (isprefix) 1005 c->flags |= CLIENT_PREFIX; 1006 else 1007 window_pane_key(wp, c, key); 1008 } 1009 continue; 1010 } 1011 1012 /* If already repeating, but this key can't repeat, skip it. */ 1013 if (c->flags & CLIENT_REPEAT && !bd->can_repeat) { 1014 c->flags &= ~CLIENT_REPEAT; 1015 if (isprefix) 1016 c->flags |= CLIENT_PREFIX; 1017 else 1018 window_pane_key(wp, c, key); 1019 continue; 1020 } 1021 1022 /* If this key can repeat, reset the repeat flags and timer. */ 1023 if (xtimeout != 0 && bd->can_repeat) { 1024 c->flags |= CLIENT_PREFIX|CLIENT_REPEAT; 1025 1026 tv.tv_sec = xtimeout / 1000; 1027 tv.tv_usec = (xtimeout % 1000) * 1000L; 1028 if (gettimeofday(&c->repeat_timer, NULL) != 0) 1029 fatal("gettimeofday failed"); 1030 timeradd(&c->repeat_timer, &tv, &c->repeat_timer); 1031 } 1032 1033 /* Dispatch the command. */ 1034 key_bindings_dispatch(bd, c); 1035 } 1036 if (c->session == NULL) 1037 return; 1038 w = c->session->curw->window; 1039 wp = w->active; 1040 oo = &c->session->options; 1041 s = wp->screen; 1042 1043 /* 1044 * Update cursor position and mode settings. The scroll region and 1045 * attributes are cleared across poll(2) as this is the most likely 1046 * time a user may interrupt tmux, for example with ~^Z in ssh(1). This 1047 * is a compromise between excessive resets and likelihood of an 1048 * interrupt. 1049 * 1050 * tty_region/tty_reset/tty_update_mode already take care of not 1051 * resetting things that are already in their default state. 1052 */ 1053 tty_region(&c->tty, 0, c->tty.sy - 1); 1054 1055 status = options_get_number(oo, "status"); 1056 if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status) 1057 tty_cursor(&c->tty, 0, 0); 1058 else 1059 tty_cursor(&c->tty, wp->xoff + s->cx, wp->yoff + s->cy); 1060 1061 mode = s->mode; 1062 if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL && 1063 options_get_number(oo, "mouse-select-pane")) 1064 mode |= MODE_MOUSE; 1065 tty_update_mode(&c->tty, mode); 1066 tty_reset(&c->tty); 1067 } 1068 1069 /* Lost a client. */ 1070 void 1071 server_lost_client(struct client *c) 1072 { 1073 u_int i; 1074 1075 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 1076 if (ARRAY_ITEM(&clients, i) == c) 1077 ARRAY_SET(&clients, i, NULL); 1078 } 1079 log_debug("lost client %d", c->ibuf.fd); 1080 1081 /* 1082 * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called 1083 * and tty_free might close an unrelated fd. 1084 */ 1085 if (c->flags & CLIENT_TERMINAL) 1086 tty_free(&c->tty); 1087 1088 screen_free(&c->status); 1089 job_tree_free(&c->status_jobs); 1090 1091 if (c->title != NULL) 1092 xfree(c->title); 1093 1094 if (c->message_string != NULL) 1095 xfree(c->message_string); 1096 1097 if (c->prompt_string != NULL) 1098 xfree(c->prompt_string); 1099 if (c->prompt_buffer != NULL) 1100 xfree(c->prompt_buffer); 1101 for (i = 0; i < ARRAY_LENGTH(&c->prompt_hdata); i++) 1102 xfree(ARRAY_ITEM(&c->prompt_hdata, i)); 1103 ARRAY_FREE(&c->prompt_hdata); 1104 1105 if (c->cwd != NULL) 1106 xfree(c->cwd); 1107 1108 close(c->ibuf.fd); 1109 imsg_clear(&c->ibuf); 1110 1111 for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) { 1112 if (ARRAY_ITEM(&dead_clients, i) == NULL) { 1113 ARRAY_SET(&dead_clients, i, c); 1114 break; 1115 } 1116 } 1117 if (i == ARRAY_LENGTH(&dead_clients)) 1118 ARRAY_ADD(&dead_clients, c); 1119 c->flags |= CLIENT_DEAD; 1120 1121 recalculate_sizes(); 1122 } 1123 1124 /* Free dead, unreferenced clients and sessions. */ 1125 void 1126 server_clean_dead(void) 1127 { 1128 struct session *s; 1129 struct client *c; 1130 u_int i; 1131 1132 for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) { 1133 s = ARRAY_ITEM(&dead_sessions, i); 1134 if (s == NULL || s->references != 0) 1135 continue; 1136 ARRAY_SET(&dead_sessions, i, NULL); 1137 xfree(s); 1138 } 1139 1140 for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) { 1141 c = ARRAY_ITEM(&dead_clients, i); 1142 if (c == NULL || c->references != 0) 1143 continue; 1144 ARRAY_SET(&dead_clients, i, NULL); 1145 xfree(c); 1146 } 1147 } 1148 1149 /* Handle window data. */ 1150 void 1151 server_handle_window(struct window *w, struct window_pane *wp) 1152 { 1153 struct session *s; 1154 u_int i; 1155 int update; 1156 1157 window_pane_parse(wp); 1158 1159 if ((w->flags & (WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT)) == 0) 1160 return; 1161 1162 update = 0; 1163 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { 1164 s = ARRAY_ITEM(&sessions, i); 1165 if (s == NULL || !session_has(s, w)) 1166 continue; 1167 1168 update += server_check_window_bell(s, w); 1169 update += server_check_window_activity(s, w); 1170 update += server_check_window_content(s, w, wp); 1171 } 1172 if (update) 1173 server_status_window(w); 1174 1175 w->flags &= ~(WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT); 1176 } 1177 1178 int 1179 server_check_window_bell(struct session *s, struct window *w) 1180 { 1181 struct client *c; 1182 u_int i; 1183 int action, visual; 1184 1185 if (!(w->flags & WINDOW_BELL)) 1186 return (0); 1187 if (session_alert_has_window(s, w, WINDOW_BELL)) 1188 return (0); 1189 session_alert_add(s, w, WINDOW_BELL); 1190 1191 action = options_get_number(&s->options, "bell-action"); 1192 switch (action) { 1193 case BELL_ANY: 1194 if (s->flags & SESSION_UNATTACHED) 1195 break; 1196 visual = options_get_number(&s->options, "visual-bell"); 1197 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 1198 c = ARRAY_ITEM(&clients, i); 1199 if (c == NULL || c->session != s) 1200 continue; 1201 if (!visual) { 1202 tty_putcode(&c->tty, TTYC_BEL); 1203 continue; 1204 } 1205 if (c->session->curw->window == w) { 1206 status_message_set(c, "Bell in current window"); 1207 continue; 1208 } 1209 status_message_set(c, "Bell in window %u", 1210 winlink_find_by_window(&s->windows, w)->idx); 1211 } 1212 break; 1213 case BELL_CURRENT: 1214 if (s->flags & SESSION_UNATTACHED) 1215 break; 1216 visual = options_get_number(&s->options, "visual-bell"); 1217 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 1218 c = ARRAY_ITEM(&clients, i); 1219 if (c == NULL || c->session != s) 1220 continue; 1221 if (c->session->curw->window != w) 1222 continue; 1223 if (!visual) { 1224 tty_putcode(&c->tty, TTYC_BEL); 1225 continue; 1226 } 1227 status_message_set(c, "Bell in current window"); 1228 } 1229 break; 1230 } 1231 return (1); 1232 } 1233 1234 int 1235 server_check_window_activity(struct session *s, struct window *w) 1236 { 1237 struct client *c; 1238 u_int i; 1239 1240 if (!(w->flags & WINDOW_ACTIVITY)) 1241 return (0); 1242 1243 if (!options_get_number(&w->options, "monitor-activity")) 1244 return (0); 1245 1246 if (session_alert_has_window(s, w, WINDOW_ACTIVITY)) 1247 return (0); 1248 if (s->curw->window == w) 1249 return (0); 1250 1251 session_alert_add(s, w, WINDOW_ACTIVITY); 1252 if (s->flags & SESSION_UNATTACHED) 1253 return (0); 1254 if (options_get_number(&s->options, "visual-activity")) { 1255 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 1256 c = ARRAY_ITEM(&clients, i); 1257 if (c == NULL || c->session != s) 1258 continue; 1259 status_message_set(c, "Activity in window %u", 1260 winlink_find_by_window(&s->windows, w)->idx); 1261 } 1262 } 1263 1264 return (1); 1265 } 1266 1267 int 1268 server_check_window_content( 1269 struct session *s, struct window *w, struct window_pane *wp) 1270 { 1271 struct client *c; 1272 u_int i; 1273 char *found, *ptr; 1274 1275 if (!(w->flags & WINDOW_ACTIVITY)) /* activity for new content */ 1276 return (0); 1277 1278 ptr = options_get_string(&w->options, "monitor-content"); 1279 if (ptr == NULL || *ptr == '\0') 1280 return (0); 1281 1282 if (session_alert_has_window(s, w, WINDOW_CONTENT)) 1283 return (0); 1284 if (s->curw->window == w) 1285 return (0); 1286 1287 if ((found = window_pane_search(wp, ptr, NULL)) == NULL) 1288 return (0); 1289 xfree(found); 1290 1291 session_alert_add(s, w, WINDOW_CONTENT); 1292 if (s->flags & SESSION_UNATTACHED) 1293 return (0); 1294 if (options_get_number(&s->options, "visual-content")) { 1295 for (i = 0; i < ARRAY_LENGTH(&clients); i++) { 1296 c = ARRAY_ITEM(&clients, i); 1297 if (c == NULL || c->session != s) 1298 continue; 1299 status_message_set(c, "Content in window %u", 1300 winlink_find_by_window(&s->windows, w)->idx); 1301 } 1302 } 1303 1304 return (1); 1305 } 1306 1307 /* Check if window still exists. */ 1308 void 1309 server_check_window(struct window *w) 1310 { 1311 struct window_pane *wp, *wq; 1312 struct options *oo = &w->options; 1313 struct session *s; 1314 struct winlink *wl; 1315 u_int i; 1316 int destroyed; 1317 1318 destroyed = 1; 1319 1320 wp = TAILQ_FIRST(&w->panes); 1321 while (wp != NULL) { 1322 wq = TAILQ_NEXT(wp, entry); 1323 /* 1324 * If the pane has died and the remain-on-exit flag is not set, 1325 * remove the pane; otherwise, if the flag is set, don't allow 1326 * the window to be destroyed (or it'll close when the last 1327 * pane dies). 1328 */ 1329 if (wp->fd == -1 && !options_get_number(oo, "remain-on-exit")) { 1330 layout_close_pane(wp); 1331 window_remove_pane(w, wp); 1332 server_redraw_window(w); 1333 } else 1334 destroyed = 0; 1335 wp = wq; 1336 } 1337 1338 if (!destroyed) 1339 return; 1340 1341 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { 1342 s = ARRAY_ITEM(&sessions, i); 1343 if (s == NULL) 1344 continue; 1345 if (!session_has(s, w)) 1346 continue; 1347 1348 restart: 1349 /* Detach window and either redraw or kill clients. */ 1350 RB_FOREACH(wl, winlinks, &s->windows) { 1351 if (wl->window != w) 1352 continue; 1353 if (session_detach(s, wl)) { 1354 server_destroy_session_group(s); 1355 break; 1356 } 1357 server_redraw_session(s); 1358 server_status_session_group(s); 1359 goto restart; 1360 } 1361 } 1362 1363 recalculate_sizes(); 1364 } 1365 1366 /* Lock the server if ALL sessions have hit the time limit. */ 1367 void 1368 server_lock_server(void) 1369 { 1370 struct session *s; 1371 u_int i; 1372 int timeout; 1373 time_t t; 1374 1375 t = time(NULL); 1376 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { 1377 if ((s = ARRAY_ITEM(&sessions, i)) == NULL) 1378 continue; 1379 1380 if (s->flags & SESSION_UNATTACHED) { 1381 s->activity = time(NULL); 1382 continue; 1383 } 1384 1385 timeout = options_get_number(&s->options, "lock-after-time"); 1386 if (timeout <= 0 || t <= s->activity + timeout) 1387 return; /* not timed out */ 1388 } 1389 1390 server_lock(); 1391 recalculate_sizes(); 1392 } 1393 1394 /* Lock any sessions which have timed out. */ 1395 void 1396 server_lock_sessions(void) 1397 { 1398 struct session *s; 1399 u_int i; 1400 int timeout; 1401 time_t t; 1402 1403 t = time(NULL); 1404 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { 1405 if ((s = ARRAY_ITEM(&sessions, i)) == NULL) 1406 continue; 1407 1408 if (s->flags & SESSION_UNATTACHED) { 1409 s->activity = time(NULL); 1410 continue; 1411 } 1412 1413 timeout = options_get_number(&s->options, "lock-after-time"); 1414 if (timeout > 0 && t > s->activity + timeout) { 1415 server_lock_session(s); 1416 recalculate_sizes(); 1417 } 1418 } 1419 } 1420 1421 /* Call any once-per-second timers. */ 1422 void 1423 server_second_timers(void) 1424 { 1425 struct window *w; 1426 struct window_pane *wp; 1427 u_int i; 1428 1429 if (options_get_number(&global_s_options, "lock-server")) 1430 server_lock_server(); 1431 else 1432 server_lock_sessions(); 1433 1434 for (i = 0; i < ARRAY_LENGTH(&windows); i++) { 1435 w = ARRAY_ITEM(&windows, i); 1436 if (w == NULL) 1437 continue; 1438 1439 TAILQ_FOREACH(wp, &w->panes, entry) { 1440 if (wp->mode != NULL && wp->mode->timer != NULL) 1441 wp->mode->timer(wp); 1442 } 1443 } 1444 } 1445 1446 /* Update socket execute permissions based on whether sessions are attached. */ 1447 int 1448 server_update_socket(void) 1449 { 1450 struct session *s; 1451 u_int i; 1452 static int last = -1; 1453 int n; 1454 1455 n = 0; 1456 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { 1457 s = ARRAY_ITEM(&sessions, i); 1458 if (s != NULL && !(s->flags & SESSION_UNATTACHED)) { 1459 n++; 1460 break; 1461 } 1462 } 1463 1464 if (n != last) { 1465 last = n; 1466 if (n != 0) 1467 chmod(socket_path, S_IRWXU); 1468 else 1469 chmod(socket_path, S_IRUSR|S_IWUSR); 1470 } 1471 1472 return (n); 1473 } 1474