Annotation of src/usr.bin/tmux/session.c, Revision 1.32
1.32 ! nicm 1: /* $OpenBSD: session.c,v 1.31 2011/04/06 21:51:31 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.25 nicm 33: u_int next_session;
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:
73: /* Find session by index. */
74: struct session *
75: session_find_by_index(u_int idx)
76: {
1.1 nicm 77: struct session *s;
78:
1.25 nicm 79: RB_FOREACH(s, sessions, &sessions) {
80: if (s->idx == idx)
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.32 ! nicm 124: s->idx = next_session++;
! 125: } else {
! 126: s->name = NULL;
! 127: do {
! 128: s->idx = next_session++;
! 129: if (s->name != NULL)
! 130: xfree (s->name);
! 131: xasprintf(&s->name, "%u", s->idx);
! 132: } while (RB_FIND(sessions, &sessions, s) != NULL);
! 133: }
1.25 nicm 134: RB_INSERT(sessions, &sessions, s);
1.15 nicm 135:
1.11 nicm 136: if (cmd != NULL) {
137: if (session_new(s, NULL, cmd, cwd, idx, cause) == NULL) {
138: session_destroy(s);
139: return (NULL);
140: }
141: session_select(s, RB_ROOT(&s->windows)->idx);
1.1 nicm 142: }
143:
144: log_debug("session %s created", s->name);
145:
146: return (s);
147: }
148:
149: /* Destroy a session. */
150: void
151: session_destroy(struct session *s)
152: {
153: log_debug("session %s destroyed", s->name);
154:
1.25 nicm 155: RB_REMOVE(sessions, &sessions, s);
1.1 nicm 156:
1.8 nicm 157: if (s->tio != NULL)
158: xfree(s->tio);
159:
1.11 nicm 160: session_group_remove(s);
1.3 nicm 161: environ_free(&s->environ);
1.1 nicm 162: options_free(&s->options);
163:
1.11 nicm 164: while (!TAILQ_EMPTY(&s->lastw))
165: winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw));
1.1 nicm 166: while (!RB_EMPTY(&s->windows))
167: winlink_remove(&s->windows, RB_ROOT(&s->windows));
168:
1.19 nicm 169: xfree(s->cwd);
1.15 nicm 170:
1.25 nicm 171: RB_INSERT(sessions, &dead_sessions, s);
1.31 nicm 172: }
173:
174: /* Check a session name is valid: not empty and no colons. */
175: int
176: session_check_name(const char *name)
177: {
178: return (*name != '\0' && strchr(name, ':') == NULL);
1.27 nicm 179: }
180:
181: /* Update session active time. */
182: void
183: session_update_activity(struct session *s)
184: {
185: if (gettimeofday(&s->activity_time, NULL) != 0)
186: fatal("gettimeofday");
1.20 nicm 187: }
188:
189: /* Find the next usable session. */
190: struct session *
191: session_next_session(struct session *s)
192: {
193: struct session *s2;
194:
1.25 nicm 195: if (RB_EMPTY(&sessions) || !session_alive(s))
1.20 nicm 196: return (NULL);
197:
1.29 nicm 198: s2 = RB_NEXT(sessions, &sessions, s);
1.28 nicm 199: if (s2 == NULL)
200: s2 = RB_MIN(sessions, &sessions);
1.25 nicm 201: if (s2 == s)
202: return (NULL);
1.20 nicm 203: return (s2);
204: }
205:
206: /* Find the previous usable session. */
207: struct session *
208: session_previous_session(struct session *s)
209: {
210: struct session *s2;
211:
1.25 nicm 212: if (RB_EMPTY(&sessions) || !session_alive(s))
1.20 nicm 213: return (NULL);
214:
1.29 nicm 215: s2 = RB_PREV(sessions, &sessions, s);
1.28 nicm 216: if (s2 == NULL)
217: s2 = RB_MAX(sessions, &sessions);
1.25 nicm 218: if (s2 == s)
219: return (NULL);
1.20 nicm 220: return (s2);
1.1 nicm 221: }
222:
223: /* Create a new window on a session. */
224: struct winlink *
1.15 nicm 225: session_new(struct session *s,
1.1 nicm 226: const char *name, const char *cmd, const char *cwd, int idx, char **cause)
227: {
228: struct window *w;
1.30 nicm 229: struct winlink *wl;
1.3 nicm 230: struct environ env;
1.6 nicm 231: const char *shell;
1.1 nicm 232: u_int hlimit;
233:
1.30 nicm 234: if ((wl = winlink_add(&s->windows, idx)) == NULL) {
235: xasprintf(cause, "index in use: %d", idx);
236: return (NULL);
237: }
238:
1.3 nicm 239: environ_init(&env);
240: environ_copy(&global_environ, &env);
241: environ_copy(&s->environ, &env);
242: server_fill_environ(s, &env);
1.1 nicm 243:
1.6 nicm 244: shell = options_get_string(&s->options, "default-shell");
245: if (*shell == '\0' || areshell(shell))
246: shell = _PATH_BSHELL;
247:
1.1 nicm 248: hlimit = options_get_number(&s->options, "history-limit");
1.4 nicm 249: w = window_create(
1.8 nicm 250: name, cmd, shell, cwd, &env, s->tio, s->sx, s->sy, hlimit, cause);
1.3 nicm 251: if (w == NULL) {
1.30 nicm 252: winlink_remove(&s->windows, wl);
1.3 nicm 253: environ_free(&env);
1.1 nicm 254: return (NULL);
1.3 nicm 255: }
1.30 nicm 256: winlink_set_window(wl, w);
1.3 nicm 257: environ_free(&env);
1.1 nicm 258:
259: if (options_get_number(&s->options, "set-remain-on-exit"))
260: options_set_number(&w->options, "remain-on-exit", 1);
261:
1.30 nicm 262: session_group_synchronize_from(s);
263: return (wl);
1.1 nicm 264: }
265:
266: /* Attach a window to a session. */
267: struct winlink *
268: session_attach(struct session *s, struct window *w, int idx, char **cause)
269: {
270: struct winlink *wl;
271:
1.30 nicm 272: if ((wl = winlink_add(&s->windows, idx)) == NULL) {
1.1 nicm 273: xasprintf(cause, "index in use: %d", idx);
1.30 nicm 274: return (NULL);
275: }
276: winlink_set_window(wl, w);
277:
1.11 nicm 278: session_group_synchronize_from(s);
1.1 nicm 279: return (wl);
280: }
281:
282: /* Detach a window from a session. */
283: int
284: session_detach(struct session *s, struct winlink *wl)
285: {
286: if (s->curw == wl &&
287: session_last(s) != 0 && session_previous(s, 0) != 0)
288: session_next(s, 0);
289:
1.18 nicm 290: wl->flags &= ~WINLINK_ALERTFLAGS;
1.1 nicm 291: winlink_stack_remove(&s->lastw, wl);
292: winlink_remove(&s->windows, wl);
1.11 nicm 293: session_group_synchronize_from(s);
1.1 nicm 294: if (RB_EMPTY(&s->windows)) {
295: session_destroy(s);
296: return (1);
297: }
298: return (0);
299: }
300:
301: /* Return if session has window. */
1.18 nicm 302: struct winlink *
1.1 nicm 303: session_has(struct session *s, struct window *w)
304: {
305: struct winlink *wl;
306:
307: RB_FOREACH(wl, winlinks, &s->windows) {
308: if (wl->window == w)
1.18 nicm 309: return (wl);
1.1 nicm 310: }
1.18 nicm 311: return (NULL);
1.1 nicm 312: }
313:
314: struct winlink *
1.18 nicm 315: session_next_alert(struct winlink *wl)
1.1 nicm 316: {
317: while (wl != NULL) {
1.18 nicm 318: if (wl->flags & WINLINK_ALERTFLAGS)
1.1 nicm 319: break;
1.14 nicm 320: wl = winlink_next(wl);
1.1 nicm 321: }
322: return (wl);
323: }
324:
325: /* Move session to next window. */
326: int
1.17 nicm 327: session_next(struct session *s, int alert)
1.1 nicm 328: {
329: struct winlink *wl;
330:
331: if (s->curw == NULL)
332: return (-1);
333:
1.14 nicm 334: wl = winlink_next(s->curw);
1.17 nicm 335: if (alert)
1.18 nicm 336: wl = session_next_alert(wl);
1.1 nicm 337: if (wl == NULL) {
338: wl = RB_MIN(winlinks, &s->windows);
1.18 nicm 339: if (alert && ((wl = session_next_alert(wl)) == NULL))
1.1 nicm 340: return (-1);
341: }
342: if (wl == s->curw)
343: return (1);
344: winlink_stack_remove(&s->lastw, wl);
345: winlink_stack_push(&s->lastw, s->curw);
346: s->curw = wl;
1.18 nicm 347: wl->flags &= ~WINLINK_ALERTFLAGS;
1.1 nicm 348: return (0);
349: }
350:
351: struct winlink *
1.18 nicm 352: session_previous_alert(struct winlink *wl)
1.1 nicm 353: {
354: while (wl != NULL) {
1.18 nicm 355: if (wl->flags & WINLINK_ALERTFLAGS)
1.1 nicm 356: break;
1.14 nicm 357: wl = winlink_previous(wl);
1.1 nicm 358: }
359: return (wl);
360: }
361:
362: /* Move session to previous window. */
363: int
1.17 nicm 364: session_previous(struct session *s, int alert)
1.1 nicm 365: {
366: struct winlink *wl;
367:
368: if (s->curw == NULL)
369: return (-1);
370:
1.14 nicm 371: wl = winlink_previous(s->curw);
1.17 nicm 372: if (alert)
1.18 nicm 373: wl = session_previous_alert(wl);
1.1 nicm 374: if (wl == NULL) {
375: wl = RB_MAX(winlinks, &s->windows);
1.18 nicm 376: if (alert && (wl = session_previous_alert(wl)) == NULL)
1.1 nicm 377: return (-1);
378: }
379: if (wl == s->curw)
380: return (1);
381: winlink_stack_remove(&s->lastw, wl);
382: winlink_stack_push(&s->lastw, s->curw);
383: s->curw = wl;
1.18 nicm 384: wl->flags &= ~WINLINK_ALERTFLAGS;
1.1 nicm 385: return (0);
386: }
387:
388: /* Move session to specific window. */
389: int
390: session_select(struct session *s, int idx)
391: {
392: struct winlink *wl;
393:
394: wl = winlink_find_by_index(&s->windows, idx);
395: if (wl == NULL)
396: return (-1);
397: if (wl == s->curw)
398: return (1);
399: winlink_stack_remove(&s->lastw, wl);
400: winlink_stack_push(&s->lastw, s->curw);
401: s->curw = wl;
1.18 nicm 402: wl->flags &= ~WINLINK_ALERTFLAGS;
1.1 nicm 403: return (0);
404: }
405:
406: /* Move session to last used window. */
407: int
408: session_last(struct session *s)
409: {
410: struct winlink *wl;
411:
1.11 nicm 412: wl = TAILQ_FIRST(&s->lastw);
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.18 nicm 421: wl->flags &= ~WINLINK_ALERTFLAGS;
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);
489: xfree(sg);
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.18 nicm 558: wl2->flags |= wl->flags & WINLINK_ALERTFLAGS;
559: }
1.11 nicm 560:
561: /* Fix up the current window. */
562: if (s->curw != NULL)
563: s->curw = winlink_find_by_index(&s->windows, s->curw->idx);
564: else
565: s->curw = winlink_find_by_index(&s->windows, target->curw->idx);
566:
567: /* Fix up the last window stack. */
568: memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
569: TAILQ_INIT(&s->lastw);
570: TAILQ_FOREACH(wl, &old_lastw, sentry) {
571: wl2 = winlink_find_by_index(&s->windows, wl->idx);
572: if (wl2 != NULL)
573: TAILQ_INSERT_TAIL(&s->lastw, wl2, sentry);
574: }
575:
576: /* Then free the old winlinks list. */
577: while (!RB_EMPTY(&old_windows)) {
578: wl = RB_ROOT(&old_windows);
1.13 nicm 579: winlink_remove(&old_windows, wl);
1.11 nicm 580: }
1.1 nicm 581: }