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

1.125   ! nicm        1: /* $OpenBSD: server.c,v 1.124 2015/05/31 23:27:06 deraadt 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 <termios.h>
                     35: #include <time.h>
                     36: #include <unistd.h>
                     37:
                     38: #include "tmux.h"
                     39:
                     40: /*
                     41:  * Main server functions.
                     42:  */
                     43:
                     44: /* Client list. */
                     45: struct clients  clients;
1.31      nicm       46: struct clients  dead_clients;
1.1       nicm       47:
1.66      nicm       48: int             server_fd;
                     49: int             server_shutdown;
                     50: struct event    server_ev_accept;
                     51: struct event    server_ev_second;
1.45      nicm       52:
1.1       nicm       53: int             server_create_socket(void);
1.66      nicm       54: void            server_loop(void);
1.42      nicm       55: int             server_should_shutdown(void);
1.66      nicm       56: void            server_send_shutdown(void);
                     57: void            server_clean_dead(void);
                     58: void            server_accept_callback(int, short, void *);
                     59: void            server_signal_callback(int, short, void *);
1.1       nicm       60: void            server_child_signal(void);
1.66      nicm       61: void            server_child_exited(pid_t, int);
                     62: void            server_child_stopped(pid_t, int);
                     63: void            server_second_callback(int, short, void *);
1.46      nicm       64: void            server_lock_server(void);
                     65: void            server_lock_sessions(void);
1.45      nicm       66:
1.60      nicm       67: /* Create server socket. */
                     68: int
                     69: server_create_socket(void)
1.1       nicm       70: {
1.60      nicm       71:        struct sockaddr_un      sa;
                     72:        size_t                  size;
                     73:        mode_t                  mask;
1.100     nicm       74:        int                     fd;
1.60      nicm       75:
                     76:        memset(&sa, 0, sizeof sa);
                     77:        sa.sun_family = AF_UNIX;
                     78:        size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
                     79:        if (size >= sizeof sa.sun_path) {
                     80:                errno = ENAMETOOLONG;
1.119     nicm       81:                return (-1);
1.60      nicm       82:        }
                     83:        unlink(sa.sun_path);
                     84:
                     85:        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.119     nicm       86:                return (-1);
1.60      nicm       87:
1.89      nicm       88:        mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
1.60      nicm       89:        if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
1.119     nicm       90:                return (-1);
1.60      nicm       91:        umask(mask);
                     92:
                     93:        if (listen(fd, 16) == -1)
1.119     nicm       94:                return (-1);
1.100     nicm       95:        setblocking(fd, 0);
1.1       nicm       96:
1.60      nicm       97:        return (fd);
                     98: }
                     99:
1.1       nicm      100: /* Fork new server. */
                    101: int
1.103     nicm      102: server_start(int lockfd, char *lockfile)
1.1       nicm      103: {
1.109     nicm      104:        int              pair[2];
                    105:        struct timeval   tv;
                    106:        char            *cause;
1.1       nicm      107:
                    108:        /* The first client is special and gets a socketpair; create it. */
                    109:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
                    110:                fatal("socketpair failed");
1.115     nicm      111:        log_debug("starting server");
1.1       nicm      112:
                    113:        switch (fork()) {
                    114:        case -1:
                    115:                fatal("fork failed");
                    116:        case 0:
                    117:                break;
                    118:        default:
                    119:                close(pair[1]);
                    120:                return (pair[0]);
                    121:        }
                    122:        close(pair[0]);
                    123:
                    124:        /*
                    125:         * Must daemonise before loading configuration as the PID changes so
                    126:         * $TMUX would be wrong for sessions created in the config file.
                    127:         */
1.16      nicm      128:        if (daemon(1, 0) != 0)
1.1       nicm      129:                fatal("daemon failed");
                    130:
1.88      nicm      131:        /* event_init() was called in our parent, need to reinit. */
1.125   ! nicm      132:        clear_signals(0);
1.88      nicm      133:        if (event_reinit(ev_base) != 0)
                    134:                fatal("event_reinit failed");
                    135:
1.16      nicm      136:        logfile("server");
                    137:        log_debug("server started, pid %ld", (long) getpid());
                    138:
1.121     nicm      139:        RB_INIT(&windows);
1.102     nicm      140:        RB_INIT(&all_window_panes);
1.122     nicm      141:        TAILQ_INIT(&clients);
                    142:        TAILQ_INIT(&dead_clients);
1.97      nicm      143:        RB_INIT(&sessions);
                    144:        RB_INIT(&dead_sessions);
1.47      nicm      145:        TAILQ_INIT(&session_groups);
1.15      nicm      146:        mode_key_init_trees();
1.1       nicm      147:        key_bindings_init();
                    148:        utf8_build();
                    149:
                    150:        start_time = time(NULL);
1.16      nicm      151:        log_debug("socket path %s", socket_path);
1.96      nicm      152:        setproctitle("server (%s)", socket_path);
1.16      nicm      153:
1.66      nicm      154:        server_fd = server_create_socket();
1.119     nicm      155:        if (server_fd == -1)
                    156:                fatal("couldn't create socket");
                    157:        server_update_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.122     nicm      168:        cfg_client = TAILQ_FIRST(&clients);
1.111     nicm      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;
1.1       nicm      214:
1.94      nicm      215:        if (!options_get_number(&global_options, "exit-unattached")) {
1.97      nicm      216:                if (!RB_EMPTY(&sessions))
                    217:                        return (0);
1.1       nicm      218:        }
1.116     nicm      219:
1.122     nicm      220:        TAILQ_FOREACH(c, &clients, entry) {
                    221:                if (c->session != NULL)
1.116     nicm      222:                        return (0);
                    223:        }
                    224:
                    225:        /*
                    226:         * No attached clients therefore want to exit - flush any waiting
                    227:         * clients but don't actually exit until they've gone.
                    228:         */
                    229:        cmd_wait_for_flush();
1.122     nicm      230:        if (!TAILQ_EMPTY(&clients))
                    231:                return (0);
1.116     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.122     nicm      240:        struct client   *c, *c1;
                    241:        struct session  *s, *s1;
1.116     nicm      242:
                    243:        cmd_wait_for_flush();
1.1       nicm      244:
1.122     nicm      245:        TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
                    246:                if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
                    247:                        server_client_lost(c);
                    248:                else
                    249:                        server_write_client(c, MSG_SHUTDOWN, NULL, 0);
                    250:                c->session = NULL;
1.42      nicm      251:        }
                    252:
1.122     nicm      253:        RB_FOREACH_SAFE(s, sessions, &sessions, s1)
1.97      nicm      254:                session_destroy(s);
1.42      nicm      255: }
                    256:
1.66      nicm      257: /* Free dead, unreferenced clients and sessions. */
                    258: void
                    259: server_clean_dead(void)
                    260: {
1.122     nicm      261:        struct session  *s, *s1;
                    262:        struct client   *c, *c1;
1.66      nicm      263:
1.122     nicm      264:        RB_FOREACH_SAFE(s, sessions, &dead_sessions, s1) {
                    265:                if (s->references != 0)
                    266:                        continue;
                    267:                RB_REMOVE(sessions, &dead_sessions, s);
                    268:                free(s->name);
                    269:                free(s);
1.66      nicm      270:        }
                    271:
1.122     nicm      272:        TAILQ_FOREACH_SAFE(c, &dead_clients, entry, c1) {
                    273:                if (c->references != 0)
1.66      nicm      274:                        continue;
1.122     nicm      275:                TAILQ_REMOVE(&dead_clients, c, entry);
1.105     nicm      276:                free(c);
1.66      nicm      277:        }
                    278: }
                    279:
                    280: /* Update socket execute permissions based on whether sessions are attached. */
1.75      nicm      281: void
1.66      nicm      282: server_update_socket(void)
1.42      nicm      283: {
1.66      nicm      284:        struct session  *s;
                    285:        static int       last = -1;
1.93      nicm      286:        int              n, mode;
                    287:        struct stat      sb;
1.1       nicm      288:
1.66      nicm      289:        n = 0;
1.97      nicm      290:        RB_FOREACH(s, sessions, &sessions) {
                    291:                if (!(s->flags & SESSION_UNATTACHED)) {
1.66      nicm      292:                        n++;
                    293:                        break;
                    294:                }
                    295:        }
                    296:
                    297:        if (n != last) {
                    298:                last = n;
1.93      nicm      299:
                    300:                if (stat(socket_path, &sb) != 0)
                    301:                        return;
                    302:                mode = sb.st_mode;
                    303:                if (n != 0) {
                    304:                        if (mode & S_IRUSR)
                    305:                                mode |= S_IXUSR;
                    306:                        if (mode & S_IRGRP)
                    307:                                mode |= S_IXGRP;
                    308:                        if (mode & S_IROTH)
                    309:                                mode |= S_IXOTH;
                    310:                } else
                    311:                        mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
                    312:                chmod(socket_path, mode);
1.66      nicm      313:        }
                    314: }
                    315:
                    316: /* Callback for server socket. */
                    317: void
                    318: server_accept_callback(int fd, short events, unused void *data)
                    319: {
                    320:        struct sockaddr_storage sa;
                    321:        socklen_t               slen = sizeof sa;
                    322:        int                     newfd;
                    323:
1.104     nicm      324:        server_add_accept(0);
1.66      nicm      325:        if (!(events & EV_READ))
                    326:                return;
                    327:
                    328:        newfd = accept(fd, (struct sockaddr *) &sa, &slen);
                    329:        if (newfd == -1) {
                    330:                if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
                    331:                        return;
1.104     nicm      332:                if (errno == ENFILE || errno == EMFILE) {
                    333:                        /* Delete and don't try again for 1 second. */
                    334:                        server_add_accept(1);
                    335:                        return;
                    336:                }
1.66      nicm      337:                fatal("accept failed");
                    338:        }
                    339:        if (server_shutdown) {
                    340:                close(newfd);
                    341:                return;
1.42      nicm      342:        }
1.66      nicm      343:        server_client_create(newfd);
                    344: }
                    345:
1.104     nicm      346: /*
                    347:  * Add accept event. If timeout is nonzero, add as a timeout instead of a read
                    348:  * event - used to backoff when running out of file descriptors.
                    349:  */
                    350: void
                    351: server_add_accept(int timeout)
                    352: {
                    353:        struct timeval tv = { timeout, 0 };
                    354:
                    355:        if (event_initialized(&server_ev_accept))
                    356:                event_del(&server_ev_accept);
                    357:
                    358:        if (timeout == 0) {
                    359:                event_set(&server_ev_accept,
                    360:                    server_fd, EV_READ, server_accept_callback, NULL);
                    361:                event_add(&server_ev_accept, NULL);
                    362:        } else {
                    363:                event_set(&server_ev_accept,
                    364:                    server_fd, EV_TIMEOUT, server_accept_callback, NULL);
                    365:                event_add(&server_ev_accept, &tv);
                    366:        }
                    367: }
                    368:
1.66      nicm      369: /* Signal handler. */
                    370: void
                    371: server_signal_callback(int sig, unused short events, unused void *data)
                    372: {
1.119     nicm      373:        int     fd;
1.120     nicm      374:
1.66      nicm      375:        switch (sig) {
                    376:        case SIGTERM:
                    377:                server_shutdown = 1;
                    378:                server_send_shutdown();
                    379:                break;
                    380:        case SIGCHLD:
                    381:                server_child_signal();
                    382:                break;
                    383:        case SIGUSR1:
                    384:                event_del(&server_ev_accept);
1.119     nicm      385:                fd = server_create_socket();
                    386:                if (fd != -1) {
                    387:                        close(server_fd);
                    388:                        server_fd = fd;
                    389:                        server_update_socket();
                    390:                }
1.104     nicm      391:                server_add_accept(0);
1.66      nicm      392:                break;
1.1       nicm      393:        }
                    394: }
                    395:
                    396: /* Handle SIGCHLD. */
                    397: void
                    398: server_child_signal(void)
                    399: {
1.66      nicm      400:        int      status;
                    401:        pid_t    pid;
1.1       nicm      402:
                    403:        for (;;) {
                    404:                switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
                    405:                case -1:
                    406:                        if (errno == ECHILD)
                    407:                                return;
1.39      nicm      408:                        fatal("waitpid failed");
1.1       nicm      409:                case 0:
                    410:                        return;
                    411:                }
1.66      nicm      412:                if (WIFSTOPPED(status))
                    413:                        server_child_stopped(pid, status);
1.79      nicm      414:                else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66      nicm      415:                        server_child_exited(pid, status);
                    416:        }
                    417: }
                    418:
                    419: /* Handle exited children. */
                    420: void
                    421: server_child_exited(pid_t pid, int status)
                    422: {
1.121     nicm      423:        struct window           *w, *w1;
1.66      nicm      424:        struct window_pane      *wp;
                    425:        struct job              *job;
                    426:
1.121     nicm      427:        RB_FOREACH_SAFE(w, windows, &windows, w1) {
1.66      nicm      428:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    429:                        if (wp->pid == pid) {
1.118     nicm      430:                                wp->status = status;
1.77      nicm      431:                                server_destroy_pane(wp);
                    432:                                break;
1.53      nicm      433:                        }
                    434:                }
1.80      nicm      435:        }
1.1       nicm      436:
1.101     nicm      437:        LIST_FOREACH(job, &all_jobs, lentry) {
1.66      nicm      438:                if (pid == job->pid) {
1.67      nicm      439:                        job_died(job, status);  /* might free job */
                    440:                        break;
1.1       nicm      441:                }
1.53      nicm      442:        }
                    443: }
                    444:
1.66      nicm      445: /* Handle stopped children. */
1.31      nicm      446: void
1.66      nicm      447: server_child_stopped(pid_t pid, int status)
1.31      nicm      448: {
1.66      nicm      449:        struct window           *w;
                    450:        struct window_pane      *wp;
1.31      nicm      451:
1.66      nicm      452:        if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
                    453:                return;
1.31      nicm      454:
1.121     nicm      455:        RB_FOREACH(w, windows, &windows) {
1.66      nicm      456:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    457:                        if (wp->pid == pid) {
                    458:                                if (killpg(pid, SIGCONT) != 0)
                    459:                                        kill(pid, SIGCONT);
                    460:                        }
                    461:                }
1.31      nicm      462:        }
1.1       nicm      463: }
                    464:
1.66      nicm      465: /* Handle once-per-second timer events. */
1.1       nicm      466: void
1.66      nicm      467: server_second_callback(unused int fd, unused short events, unused void *arg)
1.1       nicm      468: {
1.60      nicm      469:        struct window           *w;
                    470:        struct window_pane      *wp;
1.66      nicm      471:        struct timeval           tv;
1.1       nicm      472:
1.60      nicm      473:        if (options_get_number(&global_s_options, "lock-server"))
                    474:                server_lock_server();
                    475:        else
                    476:                server_lock_sessions();
1.1       nicm      477:
1.121     nicm      478:        RB_FOREACH(w, windows, &windows) {
1.60      nicm      479:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    480:                        if (wp->mode != NULL && wp->mode->timer != NULL)
                    481:                                wp->mode->timer(wp);
1.1       nicm      482:                }
                    483:        }
1.72      nicm      484:
                    485:        server_client_status_timer();
1.123     nicm      486:
                    487:        format_clean();
1.66      nicm      488:
                    489:        evtimer_del(&server_ev_second);
                    490:        memset(&tv, 0, sizeof tv);
                    491:        tv.tv_sec = 1;
                    492:        evtimer_add(&server_ev_second, &tv);
1.1       nicm      493: }
                    494:
1.46      nicm      495: /* Lock the server if ALL sessions have hit the time limit. */
                    496: void
                    497: server_lock_server(void)
                    498: {
                    499:        struct session  *s;
                    500:        int              timeout;
                    501:        time_t           t;
                    502:
                    503:        t = time(NULL);
1.97      nicm      504:        RB_FOREACH(s, sessions, &sessions) {
1.99      nicm      505:                if (s->flags & SESSION_UNATTACHED)
1.59      nicm      506:                        continue;
1.46      nicm      507:                timeout = options_get_number(&s->options, "lock-after-time");
1.65      nicm      508:                if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout)
1.46      nicm      509:                        return; /* not timed out */
                    510:        }
                    511:
                    512:        server_lock();
                    513:        recalculate_sizes();
                    514: }
                    515:
                    516: /* Lock any sessions which have timed out. */
                    517: void
                    518: server_lock_sessions(void)
                    519: {
1.80      nicm      520:        struct session  *s;
1.46      nicm      521:        int              timeout;
1.62      deraadt   522:        time_t           t;
1.46      nicm      523:
1.62      deraadt   524:        t = time(NULL);
1.97      nicm      525:        RB_FOREACH(s, sessions, &sessions) {
1.99      nicm      526:                if (s->flags & SESSION_UNATTACHED)
1.59      nicm      527:                        continue;
1.46      nicm      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: }