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

1.137   ! nicm        1: /* $OpenBSD: server.c,v 1.136 2015/08/29 08:30:54 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 <termios.h>
                     35: #include <time.h>
                     36: #include <unistd.h>
                     37:
                     38: #include "tmux.h"
                     39:
                     40: /*
                     41:  * Main server functions.
                     42:  */
                     43:
                     44: struct clients  clients;
                     45:
1.66      nicm       46: int             server_fd;
                     47: int             server_shutdown;
                     48: struct event    server_ev_accept;
1.45      nicm       49:
1.126     nicm       50: struct session         *marked_session;
                     51: struct winlink         *marked_winlink;
                     52: struct window          *marked_window;
                     53: struct window_pane     *marked_window_pane;
                     54: struct layout_cell     *marked_layout_cell;
                     55:
                     56: int    server_create_socket(void);
                     57: void   server_loop(void);
                     58: int    server_should_shutdown(void);
                     59: void   server_send_shutdown(void);
                     60: void   server_accept_callback(int, short, void *);
                     61: void   server_signal_callback(int, short, void *);
                     62: void   server_child_signal(void);
                     63: void   server_child_exited(pid_t, int);
                     64: void   server_child_stopped(pid_t, int);
                     65:
                     66: /* Set marked pane. */
                     67: void
                     68: server_set_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
                     69: {
                     70:        marked_session = s;
                     71:        marked_winlink = wl;
                     72:        marked_window = wl->window;
                     73:        marked_window_pane = wp;
                     74:        marked_layout_cell = wp->layout_cell;
                     75: }
                     76:
                     77: /* Clear marked pane. */
                     78: void
                     79: server_clear_marked(void)
                     80: {
                     81:        marked_session = NULL;
                     82:        marked_winlink = NULL;
                     83:        marked_window = NULL;
                     84:        marked_window_pane = NULL;
                     85:        marked_layout_cell = NULL;
                     86: }
                     87:
                     88: /* Is this the marked pane? */
                     89: int
                     90: server_is_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
                     91: {
                     92:        if (s == NULL || wl == NULL || wp == NULL)
                     93:                return (0);
                     94:        if (marked_session != s || marked_winlink != wl)
                     95:                return (0);
                     96:        if (marked_window_pane != wp)
                     97:                return (0);
                     98:        return (server_check_marked());
                     99: }
                    100:
                    101: /* Check if the marked pane is still valid. */
                    102: int
                    103: server_check_marked(void)
                    104: {
                    105:        struct winlink  *wl;
                    106:
                    107:        if (marked_window_pane == NULL)
                    108:                return (0);
                    109:        if (marked_layout_cell != marked_window_pane->layout_cell)
                    110:                return (0);
                    111:
                    112:        if (!session_alive(marked_session))
                    113:                return (0);
                    114:        RB_FOREACH(wl, winlinks, &marked_session->windows) {
                    115:                if (wl->window == marked_window && wl == marked_winlink)
                    116:                        break;
                    117:        }
                    118:        if (wl == NULL)
                    119:                return (0);
                    120:
                    121:        if (!window_has_pane(marked_window, marked_window_pane))
                    122:                return (0);
                    123:        return (window_pane_visible(marked_window_pane));
                    124: }
1.45      nicm      125:
1.60      nicm      126: /* Create server socket. */
                    127: int
                    128: server_create_socket(void)
1.1       nicm      129: {
1.60      nicm      130:        struct sockaddr_un      sa;
                    131:        size_t                  size;
                    132:        mode_t                  mask;
1.100     nicm      133:        int                     fd;
1.60      nicm      134:
                    135:        memset(&sa, 0, sizeof sa);
                    136:        sa.sun_family = AF_UNIX;
                    137:        size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
                    138:        if (size >= sizeof sa.sun_path) {
                    139:                errno = ENAMETOOLONG;
1.119     nicm      140:                return (-1);
1.60      nicm      141:        }
                    142:        unlink(sa.sun_path);
                    143:
                    144:        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.119     nicm      145:                return (-1);
1.60      nicm      146:
1.89      nicm      147:        mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
1.60      nicm      148:        if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
1.119     nicm      149:                return (-1);
1.60      nicm      150:        umask(mask);
                    151:
                    152:        if (listen(fd, 16) == -1)
1.119     nicm      153:                return (-1);
1.100     nicm      154:        setblocking(fd, 0);
1.1       nicm      155:
1.60      nicm      156:        return (fd);
                    157: }
                    158:
1.1       nicm      159: /* Fork new server. */
                    160: int
1.103     nicm      161: server_start(int lockfd, char *lockfile)
1.1       nicm      162: {
1.134     nicm      163:        int      pair[2];
                    164:        char    *cause;
1.1       nicm      165:
                    166:        /* The first client is special and gets a socketpair; create it. */
                    167:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
                    168:                fatal("socketpair failed");
1.115     nicm      169:        log_debug("starting server");
1.1       nicm      170:
                    171:        switch (fork()) {
                    172:        case -1:
                    173:                fatal("fork failed");
                    174:        case 0:
                    175:                break;
                    176:        default:
                    177:                close(pair[1]);
                    178:                return (pair[0]);
                    179:        }
                    180:        close(pair[0]);
                    181:
                    182:        /*
                    183:         * Must daemonise before loading configuration as the PID changes so
                    184:         * $TMUX would be wrong for sessions created in the config file.
                    185:         */
1.16      nicm      186:        if (daemon(1, 0) != 0)
1.1       nicm      187:                fatal("daemon failed");
                    188:
1.88      nicm      189:        /* event_init() was called in our parent, need to reinit. */
1.125     nicm      190:        clear_signals(0);
1.88      nicm      191:        if (event_reinit(ev_base) != 0)
                    192:                fatal("event_reinit failed");
                    193:
1.16      nicm      194:        logfile("server");
                    195:        log_debug("server started, pid %ld", (long) getpid());
                    196:
1.121     nicm      197:        RB_INIT(&windows);
1.102     nicm      198:        RB_INIT(&all_window_panes);
1.122     nicm      199:        TAILQ_INIT(&clients);
1.97      nicm      200:        RB_INIT(&sessions);
1.47      nicm      201:        TAILQ_INIT(&session_groups);
1.15      nicm      202:        mode_key_init_trees();
1.1       nicm      203:        key_bindings_init();
                    204:        utf8_build();
                    205:
                    206:        start_time = time(NULL);
1.16      nicm      207:        log_debug("socket path %s", socket_path);
1.96      nicm      208:        setproctitle("server (%s)", socket_path);
1.16      nicm      209:
1.66      nicm      210:        server_fd = server_create_socket();
1.119     nicm      211:        if (server_fd == -1)
                    212:                fatal("couldn't create socket");
                    213:        server_update_socket();
1.60      nicm      214:        server_client_create(pair[1]);
1.103     nicm      215:
                    216:        unlink(lockfile);
1.105     nicm      217:        free(lockfile);
1.103     nicm      218:        close(lockfd);
1.16      nicm      219:
1.109     nicm      220:        cfg_cmd_q = cmdq_new(NULL);
                    221:        cfg_cmd_q->emptyfn = cfg_default_done;
                    222:        cfg_finished = 0;
                    223:        cfg_references = 1;
1.122     nicm      224:        cfg_client = TAILQ_FIRST(&clients);
1.111     nicm      225:        if (cfg_client != NULL)
                    226:                cfg_client->references++;
1.109     nicm      227:
1.110     nicm      228:        if (access(TMUX_CONF, R_OK) == 0) {
1.117     nicm      229:                if (load_cfg(TMUX_CONF, cfg_cmd_q, &cause) == -1)
                    230:                        cfg_add_cause("%s: %s", TMUX_CONF, cause);
                    231:        } else if (errno != ENOENT)
                    232:                cfg_add_cause("%s: %s", TMUX_CONF, strerror(errno));
1.109     nicm      233:        if (cfg_file != NULL) {
1.117     nicm      234:                if (load_cfg(cfg_file, cfg_cmd_q, &cause) == -1)
                    235:                        cfg_add_cause("%s: %s", cfg_file, cause);
1.109     nicm      236:        }
                    237:        cmdq_continue(cfg_cmd_q);
1.129     nicm      238:        status_prompt_load_history();
1.66      nicm      239:
1.104     nicm      240:        server_add_accept(0);
1.66      nicm      241:
1.88      nicm      242:        set_signals(server_signal_callback);
1.66      nicm      243:        server_loop();
1.129     nicm      244:        status_prompt_save_history();
1.66      nicm      245:        exit(0);
1.1       nicm      246: }
                    247:
                    248: /* Main server loop. */
1.66      nicm      249: void
                    250: server_loop(void)
1.1       nicm      251: {
1.66      nicm      252:        while (!server_should_shutdown()) {
1.137   ! nicm      253:                log_debug("event dispatch enter");
1.66      nicm      254:                event_loop(EVLOOP_ONCE);
1.137   ! nicm      255:                log_debug("event dispatch exit");
1.1       nicm      256:
1.60      nicm      257:                server_client_loop();
1.80      nicm      258:        }
1.66      nicm      259: }
1.31      nicm      260:
1.112     nicm      261: /* Check if the server should exit (no more clients or sessions). */
1.66      nicm      262: int
                    263: server_should_shutdown(void)
                    264: {
1.116     nicm      265:        struct client   *c;
1.1       nicm      266:
1.94      nicm      267:        if (!options_get_number(&global_options, "exit-unattached")) {
1.97      nicm      268:                if (!RB_EMPTY(&sessions))
                    269:                        return (0);
1.1       nicm      270:        }
1.116     nicm      271:
1.122     nicm      272:        TAILQ_FOREACH(c, &clients, entry) {
                    273:                if (c->session != NULL)
1.116     nicm      274:                        return (0);
                    275:        }
                    276:
                    277:        /*
                    278:         * No attached clients therefore want to exit - flush any waiting
                    279:         * clients but don't actually exit until they've gone.
                    280:         */
                    281:        cmd_wait_for_flush();
1.122     nicm      282:        if (!TAILQ_EMPTY(&clients))
                    283:                return (0);
1.116     nicm      284:
1.66      nicm      285:        return (1);
1.1       nicm      286: }
                    287:
1.66      nicm      288: /* Shutdown the server by killing all clients and windows. */
1.1       nicm      289: void
1.66      nicm      290: server_send_shutdown(void)
1.1       nicm      291: {
1.122     nicm      292:        struct client   *c, *c1;
                    293:        struct session  *s, *s1;
1.116     nicm      294:
                    295:        cmd_wait_for_flush();
1.1       nicm      296:
1.122     nicm      297:        TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
                    298:                if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
                    299:                        server_client_lost(c);
                    300:                else
                    301:                        server_write_client(c, MSG_SHUTDOWN, NULL, 0);
                    302:                c->session = NULL;
1.42      nicm      303:        }
                    304:
1.122     nicm      305:        RB_FOREACH_SAFE(s, sessions, &sessions, s1)
1.97      nicm      306:                session_destroy(s);
1.66      nicm      307: }
                    308:
                    309: /* Update socket execute permissions based on whether sessions are attached. */
1.75      nicm      310: void
1.66      nicm      311: server_update_socket(void)
1.42      nicm      312: {
1.66      nicm      313:        struct session  *s;
                    314:        static int       last = -1;
1.93      nicm      315:        int              n, mode;
                    316:        struct stat      sb;
1.1       nicm      317:
1.66      nicm      318:        n = 0;
1.97      nicm      319:        RB_FOREACH(s, sessions, &sessions) {
                    320:                if (!(s->flags & SESSION_UNATTACHED)) {
1.66      nicm      321:                        n++;
                    322:                        break;
                    323:                }
                    324:        }
                    325:
                    326:        if (n != last) {
                    327:                last = n;
1.93      nicm      328:
                    329:                if (stat(socket_path, &sb) != 0)
                    330:                        return;
                    331:                mode = sb.st_mode;
                    332:                if (n != 0) {
                    333:                        if (mode & S_IRUSR)
                    334:                                mode |= S_IXUSR;
                    335:                        if (mode & S_IRGRP)
                    336:                                mode |= S_IXGRP;
                    337:                        if (mode & S_IROTH)
                    338:                                mode |= S_IXOTH;
                    339:                } else
                    340:                        mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
                    341:                chmod(socket_path, mode);
1.66      nicm      342:        }
                    343: }
                    344:
                    345: /* Callback for server socket. */
                    346: void
                    347: server_accept_callback(int fd, short events, unused void *data)
                    348: {
                    349:        struct sockaddr_storage sa;
                    350:        socklen_t               slen = sizeof sa;
                    351:        int                     newfd;
                    352:
1.104     nicm      353:        server_add_accept(0);
1.66      nicm      354:        if (!(events & EV_READ))
                    355:                return;
                    356:
                    357:        newfd = accept(fd, (struct sockaddr *) &sa, &slen);
                    358:        if (newfd == -1) {
                    359:                if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
                    360:                        return;
1.104     nicm      361:                if (errno == ENFILE || errno == EMFILE) {
                    362:                        /* Delete and don't try again for 1 second. */
                    363:                        server_add_accept(1);
                    364:                        return;
                    365:                }
1.66      nicm      366:                fatal("accept failed");
                    367:        }
                    368:        if (server_shutdown) {
                    369:                close(newfd);
                    370:                return;
1.42      nicm      371:        }
1.66      nicm      372:        server_client_create(newfd);
                    373: }
                    374:
1.104     nicm      375: /*
                    376:  * Add accept event. If timeout is nonzero, add as a timeout instead of a read
                    377:  * event - used to backoff when running out of file descriptors.
                    378:  */
                    379: void
                    380: server_add_accept(int timeout)
                    381: {
                    382:        struct timeval tv = { timeout, 0 };
                    383:
                    384:        if (event_initialized(&server_ev_accept))
                    385:                event_del(&server_ev_accept);
                    386:
                    387:        if (timeout == 0) {
                    388:                event_set(&server_ev_accept,
                    389:                    server_fd, EV_READ, server_accept_callback, NULL);
                    390:                event_add(&server_ev_accept, NULL);
                    391:        } else {
                    392:                event_set(&server_ev_accept,
                    393:                    server_fd, EV_TIMEOUT, server_accept_callback, NULL);
                    394:                event_add(&server_ev_accept, &tv);
                    395:        }
                    396: }
                    397:
1.66      nicm      398: /* Signal handler. */
                    399: void
                    400: server_signal_callback(int sig, unused short events, unused void *data)
                    401: {
1.119     nicm      402:        int     fd;
1.120     nicm      403:
1.66      nicm      404:        switch (sig) {
                    405:        case SIGTERM:
                    406:                server_shutdown = 1;
                    407:                server_send_shutdown();
                    408:                break;
                    409:        case SIGCHLD:
                    410:                server_child_signal();
                    411:                break;
                    412:        case SIGUSR1:
                    413:                event_del(&server_ev_accept);
1.119     nicm      414:                fd = server_create_socket();
                    415:                if (fd != -1) {
                    416:                        close(server_fd);
                    417:                        server_fd = fd;
                    418:                        server_update_socket();
                    419:                }
1.104     nicm      420:                server_add_accept(0);
1.66      nicm      421:                break;
1.1       nicm      422:        }
                    423: }
                    424:
                    425: /* Handle SIGCHLD. */
                    426: void
                    427: server_child_signal(void)
                    428: {
1.66      nicm      429:        int      status;
                    430:        pid_t    pid;
1.1       nicm      431:
                    432:        for (;;) {
                    433:                switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
                    434:                case -1:
                    435:                        if (errno == ECHILD)
                    436:                                return;
1.39      nicm      437:                        fatal("waitpid failed");
1.1       nicm      438:                case 0:
                    439:                        return;
                    440:                }
1.66      nicm      441:                if (WIFSTOPPED(status))
                    442:                        server_child_stopped(pid, status);
1.79      nicm      443:                else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66      nicm      444:                        server_child_exited(pid, status);
                    445:        }
                    446: }
                    447:
                    448: /* Handle exited children. */
                    449: void
                    450: server_child_exited(pid_t pid, int status)
                    451: {
1.121     nicm      452:        struct window           *w, *w1;
1.66      nicm      453:        struct window_pane      *wp;
                    454:        struct job              *job;
                    455:
1.121     nicm      456:        RB_FOREACH_SAFE(w, windows, &windows, w1) {
1.66      nicm      457:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    458:                        if (wp->pid == pid) {
1.118     nicm      459:                                wp->status = status;
1.77      nicm      460:                                server_destroy_pane(wp);
                    461:                                break;
1.53      nicm      462:                        }
                    463:                }
1.80      nicm      464:        }
1.1       nicm      465:
1.101     nicm      466:        LIST_FOREACH(job, &all_jobs, lentry) {
1.66      nicm      467:                if (pid == job->pid) {
1.67      nicm      468:                        job_died(job, status);  /* might free job */
                    469:                        break;
1.1       nicm      470:                }
1.53      nicm      471:        }
                    472: }
                    473:
1.66      nicm      474: /* Handle stopped children. */
1.31      nicm      475: void
1.66      nicm      476: server_child_stopped(pid_t pid, int status)
1.31      nicm      477: {
1.66      nicm      478:        struct window           *w;
                    479:        struct window_pane      *wp;
1.31      nicm      480:
1.66      nicm      481:        if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
                    482:                return;
1.31      nicm      483:
1.121     nicm      484:        RB_FOREACH(w, windows, &windows) {
1.66      nicm      485:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    486:                        if (wp->pid == pid) {
                    487:                                if (killpg(pid, SIGCONT) != 0)
                    488:                                        kill(pid, SIGCONT);
                    489:                        }
1.1       nicm      490:                }
                    491:        }
                    492: }