[BACK]Return to server-client.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/server-client.c, Revision 1.48

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