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