Annotation of src/usr.bin/tmux/session.c, Revision 1.30
1.30 ! nicm 1: /* $OpenBSD: session.c,v 1.29 2011/01/13 02:08:14 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.25 nicm 122: s->idx = next_session++;
1.1 nicm 123: if (name != NULL)
124: s->name = xstrdup(name);
125: else
1.25 nicm 126: xasprintf(&s->name, "%u", s->idx);
127: RB_INSERT(sessions, &sessions, s);
1.15 nicm 128:
1.11 nicm 129: if (cmd != NULL) {
130: if (session_new(s, NULL, cmd, cwd, idx, cause) == NULL) {
131: session_destroy(s);
132: return (NULL);
133: }
134: session_select(s, RB_ROOT(&s->windows)->idx);
1.1 nicm 135: }
136:
137: log_debug("session %s created", s->name);
138:
139: return (s);
140: }
141:
142: /* Destroy a session. */
143: void
144: session_destroy(struct session *s)
145: {
146: log_debug("session %s destroyed", s->name);
147:
1.25 nicm 148: RB_REMOVE(sessions, &sessions, s);
1.1 nicm 149:
1.8 nicm 150: if (s->tio != NULL)
151: xfree(s->tio);
152:
1.11 nicm 153: session_group_remove(s);
1.3 nicm 154: environ_free(&s->environ);
1.1 nicm 155: options_free(&s->options);
156:
1.11 nicm 157: while (!TAILQ_EMPTY(&s->lastw))
158: winlink_stack_remove(&s->lastw, TAILQ_FIRST(&s->lastw));
1.1 nicm 159: while (!RB_EMPTY(&s->windows))
160: winlink_remove(&s->windows, RB_ROOT(&s->windows));
161:
1.19 nicm 162: xfree(s->cwd);
1.15 nicm 163:
1.25 nicm 164: RB_INSERT(sessions, &dead_sessions, s);
1.27 nicm 165: }
166:
167: /* Update session active time. */
168: void
169: session_update_activity(struct session *s)
170: {
171: if (gettimeofday(&s->activity_time, NULL) != 0)
172: fatal("gettimeofday");
1.20 nicm 173: }
174:
175: /* Find the next usable session. */
176: struct session *
177: session_next_session(struct session *s)
178: {
179: struct session *s2;
180:
1.25 nicm 181: if (RB_EMPTY(&sessions) || !session_alive(s))
1.20 nicm 182: return (NULL);
183:
1.29 nicm 184: s2 = RB_NEXT(sessions, &sessions, s);
1.28 nicm 185: if (s2 == NULL)
186: s2 = RB_MIN(sessions, &sessions);
1.25 nicm 187: if (s2 == s)
188: return (NULL);
1.20 nicm 189: return (s2);
190: }
191:
192: /* Find the previous usable session. */
193: struct session *
194: session_previous_session(struct session *s)
195: {
196: struct session *s2;
197:
1.25 nicm 198: if (RB_EMPTY(&sessions) || !session_alive(s))
1.20 nicm 199: return (NULL);
200:
1.29 nicm 201: s2 = RB_PREV(sessions, &sessions, s);
1.28 nicm 202: if (s2 == NULL)
203: s2 = RB_MAX(sessions, &sessions);
1.25 nicm 204: if (s2 == s)
205: return (NULL);
1.20 nicm 206: return (s2);
1.1 nicm 207: }
208:
209: /* Create a new window on a session. */
210: struct winlink *
1.15 nicm 211: session_new(struct session *s,
1.1 nicm 212: const char *name, const char *cmd, const char *cwd, int idx, char **cause)
213: {
214: struct window *w;
1.30 ! nicm 215: struct winlink *wl;
1.3 nicm 216: struct environ env;
1.6 nicm 217: const char *shell;
1.1 nicm 218: u_int hlimit;
219:
1.30 ! nicm 220: if ((wl = winlink_add(&s->windows, idx)) == NULL) {
! 221: xasprintf(cause, "index in use: %d", idx);
! 222: return (NULL);
! 223: }
! 224:
1.3 nicm 225: environ_init(&env);
226: environ_copy(&global_environ, &env);
227: environ_copy(&s->environ, &env);
228: server_fill_environ(s, &env);
1.1 nicm 229:
1.6 nicm 230: shell = options_get_string(&s->options, "default-shell");
231: if (*shell == '\0' || areshell(shell))
232: shell = _PATH_BSHELL;
233:
1.1 nicm 234: hlimit = options_get_number(&s->options, "history-limit");
1.4 nicm 235: w = window_create(
1.8 nicm 236: name, cmd, shell, cwd, &env, s->tio, s->sx, s->sy, hlimit, cause);
1.3 nicm 237: if (w == NULL) {
1.30 ! nicm 238: winlink_remove(&s->windows, wl);
1.3 nicm 239: environ_free(&env);
1.1 nicm 240: return (NULL);
1.3 nicm 241: }
1.30 ! nicm 242: winlink_set_window(wl, w);
1.3 nicm 243: environ_free(&env);
1.1 nicm 244:
245: if (options_get_number(&s->options, "set-remain-on-exit"))
246: options_set_number(&w->options, "remain-on-exit", 1);
247:
1.30 ! nicm 248: session_group_synchronize_from(s);
! 249: return (wl);
1.1 nicm 250: }
251:
252: /* Attach a window to a session. */
253: struct winlink *
254: session_attach(struct session *s, struct window *w, int idx, char **cause)
255: {
256: struct winlink *wl;
257:
1.30 ! nicm 258: if ((wl = winlink_add(&s->windows, idx)) == NULL) {
1.1 nicm 259: xasprintf(cause, "index in use: %d", idx);
1.30 ! nicm 260: return (NULL);
! 261: }
! 262: winlink_set_window(wl, w);
! 263:
1.11 nicm 264: session_group_synchronize_from(s);
1.1 nicm 265: return (wl);
266: }
267:
268: /* Detach a window from a session. */
269: int
270: session_detach(struct session *s, struct winlink *wl)
271: {
272: if (s->curw == wl &&
273: session_last(s) != 0 && session_previous(s, 0) != 0)
274: session_next(s, 0);
275:
1.18 nicm 276: wl->flags &= ~WINLINK_ALERTFLAGS;
1.1 nicm 277: winlink_stack_remove(&s->lastw, wl);
278: winlink_remove(&s->windows, wl);
1.11 nicm 279: session_group_synchronize_from(s);
1.1 nicm 280: if (RB_EMPTY(&s->windows)) {
281: session_destroy(s);
282: return (1);
283: }
284: return (0);
285: }
286:
287: /* Return if session has window. */
1.18 nicm 288: struct winlink *
1.1 nicm 289: session_has(struct session *s, struct window *w)
290: {
291: struct winlink *wl;
292:
293: RB_FOREACH(wl, winlinks, &s->windows) {
294: if (wl->window == w)
1.18 nicm 295: return (wl);
1.1 nicm 296: }
1.18 nicm 297: return (NULL);
1.1 nicm 298: }
299:
300: struct winlink *
1.18 nicm 301: session_next_alert(struct winlink *wl)
1.1 nicm 302: {
303: while (wl != NULL) {
1.18 nicm 304: if (wl->flags & WINLINK_ALERTFLAGS)
1.1 nicm 305: break;
1.14 nicm 306: wl = winlink_next(wl);
1.1 nicm 307: }
308: return (wl);
309: }
310:
311: /* Move session to next window. */
312: int
1.17 nicm 313: session_next(struct session *s, int alert)
1.1 nicm 314: {
315: struct winlink *wl;
316:
317: if (s->curw == NULL)
318: return (-1);
319:
1.14 nicm 320: wl = winlink_next(s->curw);
1.17 nicm 321: if (alert)
1.18 nicm 322: wl = session_next_alert(wl);
1.1 nicm 323: if (wl == NULL) {
324: wl = RB_MIN(winlinks, &s->windows);
1.18 nicm 325: if (alert && ((wl = session_next_alert(wl)) == NULL))
1.1 nicm 326: return (-1);
327: }
328: if (wl == s->curw)
329: return (1);
330: winlink_stack_remove(&s->lastw, wl);
331: winlink_stack_push(&s->lastw, s->curw);
332: s->curw = wl;
1.18 nicm 333: wl->flags &= ~WINLINK_ALERTFLAGS;
1.1 nicm 334: return (0);
335: }
336:
337: struct winlink *
1.18 nicm 338: session_previous_alert(struct winlink *wl)
1.1 nicm 339: {
340: while (wl != NULL) {
1.18 nicm 341: if (wl->flags & WINLINK_ALERTFLAGS)
1.1 nicm 342: break;
1.14 nicm 343: wl = winlink_previous(wl);
1.1 nicm 344: }
345: return (wl);
346: }
347:
348: /* Move session to previous window. */
349: int
1.17 nicm 350: session_previous(struct session *s, int alert)
1.1 nicm 351: {
352: struct winlink *wl;
353:
354: if (s->curw == NULL)
355: return (-1);
356:
1.14 nicm 357: wl = winlink_previous(s->curw);
1.17 nicm 358: if (alert)
1.18 nicm 359: wl = session_previous_alert(wl);
1.1 nicm 360: if (wl == NULL) {
361: wl = RB_MAX(winlinks, &s->windows);
1.18 nicm 362: if (alert && (wl = session_previous_alert(wl)) == NULL)
1.1 nicm 363: return (-1);
364: }
365: if (wl == s->curw)
366: return (1);
367: winlink_stack_remove(&s->lastw, wl);
368: winlink_stack_push(&s->lastw, s->curw);
369: s->curw = wl;
1.18 nicm 370: wl->flags &= ~WINLINK_ALERTFLAGS;
1.1 nicm 371: return (0);
372: }
373:
374: /* Move session to specific window. */
375: int
376: session_select(struct session *s, int idx)
377: {
378: struct winlink *wl;
379:
380: wl = winlink_find_by_index(&s->windows, idx);
381: if (wl == NULL)
382: return (-1);
383: if (wl == s->curw)
384: return (1);
385: winlink_stack_remove(&s->lastw, wl);
386: winlink_stack_push(&s->lastw, s->curw);
387: s->curw = wl;
1.18 nicm 388: wl->flags &= ~WINLINK_ALERTFLAGS;
1.1 nicm 389: return (0);
390: }
391:
392: /* Move session to last used window. */
393: int
394: session_last(struct session *s)
395: {
396: struct winlink *wl;
397:
1.11 nicm 398: wl = TAILQ_FIRST(&s->lastw);
1.1 nicm 399: if (wl == NULL)
400: return (-1);
401: if (wl == s->curw)
402: return (1);
403:
404: winlink_stack_remove(&s->lastw, wl);
405: winlink_stack_push(&s->lastw, s->curw);
406: s->curw = wl;
1.18 nicm 407: wl->flags &= ~WINLINK_ALERTFLAGS;
1.1 nicm 408: return (0);
1.11 nicm 409: }
410:
411: /* Find the session group containing a session. */
412: struct session_group *
413: session_group_find(struct session *target)
414: {
415: struct session_group *sg;
416: struct session *s;
417:
418: TAILQ_FOREACH(sg, &session_groups, entry) {
419: TAILQ_FOREACH(s, &sg->sessions, gentry) {
420: if (s == target)
421: return (sg);
422: }
423: }
424: return (NULL);
425: }
426:
427: /* Find session group index. */
428: u_int
429: session_group_index(struct session_group *sg)
430: {
431: struct session_group *sg2;
432: u_int i;
433:
434: i = 0;
435: TAILQ_FOREACH(sg2, &session_groups, entry) {
436: if (sg == sg2)
437: return (i);
438: i++;
439: }
440:
441: fatalx("session group not found");
442: }
443:
444: /*
445: * Add a session to the session group containing target, creating it if
1.15 nicm 446: * necessary.
1.11 nicm 447: */
448: void
449: session_group_add(struct session *target, struct session *s)
450: {
451: struct session_group *sg;
452:
453: if ((sg = session_group_find(target)) == NULL) {
454: sg = xmalloc(sizeof *sg);
455: TAILQ_INSERT_TAIL(&session_groups, sg, entry);
456: TAILQ_INIT(&sg->sessions);
457: TAILQ_INSERT_TAIL(&sg->sessions, target, gentry);
458: }
459: TAILQ_INSERT_TAIL(&sg->sessions, s, gentry);
460: }
461:
462: /* Remove a session from its group and destroy the group if empty. */
463: void
464: session_group_remove(struct session *s)
465: {
466: struct session_group *sg;
467:
468: if ((sg = session_group_find(s)) == NULL)
469: return;
470: TAILQ_REMOVE(&sg->sessions, s, gentry);
471: if (TAILQ_NEXT(TAILQ_FIRST(&sg->sessions), gentry) == NULL)
472: TAILQ_REMOVE(&sg->sessions, TAILQ_FIRST(&sg->sessions), gentry);
473: if (TAILQ_EMPTY(&sg->sessions)) {
474: TAILQ_REMOVE(&session_groups, sg, entry);
475: xfree(sg);
476: }
477: }
478:
479: /* Synchronize a session to its session group. */
480: void
481: session_group_synchronize_to(struct session *s)
482: {
483: struct session_group *sg;
484: struct session *target;
485:
486: if ((sg = session_group_find(s)) == NULL)
487: return;
488:
489: target = NULL;
490: TAILQ_FOREACH(target, &sg->sessions, gentry) {
491: if (target != s)
492: break;
493: }
494: session_group_synchronize1(target, s);
495: }
496:
497: /* Synchronize a session group to a session. */
498: void
499: session_group_synchronize_from(struct session *target)
500: {
501: struct session_group *sg;
502: struct session *s;
503:
504: if ((sg = session_group_find(target)) == NULL)
505: return;
506:
507: TAILQ_FOREACH(s, &sg->sessions, gentry) {
508: if (s != target)
509: session_group_synchronize1(target, s);
510: }
511: }
512:
513: /*
514: * Synchronize a session with a target session. This means destroying all
515: * winlinks then recreating them, then updating the current window, last window
516: * stack and alerts.
517: */
518: void
519: session_group_synchronize1(struct session *target, struct session *s)
520: {
521: struct winlinks old_windows, *ww;
522: struct winlink_stack old_lastw;
523: struct winlink *wl, *wl2;
524:
525: /* Don't do anything if the session is empty (it'll be destroyed). */
526: ww = &target->windows;
527: if (RB_EMPTY(ww))
528: return;
529:
530: /* If the current window has vanished, move to the next now. */
1.16 nicm 531: if (s->curw != NULL &&
532: winlink_find_by_index(ww, s->curw->idx) == NULL &&
533: session_last(s) != 0 && session_previous(s, 0) != 0)
534: session_next(s, 0);
1.11 nicm 535:
536: /* Save the old pointer and reset it. */
537: memcpy(&old_windows, &s->windows, sizeof old_windows);
538: RB_INIT(&s->windows);
539:
540: /* Link all the windows from the target. */
1.18 nicm 541: RB_FOREACH(wl, winlinks, ww) {
1.30 ! nicm 542: wl2 = winlink_add(&s->windows, wl->idx);
! 543: winlink_set_window(wl2, wl->window);
1.18 nicm 544: wl2->flags |= wl->flags & WINLINK_ALERTFLAGS;
545: }
1.11 nicm 546:
547: /* Fix up the current window. */
548: if (s->curw != NULL)
549: s->curw = winlink_find_by_index(&s->windows, s->curw->idx);
550: else
551: s->curw = winlink_find_by_index(&s->windows, target->curw->idx);
552:
553: /* Fix up the last window stack. */
554: memcpy(&old_lastw, &s->lastw, sizeof old_lastw);
555: TAILQ_INIT(&s->lastw);
556: TAILQ_FOREACH(wl, &old_lastw, sentry) {
557: wl2 = winlink_find_by_index(&s->windows, wl->idx);
558: if (wl2 != NULL)
559: TAILQ_INSERT_TAIL(&s->lastw, wl2, sentry);
560: }
561:
562: /* Then free the old winlinks list. */
563: while (!RB_EMPTY(&old_windows)) {
564: wl = RB_ROOT(&old_windows);
1.13 nicm 565: winlink_remove(&old_windows, wl);
1.11 nicm 566: }
1.1 nicm 567: }