Skip to content

Commit

Permalink
Sync OpenBSD patchset 801:
Browse files Browse the repository at this point in the history
Unify the way sessions are used by callbacks - store the address and use
the reference count, then check it is still on the global sessions list
in the callback.
  • Loading branch information
Tiago Cunha committed Dec 22, 2010
1 parent b8eae39 commit 64d16cf
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
11 changes: 5 additions & 6 deletions cmd-choose-window.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $Id: cmd-choose-window.c,v 1.23 2010-12-06 22:52:20 nicm Exp $ */
/* $Id: cmd-choose-window.c,v 1.24 2010-12-22 15:28:50 tcunha Exp $ */

/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
Expand Down Expand Up @@ -129,20 +129,19 @@ void
cmd_choose_window_callback(void *data, int idx)
{
struct cmd_choose_window_data *cdata = data;
struct session *s = cdata->session;
struct cmd_list *cmdlist;
struct cmd_ctx ctx;
char *target, *template, *cause;

if (idx == -1)
return;
if (cdata->client->flags & CLIENT_DEAD)
return;
if (cdata->session->flags & SESSION_DEAD)
if (!session_alive(s))
return;
if (cdata->client->session != cdata->session)
if (cdata->client->flags & CLIENT_DEAD)
return;

xasprintf(&target, "%s:%d", cdata->session->name, idx);
xasprintf(&target, "%s:%d", s->name, idx);
template = cmd_template_replace(cdata->template, target, 1);
xfree(target);

Expand Down
35 changes: 24 additions & 11 deletions cmd-find-window.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $Id: cmd-find-window.c,v 1.14 2009-11-14 17:56:39 tcunha Exp $ */
/* $Id: cmd-find-window.c,v 1.15 2010-12-22 15:28:50 tcunha Exp $ */

/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
Expand Down Expand Up @@ -30,6 +30,7 @@
int cmd_find_window_exec(struct cmd *, struct cmd_ctx *);

void cmd_find_window_callback(void *, int);
void cmd_find_window_free(void *);

const struct cmd_entry cmd_find_window_entry = {
"find-window", "findw",
Expand All @@ -43,7 +44,7 @@ const struct cmd_entry cmd_find_window_entry = {
};

struct cmd_find_window_data {
u_int session;
struct session *session;
};

int
Expand Down Expand Up @@ -134,11 +135,11 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
}

cdata = xmalloc(sizeof *cdata);
if (session_index(s, &cdata->session) != 0)
fatalx("session not found");
cdata->session = s;
cdata->session->references++;

window_choose_ready(
wl->window->active, 0, cmd_find_window_callback, xfree, cdata);
window_choose_ready(wl->window->active,
0, cmd_find_window_callback, cmd_find_window_free, cdata);

out:
ARRAY_FREE(&list_idx);
Expand All @@ -151,12 +152,24 @@ void
cmd_find_window_callback(void *data, int idx)
{
struct cmd_find_window_data *cdata = data;
struct session *s;
struct session *s = cdata->session;

if (idx != -1 && cdata->session <= ARRAY_LENGTH(&sessions) - 1) {
s = ARRAY_ITEM(&sessions, cdata->session);
if (s != NULL && session_select(s, idx) == 0)
server_redraw_session(s);
if (idx == -1)
return;
if (!session_alive(s))
return;

if (session_select(s, idx) == 0) {
server_redraw_session(s);
recalculate_sizes();
}
}

void
cmd_find_window_free(void *data)
{
struct cmd_find_window_data *cdata = data;

cdata->session->references--;
xfree(cdata);
}
7 changes: 4 additions & 3 deletions cmd-load-buffer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $Id: cmd-load-buffer.c,v 1.17 2010-08-09 21:44:25 tcunha Exp $ */
/* $Id: cmd-load-buffer.c,v 1.18 2010-12-22 15:28:50 tcunha Exp $ */

/*
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
Expand Down Expand Up @@ -81,6 +81,7 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)

cdata = xmalloc(sizeof *cdata);
cdata->session = s;
cdata->session->references++;
cdata->buffer = data->buffer;
c->stdin_data = cdata;
c->stdin_callback = cmd_load_buffer_callback;
Expand Down Expand Up @@ -144,7 +145,6 @@ cmd_load_buffer_callback(struct client *c, void *data)
char *pdata;
size_t psize;
u_int limit;
int idx;

/*
* Event callback has already checked client is not dead and reduced
Expand All @@ -153,7 +153,7 @@ cmd_load_buffer_callback(struct client *c, void *data)
c->flags |= CLIENT_EXIT;

/* Does the target session still exist? */
if (session_index(s, &idx) != 0)
if (!session_alive(s))
goto out;

psize = EVBUFFER_LENGTH(c->stdin_event->input);
Expand All @@ -180,5 +180,6 @@ cmd_load_buffer_callback(struct client *c, void *data)
}

out:
cdata->session->references--;
xfree(cdata);
}
14 changes: 13 additions & 1 deletion session.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $Id: session.c,v 1.78 2010-09-10 13:36:17 tcunha Exp $ */
/* $Id: session.c,v 1.79 2010-12-22 15:28:50 tcunha Exp $ */

/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
Expand Down Expand Up @@ -34,6 +34,18 @@ struct session_groups session_groups;
struct winlink *session_next_alert(struct winlink *);
struct winlink *session_previous_alert(struct winlink *);

/*
* Find if session is still alive. This is true if it is still on the global
* sessions list.
*/
int
session_alive(struct session *s)
{
u_int idx;

return (session_index(s, &idx) == 0);
}

/* Find session by name. */
struct session *
session_find(const char *name)
Expand Down
3 changes: 2 additions & 1 deletion tmux.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.587 2010-12-11 18:42:20 nicm Exp $ */
/* $Id: tmux.h,v 1.588 2010-12-22 15:28:51 tcunha Exp $ */

/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
Expand Down Expand Up @@ -1966,6 +1966,7 @@ void clear_signals(int);
extern struct sessions sessions;
extern struct sessions dead_sessions;
extern struct session_groups session_groups;
int session_alive(struct session *);
struct session *session_find(const char *);
struct session *session_create(const char *, const char *, const char *,
struct environ *, struct termios *, int, u_int, u_int,
Expand Down

0 comments on commit 64d16cf

Please sign in to comment.