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

1.103   ! nicm        1: /* $OpenBSD: server.c,v 1.102 2011/03/27 20:27:26 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.103   ! nicm      107: server_start(int lockfd, char *lockfile)
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.103   ! nicm      163:
        !           164:        unlink(lockfile);
        !           165:        xfree(lockfile);
        !           166:        close(lockfd);
1.16      nicm      167:
1.82      nicm      168:        if (access(SYSTEM_CFG, R_OK) == 0)
1.83      nicm      169:                load_cfg(SYSTEM_CFG, NULL, &cfg_causes);
1.82      nicm      170:        else if (errno != ENOENT) {
1.83      nicm      171:                cfg_add_cause(
                    172:                    &cfg_causes, "%s: %s", strerror(errno), SYSTEM_CFG);
1.64      nicm      173:        }
1.82      nicm      174:        if (cfg_file != NULL)
1.83      nicm      175:                load_cfg(cfg_file, NULL, &cfg_causes);
1.82      nicm      176:
                    177:        /*
                    178:         * If there is a session already, put the current window and pane into
                    179:         * more mode.
                    180:         */
1.97      nicm      181:        if (!RB_EMPTY(&sessions) && !ARRAY_EMPTY(&cfg_causes)) {
                    182:                wp = RB_MIN(sessions, &sessions)->curw->window->active;
1.85      nicm      183:                window_pane_set_mode(wp, &window_copy_mode);
                    184:                window_copy_init_for_output(wp);
1.83      nicm      185:                for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
                    186:                        cause = ARRAY_ITEM(&cfg_causes, i);
1.85      nicm      187:                        window_copy_add(wp, "%s", cause);
1.83      nicm      188:                        xfree(cause);
1.82      nicm      189:                }
1.83      nicm      190:                ARRAY_FREE(&cfg_causes);
1.82      nicm      191:        }
                    192:        cfg_finished = 1;
1.66      nicm      193:
                    194:        event_set(&server_ev_accept,
                    195:            server_fd, EV_READ|EV_PERSIST, server_accept_callback, NULL);
                    196:        event_add(&server_ev_accept, NULL);
                    197:
                    198:        memset(&tv, 0, sizeof tv);
                    199:        tv.tv_sec = 1;
                    200:        evtimer_set(&server_ev_second, server_second_callback, NULL);
                    201:        evtimer_add(&server_ev_second, &tv);
                    202:
1.88      nicm      203:        set_signals(server_signal_callback);
1.66      nicm      204:        server_loop();
                    205:        exit(0);
1.1       nicm      206: }
                    207:
                    208: /* Main server loop. */
1.66      nicm      209: void
                    210: server_loop(void)
1.1       nicm      211: {
1.66      nicm      212:        while (!server_should_shutdown()) {
                    213:                event_loop(EVLOOP_ONCE);
1.1       nicm      214:
1.60      nicm      215:                server_window_loop();
                    216:                server_client_loop();
1.1       nicm      217:
1.8       nicm      218:                key_bindings_clean();
1.66      nicm      219:                server_clean_dead();
1.80      nicm      220:        }
1.66      nicm      221: }
1.31      nicm      222:
1.94      nicm      223: /* Check if the server should be shutting down (no more clients or sessions). */
1.66      nicm      224: int
                    225: server_should_shutdown(void)
                    226: {
                    227:        u_int   i;
1.1       nicm      228:
1.94      nicm      229:        if (!options_get_number(&global_options, "exit-unattached")) {
1.97      nicm      230:                if (!RB_EMPTY(&sessions))
                    231:                        return (0);
1.1       nicm      232:        }
                    233:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    234:                if (ARRAY_ITEM(&clients, i) != NULL)
1.66      nicm      235:                        return (0);
1.1       nicm      236:        }
1.66      nicm      237:        return (1);
1.1       nicm      238: }
                    239:
1.66      nicm      240: /* Shutdown the server by killing all clients and windows. */
1.1       nicm      241: void
1.66      nicm      242: server_send_shutdown(void)
1.1       nicm      243: {
1.66      nicm      244:        struct client   *c;
1.97      nicm      245:        struct session  *s, *next_s;
1.66      nicm      246:        u_int            i;
1.1       nicm      247:
1.42      nicm      248:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    249:                c = ARRAY_ITEM(&clients, i);
                    250:                if (c != NULL) {
                    251:                        if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
1.60      nicm      252:                                server_client_lost(c);
1.42      nicm      253:                        else
                    254:                                server_write_client(c, MSG_SHUTDOWN, NULL, 0);
1.66      nicm      255:                        c->session = NULL;
1.42      nicm      256:                }
                    257:        }
                    258:
1.97      nicm      259:        s = RB_MIN(sessions, &sessions);
                    260:        while (s != NULL) {
                    261:                next_s = RB_NEXT(sessions, &sessions, s);
                    262:                session_destroy(s);
                    263:                s = next_s;
1.1       nicm      264:        }
1.42      nicm      265: }
                    266:
1.66      nicm      267: /* Free dead, unreferenced clients and sessions. */
                    268: void
                    269: server_clean_dead(void)
                    270: {
1.97      nicm      271:        struct session  *s, *next_s;
1.66      nicm      272:        struct client   *c;
                    273:        u_int            i;
                    274:
1.97      nicm      275:        s = RB_MIN(sessions, &dead_sessions);
                    276:        while (s != NULL) {
                    277:                next_s = RB_NEXT(sessions, &dead_sessions, s);
                    278:                if (s->references == 0) {
                    279:                        RB_REMOVE(sessions, &dead_sessions, s);
                    280:                        xfree(s->name);
                    281:                        xfree(s);
                    282:                }
                    283:                s = next_s;
1.66      nicm      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:        static int       last = -1;
1.93      nicm      301:        int              n, mode;
                    302:        struct stat      sb;
1.1       nicm      303:
1.66      nicm      304:        n = 0;
1.97      nicm      305:        RB_FOREACH(s, sessions, &sessions) {
                    306:                if (!(s->flags & SESSION_UNATTACHED)) {
1.66      nicm      307:                        n++;
                    308:                        break;
                    309:                }
                    310:        }
                    311:
                    312:        if (n != last) {
                    313:                last = n;
1.93      nicm      314:
                    315:                if (stat(socket_path, &sb) != 0)
                    316:                        return;
                    317:                mode = sb.st_mode;
                    318:                if (n != 0) {
                    319:                        if (mode & S_IRUSR)
                    320:                                mode |= S_IXUSR;
                    321:                        if (mode & S_IRGRP)
                    322:                                mode |= S_IXGRP;
                    323:                        if (mode & S_IROTH)
                    324:                                mode |= S_IXOTH;
                    325:                } else
                    326:                        mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
                    327:                chmod(socket_path, mode);
1.66      nicm      328:        }
                    329: }
                    330:
                    331: /* Callback for server socket. */
1.78      nicm      332: /* ARGSUSED */
1.66      nicm      333: void
                    334: server_accept_callback(int fd, short events, unused void *data)
                    335: {
                    336:        struct sockaddr_storage sa;
                    337:        socklen_t               slen = sizeof sa;
                    338:        int                     newfd;
                    339:
                    340:        if (!(events & EV_READ))
                    341:                return;
                    342:
                    343:        newfd = accept(fd, (struct sockaddr *) &sa, &slen);
                    344:        if (newfd == -1) {
                    345:                if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
                    346:                        return;
                    347:                fatal("accept failed");
                    348:        }
                    349:        if (server_shutdown) {
                    350:                close(newfd);
                    351:                return;
1.42      nicm      352:        }
1.66      nicm      353:        server_client_create(newfd);
                    354: }
                    355:
                    356: /* Signal handler. */
1.78      nicm      357: /* ARGSUSED */
1.66      nicm      358: void
                    359: server_signal_callback(int sig, unused short events, unused void *data)
                    360: {
                    361:        switch (sig) {
                    362:        case SIGTERM:
                    363:                server_shutdown = 1;
                    364:                server_send_shutdown();
                    365:                break;
                    366:        case SIGCHLD:
                    367:                server_child_signal();
                    368:                break;
                    369:        case SIGUSR1:
                    370:                event_del(&server_ev_accept);
                    371:                close(server_fd);
                    372:                server_fd = server_create_socket();
                    373:                event_set(&server_ev_accept, server_fd,
                    374:                    EV_READ|EV_PERSIST, server_accept_callback, NULL);
                    375:                event_add(&server_ev_accept, NULL);
                    376:                break;
1.1       nicm      377:        }
                    378: }
                    379:
                    380: /* Handle SIGCHLD. */
                    381: void
                    382: server_child_signal(void)
                    383: {
1.66      nicm      384:        int      status;
                    385:        pid_t    pid;
1.1       nicm      386:
                    387:        for (;;) {
                    388:                switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
                    389:                case -1:
                    390:                        if (errno == ECHILD)
                    391:                                return;
1.39      nicm      392:                        fatal("waitpid failed");
1.1       nicm      393:                case 0:
                    394:                        return;
                    395:                }
1.66      nicm      396:                if (WIFSTOPPED(status))
                    397:                        server_child_stopped(pid, status);
1.79      nicm      398:                else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66      nicm      399:                        server_child_exited(pid, status);
                    400:        }
                    401: }
                    402:
                    403: /* Handle exited children. */
                    404: void
                    405: server_child_exited(pid_t pid, int status)
                    406: {
                    407:        struct window           *w;
                    408:        struct window_pane      *wp;
                    409:        struct job              *job;
                    410:        u_int                    i;
                    411:
                    412:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    413:                if ((w = ARRAY_ITEM(&windows, i)) == NULL)
                    414:                        continue;
                    415:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    416:                        if (wp->pid == pid) {
1.77      nicm      417:                                server_destroy_pane(wp);
                    418:                                break;
1.53      nicm      419:                        }
                    420:                }
1.80      nicm      421:        }
1.1       nicm      422:
1.101     nicm      423:        LIST_FOREACH(job, &all_jobs, lentry) {
1.66      nicm      424:                if (pid == job->pid) {
1.67      nicm      425:                        job_died(job, status);  /* might free job */
                    426:                        break;
1.1       nicm      427:                }
1.53      nicm      428:        }
                    429: }
                    430:
1.66      nicm      431: /* Handle stopped children. */
1.31      nicm      432: void
1.66      nicm      433: server_child_stopped(pid_t pid, int status)
1.31      nicm      434: {
1.66      nicm      435:        struct window           *w;
                    436:        struct window_pane      *wp;
                    437:        u_int                    i;
1.31      nicm      438:
1.66      nicm      439:        if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
                    440:                return;
1.31      nicm      441:
1.66      nicm      442:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    443:                if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1.31      nicm      444:                        continue;
1.66      nicm      445:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    446:                        if (wp->pid == pid) {
                    447:                                if (killpg(pid, SIGCONT) != 0)
                    448:                                        kill(pid, SIGCONT);
                    449:                        }
                    450:                }
1.31      nicm      451:        }
1.1       nicm      452: }
                    453:
1.66      nicm      454: /* Handle once-per-second timer events. */
1.78      nicm      455: /* ARGSUSED */
1.1       nicm      456: void
1.66      nicm      457: server_second_callback(unused int fd, unused short events, unused void *arg)
1.1       nicm      458: {
1.60      nicm      459:        struct window           *w;
                    460:        struct window_pane      *wp;
1.66      nicm      461:        struct timeval           tv;
1.35      nicm      462:        u_int                    i;
1.1       nicm      463:
1.60      nicm      464:        if (options_get_number(&global_s_options, "lock-server"))
                    465:                server_lock_server();
                    466:        else
                    467:                server_lock_sessions();
1.1       nicm      468:
1.60      nicm      469:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    470:                w = ARRAY_ITEM(&windows, i);
                    471:                if (w == NULL)
1.1       nicm      472:                        continue;
                    473:
1.60      nicm      474:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    475:                        if (wp->mode != NULL && wp->mode->timer != NULL)
                    476:                                wp->mode->timer(wp);
1.1       nicm      477:                }
                    478:        }
1.72      nicm      479:
                    480:        server_client_status_timer();
1.66      nicm      481:
                    482:        evtimer_del(&server_ev_second);
                    483:        memset(&tv, 0, sizeof tv);
                    484:        tv.tv_sec = 1;
                    485:        evtimer_add(&server_ev_second, &tv);
1.1       nicm      486: }
                    487:
1.46      nicm      488: /* Lock the server if ALL sessions have hit the time limit. */
                    489: void
                    490: server_lock_server(void)
                    491: {
                    492:        struct session  *s;
                    493:        int              timeout;
                    494:        time_t           t;
                    495:
                    496:        t = time(NULL);
1.97      nicm      497:        RB_FOREACH(s, sessions, &sessions) {
1.99      nicm      498:                if (s->flags & SESSION_UNATTACHED)
1.59      nicm      499:                        continue;
1.46      nicm      500:                timeout = options_get_number(&s->options, "lock-after-time");
1.65      nicm      501:                if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout)
1.46      nicm      502:                        return; /* not timed out */
                    503:        }
                    504:
                    505:        server_lock();
                    506:        recalculate_sizes();
                    507: }
                    508:
                    509: /* Lock any sessions which have timed out. */
                    510: void
                    511: server_lock_sessions(void)
                    512: {
1.80      nicm      513:        struct session  *s;
1.46      nicm      514:        int              timeout;
1.62      deraadt   515:        time_t           t;
1.46      nicm      516:
1.62      deraadt   517:        t = time(NULL);
1.97      nicm      518:        RB_FOREACH(s, sessions, &sessions) {
1.99      nicm      519:                if (s->flags & SESSION_UNATTACHED)
1.59      nicm      520:                        continue;
1.46      nicm      521:                timeout = options_get_number(&s->options, "lock-after-time");
1.65      nicm      522:                if (timeout > 0 && t > s->activity_time.tv_sec + timeout) {
1.46      nicm      523:                        server_lock_session(s);
                    524:                        recalculate_sizes();
1.1       nicm      525:                }
                    526:        }
                    527: }