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

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