1 /* $OpenBSD$ */ 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/time.h> 21 22 #include <string.h> 23 #include <stdlib.h> 24 #include <unistd.h> 25 #include <time.h> 26 27 #include "tmux.h" 28 29 struct sessions sessions; 30 static u_int next_session_id; 31 struct session_groups session_groups; 32 33 static void session_free(int, short, void *); 34 35 static void session_lock_timer(int, short, void *); 36 37 static struct winlink *session_next_alert(struct winlink *); 38 static struct winlink *session_previous_alert(struct winlink *); 39 40 static void session_group_remove(struct session *); 41 static u_int session_group_count(struct session_group *); 42 static void session_group_synchronize1(struct session *, struct session *); 43 44 static u_int session_group_count(struct session_group *); 45 static void session_group_synchronize1(struct session *, struct session *); 46 47 RB_GENERATE(sessions, session, entry, session_cmp); 48 49 int 50 session_cmp(struct session *s1, struct session *s2) 51 { 52 return (strcmp(s1->name, s2->name)); 53 } 54 55 RB_GENERATE(session_groups, session_group, entry, session_group_cmp); 56 57 int 58 session_group_cmp(struct session_group *s1, struct session_group *s2) 59 { 60 return (strcmp(s1->name, s2->name)); 61 } 62 63 /* 64 * Find if session is still alive. This is true if it is still on the global 65 * sessions list. 66 */ 67 int 68 session_alive(struct session *s) 69 { 70 struct session *s_loop; 71 72 RB_FOREACH(s_loop, sessions, &sessions) { 73 if (s_loop == s) 74 return (1); 75 } 76 return (0); 77 } 78 79 /* Find session by name. */ 80 struct session * 81 session_find(const char *name) 82 { 83 struct session s; 84 85 s.name = __UNCONST(name); 86 return (RB_FIND(sessions, &sessions, &s)); 87 } 88 89 /* Find session by id parsed from a string. */ 90 struct session * 91 session_find_by_id_str(const char *s) 92 { 93 const char *errstr; 94 u_int id; 95 96 if (*s != '$') 97 return (NULL); 98 99 id = strtonum(s + 1, 0, UINT_MAX, &errstr); 100 if (errstr != NULL) 101 return (NULL); 102 return (session_find_by_id(id)); 103 } 104 105 /* Find session by id. */ 106 struct session * 107 session_find_by_id(u_int id) 108 { 109 struct session *s; 110 111 RB_FOREACH(s, sessions, &sessions) { 112 if (s->id == id) 113 return (s); 114 } 115 return (NULL); 116 } 117 118 /* Create a new session. */ 119 struct session * 120 session_create(const char *prefix, const char *name, int argc, char **argv, 121 const char *path, const char *cwd, struct environ *env, struct termios *tio, 122 int idx, u_int sx, u_int sy, char **cause) 123 { 124 struct session *s; 125 struct winlink *wl; 126 127 s = xcalloc(1, sizeof *s); 128 s->references = 1; 129 s->flags = 0; 130 131 s->cwd = xstrdup(cwd); 132 133 s->curw = NULL; 134 TAILQ_INIT(&s->lastw); 135 RB_INIT(&s->windows); 136 137 s->environ = environ_create(); 138 if (env != NULL) 139 environ_copy(env, s->environ); 140 141 s->options = options_create(global_s_options); 142 s->hooks = hooks_create(global_hooks); 143 144 status_update_saved(s); 145 146 s->tio = NULL; 147 if (tio != NULL) { 148 s->tio = xmalloc(sizeof *s->tio); 149 memcpy(s->tio, tio, sizeof *s->tio); 150 } 151 152 s->sx = sx; 153 s->sy = sy; 154 155 if (name != NULL) { 156 s->name = xstrdup(name); 157 s->id = next_session_id++; 158 } else { 159 s->name = NULL; 160 do { 161 s->id = next_session_id++; 162 free(s->name); 163 if (prefix != NULL) 164 xasprintf(&s->name, "%s-%u", prefix, s->id); 165 else 166 xasprintf(&s->name, "%u", s->id); 167 } while (RB_FIND(sessions, &sessions, s) != NULL); 168 } 169 RB_INSERT(sessions, &sessions, s); 170 171 log_debug("new session %s $%u", s->name, s->id); 172 173 if (gettimeofday(&s->creation_time, NULL) != 0) 174 fatal("gettimeofday failed"); 175 session_update_activity(s, &s->creation_time); 176 177 if (argc >= 0) { 178 wl = session_new(s, NULL, argc, argv, path, cwd, idx, cause); 179 if (wl == NULL) { 180 session_destroy(s, __func__); 181 return (NULL); 182 } 183 session_select(s, RB_ROOT(&s->windows)->idx); 184 } 185 186 log_debug("session %s created", s->name); 187 188 return (s); 189 } 190 191 /* Add a reference to a session. */ 192 void 193 session_add_ref(struct session *s, const char *from) 194 { 195 s->references++; 196 log_debug("%s: %s %s, now %d", __func__, s->name, from, s->references); 197 } 198 199 /* Remove a reference from a session. */ 200 void 201 session_remove_ref(struct session *s, const char *from) 202 { 203 s->references--; 204 log_debug("%s: %s %s, now %d", __func__, s->name, from, s->references); 205 206 if (s->references == 0) 207 event_once(-1, EV_TIMEOUT, session_free, s, NULL); 208 } 209 210 /* Free session. */ 211 static void 212 session_free(__unused int fd, __unused short events, void *arg) 213 { 214 struct session *s = arg; 215 216 log_debug("session %s freed (%d references)", s->name, s->references); 217 218 if (s->references == 0) { 219 environ_free(s->environ); 220 221 options_free(s->options); 222 hooks_free(s->hooks); 223 224 free(s->name); 225 free(s); 226 } 227 } 228 229 /* Destroy a session. */ 230 void 231 session_destroy(struct session *s, const char *from) 232 { 233 struct winlink *wl; 234 235 log_debug("session %s destroyed (%s)", s->name, from); 236 s->curw = NULL; 237 238 RB_REMOVE(sessions, &sessions, s); 239 notify_session("session-closed", s); 240 241 free(s->tio); 242 243 if (event_initialized(&s->lock_timer)) 244 event_del(&s->lock_timer); 245 246 session_group_remove(s); 247 248 while (!TAILQ_EMPTY(&s->lastw)) 249 winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw)); 250 while (!RB_EMPTY(&s->windows)) { 251 wl = RB_ROOT(&s->windows); 252 notify_session_window("window-unlinked", s, wl->window); 253 winlink_remove(&s->windows, wl); 254 } 255 256 free(__UNCONST(s->cwd)); 257 258 session_remove_ref(s, __func__); 259 } 260 261 /* Check a session name is valid: not empty and no colons or periods. */ 262 int 263 session_check_name(const char *name) 264 { 265 return (*name != '\0' && name[strcspn(name, ":.")] == '\0'); 266 } 267 268 /* Lock session if it has timed out. */ 269 static void 270 session_lock_timer(__unused int fd, __unused short events, void *arg) 271 { 272 struct session *s = arg; 273 274 if (s->flags & SESSION_UNATTACHED) 275 return; 276 277 log_debug("session %s locked, activity time %lld", s->name, 278 (long long)s->activity_time.tv_sec); 279 280 server_lock_session(s); 281 recalculate_sizes(); 282 } 283 284 /* Update activity time. */ 285 void 286 session_update_activity(struct session *s, struct timeval *from) 287 { 288 struct timeval *last = &s->last_activity_time; 289 struct timeval tv; 290 291 memcpy(last, &s->activity_time, sizeof *last); 292 if (from == NULL) 293 gettimeofday(&s->activity_time, NULL); 294 else 295 memcpy(&s->activity_time, from, sizeof s->activity_time); 296 297 log_debug("session %s activity %lld.%06d (last %lld.%06d)", s->name, 298 (long long)s->activity_time.tv_sec, (int)s->activity_time.tv_usec, 299 (long long)last->tv_sec, (int)last->tv_usec); 300 301 if (evtimer_initialized(&s->lock_timer)) 302 evtimer_del(&s->lock_timer); 303 else 304 evtimer_set(&s->lock_timer, session_lock_timer, s); 305 306 if (~s->flags & SESSION_UNATTACHED) { 307 timerclear(&tv); 308 tv.tv_sec = options_get_number(s->options, "lock-after-time"); 309 if (tv.tv_sec != 0) 310 evtimer_add(&s->lock_timer, &tv); 311 } 312 } 313 314 /* Find the next usable session. */ 315 struct session * 316 session_next_session(struct session *s) 317 { 318 struct session *s2; 319 320 if (RB_EMPTY(&sessions) || !session_alive(s)) 321 return (NULL); 322 323 s2 = RB_NEXT(sessions, &sessions, s); 324 if (s2 == NULL) 325 s2 = RB_MIN(sessions, &sessions); 326 if (s2 == s) 327 return (NULL); 328 return (s2); 329 } 330 331 /* Find the previous usable session. */ 332 struct session * 333 session_previous_session(struct session *s) 334 { 335 struct session *s2; 336 337 if (RB_EMPTY(&sessions) || !session_alive(s)) 338 return (NULL); 339 340 s2 = RB_PREV(sessions, &sessions, s); 341 if (s2 == NULL) 342 s2 = RB_MAX(sessions, &sessions); 343 if (s2 == s) 344 return (NULL); 345 return (s2); 346 } 347 348 /* Create a new window on a session. */ 349 struct winlink * 350 session_new(struct session *s, const char *name, int argc, char **argv, 351 const char *path, const char *cwd, int idx, char **cause) 352 { 353 struct window *w; 354 struct winlink *wl; 355 struct environ *env; 356 const char *shell; 357 u_int hlimit; 358 359 if ((wl = winlink_add(&s->windows, idx)) == NULL) { 360 xasprintf(cause, "index in use: %d", idx); 361 return (NULL); 362 } 363 wl->session = s; 364 365 shell = options_get_string(s->options, "default-shell"); 366 if (*shell == '\0' || areshell(shell)) 367 shell = _PATH_BSHELL; 368 369 hlimit = options_get_number(s->options, "history-limit"); 370 env = environ_for_session(s, 0); 371 w = window_create_spawn(name, argc, argv, path, shell, cwd, env, s->tio, 372 s->sx, s->sy, hlimit, cause); 373 if (w == NULL) { 374 winlink_remove(&s->windows, wl); 375 environ_free(env); 376 return (NULL); 377 } 378 winlink_set_window(wl, w); 379 environ_free(env); 380 notify_session_window("window-linked", s, w); 381 382 session_group_synchronize_from(s); 383 return (wl); 384 } 385 386 /* Attach a window to a session. */ 387 struct winlink * 388 session_attach(struct session *s, struct window *w, int idx, char **cause) 389 { 390 struct winlink *wl; 391 392 if ((wl = winlink_add(&s->windows, idx)) == NULL) { 393 xasprintf(cause, "index in use: %d", idx); 394 return (NULL); 395 } 396 wl->session = s; 397 winlink_set_window(wl, w); 398 notify_session_window("window-linked", s, w); 399 400 session_group_synchronize_from(s); 401 return (wl); 402 } 403 404 /* Detach a window from a session. */ 405 int 406 session_detach(struct session *s, struct winlink *wl) 407 { 408 if (s->curw == wl && 409 session_last(s) != 0 && 410 session_previous(s, 0) != 0) 411 session_next(s, 0); 412 413 wl->flags &= ~WINLINK_ALERTFLAGS; 414 notify_session_window("window-unlinked", s, wl->window); 415 winlink_stack_remove(&s->lastw, wl); 416 winlink_remove(&s->windows, wl); 417 418 session_group_synchronize_from(s); 419 420 if (RB_EMPTY(&s->windows)) { 421 session_destroy(s, __func__); 422 return (1); 423 } 424 return (0); 425 } 426 427 /* Return if session has window. */ 428 int 429 session_has(struct session *s, struct window *w) 430 { 431 struct winlink *wl; 432 433 TAILQ_FOREACH(wl, &w->winlinks, wentry) { 434 if (wl->session == s) 435 return (1); 436 } 437 return (0); 438 } 439 440 /* 441 * Return 1 if a window is linked outside this session (not including session 442 * groups). The window must be in this session! 443 */ 444 int 445 session_is_linked(struct session *s, struct window *w) 446 { 447 struct session_group *sg; 448 449 if ((sg = session_group_contains(s)) != NULL) 450 return (w->references != session_group_count(sg)); 451 return (w->references != 1); 452 } 453 454 static struct winlink * 455 session_next_alert(struct winlink *wl) 456 { 457 while (wl != NULL) { 458 if (wl->flags & WINLINK_ALERTFLAGS) 459 break; 460 wl = winlink_next(wl); 461 } 462 return (wl); 463 } 464 465 /* Move session to next window. */ 466 int 467 session_next(struct session *s, int alert) 468 { 469 struct winlink *wl; 470 471 if (s->curw == NULL) 472 return (-1); 473 474 wl = winlink_next(s->curw); 475 if (alert) 476 wl = session_next_alert(wl); 477 if (wl == NULL) { 478 wl = RB_MIN(winlinks, &s->windows); 479 if (alert && ((wl = session_next_alert(wl)) == NULL)) 480 return (-1); 481 } 482 return (session_set_current(s, wl)); 483 } 484 485 static struct winlink * 486 session_previous_alert(struct winlink *wl) 487 { 488 while (wl != NULL) { 489 if (wl->flags & WINLINK_ALERTFLAGS) 490 break; 491 wl = winlink_previous(wl); 492 } 493 return (wl); 494 } 495 496 /* Move session to previous window. */ 497 int 498 session_previous(struct session *s, int alert) 499 { 500 struct winlink *wl; 501 502 if (s->curw == NULL) 503 return (-1); 504 505 wl = winlink_previous(s->curw); 506 if (alert) 507 wl = session_previous_alert(wl); 508 if (wl == NULL) { 509 wl = RB_MAX(winlinks, &s->windows); 510 if (alert && (wl = session_previous_alert(wl)) == NULL) 511 return (-1); 512 } 513 return (session_set_current(s, wl)); 514 } 515 516 /* Move session to specific window. */ 517 int 518 session_select(struct session *s, int idx) 519 { 520 struct winlink *wl; 521 522 wl = winlink_find_by_index(&s->windows, idx); 523 return (session_set_current(s, wl)); 524 } 525 526 /* Move session to last used window. */ 527 int 528 session_last(struct session *s) 529 { 530 struct winlink *wl; 531 532 wl = TAILQ_FIRST(&s->lastw); 533 if (wl == NULL) 534 return (-1); 535 if (wl == s->curw) 536 return (1); 537 538 return (session_set_current(s, wl)); 539 } 540 541 /* Set current winlink to wl .*/ 542 int 543 session_set_current(struct session *s, struct winlink *wl) 544 { 545 if (wl == NULL) 546 return (-1); 547 if (wl == s->curw) 548 return (1); 549 550 winlink_stack_remove(&s->lastw, wl); 551 winlink_stack_push(&s->lastw, s->curw); 552 s->curw = wl; 553 winlink_clear_flags(wl); 554 window_update_activity(wl->window); 555 notify_session("session-window-changed", s); 556 return (0); 557 } 558 559 /* Find the session group containing a session. */ 560 struct session_group * 561 session_group_contains(struct session *target) 562 { 563 struct session_group *sg; 564 struct session *s; 565 566 RB_FOREACH(sg, session_groups, &session_groups) { 567 TAILQ_FOREACH(s, &sg->sessions, gentry) { 568 if (s == target) 569 return (sg); 570 } 571 } 572 return (NULL); 573 } 574 575 /* Find session group by name. */ 576 struct session_group * 577 session_group_find(const char *name) 578 { 579 struct session_group sg; 580 581 sg.name = name; 582 return (RB_FIND(session_groups, &session_groups, &sg)); 583 } 584 585 /* Create a new session group. */ 586 struct session_group * 587 session_group_new(const char *name) 588 { 589 struct session_group *sg; 590 591 if ((sg = session_group_find(name)) != NULL) 592 return (sg); 593 594 sg = xcalloc(1, sizeof *sg); 595 sg->name = xstrdup(name); 596 TAILQ_INIT(&sg->sessions); 597 598 RB_INSERT(session_groups, &session_groups, sg); 599 return (sg); 600 } 601 602 /* Add a session to a session group. */ 603 void 604 session_group_add(struct session_group *sg, struct session *s) 605 { 606 if (session_group_contains(s) == NULL) 607 TAILQ_INSERT_TAIL(&sg->sessions, s, gentry); 608 } 609 610 /* Remove a session from its group and destroy the group if empty. */ 611 static void 612 session_group_remove(struct session *s) 613 { 614 struct session_group *sg; 615 616 if ((sg = session_group_contains(s)) == NULL) 617 return; 618 TAILQ_REMOVE(&sg->sessions, s, gentry); 619 if (TAILQ_EMPTY(&sg->sessions)) { 620 RB_REMOVE(session_groups, &session_groups, sg); 621 free(sg); 622 } 623 } 624 625 /* Count number of sessions in session group. */ 626 static u_int 627 session_group_count(struct session_group *sg) 628 { 629 struct session *s; 630 u_int n; 631 632 n = 0; 633 TAILQ_FOREACH(s, &sg->sessions, gentry) 634 n++; 635 return (n); 636 } 637 638 /* Synchronize a session to its session group. */ 639 void 640 session_group_synchronize_to(struct session *s) 641 { 642 struct session_group *sg; 643 struct session *target; 644 645 if ((sg = session_group_contains(s)) == NULL) 646 return; 647 648 target = NULL; 649 TAILQ_FOREACH(target, &sg->sessions, gentry) { 650 if (target != s) 651 break; 652 } 653 if (target != NULL) 654 session_group_synchronize1(target, s); 655 } 656 657 /* Synchronize a session group to a session. */ 658 void 659 session_group_synchronize_from(struct session *target) 660 { 661 struct session_group *sg; 662 struct session *s; 663 664 if ((sg = session_group_contains(target)) == NULL) 665 return; 666 667 TAILQ_FOREACH(s, &sg->sessions, gentry) { 668 if (s != target) 669 session_group_synchronize1(target, s); 670 } 671 } 672 673 /* 674 * Synchronize a session with a target session. This means destroying all 675 * winlinks then recreating them, then updating the current window, last window 676 * stack and alerts. 677 */ 678 static void 679 session_group_synchronize1(struct session *target, struct session *s) 680 { 681 struct winlinks old_windows, *ww; 682 struct winlink_stack old_lastw; 683 struct winlink *wl, *wl2; 684 685 /* Don't do anything if the session is empty (it'll be destroyed). */ 686 ww = &target->windows; 687 if (RB_EMPTY(ww)) 688 return; 689 690 /* If the current window has vanished, move to the next now. */ 691 if (s->curw != NULL && 692 winlink_find_by_index(ww, s->curw->idx) == NULL && 693 session_last(s) != 0 && session_previous(s, 0) != 0) 694 session_next(s, 0); 695 696 /* Save the old pointer and reset it. */ 697 memcpy(&old_windows, &s->windows, sizeof old_windows); 698 RB_INIT(&s->windows); 699 700 /* Link all the windows from the target. */ 701 RB_FOREACH(wl, winlinks, ww) { 702 wl2 = winlink_add(&s->windows, wl->idx); 703 wl2->session = s; 704 winlink_set_window(wl2, wl->window); 705 notify_session_window("window-linked", s, wl2->window); 706 wl2->flags |= wl->flags & WINLINK_ALERTFLAGS; 707 } 708 709 /* Fix up the current window. */ 710 if (s->curw != NULL) 711 s->curw = winlink_find_by_index(&s->windows, s->curw->idx); 712 else 713 s->curw = winlink_find_by_index(&s->windows, target->curw->idx); 714 715 /* Fix up the last window stack. */ 716 memcpy(&old_lastw, &s->lastw, sizeof old_lastw); 717 TAILQ_INIT(&s->lastw); 718 TAILQ_FOREACH(wl, &old_lastw, sentry) { 719 wl2 = winlink_find_by_index(&s->windows, wl->idx); 720 if (wl2 != NULL) 721 TAILQ_INSERT_TAIL(&s->lastw, wl2, sentry); 722 } 723 724 /* Then free the old winlinks list. */ 725 while (!RB_EMPTY(&old_windows)) { 726 wl = RB_ROOT(&old_windows); 727 wl2 = winlink_find_by_window_id(&s->windows, wl->window->id); 728 if (wl2 == NULL) 729 notify_session_window("window-unlinked", s, wl->window); 730 winlink_remove(&old_windows, wl); 731 } 732 } 733 734 /* Renumber the windows across winlinks attached to a specific session. */ 735 void 736 session_renumber_windows(struct session *s) 737 { 738 struct winlink *wl, *wl1, *wl_new; 739 struct winlinks old_wins; 740 struct winlink_stack old_lastw; 741 int new_idx, new_curw_idx; 742 743 /* Save and replace old window list. */ 744 memcpy(&old_wins, &s->windows, sizeof old_wins); 745 RB_INIT(&s->windows); 746 747 /* Start renumbering from the base-index if it's set. */ 748 new_idx = options_get_number(s->options, "base-index"); 749 new_curw_idx = 0; 750 751 /* Go through the winlinks and assign new indexes. */ 752 RB_FOREACH(wl, winlinks, &old_wins) { 753 wl_new = winlink_add(&s->windows, new_idx); 754 wl_new->session = s; 755 winlink_set_window(wl_new, wl->window); 756 wl_new->flags |= wl->flags & WINLINK_ALERTFLAGS; 757 758 if (wl == s->curw) 759 new_curw_idx = wl_new->idx; 760 761 new_idx++; 762 } 763 764 /* Fix the stack of last windows now. */ 765 memcpy(&old_lastw, &s->lastw, sizeof old_lastw); 766 TAILQ_INIT(&s->lastw); 767 TAILQ_FOREACH(wl, &old_lastw, sentry) { 768 wl_new = winlink_find_by_window(&s->windows, wl->window); 769 if (wl_new != NULL) 770 TAILQ_INSERT_TAIL(&s->lastw, wl_new, sentry); 771 } 772 773 /* Set the current window. */ 774 s->curw = winlink_find_by_index(&s->windows, new_curw_idx); 775 776 /* Free the old winlinks (reducing window references too). */ 777 RB_FOREACH_SAFE(wl, winlinks, &old_wins, wl1) 778 winlink_remove(&old_wins, wl); 779 } 780