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