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

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