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