Fix a few memory leaks.
[tmux-openbsd.git] / session.c
blob73f44e1862ebab7a230f4a719dbdaba64aafabb5
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
20 #include <sys/time.h>
22 #include <paths.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <vis.h>
27 #include <time.h>
29 #include "tmux.h"
31 struct sessions sessions;
32 static u_int next_session_id;
33 struct session_groups session_groups = RB_INITIALIZER(&session_groups);
35 static void session_free(int, short, void *);
37 static void session_lock_timer(int, short, void *);
39 static struct winlink *session_next_alert(struct winlink *);
40 static struct winlink *session_previous_alert(struct winlink *);
42 static void session_group_remove(struct session *);
43 static void session_group_synchronize1(struct session *, struct session *);
45 int
46 session_cmp(struct session *s1, struct session *s2)
48 return (strcmp(s1->name, s2->name));
50 RB_GENERATE(sessions, session, entry, session_cmp);
52 static int
53 session_group_cmp(struct session_group *s1, struct session_group *s2)
55 return (strcmp(s1->name, s2->name));
57 RB_GENERATE_STATIC(session_groups, session_group, entry, session_group_cmp);
60 * Find if session is still alive. This is true if it is still on the global
61 * sessions list.
63 int
64 session_alive(struct session *s)
66 struct session *s_loop;
68 RB_FOREACH(s_loop, sessions, &sessions) {
69 if (s_loop == s)
70 return (1);
72 return (0);
75 /* Find session by name. */
76 struct session *
77 session_find(const char *name)
79 struct session s;
81 s.name = (char *) name;
82 return (RB_FIND(sessions, &sessions, &s));
85 /* Find session by id parsed from a string. */
86 struct session *
87 session_find_by_id_str(const char *s)
89 const char *errstr;
90 u_int id;
92 if (*s != '$')
93 return (NULL);
95 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
96 if (errstr != NULL)
97 return (NULL);
98 return (session_find_by_id(id));
101 /* Find session by id. */
102 struct session *
103 session_find_by_id(u_int id)
105 struct session *s;
107 RB_FOREACH(s, sessions, &sessions) {
108 if (s->id == id)
109 return (s);
111 return (NULL);
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)
119 struct session *s;
121 s = xcalloc(1, sizeof *s);
122 s->references = 1;
123 s->flags = 0;
125 s->cwd = xstrdup(cwd);
127 TAILQ_INIT(&s->lastw);
128 RB_INIT(&s->windows);
130 s->environ = env;
131 s->options = oo;
133 status_update_cache(s);
135 s->tio = NULL;
136 if (tio != NULL) {
137 s->tio = xmalloc(sizeof *s->tio);
138 memcpy(s->tio, tio, sizeof *s->tio);
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);
154 RB_INSERT(sessions, &sessions, s);
156 log_debug("new session %s $%u", s->name, s->id);
158 if (gettimeofday(&s->creation_time, NULL) != 0)
159 fatal("gettimeofday failed");
160 session_update_activity(s, &s->creation_time);
162 return (s);
165 /* Add a reference to a session. */
166 void
167 session_add_ref(struct session *s, const char *from)
169 s->references++;
170 log_debug("%s: %s %s, now %d", __func__, s->name, from, s->references);
173 /* Remove a reference from a session. */
174 void
175 session_remove_ref(struct session *s, const char *from)
177 s->references--;
178 log_debug("%s: %s %s, now %d", __func__, s->name, from, s->references);
180 if (s->references == 0)
181 event_once(-1, EV_TIMEOUT, session_free, s, NULL);
184 /* Free session. */
185 static void
186 session_free(__unused int fd, __unused short events, void *arg)
188 struct session *s = arg;
190 log_debug("session %s freed (%d references)", s->name, s->references);
192 if (s->references == 0) {
193 environ_free(s->environ);
194 options_free(s->options);
196 free(s->name);
197 free(s);
201 /* Destroy a session. */
202 void
203 session_destroy(struct session *s, int notify, const char *from)
205 struct winlink *wl;
207 log_debug("session %s destroyed (%s)", s->name, from);
208 s->curw = NULL;
210 RB_REMOVE(sessions, &sessions, s);
211 if (notify)
212 notify_session("session-closed", s);
214 free(s->tio);
216 if (event_initialized(&s->lock_timer))
217 event_del(&s->lock_timer);
219 session_group_remove(s);
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);
229 free((void *)s->cwd);
231 session_remove_ref(s, __func__);
234 /* Sanitize session name. */
235 char *
236 session_check_name(const char *name)
238 char *copy, *cp, *new_name;
240 if (*name == '\0')
241 return (NULL);
242 copy = xstrdup(name);
243 for (cp = copy; *cp != '\0'; cp++) {
244 if (*cp == ':' || *cp == '.')
245 *cp = '_';
247 utf8_stravis(&new_name, copy, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
248 free(copy);
249 return (new_name);
252 /* Lock session if it has timed out. */
253 static void
254 session_lock_timer(__unused int fd, __unused short events, void *arg)
256 struct session *s = arg;
258 if (s->attached == 0)
259 return;
261 log_debug("session %s locked, activity time %lld", s->name,
262 (long long)s->activity_time.tv_sec);
264 server_lock_session(s);
265 recalculate_sizes();
268 /* Update activity time. */
269 void
270 session_update_activity(struct session *s, struct timeval *from)
272 struct timeval *last = &s->last_activity_time;
273 struct timeval tv;
275 memcpy(last, &s->activity_time, sizeof *last);
276 if (from == NULL)
277 gettimeofday(&s->activity_time, NULL);
278 else
279 memcpy(&s->activity_time, from, sizeof s->activity_time);
281 log_debug("session $%u %s activity %lld.%06d (last %lld.%06d)", s->id,
282 s->name, (long long)s->activity_time.tv_sec,
283 (int)s->activity_time.tv_usec, (long long)last->tv_sec,
284 (int)last->tv_usec);
286 if (evtimer_initialized(&s->lock_timer))
287 evtimer_del(&s->lock_timer);
288 else
289 evtimer_set(&s->lock_timer, session_lock_timer, s);
291 if (s->attached != 0) {
292 timerclear(&tv);
293 tv.tv_sec = options_get_number(s->options, "lock-after-time");
294 if (tv.tv_sec != 0)
295 evtimer_add(&s->lock_timer, &tv);
299 /* Find the next usable session. */
300 struct session *
301 session_next_session(struct session *s)
303 struct session *s2;
305 if (RB_EMPTY(&sessions) || !session_alive(s))
306 return (NULL);
308 s2 = RB_NEXT(sessions, &sessions, s);
309 if (s2 == NULL)
310 s2 = RB_MIN(sessions, &sessions);
311 if (s2 == s)
312 return (NULL);
313 return (s2);
316 /* Find the previous usable session. */
317 struct session *
318 session_previous_session(struct session *s)
320 struct session *s2;
322 if (RB_EMPTY(&sessions) || !session_alive(s))
323 return (NULL);
325 s2 = RB_PREV(sessions, &sessions, s);
326 if (s2 == NULL)
327 s2 = RB_MAX(sessions, &sessions);
328 if (s2 == s)
329 return (NULL);
330 return (s2);
333 /* Attach a window to a session. */
334 struct winlink *
335 session_attach(struct session *s, struct window *w, int idx, char **cause)
337 struct winlink *wl;
339 if ((wl = winlink_add(&s->windows, idx)) == NULL) {
340 xasprintf(cause, "index in use: %d", idx);
341 return (NULL);
343 wl->session = s;
344 winlink_set_window(wl, w);
345 notify_session_window("window-linked", s, w);
347 session_group_synchronize_from(s);
348 return (wl);
351 /* Detach a window from a session. */
353 session_detach(struct session *s, struct winlink *wl)
355 if (s->curw == wl &&
356 session_last(s) != 0 &&
357 session_previous(s, 0) != 0)
358 session_next(s, 0);
360 wl->flags &= ~WINLINK_ALERTFLAGS;
361 notify_session_window("window-unlinked", s, wl->window);
362 winlink_stack_remove(&s->lastw, wl);
363 winlink_remove(&s->windows, wl);
365 session_group_synchronize_from(s);
367 if (RB_EMPTY(&s->windows)) {
368 session_destroy(s, 1, __func__);
369 return (1);
371 return (0);
374 /* Return if session has window. */
376 session_has(struct session *s, struct window *w)
378 struct winlink *wl;
380 TAILQ_FOREACH(wl, &w->winlinks, wentry) {
381 if (wl->session == s)
382 return (1);
384 return (0);
388 * Return 1 if a window is linked outside this session (not including session
389 * groups). The window must be in this session!
392 session_is_linked(struct session *s, struct window *w)
394 struct session_group *sg;
396 if ((sg = session_group_contains(s)) != NULL)
397 return (w->references != session_group_count(sg));
398 return (w->references != 1);
401 static struct winlink *
402 session_next_alert(struct winlink *wl)
404 while (wl != NULL) {
405 if (wl->flags & WINLINK_ALERTFLAGS)
406 break;
407 wl = winlink_next(wl);
409 return (wl);
412 /* Move session to next window. */
414 session_next(struct session *s, int alert)
416 struct winlink *wl;
418 if (s->curw == NULL)
419 return (-1);
421 wl = winlink_next(s->curw);
422 if (alert)
423 wl = session_next_alert(wl);
424 if (wl == NULL) {
425 wl = RB_MIN(winlinks, &s->windows);
426 if (alert && ((wl = session_next_alert(wl)) == NULL))
427 return (-1);
429 return (session_set_current(s, wl));
432 static struct winlink *
433 session_previous_alert(struct winlink *wl)
435 while (wl != NULL) {
436 if (wl->flags & WINLINK_ALERTFLAGS)
437 break;
438 wl = winlink_previous(wl);
440 return (wl);
443 /* Move session to previous window. */
445 session_previous(struct session *s, int alert)
447 struct winlink *wl;
449 if (s->curw == NULL)
450 return (-1);
452 wl = winlink_previous(s->curw);
453 if (alert)
454 wl = session_previous_alert(wl);
455 if (wl == NULL) {
456 wl = RB_MAX(winlinks, &s->windows);
457 if (alert && (wl = session_previous_alert(wl)) == NULL)
458 return (-1);
460 return (session_set_current(s, wl));
463 /* Move session to specific window. */
465 session_select(struct session *s, int idx)
467 struct winlink *wl;
469 wl = winlink_find_by_index(&s->windows, idx);
470 return (session_set_current(s, wl));
473 /* Move session to last used window. */
475 session_last(struct session *s)
477 struct winlink *wl;
479 wl = TAILQ_FIRST(&s->lastw);
480 if (wl == NULL)
481 return (-1);
482 if (wl == s->curw)
483 return (1);
485 return (session_set_current(s, wl));
488 /* Set current winlink to wl .*/
490 session_set_current(struct session *s, struct winlink *wl)
492 struct winlink *old = s->curw;
494 if (wl == NULL)
495 return (-1);
496 if (wl == s->curw)
497 return (1);
499 winlink_stack_remove(&s->lastw, wl);
500 winlink_stack_push(&s->lastw, s->curw);
501 s->curw = wl;
502 if (options_get_number(global_options, "focus-events")) {
503 window_update_focus(old->window);
504 window_update_focus(wl->window);
506 winlink_clear_flags(wl);
507 window_update_activity(wl->window);
508 tty_update_window_offset(wl->window);
509 notify_session("session-window-changed", s);
510 return (0);
513 /* Find the session group containing a session. */
514 struct session_group *
515 session_group_contains(struct session *target)
517 struct session_group *sg;
518 struct session *s;
520 RB_FOREACH(sg, session_groups, &session_groups) {
521 TAILQ_FOREACH(s, &sg->sessions, gentry) {
522 if (s == target)
523 return (sg);
526 return (NULL);
529 /* Find session group by name. */
530 struct session_group *
531 session_group_find(const char *name)
533 struct session_group sg;
535 sg.name = name;
536 return (RB_FIND(session_groups, &session_groups, &sg));
539 /* Create a new session group. */
540 struct session_group *
541 session_group_new(const char *name)
543 struct session_group *sg;
545 if ((sg = session_group_find(name)) != NULL)
546 return (sg);
548 sg = xcalloc(1, sizeof *sg);
549 sg->name = xstrdup(name);
550 TAILQ_INIT(&sg->sessions);
552 RB_INSERT(session_groups, &session_groups, sg);
553 return (sg);
556 /* Add a session to a session group. */
557 void
558 session_group_add(struct session_group *sg, struct session *s)
560 if (session_group_contains(s) == NULL)
561 TAILQ_INSERT_TAIL(&sg->sessions, s, gentry);
564 /* Remove a session from its group and destroy the group if empty. */
565 static void
566 session_group_remove(struct session *s)
568 struct session_group *sg;
570 if ((sg = session_group_contains(s)) == NULL)
571 return;
572 TAILQ_REMOVE(&sg->sessions, s, gentry);
573 if (TAILQ_EMPTY(&sg->sessions)) {
574 RB_REMOVE(session_groups, &session_groups, sg);
575 free((void *)sg->name);
576 free(sg);
580 /* Count number of sessions in session group. */
581 u_int
582 session_group_count(struct session_group *sg)
584 struct session *s;
585 u_int n;
587 n = 0;
588 TAILQ_FOREACH(s, &sg->sessions, gentry)
589 n++;
590 return (n);
593 /* Count number of clients attached to sessions in session group. */
594 u_int
595 session_group_attached_count(struct session_group *sg)
597 struct session *s;
598 u_int n;
600 n = 0;
601 TAILQ_FOREACH(s, &sg->sessions, gentry)
602 n += s->attached;
603 return (n);
606 /* Synchronize a session to its session group. */
607 void
608 session_group_synchronize_to(struct session *s)
610 struct session_group *sg;
611 struct session *target;
613 if ((sg = session_group_contains(s)) == NULL)
614 return;
616 target = NULL;
617 TAILQ_FOREACH(target, &sg->sessions, gentry) {
618 if (target != s)
619 break;
621 if (target != NULL)
622 session_group_synchronize1(target, s);
625 /* Synchronize a session group to a session. */
626 void
627 session_group_synchronize_from(struct session *target)
629 struct session_group *sg;
630 struct session *s;
632 if ((sg = session_group_contains(target)) == NULL)
633 return;
635 TAILQ_FOREACH(s, &sg->sessions, gentry) {
636 if (s != target)
637 session_group_synchronize1(target, s);
642 * Synchronize a session with a target session. This means destroying all
643 * winlinks then recreating them, then updating the current window, last window
644 * stack and alerts.
646 static void
647 session_group_synchronize1(struct session *target, struct session *s)
649 struct winlinks old_windows, *ww;
650 struct winlink_stack old_lastw;
651 struct winlink *wl, *wl2;
653 /* Don't do anything if the session is empty (it'll be destroyed). */
654 ww = &target->windows;
655 if (RB_EMPTY(ww))
656 return;
658 /* If the current window has vanished, move to the next now. */
659 if (s->curw != NULL &&
660 winlink_find_by_index(ww, s->curw->idx) == NULL &&
661 session_last(s) != 0 && session_previous(s, 0) != 0)
662 session_next(s, 0);
664 /* Save the old pointer and reset it. */
665 memcpy(&old_windows, &s->windows, sizeof old_windows);
666 RB_INIT(&s->windows);
668 /* Link all the windows from the target. */
669 RB_FOREACH(wl, winlinks, ww) {
670 wl2 = winlink_add(&s->windows, wl->idx);
671 wl2->session = s;
672 winlink_set_window(wl2, wl->window);
673 notify_session_window("window-linked", s, wl2->window);
674 wl2->flags |= wl->flags & WINLINK_ALERTFLAGS;
677 /* Fix up the current window. */
678 if (s->curw != NULL)
679 s->curw = winlink_find_by_index(&s->windows, s->curw->idx);
680 else
681 s->curw = winlink_find_by_index(&s->windows, target->curw->idx);
683 /* Fix up the last window stack. */
684 memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
685 TAILQ_INIT(&s->lastw);
686 TAILQ_FOREACH(wl, &old_lastw, sentry) {
687 wl2 = winlink_find_by_index(&s->windows, wl->idx);
688 if (wl2 != NULL)
689 TAILQ_INSERT_TAIL(&s->lastw, wl2, sentry);
692 /* Then free the old winlinks list. */
693 while (!RB_EMPTY(&old_windows)) {
694 wl = RB_ROOT(&old_windows);
695 wl2 = winlink_find_by_window_id(&s->windows, wl->window->id);
696 if (wl2 == NULL)
697 notify_session_window("window-unlinked", s, wl->window);
698 winlink_remove(&old_windows, wl);
702 /* Renumber the windows across winlinks attached to a specific session. */
703 void
704 session_renumber_windows(struct session *s)
706 struct winlink *wl, *wl1, *wl_new;
707 struct winlinks old_wins;
708 struct winlink_stack old_lastw;
709 int new_idx, new_curw_idx;
711 /* Save and replace old window list. */
712 memcpy(&old_wins, &s->windows, sizeof old_wins);
713 RB_INIT(&s->windows);
715 /* Start renumbering from the base-index if it's set. */
716 new_idx = options_get_number(s->options, "base-index");
717 new_curw_idx = 0;
719 /* Go through the winlinks and assign new indexes. */
720 RB_FOREACH(wl, winlinks, &old_wins) {
721 wl_new = winlink_add(&s->windows, new_idx);
722 wl_new->session = s;
723 winlink_set_window(wl_new, wl->window);
724 wl_new->flags |= wl->flags & WINLINK_ALERTFLAGS;
726 if (wl == s->curw)
727 new_curw_idx = wl_new->idx;
729 new_idx++;
732 /* Fix the stack of last windows now. */
733 memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
734 TAILQ_INIT(&s->lastw);
735 TAILQ_FOREACH(wl, &old_lastw, sentry) {
736 wl_new = winlink_find_by_window(&s->windows, wl->window);
737 if (wl_new != NULL)
738 TAILQ_INSERT_TAIL(&s->lastw, wl_new, sentry);
741 /* Set the current window. */
742 s->curw = winlink_find_by_index(&s->windows, new_curw_idx);
744 /* Free the old winlinks (reducing window references too). */
745 RB_FOREACH_SAFE(wl, winlinks, &old_wins, wl1)
746 winlink_remove(&old_wins, wl);