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

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