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
server_redraw_client(struct client * c)34 server_redraw_client(struct client *c)
35 {
36 c->flags |= CLIENT_ALLREDRAWFLAGS;
37 }
38
39 void
server_status_client(struct client * c)40 server_status_client(struct client *c)
41 {
42 c->flags |= CLIENT_REDRAWSTATUS;
43 }
44
45 void
server_redraw_session(struct session * s)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
server_redraw_session_group(struct session * s)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
server_status_session(struct session * s)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
server_status_session_group(struct session * s)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
server_redraw_window(struct window * w)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
server_redraw_window_borders(struct window * w)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_REDRAWBORDERS;
112 }
113 }
114
115 void
server_status_window(struct window * w)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
server_lock(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
server_lock_session(struct session * s)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
server_lock_client(struct client * c)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
server_kill_pane(struct window_pane * wp)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, 1);
185 recalculate_sizes();
186 } else {
187 server_unzoom_window(w);
188 server_client_remove_pane(wp);
189 layout_close_pane(wp);
190 window_remove_pane(w, wp);
191 server_redraw_window(w);
192 }
193 }
194
195 void
server_kill_window(struct window * w,int renumber)196 server_kill_window(struct window *w, int renumber)
197 {
198 struct session *s, *s1;
199 struct winlink *wl;
200
201 RB_FOREACH_SAFE(s, sessions, &sessions, s1) {
202 if (!session_has(s, w))
203 continue;
204
205 server_unzoom_window(w);
206 while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
207 if (session_detach(s, wl)) {
208 server_destroy_session_group(s);
209 break;
210 } else
211 server_redraw_session_group(s);
212 }
213
214 if (renumber)
215 server_renumber_session(s);
216 }
217 recalculate_sizes();
218 }
219
220 void
server_renumber_session(struct session * s)221 server_renumber_session(struct session *s)
222 {
223 struct session_group *sg;
224
225 if (options_get_number(s->options, "renumber-windows")) {
226 if ((sg = session_group_contains(s)) != NULL) {
227 TAILQ_FOREACH(s, &sg->sessions, gentry)
228 session_renumber_windows(s);
229 } else
230 session_renumber_windows(s);
231 }
232 }
233
234 void
server_renumber_all(void)235 server_renumber_all(void)
236 {
237 struct session *s;
238
239 RB_FOREACH(s, sessions, &sessions)
240 server_renumber_session(s);
241 }
242
243 int
server_link_window(struct session * src,struct winlink * srcwl,struct session * dst,int dstidx,int killflag,int selectflag,char ** cause)244 server_link_window(struct session *src, struct winlink *srcwl,
245 struct session *dst, int dstidx, int killflag, int selectflag,
246 char **cause)
247 {
248 struct winlink *dstwl;
249 struct session_group *srcsg, *dstsg;
250
251 srcsg = session_group_contains(src);
252 dstsg = session_group_contains(dst);
253 if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
254 xasprintf(cause, "sessions are grouped");
255 return (-1);
256 }
257
258 dstwl = NULL;
259 if (dstidx != -1)
260 dstwl = winlink_find_by_index(&dst->windows, dstidx);
261 if (dstwl != NULL) {
262 if (dstwl->window == srcwl->window) {
263 xasprintf(cause, "same index: %d", dstidx);
264 return (-1);
265 }
266 if (killflag) {
267 /*
268 * Can't use session_detach as it will destroy session
269 * if this makes it empty.
270 */
271 notify_session_window("window-unlinked", dst,
272 dstwl->window);
273 dstwl->flags &= ~WINLINK_ALERTFLAGS;
274 winlink_stack_remove(&dst->lastw, dstwl);
275 winlink_remove(&dst->windows, dstwl);
276
277 /* Force select/redraw if current. */
278 if (dstwl == dst->curw) {
279 selectflag = 1;
280 dst->curw = NULL;
281 }
282 }
283 }
284
285 if (dstidx == -1)
286 dstidx = -1 - options_get_number(dst->options, "base-index");
287 dstwl = session_attach(dst, srcwl->window, dstidx, cause);
288 if (dstwl == NULL)
289 return (-1);
290
291 if (selectflag)
292 session_select(dst, dstwl->idx);
293 server_redraw_session_group(dst);
294
295 return (0);
296 }
297
298 void
server_unlink_window(struct session * s,struct winlink * wl)299 server_unlink_window(struct session *s, struct winlink *wl)
300 {
301 if (session_detach(s, wl))
302 server_destroy_session_group(s);
303 else
304 server_redraw_session_group(s);
305 }
306
307 void
server_destroy_pane(struct window_pane * wp,int notify)308 server_destroy_pane(struct window_pane *wp, int notify)
309 {
310 struct window *w = wp->window;
311 struct screen_write_ctx ctx;
312 struct grid_cell gc;
313 time_t t;
314 char tim[26];
315 int remain_on_exit;
316
317 if (wp->fd != -1) {
318 #ifdef HAVE_UTEMPTER
319 utempter_remove_record(wp->fd);
320 #endif
321 bufferevent_free(wp->event);
322 wp->event = NULL;
323 close(wp->fd);
324 wp->fd = -1;
325 }
326
327 remain_on_exit = options_get_number(wp->options, "remain-on-exit");
328 if (remain_on_exit != 0 && (~wp->flags & PANE_STATUSREADY))
329 return;
330 switch (remain_on_exit) {
331 case 0:
332 break;
333 case 2:
334 if (WIFEXITED(wp->status) && WEXITSTATUS(wp->status) == 0)
335 break;
336 /* FALLTHROUGH */
337 case 1:
338 if (wp->flags & PANE_STATUSDRAWN)
339 return;
340 wp->flags |= PANE_STATUSDRAWN;
341
342 if (notify)
343 notify_pane("pane-died", wp);
344
345 screen_write_start_pane(&ctx, wp, &wp->base);
346 screen_write_scrollregion(&ctx, 0, screen_size_y(ctx.s) - 1);
347 screen_write_cursormove(&ctx, 0, screen_size_y(ctx.s) - 1, 0);
348 screen_write_linefeed(&ctx, 1, 8);
349 memcpy(&gc, &grid_default_cell, sizeof gc);
350
351 time(&t);
352 ctime_r(&t, tim);
353 tim[strcspn(tim, "\n")] = '\0';
354
355 if (WIFEXITED(wp->status)) {
356 screen_write_nputs(&ctx, -1, &gc,
357 "Pane is dead (status %d, %s)",
358 WEXITSTATUS(wp->status),
359 tim);
360 } else if (WIFSIGNALED(wp->status)) {
361 screen_write_nputs(&ctx, -1, &gc,
362 "Pane is dead (signal %s, %s)",
363 sig2name(WTERMSIG(wp->status)),
364 tim);
365 }
366
367 screen_write_stop(&ctx);
368 wp->flags |= PANE_REDRAW;
369 return;
370 }
371
372 if (notify)
373 notify_pane("pane-exited", wp);
374
375 server_unzoom_window(w);
376 server_client_remove_pane(wp);
377 layout_close_pane(wp);
378 window_remove_pane(w, wp);
379
380 if (TAILQ_EMPTY(&w->panes))
381 server_kill_window(w, 1);
382 else
383 server_redraw_window(w);
384 }
385
386 static void
server_destroy_session_group(struct session * s)387 server_destroy_session_group(struct session *s)
388 {
389 struct session_group *sg;
390 struct session *s1;
391
392 if ((sg = session_group_contains(s)) == NULL)
393 server_destroy_session(s);
394 else {
395 TAILQ_FOREACH_SAFE(s, &sg->sessions, gentry, s1) {
396 server_destroy_session(s);
397 session_destroy(s, 1, __func__);
398 }
399 }
400 }
401
402 static struct session *
server_next_session(struct session * s)403 server_next_session(struct session *s)
404 {
405 struct session *s_loop, *s_out = NULL;
406
407 RB_FOREACH(s_loop, sessions, &sessions) {
408 if (s_loop == s)
409 continue;
410 if (s_out == NULL ||
411 timercmp(&s_loop->activity_time, &s_out->activity_time, <))
412 s_out = s_loop;
413 }
414 return (s_out);
415 }
416
417 static struct session *
server_next_detached_session(struct session * s)418 server_next_detached_session(struct session *s)
419 {
420 struct session *s_loop, *s_out = NULL;
421
422 RB_FOREACH(s_loop, sessions, &sessions) {
423 if (s_loop == s || s_loop->attached)
424 continue;
425 if (s_out == NULL ||
426 timercmp(&s_loop->activity_time, &s_out->activity_time, <))
427 s_out = s_loop;
428 }
429 return (s_out);
430 }
431
432 void
server_destroy_session(struct session * s)433 server_destroy_session(struct session *s)
434 {
435 struct client *c;
436 struct session *s_new;
437 int detach_on_destroy;
438
439 detach_on_destroy = options_get_number(s->options, "detach-on-destroy");
440 if (detach_on_destroy == 0)
441 s_new = server_next_session(s);
442 else if (detach_on_destroy == 2)
443 s_new = server_next_detached_session(s);
444 else
445 s_new = NULL;
446 TAILQ_FOREACH(c, &clients, entry) {
447 if (c->session != s)
448 continue;
449 if (s_new == NULL) {
450 c->session = NULL;
451 c->flags |= CLIENT_EXIT;
452 } else {
453 c->last_session = NULL;
454 c->session = s_new;
455 server_client_set_key_table(c, NULL);
456 tty_update_client_offset(c);
457 status_timer_start(c);
458 notify_client("client-session-changed", c);
459 session_update_activity(s_new, NULL);
460 gettimeofday(&s_new->last_attached_time, NULL);
461 server_redraw_client(c);
462 alerts_check_session(s_new);
463 }
464 }
465 recalculate_sizes();
466 }
467
468 void
server_check_unattached(void)469 server_check_unattached(void)
470 {
471 struct session *s;
472
473 /*
474 * If any sessions are no longer attached and have destroy-unattached
475 * set, collect them.
476 */
477 RB_FOREACH(s, sessions, &sessions) {
478 if (s->attached != 0)
479 continue;
480 if (options_get_number (s->options, "destroy-unattached"))
481 session_destroy(s, 1, __func__);
482 }
483 }
484
485 void
server_unzoom_window(struct window * w)486 server_unzoom_window(struct window *w)
487 {
488 if (window_unzoom(w) == 0)
489 server_redraw_window(w);
490 }
491