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

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