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

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