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