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/uio.h>
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 #include <unistd.h>
26
27 #include "tmux.h"
28
29 struct session *server_next_session(struct session *);
30 void server_callback_identify(int, short, void *);
31
32 void
server_fill_environ(struct session * s,struct environ * env)33 server_fill_environ(struct session *s, struct environ *env)
34 {
35 char *term;
36 u_int idx;
37 long pid;
38
39 if (s != NULL) {
40 term = options_get_string(global_options, "default-terminal");
41 environ_set(env, "TERM", "%s", term);
42
43 idx = s->id;
44 } else
45 idx = (u_int)-1;
46 pid = getpid();
47 environ_set(env, "TMUX", "%s,%ld,%u", socket_path, pid, idx);
48 }
49
50 void
server_redraw_client(struct client * c)51 server_redraw_client(struct client *c)
52 {
53 c->flags |= CLIENT_REDRAW;
54 }
55
56 void
server_status_client(struct client * c)57 server_status_client(struct client *c)
58 {
59 c->flags |= CLIENT_STATUS;
60 }
61
62 void
server_redraw_session(struct session * s)63 server_redraw_session(struct session *s)
64 {
65 struct client *c;
66
67 TAILQ_FOREACH(c, &clients, entry) {
68 if (c->session == s)
69 server_redraw_client(c);
70 }
71 }
72
73 void
server_redraw_session_group(struct session * s)74 server_redraw_session_group(struct session *s)
75 {
76 struct session_group *sg;
77
78 if ((sg = session_group_find(s)) == NULL)
79 server_redraw_session(s);
80 else {
81 TAILQ_FOREACH(s, &sg->sessions, gentry)
82 server_redraw_session(s);
83 }
84 }
85
86 void
server_status_session(struct session * s)87 server_status_session(struct session *s)
88 {
89 struct client *c;
90
91 TAILQ_FOREACH(c, &clients, entry) {
92 if (c->session == s)
93 server_status_client(c);
94 }
95 }
96
97 void
server_status_session_group(struct session * s)98 server_status_session_group(struct session *s)
99 {
100 struct session_group *sg;
101
102 if ((sg = session_group_find(s)) == NULL)
103 server_status_session(s);
104 else {
105 TAILQ_FOREACH(s, &sg->sessions, gentry)
106 server_status_session(s);
107 }
108 }
109
110 void
server_redraw_window(struct window * w)111 server_redraw_window(struct window *w)
112 {
113 struct client *c;
114
115 TAILQ_FOREACH(c, &clients, entry) {
116 if (c->session != NULL && c->session->curw->window == w)
117 server_redraw_client(c);
118 }
119 w->flags |= WINDOW_REDRAW;
120 }
121
122 void
server_redraw_window_borders(struct window * w)123 server_redraw_window_borders(struct window *w)
124 {
125 struct client *c;
126
127 TAILQ_FOREACH(c, &clients, entry) {
128 if (c->session != NULL && c->session->curw->window == w)
129 c->flags |= CLIENT_BORDERS;
130 }
131 }
132
133 void
server_status_window(struct window * w)134 server_status_window(struct window *w)
135 {
136 struct session *s;
137
138 /*
139 * This is slightly different. We want to redraw the status line of any
140 * clients containing this window rather than anywhere it is the
141 * current window.
142 */
143
144 RB_FOREACH(s, sessions, &sessions) {
145 if (session_has(s, w))
146 server_status_session(s);
147 }
148 }
149
150 void
server_lock(void)151 server_lock(void)
152 {
153 struct client *c;
154
155 TAILQ_FOREACH(c, &clients, entry) {
156 if (c->session != NULL)
157 server_lock_client(c);
158 }
159 }
160
161 void
server_lock_session(struct session * s)162 server_lock_session(struct session *s)
163 {
164 struct client *c;
165
166 TAILQ_FOREACH(c, &clients, entry) {
167 if (c->session == s)
168 server_lock_client(c);
169 }
170 }
171
172 void
server_lock_client(struct client * c)173 server_lock_client(struct client *c)
174 {
175 const char *cmd;
176
177 if (c->flags & CLIENT_CONTROL)
178 return;
179
180 if (c->flags & CLIENT_SUSPENDED)
181 return;
182
183 cmd = options_get_string(c->session->options, "lock-command");
184 if (strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
185 return;
186
187 tty_stop_tty(&c->tty);
188 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
189 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
190 tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
191
192 c->flags |= CLIENT_SUSPENDED;
193 proc_send_s(c->peer, MSG_LOCK, cmd);
194 }
195
196 void
server_kill_window(struct window * w)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_find(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
server_link_window(struct session * src,struct winlink * srcwl,struct session * dst,int dstidx,int killflag,int selectflag,char ** cause)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_find(src);
239 dstsg = session_group_find(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_window_unlinked(dst, dstwl->window);
259 dstwl->flags &= ~WINLINK_ALERTFLAGS;
260 winlink_stack_remove(&dst->lastw, dstwl);
261 winlink_remove(&dst->windows, dstwl);
262
263 /* Force select/redraw if current. */
264 if (dstwl == dst->curw) {
265 selectflag = 1;
266 dst->curw = NULL;
267 }
268 }
269 }
270
271 if (dstidx == -1)
272 dstidx = -1 - options_get_number(dst->options, "base-index");
273 dstwl = session_attach(dst, srcwl->window, dstidx, cause);
274 if (dstwl == NULL)
275 return (-1);
276
277 if (selectflag)
278 session_select(dst, dstwl->idx);
279 server_redraw_session_group(dst);
280
281 return (0);
282 }
283
284 void
server_unlink_window(struct session * s,struct winlink * wl)285 server_unlink_window(struct session *s, struct winlink *wl)
286 {
287 if (session_detach(s, wl))
288 server_destroy_session_group(s);
289 else
290 server_redraw_session_group(s);
291 }
292
293 void
server_destroy_pane(struct window_pane * wp,int hooks)294 server_destroy_pane(struct window_pane *wp, int hooks)
295 {
296 struct window *w = wp->window;
297 #ifndef TMATE
298 int old_fd;
299 struct screen_write_ctx ctx;
300 struct grid_cell gc;
301 struct cmd_find_state fs;
302
303 old_fd = wp->fd;
304 if (wp->fd != -1) {
305 #ifdef HAVE_UTEMPTER
306 utempter_remove_record(wp->fd);
307 #endif
308 bufferevent_free(wp->event);
309 close(wp->fd);
310 wp->fd = -1;
311 }
312
313 if (options_get_number(w->options, "remain-on-exit")) {
314 if (old_fd == -1)
315 return;
316 screen_write_start(&ctx, wp, &wp->base);
317 screen_write_scrollregion(&ctx, 0, screen_size_y(ctx.s) - 1);
318 screen_write_cursormove(&ctx, 0, screen_size_y(ctx.s) - 1);
319 screen_write_linefeed(&ctx, 1);
320 memcpy(&gc, &grid_default_cell, sizeof gc);
321 gc.attr |= GRID_ATTR_BRIGHT;
322 screen_write_puts(&ctx, &gc, "Pane is dead");
323 screen_write_stop(&ctx);
324 wp->flags |= PANE_REDRAW;
325
326 if (hooks && cmd_find_from_pane(&fs, wp) == 0)
327 hooks_run(hooks_get(fs.s), NULL, &fs, "pane-died");
328 return;
329 }
330 #endif
331
332 server_unzoom_window(w);
333 #ifndef TMATE
334 layout_close_pane(wp);
335 window_remove_pane(w, wp);
336
337 if (hooks && cmd_find_from_window(&fs, w) == 0)
338 hooks_run(hooks_get(fs.s), NULL, &fs, "pane-exited");
339 #endif
340
341 if (TAILQ_EMPTY(&w->panes))
342 server_kill_window(w);
343 else
344 server_redraw_window(w);
345 }
346
347 void
server_destroy_session_group(struct session * s)348 server_destroy_session_group(struct session *s)
349 {
350 struct session_group *sg;
351 struct session *s1;
352
353 if ((sg = session_group_find(s)) == NULL)
354 server_destroy_session(s);
355 else {
356 TAILQ_FOREACH_SAFE(s, &sg->sessions, gentry, s1) {
357 server_destroy_session(s);
358 session_destroy(s);
359 }
360 }
361 }
362
363 struct session *
server_next_session(struct session * s)364 server_next_session(struct session *s)
365 {
366 struct session *s_loop, *s_out;
367
368 s_out = NULL;
369 RB_FOREACH(s_loop, sessions, &sessions) {
370 if (s_loop == s)
371 continue;
372 if (s_out == NULL ||
373 timercmp(&s_loop->activity_time, &s_out->activity_time, <))
374 s_out = s_loop;
375 }
376 return (s_out);
377 }
378
379 void
server_destroy_session(struct session * s)380 server_destroy_session(struct session *s)
381 {
382 struct client *c;
383 struct session *s_new;
384
385 if (!options_get_number(s->options, "detach-on-destroy"))
386 s_new = server_next_session(s);
387 else
388 s_new = NULL;
389
390 TAILQ_FOREACH(c, &clients, entry) {
391 if (c->session != s)
392 continue;
393 if (s_new == NULL) {
394 c->session = NULL;
395 c->flags |= CLIENT_EXIT;
396 } else {
397 c->last_session = NULL;
398 c->session = s_new;
399 server_client_set_key_table(c, NULL);
400 status_timer_start(c);
401 notify_attached_session_changed(c);
402 session_update_activity(s_new, NULL);
403 gettimeofday(&s_new->last_attached_time, NULL);
404 server_redraw_client(c);
405 alerts_check_session(s_new);
406 }
407 }
408 recalculate_sizes();
409 }
410
411 void
server_check_unattached(void)412 server_check_unattached(void)
413 {
414 struct session *s;
415
416 /*
417 * If any sessions are no longer attached and have destroy-unattached
418 * set, collect them.
419 */
420 RB_FOREACH(s, sessions, &sessions) {
421 if (!(s->flags & SESSION_UNATTACHED))
422 continue;
423 if (options_get_number (s->options, "destroy-unattached"))
424 session_destroy(s);
425 }
426 }
427
428 void
server_set_identify(struct client * c)429 server_set_identify(struct client *c)
430 {
431 struct timeval tv;
432 int delay;
433
434 delay = options_get_number(c->session->options, "display-panes-time");
435 tv.tv_sec = delay / 1000;
436 tv.tv_usec = (delay % 1000) * 1000L;
437
438 if (event_initialized(&c->identify_timer))
439 evtimer_del(&c->identify_timer);
440 evtimer_set(&c->identify_timer, server_callback_identify, c);
441 evtimer_add(&c->identify_timer, &tv);
442
443 c->flags |= CLIENT_IDENTIFY;
444 c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
445 server_redraw_client(c);
446 }
447
448 void
server_clear_identify(struct client * c)449 server_clear_identify(struct client *c)
450 {
451 if (c->flags & CLIENT_IDENTIFY) {
452 c->flags &= ~CLIENT_IDENTIFY;
453 c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
454 server_redraw_client(c);
455 }
456 }
457
458 void
server_callback_identify(__unused int fd,__unused short events,void * data)459 server_callback_identify(__unused int fd, __unused short events, void *data)
460 {
461 struct client *c = data;
462
463 server_clear_identify(c);
464 }
465
466 /* Set stdin callback. */
467 int
server_set_stdin_callback(struct client * c,void (* cb)(struct client *,int,void *),void * cb_data,char ** cause)468 server_set_stdin_callback(struct client *c, void (*cb)(struct client *, int,
469 void *), void *cb_data, char **cause)
470 {
471 if (c == NULL || c->session != NULL) {
472 *cause = xstrdup("no client with stdin");
473 return (-1);
474 }
475 if (c->flags & CLIENT_TERMINAL) {
476 *cause = xstrdup("stdin is a tty");
477 return (-1);
478 }
479 if (c->stdin_callback != NULL) {
480 *cause = xstrdup("stdin in use");
481 return (-1);
482 }
483
484 c->stdin_callback_data = cb_data;
485 c->stdin_callback = cb;
486
487 c->references++;
488
489 if (c->stdin_closed)
490 c->stdin_callback(c, 1, c->stdin_callback_data);
491
492 proc_send(c->peer, MSG_STDIN, -1, NULL, 0);
493
494 return (0);
495 }
496
497 void
server_unzoom_window(struct window * w)498 server_unzoom_window(struct window *w)
499 {
500 if (window_unzoom(w) == 0) {
501 server_redraw_window(w);
502 server_status_window(w);
503 }
504 }
505