Annotation of src/usr.bin/tmux/server-client.c, Revision 1.54
1.54 ! nicm 1: /* $OpenBSD: server-client.c,v 1.53 2011/04/18 19:49:05 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2009 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.12 nicm 21: #include <event.h>
1.1 nicm 22: #include <fcntl.h>
23: #include <string.h>
1.4 nicm 24: #include <time.h>
1.1 nicm 25: #include <paths.h>
26: #include <unistd.h>
27:
28: #include "tmux.h"
29:
1.18 nicm 30: void server_client_handle_key(int, struct mouse_event *, void *);
1.17 nicm 31: void server_client_repeat_timer(int, short, void *);
1.36 nicm 32: void server_client_check_exit(struct client *);
1.40 nicm 33: void server_client_check_backoff(struct client *);
1.1 nicm 34: void server_client_check_redraw(struct client *);
35: void server_client_set_title(struct client *);
1.18 nicm 36: void server_client_reset_state(struct client *);
1.36 nicm 37: void server_client_in_callback(struct bufferevent *, short, void *);
38: void server_client_out_callback(struct bufferevent *, short, void *);
39: void server_client_err_callback(struct bufferevent *, short, void *);
1.1 nicm 40:
41: int server_client_msg_dispatch(struct client *);
42: void server_client_msg_command(struct client *, struct msg_command_data *);
43: void server_client_msg_identify(
1.25 nicm 44: struct client *, struct msg_identify_data *, int);
1.1 nicm 45: void server_client_msg_shell(struct client *);
46:
47: void printflike2 server_client_msg_error(struct cmd_ctx *, const char *, ...);
48: void printflike2 server_client_msg_print(struct cmd_ctx *, const char *, ...);
49: void printflike2 server_client_msg_info(struct cmd_ctx *, const char *, ...);
50:
51: /* Create a new client. */
52: void
53: server_client_create(int fd)
54: {
55: struct client *c;
56: u_int i;
57:
1.49 nicm 58: setblocking(fd, 0);
1.1 nicm 59:
60: c = xcalloc(1, sizeof *c);
61: c->references = 0;
62: imsg_init(&c->ibuf, fd);
1.14 nicm 63: server_update_event(c);
1.25 nicm 64:
1.10 nicm 65: if (gettimeofday(&c->creation_time, NULL) != 0)
1.1 nicm 66: fatal("gettimeofday failed");
1.11 nicm 67: memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.1 nicm 68:
1.36 nicm 69: c->stdin_event = NULL;
70: c->stdout_event = NULL;
71: c->stderr_event = NULL;
1.33 nicm 72:
1.1 nicm 73: c->tty.fd = -1;
74: c->title = NULL;
75:
76: c->session = NULL;
1.45 nicm 77: c->last_session = NULL;
1.1 nicm 78: c->tty.sx = 80;
79: c->tty.sy = 24;
80:
81: screen_init(&c->status, c->tty.sx, 1, 0);
1.51 nicm 82: RB_INIT(&c->status_new);
83: RB_INIT(&c->status_old);
1.1 nicm 84:
85: c->message_string = NULL;
1.21 nicm 86: ARRAY_INIT(&c->message_log);
1.1 nicm 87:
88: c->prompt_string = NULL;
89: c->prompt_buffer = NULL;
90: c->prompt_index = 0;
91:
1.17 nicm 92: evtimer_set(&c->repeat_timer, server_client_repeat_timer, c);
93:
1.1 nicm 94: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
95: if (ARRAY_ITEM(&clients, i) == NULL) {
96: ARRAY_SET(&clients, i, c);
97: return;
98: }
99: }
100: ARRAY_ADD(&clients, c);
101: log_debug("new client %d", fd);
102: }
103:
104: /* Lost a client. */
105: void
106: server_client_lost(struct client *c)
107: {
1.21 nicm 108: struct message_entry *msg;
109: u_int i;
1.1 nicm 110:
111: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
112: if (ARRAY_ITEM(&clients, i) == c)
113: ARRAY_SET(&clients, i, NULL);
114: }
115: log_debug("lost client %d", c->ibuf.fd);
116:
117: /*
118: * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
119: * and tty_free might close an unrelated fd.
120: */
121: if (c->flags & CLIENT_TERMINAL)
122: tty_free(&c->tty);
123:
1.49 nicm 124: if (c->stdin_fd != -1) {
125: setblocking(c->stdin_fd, 1);
1.36 nicm 126: close(c->stdin_fd);
1.49 nicm 127: }
1.36 nicm 128: if (c->stdin_event != NULL)
129: bufferevent_free(c->stdin_event);
1.49 nicm 130: if (c->stdout_fd != -1) {
131: setblocking(c->stdout_fd, 1);
1.36 nicm 132: close(c->stdout_fd);
1.49 nicm 133: }
1.36 nicm 134: if (c->stdout_event != NULL)
135: bufferevent_free(c->stdout_event);
1.49 nicm 136: if (c->stderr_fd != -1) {
137: setblocking(c->stderr_fd, 1);
1.36 nicm 138: close(c->stderr_fd);
1.49 nicm 139: }
1.36 nicm 140: if (c->stderr_event != NULL)
141: bufferevent_free(c->stderr_event);
1.33 nicm 142:
1.51 nicm 143: status_free_jobs(&c->status_new);
144: status_free_jobs(&c->status_old);
1.1 nicm 145: screen_free(&c->status);
146:
147: if (c->title != NULL)
148: xfree(c->title);
149:
1.17 nicm 150: evtimer_del(&c->repeat_timer);
151:
1.15 nicm 152: evtimer_del(&c->identify_timer);
153:
1.1 nicm 154: if (c->message_string != NULL)
155: xfree(c->message_string);
1.15 nicm 156: evtimer_del(&c->message_timer);
1.21 nicm 157: for (i = 0; i < ARRAY_LENGTH(&c->message_log); i++) {
158: msg = &ARRAY_ITEM(&c->message_log, i);
159: xfree(msg->msg);
160: }
161: ARRAY_FREE(&c->message_log);
1.1 nicm 162:
163: if (c->prompt_string != NULL)
164: xfree(c->prompt_string);
165: if (c->prompt_buffer != NULL)
166: xfree(c->prompt_buffer);
167:
168: if (c->cwd != NULL)
169: xfree(c->cwd);
170:
171: close(c->ibuf.fd);
172: imsg_clear(&c->ibuf);
1.12 nicm 173: event_del(&c->event);
1.13 nicm 174:
1.1 nicm 175: for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
176: if (ARRAY_ITEM(&dead_clients, i) == NULL) {
177: ARRAY_SET(&dead_clients, i, c);
178: break;
179: }
180: }
181: if (i == ARRAY_LENGTH(&dead_clients))
182: ARRAY_ADD(&dead_clients, c);
183: c->flags |= CLIENT_DEAD;
184:
185: recalculate_sizes();
1.41 nicm 186: server_check_unattached();
1.19 nicm 187: server_update_socket();
1.9 nicm 188: }
189:
1.1 nicm 190: /* Process a single client event. */
191: void
1.12 nicm 192: server_client_callback(int fd, short events, void *data)
1.1 nicm 193: {
194: struct client *c = data;
1.7 nicm 195:
196: if (c->flags & CLIENT_DEAD)
197: return;
1.1 nicm 198:
199: if (fd == c->ibuf.fd) {
1.12 nicm 200: if (events & EV_WRITE && msgbuf_write(&c->ibuf.w) < 0)
1.1 nicm 201: goto client_lost;
202:
203: if (c->flags & CLIENT_BAD) {
204: if (c->ibuf.w.queued == 0)
205: goto client_lost;
206: return;
207: }
208:
1.12 nicm 209: if (events & EV_READ && server_client_msg_dispatch(c) != 0)
1.1 nicm 210: goto client_lost;
211: }
1.14 nicm 212:
1.25 nicm 213: server_update_event(c);
1.1 nicm 214: return;
215:
216: client_lost:
217: server_client_lost(c);
218: }
219:
1.16 nicm 220: /* Handle client status timer. */
221: void
222: server_client_status_timer(void)
223: {
224: struct client *c;
225: struct session *s;
226: struct timeval tv;
1.20 nicm 227: u_int i;
228: int interval;
229: time_t difference;
1.16 nicm 230:
231: if (gettimeofday(&tv, NULL) != 0)
232: fatal("gettimeofday failed");
233:
234: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
235: c = ARRAY_ITEM(&clients, i);
236: if (c == NULL || c->session == NULL)
237: continue;
238: if (c->message_string != NULL || c->prompt_string != NULL) {
239: /*
240: * Don't need timed redraw for messages/prompts so bail
241: * now. The status timer isn't reset when they are
242: * redrawn anyway.
243: */
244: continue;
245: }
246: s = c->session;
247:
248: if (!options_get_number(&s->options, "status"))
249: continue;
250: interval = options_get_number(&s->options, "status-interval");
251:
1.20 nicm 252: difference = tv.tv_sec - c->status_timer.tv_sec;
253: if (difference >= interval) {
1.51 nicm 254: status_update_jobs(c);
1.16 nicm 255: c->flags |= CLIENT_STATUS;
256: }
257: }
258: }
259:
1.18 nicm 260: /* Handle data key input from client. */
261: void
262: server_client_handle_key(int key, struct mouse_event *mouse, void *data)
263: {
264: struct client *c = data;
265: struct session *s;
266: struct window *w;
267: struct window_pane *wp;
268: struct options *oo;
269: struct timeval tv;
270: struct key_binding *bd;
271: struct keylist *keylist;
272: int xtimeout, isprefix;
273: u_int i;
274:
275: /* Check the client is good to accept input. */
276: if ((c->flags & (CLIENT_DEAD|CLIENT_SUSPENDED)) != 0)
277: return;
278: if (c->session == NULL)
279: return;
280: s = c->session;
281:
282: /* Update the activity timer. */
283: if (gettimeofday(&c->activity_time, NULL) != 0)
284: fatal("gettimeofday failed");
285: memcpy(&s->activity_time, &c->activity_time, sizeof s->activity_time);
286:
287: w = c->session->curw->window;
288: wp = w->active;
289: oo = &c->session->options;
290:
291: /* Special case: number keys jump to pane in identify mode. */
1.25 nicm 292: if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
1.30 nicm 293: if (c->flags & CLIENT_READONLY)
294: return;
1.18 nicm 295: wp = window_pane_at_index(w, key - '0');
296: if (wp != NULL && window_pane_visible(wp))
297: window_set_active_pane(w, wp);
298: server_clear_identify(c);
299: return;
300: }
301:
302: /* Handle status line. */
1.30 nicm 303: if (!(c->flags & CLIENT_READONLY)) {
304: status_message_clear(c);
305: server_clear_identify(c);
306: }
1.18 nicm 307: if (c->prompt_string != NULL) {
1.30 nicm 308: if (!(c->flags & CLIENT_READONLY))
309: status_prompt_key(c, key);
1.18 nicm 310: return;
311: }
312:
313: /* Check for mouse keys. */
314: if (key == KEYC_MOUSE) {
1.30 nicm 315: if (c->flags & CLIENT_READONLY)
316: return;
1.54 ! nicm 317: if (options_get_number(oo, "mouse-select-pane") &&
! 318: ((!(mouse->b & MOUSE_DRAG) && mouse->b != MOUSE_UP) ||
! 319: wp->mode != &window_copy_mode)) {
! 320: /*
! 321: * Allow pane switching in copy mode only by mouse down
! 322: * (click).
! 323: */
1.18 nicm 324: window_set_active_at(w, mouse->x, mouse->y);
1.28 nicm 325: server_redraw_window_borders(w);
1.18 nicm 326: wp = w->active;
327: }
1.53 nicm 328: if (mouse->y + 1 == c->tty.sy && mouse->b == MOUSE_UP &&
329: options_get_number(oo, "mouse-select-window") &&
330: options_get_number(oo, "status")) {
331: status_set_window_at(c, mouse->x);
332: return;
333: }
1.31 nicm 334: window_pane_mouse(wp, c->session, mouse);
1.18 nicm 335: return;
336: }
337:
338: /* Is this a prefix key? */
339: keylist = options_get_data(&c->session->options, "prefix");
340: isprefix = 0;
341: for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
342: if (key == ARRAY_ITEM(keylist, i)) {
343: isprefix = 1;
344: break;
345: }
346: }
347:
348: /* No previous prefix key. */
349: if (!(c->flags & CLIENT_PREFIX)) {
350: if (isprefix)
351: c->flags |= CLIENT_PREFIX;
352: else {
353: /* Try as a non-prefix key binding. */
1.30 nicm 354: if ((bd = key_bindings_lookup(key)) == NULL) {
355: if (!(c->flags & CLIENT_READONLY))
1.31 nicm 356: window_pane_key(wp, c->session, key);
1.30 nicm 357: } else
1.18 nicm 358: key_bindings_dispatch(bd, c);
359: }
360: return;
361: }
362:
363: /* Prefix key already pressed. Reset prefix and lookup key. */
364: c->flags &= ~CLIENT_PREFIX;
365: if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
366: /* If repeating, treat this as a key, else ignore. */
367: if (c->flags & CLIENT_REPEAT) {
368: c->flags &= ~CLIENT_REPEAT;
369: if (isprefix)
370: c->flags |= CLIENT_PREFIX;
1.30 nicm 371: else if (!(c->flags & CLIENT_READONLY))
1.31 nicm 372: window_pane_key(wp, c->session, key);
1.18 nicm 373: }
374: return;
375: }
376:
377: /* If already repeating, but this key can't repeat, skip it. */
378: if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
379: c->flags &= ~CLIENT_REPEAT;
380: if (isprefix)
381: c->flags |= CLIENT_PREFIX;
1.30 nicm 382: else if (!(c->flags & CLIENT_READONLY))
1.31 nicm 383: window_pane_key(wp, c->session, key);
1.18 nicm 384: return;
385: }
386:
387: /* If this key can repeat, reset the repeat flags and timer. */
388: xtimeout = options_get_number(&c->session->options, "repeat-time");
389: if (xtimeout != 0 && bd->can_repeat) {
390: c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
1.25 nicm 391:
1.18 nicm 392: tv.tv_sec = xtimeout / 1000;
393: tv.tv_usec = (xtimeout % 1000) * 1000L;
394: evtimer_del(&c->repeat_timer);
395: evtimer_add(&c->repeat_timer, &tv);
396: }
397:
398: /* Dispatch the command. */
399: key_bindings_dispatch(bd, c);
400: }
401:
1.2 nicm 402: /* Client functions that need to happen every loop. */
403: void
404: server_client_loop(void)
405: {
406: struct client *c;
407: struct window *w;
408: struct window_pane *wp;
409: u_int i;
410:
411: for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
412: c = ARRAY_ITEM(&clients, i);
1.36 nicm 413: if (c == NULL)
1.2 nicm 414: continue;
415:
1.36 nicm 416: server_client_check_exit(c);
417: if (c->session != NULL) {
418: server_client_check_redraw(c);
419: server_client_reset_state(c);
420: }
1.2 nicm 421: }
422:
423: /*
424: * Any windows will have been redrawn as part of clients, so clear
425: * their flags now.
426: */
427: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
428: w = ARRAY_ITEM(&windows, i);
429: if (w == NULL)
430: continue;
431:
432: w->flags &= ~WINDOW_REDRAW;
433: TAILQ_FOREACH(wp, &w->panes, entry)
434: wp->flags &= ~PANE_REDRAW;
435: }
436: }
437:
1.18 nicm 438: /*
439: * Update cursor position and mode settings. The scroll region and attributes
440: * are cleared when idle (waiting for an event) as this is the most likely time
441: * a user may interrupt tmux, for example with ~^Z in ssh(1). This is a
442: * compromise between excessive resets and likelihood of an interrupt.
443: *
444: * tty_region/tty_reset/tty_update_mode already take care of not resetting
445: * things that are already in their default state.
446: */
1.1 nicm 447: void
1.18 nicm 448: server_client_reset_state(struct client *c)
1.1 nicm 449: {
1.18 nicm 450: struct window *w = c->session->curw->window;
451: struct window_pane *wp = w->active;
452: struct screen *s = wp->screen;
453: struct options *oo = &c->session->options;
1.54 ! nicm 454: struct options *wo = &w->options;
1.18 nicm 455: int status, mode;
1.1 nicm 456:
457: tty_region(&c->tty, 0, c->tty.sy - 1);
458:
459: status = options_get_number(oo, "status");
460: if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
461: tty_cursor(&c->tty, 0, 0);
462: else
463: tty_cursor(&c->tty, wp->xoff + s->cx, wp->yoff + s->cy);
464:
1.50 nicm 465: /*
466: * Any mode will do for mouse-select-pane, but set standard mode if
467: * none.
468: */
1.1 nicm 469: mode = s->mode;
1.54 ! nicm 470: if ((mode & ALL_MOUSE_MODES) == 0) {
! 471: if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
! 472: options_get_number(oo, "mouse-select-pane") == 0)
! 473: mode |= MODE_MOUSE_STANDARD;
! 474: else if (options_get_number(oo, "mouse-select-window"))
! 475: mode |= MODE_MOUSE_STANDARD;
! 476: else if (options_get_number(wo, "mode-mouse"))
! 477: mode |= MODE_MOUSE_STANDARD;
! 478: }
1.48 nicm 479:
480: /*
481: * Set UTF-8 mouse input if required. If the terminal is UTF-8, the
482: * user has set mouse-utf8 and any mouse mode is in effect, turn on
483: * UTF-8 mouse input. If the receiving terminal hasn't requested it
484: * (that is, it isn't in s->mode), then it'll be converted in
485: * input_mouse.
486: */
487: if ((c->tty.flags & TTY_UTF8) &&
488: (mode & ALL_MOUSE_MODES) && options_get_number(oo, "mouse-utf8"))
489: mode |= MODE_MOUSE_UTF8;
490: else
491: mode &= ~MODE_MOUSE_UTF8;
492:
493: /* Set the terminal mode and reset attributes. */
1.1 nicm 494: tty_update_mode(&c->tty, mode);
495: tty_reset(&c->tty);
1.17 nicm 496: }
497:
498: /* Repeat time callback. */
1.24 nicm 499: /* ARGSUSED */
1.17 nicm 500: void
501: server_client_repeat_timer(unused int fd, unused short events, void *data)
502: {
503: struct client *c = data;
504:
505: if (c->flags & CLIENT_REPEAT)
506: c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
1.1 nicm 507: }
508:
1.36 nicm 509: /* Check if client should be exited. */
510: void
511: server_client_check_exit(struct client *c)
512: {
513: struct msg_exit_data exitdata;
514:
515: if (!(c->flags & CLIENT_EXIT))
516: return;
517:
518: if (c->stdout_fd != -1 && c->stdout_event != NULL &&
519: EVBUFFER_LENGTH(c->stdout_event->output) != 0)
520: return;
521: if (c->stderr_fd != -1 && c->stderr_event != NULL &&
522: EVBUFFER_LENGTH(c->stderr_event->output) != 0)
523: return;
524:
525: exitdata.retcode = c->retcode;
526: server_write_client(c, MSG_EXIT, &exitdata, sizeof exitdata);
527:
528: c->flags &= ~CLIENT_EXIT;
529: }
530:
1.38 nicm 531: /*
532: * Check if the client should backoff. During backoff, data from external
533: * programs is not written to the terminal. When the existing data drains, the
534: * client is redrawn.
535: *
536: * There are two backoff phases - both the tty and client have backoff flags -
537: * the first to allow existing data to drain and the latter to ensure backoff
538: * is disabled until the redraw has finished and prevent the redraw triggering
539: * another backoff.
540: */
541: void
542: server_client_check_backoff(struct client *c)
543: {
544: struct tty *tty = &c->tty;
545: size_t used;
546:
547: used = EVBUFFER_LENGTH(tty->event->output);
548:
549: /*
550: * If in the second backoff phase (redrawing), don't check backoff
551: * until the redraw has completed (or enough of it to drop below the
552: * backoff threshold).
553: */
554: if (c->flags & CLIENT_BACKOFF) {
555: if (used > BACKOFF_THRESHOLD)
556: return;
557: c->flags &= ~CLIENT_BACKOFF;
558: return;
559: }
560:
561: /* Once drained, allow data through again and schedule redraw. */
562: if (tty->flags & TTY_BACKOFF) {
563: if (used != 0)
564: return;
565: tty->flags &= ~TTY_BACKOFF;
566: c->flags |= (CLIENT_BACKOFF|CLIENT_REDRAWWINDOW|CLIENT_STATUS);
567: return;
568: }
569:
570: /* If too much data, start backoff. */
571: if (used > BACKOFF_THRESHOLD)
572: tty->flags |= TTY_BACKOFF;
573: }
574:
1.1 nicm 575: /* Check for client redraws. */
576: void
577: server_client_check_redraw(struct client *c)
578: {
579: struct session *s = c->session;
580: struct window_pane *wp;
581: int flags, redraw;
582:
583: flags = c->tty.flags & TTY_FREEZE;
584: c->tty.flags &= ~TTY_FREEZE;
585:
586: if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
587: if (options_get_number(&s->options, "set-titles"))
588: server_client_set_title(c);
1.12 nicm 589:
1.1 nicm 590: if (c->message_string != NULL)
591: redraw = status_message_redraw(c);
592: else if (c->prompt_string != NULL)
593: redraw = status_prompt_redraw(c);
594: else
595: redraw = status_redraw(c);
596: if (!redraw)
597: c->flags &= ~CLIENT_STATUS;
598: }
599:
600: if (c->flags & CLIENT_REDRAW) {
1.27 nicm 601: screen_redraw_screen(c, 0, 0);
602: c->flags &= ~(CLIENT_STATUS|CLIENT_BORDERS);
1.38 nicm 603: } else if (c->flags & CLIENT_REDRAWWINDOW) {
604: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry)
605: screen_redraw_pane(c, wp);
606: c->flags &= ~CLIENT_REDRAWWINDOW;
1.1 nicm 607: } else {
608: TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
609: if (wp->flags & PANE_REDRAW)
610: screen_redraw_pane(c, wp);
611: }
612: }
613:
1.27 nicm 614: if (c->flags & CLIENT_BORDERS)
615: screen_redraw_screen(c, 0, 1);
616:
1.1 nicm 617: if (c->flags & CLIENT_STATUS)
1.27 nicm 618: screen_redraw_screen(c, 1, 0);
1.1 nicm 619:
620: c->tty.flags |= flags;
621:
1.27 nicm 622: c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS|CLIENT_BORDERS);
1.1 nicm 623: }
624:
625: /* Set client title. */
626: void
627: server_client_set_title(struct client *c)
628: {
629: struct session *s = c->session;
630: const char *template;
631: char *title;
632:
633: template = options_get_string(&s->options, "set-titles-string");
1.25 nicm 634:
1.52 nicm 635: title = status_replace(c, NULL, NULL, NULL, template, time(NULL), 1);
1.1 nicm 636: if (c->title == NULL || strcmp(title, c->title) != 0) {
637: if (c->title != NULL)
638: xfree(c->title);
639: c->title = xstrdup(title);
640: tty_set_title(&c->tty, c->title);
641: }
642: xfree(title);
643: }
644:
1.36 nicm 645: /*
646: * Error callback for client stdin. Caller must increase reference count when
647: * enabling event!
648: */
649: void
650: server_client_in_callback(
651: unused struct bufferevent *bufev, unused short what, void *data)
652: {
653: struct client *c = data;
654:
655: c->references--;
656: if (c->flags & CLIENT_DEAD)
657: return;
658:
659: bufferevent_disable(c->stdin_event, EV_READ|EV_WRITE);
1.49 nicm 660: setblocking(c->stdin_fd, 1);
1.36 nicm 661: close(c->stdin_fd);
662: c->stdin_fd = -1;
663:
664: if (c->stdin_callback != NULL)
665: c->stdin_callback(c, c->stdin_data);
666: }
667:
668: /* Error callback for client stdout. */
669: void
670: server_client_out_callback(
671: unused struct bufferevent *bufev, unused short what, unused void *data)
672: {
673: struct client *c = data;
674:
675: bufferevent_disable(c->stdout_event, EV_READ|EV_WRITE);
1.49 nicm 676: setblocking(c->stdout_fd, 1);
1.36 nicm 677: close(c->stdout_fd);
678: c->stdout_fd = -1;
679: }
680:
681: /* Error callback for client stderr. */
682: void
683: server_client_err_callback(
684: unused struct bufferevent *bufev, unused short what, unused void *data)
685: {
686: struct client *c = data;
687:
688: bufferevent_disable(c->stderr_event, EV_READ|EV_WRITE);
1.49 nicm 689: setblocking(c->stderr_fd, 1);
1.36 nicm 690: close(c->stderr_fd);
691: c->stderr_fd = -1;
692: }
693:
1.1 nicm 694: /* Dispatch message from client. */
695: int
696: server_client_msg_dispatch(struct client *c)
697: {
698: struct imsg imsg;
699: struct msg_command_data commanddata;
700: struct msg_identify_data identifydata;
701: struct msg_environ_data environdata;
702: ssize_t n, datalen;
703:
1.8 deraadt 704: if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
705: return (-1);
1.1 nicm 706:
707: for (;;) {
708: if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
709: return (-1);
710: if (n == 0)
711: return (0);
712: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
713:
714: if (imsg.hdr.peerid != PROTOCOL_VERSION) {
715: server_write_client(c, MSG_VERSION, NULL, 0);
716: c->flags |= CLIENT_BAD;
717: imsg_free(&imsg);
718: continue;
719: }
720:
721: log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
722: switch (imsg.hdr.type) {
723: case MSG_COMMAND:
724: if (datalen != sizeof commanddata)
725: fatalx("bad MSG_COMMAND size");
726: memcpy(&commanddata, imsg.data, sizeof commanddata);
727:
728: server_client_msg_command(c, &commanddata);
729: break;
730: case MSG_IDENTIFY:
731: if (datalen != sizeof identifydata)
732: fatalx("bad MSG_IDENTIFY size");
733: if (imsg.fd == -1)
734: fatalx("MSG_IDENTIFY missing fd");
735: memcpy(&identifydata, imsg.data, sizeof identifydata);
736:
1.39 nicm 737: c->stdin_fd = imsg.fd;
1.37 nicm 738: c->stdin_event = bufferevent_new(c->stdin_fd,
739: NULL, NULL, server_client_in_callback, c);
1.36 nicm 740: if (c->stdin_event == NULL)
741: fatalx("failed to create stdin event");
1.49 nicm 742: setblocking(c->stdin_fd, 0);
1.36 nicm 743:
1.1 nicm 744: server_client_msg_identify(c, &identifydata, imsg.fd);
745: break;
1.33 nicm 746: case MSG_STDOUT:
747: if (datalen != 0)
748: fatalx("bad MSG_STDOUT size");
749: if (imsg.fd == -1)
750: fatalx("MSG_STDOUT missing fd");
751:
1.36 nicm 752: c->stdout_fd = imsg.fd;
1.37 nicm 753: c->stdout_event = bufferevent_new(c->stdout_fd,
754: NULL, NULL, server_client_out_callback, c);
1.36 nicm 755: if (c->stdout_event == NULL)
756: fatalx("failed to create stdout event");
1.49 nicm 757: setblocking(c->stdout_fd, 0);
1.36 nicm 758:
1.33 nicm 759: break;
760: case MSG_STDERR:
761: if (datalen != 0)
762: fatalx("bad MSG_STDERR size");
763: if (imsg.fd == -1)
764: fatalx("MSG_STDERR missing fd");
765:
1.36 nicm 766: c->stderr_fd = imsg.fd;
1.37 nicm 767: c->stderr_event = bufferevent_new(c->stderr_fd,
768: NULL, NULL, server_client_err_callback, c);
1.36 nicm 769: if (c->stderr_event == NULL)
770: fatalx("failed to create stderr event");
1.49 nicm 771: setblocking(c->stderr_fd, 0);
1.36 nicm 772:
1.33 nicm 773: break;
1.1 nicm 774: case MSG_RESIZE:
775: if (datalen != 0)
776: fatalx("bad MSG_RESIZE size");
777:
1.32 nicm 778: if (tty_resize(&c->tty)) {
779: recalculate_sizes();
780: server_redraw_client(c);
781: }
1.1 nicm 782: break;
783: case MSG_EXITING:
784: if (datalen != 0)
785: fatalx("bad MSG_EXITING size");
786:
787: c->session = NULL;
788: tty_close(&c->tty);
789: server_write_client(c, MSG_EXITED, NULL, 0);
790: break;
791: case MSG_WAKEUP:
792: case MSG_UNLOCK:
793: if (datalen != 0)
794: fatalx("bad MSG_WAKEUP size");
795:
796: if (!(c->flags & CLIENT_SUSPENDED))
797: break;
798: c->flags &= ~CLIENT_SUSPENDED;
1.10 nicm 799:
1.11 nicm 800: if (gettimeofday(&c->activity_time, NULL) != 0)
801: fatal("gettimeofday");
1.47 nicm 802: if (c->session != NULL)
803: session_update_activity(c->session);
1.10 nicm 804:
1.1 nicm 805: tty_start_tty(&c->tty);
806: server_redraw_client(c);
807: recalculate_sizes();
808: break;
809: case MSG_ENVIRON:
810: if (datalen != sizeof environdata)
811: fatalx("bad MSG_ENVIRON size");
812: memcpy(&environdata, imsg.data, sizeof environdata);
813:
814: environdata.var[(sizeof environdata.var) - 1] = '\0';
815: if (strchr(environdata.var, '=') != NULL)
816: environ_put(&c->environ, environdata.var);
817: break;
818: case MSG_SHELL:
819: if (datalen != 0)
820: fatalx("bad MSG_SHELL size");
821:
822: server_client_msg_shell(c);
823: break;
824: default:
825: fatalx("unexpected message");
826: }
827:
828: imsg_free(&imsg);
829: }
830: }
831:
832: /* Callback to send error message to client. */
833: void printflike2
834: server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
835: {
1.33 nicm 836: va_list ap;
1.1 nicm 837:
838: va_start(ap, fmt);
1.36 nicm 839: evbuffer_add_vprintf(ctx->cmdclient->stderr_event->output, fmt, ap);
1.1 nicm 840: va_end(ap);
841:
1.36 nicm 842: bufferevent_write(ctx->cmdclient->stderr_event, "\n", 1);
1.34 nicm 843: ctx->cmdclient->retcode = 1;
1.1 nicm 844: }
845:
846: /* Callback to send print message to client. */
847: void printflike2
848: server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
849: {
1.33 nicm 850: va_list ap;
1.1 nicm 851:
852: va_start(ap, fmt);
1.36 nicm 853: evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
1.1 nicm 854: va_end(ap);
855:
1.36 nicm 856: bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
1.1 nicm 857: }
858:
859: /* Callback to send print message to client, if not quiet. */
860: void printflike2
861: server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
862: {
1.33 nicm 863: va_list ap;
1.1 nicm 864:
1.26 nicm 865: if (options_get_number(&global_options, "quiet"))
1.1 nicm 866: return;
867:
868: va_start(ap, fmt);
1.36 nicm 869: evbuffer_add_vprintf(ctx->cmdclient->stdout_event->output, fmt, ap);
1.1 nicm 870: va_end(ap);
871:
1.36 nicm 872: bufferevent_write(ctx->cmdclient->stdout_event, "\n", 1);
1.1 nicm 873: }
874:
875: /* Handle command message. */
876: void
877: server_client_msg_command(struct client *c, struct msg_command_data *data)
878: {
1.36 nicm 879: struct cmd_ctx ctx;
880: struct cmd_list *cmdlist = NULL;
881: int argc;
882: char **argv, *cause;
1.1 nicm 883:
884: ctx.error = server_client_msg_error;
885: ctx.print = server_client_msg_print;
886: ctx.info = server_client_msg_info;
887:
888: ctx.msgdata = data;
889: ctx.curclient = NULL;
890:
891: ctx.cmdclient = c;
892:
893: argc = data->argc;
894: data->argv[(sizeof data->argv) - 1] = '\0';
895: if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
896: server_client_msg_error(&ctx, "command too long");
897: goto error;
898: }
899:
900: if (argc == 0) {
901: argc = 1;
902: argv = xcalloc(1, sizeof *argv);
903: *argv = xstrdup("new-session");
904: }
905:
906: if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
907: server_client_msg_error(&ctx, "%s", cause);
908: cmd_free_argv(argc, argv);
909: goto error;
910: }
911: cmd_free_argv(argc, argv);
912:
1.36 nicm 913: if (cmd_list_exec(cmdlist, &ctx) != 1)
914: c->flags |= CLIENT_EXIT;
1.1 nicm 915: cmd_list_free(cmdlist);
916: return;
917:
918: error:
919: if (cmdlist != NULL)
920: cmd_list_free(cmdlist);
1.36 nicm 921: c->flags |= CLIENT_EXIT;
1.1 nicm 922: }
923:
924: /* Handle identify message. */
925: void
926: server_client_msg_identify(
927: struct client *c, struct msg_identify_data *data, int fd)
928: {
1.33 nicm 929: int tty_fd;
930:
1.1 nicm 931: c->cwd = NULL;
932: data->cwd[(sizeof data->cwd) - 1] = '\0';
933: if (*data->cwd != '\0')
934: c->cwd = xstrdup(data->cwd);
935:
1.33 nicm 936: if (!isatty(fd))
937: return;
938: if ((tty_fd = dup(fd)) == -1)
939: fatal("dup failed");
1.1 nicm 940: data->term[(sizeof data->term) - 1] = '\0';
1.33 nicm 941: tty_init(&c->tty, tty_fd, data->term);
1.1 nicm 942: if (data->flags & IDENTIFY_UTF8)
943: c->tty.flags |= TTY_UTF8;
944: if (data->flags & IDENTIFY_256COLOURS)
945: c->tty.term_flags |= TERM_256COLOURS;
946: else if (data->flags & IDENTIFY_88COLOURS)
947: c->tty.term_flags |= TERM_88COLOURS;
1.18 nicm 948: c->tty.key_callback = server_client_handle_key;
949: c->tty.key_data = c;
1.1 nicm 950:
951: tty_resize(&c->tty);
952:
953: c->flags |= CLIENT_TERMINAL;
954: }
955:
956: /* Handle shell message. */
957: void
958: server_client_msg_shell(struct client *c)
959: {
960: struct msg_shell_data data;
961: const char *shell;
1.25 nicm 962:
1.1 nicm 963: shell = options_get_string(&global_s_options, "default-shell");
964:
965: if (*shell == '\0' || areshell(shell))
966: shell = _PATH_BSHELL;
967: if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
968: strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
1.25 nicm 969:
1.1 nicm 970: server_write_client(c, MSG_SHELL, &data, sizeof data);
971: c->flags |= CLIENT_BAD; /* it will die after exec */
972: }