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

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