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