[BACK]Return to server-fn.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/server-fn.c, Revision 1.64

1.64    ! nicm        1: /* $OpenBSD: server-fn.c,v 1.63 2013/03/22 10:31:22 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 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.60      nicm       21: #include <stdlib.h>
1.1       nicm       22: #include <string.h>
1.5       nicm       23: #include <time.h>
1.1       nicm       24: #include <unistd.h>
                     25:
                     26: #include "tmux.h"
                     27:
1.39      nicm       28: struct session *server_next_session(struct session *);
                     29: void           server_callback_identify(int, short, void *);
1.28      nicm       30:
1.13      nicm       31: void
                     32: server_fill_environ(struct session *s, struct environ *env)
1.1       nicm       33: {
1.50      nicm       34:        char    var[MAXPATHLEN], *term;
                     35:        u_int   idx;
                     36:        long    pid;
                     37:
                     38:        if (s != NULL) {
                     39:                term = options_get_string(&s->options, "default-terminal");
                     40:                environ_set(env, "TERM", term);
                     41:
                     42:                idx = s->idx;
                     43:        } else
                     44:                idx = -1;
                     45:        pid = getpid();
                     46:        xsnprintf(var, sizeof var, "%s,%ld,%d", socket_path, pid, idx);
                     47:        environ_set(env, "TMUX", var);
1.58      nicm       48: }
                     49:
                     50: void
                     51: server_write_ready(struct client *c)
                     52: {
1.59      nicm       53:        if (c->flags & CLIENT_CONTROL)
                     54:                return;
1.58      nicm       55:        server_write_client(c, MSG_READY, NULL, 0);
1.10      nicm       56: }
                     57:
1.57      nicm       58: int
1.1       nicm       59: server_write_client(
1.11      nicm       60:     struct client *c, enum msgtype type, const void *buf, size_t len)
1.1       nicm       61: {
1.14      nicm       62:        struct imsgbuf  *ibuf = &c->ibuf;
1.57      nicm       63:        int              error;
1.1       nicm       64:
1.12      nicm       65:        if (c->flags & CLIENT_BAD)
1.57      nicm       66:                return (-1);
1.14      nicm       67:        log_debug("writing %d to client %d", type, c->ibuf.fd);
1.57      nicm       68:        error = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, -1,
                     69:            (void *) buf, len);
                     70:        if (error == 1)
                     71:                server_update_event(c);
                     72:        return (error == 1 ? 0 : -1);
1.1       nicm       73: }
                     74:
                     75: void
                     76: server_write_session(
1.11      nicm       77:     struct session *s, enum msgtype type, const void *buf, size_t len)
1.1       nicm       78: {
                     79:        struct client   *c;
                     80:        u_int            i;
                     81:
                     82:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     83:                c = ARRAY_ITEM(&clients, i);
                     84:                if (c == NULL || c->session == NULL)
                     85:                        continue;
                     86:                if (c->session == s)
                     87:                        server_write_client(c, type, buf, len);
                     88:        }
                     89: }
                     90:
                     91: void
                     92: server_redraw_client(struct client *c)
                     93: {
                     94:        c->flags |= CLIENT_REDRAW;
                     95: }
                     96:
                     97: void
                     98: server_status_client(struct client *c)
                     99: {
                    100:        c->flags |= CLIENT_STATUS;
                    101: }
                    102:
                    103: void
                    104: server_redraw_session(struct session *s)
                    105: {
                    106:        struct client   *c;
                    107:        u_int            i;
                    108:
                    109:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    110:                c = ARRAY_ITEM(&clients, i);
                    111:                if (c == NULL || c->session == NULL)
                    112:                        continue;
                    113:                if (c->session == s)
                    114:                        server_redraw_client(c);
                    115:        }
                    116: }
                    117:
                    118: void
1.25      nicm      119: server_redraw_session_group(struct session *s)
                    120: {
                    121:        struct session_group    *sg;
                    122:
                    123:        if ((sg = session_group_find(s)) == NULL)
                    124:                server_redraw_session(s);
                    125:        else {
                    126:                TAILQ_FOREACH(s, &sg->sessions, gentry)
                    127:                        server_redraw_session(s);
                    128:        }
                    129: }
                    130:
                    131: void
1.1       nicm      132: server_status_session(struct session *s)
                    133: {
                    134:        struct client   *c;
                    135:        u_int            i;
                    136:
                    137:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    138:                c = ARRAY_ITEM(&clients, i);
                    139:                if (c == NULL || c->session == NULL)
                    140:                        continue;
                    141:                if (c->session == s)
                    142:                        server_status_client(c);
                    143:        }
                    144: }
                    145:
                    146: void
1.25      nicm      147: server_status_session_group(struct session *s)
                    148: {
                    149:        struct session_group    *sg;
                    150:
                    151:        if ((sg = session_group_find(s)) == NULL)
                    152:                server_status_session(s);
                    153:        else {
                    154:                TAILQ_FOREACH(s, &sg->sessions, gentry)
                    155:                        server_status_session(s);
                    156:        }
                    157: }
                    158:
                    159: void
1.1       nicm      160: server_redraw_window(struct window *w)
                    161: {
                    162:        struct client   *c;
                    163:        u_int            i;
                    164:
                    165:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    166:                c = ARRAY_ITEM(&clients, i);
                    167:                if (c == NULL || c->session == NULL)
                    168:                        continue;
                    169:                if (c->session->curw->window == w)
                    170:                        server_redraw_client(c);
                    171:        }
                    172:        w->flags |= WINDOW_REDRAW;
1.33      nicm      173: }
                    174:
                    175: void
                    176: server_redraw_window_borders(struct window *w)
                    177: {
                    178:        struct client   *c;
                    179:        u_int            i;
                    180:
                    181:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    182:                c = ARRAY_ITEM(&clients, i);
                    183:                if (c == NULL || c->session == NULL)
                    184:                        continue;
                    185:                if (c->session->curw->window == w)
                    186:                        c->flags |= CLIENT_BORDERS;
                    187:        }
1.1       nicm      188: }
                    189:
                    190: void
                    191: server_status_window(struct window *w)
                    192: {
                    193:        struct session  *s;
                    194:
                    195:        /*
                    196:         * This is slightly different. We want to redraw the status line of any
                    197:         * clients containing this window rather than any where it is the
                    198:         * current window.
                    199:         */
                    200:
1.47      nicm      201:        RB_FOREACH(s, sessions, &sessions) {
                    202:                if (session_has(s, w) != NULL)
1.1       nicm      203:                        server_status_session(s);
                    204:        }
                    205: }
                    206:
                    207: void
                    208: server_lock(void)
                    209: {
1.23      nicm      210:        struct client   *c;
                    211:        u_int            i;
1.17      nicm      212:
1.1       nicm      213:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    214:                c = ARRAY_ITEM(&clients, i);
                    215:                if (c == NULL || c->session == NULL)
                    216:                        continue;
1.23      nicm      217:                server_lock_client(c);
                    218:        }
                    219: }
1.1       nicm      220:
1.23      nicm      221: void
                    222: server_lock_session(struct session *s)
                    223: {
                    224:        struct client   *c;
                    225:        u_int            i;
                    226:
                    227:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    228:                c = ARRAY_ITEM(&clients, i);
                    229:                if (c == NULL || c->session == NULL || c->session != s)
                    230:                        continue;
                    231:                server_lock_client(c);
1.31      nicm      232:        }
1.23      nicm      233: }
1.1       nicm      234:
1.23      nicm      235: void
                    236: server_lock_client(struct client *c)
                    237: {
                    238:        const char              *cmd;
                    239:        size_t                   cmdlen;
                    240:        struct msg_lock_data     lockdata;
1.62      nicm      241:
1.64    ! nicm      242:        if (c->flags & CLIENT_CONTROL)
1.62      nicm      243:                return;
1.24      nicm      244:
                    245:        if (c->flags & CLIENT_SUSPENDED)
                    246:                return;
1.1       nicm      247:
1.23      nicm      248:        cmd = options_get_string(&c->session->options, "lock-command");
                    249:        cmdlen = strlcpy(lockdata.cmd, cmd, sizeof lockdata.cmd);
                    250:        if (cmdlen >= sizeof lockdata.cmd)
                    251:                return;
1.31      nicm      252:
1.23      nicm      253:        tty_stop_tty(&c->tty);
                    254:        tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_SMCUP));
                    255:        tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_CLEAR));
1.52      nicm      256:        tty_raw(&c->tty, tty_term_string(c->tty.term, TTYC_E3));
1.23      nicm      257:
                    258:        c->flags |= CLIENT_SUSPENDED;
                    259:        server_write_client(c, MSG_LOCK, &lockdata, sizeof lockdata);
1.8       nicm      260: }
                    261:
                    262: void
                    263: server_kill_window(struct window *w)
                    264: {
1.48      nicm      265:        struct session  *s, *next_s;
1.8       nicm      266:        struct winlink  *wl;
1.31      nicm      267:
1.48      nicm      268:        next_s = RB_MIN(sessions, &sessions);
                    269:        while (next_s != NULL) {
                    270:                s = next_s;
                    271:                next_s = RB_NEXT(sessions, &sessions, s);
                    272:
1.47      nicm      273:                if (session_has(s, w) == NULL)
1.8       nicm      274:                        continue;
1.34      nicm      275:                while ((wl = winlink_find_by_window(&s->windows, w)) != NULL) {
                    276:                        if (session_detach(s, wl)) {
                    277:                                server_destroy_session_group(s);
                    278:                                break;
                    279:                        } else
                    280:                                server_redraw_session_group(s);
                    281:                }
1.56      nicm      282:
                    283:                if (options_get_number(&s->options, "renumber-windows"))
                    284:                        session_renumber_windows(s);
1.8       nicm      285:        }
1.21      nicm      286: }
                    287:
                    288: int
1.25      nicm      289: server_link_window(struct session *src, struct winlink *srcwl,
                    290:     struct session *dst, int dstidx, int killflag, int selectflag, char **cause)
1.21      nicm      291: {
1.25      nicm      292:        struct winlink          *dstwl;
                    293:        struct session_group    *srcsg, *dstsg;
                    294:
                    295:        srcsg = session_group_find(src);
                    296:        dstsg = session_group_find(dst);
                    297:        if (src != dst && srcsg != NULL && dstsg != NULL && srcsg == dstsg) {
                    298:                xasprintf(cause, "sessions are grouped");
                    299:                return (-1);
                    300:        }
1.21      nicm      301:
                    302:        dstwl = NULL;
                    303:        if (dstidx != -1)
                    304:                dstwl = winlink_find_by_index(&dst->windows, dstidx);
                    305:        if (dstwl != NULL) {
1.44      nicm      306:                if (dstwl->window == srcwl->window) {
                    307:                        xasprintf(cause, "same index: %d", dstidx);
1.41      nicm      308:                        return (-1);
1.44      nicm      309:                }
1.21      nicm      310:                if (killflag) {
                    311:                        /*
                    312:                         * Can't use session_detach as it will destroy session
                    313:                         * if this makes it empty.
                    314:                         */
1.55      nicm      315:                        notify_window_unlinked(dst, dstwl->window);
1.37      nicm      316:                        dstwl->flags &= ~WINLINK_ALERTFLAGS;
1.21      nicm      317:                        winlink_stack_remove(&dst->lastw, dstwl);
                    318:                        winlink_remove(&dst->windows, dstwl);
                    319:
                    320:                        /* Force select/redraw if current. */
1.26      nicm      321:                        if (dstwl == dst->curw) {
1.21      nicm      322:                                selectflag = 1;
1.26      nicm      323:                                dst->curw = NULL;
                    324:                        }
1.21      nicm      325:                }
                    326:        }
                    327:
                    328:        if (dstidx == -1)
                    329:                dstidx = -1 - options_get_number(&dst->options, "base-index");
                    330:        dstwl = session_attach(dst, srcwl->window, dstidx, cause);
                    331:        if (dstwl == NULL)
                    332:                return (-1);
                    333:
1.25      nicm      334:        if (selectflag)
1.21      nicm      335:                session_select(dst, dstwl->idx);
1.25      nicm      336:        server_redraw_session_group(dst);
1.21      nicm      337:
                    338:        return (0);
                    339: }
                    340:
                    341: void
                    342: server_unlink_window(struct session *s, struct winlink *wl)
                    343: {
                    344:        if (session_detach(s, wl))
1.25      nicm      345:                server_destroy_session_group(s);
                    346:        else
                    347:                server_redraw_session_group(s);
1.29      nicm      348: }
                    349:
                    350: void
                    351: server_destroy_pane(struct window_pane *wp)
                    352: {
1.51      nicm      353:        struct window           *w = wp->window;
                    354:        int                      old_fd;
                    355:        struct screen_write_ctx  ctx;
                    356:        struct grid_cell         gc;
1.29      nicm      357:
1.51      nicm      358:        old_fd = wp->fd;
1.36      nicm      359:        if (wp->fd != -1) {
1.53      nicm      360:                bufferevent_free(wp->event);
1.36      nicm      361:                close(wp->fd);
                    362:                wp->fd = -1;
                    363:        }
1.29      nicm      364:
1.51      nicm      365:        if (options_get_number(&w->options, "remain-on-exit")) {
                    366:                if (old_fd == -1)
                    367:                        return;
                    368:                screen_write_start(&ctx, wp, &wp->base);
                    369:                screen_write_scrollregion(&ctx, 0, screen_size_y(ctx.s) - 1);
                    370:                screen_write_cursormove(&ctx, 0, screen_size_y(ctx.s) - 1);
                    371:                screen_write_linefeed(&ctx, 1);
                    372:                memcpy(&gc, &grid_default_cell, sizeof gc);
                    373:                gc.attr |= GRID_ATTR_BRIGHT;
                    374:                screen_write_puts(&ctx, &gc, "Pane is dead");
                    375:                screen_write_stop(&ctx);
                    376:                wp->flags |= PANE_REDRAW;
1.29      nicm      377:                return;
1.51      nicm      378:        }
1.29      nicm      379:
                    380:        layout_close_pane(wp);
                    381:        window_remove_pane(w, wp);
                    382:
                    383:        if (TAILQ_EMPTY(&w->panes))
                    384:                server_kill_window(w);
                    385:        else
                    386:                server_redraw_window(w);
1.25      nicm      387: }
                    388:
                    389: void
                    390: server_destroy_session_group(struct session *s)
                    391: {
                    392:        struct session_group    *sg;
                    393:
                    394:        if ((sg = session_group_find(s)) == NULL)
1.21      nicm      395:                server_destroy_session(s);
1.25      nicm      396:        else {
                    397:                TAILQ_FOREACH(s, &sg->sessions, gentry)
                    398:                        server_destroy_session(s);
                    399:                TAILQ_REMOVE(&session_groups, sg, entry);
1.60      nicm      400:                free(sg);
1.25      nicm      401:        }
1.19      nicm      402: }
                    403:
1.39      nicm      404: struct session *
                    405: server_next_session(struct session *s)
                    406: {
                    407:        struct session *s_loop, *s_out;
                    408:
                    409:        s_out = NULL;
1.47      nicm      410:        RB_FOREACH(s_loop, sessions, &sessions) {
                    411:                if (s_loop == s)
1.39      nicm      412:                        continue;
                    413:                if (s_out == NULL ||
                    414:                    timercmp(&s_loop->activity_time, &s_out->activity_time, <))
                    415:                        s_out = s_loop;
                    416:        }
                    417:        return (s_out);
                    418: }
                    419:
1.19      nicm      420: void
                    421: server_destroy_session(struct session *s)
                    422: {
                    423:        struct client   *c;
1.39      nicm      424:        struct session  *s_new;
1.19      nicm      425:        u_int            i;
1.31      nicm      426:
1.39      nicm      427:        if (!options_get_number(&s->options, "detach-on-destroy"))
                    428:                s_new = server_next_session(s);
                    429:        else
                    430:                s_new = NULL;
                    431:
1.19      nicm      432:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    433:                c = ARRAY_ITEM(&clients, i);
                    434:                if (c == NULL || c->session != s)
                    435:                        continue;
1.39      nicm      436:                if (s_new == NULL) {
                    437:                        c->session = NULL;
1.40      nicm      438:                        c->flags |= CLIENT_EXIT;
1.39      nicm      439:                } else {
1.46      nicm      440:                        c->last_session = NULL;
1.39      nicm      441:                        c->session = s_new;
1.55      nicm      442:                        notify_attached_session_changed(c);
1.49      nicm      443:                        session_update_activity(s_new);
1.39      nicm      444:                        server_redraw_client(c);
                    445:                }
1.19      nicm      446:        }
1.38      nicm      447:        recalculate_sizes();
1.42      nicm      448: }
                    449:
                    450: void
                    451: server_check_unattached (void)
                    452: {
                    453:        struct session  *s;
                    454:
                    455:        /*
                    456:         * If any sessions are no longer attached and have destroy-unattached
                    457:         * set, collect them.
                    458:         */
1.47      nicm      459:        RB_FOREACH(s, sessions, &sessions) {
                    460:                if (!(s->flags & SESSION_UNATTACHED))
1.42      nicm      461:                        continue;
                    462:                if (options_get_number (&s->options, "destroy-unattached"))
                    463:                        session_destroy(s);
                    464:        }
1.15      nicm      465: }
                    466:
                    467: void
                    468: server_set_identify(struct client *c)
                    469: {
                    470:        struct timeval  tv;
                    471:        int             delay;
                    472:
                    473:        delay = options_get_number(&c->session->options, "display-panes-time");
                    474:        tv.tv_sec = delay / 1000;
                    475:        tv.tv_usec = (delay % 1000) * 1000L;
1.31      nicm      476:
1.54      nicm      477:        if (event_initialized (&c->identify_timer))
                    478:                evtimer_del(&c->identify_timer);
1.28      nicm      479:        evtimer_set(&c->identify_timer, server_callback_identify, c);
                    480:        evtimer_add(&c->identify_timer, &tv);
1.15      nicm      481:
                    482:        c->flags |= CLIENT_IDENTIFY;
                    483:        c->tty.flags |= (TTY_FREEZE|TTY_NOCURSOR);
                    484:        server_redraw_client(c);
                    485: }
                    486:
                    487: void
                    488: server_clear_identify(struct client *c)
                    489: {
                    490:        if (c->flags & CLIENT_IDENTIFY) {
                    491:                c->flags &= ~CLIENT_IDENTIFY;
                    492:                c->tty.flags &= ~(TTY_FREEZE|TTY_NOCURSOR);
                    493:                server_redraw_client(c);
                    494:        }
1.28      nicm      495: }
                    496:
                    497: void
                    498: server_callback_identify(unused int fd, unused short events, void *data)
                    499: {
                    500:        struct client   *c = data;
                    501:
                    502:        server_clear_identify(c);
1.27      nicm      503: }
                    504:
                    505: void
                    506: server_update_event(struct client *c)
                    507: {
                    508:        short   events;
                    509:
                    510:        events = 0;
                    511:        if (!(c->flags & CLIENT_BAD))
                    512:                events |= EV_READ;
                    513:        if (c->ibuf.w.queued > 0)
                    514:                events |= EV_WRITE;
1.54      nicm      515:        if (event_initialized(&c->event))
                    516:                event_del(&c->event);
1.27      nicm      517:        event_set(&c->event, c->ibuf.fd, events, server_client_callback, c);
1.31      nicm      518:        event_add(&c->event, NULL);
1.57      nicm      519: }
                    520:
                    521: /* Push stdout to client if possible. */
                    522: void
                    523: server_push_stdout(struct client *c)
                    524: {
                    525:        struct msg_stdout_data data;
                    526:        size_t                 size;
                    527:
                    528:        size = EVBUFFER_LENGTH(c->stdout_data);
                    529:        if (size == 0)
                    530:                return;
                    531:        if (size > sizeof data.data)
                    532:                size = sizeof data.data;
                    533:
                    534:        memcpy(data.data, EVBUFFER_DATA(c->stdout_data), size);
                    535:        data.size = size;
                    536:
                    537:        if (server_write_client(c, MSG_STDOUT, &data, sizeof data) == 0)
                    538:                evbuffer_drain(c->stdout_data, size);
                    539: }
                    540:
                    541: /* Push stderr to client if possible. */
                    542: void
                    543: server_push_stderr(struct client *c)
                    544: {
                    545:        struct msg_stderr_data data;
                    546:        size_t                 size;
                    547:
                    548:        size = EVBUFFER_LENGTH(c->stderr_data);
                    549:        if (size == 0)
                    550:                return;
                    551:        if (size > sizeof data.data)
                    552:                size = sizeof data.data;
                    553:
                    554:        memcpy(data.data, EVBUFFER_DATA(c->stderr_data), size);
                    555:        data.size = size;
                    556:
                    557:        if (server_write_client(c, MSG_STDERR, &data, sizeof data) == 0)
                    558:                evbuffer_drain(c->stderr_data, size);
                    559: }
                    560:
                    561: /* Set stdin callback. */
                    562: int
                    563: server_set_stdin_callback(struct client *c, void (*cb)(struct client *, int,
                    564:     void *), void *cb_data, char **cause)
                    565: {
                    566:        if (c == NULL) {
                    567:                *cause = xstrdup("no client with stdin");
                    568:                return (-1);
                    569:        }
                    570:        if (c->flags & CLIENT_TERMINAL) {
                    571:                *cause = xstrdup("stdin is a tty");
                    572:                return (-1);
                    573:        }
                    574:        if (c->stdin_callback != NULL) {
                    575:                *cause = xstrdup("stdin in use");
                    576:                return (-1);
                    577:        }
                    578:
                    579:        c->stdin_callback_data = cb_data;
                    580:        c->stdin_callback = cb;
                    581:
                    582:        c->references++;
                    583:
                    584:        if (c->stdin_closed)
                    585:                c->stdin_callback (c, 1, c->stdin_callback_data);
1.61      nicm      586:
                    587:        server_write_client(c, MSG_STDIN, NULL, 0);
                    588:
1.57      nicm      589:        return (0);
1.1       nicm      590: }