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

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