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

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