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

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