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

1.117   ! nicm        1: /* $OpenBSD: server.c,v 1.116 2014/09/01 21:58:41 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;
1.100     nicm       75:        int                     fd;
1.60      nicm       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.100     nicm       96:        setblocking(fd, 0);
1.1       nicm       97:
1.75      nicm       98:        server_update_socket();
                     99:
1.60      nicm      100:        return (fd);
                    101: }
                    102:
1.1       nicm      103: /* Fork new server. */
                    104: int
1.103     nicm      105: server_start(int lockfd, char *lockfile)
1.1       nicm      106: {
1.109     nicm      107:        int              pair[2];
                    108:        struct timeval   tv;
                    109:        char            *cause;
1.1       nicm      110:
                    111:        /* The first client is special and gets a socketpair; create it. */
                    112:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
                    113:                fatal("socketpair failed");
1.115     nicm      114:        log_debug("starting server");
1.1       nicm      115:
                    116:        switch (fork()) {
                    117:        case -1:
                    118:                fatal("fork failed");
                    119:        case 0:
                    120:                break;
                    121:        default:
                    122:                close(pair[1]);
                    123:                return (pair[0]);
                    124:        }
                    125:        close(pair[0]);
                    126:
                    127:        /*
                    128:         * Must daemonise before loading configuration as the PID changes so
                    129:         * $TMUX would be wrong for sessions created in the config file.
                    130:         */
1.16      nicm      131:        if (daemon(1, 0) != 0)
1.1       nicm      132:                fatal("daemon failed");
                    133:
1.88      nicm      134:        /* event_init() was called in our parent, need to reinit. */
                    135:        if (event_reinit(ev_base) != 0)
                    136:                fatal("event_reinit failed");
1.92      nicm      137:        clear_signals(0);
1.88      nicm      138:
1.16      nicm      139:        logfile("server");
                    140:        log_debug("server started, pid %ld", (long) getpid());
                    141:
1.1       nicm      142:        ARRAY_INIT(&windows);
1.102     nicm      143:        RB_INIT(&all_window_panes);
1.1       nicm      144:        ARRAY_INIT(&clients);
1.31      nicm      145:        ARRAY_INIT(&dead_clients);
1.97      nicm      146:        RB_INIT(&sessions);
                    147:        RB_INIT(&dead_sessions);
1.47      nicm      148:        TAILQ_INIT(&session_groups);
1.15      nicm      149:        mode_key_init_trees();
1.1       nicm      150:        key_bindings_init();
                    151:        utf8_build();
                    152:
                    153:        start_time = time(NULL);
1.16      nicm      154:        log_debug("socket path %s", socket_path);
1.96      nicm      155:        setproctitle("server (%s)", socket_path);
1.16      nicm      156:
1.66      nicm      157:        server_fd = server_create_socket();
1.60      nicm      158:        server_client_create(pair[1]);
1.103     nicm      159:
                    160:        unlink(lockfile);
1.105     nicm      161:        free(lockfile);
1.103     nicm      162:        close(lockfd);
1.16      nicm      163:
1.109     nicm      164:        cfg_cmd_q = cmdq_new(NULL);
                    165:        cfg_cmd_q->emptyfn = cfg_default_done;
                    166:        cfg_finished = 0;
                    167:        cfg_references = 1;
1.111     nicm      168:        cfg_client = ARRAY_FIRST(&clients);
                    169:        if (cfg_client != NULL)
                    170:                cfg_client->references++;
1.109     nicm      171:
1.110     nicm      172:        if (access(TMUX_CONF, R_OK) == 0) {
1.117   ! nicm      173:                if (load_cfg(TMUX_CONF, cfg_cmd_q, &cause) == -1)
        !           174:                        cfg_add_cause("%s: %s", TMUX_CONF, cause);
        !           175:        } else if (errno != ENOENT)
        !           176:                cfg_add_cause("%s: %s", TMUX_CONF, strerror(errno));
1.109     nicm      177:        if (cfg_file != NULL) {
1.117   ! nicm      178:                if (load_cfg(cfg_file, cfg_cmd_q, &cause) == -1)
        !           179:                        cfg_add_cause("%s: %s", cfg_file, cause);
1.109     nicm      180:        }
                    181:        cmdq_continue(cfg_cmd_q);
1.66      nicm      182:
1.104     nicm      183:        server_add_accept(0);
1.66      nicm      184:
                    185:        memset(&tv, 0, sizeof tv);
                    186:        tv.tv_sec = 1;
                    187:        evtimer_set(&server_ev_second, server_second_callback, NULL);
                    188:        evtimer_add(&server_ev_second, &tv);
                    189:
1.88      nicm      190:        set_signals(server_signal_callback);
1.66      nicm      191:        server_loop();
                    192:        exit(0);
1.1       nicm      193: }
                    194:
                    195: /* Main server loop. */
1.66      nicm      196: void
                    197: server_loop(void)
1.1       nicm      198: {
1.66      nicm      199:        while (!server_should_shutdown()) {
                    200:                event_loop(EVLOOP_ONCE);
1.1       nicm      201:
1.60      nicm      202:                server_window_loop();
                    203:                server_client_loop();
1.1       nicm      204:
1.66      nicm      205:                server_clean_dead();
1.80      nicm      206:        }
1.66      nicm      207: }
1.31      nicm      208:
1.112     nicm      209: /* Check if the server should exit (no more clients or sessions). */
1.66      nicm      210: int
                    211: server_should_shutdown(void)
                    212: {
1.116     nicm      213:        struct client   *c;
                    214:        u_int            i;
1.1       nicm      215:
1.94      nicm      216:        if (!options_get_number(&global_options, "exit-unattached")) {
1.97      nicm      217:                if (!RB_EMPTY(&sessions))
                    218:                        return (0);
1.1       nicm      219:        }
1.116     nicm      220:
                    221:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    222:                c = ARRAY_ITEM(&clients, i);
                    223:                if (c != NULL && c->session != NULL)
                    224:                        return (0);
                    225:        }
                    226:
                    227:        /*
                    228:         * No attached clients therefore want to exit - flush any waiting
                    229:         * clients but don't actually exit until they've gone.
                    230:         */
                    231:        cmd_wait_for_flush();
1.1       nicm      232:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    233:                if (ARRAY_ITEM(&clients, i) != NULL)
1.66      nicm      234:                        return (0);
1.1       nicm      235:        }
1.116     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.116     nicm      247:
                    248:        cmd_wait_for_flush();
1.1       nicm      249:
1.42      nicm      250:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    251:                c = ARRAY_ITEM(&clients, i);
                    252:                if (c != NULL) {
                    253:                        if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
1.60      nicm      254:                                server_client_lost(c);
1.42      nicm      255:                        else
                    256:                                server_write_client(c, MSG_SHUTDOWN, NULL, 0);
1.66      nicm      257:                        c->session = NULL;
1.42      nicm      258:                }
                    259:        }
                    260:
1.97      nicm      261:        s = RB_MIN(sessions, &sessions);
                    262:        while (s != NULL) {
                    263:                next_s = RB_NEXT(sessions, &sessions, s);
                    264:                session_destroy(s);
                    265:                s = next_s;
1.1       nicm      266:        }
1.42      nicm      267: }
                    268:
1.66      nicm      269: /* Free dead, unreferenced clients and sessions. */
                    270: void
                    271: server_clean_dead(void)
                    272: {
1.97      nicm      273:        struct session  *s, *next_s;
1.66      nicm      274:        struct client   *c;
                    275:        u_int            i;
                    276:
1.97      nicm      277:        s = RB_MIN(sessions, &dead_sessions);
                    278:        while (s != NULL) {
                    279:                next_s = RB_NEXT(sessions, &dead_sessions, s);
                    280:                if (s->references == 0) {
                    281:                        RB_REMOVE(sessions, &dead_sessions, s);
1.105     nicm      282:                        free(s->name);
                    283:                        free(s);
1.97      nicm      284:                }
                    285:                s = next_s;
1.66      nicm      286:        }
                    287:
                    288:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                    289:                c = ARRAY_ITEM(&dead_clients, i);
                    290:                if (c == NULL || c->references != 0)
                    291:                        continue;
                    292:                ARRAY_SET(&dead_clients, i, NULL);
1.105     nicm      293:                free(c);
1.66      nicm      294:        }
                    295: }
                    296:
                    297: /* Update socket execute permissions based on whether sessions are attached. */
1.75      nicm      298: void
1.66      nicm      299: server_update_socket(void)
1.42      nicm      300: {
1.66      nicm      301:        struct session  *s;
                    302:        static int       last = -1;
1.93      nicm      303:        int              n, mode;
                    304:        struct stat      sb;
1.1       nicm      305:
1.66      nicm      306:        n = 0;
1.97      nicm      307:        RB_FOREACH(s, sessions, &sessions) {
                    308:                if (!(s->flags & SESSION_UNATTACHED)) {
1.66      nicm      309:                        n++;
                    310:                        break;
                    311:                }
                    312:        }
                    313:
                    314:        if (n != last) {
                    315:                last = n;
1.93      nicm      316:
                    317:                if (stat(socket_path, &sb) != 0)
                    318:                        return;
                    319:                mode = sb.st_mode;
                    320:                if (n != 0) {
                    321:                        if (mode & S_IRUSR)
                    322:                                mode |= S_IXUSR;
                    323:                        if (mode & S_IRGRP)
                    324:                                mode |= S_IXGRP;
                    325:                        if (mode & S_IROTH)
                    326:                                mode |= S_IXOTH;
                    327:                } else
                    328:                        mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
                    329:                chmod(socket_path, mode);
1.66      nicm      330:        }
                    331: }
                    332:
                    333: /* Callback for server socket. */
                    334: void
                    335: server_accept_callback(int fd, short events, unused void *data)
                    336: {
                    337:        struct sockaddr_storage sa;
                    338:        socklen_t               slen = sizeof sa;
                    339:        int                     newfd;
                    340:
1.104     nicm      341:        server_add_accept(0);
1.66      nicm      342:        if (!(events & EV_READ))
                    343:                return;
                    344:
                    345:        newfd = accept(fd, (struct sockaddr *) &sa, &slen);
                    346:        if (newfd == -1) {
                    347:                if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
                    348:                        return;
1.104     nicm      349:                if (errno == ENFILE || errno == EMFILE) {
                    350:                        /* Delete and don't try again for 1 second. */
                    351:                        server_add_accept(1);
                    352:                        return;
                    353:                }
1.66      nicm      354:                fatal("accept failed");
                    355:        }
                    356:        if (server_shutdown) {
                    357:                close(newfd);
                    358:                return;
1.42      nicm      359:        }
1.66      nicm      360:        server_client_create(newfd);
                    361: }
                    362:
1.104     nicm      363: /*
                    364:  * Add accept event. If timeout is nonzero, add as a timeout instead of a read
                    365:  * event - used to backoff when running out of file descriptors.
                    366:  */
                    367: void
                    368: server_add_accept(int timeout)
                    369: {
                    370:        struct timeval tv = { timeout, 0 };
                    371:
                    372:        if (event_initialized(&server_ev_accept))
                    373:                event_del(&server_ev_accept);
                    374:
                    375:        if (timeout == 0) {
                    376:                event_set(&server_ev_accept,
                    377:                    server_fd, EV_READ, server_accept_callback, NULL);
                    378:                event_add(&server_ev_accept, NULL);
                    379:        } else {
                    380:                event_set(&server_ev_accept,
                    381:                    server_fd, EV_TIMEOUT, server_accept_callback, NULL);
                    382:                event_add(&server_ev_accept, &tv);
                    383:        }
                    384: }
                    385:
1.66      nicm      386: /* Signal handler. */
                    387: void
                    388: server_signal_callback(int sig, unused short events, unused void *data)
                    389: {
                    390:        switch (sig) {
                    391:        case SIGTERM:
                    392:                server_shutdown = 1;
                    393:                server_send_shutdown();
                    394:                break;
                    395:        case SIGCHLD:
                    396:                server_child_signal();
                    397:                break;
                    398:        case SIGUSR1:
                    399:                event_del(&server_ev_accept);
                    400:                close(server_fd);
                    401:                server_fd = server_create_socket();
1.104     nicm      402:                server_add_accept(0);
1.66      nicm      403:                break;
1.1       nicm      404:        }
                    405: }
                    406:
                    407: /* Handle SIGCHLD. */
                    408: void
                    409: server_child_signal(void)
                    410: {
1.66      nicm      411:        int      status;
                    412:        pid_t    pid;
1.1       nicm      413:
                    414:        for (;;) {
                    415:                switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
                    416:                case -1:
                    417:                        if (errno == ECHILD)
                    418:                                return;
1.39      nicm      419:                        fatal("waitpid failed");
1.1       nicm      420:                case 0:
                    421:                        return;
                    422:                }
1.66      nicm      423:                if (WIFSTOPPED(status))
                    424:                        server_child_stopped(pid, status);
1.79      nicm      425:                else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66      nicm      426:                        server_child_exited(pid, status);
                    427:        }
                    428: }
                    429:
                    430: /* Handle exited children. */
                    431: void
                    432: server_child_exited(pid_t pid, int status)
                    433: {
                    434:        struct window           *w;
                    435:        struct window_pane      *wp;
                    436:        struct job              *job;
                    437:        u_int                    i;
                    438:
                    439:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    440:                if ((w = ARRAY_ITEM(&windows, i)) == NULL)
                    441:                        continue;
                    442:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    443:                        if (wp->pid == pid) {
1.77      nicm      444:                                server_destroy_pane(wp);
                    445:                                break;
1.53      nicm      446:                        }
                    447:                }
1.80      nicm      448:        }
1.1       nicm      449:
1.101     nicm      450:        LIST_FOREACH(job, &all_jobs, lentry) {
1.66      nicm      451:                if (pid == job->pid) {
1.67      nicm      452:                        job_died(job, status);  /* might free job */
                    453:                        break;
1.1       nicm      454:                }
1.53      nicm      455:        }
                    456: }
                    457:
1.66      nicm      458: /* Handle stopped children. */
1.31      nicm      459: void
1.66      nicm      460: server_child_stopped(pid_t pid, int status)
1.31      nicm      461: {
1.66      nicm      462:        struct window           *w;
                    463:        struct window_pane      *wp;
                    464:        u_int                    i;
1.31      nicm      465:
1.66      nicm      466:        if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
                    467:                return;
1.31      nicm      468:
1.66      nicm      469:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    470:                if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1.31      nicm      471:                        continue;
1.66      nicm      472:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    473:                        if (wp->pid == pid) {
                    474:                                if (killpg(pid, SIGCONT) != 0)
                    475:                                        kill(pid, SIGCONT);
                    476:                        }
                    477:                }
1.31      nicm      478:        }
1.1       nicm      479: }
                    480:
1.66      nicm      481: /* Handle once-per-second timer events. */
1.1       nicm      482: void
1.66      nicm      483: server_second_callback(unused int fd, unused short events, unused void *arg)
1.1       nicm      484: {
1.60      nicm      485:        struct window           *w;
                    486:        struct window_pane      *wp;
1.66      nicm      487:        struct timeval           tv;
1.35      nicm      488:        u_int                    i;
1.1       nicm      489:
1.60      nicm      490:        if (options_get_number(&global_s_options, "lock-server"))
                    491:                server_lock_server();
                    492:        else
                    493:                server_lock_sessions();
1.1       nicm      494:
1.60      nicm      495:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    496:                w = ARRAY_ITEM(&windows, i);
                    497:                if (w == NULL)
1.1       nicm      498:                        continue;
                    499:
1.60      nicm      500:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    501:                        if (wp->mode != NULL && wp->mode->timer != NULL)
                    502:                                wp->mode->timer(wp);
1.1       nicm      503:                }
                    504:        }
1.72      nicm      505:
                    506:        server_client_status_timer();
1.66      nicm      507:
                    508:        evtimer_del(&server_ev_second);
                    509:        memset(&tv, 0, sizeof tv);
                    510:        tv.tv_sec = 1;
                    511:        evtimer_add(&server_ev_second, &tv);
1.1       nicm      512: }
                    513:
1.46      nicm      514: /* Lock the server if ALL sessions have hit the time limit. */
                    515: void
                    516: server_lock_server(void)
                    517: {
                    518:        struct session  *s;
                    519:        int              timeout;
                    520:        time_t           t;
                    521:
                    522:        t = time(NULL);
1.97      nicm      523:        RB_FOREACH(s, sessions, &sessions) {
1.99      nicm      524:                if (s->flags & SESSION_UNATTACHED)
1.59      nicm      525:                        continue;
1.46      nicm      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:                        return; /* not timed out */
                    529:        }
                    530:
                    531:        server_lock();
                    532:        recalculate_sizes();
                    533: }
                    534:
                    535: /* Lock any sessions which have timed out. */
                    536: void
                    537: server_lock_sessions(void)
                    538: {
1.80      nicm      539:        struct session  *s;
1.46      nicm      540:        int              timeout;
1.62      deraadt   541:        time_t           t;
1.46      nicm      542:
1.62      deraadt   543:        t = time(NULL);
1.97      nicm      544:        RB_FOREACH(s, sessions, &sessions) {
1.99      nicm      545:                if (s->flags & SESSION_UNATTACHED)
1.59      nicm      546:                        continue;
1.46      nicm      547:                timeout = options_get_number(&s->options, "lock-after-time");
1.65      nicm      548:                if (timeout > 0 && t > s->activity_time.tv_sec + timeout) {
1.46      nicm      549:                        server_lock_session(s);
                    550:                        recalculate_sizes();
1.1       nicm      551:                }
                    552:        }
                    553: }