Annotation of src/usr.bin/tmux/server-fn.c, Revision 1.126
1.126 ! nicm 1: /* $OpenBSD: server-fn.c,v 1.125 2020/05/16 15:34:08 nicm Exp $ */
1.1 nicm 2:
3: /*
1.98 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
1.90 nicm 20: #include <sys/queue.h>
1.112 nicm 21: #include <sys/wait.h>
1.90 nicm 22: #include <sys/uio.h>
1.1 nicm 23:
1.90 nicm 24: #include <imsg.h>
1.60 nicm 25: #include <stdlib.h>
1.1 nicm 26: #include <string.h>
1.5 nicm 27: #include <time.h>
1.1 nicm 28: #include <unistd.h>
29:
30: #include "tmux.h"
31:
1.100 nicm 32: static struct session *server_next_session(struct session *);
1.101 nicm 33: static void server_destroy_session_group(struct session *);
1.58 nicm 34:
35: void
1.1 nicm 36: server_redraw_client(struct client *c)
37: {
1.116 nicm 38: c->flags |= CLIENT_ALLREDRAWFLAGS;
1.1 nicm 39: }
40:
41: void
42: server_status_client(struct client *c)
43: {
1.116 nicm 44: c->flags |= CLIENT_REDRAWSTATUS;
1.1 nicm 45: }
46:
47: void
48: server_redraw_session(struct session *s)
49: {
50: struct client *c;
51:
1.84 nicm 52: TAILQ_FOREACH(c, &clients, entry) {
1.1 nicm 53: if (c->session == s)
54: server_redraw_client(c);
55: }
56: }
57:
58: void
1.25 nicm 59: server_redraw_session_group(struct session *s)
60: {
61: struct session_group *sg;
62:
1.104 nicm 63: if ((sg = session_group_contains(s)) == NULL)
1.25 nicm 64: server_redraw_session(s);
65: else {
66: TAILQ_FOREACH(s, &sg->sessions, gentry)
67: server_redraw_session(s);
68: }
69: }
70:
71: void
1.1 nicm 72: server_status_session(struct session *s)
73: {
74: struct client *c;
75:
1.84 nicm 76: TAILQ_FOREACH(c, &clients, entry) {
1.1 nicm 77: if (c->session == s)
78: server_status_client(c);
79: }
80: }
81:
82: void
1.25 nicm 83: server_status_session_group(struct session *s)
84: {
85: struct session_group *sg;
86:
1.104 nicm 87: if ((sg = session_group_contains(s)) == NULL)
1.25 nicm 88: server_status_session(s);
89: else {
90: TAILQ_FOREACH(s, &sg->sessions, gentry)
91: server_status_session(s);
92: }
93: }
94:
95: void
1.1 nicm 96: server_redraw_window(struct window *w)
97: {
98: struct client *c;
99:
1.84 nicm 100: TAILQ_FOREACH(c, &clients, entry) {
101: if (c->session != NULL && c->session->curw->window == w)
1.1 nicm 102: server_redraw_client(c);
103: }
1.33 nicm 104: }
105:
106: void
107: server_redraw_window_borders(struct window *w)
108: {
109: struct client *c;
110:
1.84 nicm 111: TAILQ_FOREACH(c, &clients, entry) {
112: if (c->session != NULL && c->session->curw->window == w)
1.116 nicm 113: c->flags |= CLIENT_REDRAWBORDERS;
1.33 nicm 114: }
1.1 nicm 115: }
116:
117: void
118: server_status_window(struct window *w)
119: {
120: struct session *s;
121:
122: /*
123: * This is slightly different. We want to redraw the status line of any
1.65 nicm 124: * clients containing this window rather than anywhere it is the
1.1 nicm 125: * current window.
126: */
127:
1.47 nicm 128: RB_FOREACH(s, sessions, &sessions) {
1.83 nicm 129: if (session_has(s, w))
1.1 nicm 130: server_status_session(s);
131: }
132: }
133:
134: void
135: server_lock(void)
136: {
1.23 nicm 137: struct client *c;
1.17 nicm 138:
1.84 nicm 139: TAILQ_FOREACH(c, &clients, entry) {
140: if (c->session != NULL)
141: server_lock_client(c);
1.23 nicm 142: }
143: }
1.1 nicm 144:
1.23 nicm 145: void
146: server_lock_session(struct session *s)
147: {
148: struct client *c;
149:
1.84 nicm 150: TAILQ_FOREACH(c, &clients, entry) {
151: if (c->session == s)
152: server_lock_client(c);
1.31 nicm 153: }
1.23 nicm 154: }
1.1 nicm 155:
1.23 nicm 156: void
157: server_lock_client(struct client *c)
158: {
1.72 nicm 159: const char *cmd;
1.62 nicm 160:
1.64 nicm 161: if (c->flags & CLIENT_CONTROL)
1.62 nicm 162: return;
1.24 nicm 163:
164: if (c->flags & CLIENT_SUSPENDED)
165: return;
1.1 nicm 166:
1.91 nicm 167: cmd = options_get_string(c->session->options, "lock-command");
1.111 nicm 168: if (*cmd == '\0' || strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1.23 nicm 169: return;
1.31 nicm 170:
1.23 nicm 171: tty_stop_tty(&c->tty);
172: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
173: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
1.52 nicm 174: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
1.23 nicm 175:
176: c->flags |= CLIENT_SUSPENDED;
1.110 nicm 177: proc_send(c->peer, MSG_LOCK, -1, cmd, strlen(cmd) + 1);
1.114 nicm 178: }
179:
180: void
181: server_kill_pane(struct window_pane *wp)
182: {
183: struct window *w = wp->window;
184:
185: if (window_count_panes(w) == 1) {
186: server_kill_window(w);
187: recalculate_sizes();
188: } else {
189: server_unzoom_window(w);
1.126 ! nicm 190: server_client_remove_pane(wp);
1.114 nicm 191: layout_close_pane(wp);
192: window_remove_pane(w, wp);
193: server_redraw_window(w);
194: }
1.8 nicm 195: }
196:
197: void
198: server_kill_window(struct window *w)
199: {
1.70 nicm 200: struct session *s, *next_s, *target_s;
201: struct session_group *sg;
202: struct winlink *wl;
1.31 nicm 203:
1.48 nicm 204: next_s = RB_MIN(sessions, &sessions);
205: while (next_s != NULL) {
206: s = next_s;
207: next_s = RB_NEXT(sessions, &sessions, s);
208:
1.83 nicm 209: if (!session_has(s, w))
1.8 nicm 210: continue;
1.78 nicm 211: server_unzoom_window(w);
1.34 nicm 212: while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
213: if (session_detach(s, wl)) {
214: server_destroy_session_group(s);
215: break;
216: } else
217: server_redraw_session_group(s);
218: }
1.56 nicm 219:
1.91 nicm 220: if (options_get_number(s->options, "renumber-windows")) {
1.104 nicm 221: if ((sg = session_group_contains(s)) != NULL) {
1.70 nicm 222: TAILQ_FOREACH(target_s, &sg->sessions, gentry)
223: session_renumber_windows(target_s);
224: } else
225: session_renumber_windows(s);
226: }
1.8 nicm 227: }
1.69 nicm 228: recalculate_sizes();
1.21 nicm 229: }
230:
231: int
1.25 nicm 232: server_link_window(struct session *src, struct winlink *srcwl,
1.76 nicm 233: struct session *dst, int dstidx, int killflag, int selectflag,
234: char **cause)
1.21 nicm 235: {
1.25 nicm 236: struct winlink *dstwl;
237: struct session_group *srcsg, *dstsg;
238:
1.104 nicm 239: srcsg = session_group_contains(src);
240: dstsg = session_group_contains(dst);
1.25 nicm 241: if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
242: xasprintf(cause, "sessions are grouped");
243: return (-1);
244: }
1.21 nicm 245:
246: dstwl = NULL;
247: if (dstidx != -1)
248: dstwl = winlink_find_by_index(&dst->windows, dstidx);
249: if (dstwl != NULL) {
1.44 nicm 250: if (dstwl->window == srcwl->window) {
251: xasprintf(cause, "same index: %d", dstidx);
1.41 nicm 252: return (-1);
1.44 nicm 253: }
1.21 nicm 254: if (killflag) {
255: /*
256: * Can't use session_detach as it will destroy session
257: * if this makes it empty.
258: */
1.102 nicm 259: notify_session_window("window-unlinked", dst,
260: dstwl->window);
1.37 nicm 261: dstwl->flags &= ~WINLINK_ALERTFLAGS;
1.21 nicm 262: winlink_stack_remove(&dst->lastw, dstwl);
263: winlink_remove(&dst->windows, dstwl);
264:
265: /* Force select/redraw if current. */
1.26 nicm 266: if (dstwl == dst->curw) {
1.21 nicm 267: selectflag = 1;
1.26 nicm 268: dst->curw = NULL;
269: }
1.21 nicm 270: }
271: }
272:
273: if (dstidx == -1)
1.91 nicm 274: dstidx = -1 - options_get_number(dst->options, "base-index");
1.21 nicm 275: dstwl = session_attach(dst, srcwl->window, dstidx, cause);
276: if (dstwl == NULL)
277: return (-1);
278:
1.25 nicm 279: if (selectflag)
1.21 nicm 280: session_select(dst, dstwl->idx);
1.25 nicm 281: server_redraw_session_group(dst);
1.21 nicm 282:
283: return (0);
284: }
285:
286: void
287: server_unlink_window(struct session *s, struct winlink *wl)
288: {
289: if (session_detach(s, wl))
1.25 nicm 290: server_destroy_session_group(s);
291: else
292: server_redraw_session_group(s);
1.29 nicm 293: }
294:
295: void
1.102 nicm 296: server_destroy_pane(struct window_pane *wp, int notify)
1.29 nicm 297: {
1.51 nicm 298: struct window *w = wp->window;
299: struct screen_write_ctx ctx;
300: struct grid_cell gc;
1.112 nicm 301: time_t t;
302: char tim[26];
1.29 nicm 303:
1.36 nicm 304: if (wp->fd != -1) {
1.53 nicm 305: bufferevent_free(wp->event);
1.118 nicm 306: wp->event = NULL;
1.36 nicm 307: close(wp->fd);
308: wp->fd = -1;
309: }
1.29 nicm 310:
1.122 nicm 311: if (options_get_number(wp->options, "remain-on-exit")) {
1.112 nicm 312: if (~wp->flags & PANE_STATUSREADY)
1.51 nicm 313: return;
1.102 nicm 314:
1.112 nicm 315: if (wp->flags & PANE_STATUSDRAWN)
316: return;
317: wp->flags |= PANE_STATUSDRAWN;
318:
1.102 nicm 319: if (notify)
320: notify_pane("pane-died", wp);
321:
1.125 nicm 322: screen_write_start_pane(&ctx, wp, &wp->base);
1.51 nicm 323: screen_write_scrollregion(&ctx, 0, screen_size_y(ctx.s) - 1);
1.119 nicm 324: screen_write_cursormove(&ctx, 0, screen_size_y(ctx.s) - 1, 0);
1.108 nicm 325: screen_write_linefeed(&ctx, 1, 8);
1.51 nicm 326: memcpy(&gc, &grid_default_cell, sizeof gc);
1.112 nicm 327:
328: time(&t);
329: ctime_r(&t, tim);
330:
331: if (WIFEXITED(wp->status)) {
332: screen_write_nputs(&ctx, -1, &gc,
333: "Pane is dead (status %d, %s)",
334: WEXITSTATUS(wp->status),
335: tim);
336: } else if (WIFSIGNALED(wp->status)) {
337: screen_write_nputs(&ctx, -1, &gc,
1.124 nicm 338: "Pane is dead (signal %s, %s)",
339: sig2name(WTERMSIG(wp->status)),
1.112 nicm 340: tim);
341: }
342:
1.51 nicm 343: screen_write_stop(&ctx);
344: wp->flags |= PANE_REDRAW;
1.29 nicm 345: return;
1.51 nicm 346: }
1.29 nicm 347:
1.102 nicm 348: if (notify)
349: notify_pane("pane-exited", wp);
350:
1.66 nicm 351: server_unzoom_window(w);
1.126 ! nicm 352: server_client_remove_pane(wp);
1.29 nicm 353: layout_close_pane(wp);
354: window_remove_pane(w, wp);
1.97 nicm 355:
1.29 nicm 356: if (TAILQ_EMPTY(&w->panes))
357: server_kill_window(w);
358: else
359: server_redraw_window(w);
1.25 nicm 360: }
361:
1.101 nicm 362: static void
1.25 nicm 363: server_destroy_session_group(struct session *s)
364: {
365: struct session_group *sg;
1.71 nicm 366: struct session *s1;
1.25 nicm 367:
1.104 nicm 368: if ((sg = session_group_contains(s)) == NULL)
1.21 nicm 369: server_destroy_session(s);
1.25 nicm 370: else {
1.71 nicm 371: TAILQ_FOREACH_SAFE(s, &sg->sessions, gentry, s1) {
1.25 nicm 372: server_destroy_session(s);
1.120 nicm 373: session_destroy(s, 1, __func__);
1.71 nicm 374: }
1.25 nicm 375: }
1.19 nicm 376: }
377:
1.100 nicm 378: static struct session *
1.39 nicm 379: server_next_session(struct session *s)
380: {
381: struct session *s_loop, *s_out;
382:
383: s_out = NULL;
1.47 nicm 384: RB_FOREACH(s_loop, sessions, &sessions) {
385: if (s_loop == s)
1.39 nicm 386: continue;
387: if (s_out == NULL ||
388: timercmp(&s_loop->activity_time, &s_out->activity_time, <))
389: s_out = s_loop;
390: }
391: return (s_out);
392: }
393:
1.19 nicm 394: void
395: server_destroy_session(struct session *s)
396: {
397: struct client *c;
1.39 nicm 398: struct session *s_new;
1.31 nicm 399:
1.91 nicm 400: if (!options_get_number(s->options, "detach-on-destroy"))
1.39 nicm 401: s_new = server_next_session(s);
402: else
403: s_new = NULL;
404:
1.84 nicm 405: TAILQ_FOREACH(c, &clients, entry) {
406: if (c->session != s)
1.19 nicm 407: continue;
1.39 nicm 408: if (s_new == NULL) {
409: c->session = NULL;
1.40 nicm 410: c->flags |= CLIENT_EXIT;
1.39 nicm 411: } else {
1.46 nicm 412: c->last_session = NULL;
1.39 nicm 413: c->session = s_new;
1.96 nicm 414: server_client_set_key_table(c, NULL);
1.117 nicm 415: tty_update_client_offset(c);
1.86 nicm 416: status_timer_start(c);
1.102 nicm 417: notify_client("client-session-changed", c);
1.87 nicm 418: session_update_activity(s_new, NULL);
1.88 nicm 419: gettimeofday(&s_new->last_attached_time, NULL);
1.39 nicm 420: server_redraw_client(c);
1.95 nicm 421: alerts_check_session(s_new);
1.39 nicm 422: }
1.19 nicm 423: }
1.38 nicm 424: recalculate_sizes();
1.42 nicm 425: }
426:
427: void
1.75 nicm 428: server_check_unattached(void)
1.42 nicm 429: {
430: struct session *s;
431:
432: /*
433: * If any sessions are no longer attached and have destroy-unattached
434: * set, collect them.
435: */
1.47 nicm 436: RB_FOREACH(s, sessions, &sessions) {
1.115 nicm 437: if (s->attached != 0)
1.42 nicm 438: continue;
1.91 nicm 439: if (options_get_number (s->options, "destroy-unattached"))
1.120 nicm 440: session_destroy(s, 1, __func__);
1.42 nicm 441: }
1.66 nicm 442: }
443:
444: void
445: server_unzoom_window(struct window *w)
446: {
1.113 nicm 447: if (window_unzoom(w) == 0)
1.80 nicm 448: server_redraw_window(w);
1.1 nicm 449: }