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

1.135   ! nicm        1: /* $OpenBSD: server.c,v 1.134 2015/08/28 13:12:20 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()) {
                    253:                event_loop(EVLOOP_ONCE);
1.1       nicm      254:
1.60      nicm      255:                server_window_loop();
                    256:                server_client_loop();
1.80      nicm      257:        }
1.66      nicm      258: }
1.31      nicm      259:
1.112     nicm      260: /* Check if the server should exit (no more clients or sessions). */
1.66      nicm      261: int
                    262: server_should_shutdown(void)
                    263: {
1.116     nicm      264:        struct client   *c;
1.1       nicm      265:
1.94      nicm      266:        if (!options_get_number(&global_options, "exit-unattached")) {
1.97      nicm      267:                if (!RB_EMPTY(&sessions))
                    268:                        return (0);
1.1       nicm      269:        }
1.116     nicm      270:
1.122     nicm      271:        TAILQ_FOREACH(c, &clients, entry) {
                    272:                if (c->session != NULL)
1.116     nicm      273:                        return (0);
                    274:        }
                    275:
                    276:        /*
                    277:         * No attached clients therefore want to exit - flush any waiting
                    278:         * clients but don't actually exit until they've gone.
                    279:         */
                    280:        cmd_wait_for_flush();
1.122     nicm      281:        if (!TAILQ_EMPTY(&clients))
                    282:                return (0);
1.116     nicm      283:
1.66      nicm      284:        return (1);
1.1       nicm      285: }
                    286:
1.66      nicm      287: /* Shutdown the server by killing all clients and windows. */
1.1       nicm      288: void
1.66      nicm      289: server_send_shutdown(void)
1.1       nicm      290: {
1.122     nicm      291:        struct client   *c, *c1;
                    292:        struct session  *s, *s1;
1.116     nicm      293:
                    294:        cmd_wait_for_flush();
1.1       nicm      295:
1.122     nicm      296:        TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
                    297:                if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
                    298:                        server_client_lost(c);
                    299:                else
                    300:                        server_write_client(c, MSG_SHUTDOWN, NULL, 0);
                    301:                c->session = NULL;
1.42      nicm      302:        }
                    303:
1.122     nicm      304:        RB_FOREACH_SAFE(s, sessions, &sessions, s1)
1.97      nicm      305:                session_destroy(s);
1.66      nicm      306: }
                    307:
                    308: /* Update socket execute permissions based on whether sessions are attached. */
1.75      nicm      309: void
1.66      nicm      310: server_update_socket(void)
1.42      nicm      311: {
1.66      nicm      312:        struct session  *s;
                    313:        static int       last = -1;
1.93      nicm      314:        int              n, mode;
                    315:        struct stat      sb;
1.1       nicm      316:
1.66      nicm      317:        n = 0;
1.97      nicm      318:        RB_FOREACH(s, sessions, &sessions) {
                    319:                if (!(s->flags & SESSION_UNATTACHED)) {
1.66      nicm      320:                        n++;
                    321:                        break;
                    322:                }
                    323:        }
                    324:
                    325:        if (n != last) {
                    326:                last = n;
1.93      nicm      327:
                    328:                if (stat(socket_path, &sb) != 0)
                    329:                        return;
                    330:                mode = sb.st_mode;
                    331:                if (n != 0) {
                    332:                        if (mode & S_IRUSR)
                    333:                                mode |= S_IXUSR;
                    334:                        if (mode & S_IRGRP)
                    335:                                mode |= S_IXGRP;
                    336:                        if (mode & S_IROTH)
                    337:                                mode |= S_IXOTH;
                    338:                } else
                    339:                        mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
                    340:                chmod(socket_path, mode);
1.66      nicm      341:        }
                    342: }
                    343:
                    344: /* Callback for server socket. */
                    345: void
                    346: server_accept_callback(int fd, short events, unused void *data)
                    347: {
                    348:        struct sockaddr_storage sa;
                    349:        socklen_t               slen = sizeof sa;
                    350:        int                     newfd;
                    351:
1.104     nicm      352:        server_add_accept(0);
1.66      nicm      353:        if (!(events & EV_READ))
                    354:                return;
                    355:
                    356:        newfd = accept(fd, (struct sockaddr *) &sa, &slen);
                    357:        if (newfd == -1) {
                    358:                if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
                    359:                        return;
1.104     nicm      360:                if (errno == ENFILE || errno == EMFILE) {
                    361:                        /* Delete and don't try again for 1 second. */
                    362:                        server_add_accept(1);
                    363:                        return;
                    364:                }
1.66      nicm      365:                fatal("accept failed");
                    366:        }
                    367:        if (server_shutdown) {
                    368:                close(newfd);
                    369:                return;
1.42      nicm      370:        }
1.66      nicm      371:        server_client_create(newfd);
                    372: }
                    373:
1.104     nicm      374: /*
                    375:  * Add accept event. If timeout is nonzero, add as a timeout instead of a read
                    376:  * event - used to backoff when running out of file descriptors.
                    377:  */
                    378: void
                    379: server_add_accept(int timeout)
                    380: {
                    381:        struct timeval tv = { timeout, 0 };
                    382:
                    383:        if (event_initialized(&server_ev_accept))
                    384:                event_del(&server_ev_accept);
                    385:
                    386:        if (timeout == 0) {
                    387:                event_set(&server_ev_accept,
                    388:                    server_fd, EV_READ, server_accept_callback, NULL);
                    389:                event_add(&server_ev_accept, NULL);
                    390:        } else {
                    391:                event_set(&server_ev_accept,
                    392:                    server_fd, EV_TIMEOUT, server_accept_callback, NULL);
                    393:                event_add(&server_ev_accept, &tv);
                    394:        }
                    395: }
                    396:
1.66      nicm      397: /* Signal handler. */
                    398: void
                    399: server_signal_callback(int sig, unused short events, unused void *data)
                    400: {
1.119     nicm      401:        int     fd;
1.120     nicm      402:
1.66      nicm      403:        switch (sig) {
                    404:        case SIGTERM:
                    405:                server_shutdown = 1;
                    406:                server_send_shutdown();
                    407:                break;
                    408:        case SIGCHLD:
                    409:                server_child_signal();
                    410:                break;
                    411:        case SIGUSR1:
                    412:                event_del(&server_ev_accept);
1.119     nicm      413:                fd = server_create_socket();
                    414:                if (fd != -1) {
                    415:                        close(server_fd);
                    416:                        server_fd = fd;
                    417:                        server_update_socket();
                    418:                }
1.104     nicm      419:                server_add_accept(0);
1.66      nicm      420:                break;
1.1       nicm      421:        }
                    422: }
                    423:
                    424: /* Handle SIGCHLD. */
                    425: void
                    426: server_child_signal(void)
                    427: {
1.66      nicm      428:        int      status;
                    429:        pid_t    pid;
1.1       nicm      430:
                    431:        for (;;) {
                    432:                switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
                    433:                case -1:
                    434:                        if (errno == ECHILD)
                    435:                                return;
1.39      nicm      436:                        fatal("waitpid failed");
1.1       nicm      437:                case 0:
                    438:                        return;
                    439:                }
1.66      nicm      440:                if (WIFSTOPPED(status))
                    441:                        server_child_stopped(pid, status);
1.79      nicm      442:                else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66      nicm      443:                        server_child_exited(pid, status);
                    444:        }
                    445: }
                    446:
                    447: /* Handle exited children. */
                    448: void
                    449: server_child_exited(pid_t pid, int status)
                    450: {
1.121     nicm      451:        struct window           *w, *w1;
1.66      nicm      452:        struct window_pane      *wp;
                    453:        struct job              *job;
                    454:
1.121     nicm      455:        RB_FOREACH_SAFE(w, windows, &windows, w1) {
1.66      nicm      456:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    457:                        if (wp->pid == pid) {
1.118     nicm      458:                                wp->status = status;
1.77      nicm      459:                                server_destroy_pane(wp);
                    460:                                break;
1.53      nicm      461:                        }
                    462:                }
1.80      nicm      463:        }
1.1       nicm      464:
1.101     nicm      465:        LIST_FOREACH(job, &all_jobs, lentry) {
1.66      nicm      466:                if (pid == job->pid) {
1.67      nicm      467:                        job_died(job, status);  /* might free job */
                    468:                        break;
1.1       nicm      469:                }
1.53      nicm      470:        }
                    471: }
                    472:
1.66      nicm      473: /* Handle stopped children. */
1.31      nicm      474: void
1.66      nicm      475: server_child_stopped(pid_t pid, int status)
1.31      nicm      476: {
1.66      nicm      477:        struct window           *w;
                    478:        struct window_pane      *wp;
1.31      nicm      479:
1.66      nicm      480:        if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
                    481:                return;
1.31      nicm      482:
1.121     nicm      483:        RB_FOREACH(w, windows, &windows) {
1.66      nicm      484:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    485:                        if (wp->pid == pid) {
                    486:                                if (killpg(pid, SIGCONT) != 0)
                    487:                                        kill(pid, SIGCONT);
                    488:                        }
1.1       nicm      489:                }
                    490:        }
                    491: }