From 509221520c87510016f5c90aeea0d4dcc4b74a98 Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 1 Feb 2021 08:01:14 +0000 Subject: Add a no-detached choice to detach-on-destroy which detaches only if there are no other detached sessions to switch to, from Sencer Selcuk in GitHub issue 2553. --- server-fn.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'server-fn.c') diff --git a/server-fn.c b/server-fn.c index d0e06c0f..4358fa5c 100644 --- a/server-fn.c +++ b/server-fn.c @@ -401,9 +401,8 @@ server_destroy_session_group(struct session *s) static struct session * server_next_session(struct session *s) { - struct session *s_loop, *s_out; + struct session *s_loop, *s_out = NULL; - s_out = NULL; RB_FOREACH(s_loop, sessions, &sessions) { if (s_loop == s) continue; @@ -414,17 +413,35 @@ server_next_session(struct session *s) return (s_out); } +static struct session * +server_next_detached_session(struct session *s) +{ + struct session *s_loop, *s_out = NULL; + + RB_FOREACH(s_loop, sessions, &sessions) { + if (s_loop == s || s_loop->attached) + continue; + if (s_out == NULL || + timercmp(&s_loop->activity_time, &s_out->activity_time, <)) + s_out = s_loop; + } + return (s_out); +} + void server_destroy_session(struct session *s) { struct client *c; struct session *s_new; + int detach_on_destroy; - if (!options_get_number(s->options, "detach-on-destroy")) + detach_on_destroy = options_get_number(s->options, "detach-on-destroy"); + if (detach_on_destroy == 0) s_new = server_next_session(s); + else if (detach_on_destroy == 2) + s_new = server_next_detached_session(s); else s_new = NULL; - TAILQ_FOREACH(c, &clients, entry) { if (c->session != s) continue; -- cgit