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

1.11    ! nicm        1: /* $OpenBSD: server-client.c,v 1.10 2009/11/03 20:29:47 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:
                     21: #include <fcntl.h>
                     22: #include <string.h>
1.4       nicm       23: #include <time.h>
1.1       nicm       24: #include <paths.h>
                     25: #include <unistd.h>
                     26:
                     27: #include "tmux.h"
                     28:
                     29: void   server_client_handle_data(struct client *);
                     30: void   server_client_check_redraw(struct client *);
                     31: void   server_client_set_title(struct client *);
                     32: void   server_client_check_timers(struct client *);
                     33:
                     34: int    server_client_msg_dispatch(struct client *);
                     35: void   server_client_msg_command(struct client *, struct msg_command_data *);
                     36: void   server_client_msg_identify(
                     37:            struct client *, struct msg_identify_data *, int);
                     38: void   server_client_msg_shell(struct client *);
                     39:
                     40: void printflike2 server_client_msg_error(struct cmd_ctx *, const char *, ...);
                     41: void printflike2 server_client_msg_print(struct cmd_ctx *, const char *, ...);
                     42: void printflike2 server_client_msg_info(struct cmd_ctx *, const char *, ...);
                     43:
                     44:
                     45: /* Create a new client. */
                     46: void
                     47: server_client_create(int fd)
                     48: {
                     49:        struct client   *c;
                     50:        int              mode;
                     51:        u_int            i;
                     52:
                     53:        if ((mode = fcntl(fd, F_GETFL)) == -1)
                     54:                fatal("fcntl failed");
                     55:        if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
                     56:                fatal("fcntl failed");
                     57:        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
                     58:                fatal("fcntl failed");
                     59:
                     60:        c = xcalloc(1, sizeof *c);
                     61:        c->references = 0;
                     62:        imsg_init(&c->ibuf, fd);
                     63:
1.10      nicm       64:        if (gettimeofday(&c->creation_time, NULL) != 0)
1.1       nicm       65:                fatal("gettimeofday failed");
1.11    ! nicm       66:        memcpy(&c->activity_time, &c->creation_time, sizeof c->activity_time);
1.1       nicm       67:
                     68:        ARRAY_INIT(&c->prompt_hdata);
                     69:
                     70:        c->tty.fd = -1;
                     71:        c->title = NULL;
                     72:
                     73:        c->session = NULL;
                     74:        c->tty.sx = 80;
                     75:        c->tty.sy = 24;
                     76:
                     77:        screen_init(&c->status, c->tty.sx, 1, 0);
                     78:        job_tree_init(&c->status_jobs);
                     79:
                     80:        c->message_string = NULL;
                     81:
                     82:        c->prompt_string = NULL;
                     83:        c->prompt_buffer = NULL;
                     84:        c->prompt_index = 0;
                     85:
                     86:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     87:                if (ARRAY_ITEM(&clients, i) == NULL) {
                     88:                        ARRAY_SET(&clients, i, c);
                     89:                        return;
                     90:                }
                     91:        }
                     92:        ARRAY_ADD(&clients, c);
                     93:        log_debug("new client %d", fd);
                     94: }
                     95:
                     96: /* Lost a client. */
                     97: void
                     98: server_client_lost(struct client *c)
                     99: {
                    100:        u_int   i;
                    101:
                    102:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    103:                if (ARRAY_ITEM(&clients, i) == c)
                    104:                        ARRAY_SET(&clients, i, NULL);
                    105:        }
                    106:        log_debug("lost client %d", c->ibuf.fd);
                    107:
                    108:        /*
                    109:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    110:         * and tty_free might close an unrelated fd.
                    111:         */
                    112:        if (c->flags & CLIENT_TERMINAL)
                    113:                tty_free(&c->tty);
                    114:
                    115:        screen_free(&c->status);
                    116:        job_tree_free(&c->status_jobs);
                    117:
                    118:        if (c->title != NULL)
                    119:                xfree(c->title);
                    120:
                    121:        if (c->message_string != NULL)
                    122:                xfree(c->message_string);
                    123:
                    124:        if (c->prompt_string != NULL)
                    125:                xfree(c->prompt_string);
                    126:        if (c->prompt_buffer != NULL)
                    127:                xfree(c->prompt_buffer);
                    128:        for (i = 0; i < ARRAY_LENGTH(&c->prompt_hdata); i++)
                    129:                xfree(ARRAY_ITEM(&c->prompt_hdata, i));
                    130:        ARRAY_FREE(&c->prompt_hdata);
                    131:
                    132:        if (c->cwd != NULL)
                    133:                xfree(c->cwd);
                    134:
                    135:        close(c->ibuf.fd);
                    136:        imsg_clear(&c->ibuf);
                    137:
                    138:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                    139:                if (ARRAY_ITEM(&dead_clients, i) == NULL) {
                    140:                        ARRAY_SET(&dead_clients, i, c);
                    141:                        break;
                    142:                }
                    143:        }
                    144:        if (i == ARRAY_LENGTH(&dead_clients))
                    145:                ARRAY_ADD(&dead_clients, c);
                    146:        c->flags |= CLIENT_DEAD;
                    147:
                    148:        recalculate_sizes();
1.9       nicm      149: }
                    150:
                    151: /* Register clients for poll. */
                    152: void
                    153: server_client_prepare(void)
                    154: {
                    155:        struct client   *c;
                    156:        u_int            i;
                    157:        int              events;
                    158:
                    159:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    160:                if ((c = ARRAY_ITEM(&clients, i)) == NULL)
                    161:                        continue;
                    162:
                    163:                events = 0;
                    164:                if (!(c->flags & CLIENT_BAD))
                    165:                        events |= POLLIN;
                    166:                if (c->ibuf.w.queued > 0)
                    167:                        events |= POLLOUT;
                    168:                server_poll_add(c->ibuf.fd, events, server_client_callback, c);
                    169:
                    170:                if (c->tty.fd == -1)
                    171:                        continue;
                    172:                if (c->flags & CLIENT_SUSPENDED || c->session == NULL)
                    173:                        continue;
                    174:                events = POLLIN;
                    175:                if (BUFFER_USED(c->tty.out) > 0)
                    176:                        events |= POLLOUT;
                    177:                server_poll_add(c->tty.fd, events, server_client_callback, c);
                    178:        }
1.1       nicm      179: }
                    180:
                    181: /* Process a single client event. */
                    182: void
                    183: server_client_callback(int fd, int events, void *data)
                    184: {
                    185:        struct client   *c = data;
1.7       nicm      186:
                    187:        if (c->flags & CLIENT_DEAD)
                    188:                return;
1.1       nicm      189:
                    190:        if (fd == c->ibuf.fd) {
                    191:                if (events & (POLLERR|POLLNVAL|POLLHUP))
                    192:                        goto client_lost;
                    193:
                    194:                if (events & POLLOUT && msgbuf_write(&c->ibuf.w) < 0)
                    195:                        goto client_lost;
                    196:
                    197:                if (c->flags & CLIENT_BAD) {
                    198:                        if (c->ibuf.w.queued == 0)
                    199:                                goto client_lost;
                    200:                        return;
                    201:                }
                    202:
                    203:                if (events & POLLIN && server_client_msg_dispatch(c) != 0)
                    204:                        goto client_lost;
                    205:        }
                    206:
                    207:        if (c->tty.fd != -1 && fd == c->tty.fd) {
                    208:                if (c->flags & CLIENT_SUSPENDED || c->session == NULL)
                    209:                        return;
                    210:
                    211:                if (buffer_poll(fd, events, c->tty.in, c->tty.out) != 0)
                    212:                        goto client_lost;
                    213:        }
                    214:
                    215:        return;
                    216:
                    217: client_lost:
                    218:        server_client_lost(c);
                    219: }
                    220:
1.2       nicm      221: /* Client functions that need to happen every loop. */
                    222: void
                    223: server_client_loop(void)
                    224: {
                    225:        struct client           *c;
                    226:        struct window           *w;
                    227:        struct window_pane      *wp;
                    228:        u_int                    i;
                    229:
                    230:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    231:                c = ARRAY_ITEM(&clients, i);
                    232:                if (c == NULL || c->session == NULL)
                    233:                        continue;
                    234:
1.3       nicm      235:                server_client_handle_data(c);
1.6       nicm      236:                if (c->session != NULL) {
                    237:                        server_client_check_timers(c);
                    238:                        server_client_check_redraw(c);
                    239:                }
1.2       nicm      240:        }
                    241:
                    242:        /*
                    243:         * Any windows will have been redrawn as part of clients, so clear
                    244:         * their flags now.
                    245:         */
                    246:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    247:                w = ARRAY_ITEM(&windows, i);
                    248:                if (w == NULL)
                    249:                        continue;
                    250:
                    251:                w->flags &= ~WINDOW_REDRAW;
                    252:                TAILQ_FOREACH(wp, &w->panes, entry)
                    253:                        wp->flags &= ~PANE_REDRAW;
                    254:        }
                    255: }
                    256:
                    257: /* Handle data input or output from client. */
1.1       nicm      258: void
                    259: server_client_handle_data(struct client *c)
                    260: {
                    261:        struct window           *w;
                    262:        struct window_pane      *wp;
                    263:        struct screen           *s;
                    264:        struct options          *oo;
1.10      nicm      265:        struct timeval           tv_add, tv_now;
1.1       nicm      266:        struct key_binding      *bd;
                    267:        struct keylist          *keylist;
                    268:        struct mouse_event       mouse;
                    269:        int                      key, status, xtimeout, mode, isprefix;
                    270:        u_int                    i;
                    271:
1.10      nicm      272:        /* Check and update repeat flag. */
                    273:        if (gettimeofday(&tv_now, NULL) != 0)
                    274:                fatal("gettimeofday failed");
1.1       nicm      275:        xtimeout = options_get_number(&c->session->options, "repeat-time");
                    276:        if (xtimeout != 0 && c->flags & CLIENT_REPEAT) {
1.10      nicm      277:                if (timercmp(&tv_now, &c->repeat_timer, >))
1.1       nicm      278:                        c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
                    279:        }
                    280:
                    281:        /* Process keys. */
                    282:        keylist = options_get_data(&c->session->options, "prefix");
                    283:        while (tty_keys_next(&c->tty, &key, &mouse) == 0) {
                    284:                if (c->session == NULL)
                    285:                        return;
                    286:                w = c->session->curw->window;
                    287:                wp = w->active; /* could die */
                    288:                oo = &c->session->options;
                    289:
1.10      nicm      290:                /* Update activity timer. */
1.11    ! nicm      291:                memcpy(&c->activity_time, &tv_now, sizeof c->activity_time);
1.10      nicm      292:                memcpy(&c->session->activity_time,
                    293:                    &tv_now, sizeof c->session->activity_time);
                    294:
1.1       nicm      295:                /* Special case: number keys jump to pane in identify mode. */
                    296:                if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
                    297:                        wp = window_pane_at_index(w, key - '0');
                    298:                        if (wp != NULL && window_pane_visible(wp))
                    299:                                window_set_active_pane(w, wp);
                    300:                        server_clear_identify(c);
                    301:                        continue;
                    302:                }
                    303:
                    304:                status_message_clear(c);
                    305:                server_clear_identify(c);
                    306:                if (c->prompt_string != NULL) {
                    307:                        status_prompt_key(c, key);
                    308:                        continue;
                    309:                }
                    310:
                    311:                /* Check for mouse keys. */
                    312:                if (key == KEYC_MOUSE) {
                    313:                        if (options_get_number(oo, "mouse-select-pane")) {
                    314:                                window_set_active_at(w, mouse.x, mouse.y);
                    315:                                wp = w->active;
                    316:                        }
                    317:                        window_pane_mouse(wp, c, &mouse);
                    318:                        continue;
                    319:                }
                    320:
                    321:                /* Is this a prefix key? */
                    322:                isprefix = 0;
                    323:                for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
                    324:                        if (key == ARRAY_ITEM(keylist, i)) {
                    325:                                isprefix = 1;
                    326:                                break;
                    327:                        }
                    328:                }
                    329:
                    330:                /* No previous prefix key. */
                    331:                if (!(c->flags & CLIENT_PREFIX)) {
                    332:                        if (isprefix)
                    333:                                c->flags |= CLIENT_PREFIX;
                    334:                        else {
                    335:                                /* Try as a non-prefix key binding. */
                    336:                                if ((bd = key_bindings_lookup(key)) == NULL)
                    337:                                        window_pane_key(wp, c, key);
                    338:                                else
                    339:                                        key_bindings_dispatch(bd, c);
                    340:                        }
                    341:                        continue;
                    342:                }
                    343:
                    344:                /* Prefix key already pressed. Reset prefix and lookup key. */
                    345:                c->flags &= ~CLIENT_PREFIX;
                    346:                if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
                    347:                        /* If repeating, treat this as a key, else ignore. */
                    348:                        if (c->flags & CLIENT_REPEAT) {
                    349:                                c->flags &= ~CLIENT_REPEAT;
                    350:                                if (isprefix)
                    351:                                        c->flags |= CLIENT_PREFIX;
                    352:                                else
                    353:                                        window_pane_key(wp, c, key);
                    354:                        }
                    355:                        continue;
                    356:                }
                    357:
                    358:                /* If already repeating, but this key can't repeat, skip it. */
                    359:                if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
                    360:                        c->flags &= ~CLIENT_REPEAT;
                    361:                        if (isprefix)
                    362:                                c->flags |= CLIENT_PREFIX;
                    363:                        else
                    364:                                window_pane_key(wp, c, key);
                    365:                        continue;
                    366:                }
                    367:
                    368:                /* If this key can repeat, reset the repeat flags and timer. */
                    369:                if (xtimeout != 0 && bd->can_repeat) {
                    370:                        c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
                    371:
1.10      nicm      372:                        tv_add.tv_sec = xtimeout / 1000;
                    373:                        tv_add.tv_usec = (xtimeout % 1000) * 1000L;
                    374:                        timeradd(&tv_now, &tv_add, &c->repeat_timer);
1.1       nicm      375:                }
                    376:
                    377:                /* Dispatch the command. */
                    378:                key_bindings_dispatch(bd, c);
                    379:        }
                    380:        if (c->session == NULL)
                    381:                return;
                    382:        w = c->session->curw->window;
                    383:        wp = w->active;
                    384:        oo = &c->session->options;
                    385:        s = wp->screen;
                    386:
                    387:        /*
                    388:         * Update cursor position and mode settings. The scroll region and
                    389:         * attributes are cleared across poll(2) as this is the most likely
                    390:         * time a user may interrupt tmux, for example with ~^Z in ssh(1). This
                    391:         * is a compromise between excessive resets and likelihood of an
                    392:         * interrupt.
                    393:         *
                    394:         * tty_region/tty_reset/tty_update_mode already take care of not
                    395:         * resetting things that are already in their default state.
                    396:         */
                    397:        tty_region(&c->tty, 0, c->tty.sy - 1);
                    398:
                    399:        status = options_get_number(oo, "status");
                    400:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                    401:                tty_cursor(&c->tty, 0, 0);
                    402:        else
                    403:                tty_cursor(&c->tty, wp->xoff + s->cx, wp->yoff + s->cy);
                    404:
                    405:        mode = s->mode;
                    406:        if (TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry) != NULL &&
                    407:            options_get_number(oo, "mouse-select-pane"))
                    408:                mode |= MODE_MOUSE;
                    409:        tty_update_mode(&c->tty, mode);
                    410:        tty_reset(&c->tty);
                    411: }
                    412:
                    413: /* Check for client redraws. */
                    414: void
                    415: server_client_check_redraw(struct client *c)
                    416: {
                    417:        struct session          *s = c->session;
                    418:        struct window_pane      *wp;
                    419:        int                      flags, redraw;
                    420:
                    421:        flags = c->tty.flags & TTY_FREEZE;
                    422:        c->tty.flags &= ~TTY_FREEZE;
                    423:
                    424:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
                    425:                if (options_get_number(&s->options, "set-titles"))
                    426:                        server_client_set_title(c);
                    427:
                    428:                if (c->message_string != NULL)
                    429:                        redraw = status_message_redraw(c);
                    430:                else if (c->prompt_string != NULL)
                    431:                        redraw = status_prompt_redraw(c);
                    432:                else
                    433:                        redraw = status_redraw(c);
                    434:                if (!redraw)
                    435:                        c->flags &= ~CLIENT_STATUS;
                    436:        }
                    437:
                    438:        if (c->flags & CLIENT_REDRAW) {
                    439:                screen_redraw_screen(c, 0);
                    440:                c->flags &= ~CLIENT_STATUS;
                    441:        } else {
                    442:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
                    443:                        if (wp->flags & PANE_REDRAW)
                    444:                                screen_redraw_pane(c, wp);
                    445:                }
                    446:        }
                    447:
                    448:        if (c->flags & CLIENT_STATUS)
                    449:                screen_redraw_screen(c, 1);
                    450:
                    451:        c->tty.flags |= flags;
                    452:
                    453:        c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS);
                    454: }
                    455:
                    456: /* Set client title. */
                    457: void
                    458: server_client_set_title(struct client *c)
                    459: {
                    460:        struct session  *s = c->session;
                    461:        const char      *template;
                    462:        char            *title;
                    463:
                    464:        template = options_get_string(&s->options, "set-titles-string");
                    465:
                    466:        title = status_replace(c, template, time(NULL));
                    467:        if (c->title == NULL || strcmp(title, c->title) != 0) {
                    468:                if (c->title != NULL)
                    469:                        xfree(c->title);
                    470:                c->title = xstrdup(title);
                    471:                tty_set_title(&c->tty, c->title);
                    472:        }
                    473:        xfree(title);
                    474: }
                    475:
                    476: /* Check client timers. */
                    477: void
                    478: server_client_check_timers(struct client *c)
                    479: {
                    480:        struct session  *s = c->session;
                    481:        struct job      *job;
                    482:        struct timeval   tv;
                    483:        u_int            interval;
                    484:
                    485:        if (gettimeofday(&tv, NULL) != 0)
                    486:                fatal("gettimeofday failed");
                    487:
                    488:        if (c->flags & CLIENT_IDENTIFY && timercmp(&tv, &c->identify_timer, >))
                    489:                server_clear_identify(c);
                    490:
                    491:        if (c->message_string != NULL && timercmp(&tv, &c->message_timer, >))
                    492:                status_message_clear(c);
                    493:
                    494:        if (c->message_string != NULL || c->prompt_string != NULL) {
                    495:                /*
                    496:                 * Don't need timed redraw for messages/prompts so bail now.
                    497:                 * The status timer isn't reset when they are redrawn anyway.
                    498:                 */
                    499:                return;
                    500:
                    501:        }
                    502:        if (!options_get_number(&s->options, "status"))
                    503:                return;
                    504:
                    505:        /* Check timer; resolution is only a second so don't be too clever. */
                    506:        interval = options_get_number(&s->options, "status-interval");
                    507:        if (interval == 0)
                    508:                return;
                    509:        if (tv.tv_sec < c->status_timer.tv_sec ||
                    510:            ((u_int) tv.tv_sec) - c->status_timer.tv_sec >= interval) {
                    511:                /* Run the jobs for this client and schedule for redraw. */
                    512:                RB_FOREACH(job, jobs, &c->status_jobs)
                    513:                        job_run(job);
                    514:                c->flags |= CLIENT_STATUS;
                    515:        }
                    516: }
                    517:
                    518: /* Dispatch message from client. */
                    519: int
                    520: server_client_msg_dispatch(struct client *c)
                    521: {
                    522:        struct imsg              imsg;
                    523:        struct msg_command_data  commanddata;
                    524:        struct msg_identify_data identifydata;
                    525:        struct msg_environ_data  environdata;
                    526:        ssize_t                  n, datalen;
                    527:
1.8       deraadt   528:        if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
                    529:                return (-1);
1.1       nicm      530:
                    531:        for (;;) {
                    532:                if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
                    533:                        return (-1);
                    534:                if (n == 0)
                    535:                        return (0);
                    536:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
                    537:
                    538:                if (imsg.hdr.peerid != PROTOCOL_VERSION) {
                    539:                        server_write_client(c, MSG_VERSION, NULL, 0);
                    540:                        c->flags |= CLIENT_BAD;
                    541:                        imsg_free(&imsg);
                    542:                        continue;
                    543:                }
                    544:
                    545:                log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
                    546:                switch (imsg.hdr.type) {
                    547:                case MSG_COMMAND:
                    548:                        if (datalen != sizeof commanddata)
                    549:                                fatalx("bad MSG_COMMAND size");
                    550:                        memcpy(&commanddata, imsg.data, sizeof commanddata);
                    551:
                    552:                        server_client_msg_command(c, &commanddata);
                    553:                        break;
                    554:                case MSG_IDENTIFY:
                    555:                        if (datalen != sizeof identifydata)
                    556:                                fatalx("bad MSG_IDENTIFY size");
                    557:                        if (imsg.fd == -1)
                    558:                                fatalx("MSG_IDENTIFY missing fd");
                    559:                        memcpy(&identifydata, imsg.data, sizeof identifydata);
                    560:
                    561:                        server_client_msg_identify(c, &identifydata, imsg.fd);
                    562:                        break;
                    563:                case MSG_RESIZE:
                    564:                        if (datalen != 0)
                    565:                                fatalx("bad MSG_RESIZE size");
                    566:
                    567:                        tty_resize(&c->tty);
                    568:                        recalculate_sizes();
                    569:                        server_redraw_client(c);
                    570:                        break;
                    571:                case MSG_EXITING:
                    572:                        if (datalen != 0)
                    573:                                fatalx("bad MSG_EXITING size");
                    574:
                    575:                        c->session = NULL;
                    576:                        tty_close(&c->tty);
                    577:                        server_write_client(c, MSG_EXITED, NULL, 0);
                    578:                        break;
                    579:                case MSG_WAKEUP:
                    580:                case MSG_UNLOCK:
                    581:                        if (datalen != 0)
                    582:                                fatalx("bad MSG_WAKEUP size");
                    583:
                    584:                        if (!(c->flags & CLIENT_SUSPENDED))
                    585:                                break;
                    586:                        c->flags &= ~CLIENT_SUSPENDED;
1.10      nicm      587:
1.11    ! nicm      588:                        if (gettimeofday(&c->activity_time, NULL) != 0)
        !           589:                                fatal("gettimeofday");
        !           590:                        if (c->session != NULL) {
        !           591:                                memcpy(&c->session->activity_time,
        !           592:                                    &c->activity_time,
        !           593:                                    sizeof c->session->activity_time);
        !           594:                        }
1.10      nicm      595:
1.1       nicm      596:                        tty_start_tty(&c->tty);
                    597:                        server_redraw_client(c);
                    598:                        recalculate_sizes();
                    599:                        break;
                    600:                case MSG_ENVIRON:
                    601:                        if (datalen != sizeof environdata)
                    602:                                fatalx("bad MSG_ENVIRON size");
                    603:                        memcpy(&environdata, imsg.data, sizeof environdata);
                    604:
                    605:                        environdata.var[(sizeof environdata.var) - 1] = '\0';
                    606:                        if (strchr(environdata.var, '=') != NULL)
                    607:                                environ_put(&c->environ, environdata.var);
                    608:                        break;
                    609:                case MSG_SHELL:
                    610:                        if (datalen != 0)
                    611:                                fatalx("bad MSG_SHELL size");
                    612:
                    613:                        server_client_msg_shell(c);
                    614:                        break;
                    615:                default:
                    616:                        fatalx("unexpected message");
                    617:                }
                    618:
                    619:                imsg_free(&imsg);
                    620:        }
                    621: }
                    622:
                    623: /* Callback to send error message to client. */
                    624: void printflike2
                    625: server_client_msg_error(struct cmd_ctx *ctx, const char *fmt, ...)
                    626: {
                    627:        struct msg_print_data   data;
                    628:        va_list                 ap;
                    629:
                    630:        va_start(ap, fmt);
                    631:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
                    632:        va_end(ap);
                    633:
                    634:        server_write_client(ctx->cmdclient, MSG_ERROR, &data, sizeof data);
                    635: }
                    636:
                    637: /* Callback to send print message to client. */
                    638: void printflike2
                    639: server_client_msg_print(struct cmd_ctx *ctx, const char *fmt, ...)
                    640: {
                    641:        struct msg_print_data   data;
                    642:        va_list                 ap;
                    643:
                    644:        va_start(ap, fmt);
                    645:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
                    646:        va_end(ap);
                    647:
                    648:        server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
                    649: }
                    650:
                    651: /* Callback to send print message to client, if not quiet. */
                    652: void printflike2
                    653: server_client_msg_info(struct cmd_ctx *ctx, const char *fmt, ...)
                    654: {
                    655:        struct msg_print_data   data;
                    656:        va_list                 ap;
                    657:
                    658:        if (be_quiet)
                    659:                return;
                    660:
                    661:        va_start(ap, fmt);
                    662:        xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
                    663:        va_end(ap);
                    664:
                    665:        server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
                    666: }
                    667:
                    668: /* Handle command message. */
                    669: void
                    670: server_client_msg_command(struct client *c, struct msg_command_data *data)
                    671: {
                    672:        struct cmd_ctx   ctx;
                    673:        struct cmd_list *cmdlist = NULL;
                    674:        struct cmd      *cmd;
                    675:        int              argc;
                    676:        char           **argv, *cause;
                    677:
                    678:        ctx.error = server_client_msg_error;
                    679:        ctx.print = server_client_msg_print;
                    680:        ctx.info = server_client_msg_info;
                    681:
                    682:        ctx.msgdata = data;
                    683:        ctx.curclient = NULL;
                    684:
                    685:        ctx.cmdclient = c;
                    686:
                    687:        argc = data->argc;
                    688:        data->argv[(sizeof data->argv) - 1] = '\0';
                    689:        if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
                    690:                server_client_msg_error(&ctx, "command too long");
                    691:                goto error;
                    692:        }
                    693:
                    694:        if (argc == 0) {
                    695:                argc = 1;
                    696:                argv = xcalloc(1, sizeof *argv);
                    697:                *argv = xstrdup("new-session");
                    698:        }
                    699:
                    700:        if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
                    701:                server_client_msg_error(&ctx, "%s", cause);
                    702:                cmd_free_argv(argc, argv);
                    703:                goto error;
                    704:        }
                    705:        cmd_free_argv(argc, argv);
                    706:
                    707:        if (data->pid != -1) {
                    708:                TAILQ_FOREACH(cmd, cmdlist, qentry) {
                    709:                        if (cmd->entry->flags & CMD_CANTNEST) {
                    710:                                server_client_msg_error(&ctx,
                    711:                                    "sessions should be nested with care. "
                    712:                                    "unset $TMUX to force");
                    713:                                goto error;
                    714:                        }
                    715:                }
                    716:        }
                    717:
                    718:        if (cmd_list_exec(cmdlist, &ctx) != 1)
                    719:                server_write_client(c, MSG_EXIT, NULL, 0);
                    720:        cmd_list_free(cmdlist);
                    721:        return;
                    722:
                    723: error:
                    724:        if (cmdlist != NULL)
                    725:                cmd_list_free(cmdlist);
                    726:        server_write_client(c, MSG_EXIT, NULL, 0);
                    727: }
                    728:
                    729: /* Handle identify message. */
                    730: void
                    731: server_client_msg_identify(
                    732:     struct client *c, struct msg_identify_data *data, int fd)
                    733: {
                    734:        c->cwd = NULL;
                    735:        data->cwd[(sizeof data->cwd) - 1] = '\0';
                    736:        if (*data->cwd != '\0')
                    737:                c->cwd = xstrdup(data->cwd);
                    738:
                    739:        data->term[(sizeof data->term) - 1] = '\0';
                    740:        tty_init(&c->tty, fd, data->term);
                    741:        if (data->flags & IDENTIFY_UTF8)
                    742:                c->tty.flags |= TTY_UTF8;
                    743:        if (data->flags & IDENTIFY_256COLOURS)
                    744:                c->tty.term_flags |= TERM_256COLOURS;
                    745:        else if (data->flags & IDENTIFY_88COLOURS)
                    746:                c->tty.term_flags |= TERM_88COLOURS;
                    747:
                    748:        tty_resize(&c->tty);
                    749:
                    750:        c->flags |= CLIENT_TERMINAL;
                    751: }
                    752:
                    753: /* Handle shell message. */
                    754: void
                    755: server_client_msg_shell(struct client *c)
                    756: {
                    757:        struct msg_shell_data    data;
                    758:        const char              *shell;
                    759:
                    760:        shell = options_get_string(&global_s_options, "default-shell");
                    761:
                    762:        if (*shell == '\0' || areshell(shell))
                    763:                shell = _PATH_BSHELL;
                    764:        if (strlcpy(data.shell, shell, sizeof data.shell) >= sizeof data.shell)
                    765:                strlcpy(data.shell, _PATH_BSHELL, sizeof data.shell);
                    766:
                    767:        server_write_client(c, MSG_SHELL, &data, sizeof data);
                    768:        c->flags |= CLIENT_BAD; /* it will die after exec */
                    769: }