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

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