diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2011-01-25 22:31:51 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2011-01-25 22:31:51 +0000 |
commit | 0604ff1925048a0139beaea00461164baec912aa (patch) | |
tree | 5378a66e5da3e61bb7ac429f125b69c13ea36dfa /usr.bin | |
parent | ab43325d2a999b4b2a5cdefc100cccb71dc29b8f (diff) |
Check if the index is in use and fail before creating the child process,
rather than leaving a stray child on failure.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/tmux/session.c | 22 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 5 | ||||
-rw-r--r-- | usr.bin/tmux/window.c | 26 |
3 files changed, 37 insertions, 16 deletions
diff --git a/usr.bin/tmux/session.c b/usr.bin/tmux/session.c index d9239217d86..04bd6996881 100644 --- a/usr.bin/tmux/session.c +++ b/usr.bin/tmux/session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: session.c,v 1.29 2011/01/13 02:08:14 nicm Exp $ */ +/* $OpenBSD: session.c,v 1.30 2011/01/25 22:31:50 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -212,10 +212,16 @@ session_new(struct session *s, const char *name, const char *cmd, const char *cwd, int idx, char **cause) { struct window *w; + struct winlink *wl; struct environ env; const char *shell; u_int hlimit; + if ((wl = winlink_add(&s->windows, idx)) == NULL) { + xasprintf(cause, "index in use: %d", idx); + return (NULL); + } + environ_init(&env); environ_copy(&global_environ, &env); environ_copy(&s->environ, &env); @@ -229,15 +235,18 @@ session_new(struct session *s, w = window_create( name, cmd, shell, cwd, &env, s->tio, s->sx, s->sy, hlimit, cause); if (w == NULL) { + winlink_remove(&s->windows, wl); environ_free(&env); return (NULL); } + winlink_set_window(wl, w); environ_free(&env); if (options_get_number(&s->options, "set-remain-on-exit")) options_set_number(&w->options, "remain-on-exit", 1); - return (session_attach(s, w, idx, cause)); + session_group_synchronize_from(s); + return (wl); } /* Attach a window to a session. */ @@ -246,8 +255,12 @@ session_attach(struct session *s, struct window *w, int idx, char **cause) { struct winlink *wl; - if ((wl = winlink_add(&s->windows, w, idx)) == NULL) + if ((wl = winlink_add(&s->windows, idx)) == NULL) { xasprintf(cause, "index in use: %d", idx); + return (NULL); + } + winlink_set_window(wl, w); + session_group_synchronize_from(s); return (wl); } @@ -526,7 +539,8 @@ session_group_synchronize1(struct session *target, struct session *s) /* Link all the windows from the target. */ RB_FOREACH(wl, winlinks, ww) { - wl2 = winlink_add(&s->windows, wl->window, wl->idx); + wl2 = winlink_add(&s->windows, wl->idx); + winlink_set_window(wl2, wl->window); wl2->flags |= wl->flags & WINLINK_ALERTFLAGS; } diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index db99b4b8763..7607d717f23 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.267 2011/01/23 11:03:43 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.268 2011/01/25 22:31:50 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -1828,7 +1828,8 @@ struct winlink *winlink_find_by_index(struct winlinks *, int); struct winlink *winlink_find_by_window(struct winlinks *, struct window *); int winlink_next_index(struct winlinks *, int); u_int winlink_count(struct winlinks *); -struct winlink *winlink_add(struct winlinks *, struct window *, int); +struct winlink *winlink_add(struct winlinks *, int); +void winlink_set_window(struct winlink *, struct window *); void winlink_remove(struct winlinks *, struct winlink *); struct winlink *winlink_next(struct winlink *); struct winlink *winlink_previous(struct winlink *); diff --git a/usr.bin/tmux/window.c b/usr.bin/tmux/window.c index ada1c52ef23..d330d9df431 100644 --- a/usr.bin/tmux/window.c +++ b/usr.bin/tmux/window.c @@ -1,4 +1,4 @@ -/* $OpenBSD: window.c,v 1.62 2011/01/08 01:52:37 nicm Exp $ */ +/* $OpenBSD: window.c,v 1.63 2011/01/25 22:31:50 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -123,7 +123,7 @@ winlink_count(struct winlinks *wwl) } struct winlink * -winlink_add(struct winlinks *wwl, struct window *w, int idx) +winlink_add(struct winlinks *wwl, int idx) { struct winlink *wl; @@ -135,15 +135,19 @@ winlink_add(struct winlinks *wwl, struct window *w, int idx) wl = xcalloc(1, sizeof *wl); wl->idx = idx; - wl->window = w; RB_INSERT(winlinks, wwl, wl); - w->references++; - return (wl); } void +winlink_set_window(struct winlink *wl, struct window *w) +{ + wl->window = w; + w->references++; +} + +void winlink_remove(struct winlinks *wwl, struct winlink *wl) { struct window *w = wl->window; @@ -153,11 +157,13 @@ winlink_remove(struct winlinks *wwl, struct winlink *wl) xfree(wl->status_text); xfree(wl); - if (w->references == 0) - fatal("bad reference count"); - w->references--; - if (w->references == 0) - window_destroy(w); + if (w != NULL) { + if (w->references == 0) + fatal("bad reference count"); + w->references--; + if (w->references == 0) + window_destroy(w); + } } struct winlink * |