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

1.40    ! nicm        1: /* $OpenBSD: server.c,v 1.39 2009/09/20 14:58:12 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;
1.40    ! nicm      801:        struct keylist          *keylist;
        !           802:        int                      key, status, xtimeout, mode, isprefix;
        !           803:        u_int                    i;
1.1       nicm      804:        u_char                   mouse[3];
                    805:
                    806:        xtimeout = options_get_number(&c->session->options, "repeat-time");
                    807:        if (xtimeout != 0 && c->flags & CLIENT_REPEAT) {
                    808:                if (gettimeofday(&tv, NULL) != 0)
1.39      nicm      809:                        fatal("gettimeofday failed");
1.1       nicm      810:                if (timercmp(&tv, &c->repeat_timer, >))
                    811:                        c->flags &= ~(CLIENT_PREFIX|CLIENT_REPEAT);
                    812:        }
                    813:
                    814:        /* Process keys. */
1.40    ! nicm      815:        keylist = options_get_data(&c->session->options, "prefix");
1.1       nicm      816:        while (tty_keys_next(&c->tty, &key, mouse) == 0) {
                    817:                server_activity = time(NULL);
                    818:
                    819:                if (c->session == NULL)
                    820:                        return;
1.32      nicm      821:                w = c->session->curw->window;
                    822:                wp = w->active; /* could die */
1.1       nicm      823:
1.32      nicm      824:                /* Special case: number keys jump to pane in identify mode. */
                    825:                if (c->flags & CLIENT_IDENTIFY && key >= '0' && key <= '9') {
                    826:                        wp = window_pane_at_index(w, key - '0');
                    827:                        if (wp != NULL && window_pane_visible(wp))
                    828:                                window_set_active_pane(w, wp);
                    829:                        server_clear_identify(c);
                    830:                        continue;
                    831:                }
                    832:
1.1       nicm      833:                status_message_clear(c);
1.26      nicm      834:                server_clear_identify(c);
1.1       nicm      835:                if (c->prompt_string != NULL) {
                    836:                        status_prompt_key(c, key);
                    837:                        continue;
                    838:                }
                    839:                if (server_locked)
                    840:                        continue;
                    841:
                    842:                /* Check for mouse keys. */
                    843:                if (key == KEYC_MOUSE) {
                    844:                        window_pane_mouse(wp, c, mouse[0], mouse[1], mouse[2]);
                    845:                        continue;
                    846:                }
                    847:
1.40    ! nicm      848:                /* Is this a prefix key? */
        !           849:                isprefix = 0;
        !           850:                for (i = 0; i < ARRAY_LENGTH(keylist); i++) {
        !           851:                        if (key == ARRAY_ITEM(keylist, i)) {
        !           852:                                isprefix = 1;
        !           853:                                break;
        !           854:                        }
        !           855:                }
        !           856:
1.1       nicm      857:                /* No previous prefix key. */
                    858:                if (!(c->flags & CLIENT_PREFIX)) {
1.40    ! nicm      859:                        if (isprefix)
1.1       nicm      860:                                c->flags |= CLIENT_PREFIX;
1.14      nicm      861:                        else {
                    862:                                /* Try as a non-prefix key binding. */
                    863:                                if ((bd = key_bindings_lookup(key)) == NULL)
                    864:                                        window_pane_key(wp, c, key);
                    865:                                else
                    866:                                        key_bindings_dispatch(bd, c);
                    867:                        }
1.1       nicm      868:                        continue;
                    869:                }
                    870:
                    871:                /* Prefix key already pressed. Reset prefix and lookup key. */
                    872:                c->flags &= ~CLIENT_PREFIX;
1.14      nicm      873:                if ((bd = key_bindings_lookup(key | KEYC_PREFIX)) == NULL) {
1.1       nicm      874:                        /* If repeating, treat this as a key, else ignore. */
                    875:                        if (c->flags & CLIENT_REPEAT) {
                    876:                                c->flags &= ~CLIENT_REPEAT;
1.40    ! nicm      877:                                if (isprefix)
1.1       nicm      878:                                        c->flags |= CLIENT_PREFIX;
                    879:                                else
                    880:                                        window_pane_key(wp, c, key);
                    881:                        }
                    882:                        continue;
                    883:                }
                    884:
                    885:                /* If already repeating, but this key can't repeat, skip it. */
                    886:                if (c->flags & CLIENT_REPEAT && !bd->can_repeat) {
                    887:                        c->flags &= ~CLIENT_REPEAT;
1.40    ! nicm      888:                        if (isprefix)
1.1       nicm      889:                                c->flags |= CLIENT_PREFIX;
                    890:                        else
                    891:                                window_pane_key(wp, c, key);
                    892:                        continue;
                    893:                }
                    894:
                    895:                /* If this key can repeat, reset the repeat flags and timer. */
                    896:                if (xtimeout != 0 && bd->can_repeat) {
                    897:                        c->flags |= CLIENT_PREFIX|CLIENT_REPEAT;
                    898:
                    899:                        tv.tv_sec = xtimeout / 1000;
                    900:                        tv.tv_usec = (xtimeout % 1000) * 1000L;
                    901:                        if (gettimeofday(&c->repeat_timer, NULL) != 0)
1.39      nicm      902:                                fatal("gettimeofday failed");
1.1       nicm      903:                        timeradd(&c->repeat_timer, &tv, &c->repeat_timer);
                    904:                }
                    905:
                    906:                /* Dispatch the command. */
                    907:                key_bindings_dispatch(bd, c);
                    908:        }
                    909:        if (c->session == NULL)
                    910:                return;
                    911:        wp = c->session->curw->window->active;  /* could die - do each loop */
                    912:        s = wp->screen;
                    913:
1.21      nicm      914:        /*
                    915:         * Update cursor position and mode settings. The scroll region and
                    916:         * attributes are cleared across poll(2) as this is the most likely
                    917:         * time a user may interrupt tmux, for example with ~^Z in ssh(1). This
                    918:         * is a compromise between excessive resets and likelihood of an
                    919:         * interrupt.
                    920:         *
                    921:         * tty_region/tty_reset/tty_update_mode already take care of not
                    922:         * resetting things that are already in their default state.
                    923:         */
1.1       nicm      924:        status = options_get_number(&c->session->options, "status");
1.17      nicm      925:        tty_region(&c->tty, 0, c->tty.sy - 1, 0);
1.11      nicm      926:        if (!window_pane_visible(wp) || wp->yoff + s->cy >= c->tty.sy - status)
                    927:                tty_cursor(&c->tty, 0, 0, 0, 0);
                    928:        else
1.1       nicm      929:                tty_cursor(&c->tty, s->cx, s->cy, wp->xoff, wp->yoff);
                    930:
                    931:        mode = s->mode;
                    932:        if (server_locked)
                    933:                mode &= ~TTY_NOCURSOR;
                    934:        tty_update_mode(&c->tty, mode);
1.21      nicm      935:        tty_reset(&c->tty);
1.1       nicm      936: }
                    937:
                    938: /* Lost a client. */
                    939: void
                    940: server_lost_client(struct client *c)
                    941: {
                    942:        u_int   i;
                    943:
                    944:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    945:                if (ARRAY_ITEM(&clients, i) == c)
                    946:                        ARRAY_SET(&clients, i, NULL);
                    947:        }
1.20      nicm      948:        log_debug("lost client %d", c->ibuf.fd);
1.1       nicm      949:
1.25      nicm      950:        /*
                    951:         * If CLIENT_TERMINAL hasn't been set, then tty_init hasn't been called
                    952:         * and tty_free might close an unrelated fd.
                    953:         */
                    954:        if (c->flags & CLIENT_TERMINAL)
                    955:                tty_free(&c->tty);
1.1       nicm      956:
                    957:        screen_free(&c->status);
                    958:
                    959:        if (c->title != NULL)
                    960:                xfree(c->title);
                    961:
                    962:        if (c->message_string != NULL)
                    963:                xfree(c->message_string);
                    964:
                    965:        if (c->prompt_string != NULL)
                    966:                xfree(c->prompt_string);
                    967:        if (c->prompt_buffer != NULL)
                    968:                xfree(c->prompt_buffer);
                    969:        for (i = 0; i < ARRAY_LENGTH(&c->prompt_hdata); i++)
                    970:                xfree(ARRAY_ITEM(&c->prompt_hdata, i));
                    971:        ARRAY_FREE(&c->prompt_hdata);
                    972:
                    973:        if (c->cwd != NULL)
                    974:                xfree(c->cwd);
                    975:
1.18      nicm      976:        close(c->ibuf.fd);
                    977:        imsg_clear(&c->ibuf);
1.31      nicm      978:
                    979:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                    980:                if (ARRAY_ITEM(&dead_clients, i) == NULL) {
                    981:                        ARRAY_SET(&dead_clients, i, c);
                    982:                        break;
                    983:                }
                    984:        }
                    985:        if (i == ARRAY_LENGTH(&dead_clients))
                    986:                ARRAY_ADD(&dead_clients, c);
                    987:        c->flags |= CLIENT_DEAD;
1.1       nicm      988:
                    989:        recalculate_sizes();
1.31      nicm      990: }
                    991:
                    992: /* Free dead, unreferenced clients and sessions. */
                    993: void
                    994: server_clean_dead(void)
                    995: {
                    996:        struct session  *s;
                    997:        struct client   *c;
                    998:        u_int            i;
                    999:
                   1000:        for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
                   1001:                s = ARRAY_ITEM(&dead_sessions, i);
                   1002:                if (s == NULL || s->references != 0)
                   1003:                        continue;
                   1004:                ARRAY_SET(&dead_sessions, i, NULL);
                   1005:                xfree(s);
                   1006:        }
                   1007:
                   1008:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                   1009:                c = ARRAY_ITEM(&dead_clients, i);
                   1010:                if (c == NULL || c->references != 0)
                   1011:                        continue;
                   1012:                ARRAY_SET(&dead_clients, i, NULL);
                   1013:                xfree(c);
                   1014:        }
1.1       nicm     1015: }
                   1016:
                   1017: /* Handle window data. */
                   1018: void
                   1019: server_handle_window(struct window *w, struct window_pane *wp)
                   1020: {
                   1021:        struct session  *s;
                   1022:        u_int            i;
                   1023:        int              update;
                   1024:
                   1025:        window_pane_parse(wp);
                   1026:
                   1027:        if ((w->flags & (WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT)) == 0)
                   1028:                return;
                   1029:
                   1030:        update = 0;
                   1031:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                   1032:                s = ARRAY_ITEM(&sessions, i);
                   1033:                if (s == NULL || !session_has(s, w))
                   1034:                        continue;
                   1035:
1.10      nicm     1036:                update += server_check_window_bell(s, w);
1.1       nicm     1037:                update += server_check_window_activity(s, w);
                   1038:                update += server_check_window_content(s, w, wp);
                   1039:        }
                   1040:        if (update)
                   1041:                server_status_window(w);
                   1042:
                   1043:        w->flags &= ~(WINDOW_BELL|WINDOW_ACTIVITY|WINDOW_CONTENT);
                   1044: }
                   1045:
                   1046: int
1.10      nicm     1047: server_check_window_bell(struct session *s, struct window *w)
1.1       nicm     1048: {
                   1049:        struct client   *c;
                   1050:        u_int            i;
1.10      nicm     1051:        int              action, visual;
1.1       nicm     1052:
                   1053:        if (!(w->flags & WINDOW_BELL))
                   1054:                return (0);
                   1055:        if (session_alert_has_window(s, w, WINDOW_BELL))
                   1056:                return (0);
                   1057:        session_alert_add(s, w, WINDOW_BELL);
                   1058:
                   1059:        action = options_get_number(&s->options, "bell-action");
                   1060:        switch (action) {
                   1061:        case BELL_ANY:
                   1062:                if (s->flags & SESSION_UNATTACHED)
                   1063:                        break;
1.10      nicm     1064:                visual = options_get_number(&s->options, "visual-bell");
1.1       nicm     1065:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                   1066:                        c = ARRAY_ITEM(&clients, i);
1.10      nicm     1067:                        if (c == NULL || c->session != s)
                   1068:                                continue;
                   1069:                        if (!visual) {
1.1       nicm     1070:                                tty_putcode(&c->tty, TTYC_BEL);
1.10      nicm     1071:                                continue;
                   1072:                        }
                   1073:                        if (c->session->curw->window == w) {
                   1074:                                status_message_set(c, "Bell in current window");
                   1075:                                continue;
                   1076:                        }
                   1077:                        status_message_set(c, "Bell in window %u",
                   1078:                            winlink_find_by_window(&s->windows, w)->idx);
1.1       nicm     1079:                }
                   1080:                break;
                   1081:        case BELL_CURRENT:
1.10      nicm     1082:                if (s->flags & SESSION_UNATTACHED)
1.1       nicm     1083:                        break;
1.10      nicm     1084:                visual = options_get_number(&s->options, "visual-bell");
1.1       nicm     1085:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                   1086:                        c = ARRAY_ITEM(&clients, i);
1.10      nicm     1087:                        if (c == NULL || c->session != s)
                   1088:                                continue;
                   1089:                        if (c->session->curw->window != w)
                   1090:                                continue;
                   1091:                        if (!visual) {
1.1       nicm     1092:                                tty_putcode(&c->tty, TTYC_BEL);
1.10      nicm     1093:                                continue;
                   1094:                        }
                   1095:                        status_message_set(c, "Bell in current window");
1.1       nicm     1096:                }
                   1097:                break;
                   1098:        }
                   1099:        return (1);
                   1100: }
                   1101:
                   1102: int
                   1103: server_check_window_activity(struct session *s, struct window *w)
                   1104: {
1.10      nicm     1105:        struct client   *c;
                   1106:        u_int            i;
                   1107:
1.1       nicm     1108:        if (!(w->flags & WINDOW_ACTIVITY))
                   1109:                return (0);
1.10      nicm     1110:
1.1       nicm     1111:        if (!options_get_number(&w->options, "monitor-activity"))
                   1112:                return (0);
1.10      nicm     1113:
1.1       nicm     1114:        if (session_alert_has_window(s, w, WINDOW_ACTIVITY))
                   1115:                return (0);
1.10      nicm     1116:        if (s->curw->window == w)
                   1117:                return (0);
                   1118:
1.1       nicm     1119:        session_alert_add(s, w, WINDOW_ACTIVITY);
1.10      nicm     1120:        if (s->flags & SESSION_UNATTACHED)
                   1121:                return (0);
                   1122:        if (options_get_number(&s->options, "visual-activity")) {
                   1123:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                   1124:                        c = ARRAY_ITEM(&clients, i);
                   1125:                        if (c == NULL || c->session != s)
                   1126:                                continue;
                   1127:                        status_message_set(c, "Activity in window %u",
                   1128:                            winlink_find_by_window(&s->windows, w)->idx);
                   1129:                }
                   1130:        }
                   1131:
1.1       nicm     1132:        return (1);
                   1133: }
                   1134:
                   1135: int
                   1136: server_check_window_content(
                   1137:     struct session *s, struct window *w, struct window_pane *wp)
                   1138: {
1.10      nicm     1139:        struct client   *c;
                   1140:        u_int            i;
                   1141:        char            *found, *ptr;
                   1142:
                   1143:        if (!(w->flags & WINDOW_ACTIVITY))      /* activity for new content */
                   1144:                return (0);
1.1       nicm     1145:
1.10      nicm     1146:        ptr = options_get_string(&w->options, "monitor-content");
                   1147:        if (ptr == NULL || *ptr == '\0')
1.1       nicm     1148:                return (0);
1.10      nicm     1149:
                   1150:        if (session_alert_has_window(s, w, WINDOW_CONTENT))
1.1       nicm     1151:                return (0);
1.10      nicm     1152:        if (s->curw->window == w)
1.1       nicm     1153:                return (0);
1.10      nicm     1154:
1.3       nicm     1155:        if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
1.1       nicm     1156:                return (0);
1.10      nicm     1157:        xfree(found);
                   1158:
1.1       nicm     1159:        session_alert_add(s, w, WINDOW_CONTENT);
1.10      nicm     1160:        if (s->flags & SESSION_UNATTACHED)
                   1161:                return (0);
                   1162:        if (options_get_number(&s->options, "visual-content")) {
                   1163:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                   1164:                        c = ARRAY_ITEM(&clients, i);
                   1165:                        if (c == NULL || c->session != s)
                   1166:                                continue;
                   1167:                        status_message_set(c, "Content in window %u",
                   1168:                            winlink_find_by_window(&s->windows, w)->idx);
                   1169:                }
                   1170:        }
                   1171:
1.1       nicm     1172:        return (1);
                   1173: }
                   1174:
1.2       nicm     1175: /* Check if window still exists. */
1.1       nicm     1176: void
                   1177: server_check_window(struct window *w)
                   1178: {
                   1179:        struct window_pane      *wp, *wq;
1.22      nicm     1180:        struct options          *oo = &w->options;
1.1       nicm     1181:        struct session          *s;
                   1182:        struct winlink          *wl;
1.35      nicm     1183:        u_int                    i;
1.22      nicm     1184:        int                      destroyed;
1.1       nicm     1185:
                   1186:        destroyed = 1;
                   1187:
                   1188:        wp = TAILQ_FIRST(&w->panes);
                   1189:        while (wp != NULL) {
                   1190:                wq = TAILQ_NEXT(wp, entry);
1.2       nicm     1191:                /*
                   1192:                 * If the pane has died and the remain-on-exit flag is not set,
                   1193:                 * remove the pane; otherwise, if the flag is set, don't allow
                   1194:                 * the window to be destroyed (or it'll close when the last
                   1195:                 * pane dies).
                   1196:                 */
1.23      nicm     1197:                if (wp->fd == -1 && !options_get_number(oo, "remain-on-exit")) {
1.11      nicm     1198:                        layout_close_pane(wp);
1.1       nicm     1199:                        window_remove_pane(w, wp);
                   1200:                        server_redraw_window(w);
1.2       nicm     1201:                } else
                   1202:                        destroyed = 0;
1.1       nicm     1203:                wp = wq;
                   1204:        }
                   1205:
                   1206:        if (!destroyed)
                   1207:                return;
                   1208:
                   1209:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                   1210:                s = ARRAY_ITEM(&sessions, i);
                   1211:                if (s == NULL)
                   1212:                        continue;
                   1213:                if (!session_has(s, w))
                   1214:                        continue;
                   1215:
                   1216:        restart:
                   1217:                /* Detach window and either redraw or kill clients. */
                   1218:                RB_FOREACH(wl, winlinks, &s->windows) {
                   1219:                        if (wl->window != w)
                   1220:                                continue;
1.34      nicm     1221:                        if (session_detach(s, wl)) {
                   1222:                                server_destroy_session(s);
                   1223:                                break;
1.1       nicm     1224:                        }
1.34      nicm     1225:                        server_redraw_session(s);
1.1       nicm     1226:                        goto restart;
                   1227:                }
                   1228:        }
                   1229:
                   1230:        recalculate_sizes();
                   1231: }
                   1232:
                   1233: /* Call any once-per-second timers. */
                   1234: void
                   1235: server_second_timers(void)
                   1236: {
                   1237:        struct window           *w;
1.28      nicm     1238:        struct client           *c;
1.1       nicm     1239:        struct window_pane      *wp;
                   1240:        u_int                    i;
                   1241:        int                      xtimeout;
                   1242:        struct tm                now, then;
                   1243:        static time_t            last_t = 0;
                   1244:        time_t                   t;
                   1245:
                   1246:        t = time(NULL);
1.28      nicm     1247:
1.6       nicm     1248:        xtimeout = options_get_number(&global_s_options, "lock-after-time");
1.1       nicm     1249:        if (xtimeout > 0 && t > server_activity + xtimeout)
                   1250:                server_lock();
                   1251:
                   1252:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                   1253:                w = ARRAY_ITEM(&windows, i);
                   1254:                if (w == NULL)
                   1255:                        continue;
                   1256:
                   1257:                TAILQ_FOREACH(wp, &w->panes, entry) {
                   1258:                        if (wp->mode != NULL && wp->mode->timer != NULL)
                   1259:                                wp->mode->timer(wp);
1.28      nicm     1260:                }
                   1261:        }
                   1262:
1.29      nicm     1263:        if (password_backoff != 0 && t >= password_backoff) {
1.28      nicm     1264:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                   1265:                        if ((c = ARRAY_ITEM(&clients, i)) != NULL)
                   1266:                                server_redraw_client(c);
1.1       nicm     1267:                }
1.29      nicm     1268:                password_backoff = 0;
1.1       nicm     1269:        }
                   1270:
                   1271:        /* Check for a minute having passed. */
                   1272:        gmtime_r(&t, &now);
                   1273:        gmtime_r(&last_t, &then);
                   1274:        if (now.tm_min == then.tm_min)
                   1275:                return;
                   1276:        last_t = t;
                   1277:
                   1278:        /* If locked, redraw all clients. */
                   1279:        if (server_locked) {
                   1280:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1.30      kili     1281:                        if ((c = ARRAY_ITEM(&clients, i)) != NULL)
                   1282:                                server_redraw_client(c);
1.1       nicm     1283:                }
                   1284:        }
                   1285: }
                   1286:
                   1287: /* Update socket execute permissions based on whether sessions are attached. */
                   1288: int
                   1289: server_update_socket(void)
                   1290: {
                   1291:        struct session  *s;
                   1292:        u_int            i;
                   1293:        static int       last = -1;
                   1294:        int              n;
                   1295:
                   1296:        n = 0;
                   1297:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                   1298:                s = ARRAY_ITEM(&sessions, i);
                   1299:                if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
                   1300:                        n++;
                   1301:                        break;
                   1302:                }
                   1303:        }
                   1304:
                   1305:        if (n != last) {
                   1306:                last = n;
                   1307:                if (n != 0)
                   1308:                        chmod(socket_path, S_IRWXU);
                   1309:                else
                   1310:                        chmod(socket_path, S_IRUSR|S_IWUSR);
                   1311:        }
                   1312:
                   1313:        return (n);
                   1314: }