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