[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.57

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