From b7d031cc92a1d9ac3ca8c2a71b76b0a78ea77465 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Sat, 10 Oct 2009 09:46:11 +0000 Subject: [PATCH] Support for individual session idle time locking. May be enabled by turning off the lock-server option (it is on by default). When this is off, each session locks when it has been idle for the lock-after-time setting. When on, the entire server locks when ALL sessions have been idle for their individual lock-after-time settings. This replaces one global-only option (lock-after-time) with another (lock-server), but the default behaviour is usually preferable so there don't seem to be many alternatives. Diff/idea largely from Thomas Adam, tweaked by me. --- cmd-set-option.c | 1 + server-msg.c | 6 +++-- server.c | 66 ++++++++++++++++++++++++++++++++++++++---------- session.c | 2 ++ tmux.1 | 25 +++++++++++++----- tmux.c | 3 +-- tmux.h | 2 +- 7 files changed, 81 insertions(+), 24 deletions(-) diff --git a/cmd-set-option.c b/cmd-set-option.c index 4984cb1a..5fbb9e85 100644 --- a/cmd-set-option.c +++ b/cmd-set-option.c @@ -63,6 +63,7 @@ const struct set_option_entry set_option_table[] = { { "history-limit", SET_OPTION_NUMBER, 0, INT_MAX, NULL }, { "lock-after-time", SET_OPTION_NUMBER, 0, INT_MAX, NULL }, { "lock-command", SET_OPTION_STRING, 0, 0, NULL }, + { "lock-server", SET_OPTION_FLAG, 0, 0, NULL }, { "message-attr", SET_OPTION_ATTRIBUTES, 0, 0, NULL }, { "message-bg", SET_OPTION_COLOUR, 0, 0, NULL }, { "message-fg", SET_OPTION_COLOUR, 0, 0, NULL }, diff --git a/server-msg.c b/server-msg.c index ec085610..248b94d4 100644 --- a/server-msg.c +++ b/server-msg.c @@ -105,7 +105,8 @@ server_msg_dispatch(struct client *c) tty_start_tty(&c->tty); server_redraw_client(c); recalculate_sizes(); - server_activity = time(NULL); + if (c->session != NULL) + c->session->activity = time(NULL); break; case MSG_ENVIRON: if (datalen != sizeof environdata) @@ -181,7 +182,8 @@ server_msg_command(struct client *c, struct msg_command_data *data) int argc; char **argv, *cause; - server_activity = time(NULL); + if (c->session != NULL) + c->session->activity = time(NULL); ctx.error = server_msg_command_error; ctx.print = server_msg_command_print; diff --git a/server.c b/server.c index a1a0211f..9c37b7c1 100644 --- a/server.c +++ b/server.c @@ -86,6 +86,8 @@ void server_check_window(struct window *); void server_check_redraw(struct client *); void server_set_title(struct client *); void server_check_timers(struct client *); +void server_lock_server(void); +void server_lock_sessions(void); void server_second_timers(void); int server_update_socket(void); @@ -249,8 +251,6 @@ server_start(char *path) key_bindings_init(); utf8_build(); - server_activity = time(NULL); - start_time = time(NULL); socket_path = path; @@ -842,10 +842,10 @@ server_handle_client(struct client *c) /* Process keys. */ keylist = options_get_data(&c->session->options, "prefix"); while (tty_keys_next(&c->tty, &key, mouse) == 0) { - server_activity = time(NULL); - if (c->session == NULL) return; + + c->session->activity = time(NULL); w = c->session->curw->window; wp = w->active; /* could die */ @@ -1254,6 +1254,51 @@ server_check_window(struct window *w) recalculate_sizes(); } +/* Lock the server if ALL sessions have hit the time limit. */ +void +server_lock_server(void) +{ + struct session *s; + u_int i; + int timeout; + time_t t; + + t = time(NULL); + for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { + if ((s = ARRAY_ITEM(&sessions, i)) == NULL) + continue; + + timeout = options_get_number(&s->options, "lock-after-time"); + if (timeout <= 0 || t <= s->activity + timeout) + return; /* not timed out */ + } + + server_lock(); + recalculate_sizes(); +} + +/* Lock any sessions which have timed out. */ +void +server_lock_sessions(void) +{ + struct session *s; + u_int i; + int timeout; + time_t t; + + t = time(NULL); + for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { + if ((s = ARRAY_ITEM(&sessions, i)) == NULL) + continue; + + timeout = options_get_number(&s->options, "lock-after-time"); + if (timeout > 0 && t > s->activity + timeout) { + server_lock_session(s); + recalculate_sizes(); + } + } +} + /* Call any once-per-second timers. */ void server_second_timers(void) @@ -1261,16 +1306,11 @@ server_second_timers(void) struct window *w; struct window_pane *wp; u_int i; - int xtimeout; - time_t t; - t = time(NULL); - - xtimeout = options_get_number(&global_s_options, "lock-after-time"); - if (xtimeout > 0 && t > server_activity + xtimeout) { - server_lock(); - recalculate_sizes(); - } + if (options_get_number(&global_s_options, "lock-server")) + server_lock_server(); + else + server_lock_sessions(); for (i = 0; i < ARRAY_LENGTH(&windows); i++) { w = ARRAY_ITEM(&windows, i); diff --git a/session.c b/session.c index fb49a365..62c5b746 100644 --- a/session.c +++ b/session.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "tmux.h" @@ -124,6 +125,7 @@ session_create(const char *name, const char *cmd, const char *cwd, s = xmalloc(sizeof *s); s->references = 0; s->flags = 0; + s->activity = time(NULL); if (gettimeofday(&s->tv, NULL) != 0) fatal("gettimeofday failed"); diff --git a/tmux.1 b/tmux.1 index 397ff83b..750e0134 100644 --- a/tmux.1 +++ b/tmux.1 @@ -1253,20 +1253,33 @@ Set the maximum number of lines held in window history. This setting applies only to new windows - existing window histories are not resized and retain the limit at the point they were created. .It Ic lock-after-time Ar number -Lock the server (like the -.Ic lock-server +Lock the session (like the +.Ic lock-session command) after .Ar number -seconds of inactivity. -The default is off (set to 0). -This has no effect as a session option; it must be set as a global option using -.Fl g . +seconds of inactivity, or the entire server (all sessions) if the +.Ic lock-server +option is set. +The default is not to lock (set to 0). .It Ic lock-command Ar command Command to run when locking each client. The default is to run .Xr lock 1 with .Fl np . +.It Xo Ic lock-server +.Op Ic on | off +.Xc +If this option is +.Ic on +(the default), +instead of each session locking individually as each has been +idle for +.Ic lock-after-time +, the entire server will lock after +.Em all +sessions would have locked. +This has no effect as a session option; it must be set as a global option. .It Ic message-attr Ar attributes Set status line message attributes, where .Ar attributes diff --git a/tmux.c b/tmux.c index eed7e9e1..cfbf8aac 100644 --- a/tmux.c +++ b/tmux.c @@ -46,8 +46,6 @@ struct options global_s_options; /* session options */ struct options global_w_options; /* window options */ struct environ global_environ; -time_t server_activity; - int debug_level; int be_quiet; time_t start_time; @@ -379,6 +377,7 @@ main(int argc, char **argv) options_set_number(so, "history-limit", 2000); options_set_number(so, "lock-after-time", 0); options_set_string(so, "lock-command", "lock -np"); + options_set_number(so, "lock-server", 1); options_set_number(so, "message-attr", 0); options_set_number(so, "message-bg", 3); options_set_number(so, "message-fg", 0); diff --git a/tmux.h b/tmux.h index 100793ed..9fd864f5 100644 --- a/tmux.h +++ b/tmux.h @@ -800,6 +800,7 @@ struct session_alert { struct session { char *name; struct timeval tv; + time_t activity; u_int sx; u_int sy; @@ -1117,7 +1118,6 @@ extern struct options global_s_options; extern struct options global_w_options; extern struct environ global_environ; extern char *cfg_file; -extern time_t server_activity; extern int debug_level; extern int be_quiet; extern time_t start_time;