xref: /openbsd-src/usr.bin/tmux/server-fn.c (revision 4b70baf6e17fc8b27fc1f7fa7929335753fa94c3)
1 /* $OpenBSD: server-fn.c,v 1.121 2019/05/03 20:44:24 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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>
20 #include <sys/queue.h>
21 #include <sys/wait.h>
22 #include <sys/uio.h>
23 
24 #include <imsg.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <unistd.h>
29 
30 #include "tmux.h"
31 
32 static struct session	*server_next_session(struct session *);
33 static void		 server_destroy_session_group(struct session *);
34 
35 void
36 server_redraw_client(struct client *c)
37 {
38 	c->flags |= CLIENT_ALLREDRAWFLAGS;
39 }
40 
41 void
42 server_status_client(struct client *c)
43 {
44 	c->flags |= CLIENT_REDRAWSTATUS;
45 }
46 
47 void
48 server_redraw_session(struct session *s)
49 {
50 	struct client	*c;
51 
52 	TAILQ_FOREACH(c, &clients, entry) {
53 		if (c->session == s)
54 			server_redraw_client(c);
55 	}
56 }
57 
58 void
59 server_redraw_session_group(struct session *s)
60 {
61 	struct session_group	*sg;
62 
63 	if ((sg = session_group_contains(s)) == NULL)
64 		server_redraw_session(s);
65 	else {
66 		TAILQ_FOREACH(s, &sg->sessions, gentry)
67 			server_redraw_session(s);
68 	}
69 }
70 
71 void
72 server_status_session(struct session *s)
73 {
74 	struct client	*c;
75 
76 	TAILQ_FOREACH(c, &clients, entry) {
77 		if (c->session == s)
78 			server_status_client(c);
79 	}
80 }
81 
82 void
83 server_status_session_group(struct session *s)
84 {
85 	struct session_group	*sg;
86 
87 	if ((sg = session_group_contains(s)) == NULL)
88 		server_status_session(s);
89 	else {
90 		TAILQ_FOREACH(s, &sg->sessions, gentry)
91 			server_status_session(s);
92 	}
93 }
94 
95 void
96 server_redraw_window(struct window *w)
97 {
98 	struct client	*c;
99 
100 	TAILQ_FOREACH(c, &clients, entry) {
101 		if (c->session != NULL && c->session->curw->window == w)
102 			server_redraw_client(c);
103 	}
104 }
105 
106 void
107 server_redraw_window_borders(struct window *w)
108 {
109 	struct client	*c;
110 
111 	TAILQ_FOREACH(c, &clients, entry) {
112 		if (c->session != NULL && c->session->curw->window == w)
113 			c->flags |= CLIENT_REDRAWBORDERS;
114 	}
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
124 	 * clients containing this window rather than anywhere it is the
125 	 * current window.
126 	 */
127 
128 	RB_FOREACH(s, sessions, &sessions) {
129 		if (session_has(s, w))
130 			server_status_session(s);
131 	}
132 }
133 
134 void
135 server_lock(void)
136 {
137 	struct client	*c;
138 
139 	TAILQ_FOREACH(c, &clients, entry) {
140 		if (c->session != NULL)
141 			server_lock_client(c);
142 	}
143 }
144 
145 void
146 server_lock_session(struct session *s)
147 {
148 	struct client	*c;
149 
150 	TAILQ_FOREACH(c, &clients, entry) {
151 		if (c->session == s)
152 			server_lock_client(c);
153 	}
154 }
155 
156 void
157 server_lock_client(struct client *c)
158 {
159 	const char	*cmd;
160 
161 	if (c->flags & CLIENT_CONTROL)
162 		return;
163 
164 	if (c->flags & CLIENT_SUSPENDED)
165 		return;
166 
167 	cmd = options_get_string(c->session->options, "lock-command");
168 	if (*cmd == '\0' || strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
169 		return;
170 
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));
174 	tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
175 
176 	c->flags |= CLIENT_SUSPENDED;
177 	proc_send(c->peer, MSG_LOCK, -1, cmd, strlen(cmd) + 1);
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);
190 		layout_close_pane(wp);
191 		window_remove_pane(w, wp);
192 		server_redraw_window(w);
193 	}
194 }
195 
196 void
197 server_kill_window(struct window *w)
198 {
199 	struct session		*s, *next_s, *target_s;
200 	struct session_group	*sg;
201 	struct winlink		*wl;
202 
203 	next_s = RB_MIN(sessions, &sessions);
204 	while (next_s != NULL) {
205 		s = next_s;
206 		next_s = RB_NEXT(sessions, &sessions, s);
207 
208 		if (!session_has(s, w))
209 			continue;
210 		server_unzoom_window(w);
211 		while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
212 			if (session_detach(s, wl)) {
213 				server_destroy_session_group(s);
214 				break;
215 			} else
216 				server_redraw_session_group(s);
217 		}
218 
219 		if (options_get_number(s->options, "renumber-windows")) {
220 			if ((sg = session_group_contains(s)) != NULL) {
221 				TAILQ_FOREACH(target_s, &sg->sessions, gentry)
222 					session_renumber_windows(target_s);
223 			} else
224 				session_renumber_windows(s);
225 		}
226 	}
227 	recalculate_sizes();
228 }
229 
230 int
231 server_link_window(struct session *src, struct winlink *srcwl,
232     struct session *dst, int dstidx, int killflag, int selectflag,
233     char **cause)
234 {
235 	struct winlink		*dstwl;
236 	struct session_group	*srcsg, *dstsg;
237 
238 	srcsg = session_group_contains(src);
239 	dstsg = session_group_contains(dst);
240 	if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
241 		xasprintf(cause, "sessions are grouped");
242 		return (-1);
243 	}
244 
245 	dstwl = NULL;
246 	if (dstidx != -1)
247 		dstwl = winlink_find_by_index(&dst->windows, dstidx);
248 	if (dstwl != NULL) {
249 		if (dstwl->window == srcwl->window) {
250 			xasprintf(cause, "same index: %d", dstidx);
251 			return (-1);
252 		}
253 		if (killflag) {
254 			/*
255 			 * Can't use session_detach as it will destroy session
256 			 * if this makes it empty.
257 			 */
258 			notify_session_window("window-unlinked", dst,
259 			    dstwl->window);
260 			dstwl->flags &= ~WINLINK_ALERTFLAGS;
261 			winlink_stack_remove(&dst->lastw, dstwl);
262 			winlink_remove(&dst->windows, dstwl);
263 
264 			/* Force select/redraw if current. */
265 			if (dstwl == dst->curw) {
266 				selectflag = 1;
267 				dst->curw = NULL;
268 			}
269 		}
270 	}
271 
272 	if (dstidx == -1)
273 		dstidx = -1 - options_get_number(dst->options, "base-index");
274 	dstwl = session_attach(dst, srcwl->window, dstidx, cause);
275 	if (dstwl == NULL)
276 		return (-1);
277 
278 	if (selectflag)
279 		session_select(dst, dstwl->idx);
280 	server_redraw_session_group(dst);
281 
282 	return (0);
283 }
284 
285 void
286 server_unlink_window(struct session *s, struct winlink *wl)
287 {
288 	if (session_detach(s, wl))
289 		server_destroy_session_group(s);
290 	else
291 		server_redraw_session_group(s);
292 }
293 
294 void
295 server_destroy_pane(struct window_pane *wp, int notify)
296 {
297 	struct window		*w = wp->window;
298 	struct screen_write_ctx	 ctx;
299 	struct grid_cell	 gc;
300 	time_t			 t;
301 	char			 tim[26];
302 
303 	if (wp->fd != -1) {
304 		bufferevent_free(wp->event);
305 		wp->event = NULL;
306 		close(wp->fd);
307 		wp->fd = -1;
308 	}
309 
310 	if (options_get_number(w->options, "remain-on-exit")) {
311 		if (~wp->flags & PANE_STATUSREADY)
312 			return;
313 
314 		if (wp->flags & PANE_STATUSDRAWN)
315 			return;
316 		wp->flags |= PANE_STATUSDRAWN;
317 
318 		if (notify)
319 			notify_pane("pane-died", wp);
320 
321 		screen_write_start(&ctx, wp, &wp->base);
322 		screen_write_scrollregion(&ctx, 0, screen_size_y(ctx.s) - 1);
323 		screen_write_cursormove(&ctx, 0, screen_size_y(ctx.s) - 1, 0);
324 		screen_write_linefeed(&ctx, 1, 8);
325 		memcpy(&gc, &grid_default_cell, sizeof gc);
326 
327 		time(&t);
328 		ctime_r(&t, tim);
329 
330 		if (WIFEXITED(wp->status)) {
331 			screen_write_nputs(&ctx, -1, &gc,
332 			    "Pane is dead (status %d, %s)",
333 			    WEXITSTATUS(wp->status),
334 			    tim);
335 		} else if (WIFSIGNALED(wp->status)) {
336 			screen_write_nputs(&ctx, -1, &gc,
337 			    "Pane is dead (signal %d, %s)",
338 			    WTERMSIG(wp->status),
339 			    tim);
340 		}
341 
342 		screen_write_stop(&ctx);
343 		wp->flags |= PANE_REDRAW;
344 		return;
345 	}
346 
347 	if (notify)
348 		notify_pane("pane-exited", wp);
349 
350 	server_unzoom_window(w);
351 	layout_close_pane(wp);
352 	window_remove_pane(w, wp);
353 
354 	if (TAILQ_EMPTY(&w->panes))
355 		server_kill_window(w);
356 	else
357 		server_redraw_window(w);
358 }
359 
360 static void
361 server_destroy_session_group(struct session *s)
362 {
363 	struct session_group	*sg;
364 	struct session		*s1;
365 
366 	if ((sg = session_group_contains(s)) == NULL)
367 		server_destroy_session(s);
368 	else {
369 		TAILQ_FOREACH_SAFE(s, &sg->sessions, gentry, s1) {
370 			server_destroy_session(s);
371 			session_destroy(s, 1, __func__);
372 		}
373 	}
374 }
375 
376 static struct session *
377 server_next_session(struct session *s)
378 {
379 	struct session *s_loop, *s_out;
380 
381 	s_out = NULL;
382 	RB_FOREACH(s_loop, sessions, &sessions) {
383 		if (s_loop == s)
384 			continue;
385 		if (s_out == NULL ||
386 		    timercmp(&s_loop->activity_time, &s_out->activity_time, <))
387 			s_out = s_loop;
388 	}
389 	return (s_out);
390 }
391 
392 void
393 server_destroy_session(struct session *s)
394 {
395 	struct client	*c;
396 	struct session	*s_new;
397 
398 	if (!options_get_number(s->options, "detach-on-destroy"))
399 		s_new = server_next_session(s);
400 	else
401 		s_new = NULL;
402 
403 	TAILQ_FOREACH(c, &clients, entry) {
404 		if (c->session != s)
405 			continue;
406 		if (s_new == NULL) {
407 			c->session = NULL;
408 			c->flags |= CLIENT_EXIT;
409 		} else {
410 			c->last_session = NULL;
411 			c->session = s_new;
412 			server_client_set_key_table(c, NULL);
413 			tty_update_client_offset(c);
414 			status_timer_start(c);
415 			notify_client("client-session-changed", c);
416 			session_update_activity(s_new, NULL);
417 			gettimeofday(&s_new->last_attached_time, NULL);
418 			server_redraw_client(c);
419 			alerts_check_session(s_new);
420 		}
421 	}
422 	recalculate_sizes();
423 }
424 
425 void
426 server_check_unattached(void)
427 {
428 	struct session	*s;
429 
430 	/*
431 	 * If any sessions are no longer attached and have destroy-unattached
432 	 * set, collect them.
433 	 */
434 	RB_FOREACH(s, sessions, &sessions) {
435 		if (s->attached != 0)
436 			continue;
437 		if (options_get_number (s->options, "destroy-unattached"))
438 			session_destroy(s, 1, __func__);
439 	}
440 }
441 
442 int
443 server_set_stdin_callback(struct client *c, void (*cb)(struct client *, int,
444     void *), void *cb_data, char **cause)
445 {
446 	if (c == NULL || c->session != NULL) {
447 		*cause = xstrdup("no client with stdin");
448 		return (-1);
449 	}
450 	if (c->flags & CLIENT_TERMINAL) {
451 		*cause = xstrdup("stdin is a tty");
452 		return (-1);
453 	}
454 	if (c->stdin_callback != NULL) {
455 		*cause = xstrdup("stdin is in use");
456 		return (-1);
457 	}
458 
459 	c->stdin_callback_data = cb_data;
460 	c->stdin_callback = cb;
461 
462 	c->references++;
463 
464 	if (c->stdin_closed)
465 		c->stdin_callback(c, 1, c->stdin_callback_data);
466 
467 	proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
468 
469 	return (0);
470 }
471 
472 void
473 server_unzoom_window(struct window *w)
474 {
475 	if (window_unzoom(w) == 0)
476 		server_redraw_window(w);
477 }
478