2014-11-08 12:27:43 +00:00
|
|
|
/* $OpenBSD$ */
|
2007-09-26 10:35:24 +00:00
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2007-09-26 10:35:24 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2017-10-12 11:32:27 +00:00
|
|
|
#include <sys/wait.h>
|
2015-10-27 13:23:24 +00:00
|
|
|
#include <sys/uio.h>
|
2007-09-26 10:35:24 +00:00
|
|
|
|
2012-07-11 19:34:16 +00:00
|
|
|
#include <stdlib.h>
|
2007-09-26 10:35:24 +00:00
|
|
|
#include <string.h>
|
2009-06-25 17:02:59 +00:00
|
|
|
#include <time.h>
|
2007-09-27 09:52:03 +00:00
|
|
|
#include <unistd.h>
|
2007-09-26 10:35:24 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2023-09-01 13:48:54 +00:00
|
|
|
static void server_destroy_session_group(struct session *);
|
2009-11-08 23:11:23 +00:00
|
|
|
|
2007-09-26 10:35:24 +00:00
|
|
|
void
|
2007-11-27 19:23:34 +00:00
|
|
|
server_redraw_client(struct client *c)
|
2007-10-04 00:02:10 +00:00
|
|
|
{
|
2018-08-19 16:45:03 +00:00
|
|
|
c->flags |= CLIENT_ALLREDRAWFLAGS;
|
2007-10-04 00:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-11-27 19:23:34 +00:00
|
|
|
server_status_client(struct client *c)
|
2007-09-26 10:35:24 +00:00
|
|
|
{
|
2018-08-19 16:45:03 +00:00
|
|
|
c->flags |= CLIENT_REDRAWSTATUS;
|
2007-10-04 19:03:52 +00:00
|
|
|
}
|
|
|
|
|
2007-10-01 14:53:29 +00:00
|
|
|
void
|
2007-10-04 00:02:10 +00:00
|
|
|
server_redraw_session(struct session *s)
|
2007-10-01 14:53:29 +00:00
|
|
|
{
|
2008-06-07 06:47:38 +00:00
|
|
|
struct client *c;
|
2007-11-27 19:23:34 +00:00
|
|
|
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
2008-06-07 06:47:38 +00:00
|
|
|
if (c->session == s)
|
|
|
|
server_redraw_client(c);
|
|
|
|
}
|
2007-09-26 10:35:24 +00:00
|
|
|
}
|
|
|
|
|
2009-10-11 23:38:16 +00:00
|
|
|
void
|
|
|
|
server_redraw_session_group(struct session *s)
|
|
|
|
{
|
|
|
|
struct session_group *sg;
|
|
|
|
|
2017-02-09 15:04:53 +00:00
|
|
|
if ((sg = session_group_contains(s)) == NULL)
|
2009-10-11 23:38:16 +00:00
|
|
|
server_redraw_session(s);
|
|
|
|
else {
|
|
|
|
TAILQ_FOREACH(s, &sg->sessions, gentry)
|
|
|
|
server_redraw_session(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-12 11:24:15 +00:00
|
|
|
void
|
|
|
|
server_status_session(struct session *s)
|
|
|
|
{
|
2008-06-07 06:47:38 +00:00
|
|
|
struct client *c;
|
|
|
|
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
2008-06-07 06:47:38 +00:00
|
|
|
if (c->session == s)
|
|
|
|
server_status_client(c);
|
|
|
|
}
|
2007-10-12 11:24:15 +00:00
|
|
|
}
|
|
|
|
|
2009-10-11 23:38:16 +00:00
|
|
|
void
|
|
|
|
server_status_session_group(struct session *s)
|
|
|
|
{
|
|
|
|
struct session_group *sg;
|
|
|
|
|
2017-02-09 15:04:53 +00:00
|
|
|
if ((sg = session_group_contains(s)) == NULL)
|
2009-10-11 23:38:16 +00:00
|
|
|
server_status_session(s);
|
|
|
|
else {
|
|
|
|
TAILQ_FOREACH(s, &sg->sessions, gentry)
|
|
|
|
server_status_session(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-19 10:21:36 +00:00
|
|
|
void
|
2007-11-27 19:23:34 +00:00
|
|
|
server_redraw_window(struct window *w)
|
2007-09-29 13:22:15 +00:00
|
|
|
{
|
2008-06-07 06:47:38 +00:00
|
|
|
struct client *c;
|
2007-10-19 10:21:36 +00:00
|
|
|
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->session != NULL && c->session->curw->window == w)
|
2008-06-07 06:47:38 +00:00
|
|
|
server_redraw_client(c);
|
|
|
|
}
|
2007-10-19 10:21:36 +00:00
|
|
|
}
|
|
|
|
|
2010-01-05 23:52:37 +00:00
|
|
|
void
|
|
|
|
server_redraw_window_borders(struct window *w)
|
|
|
|
{
|
|
|
|
struct client *c;
|
|
|
|
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->session != NULL && c->session->curw->window == w)
|
2018-08-19 16:45:03 +00:00
|
|
|
c->flags |= CLIENT_REDRAWBORDERS;
|
2010-01-05 23:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-19 10:21:36 +00:00
|
|
|
void
|
2007-11-27 19:23:34 +00:00
|
|
|
server_status_window(struct window *w)
|
2007-10-19 10:21:36 +00:00
|
|
|
{
|
2007-11-27 19:23:34 +00:00
|
|
|
struct session *s;
|
2007-10-19 10:21:36 +00:00
|
|
|
|
2007-11-27 19:23:34 +00:00
|
|
|
/*
|
|
|
|
* This is slightly different. We want to redraw the status line of any
|
2013-02-23 22:25:58 +00:00
|
|
|
* clients containing this window rather than anywhere it is the
|
2007-11-27 19:23:34 +00:00
|
|
|
* current window.
|
|
|
|
*/
|
2007-10-12 11:24:15 +00:00
|
|
|
|
2010-12-22 15:36:44 +00:00
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
2015-04-22 15:32:33 +00:00
|
|
|
if (session_has(s, w))
|
2007-11-27 19:23:34 +00:00
|
|
|
server_status_session(s);
|
2007-10-12 11:24:15 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-11 00:48:42 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
server_lock(void)
|
|
|
|
{
|
2009-09-25 17:51:39 +00:00
|
|
|
struct client *c;
|
2009-09-03 20:44:38 +00:00
|
|
|
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->session != NULL)
|
|
|
|
server_lock_client(c);
|
2009-09-25 17:51:39 +00:00
|
|
|
}
|
|
|
|
}
|
2009-01-11 00:48:42 +00:00
|
|
|
|
2009-09-25 17:51:39 +00:00
|
|
|
void
|
|
|
|
server_lock_session(struct session *s)
|
|
|
|
{
|
|
|
|
struct client *c;
|
|
|
|
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->session == s)
|
|
|
|
server_lock_client(c);
|
2009-12-04 22:14:47 +00:00
|
|
|
}
|
2009-09-25 17:51:39 +00:00
|
|
|
}
|
2009-05-29 23:25:26 +00:00
|
|
|
|
2009-09-25 17:51:39 +00:00
|
|
|
void
|
|
|
|
server_lock_client(struct client *c)
|
|
|
|
{
|
2013-10-05 23:28:35 +00:00
|
|
|
const char *cmd;
|
2009-05-29 23:25:26 +00:00
|
|
|
|
2013-02-19 21:11:32 +00:00
|
|
|
if (c->flags & CLIENT_CONTROL)
|
2013-02-17 10:12:55 +00:00
|
|
|
return;
|
|
|
|
|
2009-10-05 18:23:31 +00:00
|
|
|
if (c->flags & CLIENT_SUSPENDED)
|
|
|
|
return;
|
|
|
|
|
2015-10-27 15:58:42 +00:00
|
|
|
cmd = options_get_string(c->session->options, "lock-command");
|
2017-08-29 09:18:48 +00:00
|
|
|
if (*cmd == '\0' || strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
|
2009-09-25 17:51:39 +00:00
|
|
|
return;
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2009-09-25 17:51:39 +00:00
|
|
|
tty_stop_tty(&c->tty);
|
|
|
|
tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
|
|
|
|
tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
|
2011-10-23 15:10:22 +00:00
|
|
|
tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
|
2009-09-25 17:51:39 +00:00
|
|
|
|
|
|
|
c->flags |= CLIENT_SUSPENDED;
|
2017-07-12 09:07:52 +00:00
|
|
|
proc_send(c->peer, MSG_LOCK, -1, cmd, strlen(cmd) + 1);
|
2009-01-11 00:48:42 +00:00
|
|
|
}
|
2009-07-18 11:07:14 +00:00
|
|
|
|
2018-04-10 10:48:44 +00:00
|
|
|
void
|
|
|
|
server_kill_pane(struct window_pane *wp)
|
|
|
|
{
|
|
|
|
struct window *w = wp->window;
|
|
|
|
|
|
|
|
if (window_count_panes(w) == 1) {
|
2020-07-04 14:24:02 +00:00
|
|
|
server_kill_window(w, 1);
|
2018-04-10 10:48:44 +00:00
|
|
|
recalculate_sizes();
|
|
|
|
} else {
|
|
|
|
server_unzoom_window(w);
|
2020-05-14 10:18:19 +00:00
|
|
|
server_client_remove_pane(wp);
|
2018-04-10 10:48:44 +00:00
|
|
|
layout_close_pane(wp);
|
|
|
|
window_remove_pane(w, wp);
|
|
|
|
server_redraw_window(w);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-18 11:07:14 +00:00
|
|
|
void
|
2020-07-04 14:24:02 +00:00
|
|
|
server_kill_window(struct window *w, int renumber)
|
2009-07-18 11:07:14 +00:00
|
|
|
{
|
2020-07-04 14:24:02 +00:00
|
|
|
struct session *s, *s1;
|
|
|
|
struct winlink *wl;
|
2010-12-25 23:44:37 +00:00
|
|
|
|
2020-07-04 14:24:02 +00:00
|
|
|
RB_FOREACH_SAFE(s, sessions, &sessions, s1) {
|
2015-04-22 15:32:33 +00:00
|
|
|
if (!session_has(s, w))
|
2009-07-18 11:07:14 +00:00
|
|
|
continue;
|
2020-07-04 14:24:02 +00:00
|
|
|
|
2015-01-06 09:12:53 +00:00
|
|
|
server_unzoom_window(w);
|
2010-01-25 17:13:43 +00:00
|
|
|
while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
|
|
|
|
if (session_detach(s, wl)) {
|
|
|
|
server_destroy_session_group(s);
|
|
|
|
break;
|
2023-09-01 13:48:54 +00:00
|
|
|
}
|
|
|
|
server_redraw_session_group(s);
|
2010-01-25 17:13:43 +00:00
|
|
|
}
|
2012-05-03 17:51:04 +00:00
|
|
|
|
2020-07-04 14:24:02 +00:00
|
|
|
if (renumber)
|
|
|
|
server_renumber_session(s);
|
2009-07-18 11:07:14 +00:00
|
|
|
}
|
2013-04-22 13:35:18 +00:00
|
|
|
recalculate_sizes();
|
2009-09-20 22:15:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 14:24:02 +00:00
|
|
|
void
|
|
|
|
server_renumber_session(struct session *s)
|
|
|
|
{
|
|
|
|
struct session_group *sg;
|
|
|
|
|
|
|
|
if (options_get_number(s->options, "renumber-windows")) {
|
|
|
|
if ((sg = session_group_contains(s)) != NULL) {
|
|
|
|
TAILQ_FOREACH(s, &sg->sessions, gentry)
|
|
|
|
session_renumber_windows(s);
|
|
|
|
} else
|
|
|
|
session_renumber_windows(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
server_renumber_all(void)
|
|
|
|
{
|
|
|
|
struct session *s;
|
|
|
|
|
|
|
|
RB_FOREACH(s, sessions, &sessions)
|
|
|
|
server_renumber_session(s);
|
|
|
|
}
|
|
|
|
|
2009-09-20 22:15:32 +00:00
|
|
|
int
|
2009-10-11 23:38:16 +00:00
|
|
|
server_link_window(struct session *src, struct winlink *srcwl,
|
2014-04-17 14:45:49 +00:00
|
|
|
struct session *dst, int dstidx, int killflag, int selectflag,
|
|
|
|
char **cause)
|
2009-09-20 22:15:32 +00:00
|
|
|
{
|
2009-10-11 23:38:16 +00:00
|
|
|
struct winlink *dstwl;
|
|
|
|
struct session_group *srcsg, *dstsg;
|
|
|
|
|
2017-02-09 15:04:53 +00:00
|
|
|
srcsg = session_group_contains(src);
|
|
|
|
dstsg = session_group_contains(dst);
|
2009-10-11 23:38:16 +00:00
|
|
|
if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
|
|
|
|
xasprintf(cause, "sessions are grouped");
|
|
|
|
return (-1);
|
|
|
|
}
|
2009-09-20 22:15:32 +00:00
|
|
|
|
|
|
|
dstwl = NULL;
|
|
|
|
if (dstidx != -1)
|
|
|
|
dstwl = winlink_find_by_index(&dst->windows, dstidx);
|
|
|
|
if (dstwl != NULL) {
|
2010-10-09 14:31:50 +00:00
|
|
|
if (dstwl->window == srcwl->window) {
|
|
|
|
xasprintf(cause, "same index: %d", dstidx);
|
2010-08-11 22:16:43 +00:00
|
|
|
return (-1);
|
2010-10-09 14:31:50 +00:00
|
|
|
}
|
2009-09-20 22:15:32 +00:00
|
|
|
if (killflag) {
|
|
|
|
/*
|
|
|
|
* Can't use session_detach as it will destroy session
|
|
|
|
* if this makes it empty.
|
|
|
|
*/
|
2016-10-16 22:06:40 +00:00
|
|
|
notify_session_window("window-unlinked", dst,
|
|
|
|
dstwl->window);
|
2010-06-22 23:26:18 +00:00
|
|
|
dstwl->flags &= ~WINLINK_ALERTFLAGS;
|
2009-09-20 22:15:32 +00:00
|
|
|
winlink_stack_remove(&dst->lastw, dstwl);
|
|
|
|
winlink_remove(&dst->windows, dstwl);
|
|
|
|
|
|
|
|
/* Force select/redraw if current. */
|
2009-10-12 00:37:41 +00:00
|
|
|
if (dstwl == dst->curw) {
|
2009-09-20 22:15:32 +00:00
|
|
|
selectflag = 1;
|
2009-10-12 00:37:41 +00:00
|
|
|
dst->curw = NULL;
|
|
|
|
}
|
2009-09-20 22:15:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dstidx == -1)
|
2015-10-27 15:58:42 +00:00
|
|
|
dstidx = -1 - options_get_number(dst->options, "base-index");
|
2009-09-20 22:15:32 +00:00
|
|
|
dstwl = session_attach(dst, srcwl->window, dstidx, cause);
|
|
|
|
if (dstwl == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2009-10-11 23:38:16 +00:00
|
|
|
if (selectflag)
|
2009-09-20 22:15:32 +00:00
|
|
|
session_select(dst, dstwl->idx);
|
2009-10-11 23:38:16 +00:00
|
|
|
server_redraw_session_group(dst);
|
2009-09-20 22:15:32 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
server_unlink_window(struct session *s, struct winlink *wl)
|
|
|
|
{
|
|
|
|
if (session_detach(s, wl))
|
2009-10-11 23:38:16 +00:00
|
|
|
server_destroy_session_group(s);
|
2009-09-20 22:15:32 +00:00
|
|
|
else
|
2009-10-11 23:38:16 +00:00
|
|
|
server_redraw_session_group(s);
|
|
|
|
}
|
|
|
|
|
2009-11-14 17:48:39 +00:00
|
|
|
void
|
2016-10-16 22:06:40 +00:00
|
|
|
server_destroy_pane(struct window_pane *wp, int notify)
|
2009-11-14 17:48:39 +00:00
|
|
|
{
|
2011-09-21 16:35:13 +00:00
|
|
|
struct window *w = wp->window;
|
|
|
|
struct screen_write_ctx ctx;
|
|
|
|
struct grid_cell gc;
|
2021-01-04 08:43:16 +00:00
|
|
|
int remain_on_exit;
|
2022-03-08 18:31:46 +00:00
|
|
|
const char *s;
|
|
|
|
char *expanded;
|
|
|
|
u_int sx = screen_size_x(&wp->base);
|
|
|
|
u_int sy = screen_size_y(&wp->base);
|
2009-11-14 17:48:39 +00:00
|
|
|
|
2010-04-18 15:10:55 +00:00
|
|
|
if (wp->fd != -1) {
|
2014-02-24 23:07:22 +00:00
|
|
|
#ifdef HAVE_UTEMPTER
|
|
|
|
utempter_remove_record(wp->fd);
|
|
|
|
#endif
|
2010-04-18 15:10:55 +00:00
|
|
|
bufferevent_free(wp->event);
|
2018-11-30 08:44:40 +00:00
|
|
|
wp->event = NULL;
|
2012-01-29 12:53:33 +00:00
|
|
|
close(wp->fd);
|
2010-04-18 15:10:55 +00:00
|
|
|
wp->fd = -1;
|
|
|
|
}
|
2009-11-14 17:48:39 +00:00
|
|
|
|
2021-01-04 08:43:16 +00:00
|
|
|
remain_on_exit = options_get_number(wp->options, "remain-on-exit");
|
|
|
|
if (remain_on_exit != 0 && (~wp->flags & PANE_STATUSREADY))
|
|
|
|
return;
|
|
|
|
switch (remain_on_exit) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (WIFEXITED(wp->status) && WEXITSTATUS(wp->status) == 0)
|
|
|
|
break;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 1:
|
2017-10-12 11:32:27 +00:00
|
|
|
if (wp->flags & PANE_STATUSDRAWN)
|
|
|
|
return;
|
|
|
|
wp->flags |= PANE_STATUSDRAWN;
|
|
|
|
|
2022-03-08 18:31:46 +00:00
|
|
|
gettimeofday(&wp->dead_time, NULL);
|
2016-10-16 22:06:40 +00:00
|
|
|
if (notify)
|
|
|
|
notify_pane("pane-died", wp);
|
|
|
|
|
2022-03-08 18:31:46 +00:00
|
|
|
s = options_get_string(wp->options, "remain-on-exit-format");
|
|
|
|
if (*s != '\0') {
|
|
|
|
screen_write_start_pane(&ctx, wp, &wp->base);
|
|
|
|
screen_write_scrollregion(&ctx, 0, sy - 1);
|
|
|
|
screen_write_cursormove(&ctx, 0, sy - 1, 0);
|
|
|
|
screen_write_linefeed(&ctx, 1, 8);
|
|
|
|
memcpy(&gc, &grid_default_cell, sizeof gc);
|
|
|
|
|
|
|
|
expanded = format_single(NULL, s, NULL, NULL, NULL, wp);
|
|
|
|
format_draw(&ctx, &gc, sx, expanded, NULL, 0);
|
|
|
|
free(expanded);
|
|
|
|
|
|
|
|
screen_write_stop(&ctx);
|
2017-10-12 11:32:27 +00:00
|
|
|
}
|
2022-03-08 18:31:46 +00:00
|
|
|
wp->base.mode &= ~MODE_CURSOR;
|
2017-10-12 11:32:27 +00:00
|
|
|
|
2011-09-21 16:35:13 +00:00
|
|
|
wp->flags |= PANE_REDRAW;
|
2009-11-14 17:48:39 +00:00
|
|
|
return;
|
2011-09-21 16:35:13 +00:00
|
|
|
}
|
2009-11-14 17:48:39 +00:00
|
|
|
|
2016-10-16 22:06:40 +00:00
|
|
|
if (notify)
|
|
|
|
notify_pane("pane-exited", wp);
|
|
|
|
|
2013-02-24 00:25:03 +00:00
|
|
|
server_unzoom_window(w);
|
2020-05-14 10:18:19 +00:00
|
|
|
server_client_remove_pane(wp);
|
2009-11-14 17:48:39 +00:00
|
|
|
layout_close_pane(wp);
|
|
|
|
window_remove_pane(w, wp);
|
|
|
|
|
|
|
|
if (TAILQ_EMPTY(&w->panes))
|
2020-07-04 14:24:02 +00:00
|
|
|
server_kill_window(w, 1);
|
2009-11-14 17:48:39 +00:00
|
|
|
else
|
|
|
|
server_redraw_window(w);
|
|
|
|
}
|
|
|
|
|
2016-10-11 13:21:59 +00:00
|
|
|
static void
|
2009-10-11 23:38:16 +00:00
|
|
|
server_destroy_session_group(struct session *s)
|
|
|
|
{
|
|
|
|
struct session_group *sg;
|
2013-08-01 22:38:35 +00:00
|
|
|
struct session *s1;
|
2009-10-11 23:38:16 +00:00
|
|
|
|
2023-09-01 13:48:54 +00:00
|
|
|
if ((sg = session_group_contains(s)) == NULL) {
|
2009-10-11 23:38:16 +00:00
|
|
|
server_destroy_session(s);
|
2023-09-01 13:48:54 +00:00
|
|
|
session_destroy(s, 1, __func__);
|
|
|
|
} else {
|
2013-08-01 22:38:35 +00:00
|
|
|
TAILQ_FOREACH_SAFE(s, &sg->sessions, gentry, s1) {
|
2009-10-11 23:38:16 +00:00
|
|
|
server_destroy_session(s);
|
2019-04-07 12:01:03 +00:00
|
|
|
session_destroy(s, 1, __func__);
|
2013-08-01 22:38:35 +00:00
|
|
|
}
|
2009-10-11 23:38:16 +00:00
|
|
|
}
|
2009-07-18 11:07:14 +00:00
|
|
|
}
|
2009-08-31 22:30:15 +00:00
|
|
|
|
2016-10-10 21:29:23 +00:00
|
|
|
static struct session *
|
2023-09-01 13:48:54 +00:00
|
|
|
server_find_session(struct session *s,
|
|
|
|
int (*f)(struct session *, struct session *))
|
2010-07-02 02:45:52 +00:00
|
|
|
{
|
2021-02-01 08:01:14 +00:00
|
|
|
struct session *s_loop, *s_out = NULL;
|
2010-07-02 02:45:52 +00:00
|
|
|
|
2010-12-22 15:36:44 +00:00
|
|
|
RB_FOREACH(s_loop, sessions, &sessions) {
|
2023-09-01 13:48:54 +00:00
|
|
|
if (s_loop != s && (s_out == NULL || f(s_loop, s_out)))
|
2010-07-02 02:45:52 +00:00
|
|
|
s_out = s_loop;
|
|
|
|
}
|
|
|
|
return (s_out);
|
|
|
|
}
|
|
|
|
|
2023-09-01 13:48:54 +00:00
|
|
|
static int
|
|
|
|
server_newer_session(struct session *s_loop, struct session *s_out)
|
2021-02-01 08:01:14 +00:00
|
|
|
{
|
2023-09-01 13:48:54 +00:00
|
|
|
return (timercmp(&s_loop->activity_time, &s_out->activity_time, <));
|
|
|
|
}
|
2021-02-01 08:01:14 +00:00
|
|
|
|
2023-09-01 13:48:54 +00:00
|
|
|
static int
|
|
|
|
server_newer_detached_session(struct session *s_loop, struct session *s_out)
|
|
|
|
{
|
|
|
|
if (s_loop->attached)
|
|
|
|
return (0);
|
|
|
|
return (server_newer_session(s_loop, s_out));
|
2021-02-01 08:01:14 +00:00
|
|
|
}
|
|
|
|
|
2009-09-13 20:37:37 +00:00
|
|
|
void
|
|
|
|
server_destroy_session(struct session *s)
|
|
|
|
{
|
|
|
|
struct client *c;
|
2023-09-01 13:48:54 +00:00
|
|
|
struct session *s_new = NULL;
|
2021-02-01 08:01:14 +00:00
|
|
|
int detach_on_destroy;
|
2009-12-04 22:14:47 +00:00
|
|
|
|
2021-02-01 08:01:14 +00:00
|
|
|
detach_on_destroy = options_get_number(s->options, "detach-on-destroy");
|
|
|
|
if (detach_on_destroy == 0)
|
2023-09-01 13:48:54 +00:00
|
|
|
s_new = server_find_session(s, server_newer_session);
|
2021-02-01 08:01:14 +00:00
|
|
|
else if (detach_on_destroy == 2)
|
2023-09-01 13:48:54 +00:00
|
|
|
s_new = server_find_session(s, server_newer_detached_session);
|
|
|
|
else if (detach_on_destroy == 3)
|
|
|
|
s_new = session_previous_session(s);
|
|
|
|
else if (detach_on_destroy == 4)
|
|
|
|
s_new = session_next_session(s);
|
|
|
|
if (s_new == s)
|
2010-07-02 02:45:52 +00:00
|
|
|
s_new = NULL;
|
2015-04-24 23:17:11 +00:00
|
|
|
TAILQ_FOREACH(c, &clients, entry) {
|
|
|
|
if (c->session != s)
|
2009-09-13 20:37:37 +00:00
|
|
|
continue;
|
2023-09-01 13:48:54 +00:00
|
|
|
c->session = NULL;
|
|
|
|
c->last_session = NULL;
|
2021-08-17 16:19:00 +00:00
|
|
|
server_client_set_session(c, s_new);
|
2021-08-13 06:52:51 +00:00
|
|
|
if (s_new == NULL)
|
2010-08-09 21:44:25 +00:00
|
|
|
c->flags |= CLIENT_EXIT;
|
2009-09-13 20:37:37 +00:00
|
|
|
}
|
2010-07-02 02:43:50 +00:00
|
|
|
recalculate_sizes();
|
2009-09-13 20:37:37 +00:00
|
|
|
}
|
|
|
|
|
2010-10-09 14:29:32 +00:00
|
|
|
void
|
2014-02-14 13:59:01 +00:00
|
|
|
server_check_unattached(void)
|
2010-10-09 14:29:32 +00:00
|
|
|
{
|
2024-02-13 08:10:23 +00:00
|
|
|
struct session *s;
|
|
|
|
struct session_group *sg;
|
2010-10-09 14:29:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If any sessions are no longer attached and have destroy-unattached
|
|
|
|
* set, collect them.
|
|
|
|
*/
|
2010-12-22 15:36:44 +00:00
|
|
|
RB_FOREACH(s, sessions, &sessions) {
|
2018-08-18 20:08:52 +00:00
|
|
|
if (s->attached != 0)
|
2010-10-09 14:29:32 +00:00
|
|
|
continue;
|
2024-02-13 08:10:23 +00:00
|
|
|
switch (options_get_number(s->options, "destroy-unattached")) {
|
|
|
|
case 0: /* off */
|
|
|
|
continue;
|
|
|
|
case 1: /* on */
|
|
|
|
break;
|
|
|
|
case 2: /* keep-last */
|
|
|
|
sg = session_group_contains(s);
|
|
|
|
if (sg == NULL || session_group_count(sg) <= 1)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case 3: /* keep-group */
|
|
|
|
sg = session_group_contains(s);
|
|
|
|
if (sg != NULL && session_group_count(sg) == 1)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
session_destroy(s, 1, __func__);
|
2010-10-09 14:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-24 00:25:03 +00:00
|
|
|
void
|
|
|
|
server_unzoom_window(struct window *w)
|
|
|
|
{
|
2018-02-28 08:55:44 +00:00
|
|
|
if (window_unzoom(w) == 0)
|
2015-04-19 21:34:21 +00:00
|
|
|
server_redraw_window(w);
|
2013-02-24 00:25:03 +00:00
|
|
|
}
|