xref: /netbsd-src/external/bsd/tmux/dist/server-fn.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /* $OpenBSD$ */
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/wait.h>
21 #include <sys/uio.h>
22 
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
27 
28 #include "tmux.h"
29 
30 static struct session	*server_next_session(struct session *);
31 static void		 server_destroy_session_group(struct session *);
32 
33 void
34 server_redraw_client(struct client *c)
35 {
36 	c->flags |= CLIENT_REDRAW;
37 }
38 
39 void
40 server_status_client(struct client *c)
41 {
42 	c->flags |= CLIENT_STATUS;
43 }
44 
45 void
46 server_redraw_session(struct session *s)
47 {
48 	struct client	*c;
49 
50 	TAILQ_FOREACH(c, &clients, entry) {
51 		if (c->session == s)
52 			server_redraw_client(c);
53 	}
54 }
55 
56 void
57 server_redraw_session_group(struct session *s)
58 {
59 	struct session_group	*sg;
60 
61 	if ((sg = session_group_contains(s)) == NULL)
62 		server_redraw_session(s);
63 	else {
64 		TAILQ_FOREACH(s, &sg->sessions, gentry)
65 			server_redraw_session(s);
66 	}
67 }
68 
69 void
70 server_status_session(struct session *s)
71 {
72 	struct client	*c;
73 
74 	TAILQ_FOREACH(c, &clients, entry) {
75 		if (c->session == s)
76 			server_status_client(c);
77 	}
78 }
79 
80 void
81 server_status_session_group(struct session *s)
82 {
83 	struct session_group	*sg;
84 
85 	if ((sg = session_group_contains(s)) == NULL)
86 		server_status_session(s);
87 	else {
88 		TAILQ_FOREACH(s, &sg->sessions, gentry)
89 			server_status_session(s);
90 	}
91 }
92 
93 void
94 server_redraw_window(struct window *w)
95 {
96 	struct client	*c;
97 
98 	TAILQ_FOREACH(c, &clients, entry) {
99 		if (c->session != NULL && c->session->curw->window == w)
100 			server_redraw_client(c);
101 	}
102 }
103 
104 void
105 server_redraw_window_borders(struct window *w)
106 {
107 	struct client	*c;
108 
109 	TAILQ_FOREACH(c, &clients, entry) {
110 		if (c->session != NULL && c->session->curw->window == w)
111 			c->flags |= CLIENT_BORDERS;
112 	}
113 }
114 
115 void
116 server_status_window(struct window *w)
117 {
118 	struct session	*s;
119 
120 	/*
121 	 * This is slightly different. We want to redraw the status line of any
122 	 * clients containing this window rather than anywhere it is the
123 	 * current window.
124 	 */
125 
126 	RB_FOREACH(s, sessions, &sessions) {
127 		if (session_has(s, w))
128 			server_status_session(s);
129 	}
130 }
131 
132 void
133 server_lock(void)
134 {
135 	struct client	*c;
136 
137 	TAILQ_FOREACH(c, &clients, entry) {
138 		if (c->session != NULL)
139 			server_lock_client(c);
140 	}
141 }
142 
143 void
144 server_lock_session(struct session *s)
145 {
146 	struct client	*c;
147 
148 	TAILQ_FOREACH(c, &clients, entry) {
149 		if (c->session == s)
150 			server_lock_client(c);
151 	}
152 }
153 
154 void
155 server_lock_client(struct client *c)
156 {
157 	const char	*cmd;
158 
159 	if (c->flags & CLIENT_CONTROL)
160 		return;
161 
162 	if (c->flags & CLIENT_SUSPENDED)
163 		return;
164 
165 	cmd = options_get_string(c->session->options, "lock-command");
166 	if (*cmd == '\0' || strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
167 		return;
168 
169 	tty_stop_tty(&c->tty);
170 	tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
171 	tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
172 	tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
173 
174 	c->flags |= CLIENT_SUSPENDED;
175 	proc_send(c->peer, MSG_LOCK, -1, cmd, strlen(cmd) + 1);
176 }
177 
178 void
179 server_kill_pane(struct window_pane *wp)
180 {
181 	struct window	*w = wp->window;
182 
183 	if (window_count_panes(w) == 1) {
184 		server_kill_window(w);
185 		recalculate_sizes();
186 	} else {
187 		server_unzoom_window(w);
188 		layout_close_pane(wp);
189 		window_remove_pane(w, wp);
190 		server_redraw_window(w);
191 	}
192 }
193 
194 void
195 server_kill_window(struct window *w)
196 {
197 	struct session		*s, *next_s, *target_s;
198 	struct session_group	*sg;
199 	struct winlink		*wl;
200 
201 	next_s = RB_MIN(sessions, &sessions);
202 	while (next_s != NULL) {
203 		s = next_s;
204 		next_s = RB_NEXT(sessions, &sessions, s);
205 
206 		if (!session_has(s, w))
207 			continue;
208 		server_unzoom_window(w);
209 		while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
210 			if (session_detach(s, wl)) {
211 				server_destroy_session_group(s);
212 				break;
213 			} else
214 				server_redraw_session_group(s);
215 		}
216 
217 		if (options_get_number(s->options, "renumber-windows")) {
218 			if ((sg = session_group_contains(s)) != NULL) {
219 				TAILQ_FOREACH(target_s, &sg->sessions, gentry)
220 					session_renumber_windows(target_s);
221 			} else
222 				session_renumber_windows(s);
223 		}
224 	}
225 	recalculate_sizes();
226 }
227 
228 int
229 server_link_window(struct session *src, struct winlink *srcwl,
230     struct session *dst, int dstidx, int killflag, int selectflag,
231     char **cause)
232 {
233 	struct winlink		*dstwl;
234 	struct session_group	*srcsg, *dstsg;
235 
236 	srcsg = session_group_contains(src);
237 	dstsg = session_group_contains(dst);
238 	if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
239 		xasprintf(cause, "sessions are grouped");
240 		return (-1);
241 	}
242 
243 	dstwl = NULL;
244 	if (dstidx != -1)
245 		dstwl = winlink_find_by_index(&dst->windows, dstidx);
246 	if (dstwl != NULL) {
247 		if (dstwl->window == srcwl->window) {
248 			xasprintf(cause, "same index: %d", dstidx);
249 			return (-1);
250 		}
251 		if (killflag) {
252 			/*
253 			 * Can't use session_detach as it will destroy session
254 			 * if this makes it empty.
255 			 */
256 			notify_session_window("window-unlinked", dst,
257 			    dstwl->window);
258 			dstwl->flags &= ~WINLINK_ALERTFLAGS;
259 			winlink_stack_remove(&dst->lastw, dstwl);
260 			winlink_remove(&dst->windows, dstwl);
261 
262 			/* Force select/redraw if current. */
263 			if (dstwl == dst->curw) {
264 				selectflag = 1;
265 				dst->curw = NULL;
266 			}
267 		}
268 	}
269 
270 	if (dstidx == -1)
271 		dstidx = -1 - options_get_number(dst->options, "base-index");
272 	dstwl = session_attach(dst, srcwl->window, dstidx, cause);
273 	if (dstwl == NULL)
274 		return (-1);
275 
276 	if (selectflag)
277 		session_select(dst, dstwl->idx);
278 	server_redraw_session_group(dst);
279 
280 	return (0);
281 }
282 
283 void
284 server_unlink_window(struct session *s, struct winlink *wl)
285 {
286 	if (session_detach(s, wl))
287 		server_destroy_session_group(s);
288 	else
289 		server_redraw_session_group(s);
290 }
291 
292 void
293 server_destroy_pane(struct window_pane *wp, int notify)
294 {
295 	struct window		*w = wp->window;
296 	struct screen_write_ctx	 ctx;
297 	struct grid_cell	 gc;
298 	time_t			 t;
299 	char			 tim[26];
300 
301 	if (wp->fd != -1) {
302 #ifdef HAVE_UTEMPTER
303 		utempter_remove_record(wp->fd);
304 #endif
305 		bufferevent_free(wp->event);
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);
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, __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 			status_timer_start(c);
414 			notify_client("client-session-changed", c);
415 			session_update_activity(s_new, NULL);
416 			gettimeofday(&s_new->last_attached_time, NULL);
417 			server_redraw_client(c);
418 			alerts_check_session(s_new);
419 		}
420 	}
421 	recalculate_sizes();
422 }
423 
424 void
425 server_check_unattached(void)
426 {
427 	struct session	*s;
428 
429 	/*
430 	 * If any sessions are no longer attached and have destroy-unattached
431 	 * set, collect them.
432 	 */
433 	RB_FOREACH(s, sessions, &sessions) {
434 		if (!(s->flags & SESSION_UNATTACHED))
435 			continue;
436 		if (options_get_number (s->options, "destroy-unattached"))
437 			session_destroy(s, __func__);
438 	}
439 }
440 
441 /* Set stdin callback. */
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 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