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