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

1.81    ! nicm        1: /* $OpenBSD: server.c,v 1.80 2009/12/03 22:50: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>
1.66      nicm       27: #include <event.h>
1.1       nicm       28: #include <fcntl.h>
1.5       nicm       29: #include <paths.h>
1.1       nicm       30: #include <signal.h>
                     31: #include <stdio.h>
                     32: #include <stdlib.h>
                     33: #include <string.h>
                     34: #include <syslog.h>
                     35: #include <termios.h>
                     36: #include <time.h>
                     37: #include <unistd.h>
                     38:
                     39: #include "tmux.h"
                     40:
                     41: /*
                     42:  * Main server functions.
                     43:  */
                     44:
                     45: /* Client list. */
                     46: struct clients  clients;
1.31      nicm       47: struct clients  dead_clients;
1.1       nicm       48:
1.66      nicm       49: int             server_fd;
                     50: int             server_shutdown;
                     51: struct event    server_ev_accept;
                     52: struct event    server_ev_sigterm;
                     53: struct event    server_ev_sigusr1;
                     54: struct event    server_ev_sigchld;
                     55: struct event    server_ev_second;
1.45      nicm       56:
1.1       nicm       57: int             server_create_socket(void);
1.66      nicm       58: void            server_loop(void);
1.42      nicm       59: int             server_should_shutdown(void);
1.66      nicm       60: void            server_send_shutdown(void);
                     61: void            server_clean_dead(void);
                     62: void            server_accept_callback(int, short, void *);
                     63: void            server_signal_callback(int, short, void *);
1.1       nicm       64: void            server_child_signal(void);
1.66      nicm       65: void            server_child_exited(pid_t, int);
                     66: void            server_child_stopped(pid_t, int);
                     67: void            server_second_callback(int, short, void *);
1.46      nicm       68: void            server_lock_server(void);
                     69: void            server_lock_sessions(void);
1.45      nicm       70:
1.60      nicm       71: /* Create server socket. */
                     72: int
                     73: server_create_socket(void)
1.1       nicm       74: {
1.60      nicm       75:        struct sockaddr_un      sa;
                     76:        size_t                  size;
                     77:        mode_t                  mask;
                     78:        int                     fd, mode;
                     79:
                     80:        memset(&sa, 0, sizeof sa);
                     81:        sa.sun_family = AF_UNIX;
                     82:        size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
                     83:        if (size >= sizeof sa.sun_path) {
                     84:                errno = ENAMETOOLONG;
                     85:                fatal("socket failed");
                     86:        }
                     87:        unlink(sa.sun_path);
                     88:
                     89:        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
                     90:                fatal("socket failed");
                     91:
                     92:        mask = umask(S_IXUSR|S_IRWXG|S_IRWXO);
                     93:        if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
                     94:                fatal("bind failed");
                     95:        umask(mask);
                     96:
                     97:        if (listen(fd, 16) == -1)
                     98:                fatal("listen failed");
1.1       nicm       99:
                    100:        if ((mode = fcntl(fd, F_GETFL)) == -1)
                    101:                fatal("fcntl failed");
                    102:        if (fcntl(fd, F_SETFL, mode|O_NONBLOCK) == -1)
                    103:                fatal("fcntl failed");
                    104:        if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
                    105:                fatal("fcntl failed");
                    106:
1.75      nicm      107:        server_update_socket();
                    108:
1.60      nicm      109:        return (fd);
                    110: }
                    111:
1.1       nicm      112: /* Fork new server. */
                    113: int
                    114: server_start(char *path)
                    115: {
1.16      nicm      116:        struct client   *c;
1.66      nicm      117:        int              pair[2];
                    118:        char            *cause, rpathbuf[MAXPATHLEN];
                    119:        struct timeval   tv;
1.1       nicm      120:
                    121:        /* The first client is special and gets a socketpair; create it. */
                    122:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
                    123:                fatal("socketpair failed");
                    124:
                    125:        switch (fork()) {
                    126:        case -1:
                    127:                fatal("fork failed");
                    128:        case 0:
                    129:                break;
                    130:        default:
                    131:                close(pair[1]);
                    132:                return (pair[0]);
                    133:        }
                    134:        close(pair[0]);
                    135:
                    136:        /*
                    137:         * Must daemonise before loading configuration as the PID changes so
                    138:         * $TMUX would be wrong for sessions created in the config file.
                    139:         */
1.16      nicm      140:        if (daemon(1, 0) != 0)
1.1       nicm      141:                fatal("daemon failed");
                    142:
1.16      nicm      143:        logfile("server");
                    144:        log_debug("server started, pid %ld", (long) getpid());
                    145:
1.1       nicm      146:        ARRAY_INIT(&windows);
                    147:        ARRAY_INIT(&clients);
1.31      nicm      148:        ARRAY_INIT(&dead_clients);
1.1       nicm      149:        ARRAY_INIT(&sessions);
1.31      nicm      150:        ARRAY_INIT(&dead_sessions);
1.47      nicm      151:        TAILQ_INIT(&session_groups);
1.15      nicm      152:        mode_key_init_trees();
1.1       nicm      153:        key_bindings_init();
                    154:        utf8_build();
                    155:
                    156:        start_time = time(NULL);
                    157:        socket_path = path;
                    158:
1.16      nicm      159:        if (realpath(socket_path, rpathbuf) == NULL)
                    160:                strlcpy(rpathbuf, socket_path, sizeof rpathbuf);
                    161:        log_debug("socket path %s", socket_path);
                    162:        setproctitle("server (%s)", rpathbuf);
                    163:
1.71      nicm      164:        event_init();
                    165:
1.66      nicm      166:        server_fd = server_create_socket();
1.60      nicm      167:        server_client_create(pair[1]);
1.16      nicm      168:
1.64      nicm      169:        if (access(SYSTEM_CFG, R_OK) == 0) {
                    170:                if (load_cfg(SYSTEM_CFG, NULL, &cause) != 0)
1.16      nicm      171:                        goto error;
1.64      nicm      172:        } else if (errno != ENOENT) {
                    173:                xasprintf(&cause, "%s: %s", strerror(errno), SYSTEM_CFG);
1.16      nicm      174:                goto error;
1.64      nicm      175:        }
1.24      nicm      176:        if (cfg_file != NULL && load_cfg(cfg_file, NULL, &cause) != 0)
1.16      nicm      177:                goto error;
1.66      nicm      178:
                    179:        event_set(&server_ev_accept,
                    180:            server_fd, EV_READ|EV_PERSIST, server_accept_callback, NULL);
                    181:        event_add(&server_ev_accept, NULL);
                    182:
                    183:        memset(&tv, 0, sizeof tv);
                    184:        tv.tv_sec = 1;
                    185:        evtimer_set(&server_ev_second, server_second_callback, NULL);
                    186:        evtimer_add(&server_ev_second, &tv);
                    187:
                    188:        server_signal_set();
                    189:        server_loop();
                    190:        exit(0);
1.1       nicm      191:
1.16      nicm      192: error:
                    193:        /* Write the error and shutdown the server. */
                    194:        c = ARRAY_FIRST(&clients);
1.1       nicm      195:
1.16      nicm      196:        server_write_error(c, cause);
1.70      nicm      197:        server_write_client(c, MSG_EXIT, NULL, 0);
1.16      nicm      198:        xfree(cause);
1.1       nicm      199:
1.66      nicm      200:        server_shutdown = 1;
1.1       nicm      201:
1.66      nicm      202:        server_signal_set();
                    203:        server_loop();
                    204:        exit(1);
1.1       nicm      205: }
                    206:
                    207: /* Main server loop. */
1.66      nicm      208: void
                    209: server_loop(void)
1.1       nicm      210: {
1.66      nicm      211:        while (!server_should_shutdown()) {
                    212:                event_loop(EVLOOP_ONCE);
1.1       nicm      213:
1.60      nicm      214:                server_window_loop();
                    215:                server_client_loop();
1.1       nicm      216:
1.8       nicm      217:                key_bindings_clean();
1.66      nicm      218:                server_clean_dead();
1.80      nicm      219:        }
1.66      nicm      220: }
1.31      nicm      221:
1.66      nicm      222: /* Check if the server should be shutting down (no more clients or windows). */
                    223: int
                    224: server_should_shutdown(void)
                    225: {
                    226:        u_int   i;
1.1       nicm      227:
                    228:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    229:                if (ARRAY_ITEM(&sessions, i) != NULL)
1.66      nicm      230:                        return (0);
1.1       nicm      231:        }
                    232:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    233:                if (ARRAY_ITEM(&clients, i) != NULL)
1.66      nicm      234:                        return (0);
1.1       nicm      235:        }
1.66      nicm      236:        return (1);
1.1       nicm      237: }
                    238:
1.66      nicm      239: /* Shutdown the server by killing all clients and windows. */
1.1       nicm      240: void
1.66      nicm      241: server_send_shutdown(void)
1.1       nicm      242: {
1.66      nicm      243:        struct client   *c;
1.1       nicm      244:        struct session  *s;
1.66      nicm      245:        u_int            i;
1.1       nicm      246:
1.42      nicm      247:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    248:                c = ARRAY_ITEM(&clients, i);
                    249:                if (c != NULL) {
                    250:                        if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
1.60      nicm      251:                                server_client_lost(c);
1.42      nicm      252:                        else
                    253:                                server_write_client(c, MSG_SHUTDOWN, NULL, 0);
1.66      nicm      254:                        c->session = NULL;
1.42      nicm      255:                }
                    256:        }
                    257:
1.1       nicm      258:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.66      nicm      259:                if ((s = ARRAY_ITEM(&sessions, i)) != NULL)
1.1       nicm      260:                        session_destroy(s);
                    261:        }
1.42      nicm      262: }
                    263:
1.66      nicm      264: /* Free dead, unreferenced clients and sessions. */
                    265: void
                    266: server_clean_dead(void)
                    267: {
                    268:        struct session  *s;
                    269:        struct client   *c;
                    270:        u_int            i;
                    271:
                    272:        for (i = 0; i < ARRAY_LENGTH(&dead_sessions); i++) {
                    273:                s = ARRAY_ITEM(&dead_sessions, i);
                    274:                if (s == NULL || s->references != 0)
                    275:                        continue;
                    276:                ARRAY_SET(&dead_sessions, i, NULL);
                    277:                xfree(s);
                    278:        }
                    279:
                    280:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                    281:                c = ARRAY_ITEM(&dead_clients, i);
                    282:                if (c == NULL || c->references != 0)
                    283:                        continue;
                    284:                ARRAY_SET(&dead_clients, i, NULL);
                    285:                xfree(c);
                    286:        }
                    287: }
                    288:
                    289: /* Update socket execute permissions based on whether sessions are attached. */
1.75      nicm      290: void
1.66      nicm      291: server_update_socket(void)
1.42      nicm      292: {
1.66      nicm      293:        struct session  *s;
                    294:        u_int            i;
                    295:        static int       last = -1;
                    296:        int              n;
1.1       nicm      297:
1.66      nicm      298:        n = 0;
1.42      nicm      299:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.66      nicm      300:                s = ARRAY_ITEM(&sessions, i);
                    301:                if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
                    302:                        n++;
                    303:                        break;
                    304:                }
                    305:        }
                    306:
                    307:        if (n != last) {
                    308:                last = n;
                    309:                if (n != 0)
                    310:                        chmod(socket_path, S_IRWXU);
                    311:                else
                    312:                        chmod(socket_path, S_IRUSR|S_IWUSR);
                    313:        }
                    314: }
                    315:
                    316: /* Callback for server socket. */
1.78      nicm      317: /* ARGSUSED */
1.66      nicm      318: void
                    319: server_accept_callback(int fd, short events, unused void *data)
                    320: {
                    321:        struct sockaddr_storage sa;
                    322:        socklen_t               slen = sizeof sa;
                    323:        int                     newfd;
                    324:
                    325:        if (!(events & EV_READ))
                    326:                return;
                    327:
                    328:        newfd = accept(fd, (struct sockaddr *) &sa, &slen);
                    329:        if (newfd == -1) {
                    330:                if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
                    331:                        return;
                    332:                fatal("accept failed");
                    333:        }
                    334:        if (server_shutdown) {
                    335:                close(newfd);
                    336:                return;
1.42      nicm      337:        }
1.66      nicm      338:        server_client_create(newfd);
                    339: }
                    340:
                    341: /* Set up server signal handling. */
                    342: void
                    343: server_signal_set(void)
                    344: {
                    345:        struct sigaction         sigact;
                    346:
                    347:        memset(&sigact, 0, sizeof sigact);
                    348:        sigemptyset(&sigact.sa_mask);
                    349:        sigact.sa_flags = SA_RESTART;
                    350:        sigact.sa_handler = SIG_IGN;
                    351:        if (sigaction(SIGINT, &sigact, NULL) != 0)
                    352:                fatal("sigaction failed");
                    353:        if (sigaction(SIGPIPE, &sigact, NULL) != 0)
                    354:                fatal("sigaction failed");
                    355:        if (sigaction(SIGUSR2, &sigact, NULL) != 0)
                    356:                fatal("sigaction failed");
                    357:        if (sigaction(SIGTSTP, &sigact, NULL) != 0)
                    358:                fatal("sigaction failed");
1.81    ! nicm      359:        if (sigaction(SIGHUP, &sigact, NULL) != 0)
        !           360:                fatal("sigaction failed");
1.66      nicm      361:
                    362:        signal_set(&server_ev_sigchld, SIGCHLD, server_signal_callback, NULL);
                    363:        signal_add(&server_ev_sigchld, NULL);
                    364:        signal_set(&server_ev_sigterm, SIGTERM, server_signal_callback, NULL);
                    365:        signal_add(&server_ev_sigterm, NULL);
                    366:        signal_set(&server_ev_sigusr1, SIGUSR1, server_signal_callback, NULL);
                    367:        signal_add(&server_ev_sigusr1, NULL);
                    368: }
                    369:
                    370: /* Destroy server signal events. */
                    371: void
                    372: server_signal_clear(void)
                    373: {
                    374:        struct sigaction         sigact;
                    375:
                    376:        memset(&sigact, 0, sizeof sigact);
                    377:        sigemptyset(&sigact.sa_mask);
                    378:        sigact.sa_flags = SA_RESTART;
                    379:        sigact.sa_handler = SIG_DFL;
                    380:        if (sigaction(SIGINT, &sigact, NULL) != 0)
                    381:                fatal("sigaction failed");
                    382:        if (sigaction(SIGPIPE, &sigact, NULL) != 0)
                    383:                fatal("sigaction failed");
                    384:        if (sigaction(SIGUSR2, &sigact, NULL) != 0)
                    385:                fatal("sigaction failed");
                    386:        if (sigaction(SIGTSTP, &sigact, NULL) != 0)
1.81    ! nicm      387:                fatal("sigaction failed");
        !           388:        if (sigaction(SIGHUP, &sigact, NULL) != 0)
1.66      nicm      389:                fatal("sigaction failed");
                    390:
                    391:        signal_del(&server_ev_sigchld);
                    392:        signal_del(&server_ev_sigterm);
                    393:        signal_del(&server_ev_sigusr1);
                    394: }
                    395:
                    396: /* Signal handler. */
1.78      nicm      397: /* ARGSUSED */
1.66      nicm      398: void
                    399: server_signal_callback(int sig, unused short events, unused void *data)
                    400: {
                    401:        switch (sig) {
                    402:        case SIGTERM:
                    403:                server_shutdown = 1;
                    404:                server_send_shutdown();
                    405:                break;
                    406:        case SIGCHLD:
                    407:                server_child_signal();
                    408:                break;
                    409:        case SIGUSR1:
                    410:                event_del(&server_ev_accept);
                    411:                close(server_fd);
                    412:                server_fd = server_create_socket();
                    413:                event_set(&server_ev_accept, server_fd,
                    414:                    EV_READ|EV_PERSIST, server_accept_callback, NULL);
                    415:                event_add(&server_ev_accept, NULL);
                    416:                break;
1.1       nicm      417:        }
                    418: }
                    419:
                    420: /* Handle SIGCHLD. */
                    421: void
                    422: server_child_signal(void)
                    423: {
1.66      nicm      424:        int      status;
                    425:        pid_t    pid;
1.1       nicm      426:
                    427:        for (;;) {
                    428:                switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
                    429:                case -1:
                    430:                        if (errno == ECHILD)
                    431:                                return;
1.39      nicm      432:                        fatal("waitpid failed");
1.1       nicm      433:                case 0:
                    434:                        return;
                    435:                }
1.66      nicm      436:                if (WIFSTOPPED(status))
                    437:                        server_child_stopped(pid, status);
1.79      nicm      438:                else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66      nicm      439:                        server_child_exited(pid, status);
                    440:        }
                    441: }
                    442:
                    443: /* Handle exited children. */
                    444: void
                    445: server_child_exited(pid_t pid, int status)
                    446: {
                    447:        struct window           *w;
                    448:        struct window_pane      *wp;
                    449:        struct job              *job;
                    450:        u_int                    i;
                    451:
                    452:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    453:                if ((w = ARRAY_ITEM(&windows, i)) == NULL)
                    454:                        continue;
                    455:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    456:                        if (wp->pid == pid) {
1.77      nicm      457:                                server_destroy_pane(wp);
                    458:                                break;
1.53      nicm      459:                        }
                    460:                }
1.80      nicm      461:        }
1.1       nicm      462:
1.66      nicm      463:        SLIST_FOREACH(job, &all_jobs, lentry) {
                    464:                if (pid == job->pid) {
1.67      nicm      465:                        job_died(job, status);  /* might free job */
                    466:                        break;
1.1       nicm      467:                }
1.53      nicm      468:        }
                    469: }
                    470:
1.66      nicm      471: /* Handle stopped children. */
1.31      nicm      472: void
1.66      nicm      473: server_child_stopped(pid_t pid, int status)
1.31      nicm      474: {
1.66      nicm      475:        struct window           *w;
                    476:        struct window_pane      *wp;
                    477:        u_int                    i;
1.31      nicm      478:
1.66      nicm      479:        if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
                    480:                return;
1.31      nicm      481:
1.66      nicm      482:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    483:                if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1.31      nicm      484:                        continue;
1.66      nicm      485:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    486:                        if (wp->pid == pid) {
                    487:                                if (killpg(pid, SIGCONT) != 0)
                    488:                                        kill(pid, SIGCONT);
                    489:                        }
                    490:                }
1.31      nicm      491:        }
1.1       nicm      492: }
                    493:
1.66      nicm      494: /* Handle once-per-second timer events. */
1.78      nicm      495: /* ARGSUSED */
1.1       nicm      496: void
1.66      nicm      497: server_second_callback(unused int fd, unused short events, unused void *arg)
1.1       nicm      498: {
1.60      nicm      499:        struct window           *w;
                    500:        struct window_pane      *wp;
1.66      nicm      501:        struct timeval           tv;
1.35      nicm      502:        u_int                    i;
1.1       nicm      503:
1.60      nicm      504:        if (options_get_number(&global_s_options, "lock-server"))
                    505:                server_lock_server();
                    506:        else
                    507:                server_lock_sessions();
1.1       nicm      508:
1.60      nicm      509:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    510:                w = ARRAY_ITEM(&windows, i);
                    511:                if (w == NULL)
1.1       nicm      512:                        continue;
                    513:
1.60      nicm      514:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    515:                        if (wp->mode != NULL && wp->mode->timer != NULL)
                    516:                                wp->mode->timer(wp);
1.1       nicm      517:                }
                    518:        }
1.72      nicm      519:
                    520:        server_client_status_timer();
1.66      nicm      521:
                    522:        evtimer_del(&server_ev_second);
                    523:        memset(&tv, 0, sizeof tv);
                    524:        tv.tv_sec = 1;
                    525:        evtimer_add(&server_ev_second, &tv);
1.1       nicm      526: }
                    527:
1.46      nicm      528: /* Lock the server if ALL sessions have hit the time limit. */
                    529: void
                    530: server_lock_server(void)
                    531: {
                    532:        struct session  *s;
                    533:        u_int            i;
                    534:        int              timeout;
                    535:        time_t           t;
                    536:
                    537:        t = time(NULL);
                    538:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    539:                if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
                    540:                        continue;
                    541:
1.59      nicm      542:                if (s->flags & SESSION_UNATTACHED) {
1.65      nicm      543:                        if (gettimeofday(&s->activity_time, NULL) != 0)
                    544:                                fatal("gettimeofday failed");
1.59      nicm      545:                        continue;
                    546:                }
                    547:
1.46      nicm      548:                timeout = options_get_number(&s->options, "lock-after-time");
1.65      nicm      549:                if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout)
1.46      nicm      550:                        return; /* not timed out */
                    551:        }
                    552:
                    553:        server_lock();
                    554:        recalculate_sizes();
                    555: }
                    556:
                    557: /* Lock any sessions which have timed out. */
                    558: void
                    559: server_lock_sessions(void)
                    560: {
1.80      nicm      561:        struct session  *s;
1.62      deraadt   562:        u_int            i;
1.46      nicm      563:        int              timeout;
1.62      deraadt   564:        time_t           t;
1.46      nicm      565:
1.62      deraadt   566:        t = time(NULL);
                    567:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.46      nicm      568:                if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
                    569:                        continue;
1.59      nicm      570:
                    571:                if (s->flags & SESSION_UNATTACHED) {
1.65      nicm      572:                        if (gettimeofday(&s->activity_time, NULL) != 0)
                    573:                                fatal("gettimeofday failed");
1.59      nicm      574:                        continue;
                    575:                }
1.46      nicm      576:
                    577:                timeout = options_get_number(&s->options, "lock-after-time");
1.65      nicm      578:                if (timeout > 0 && t > s->activity_time.tv_sec + timeout) {
1.46      nicm      579:                        server_lock_session(s);
                    580:                        recalculate_sizes();
1.1       nicm      581:                }
                    582:        }
                    583: }