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