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