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

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