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

Annotation of src/usr.bin/tmux/server.c, Revision 1.44

1.44    ! nicm        1: /* $OpenBSD: server.c,v 1.43 2009/09/24 07:02:56 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: #include <sys/ioctl.h>
                     21: #include <sys/socket.h>
                     22: #include <sys/stat.h>
                     23: #include <sys/un.h>
                     24: #include <sys/wait.h>
                     25:
                     26: #include <errno.h>
                     27: #include <fcntl.h>
1.5       nicm       28: #include <paths.h>
1.1       nicm       29: #include <signal.h>
                     30: #include <stdio.h>
                     31: #include <stdlib.h>
                     32: #include <string.h>
                     33: #include <syslog.h>
                     34: #include <termios.h>
                     35: #include <time.h>
                     36: #include <unistd.h>
                     37:
                     38: #include "tmux.h"
                     39:
                     40: /*
                     41:  * Main server functions.
                     42:  */
                     43:
                     44: /* Client list. */
                     45: struct clients  clients;
1.31      nicm       46: struct clients  dead_clients;
1.1       nicm       47:
1.13      nicm       48: void            server_create_client(int);
1.1       nicm       49: int             server_create_socket(void);
                     50: int             server_main(int);
                     51: void            server_shutdown(void);
1.42      nicm       52: int             server_should_shutdown(void);
1.1       nicm       53: void            server_child_signal(void);
                     54: void            server_fill_windows(struct pollfd **);
                     55: void            server_handle_windows(struct pollfd **);
                     56: void            server_fill_clients(struct pollfd **);
                     57: void            server_handle_clients(struct pollfd **);
1.13      nicm       58: void            server_accept_client(int);
1.1       nicm       59: void            server_handle_client(struct client *);
                     60: void            server_handle_window(struct window *, struct window_pane *);
1.10      nicm       61: int             server_check_window_bell(struct session *, struct window *);
1.1       nicm       62: int             server_check_window_activity(struct session *,
                     63:                      struct window *);
                     64: int             server_check_window_content(struct session *, struct window *,
                     65:                      struct window_pane *);
1.31      nicm       66: void            server_clean_dead(void);
1.1       nicm       67: void            server_lost_client(struct client *);
                     68: void            server_check_window(struct window *);
                     69: void            server_check_redraw(struct client *);
1.38      nicm       70: void            server_set_title(struct client *);
1.1       nicm       71: void            server_check_timers(struct client *);
                     72: void            server_second_timers(void);
                     73: int             server_update_socket(void);
                     74:
                     75: /* Create a new client. */
1.13      nicm       76: void
1.1       nicm       77: server_create_client(int fd)
                     78: {
                     79:        struct client   *c;
                     80:        int              mode;
                     81:        u_int            i;
                     82:
                     83:        if ((mode = fcntl(fd, F_GETFL)) == -1)
                     84:                fatal("fcntl failed");
                     85:        if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
                     86:                fatal("fcntl failed");
                     87:        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
                     88:                fatal("fcntl failed");
                     89:
                     90:        c = xcalloc(1, sizeof *c);
1.31      nicm       91:        c->references = 0;
1.18      nicm       92:        imsg_init(&c->ibuf, fd);
1.44    ! nicm       93:
        !            94:        if (gettimeofday(&c->tv, NULL) != 0)
        !            95:                fatal("gettimeofday failed");
1.1       nicm       96:
                     97:        ARRAY_INIT(&c->prompt_hdata);
                     98:
                     99:        c->tty.fd = -1;
                    100:        c->title = NULL;
                    101:
                    102:        c->session = NULL;
                    103:        c->tty.sx = 80;
1.37      nicm      104:        c->tty.sy = 24;
1.1       nicm      105:        screen_init(&c->status, c->tty.sx, 1, 0);
                    106:
                    107:        c->message_string = NULL;
                    108:
                    109:        c->prompt_string = NULL;
                    110:        c->prompt_buffer = NULL;
                    111:        c->prompt_index = 0;
                    112:
                    113:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    114:                if (ARRAY_ITEM(&clients, i) == NULL) {
                    115:                        ARRAY_SET(&clients, i, c);
1.13      nicm      116:                        return;
1.1       nicm      117:                }
                    118:        }
                    119:        ARRAY_ADD(&clients, c);
1.20      nicm      120:        log_debug("new client %d", fd);
1.1       nicm      121: }
                    122:
                    123: /* Fork new server. */
                    124: int
                    125: server_start(char *path)
                    126: {
1.16      nicm      127:        struct client   *c;
                    128:        int              pair[2], srv_fd;
                    129:        char            *cause;
                    130:        char             rpathbuf[MAXPATHLEN];
1.1       nicm      131:
                    132:        /* The first client is special and gets a socketpair; create it. */
                    133:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
                    134:                fatal("socketpair failed");
                    135:
                    136:        switch (fork()) {
                    137:        case -1:
                    138:                fatal("fork failed");
                    139:        case 0:
                    140:                break;
                    141:        default:
                    142:                close(pair[1]);
                    143:                return (pair[0]);
                    144:        }
                    145:        close(pair[0]);
                    146:
                    147:        /*
                    148:         * Must daemonise before loading configuration as the PID changes so
                    149:         * $TMUX would be wrong for sessions created in the config file.
                    150:         */
1.16      nicm      151:        if (daemon(1, 0) != 0)
1.1       nicm      152:                fatal("daemon failed");
                    153:
1.16      nicm      154:        logfile("server");
                    155:        log_debug("server started, pid %ld", (long) getpid());
                    156:
1.1       nicm      157:        ARRAY_INIT(&windows);
                    158:        ARRAY_INIT(&clients);
1.31      nicm      159:        ARRAY_INIT(&dead_clients);
1.1       nicm      160:        ARRAY_INIT(&sessions);
1.31      nicm      161:        ARRAY_INIT(&dead_sessions);
1.15      nicm      162:        mode_key_init_trees();
1.1       nicm      163:        key_bindings_init();
                    164:        utf8_build();
                    165:
                    166:        server_activity = time(NULL);
                    167:
                    168:        start_time = time(NULL);
                    169:        socket_path = path;
                    170:
1.16      nicm      171:        if (realpath(socket_path, rpathbuf) == NULL)
                    172:                strlcpy(rpathbuf, socket_path, sizeof rpathbuf);
                    173:        log_debug("socket path %s", socket_path);
                    174:        setproctitle("server (%s)", rpathbuf);
                    175:
                    176:        srv_fd = server_create_socket();
                    177:        server_create_client(pair[1]);
                    178:
1.7       nicm      179:        if (access(SYSTEM_CFG, R_OK) != 0) {
                    180:                if (errno != ENOENT) {
1.16      nicm      181:                        xasprintf(
                    182:                            &cause, "%s: %s", strerror(errno), SYSTEM_CFG);
                    183:                        goto error;
                    184:                }
1.24      nicm      185:        } else if (load_cfg(SYSTEM_CFG, NULL, &cause) != 0)
1.16      nicm      186:                goto error;
1.24      nicm      187:        if (cfg_file != NULL && load_cfg(cfg_file, NULL, &cause) != 0)
1.16      nicm      188:                goto error;
1.5       nicm      189:
1.16      nicm      190:        exit(server_main(srv_fd));
1.1       nicm      191:
1.16      nicm      192: error:
                    193:        /* Write the error and shutdown the server. */
                    194:        c = ARRAY_FIRST(&clients);
1.1       nicm      195:
1.16      nicm      196:        server_write_error(c, cause);
                    197:        xfree(cause);
1.1       nicm      198:
1.27      nicm      199:        sigterm = 1;
1.16      nicm      200:        server_shutdown();
1.1       nicm      201:
                    202:        exit(server_main(srv_fd));
                    203: }
                    204:
                    205: /* Create server socket. */
                    206: int
                    207: server_create_socket(void)
                    208: {
                    209:        struct sockaddr_un      sa;
                    210:        size_t                  size;
                    211:        mode_t                  mask;
                    212:        int                     fd, mode;
                    213:
                    214:        memset(&sa, 0, sizeof sa);
                    215:        sa.sun_family = AF_UNIX;
                    216:        size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
                    217:        if (size >= sizeof sa.sun_path) {
                    218:                errno = ENAMETOOLONG;
                    219:                fatal("socket failed");
                    220:        }
                    221:        unlink(sa.sun_path);
                    222:
                    223:        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
                    224:                fatal("socket failed");
                    225:
                    226:        mask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
                    227:        if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
                    228:                fatal("bind failed");
                    229:        umask(mask);
                    230:
                    231:        if (listen(fd, 16) == -1)
                    232:                fatal("listen failed");
                    233:
                    234:        if ((mode = fcntl(fd, F_GETFL)) == -1)
                    235:                fatal("fcntl failed");
                    236:        if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
                    237:                fatal("fcntl failed");
                    238:        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
                    239:                fatal("fcntl failed");
                    240:
                    241:        return (fd);
                    242: }
                    243:
                    244: /* Main server loop. */
                    245: int
                    246: server_main(int srv_fd)
                    247: {
                    248:        struct window   *w;
                    249:        struct pollfd   *pfds, *pfd;
                    250:        int              nfds, xtimeout;
1.42      nicm      251:        u_int            i;
1.1       nicm      252:        time_t           now, last;
                    253:
                    254:        siginit();
1.20      nicm      255:        log_debug("server socket is %d", srv_fd);
1.1       nicm      256:
                    257:        last = time(NULL);
                    258:
                    259:        pfds = NULL;
                    260:        for (;;) {
                    261:                /* If sigterm, kill all windows and clients. */
                    262:                if (sigterm)
                    263:                        server_shutdown();
                    264:
1.42      nicm      265:                /* Stop if no sessions or clients left. */
                    266:                if (server_should_shutdown())
                    267:                        break;
                    268:
1.1       nicm      269:                /* Handle child exit. */
                    270:                if (sigchld) {
                    271:                        server_child_signal();
                    272:                        sigchld = 0;
                    273:                }
                    274:
                    275:                /* Recreate socket on SIGUSR1. */
                    276:                if (sigusr1) {
                    277:                        close(srv_fd);
                    278:                        srv_fd = server_create_socket();
                    279:                        sigusr1 = 0;
                    280:                }
                    281:
                    282:                /* Initialise pollfd array. */
                    283:                nfds = 1;
                    284:                for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    285:                        w = ARRAY_ITEM(&windows, i);
                    286:                        if (w != NULL)
                    287:                                nfds += window_count_panes(w);
                    288:                }
                    289:                nfds += ARRAY_LENGTH(&clients) * 2;
                    290:                pfds = xrealloc(pfds, nfds, sizeof *pfds);
                    291:                memset(pfds, 0, nfds * sizeof *pfds);
                    292:                pfd = pfds;
                    293:
                    294:                /* Fill server socket. */
                    295:                pfd->fd = srv_fd;
                    296:                pfd->events = POLLIN;
                    297:                pfd++;
                    298:
                    299:                /* Fill window and client sockets. */
                    300:                server_fill_windows(&pfd);
                    301:                server_fill_clients(&pfd);
                    302:
                    303:                /* Update socket permissions. */
                    304:                xtimeout = INFTIM;
1.27      nicm      305:                if (server_update_socket() != 0)
1.1       nicm      306:                        xtimeout = POLL_TIMEOUT;
                    307:
                    308:                /* Do the poll. */
1.4       nicm      309:                if (poll(pfds, nfds, xtimeout) == -1) {
1.1       nicm      310:                        if (errno == EAGAIN || errno == EINTR)
                    311:                                continue;
                    312:                        fatal("poll failed");
                    313:                }
                    314:                pfd = pfds;
                    315:
                    316:                /* Handle server socket. */
                    317:                if (pfd->revents & (POLLERR|POLLNVAL|POLLHUP))
                    318:                        fatalx("lost server socket");
                    319:                if (pfd->revents & POLLIN) {
                    320:                        server_accept_client(srv_fd);
                    321:                        continue;
                    322:                }
                    323:                pfd++;
                    324:
                    325:                /* Call second-based timers. */
                    326:                now = time(NULL);
                    327:                if (now != last) {
                    328:                        last = now;
                    329:                        server_second_timers();
                    330:                }
                    331:
                    332:                /* Set window names. */
                    333:                set_window_names();
                    334:
                    335:                /*
                    336:                 * Handle window and client sockets. Clients can create
                    337:                 * windows, so windows must come first to avoid messing up by
                    338:                 * increasing the array size.
                    339:                 */
                    340:                server_handle_windows(&pfd);
                    341:                server_handle_clients(&pfd);
                    342:
1.8       nicm      343:                /* Collect any unset key bindings. */
                    344:                key_bindings_clean();
1.31      nicm      345:
                    346:                /* Collect dead clients and sessions. */
                    347:                server_clean_dead();
1.1       nicm      348:        }
                    349:        if (pfds != NULL)
                    350:                xfree(pfds);
                    351:
                    352:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    353:                if (ARRAY_ITEM(&sessions, i) != NULL)
                    354:                        session_destroy(ARRAY_ITEM(&sessions, i));
                    355:        }
                    356:        ARRAY_FREE(&sessions);
                    357:
                    358:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    359:                if (ARRAY_ITEM(&clients, i) != NULL)
                    360:                        server_lost_client(ARRAY_ITEM(&clients, i));
                    361:        }
                    362:        ARRAY_FREE(&clients);
                    363:
1.15      nicm      364:        mode_key_free_trees();
1.1       nicm      365:        key_bindings_free();
                    366:
                    367:        close(srv_fd);
                    368:
                    369:        unlink(socket_path);
                    370:        xfree(socket_path);
                    371:
1.6       nicm      372:        options_free(&global_s_options);
                    373:        options_free(&global_w_options);
1.1       nicm      374:
                    375:        return (0);
                    376: }
                    377:
                    378: /* Kill all clients. */
                    379: void
                    380: server_shutdown(void)
                    381: {
                    382:        struct session  *s;
                    383:        struct client   *c;
                    384:        u_int            i, j;
                    385:
1.42      nicm      386:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    387:                c = ARRAY_ITEM(&clients, i);
                    388:                if (c != NULL) {
                    389:                        if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
                    390:                                server_lost_client(c);
                    391:                        else
                    392:                                server_write_client(c, MSG_SHUTDOWN, NULL, 0);
                    393:                }
                    394:        }
                    395:
1.1       nicm      396:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    397:                s = ARRAY_ITEM(&sessions, i);
                    398:                for (j = 0; j < ARRAY_LENGTH(&clients); j++) {
                    399:                        c = ARRAY_ITEM(&clients, j);
                    400:                        if (c != NULL && c->session == s) {
                    401:                                s = NULL;
                    402:                                break;
                    403:                        }
                    404:                }
                    405:                if (s != NULL)
                    406:                        session_destroy(s);
                    407:        }
1.42      nicm      408: }
                    409:
                    410: /* Check if the server should be shutting down (no more clients or windows). */
                    411: int
                    412: server_should_shutdown(void)
                    413: {
                    414:        u_int   i;
1.1       nicm      415:
1.42      nicm      416:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    417:                if (ARRAY_ITEM(&sessions, i) != NULL)
                    418:                        return (0);
                    419:        }
1.1       nicm      420:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1.42      nicm      421:                if (ARRAY_ITEM(&clients, i) != NULL)
                    422:                        return (0);
1.1       nicm      423:        }
1.42      nicm      424:        return (1);
1.1       nicm      425: }
                    426:
                    427: /* Handle SIGCHLD. */
                    428: void
                    429: server_child_signal(void)
                    430: {
                    431:        struct window           *w;
                    432:        struct window_pane      *wp;
                    433:        int                      status;
                    434:        pid_t                    pid;
                    435:        u_int                    i;
                    436:
                    437:        for (;;) {
                    438:                switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
                    439:                case -1:
                    440:                        if (errno == ECHILD)
                    441:                                return;
1.39      nicm      442:                        fatal("waitpid failed");
1.1       nicm      443:                case 0:
                    444:                        return;
                    445:                }
                    446:                if (!WIFSTOPPED(status))
                    447:                        continue;
                    448:                if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
                    449:                        continue;
                    450:
                    451:                for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    452:                        w = ARRAY_ITEM(&windows, i);
                    453:                        if (w == NULL)
                    454:                                continue;
                    455:                        TAILQ_FOREACH(wp, &w->panes, entry) {
                    456:                                if (wp->pid == pid) {
                    457:                                        if (killpg(pid, SIGCONT) != 0)
                    458:                                                kill(pid, SIGCONT);
                    459:                                }
                    460:                        }
                    461:                }
                    462:        }
                    463: }
                    464:
                    465: /* Fill window pollfds. */
                    466: void
                    467: server_fill_windows(struct pollfd **pfd)
                    468: {
                    469:        struct window           *w;
                    470:        struct window_pane      *wp;
                    471:        u_int                    i;
                    472:
                    473:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    474:                w = ARRAY_ITEM(&windows, i);
                    475:                if (w == NULL)
                    476:                        continue;
                    477:
                    478:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    479:                        (*pfd)->fd = wp->fd;
                    480:                        if (wp->fd != -1) {
                    481:                                (*pfd)->events = POLLIN;
                    482:                                if (BUFFER_USED(wp->out) > 0)
                    483:                                        (*pfd)->events |= POLLOUT;
                    484:                        }
                    485:                        (*pfd)++;
                    486:                }
                    487:        }
                    488: }
                    489:
                    490: /* Handle window pollfds. */
                    491: void
                    492: server_handle_windows(struct pollfd **pfd)
                    493: {
                    494:        struct window           *w;
                    495:        struct window_pane      *wp;
                    496:        u_int                    i;
                    497:
                    498:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    499:                w = ARRAY_ITEM(&windows, i);
                    500:                if (w == NULL)
                    501:                        continue;
                    502:
                    503:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    504:                        if (wp->fd != -1) {
                    505:                                if (buffer_poll(*pfd, wp->in, wp->out) != 0) {
                    506:                                        close(wp->fd);
                    507:                                        wp->fd = -1;
                    508:                                } else
                    509:                                        server_handle_window(w, wp);
                    510:                        }
                    511:                        (*pfd)++;
                    512:                }
                    513:
                    514:                server_check_window(w);
                    515:        }
                    516: }
                    517:
                    518: /* Check for general redraw on client. */
                    519: void
                    520: server_check_redraw(struct client *c)
                    521: {
                    522:        struct session          *s;
                    523:        struct window_pane      *wp;
                    524:        int                      flags, redraw;
                    525:
                    526:        if (c == NULL || c->session == NULL)
                    527:                return;
                    528:        s = c->session;
                    529:
                    530:        flags = c->tty.flags & TTY_FREEZE;
                    531:        c->tty.flags &= ~TTY_FREEZE;
                    532:
                    533:        if (c->flags & (CLIENT_REDRAW|CLIENT_STATUS)) {
1.38      nicm      534:                if (options_get_number(&s->options, "set-titles"))
                    535:                        server_set_title(c);
                    536:
1.1       nicm      537:                if (c->message_string != NULL)
                    538:                        redraw = status_message_redraw(c);
                    539:                else if (c->prompt_string != NULL)
                    540:                        redraw = status_prompt_redraw(c);
                    541:                else
                    542:                        redraw = status_redraw(c);
                    543:                if (!redraw)
                    544:                        c->flags &= ~CLIENT_STATUS;
                    545:        }
                    546:
                    547:        if (c->flags & CLIENT_REDRAW) {
1.41      nicm      548:                screen_redraw_screen(c, 0);
1.1       nicm      549:                c->flags &= ~CLIENT_STATUS;
                    550:        } else {
                    551:                TAILQ_FOREACH(wp, &c->session->curw->window->panes, entry) {
                    552:                        if (wp->flags & PANE_REDRAW)
                    553:                                screen_redraw_pane(c, wp);
                    554:                }
                    555:        }
                    556:
                    557:        if (c->flags & CLIENT_STATUS)
1.9       nicm      558:                screen_redraw_screen(c, 1);
1.1       nicm      559:
                    560:        c->tty.flags |= flags;
                    561:
                    562:        c->flags &= ~(CLIENT_REDRAW|CLIENT_STATUS);
1.38      nicm      563: }
                    564:
                    565: /* Set client title. */
                    566: void
                    567: server_set_title(struct client *c)
                    568: {
                    569:        struct session  *s = c->session;
                    570:        const char      *template;
                    571:        char            *title;
                    572:
                    573:        template = options_get_string(&s->options, "set-titles-string");
                    574:
                    575:        title = status_replace(c->session, template, time(NULL));
                    576:        if (c->title == NULL || strcmp(title, c->title) != 0) {
                    577:                if (c->title != NULL)
                    578:                        xfree(c->title);
                    579:                c->title = xstrdup(title);
                    580:                tty_set_title(&c->tty, c->title);
                    581:        }
                    582:        xfree(title);
1.1       nicm      583: }
                    584:
                    585: /* Check for timers on client. */
                    586: void
                    587: server_check_timers(struct client *c)
                    588: {
                    589:        struct session  *s;
                    590:        struct timeval   tv;
                    591:        u_int            interval;
                    592:
                    593:        if (c == NULL || c->session == NULL)
                    594:                return;
                    595:        s = c->session;
                    596:
                    597:        if (gettimeofday(&tv, NULL) != 0)
1.39      nicm      598:                fatal("gettimeofday failed");
1.1       nicm      599:
1.26      nicm      600:        if (c->flags & CLIENT_IDENTIFY && timercmp(&tv, &c->identify_timer, >))
                    601:                server_clear_identify(c);
                    602:
1.1       nicm      603:        if (c->message_string != NULL && timercmp(&tv, &c->message_timer, >))
                    604:                status_message_clear(c);
                    605:
                    606:        if (c->message_string != NULL || c->prompt_string != NULL) {
                    607:                /*
                    608:                 * Don't need timed redraw for messages/prompts so bail now.
                    609:                 * The status timer isn't reset when they are redrawn anyway.
                    610:                 */
                    611:                return;
                    612:        }
                    613:        if (!options_get_number(&s->options, "status"))
                    614:                return;
                    615:
                    616:        /* Check timer; resolution is only a second so don't be too clever. */
                    617:        interval = options_get_number(&s->options, "status-interval");
                    618:        if (interval == 0)
                    619:                return;
                    620:        if (tv.tv_sec < c->status_timer.tv_sec ||
                    621:            ((u_int) tv.tv_sec) - c->status_timer.tv_sec >= interval)
                    622:                c->flags |= CLIENT_STATUS;
                    623: }
                    624:
                    625: /* Fill client pollfds. */
                    626: void
                    627: server_fill_clients(struct pollfd **pfd)
                    628: {
                    629:        struct client           *c;
                    630:        struct window           *w;
                    631:        struct window_pane      *wp;
                    632:        u_int                    i;
                    633:
                    634:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    635:                c = ARRAY_ITEM(&clients, i);
                    636:
                    637:                server_check_timers(c);
                    638:                server_check_redraw(c);
                    639:
                    640:                if (c == NULL)
                    641:                        (*pfd)->fd = -1;
                    642:                else {
1.18      nicm      643:                        (*pfd)->fd = c->ibuf.fd;
1.16      nicm      644:                        if (!(c->flags & CLIENT_BAD))
1.18      nicm      645:                                (*pfd)->events |= POLLIN;
                    646:                        if (c->ibuf.w.queued > 0)
1.1       nicm      647:                                (*pfd)->events |= POLLOUT;
                    648:                }
                    649:                (*pfd)++;
                    650:
                    651:                if (c == NULL || c->flags & CLIENT_SUSPENDED ||
                    652:                    c->tty.fd == -1 || c->session == NULL)
                    653:                        (*pfd)->fd = -1;
                    654:                else {
                    655:                        (*pfd)->fd = c->tty.fd;
                    656:                        (*pfd)->events = POLLIN;
                    657:                        if (BUFFER_USED(c->tty.out) > 0)
                    658:                                (*pfd)->events |= POLLOUT;
                    659:                }
                    660:                (*pfd)++;
                    661:        }
                    662:
                    663:        /*
                    664:         * Clear any window redraw flags (will have been redrawn as part of
                    665:         * client).
                    666:         */
                    667:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    668:                w = ARRAY_ITEM(&windows, i);
                    669:                if (w == NULL)
                    670:                        continue;
                    671:
                    672:                w->flags &= ~WINDOW_REDRAW;
                    673:                TAILQ_FOREACH(wp, &w->panes, entry)
                    674:                        wp->flags &= ~PANE_REDRAW;
                    675:        }
                    676: }
                    677:
                    678: /* Handle client pollfds. */
                    679: void
                    680: server_handle_clients(struct pollfd **pfd)
                    681: {
                    682:        struct client   *c;
                    683:        u_int            i;
                    684:
                    685:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    686:                c = ARRAY_ITEM(&clients, i);
                    687:
                    688:                if (c != NULL) {
1.18      nicm      689:                        if ((*pfd)->revents & (POLLERR|POLLNVAL|POLLHUP)) {
1.1       nicm      690:                                server_lost_client(c);
                    691:                                (*pfd) += 2;
                    692:                                continue;
1.18      nicm      693:                        }
                    694:
                    695:                        if ((*pfd)->revents & POLLOUT) {
                    696:                                if (msgbuf_write(&c->ibuf.w) < 0) {
                    697:                                        server_lost_client(c);
                    698:                                        (*pfd) += 2;
                    699:                                        continue;
                    700:                                }
                    701:                        }
                    702:
                    703:                        if (c->flags & CLIENT_BAD) {
                    704:                                if (c->ibuf.w.queued == 0)
1.16      nicm      705:                                        server_lost_client(c);
                    706:                                (*pfd) += 2;
1.18      nicm      707:                                continue;
                    708:                        } else if ((*pfd)->revents & POLLIN) {
                    709:                                if (server_msg_dispatch(c) != 0) {
                    710:                                        server_lost_client(c);
                    711:                                        (*pfd) += 2;
                    712:                                        continue;
                    713:                                }
                    714:                        }
1.1       nicm      715:                }
                    716:                (*pfd)++;
                    717:
                    718:                if (c != NULL && !(c->flags & CLIENT_SUSPENDED) &&
                    719:                    c->tty.fd != -1 && c->session != NULL) {
                    720:                        if (buffer_poll(*pfd, c->tty.in, c->tty.out) != 0)
                    721:                                server_lost_client(c);
                    722:                        else
                    723:                                server_handle_client(c);
                    724:                }
                    725:                (*pfd)++;
                    726:        }
                    727: }
                    728:
                    729: /* accept(2) and create new client. */
1.13      nicm      730: void
1.1       nicm      731: server_accept_client(int srv_fd)
                    732: {
                    733:        struct sockaddr_storage sa;
                    734:        socklen_t               slen = sizeof sa;
                    735:        int                     fd;
                    736:
                    737:        fd = accept(srv_fd, (struct sockaddr *) &sa, &slen);
                    738:        if (fd == -1) {
                    739:                if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
1.13      nicm      740:                        return;
1.1       nicm      741:                fatal("accept failed");
                    742:        }
                    743:        if (sigterm) {
                    744:                close(fd);
1.13      nicm      745:                return;
1.1       nicm      746:        }
1.13      nicm      747:        server_create_client(fd);
1.1       nicm      748: }
                    749:
                    750: /* Input data from client. */
                    751: void
                    752: server_handle_client(struct client *c)
                    753: {
1.32      nicm      754:        struct window           *w;
1.1       nicm      755:        struct window_pane      *wp;
                    756:        struct screen           *s;
                    757:        struct timeval           tv;
                    758:        struct key_binding      *bd;
1.40      nicm      759:        struct keylist          *keylist;
                    760:        int                      key, status, xtimeout, mode, isprefix;
                    761:        u_int                    i;
1.1       nicm      762:        u_char                   mouse[3];
                    763:
                    764:        xtimeout = options_get_number(&c->session->options, "repeat-time");
                    765:        if (xtimeout != 0 && c->flags & CLIENT_REPEAT) {
                    766:                if (gettimeofday(&tv, NULL) != 0)
1.39      nicm      767:                        fatal("gettimeofday failed");
1.1       nicm      768:                if (timercmp(&tv, &c->repeat_timer, >))
                    769:                        c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
                    770:        }
                    771:
                    772:        /* Process keys. */
1.40      nicm      773:        keylist = options_get_data(&c->session->options, "prefix");
1.1       nicm      774:        while (tty_keys_next(&c->tty, &key, mouse) == 0) {
                    775:                server_activity = time(NULL);
                    776:
                    777:                if (c->session == NULL)
                    778:                        return;
1.32      nicm      779:                w = c->session->curw->window;
                    780:                wp = w->active; /* could die */
1.1       nicm      781:
1.32      nicm      782:                /* Special case: number keys jump to pane in identify mode. */
                    783:                if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
                    784:                        wp = window_pane_at_index(w, key - '0');
                    785:                        if (wp != NULL && window_pane_visible(wp))
                    786:                                window_set_active_pane(w, wp);
                    787:                        server_clear_identify(c);
                    788:                        continue;
                    789:                }
                    790:
1.1       nicm      791:                status_message_clear(c);
1.26      nicm      792:                server_clear_identify(c);
1.1       nicm      793:                if (c->prompt_string != NULL) {
                    794:                        status_prompt_key(c, key);
                    795:                        continue;
                    796:                }
                    797:
                    798:                /* Check for mouse keys. */
                    799:                if (key == KEYC_MOUSE) {
                    800:                        window_pane_mouse(wp, c, mouse[0], mouse[1], mouse[2]);
                    801:                        continue;
                    802:                }
                    803:
1.40      nicm      804:                /* Is this a prefix key? */
                    805:                isprefix = 0;
                    806:                for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
                    807:                        if (key == ARRAY_ITEM(keylist, i)) {
                    808:                                isprefix = 1;
                    809:                                break;
                    810:                        }
                    811:                }
                    812:
1.1       nicm      813:                /* No previous prefix key. */
                    814:                if (!(c->flags & CLIENT_PREFIX)) {
1.40      nicm      815:                        if (isprefix)
1.1       nicm      816:                                c->flags |= CLIENT_PREFIX;
1.14      nicm      817:                        else {
                    818:                                /* Try as a non-prefix key binding. */
                    819:                                if ((bd = key_bindings_lookup(key)) == NULL)
                    820:                                        window_pane_key(wp, c, key);
                    821:                                else
                    822:                                        key_bindings_dispatch(bd, c);
                    823:                        }
1.1       nicm      824:                        continue;
                    825:                }
                    826:
                    827:                /* Prefix key already pressed. Reset prefix and lookup key. */
                    828:                c->flags &= ~CLIENT_PREFIX;
1.14      nicm      829:                if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
1.1       nicm      830:                        /* If repeating, treat this as a key, else ignore. */
                    831:                        if (c->flags & CLIENT_REPEAT) {
                    832:                                c->flags &= ~CLIENT_REPEAT;
1.40      nicm      833:                                if (isprefix)
1.1       nicm      834:                                        c->flags |= CLIENT_PREFIX;
                    835:                                else
                    836:                                        window_pane_key(wp, c, key);
                    837:                        }
                    838:                        continue;
                    839:                }
                    840:
                    841:                /* If already repeating, but this key can't repeat, skip it. */
                    842:                if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
                    843:                        c->flags &= ~CLIENT_REPEAT;
1.40      nicm      844:                        if (isprefix)
1.1       nicm      845:                                c->flags |= CLIENT_PREFIX;
                    846:                        else
                    847:                                window_pane_key(wp, c, key);
                    848:                        continue;
                    849:                }
                    850:
                    851:                /* If this key can repeat, reset the repeat flags and timer. */
                    852:                if (xtimeout != 0 && bd->can_repeat) {
                    853:                        c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
                    854:
                    855:                        tv.tv_sec = xtimeout / 1000;
                    856:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                    857:                        if (gettimeofday(&c->repeat_timer, NULL) != 0)
1.39      nicm      858:                                fatal("gettimeofday failed");
1.1       nicm      859:                        timeradd(&c->repeat_timer, &tv, &c->repeat_timer);
                    860:                }
                    861:
                    862:                /* Dispatch the command. */
                    863:                key_bindings_dispatch(bd, c);
                    864:        }
                    865:        if (c->session == NULL)
                    866:                return;
                    867:        wp = c->session->curw->window->active;  /* could die - do each loop */
                    868:        s = wp->screen;
                    869:
1.21      nicm      870:        /*
                    871:         * Update cursor position and mode settings. The scroll region and
                    872:         * attributes are cleared across poll(2) as this is the most likely
                    873:         * time a user may interrupt tmux, for example with ~^Z in ssh(1). This
                    874:         * is a compromise between excessive resets and likelihood of an
                    875:         * interrupt.
                    876:         *
                    877:         * tty_region/tty_reset/tty_update_mode already take care of not
                    878:         * resetting things that are already in their default state.
                    879:         */
1.1       nicm      880:        status = options_get_number(&c->session->options, "status");
1.17      nicm      881:        tty_region(&c->tty, 0, c->tty.sy - 1, 0);
1.11      nicm      882:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                    883:                tty_cursor(&c->tty, 0, 0, 0, 0);
                    884:        else
1.1       nicm      885:                tty_cursor(&c->tty, s->cx, s->cy, wp->xoff, wp->yoff);
                    886:
                    887:        mode = s->mode;
                    888:        tty_update_mode(&c->tty, mode);
1.21      nicm      889:        tty_reset(&c->tty);
1.1       nicm      890: }
                    891:
                    892: /* Lost a client. */
                    893: void
                    894: server_lost_client(struct client *c)
                    895: {
                    896:        u_int   i;
                    897:
                    898:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    899:                if (ARRAY_ITEM(&clients, i) == c)
                    900:                        ARRAY_SET(&clients, i, NULL);
                    901:        }
1.20      nicm      902:        log_debug("lost client %d", c->ibuf.fd);
1.1       nicm      903:
1.25      nicm      904:        /*
                    905:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    906:         * and tty_free might close an unrelated fd.
                    907:         */
                    908:        if (c->flags & CLIENT_TERMINAL)
                    909:                tty_free(&c->tty);
1.1       nicm      910:
                    911:        screen_free(&c->status);
                    912:
                    913:        if (c->title != NULL)
                    914:                xfree(c->title);
                    915:
                    916:        if (c->message_string != NULL)
                    917:                xfree(c->message_string);
                    918:
                    919:        if (c->prompt_string != NULL)
                    920:                xfree(c->prompt_string);
                    921:        if (c->prompt_buffer != NULL)
                    922:                xfree(c->prompt_buffer);
                    923:        for (i = 0; i < ARRAY_LENGTH(&c->prompt_hdata); i++)
                    924:                xfree(ARRAY_ITEM(&c->prompt_hdata, i));
                    925:        ARRAY_FREE(&c->prompt_hdata);
                    926:
                    927:        if (c->cwd != NULL)
                    928:                xfree(c->cwd);
                    929:
1.18      nicm      930:        close(c->ibuf.fd);
                    931:        imsg_clear(&c->ibuf);
1.31      nicm      932:
                    933:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                    934:                if (ARRAY_ITEM(&dead_clients, i) == NULL) {
                    935:                        ARRAY_SET(&dead_clients, i, c);
                    936:                        break;
                    937:                }
                    938:        }
                    939:        if (i == ARRAY_LENGTH(&dead_clients))
                    940:                ARRAY_ADD(&dead_clients, c);
                    941:        c->flags |= CLIENT_DEAD;
1.1       nicm      942:
                    943:        recalculate_sizes();
1.31      nicm      944: }
                    945:
                    946: /* Free dead, unreferenced clients and sessions. */
                    947: void
                    948: server_clean_dead(void)
                    949: {
                    950:        struct session  *s;
                    951:        struct client   *c;
                    952:        u_int            i;
                    953:
                    954:        for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
                    955:                s = ARRAY_ITEM(&dead_sessions, i);
                    956:                if (s == NULL || s->references != 0)
                    957:                        continue;
                    958:                ARRAY_SET(&dead_sessions, i, NULL);
                    959:                xfree(s);
                    960:        }
                    961:
                    962:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                    963:                c = ARRAY_ITEM(&dead_clients, i);
                    964:                if (c == NULL || c->references != 0)
                    965:                        continue;
                    966:                ARRAY_SET(&dead_clients, i, NULL);
                    967:                xfree(c);
                    968:        }
1.1       nicm      969: }
                    970:
                    971: /* Handle window data. */
                    972: void
                    973: server_handle_window(struct window *w, struct window_pane *wp)
                    974: {
                    975:        struct session  *s;
                    976:        u_int            i;
                    977:        int              update;
                    978:
                    979:        window_pane_parse(wp);
                    980:
                    981:        if ((w->flags & (WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT)) == 0)
                    982:                return;
                    983:
                    984:        update = 0;
                    985:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    986:                s = ARRAY_ITEM(&sessions, i);
                    987:                if (s == NULL || !session_has(s, w))
                    988:                        continue;
                    989:
1.10      nicm      990:                update += server_check_window_bell(s, w);
1.1       nicm      991:                update += server_check_window_activity(s, w);
                    992:                update += server_check_window_content(s, w, wp);
                    993:        }
                    994:        if (update)
                    995:                server_status_window(w);
                    996:
                    997:        w->flags &= ~(WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT);
                    998: }
                    999:
                   1000: int
1.10      nicm     1001: server_check_window_bell(struct session *s, struct window *w)
1.1       nicm     1002: {
                   1003:        struct client   *c;
                   1004:        u_int            i;
1.10      nicm     1005:        int              action, visual;
1.1       nicm     1006:
                   1007:        if (!(w->flags & WINDOW_BELL))
                   1008:                return (0);
                   1009:        if (session_alert_has_window(s, w, WINDOW_BELL))
                   1010:                return (0);
                   1011:        session_alert_add(s, w, WINDOW_BELL);
                   1012:
                   1013:        action = options_get_number(&s->options, "bell-action");
                   1014:        switch (action) {
                   1015:        case BELL_ANY:
                   1016:                if (s->flags & SESSION_UNATTACHED)
                   1017:                        break;
1.10      nicm     1018:                visual = options_get_number(&s->options, "visual-bell");
1.1       nicm     1019:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                   1020:                        c = ARRAY_ITEM(&clients, i);
1.10      nicm     1021:                        if (c == NULL || c->session != s)
                   1022:                                continue;
                   1023:                        if (!visual) {
1.1       nicm     1024:                                tty_putcode(&c->tty, TTYC_BEL);
1.10      nicm     1025:                                continue;
                   1026:                        }
                   1027:                        if (c->session->curw->window == w) {
                   1028:                                status_message_set(c, "Bell in current window");
                   1029:                                continue;
                   1030:                        }
                   1031:                        status_message_set(c, "Bell in window %u",
                   1032:                            winlink_find_by_window(&s->windows, w)->idx);
1.1       nicm     1033:                }
                   1034:                break;
                   1035:        case BELL_CURRENT:
1.10      nicm     1036:                if (s->flags & SESSION_UNATTACHED)
1.1       nicm     1037:                        break;
1.10      nicm     1038:                visual = options_get_number(&s->options, "visual-bell");
1.1       nicm     1039:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                   1040:                        c = ARRAY_ITEM(&clients, i);
1.10      nicm     1041:                        if (c == NULL || c->session != s)
                   1042:                                continue;
                   1043:                        if (c->session->curw->window != w)
                   1044:                                continue;
                   1045:                        if (!visual) {
1.1       nicm     1046:                                tty_putcode(&c->tty, TTYC_BEL);
1.10      nicm     1047:                                continue;
                   1048:                        }
                   1049:                        status_message_set(c, "Bell in current window");
1.1       nicm     1050:                }
                   1051:                break;
                   1052:        }
                   1053:        return (1);
                   1054: }
                   1055:
                   1056: int
                   1057: server_check_window_activity(struct session *s, struct window *w)
                   1058: {
1.10      nicm     1059:        struct client   *c;
                   1060:        u_int            i;
                   1061:
1.1       nicm     1062:        if (!(w->flags & WINDOW_ACTIVITY))
                   1063:                return (0);
1.10      nicm     1064:
1.1       nicm     1065:        if (!options_get_number(&w->options, "monitor-activity"))
                   1066:                return (0);
1.10      nicm     1067:
1.1       nicm     1068:        if (session_alert_has_window(s, w, WINDOW_ACTIVITY))
                   1069:                return (0);
1.10      nicm     1070:        if (s->curw->window == w)
                   1071:                return (0);
                   1072:
1.1       nicm     1073:        session_alert_add(s, w, WINDOW_ACTIVITY);
1.10      nicm     1074:        if (s->flags & SESSION_UNATTACHED)
                   1075:                return (0);
                   1076:        if (options_get_number(&s->options, "visual-activity")) {
                   1077:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                   1078:                        c = ARRAY_ITEM(&clients, i);
                   1079:                        if (c == NULL || c->session != s)
                   1080:                                continue;
                   1081:                        status_message_set(c, "Activity in window %u",
                   1082:                            winlink_find_by_window(&s->windows, w)->idx);
                   1083:                }
                   1084:        }
                   1085:
1.1       nicm     1086:        return (1);
                   1087: }
                   1088:
                   1089: int
                   1090: server_check_window_content(
                   1091:     struct session *s, struct window *w, struct window_pane *wp)
                   1092: {
1.10      nicm     1093:        struct client   *c;
                   1094:        u_int            i;
                   1095:        char            *found, *ptr;
                   1096:
                   1097:        if (!(w->flags & WINDOW_ACTIVITY))      /* activity for new content */
                   1098:                return (0);
1.1       nicm     1099:
1.10      nicm     1100:        ptr = options_get_string(&w->options, "monitor-content");
                   1101:        if (ptr == NULL || *ptr == '\0')
1.1       nicm     1102:                return (0);
1.10      nicm     1103:
                   1104:        if (session_alert_has_window(s, w, WINDOW_CONTENT))
1.1       nicm     1105:                return (0);
1.10      nicm     1106:        if (s->curw->window == w)
1.1       nicm     1107:                return (0);
1.10      nicm     1108:
1.3       nicm     1109:        if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
1.1       nicm     1110:                return (0);
1.10      nicm     1111:        xfree(found);
                   1112:
1.1       nicm     1113:        session_alert_add(s, w, WINDOW_CONTENT);
1.10      nicm     1114:        if (s->flags & SESSION_UNATTACHED)
                   1115:                return (0);
                   1116:        if (options_get_number(&s->options, "visual-content")) {
                   1117:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                   1118:                        c = ARRAY_ITEM(&clients, i);
                   1119:                        if (c == NULL || c->session != s)
                   1120:                                continue;
                   1121:                        status_message_set(c, "Content in window %u",
                   1122:                            winlink_find_by_window(&s->windows, w)->idx);
                   1123:                }
                   1124:        }
                   1125:
1.1       nicm     1126:        return (1);
                   1127: }
                   1128:
1.2       nicm     1129: /* Check if window still exists. */
1.1       nicm     1130: void
                   1131: server_check_window(struct window *w)
                   1132: {
                   1133:        struct window_pane      *wp, *wq;
1.22      nicm     1134:        struct options          *oo = &w->options;
1.1       nicm     1135:        struct session          *s;
                   1136:        struct winlink          *wl;
1.35      nicm     1137:        u_int                    i;
1.22      nicm     1138:        int                      destroyed;
1.1       nicm     1139:
                   1140:        destroyed = 1;
                   1141:
                   1142:        wp = TAILQ_FIRST(&w->panes);
                   1143:        while (wp != NULL) {
                   1144:                wq = TAILQ_NEXT(wp, entry);
1.2       nicm     1145:                /*
                   1146:                 * If the pane has died and the remain-on-exit flag is not set,
                   1147:                 * remove the pane; otherwise, if the flag is set, don't allow
                   1148:                 * the window to be destroyed (or it'll close when the last
                   1149:                 * pane dies).
                   1150:                 */
1.23      nicm     1151:                if (wp->fd == -1 && !options_get_number(oo, "remain-on-exit")) {
1.11      nicm     1152:                        layout_close_pane(wp);
1.1       nicm     1153:                        window_remove_pane(w, wp);
                   1154:                        server_redraw_window(w);
1.2       nicm     1155:                } else
                   1156:                        destroyed = 0;
1.1       nicm     1157:                wp = wq;
                   1158:        }
                   1159:
                   1160:        if (!destroyed)
                   1161:                return;
                   1162:
                   1163:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                   1164:                s = ARRAY_ITEM(&sessions, i);
                   1165:                if (s == NULL)
                   1166:                        continue;
                   1167:                if (!session_has(s, w))
                   1168:                        continue;
                   1169:
                   1170:        restart:
                   1171:                /* Detach window and either redraw or kill clients. */
                   1172:                RB_FOREACH(wl, winlinks, &s->windows) {
                   1173:                        if (wl->window != w)
                   1174:                                continue;
1.34      nicm     1175:                        if (session_detach(s, wl)) {
                   1176:                                server_destroy_session(s);
                   1177:                                break;
1.1       nicm     1178:                        }
1.34      nicm     1179:                        server_redraw_session(s);
1.1       nicm     1180:                        goto restart;
                   1181:                }
                   1182:        }
                   1183:
                   1184:        recalculate_sizes();
                   1185: }
                   1186:
                   1187: /* Call any once-per-second timers. */
                   1188: void
                   1189: server_second_timers(void)
                   1190: {
                   1191:        struct window           *w;
                   1192:        struct window_pane      *wp;
                   1193:        u_int                    i;
                   1194:        int                      xtimeout;
                   1195:        time_t                   t;
                   1196:
                   1197:        t = time(NULL);
1.28      nicm     1198:
1.6       nicm     1199:        xtimeout = options_get_number(&global_s_options, "lock-after-time");
1.43      nicm     1200:        if (xtimeout > 0 && t > server_activity + xtimeout) {
1.1       nicm     1201:                server_lock();
1.43      nicm     1202:                recalculate_sizes();
                   1203:        }
1.1       nicm     1204:
                   1205:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                   1206:                w = ARRAY_ITEM(&windows, i);
                   1207:                if (w == NULL)
                   1208:                        continue;
                   1209:
                   1210:                TAILQ_FOREACH(wp, &w->panes, entry) {
                   1211:                        if (wp->mode != NULL && wp->mode->timer != NULL)
                   1212:                                wp->mode->timer(wp);
                   1213:                }
                   1214:        }
                   1215: }
                   1216:
                   1217: /* Update socket execute permissions based on whether sessions are attached. */
                   1218: int
                   1219: server_update_socket(void)
                   1220: {
                   1221:        struct session  *s;
                   1222:        u_int            i;
                   1223:        static int       last = -1;
                   1224:        int              n;
                   1225:
                   1226:        n = 0;
                   1227:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                   1228:                s = ARRAY_ITEM(&sessions, i);
                   1229:                if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
                   1230:                        n++;
                   1231:                        break;
                   1232:                }
                   1233:        }
                   1234:
                   1235:        if (n != last) {
                   1236:                last = n;
                   1237:                if (n != 0)
                   1238:                        chmod(socket_path, S_IRWXU);
                   1239:                else
                   1240:                        chmod(socket_path, S_IRUSR|S_IWUSR);
                   1241:        }
                   1242:
                   1243:        return (n);
                   1244: }