Sync OpenBSD patchset 320:

Reference count clients and sessions rather than relying on a saved index for
cmd-choose-*.
This commit is contained in:
Tiago Cunha 2009-09-07 23:59:19 +00:00
parent 1de812d5a8
commit 930f67f474
7 changed files with 113 additions and 46 deletions

View File

@ -1,4 +1,4 @@
/* $Id: cmd-choose-client.c,v 1.2 2009-08-25 13:55:29 tcunha Exp $ */ /* $Id: cmd-choose-client.c,v 1.3 2009-09-07 23:59:19 tcunha Exp $ */
/* /*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -43,7 +43,7 @@ const struct cmd_entry cmd_choose_client_entry = {
}; };
struct cmd_choose_client_data { struct cmd_choose_client_data {
u_int client; struct client *client;
char *template; char *template;
}; };
@ -87,7 +87,8 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
cdata->template = xstrdup(data->arg); cdata->template = xstrdup(data->arg);
else else
cdata->template = xstrdup("detach-client -t '%%'"); cdata->template = xstrdup("detach-client -t '%%'");
cdata->client = server_client_index(ctx->curclient); cdata->client = ctx->curclient;
cdata->client->references++;
window_choose_ready(wl->window->active, window_choose_ready(wl->window->active,
cur, cmd_choose_client_callback, cmd_choose_client_free, cdata); cur, cmd_choose_client_callback, cmd_choose_client_free, cdata);
@ -99,23 +100,22 @@ void
cmd_choose_client_callback(void *data, int idx) cmd_choose_client_callback(void *data, int idx)
{ {
struct cmd_choose_client_data *cdata = data; struct cmd_choose_client_data *cdata = data;
struct client *c, *c2; struct client *c;
struct cmd_list *cmdlist; struct cmd_list *cmdlist;
struct cmd_ctx ctx; struct cmd_ctx ctx;
char *template, *cause; char *template, *cause;
if (idx == -1) if (idx == -1)
return; return;
if (cdata->client > ARRAY_LENGTH(&clients) - 1) if (cdata->client->flags & CLIENT_DEAD)
return; return;
c = ARRAY_ITEM(&clients, cdata->client);
if ((u_int) idx > ARRAY_LENGTH(&clients) - 1) if ((u_int) idx > ARRAY_LENGTH(&clients) - 1)
return; return;
c2 = ARRAY_ITEM(&clients, idx); c = ARRAY_ITEM(&clients, idx);
if (c2 == NULL || c2->session == NULL) if (c == NULL || c->session == NULL)
return; return;
template = cmd_template_replace(cdata->template, c2->tty.path, 1); template = cmd_template_replace(cdata->template, c->tty.path, 1);
if (cmd_string_parse(template, &cmdlist, &cause) != 0) { if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
if (cause != NULL) { if (cause != NULL) {
@ -129,7 +129,7 @@ cmd_choose_client_callback(void *data, int idx)
xfree(template); xfree(template);
ctx.msgdata = NULL; ctx.msgdata = NULL;
ctx.curclient = c; ctx.curclient = cdata->client;
ctx.error = key_bindings_error; ctx.error = key_bindings_error;
ctx.print = key_bindings_print; ctx.print = key_bindings_print;
@ -146,6 +146,7 @@ cmd_choose_client_free(void *data)
{ {
struct cmd_choose_client_data *cdata = data; struct cmd_choose_client_data *cdata = data;
cdata->client->references--;
xfree(cdata->template); xfree(cdata->template);
xfree(cdata); xfree(cdata);
} }

View File

@ -1,4 +1,4 @@
/* $Id: cmd-choose-session.c,v 1.12 2009-08-25 13:55:29 tcunha Exp $ */ /* $Id: cmd-choose-session.c,v 1.13 2009-09-07 23:59:19 tcunha Exp $ */
/* /*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -43,7 +43,7 @@ const struct cmd_entry cmd_choose_session_entry = {
}; };
struct cmd_choose_session_data { struct cmd_choose_session_data {
u_int client; struct client *client;
char *template; char *template;
}; };
@ -87,7 +87,8 @@ cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx)
cdata->template = xstrdup(data->arg); cdata->template = xstrdup(data->arg);
else else
cdata->template = xstrdup("switch-client -t '%%'"); cdata->template = xstrdup("switch-client -t '%%'");
cdata->client = server_client_index(ctx->curclient); cdata->client = ctx->curclient;
cdata->client->references++;
window_choose_ready(wl->window->active, window_choose_ready(wl->window->active,
cur, cmd_choose_session_callback, cmd_choose_session_free, cdata); cur, cmd_choose_session_callback, cmd_choose_session_free, cdata);
@ -99,7 +100,6 @@ void
cmd_choose_session_callback(void *data, int idx) cmd_choose_session_callback(void *data, int idx)
{ {
struct cmd_choose_session_data *cdata = data; struct cmd_choose_session_data *cdata = data;
struct client *c;
struct session *s; struct session *s;
struct cmd_list *cmdlist; struct cmd_list *cmdlist;
struct cmd_ctx ctx; struct cmd_ctx ctx;
@ -107,9 +107,8 @@ cmd_choose_session_callback(void *data, int idx)
if (idx == -1) if (idx == -1)
return; return;
if (cdata->client > ARRAY_LENGTH(&clients) - 1) if (cdata->client->flags & CLIENT_DEAD)
return; return;
c = ARRAY_ITEM(&clients, cdata->client);
if ((u_int) idx > ARRAY_LENGTH(&sessions) - 1) if ((u_int) idx > ARRAY_LENGTH(&sessions) - 1)
return; return;
@ -121,7 +120,7 @@ cmd_choose_session_callback(void *data, int idx)
if (cmd_string_parse(template, &cmdlist, &cause) != 0) { if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
if (cause != NULL) { if (cause != NULL) {
*cause = toupper((u_char) *cause); *cause = toupper((u_char) *cause);
status_message_set(c, "%s", cause); status_message_set(cdata->client, "%s", cause);
xfree(cause); xfree(cause);
} }
xfree(template); xfree(template);
@ -130,7 +129,7 @@ cmd_choose_session_callback(void *data, int idx)
xfree(template); xfree(template);
ctx.msgdata = NULL; ctx.msgdata = NULL;
ctx.curclient = c; ctx.curclient = cdata->client;
ctx.error = key_bindings_error; ctx.error = key_bindings_error;
ctx.print = key_bindings_print; ctx.print = key_bindings_print;
@ -147,6 +146,7 @@ cmd_choose_session_free(void *data)
{ {
struct cmd_choose_session_data *cdata = data; struct cmd_choose_session_data *cdata = data;
cdata->client->references--;
xfree(cdata->template); xfree(cdata->template);
xfree(cdata); xfree(cdata);
} }

View File

@ -1,4 +1,4 @@
/* $Id: cmd-choose-window.c,v 1.16 2009-08-25 13:55:29 tcunha Exp $ */ /* $Id: cmd-choose-window.c,v 1.17 2009-09-07 23:59:19 tcunha Exp $ */
/* /*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -43,8 +43,8 @@ const struct cmd_entry cmd_choose_window_entry = {
}; };
struct cmd_choose_window_data { struct cmd_choose_window_data {
u_int client; struct client *client;
u_int session; struct session *session;
char *template; char *template;
}; };
@ -107,13 +107,14 @@ cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
} }
cdata = xmalloc(sizeof *cdata); cdata = xmalloc(sizeof *cdata);
if (session_index(s, &cdata->session) != 0)
fatalx("session not found");
if (data->arg != NULL) if (data->arg != NULL)
cdata->template = xstrdup(data->arg); cdata->template = xstrdup(data->arg);
else else
cdata->template = xstrdup("select-window -t '%%'"); cdata->template = xstrdup("select-window -t '%%'");
cdata->client = server_client_index(ctx->curclient); cdata->session = s;
cdata->session->references++;
cdata->client = ctx->curclient;
cdata->client->references++;
window_choose_ready(wl->window->active, window_choose_ready(wl->window->active,
cur, cmd_choose_window_callback, cmd_choose_window_free, cdata); cur, cmd_choose_window_callback, cmd_choose_window_free, cdata);
@ -125,31 +126,27 @@ void
cmd_choose_window_callback(void *data, int idx) cmd_choose_window_callback(void *data, int idx)
{ {
struct cmd_choose_window_data *cdata = data; struct cmd_choose_window_data *cdata = data;
struct client *c;
struct session *s;
struct cmd_list *cmdlist; struct cmd_list *cmdlist;
struct cmd_ctx ctx; struct cmd_ctx ctx;
char *target, *template, *cause; char *target, *template, *cause;
if (idx == -1) if (idx == -1)
return; return;
if (cdata->client > ARRAY_LENGTH(&clients) - 1) if (cdata->client->flags & CLIENT_DEAD)
return;
if (cdata->session->flags & SESSION_DEAD)
return; return;
c = ARRAY_ITEM(&clients, cdata->client); if (cdata->client->session != cdata->session)
if (cdata->session > ARRAY_LENGTH(&sessions) - 1)
return;
s = ARRAY_ITEM(&sessions, cdata->session);
if (c->session != s)
return; return;
xasprintf(&target, "%s:%d", s->name, idx); xasprintf(&target, "%s:%d", cdata->session->name, idx);
template = cmd_template_replace(cdata->template, target, 1); template = cmd_template_replace(cdata->template, target, 1);
xfree(target); xfree(target);
if (cmd_string_parse(template, &cmdlist, &cause) != 0) { if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
if (cause != NULL) { if (cause != NULL) {
*cause = toupper((u_char) *cause); *cause = toupper((u_char) *cause);
status_message_set(c, "%s", cause); status_message_set(cdata->client, "%s", cause);
xfree(cause); xfree(cause);
} }
xfree(template); xfree(template);
@ -158,7 +155,7 @@ cmd_choose_window_callback(void *data, int idx)
xfree(template); xfree(template);
ctx.msgdata = NULL; ctx.msgdata = NULL;
ctx.curclient = c; ctx.curclient = cdata->client;
ctx.error = key_bindings_error; ctx.error = key_bindings_error;
ctx.print = key_bindings_print; ctx.print = key_bindings_print;
@ -175,6 +172,8 @@ cmd_choose_window_free(void *data)
{ {
struct cmd_choose_window_data *cdata = data; struct cmd_choose_window_data *cdata = data;
cdata->session->references--;
cdata->client->references--;
xfree(cdata->template); xfree(cdata->template);
xfree(cdata); xfree(cdata);
} }

View File

@ -1,4 +1,4 @@
/* $Id: cmd-server-info.c,v 1.27 2009-09-03 21:02:55 tcunha Exp $ */ /* $Id: cmd-server-info.c,v 1.28 2009-09-07 23:59:19 tcunha Exp $ */
/* /*
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
@ -91,9 +91,10 @@ cmd_server_info_exec(unused struct cmd *self, struct cmd_ctx *ctx)
continue; continue;
ctx->print(ctx, "%2d: %s (%d, %d): %s [%ux%u %s] " ctx->print(ctx, "%2d: %s (%d, %d): %s [%ux%u %s] "
"[flags=0x%x/0x%x]", i, c->tty.path, c->ibuf.fd, c->tty.fd, "[flags=0x%x/0x%x, references=%u]", i, c->tty.path,
c->session->name, c->tty.sx, c->tty.sy, c->tty.termname, c->ibuf.fd, c->tty.fd, c->session->name,
c->flags, c->tty.flags); c->tty.sx, c->tty.sy, c->tty.termname, c->flags,
c->tty.flags, c->references);
} }
ctx->print(ctx, "%s", ""); ctx->print(ctx, "%s", "");
@ -109,8 +110,9 @@ cmd_server_info_exec(unused struct cmd *self, struct cmd_ctx *ctx)
*strchr(tim, '\n') = '\0'; *strchr(tim, '\n') = '\0';
ctx->print(ctx, "%2u: %s: %u windows (created %s) [%ux%u] " ctx->print(ctx, "%2u: %s: %u windows (created %s) [%ux%u] "
"[flags=0x%x]", i, s->name, winlink_count(&s->windows), "[flags=0x%x, references=%u]", i, s->name,
tim, s->sx, s->sy, s->flags); winlink_count(&s->windows), tim, s->sx, s->sy, s->flags,
s->references);
RB_FOREACH(wl, winlinks, &s->windows) { RB_FOREACH(wl, winlinks, &s->windows) {
w = wl->window; w = wl->window;
ctx->print(ctx, "%4u: %s [%ux%u] [flags=0x%x, " ctx->print(ctx, "%4u: %s [%ux%u] [flags=0x%x, "

View File

@ -1,4 +1,4 @@
/* $Id: server.c,v 1.181 2009-09-07 23:50:07 tcunha Exp $ */ /* $Id: server.c,v 1.182 2009-09-07 23:59:19 tcunha Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -42,6 +42,7 @@
/* Client list. */ /* Client list. */
struct clients clients; struct clients clients;
struct clients dead_clients;
void server_create_client(int); void server_create_client(int);
int server_create_socket(void); int server_create_socket(void);
@ -60,6 +61,7 @@ int server_check_window_activity(struct session *,
struct window *); struct window *);
int server_check_window_content(struct session *, struct window *, int server_check_window_content(struct session *, struct window *,
struct window_pane *); struct window_pane *);
void server_clean_dead(void);
void server_lost_client(struct client *); void server_lost_client(struct client *);
void server_check_window(struct window *); void server_check_window(struct window *);
void server_check_redraw(struct client *); void server_check_redraw(struct client *);
@ -84,6 +86,7 @@ server_create_client(int fd)
fatal("fcntl failed"); fatal("fcntl failed");
c = xcalloc(1, sizeof *c); c = xcalloc(1, sizeof *c);
c->references = 0;
imsg_init(&c->ibuf, fd); imsg_init(&c->ibuf, fd);
ARRAY_INIT(&c->prompt_hdata); ARRAY_INIT(&c->prompt_hdata);
@ -163,7 +166,9 @@ server_start(char *path)
ARRAY_INIT(&windows); ARRAY_INIT(&windows);
ARRAY_INIT(&clients); ARRAY_INIT(&clients);
ARRAY_INIT(&dead_clients);
ARRAY_INIT(&sessions); ARRAY_INIT(&sessions);
ARRAY_INIT(&dead_sessions);
mode_key_init_trees(); mode_key_init_trees();
key_bindings_init(); key_bindings_init();
utf8_build(); utf8_build();
@ -347,6 +352,9 @@ server_main(int srv_fd)
/* Collect any unset key bindings. */ /* Collect any unset key bindings. */
key_bindings_clean(); key_bindings_clean();
/* Collect dead clients and sessions. */
server_clean_dead();
/* /*
* If we have no sessions and clients left, let's get out * If we have no sessions and clients left, let's get out
@ -951,11 +959,45 @@ server_lost_client(struct client *c)
close(c->ibuf.fd); close(c->ibuf.fd);
imsg_clear(&c->ibuf); imsg_clear(&c->ibuf);
xfree(c);
for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
if (ARRAY_ITEM(&dead_clients, i) == NULL) {
ARRAY_SET(&dead_clients, i, c);
break;
}
}
if (i == ARRAY_LENGTH(&dead_clients))
ARRAY_ADD(&dead_clients, c);
c->flags |= CLIENT_DEAD;
recalculate_sizes(); recalculate_sizes();
} }
/* Free dead, unreferenced clients and sessions. */
void
server_clean_dead(void)
{
struct session *s;
struct client *c;
u_int i;
for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
s = ARRAY_ITEM(&dead_sessions, i);
if (s == NULL || s->references != 0)
continue;
ARRAY_SET(&dead_sessions, i, NULL);
xfree(s);
}
for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
c = ARRAY_ITEM(&dead_clients, i);
if (c == NULL || c->references != 0)
continue;
ARRAY_SET(&dead_clients, i, NULL);
xfree(c);
}
}
/* Handle window data. */ /* Handle window data. */
void void
server_handle_window(struct window *w, struct window_pane *wp) server_handle_window(struct window *w, struct window_pane *wp)

View File

@ -1,4 +1,4 @@
/* $Id: session.c,v 1.64 2009-09-02 01:05:55 tcunha Exp $ */ /* $Id: session.c,v 1.65 2009-09-07 23:59:19 tcunha Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -27,6 +27,7 @@
/* Global session list. */ /* Global session list. */
struct sessions sessions; struct sessions sessions;
struct sessions dead_sessions;
struct winlink *session_next_activity(struct session *, struct winlink *); struct winlink *session_next_activity(struct session *, struct winlink *);
struct winlink *session_previous_activity(struct session *, struct winlink *); struct winlink *session_previous_activity(struct session *, struct winlink *);
@ -120,14 +121,19 @@ session_create(const char *name, const char *cmd, const char *cwd,
u_int i; u_int i;
s = xmalloc(sizeof *s); s = xmalloc(sizeof *s);
s->references = 0;
s->flags = 0; s->flags = 0;
if (gettimeofday(&s->tv, NULL) != 0) if (gettimeofday(&s->tv, NULL) != 0)
fatal("gettimeofday"); fatal("gettimeofday");
s->curw = NULL; s->curw = NULL;
SLIST_INIT(&s->lastw); SLIST_INIT(&s->lastw);
RB_INIT(&s->windows); RB_INIT(&s->windows);
SLIST_INIT(&s->alerts); SLIST_INIT(&s->alerts);
paste_init_stack(&s->buffers); paste_init_stack(&s->buffers);
options_init(&s->options, &global_s_options); options_init(&s->options, &global_s_options);
environ_init(&s->environ); environ_init(&s->environ);
if (env != NULL) if (env != NULL)
@ -186,7 +192,16 @@ session_destroy(struct session *s)
winlink_remove(&s->windows, RB_ROOT(&s->windows)); winlink_remove(&s->windows, RB_ROOT(&s->windows));
xfree(s->name); xfree(s->name);
xfree(s);
for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
if (ARRAY_ITEM(&dead_sessions, i) == NULL) {
ARRAY_SET(&dead_sessions, i, s);
break;
}
}
if (i == ARRAY_LENGTH(&dead_sessions))
ARRAY_ADD(&dead_sessions, s);
s->flags |= SESSION_DEAD;
} }
/* Find session index. */ /* Find session index. */

10
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.438 2009-09-07 23:48:54 tcunha Exp $ */ /* $Id: tmux.h,v 1.439 2009-09-07 23:59:19 tcunha Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -810,11 +810,14 @@ struct session {
SLIST_HEAD(, session_alert) alerts; SLIST_HEAD(, session_alert) alerts;
#define SESSION_UNATTACHED 0x1 /* not attached to any clients */ #define SESSION_UNATTACHED 0x1 /* not attached to any clients */
#define SESSION_DEAD 0x2
int flags; int flags;
struct termios tio; struct termios tio;
struct environ environ; struct environ environ;
int references;
}; };
ARRAY_DECL(sessions, struct session *); ARRAY_DECL(sessions, struct session *);
@ -937,6 +940,7 @@ struct client {
#define CLIENT_SUSPENDED 0x40 #define CLIENT_SUSPENDED 0x40
#define CLIENT_BAD 0x80 #define CLIENT_BAD 0x80
#define CLIENT_IDENTIFY 0x100 #define CLIENT_IDENTIFY 0x100
#define CLIENT_DEAD 0x200
int flags; int flags;
struct timeval identify_timer; struct timeval identify_timer;
@ -961,6 +965,8 @@ struct client {
struct mode_key_data prompt_mdata; struct mode_key_data prompt_mdata;
struct session *session; struct session *session;
int references;
}; };
ARRAY_DECL(clients, struct client *); ARRAY_DECL(clients, struct client *);
@ -1426,6 +1432,7 @@ const char *key_string_lookup_key(int);
/* server.c */ /* server.c */
extern struct clients clients; extern struct clients clients;
extern struct clients dead_clients;
int server_client_index(struct client *); int server_client_index(struct client *);
int server_start(char *); int server_start(char *);
@ -1700,6 +1707,7 @@ char *default_window_name(struct window *);
/* session.c */ /* session.c */
extern struct sessions sessions; extern struct sessions sessions;
extern struct sessions dead_sessions;
void session_alert_add(struct session *, struct window *, int); void session_alert_add(struct session *, struct window *, int);
void session_alert_cancel(struct session *, struct winlink *); void session_alert_cancel(struct session *, struct winlink *);
int session_alert_has(struct session *, struct winlink *, int); int session_alert_has(struct session *, struct winlink *, int);