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