Annotation of src/usr.bin/tmux/server-fn.c, Revision 1.77
1.77 ! nicm 1: /* $OpenBSD: server-fn.c,v 1.76 2014/04/17 14:45:49 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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:
1.60 nicm 21: #include <stdlib.h>
1.1 nicm 22: #include <string.h>
1.5 nicm 23: #include <time.h>
1.1 nicm 24: #include <unistd.h>
25:
26: #include "tmux.h"
27:
1.39 nicm 28: struct session *server_next_session(struct session *);
29: void server_callback_identify(int, short, void *);
1.28 nicm 30:
1.13 nicm 31: void
32: server_fill_environ(struct session *s, struct environ *env)
1.1 nicm 33: {
1.77 ! nicm 34: char var[PATH_MAX], *term;
1.50 nicm 35: u_int idx;
36: long pid;
37:
38: if (s != NULL) {
39: term = options_get_string(&s->options, "default-terminal");
40: environ_set(env, "TERM", term);
41:
1.67 nicm 42: idx = s->id;
1.50 nicm 43: } else
44: idx = -1;
45: pid = getpid();
46: xsnprintf(var, sizeof var, "%s,%ld,%d", socket_path, pid, idx);
47: environ_set(env, "TMUX", var);
1.58 nicm 48: }
49:
50: void
51: server_write_ready(struct client *c)
52: {
1.59 nicm 53: if (c->flags & CLIENT_CONTROL)
54: return;
1.58 nicm 55: server_write_client(c, MSG_READY, NULL, 0);
1.10 nicm 56: }
57:
1.57 nicm 58: int
1.74 nicm 59: server_write_client(struct client *c, enum msgtype type, const void *buf,
60: size_t len)
1.1 nicm 61: {
1.14 nicm 62: struct imsgbuf *ibuf = &c->ibuf;
1.57 nicm 63: int error;
1.1 nicm 64:
1.12 nicm 65: if (c->flags & CLIENT_BAD)
1.57 nicm 66: return (-1);
1.14 nicm 67: log_debug("writing %d to client %d", type, c->ibuf.fd);
1.57 nicm 68: error = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, -1,
69: (void *) buf, len);
70: if (error == 1)
71: server_update_event(c);
72: return (error == 1 ? 0 : -1);
1.1 nicm 73: }
74:
75: void
1.74 nicm 76: server_write_session(struct session *s, enum msgtype type, const void *buf,
77: size_t len)
1.1 nicm 78: {
79: struct client *c;
80: u_int i;
81:
82: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
83: c = ARRAY_ITEM(&clients, i);
84: if (c == NULL || c->session == NULL)
85: continue;
86: if (c->session == s)
87: server_write_client(c, type, buf, len);
88: }
89: }
90:
91: void
92: server_redraw_client(struct client *c)
93: {
94: c->flags |= CLIENT_REDRAW;
95: }
96:
97: void
98: server_status_client(struct client *c)
99: {
100: c->flags |= CLIENT_STATUS;
101: }
102:
103: void
104: server_redraw_session(struct session *s)
105: {
106: struct client *c;
107: u_int i;
108:
109: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
110: c = ARRAY_ITEM(&clients, i);
111: if (c == NULL || c->session == NULL)
112: continue;
113: if (c->session == s)
114: server_redraw_client(c);
115: }
116: }
117:
118: void
1.25 nicm 119: server_redraw_session_group(struct session *s)
120: {
121: struct session_group *sg;
122:
123: if ((sg = session_group_find(s)) == NULL)
124: server_redraw_session(s);
125: else {
126: TAILQ_FOREACH(s, &sg->sessions, gentry)
127: server_redraw_session(s);
128: }
129: }
130:
131: void
1.1 nicm 132: server_status_session(struct session *s)
133: {
134: struct client *c;
135: u_int i;
136:
137: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
138: c = ARRAY_ITEM(&clients, i);
139: if (c == NULL || c->session == NULL)
140: continue;
141: if (c->session == s)
142: server_status_client(c);
143: }
144: }
145:
146: void
1.25 nicm 147: server_status_session_group(struct session *s)
148: {
149: struct session_group *sg;
150:
151: if ((sg = session_group_find(s)) == NULL)
152: server_status_session(s);
153: else {
154: TAILQ_FOREACH(s, &sg->sessions, gentry)
155: server_status_session(s);
156: }
157: }
158:
159: void
1.1 nicm 160: server_redraw_window(struct window *w)
161: {
162: struct client *c;
163: u_int i;
164:
165: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
166: c = ARRAY_ITEM(&clients, i);
167: if (c == NULL || c->session == NULL)
168: continue;
169: if (c->session->curw->window == w)
170: server_redraw_client(c);
171: }
172: w->flags |= WINDOW_REDRAW;
1.33 nicm 173: }
174:
175: void
176: server_redraw_window_borders(struct window *w)
177: {
178: struct client *c;
179: u_int i;
180:
181: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
182: c = ARRAY_ITEM(&clients, i);
183: if (c == NULL || c->session == NULL)
184: continue;
185: if (c->session->curw->window == w)
186: c->flags |= CLIENT_BORDERS;
187: }
1.1 nicm 188: }
189:
190: void
191: server_status_window(struct window *w)
192: {
193: struct session *s;
194:
195: /*
196: * This is slightly different. We want to redraw the status line of any
1.65 nicm 197: * clients containing this window rather than anywhere it is the
1.1 nicm 198: * current window.
199: */
200:
1.47 nicm 201: RB_FOREACH(s, sessions, &sessions) {
202: if (session_has(s, w) != NULL)
1.1 nicm 203: server_status_session(s);
204: }
205: }
206:
207: void
208: server_lock(void)
209: {
1.23 nicm 210: struct client *c;
211: u_int i;
1.17 nicm 212:
1.1 nicm 213: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
214: c = ARRAY_ITEM(&clients, i);
215: if (c == NULL || c->session == NULL)
216: continue;
1.23 nicm 217: server_lock_client(c);
218: }
219: }
1.1 nicm 220:
1.23 nicm 221: void
222: server_lock_session(struct session *s)
223: {
224: struct client *c;
225: u_int i;
226:
227: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
228: c = ARRAY_ITEM(&clients, i);
229: if (c == NULL || c->session == NULL || c->session != s)
230: continue;
231: server_lock_client(c);
1.31 nicm 232: }
1.23 nicm 233: }
1.1 nicm 234:
1.23 nicm 235: void
236: server_lock_client(struct client *c)
237: {
1.72 nicm 238: const char *cmd;
1.62 nicm 239:
1.64 nicm 240: if (c->flags & CLIENT_CONTROL)
1.62 nicm 241: return;
1.24 nicm 242:
243: if (c->flags & CLIENT_SUSPENDED)
244: return;
1.1 nicm 245:
1.23 nicm 246: cmd = options_get_string(&c->session->options, "lock-command");
1.72 nicm 247: if (strlen(cmd) + 1 > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
1.23 nicm 248: return;
1.31 nicm 249:
1.23 nicm 250: tty_stop_tty(&c->tty);
251: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
252: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
1.52 nicm 253: tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
1.23 nicm 254:
255: c->flags |= CLIENT_SUSPENDED;
1.72 nicm 256: server_write_client(c, MSG_LOCK, cmd, strlen(cmd) + 1);
1.8 nicm 257: }
258:
259: void
260: server_kill_window(struct window *w)
261: {
1.70 nicm 262: struct session *s, *next_s, *target_s;
263: struct session_group *sg;
264: struct winlink *wl;
1.31 nicm 265:
1.48 nicm 266: next_s = RB_MIN(sessions, &sessions);
267: while (next_s != NULL) {
268: s = next_s;
269: next_s = RB_NEXT(sessions, &sessions, s);
270:
1.47 nicm 271: if (session_has(s, w) == NULL)
1.8 nicm 272: continue;
1.34 nicm 273: while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
274: if (session_detach(s, wl)) {
275: server_destroy_session_group(s);
276: break;
277: } else
278: server_redraw_session_group(s);
279: }
1.56 nicm 280:
1.70 nicm 281: if (options_get_number(&s->options, "renumber-windows")) {
282: if ((sg = session_group_find(s)) != NULL) {
283: TAILQ_FOREACH(target_s, &sg->sessions, gentry)
284: session_renumber_windows(target_s);
285: } else
286: session_renumber_windows(s);
287: }
1.8 nicm 288: }
1.69 nicm 289: recalculate_sizes();
1.21 nicm 290: }
291:
292: int
1.25 nicm 293: server_link_window(struct session *src, struct winlink *srcwl,
1.76 nicm 294: struct session *dst, int dstidx, int killflag, int selectflag,
295: char **cause)
1.21 nicm 296: {
1.25 nicm 297: struct winlink *dstwl;
298: struct session_group *srcsg, *dstsg;
299:
300: srcsg = session_group_find(src);
301: dstsg = session_group_find(dst);
302: if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
303: xasprintf(cause, "sessions are grouped");
304: return (-1);
305: }
1.21 nicm 306:
307: dstwl = NULL;
308: if (dstidx != -1)
309: dstwl = winlink_find_by_index(&dst->windows, dstidx);
310: if (dstwl != NULL) {
1.44 nicm 311: if (dstwl->window == srcwl->window) {
312: xasprintf(cause, "same index: %d", dstidx);
1.41 nicm 313: return (-1);
1.44 nicm 314: }
1.21 nicm 315: if (killflag) {
316: /*
317: * Can't use session_detach as it will destroy session
318: * if this makes it empty.
319: */
1.55 nicm 320: notify_window_unlinked(dst, dstwl->window);
1.37 nicm 321: dstwl->flags &= ~WINLINK_ALERTFLAGS;
1.21 nicm 322: winlink_stack_remove(&dst->lastw, dstwl);
323: winlink_remove(&dst->windows, dstwl);
324:
325: /* Force select/redraw if current. */
1.26 nicm 326: if (dstwl == dst->curw) {
1.21 nicm 327: selectflag = 1;
1.26 nicm 328: dst->curw = NULL;
329: }
1.21 nicm 330: }
331: }
332:
333: if (dstidx == -1)
334: dstidx = -1 - options_get_number(&dst->options, "base-index");
335: dstwl = session_attach(dst, srcwl->window, dstidx, cause);
336: if (dstwl == NULL)
337: return (-1);
338:
1.25 nicm 339: if (selectflag)
1.21 nicm 340: session_select(dst, dstwl->idx);
1.25 nicm 341: server_redraw_session_group(dst);
1.21 nicm 342:
343: return (0);
344: }
345:
346: void
347: server_unlink_window(struct session *s, struct winlink *wl)
348: {
349: if (session_detach(s, wl))
1.25 nicm 350: server_destroy_session_group(s);
351: else
352: server_redraw_session_group(s);
1.29 nicm 353: }
354:
355: void
356: server_destroy_pane(struct window_pane *wp)
357: {
1.51 nicm 358: struct window *w = wp->window;
359: int old_fd;
360: struct screen_write_ctx ctx;
361: struct grid_cell gc;
1.29 nicm 362:
1.51 nicm 363: old_fd = wp->fd;
1.36 nicm 364: if (wp->fd != -1) {
1.53 nicm 365: bufferevent_free(wp->event);
1.36 nicm 366: close(wp->fd);
367: wp->fd = -1;
368: }
1.29 nicm 369:
1.51 nicm 370: if (options_get_number(&w->options, "remain-on-exit")) {
371: if (old_fd == -1)
372: return;
373: screen_write_start(&ctx, wp, &wp->base);
374: screen_write_scrollregion(&ctx, 0, screen_size_y(ctx.s) - 1);
375: screen_write_cursormove(&ctx, 0, screen_size_y(ctx.s) - 1);
376: screen_write_linefeed(&ctx, 1);
377: memcpy(&gc, &grid_default_cell, sizeof gc);
378: gc.attr |= GRID_ATTR_BRIGHT;
379: screen_write_puts(&ctx, &gc, "Pane is dead");
380: screen_write_stop(&ctx);
381: wp->flags |= PANE_REDRAW;
1.29 nicm 382: return;
1.51 nicm 383: }
1.29 nicm 384:
1.66 nicm 385: server_unzoom_window(w);
1.29 nicm 386: layout_close_pane(wp);
387: window_remove_pane(w, wp);
388:
389: if (TAILQ_EMPTY(&w->panes))
390: server_kill_window(w);
391: else
392: server_redraw_window(w);
1.25 nicm 393: }
394:
395: void
396: server_destroy_session_group(struct session *s)
397: {
398: struct session_group *sg;
1.71 nicm 399: struct session *s1;
1.25 nicm 400:
401: if ((sg = session_group_find(s)) == NULL)
1.21 nicm 402: server_destroy_session(s);
1.25 nicm 403: else {
1.71 nicm 404: TAILQ_FOREACH_SAFE(s, &sg->sessions, gentry, s1) {
1.25 nicm 405: server_destroy_session(s);
1.71 nicm 406: session_destroy(s);
407: }
1.25 nicm 408: }
1.19 nicm 409: }
410:
1.39 nicm 411: struct session *
412: server_next_session(struct session *s)
413: {
414: struct session *s_loop, *s_out;
415:
416: s_out = NULL;
1.47 nicm 417: RB_FOREACH(s_loop, sessions, &sessions) {
418: if (s_loop == s)
1.39 nicm 419: continue;
420: if (s_out == NULL ||
421: timercmp(&s_loop->activity_time, &s_out->activity_time, <))
422: s_out = s_loop;
423: }
424: return (s_out);
425: }
426:
1.19 nicm 427: void
428: server_destroy_session(struct session *s)
429: {
430: struct client *c;
1.39 nicm 431: struct session *s_new;
1.19 nicm 432: u_int i;
1.31 nicm 433:
1.39 nicm 434: if (!options_get_number(&s->options, "detach-on-destroy"))
435: s_new = server_next_session(s);
436: else
437: s_new = NULL;
438:
1.19 nicm 439: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
440: c = ARRAY_ITEM(&clients, i);
441: if (c == NULL || c->session != s)
442: continue;
1.39 nicm 443: if (s_new == NULL) {
444: c->session = NULL;
1.40 nicm 445: c->flags |= CLIENT_EXIT;
1.39 nicm 446: } else {
1.46 nicm 447: c->last_session = NULL;
1.39 nicm 448: c->session = s_new;
1.55 nicm 449: notify_attached_session_changed(c);
1.49 nicm 450: session_update_activity(s_new);
1.39 nicm 451: server_redraw_client(c);
452: }
1.19 nicm 453: }
1.38 nicm 454: recalculate_sizes();
1.42 nicm 455: }
456:
457: void
1.75 nicm 458: server_check_unattached(void)
1.42 nicm 459: {
460: struct session *s;
461:
462: /*
463: * If any sessions are no longer attached and have destroy-unattached
464: * set, collect them.
465: */
1.47 nicm 466: RB_FOREACH(s, sessions, &sessions) {
467: if (!(s->flags & SESSION_UNATTACHED))
1.42 nicm 468: continue;
469: if (options_get_number (&s->options, "destroy-unattached"))
470: session_destroy(s);
471: }
1.15 nicm 472: }
473:
474: void
475: server_set_identify(struct client *c)
476: {
477: struct timeval tv;
478: int delay;
479:
480: delay = options_get_number(&c->session->options, "display-panes-time");
481: tv.tv_sec = delay / 1000;
482: tv.tv_usec = (delay % 1000) * 1000L;
1.31 nicm 483:
1.75 nicm 484: if (event_initialized(&c->identify_timer))
1.54 nicm 485: evtimer_del(&c->identify_timer);
1.28 nicm 486: evtimer_set(&c->identify_timer, server_callback_identify, c);
487: evtimer_add(&c->identify_timer, &tv);
1.15 nicm 488:
489: c->flags |= CLIENT_IDENTIFY;
490: c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
491: server_redraw_client(c);
492: }
493:
494: void
495: server_clear_identify(struct client *c)
496: {
497: if (c->flags & CLIENT_IDENTIFY) {
498: c->flags &= ~CLIENT_IDENTIFY;
499: c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
500: server_redraw_client(c);
501: }
1.28 nicm 502: }
503:
504: void
505: server_callback_identify(unused int fd, unused short events, void *data)
506: {
507: struct client *c = data;
508:
509: server_clear_identify(c);
1.27 nicm 510: }
511:
512: void
513: server_update_event(struct client *c)
514: {
515: short events;
516:
517: events = 0;
518: if (!(c->flags & CLIENT_BAD))
519: events |= EV_READ;
520: if (c->ibuf.w.queued > 0)
521: events |= EV_WRITE;
1.54 nicm 522: if (event_initialized(&c->event))
523: event_del(&c->event);
1.27 nicm 524: event_set(&c->event, c->ibuf.fd, events, server_client_callback, c);
1.31 nicm 525: event_add(&c->event, NULL);
1.57 nicm 526: }
527:
528: /* Push stdout to client if possible. */
529: void
530: server_push_stdout(struct client *c)
531: {
532: struct msg_stdout_data data;
533: size_t size;
534:
535: size = EVBUFFER_LENGTH(c->stdout_data);
536: if (size == 0)
537: return;
538: if (size > sizeof data.data)
539: size = sizeof data.data;
540:
541: memcpy(data.data, EVBUFFER_DATA(c->stdout_data), size);
542: data.size = size;
543:
544: if (server_write_client(c, MSG_STDOUT, &data, sizeof data) == 0)
545: evbuffer_drain(c->stdout_data, size);
546: }
547:
548: /* Push stderr to client if possible. */
549: void
550: server_push_stderr(struct client *c)
551: {
552: struct msg_stderr_data data;
553: size_t size;
554:
1.68 nicm 555: if (c->stderr_data == c->stdout_data) {
556: server_push_stdout(c);
557: return;
558: }
1.57 nicm 559: size = EVBUFFER_LENGTH(c->stderr_data);
560: if (size == 0)
561: return;
562: if (size > sizeof data.data)
563: size = sizeof data.data;
564:
565: memcpy(data.data, EVBUFFER_DATA(c->stderr_data), size);
566: data.size = size;
567:
568: if (server_write_client(c, MSG_STDERR, &data, sizeof data) == 0)
569: evbuffer_drain(c->stderr_data, size);
570: }
571:
572: /* Set stdin callback. */
573: int
574: server_set_stdin_callback(struct client *c, void (*cb)(struct client *, int,
575: void *), void *cb_data, char **cause)
576: {
1.65 nicm 577: if (c == NULL || c->session != NULL) {
1.57 nicm 578: *cause = xstrdup("no client with stdin");
579: return (-1);
580: }
581: if (c->flags & CLIENT_TERMINAL) {
582: *cause = xstrdup("stdin is a tty");
583: return (-1);
584: }
585: if (c->stdin_callback != NULL) {
586: *cause = xstrdup("stdin in use");
587: return (-1);
588: }
589:
590: c->stdin_callback_data = cb_data;
591: c->stdin_callback = cb;
592:
593: c->references++;
594:
595: if (c->stdin_closed)
1.75 nicm 596: c->stdin_callback(c, 1, c->stdin_callback_data);
1.61 nicm 597:
598: server_write_client(c, MSG_STDIN, NULL, 0);
599:
1.57 nicm 600: return (0);
1.66 nicm 601: }
602:
603: void
604: server_unzoom_window(struct window *w)
605: {
606: window_unzoom(w);
607: server_redraw_window(w);
608: server_status_window(w);
1.1 nicm 609: }