diff --git a/options-table.c b/options-table.c index dd9f696e..4a20c548 100644 --- a/options-table.c +++ b/options-table.c @@ -989,6 +989,14 @@ const struct options_table_entry options_table[] = { .scope = OPTIONS_TABLE_SERVER, .default_str = "" }, + + { .name = "tmate-foreground-restart", + .type = OPTIONS_TABLE_NUMBER, + .scope = OPTIONS_TABLE_SERVER, + .minimum = 0, + .maximum = 1, + .default_num = 1 + }, #endif { .name = NULL } diff --git a/session.c b/session.c index ab58ed9f..d1a644d5 100644 --- a/session.c +++ b/session.c @@ -207,6 +207,36 @@ session_free(__unused int fd, __unused short events, void *arg) } } +static void maybe_restart_session(void) +{ + int fg_restart = options_get_number(global_options, "tmate-foreground-restart"); + if (!fg_restart) + return; + + /* + * throttle restarts. This is a blocking sleep. It's + * simpler than using a timer, but fairly harmless + * from a blocking perspective. + */ + usleep(500*1000); + next_session_id = 0; + run_initial_client_cmd(); + + tmate_info("Shell exited. Shell restarted"); + + struct session *s; + s = RB_MIN(sessions, &sessions); + if (!s) + return; + + struct window_pane *wp; + wp = s->curw->window->active; + window_pane_set_mode(wp, &window_copy_mode); + window_copy_init_for_output(wp); + window_copy_add(wp, "%s", "Shell exited. Shell restarted."); + window_copy_add(wp, "%s", "Note: press the following sequence to disconnect from SSH: ~."); +} + /* Destroy a session. */ void session_destroy(struct session *s) @@ -239,15 +269,7 @@ session_destroy(struct session *s) #ifdef TMATE if (tmate_foreground && !server_exit) { - tmate_info("Shell exited, restarting"); - /* - * throttle restarts. This is a blocking sleep. - * It's simpler than using a timer, but fairly harmless - * from a blocking perspective. - */ - usleep(500*1000); - next_session_id = 0; - run_initial_client_cmd(); + maybe_restart_session(); } else { tmate_info("Session closed"); tmate_write_fin();