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