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