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

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