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

1.183   ! nicm        1: /* $OpenBSD: server.c,v 1.182 2018/08/18 20:08:52 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.157     nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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:
1.155     nicm       44: struct clients          clients;
1.1       nicm       45:
1.155     nicm       46: struct tmuxproc                *server_proc;
1.160     nicm       47: static int              server_fd;
                     48: static int              server_exit;
                     49: static struct event     server_ev_accept;
1.155     nicm       50:
                     51: struct cmd_find_state   marked_pane;
1.126     nicm       52:
1.160     nicm       53: static int     server_loop(void);
                     54: static void    server_send_exit(void);
                     55: static void    server_accept(int, short, void *);
                     56: static void    server_signal(int);
                     57: static void    server_child_signal(void);
                     58: static void    server_child_exited(pid_t, int);
                     59: static void    server_child_stopped(pid_t, int);
1.126     nicm       60:
                     61: /* Set marked pane. */
                     62: void
                     63: server_set_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
                     64: {
1.169     nicm       65:        cmd_find_clear_state(&marked_pane, 0);
1.155     nicm       66:        marked_pane.s = s;
                     67:        marked_pane.wl = wl;
                     68:        marked_pane.w = wl->window;
                     69:        marked_pane.wp = wp;
1.126     nicm       70: }
                     71:
                     72: /* Clear marked pane. */
                     73: void
                     74: server_clear_marked(void)
                     75: {
1.169     nicm       76:        cmd_find_clear_state(&marked_pane, 0);
1.126     nicm       77: }
                     78:
                     79: /* Is this the marked pane? */
                     80: int
                     81: server_is_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
                     82: {
                     83:        if (s == NULL || wl == NULL || wp == NULL)
                     84:                return (0);
1.155     nicm       85:        if (marked_pane.s != s || marked_pane.wl != wl)
1.126     nicm       86:                return (0);
1.155     nicm       87:        if (marked_pane.wp != wp)
1.126     nicm       88:                return (0);
                     89:        return (server_check_marked());
                     90: }
                     91:
                     92: /* Check if the marked pane is still valid. */
                     93: int
                     94: server_check_marked(void)
                     95: {
1.155     nicm       96:        return (cmd_find_valid_state(&marked_pane));
1.126     nicm       97: }
1.45      nicm       98:
1.60      nicm       99: /* Create server socket. */
1.160     nicm      100: static int
1.178     nicm      101: server_create_socket(char **cause)
1.1       nicm      102: {
1.60      nicm      103:        struct sockaddr_un      sa;
                    104:        size_t                  size;
                    105:        mode_t                  mask;
1.178     nicm      106:        int                     fd, saved_errno;
1.60      nicm      107:
                    108:        memset(&sa, 0, sizeof sa);
                    109:        sa.sun_family = AF_UNIX;
                    110:        size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
                    111:        if (size >= sizeof sa.sun_path) {
                    112:                errno = ENAMETOOLONG;
1.178     nicm      113:                goto fail;
1.60      nicm      114:        }
                    115:        unlink(sa.sun_path);
                    116:
                    117:        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.178     nicm      118:                goto fail;
1.60      nicm      119:
1.89      nicm      120:        mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
1.178     nicm      121:        if (bind(fd, (struct sockaddr *)&sa, sizeof sa) == -1) {
                    122:                saved_errno = errno;
1.170     nicm      123:                close(fd);
1.178     nicm      124:                errno = saved_errno;
                    125:                goto fail;
1.170     nicm      126:        }
1.60      nicm      127:        umask(mask);
                    128:
1.170     nicm      129:        if (listen(fd, 128) == -1) {
1.178     nicm      130:                saved_errno = errno;
1.170     nicm      131:                close(fd);
1.178     nicm      132:                errno = saved_errno;
                    133:                goto fail;
1.170     nicm      134:        }
1.100     nicm      135:        setblocking(fd, 0);
1.1       nicm      136:
1.60      nicm      137:        return (fd);
1.178     nicm      138:
                    139: fail:
                    140:        if (cause != NULL) {
                    141:                xasprintf(cause, "error creating %s (%s)", socket_path,
                    142:                    strerror(errno));
                    143:        }
                    144:        return (-1);
                    145: }
                    146:
                    147: /* Server error callback. */
                    148: static enum cmd_retval
                    149: server_start_error(struct cmdq_item *item, void *data)
                    150: {
                    151:        char    *error = data;
                    152:
                    153:        cmdq_error(item, "%s", error);
                    154:        free(error);
                    155:
                    156:        return (CMD_RETURN_NORMAL);
1.60      nicm      157: }
                    158:
1.1       nicm      159: /* Fork new server. */
                    160: int
1.174     nicm      161: server_start(struct tmuxproc *client, struct event_base *base, int lockfd,
                    162:     char *lockfile)
1.1       nicm      163: {
1.167     nicm      164:        int              pair[2];
1.175     nicm      165:        sigset_t         set, oldset;
1.178     nicm      166:        struct client   *c;
                    167:        char            *cause = NULL;
1.1       nicm      168:
                    169:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
                    170:                fatal("socketpair failed");
                    171:
1.175     nicm      172:        sigfillset(&set);
                    173:        sigprocmask(SIG_BLOCK, &set, &oldset);
1.174     nicm      174:        switch (fork()) {
                    175:        case -1:
                    176:                fatal("fork failed");
                    177:        case 0:
                    178:                break;
                    179:        default:
1.175     nicm      180:                sigprocmask(SIG_SETMASK, &oldset, NULL);
1.1       nicm      181:                close(pair[1]);
                    182:                return (pair[0]);
                    183:        }
                    184:        close(pair[0]);
1.174     nicm      185:        if (daemon(1, 0) != 0)
                    186:                fatal("daemon failed");
1.176     nicm      187:        proc_clear_signals(client, 0);
1.174     nicm      188:        if (event_reinit(base) != 0)
                    189:                fatalx("event_reinit failed");
                    190:        server_proc = proc_start("server");
                    191:        proc_set_signals(server_proc, server_signal);
1.175     nicm      192:        sigprocmask(SIG_SETMASK, &oldset, NULL);
1.143     nicm      193:
1.171     nicm      194:        if (log_get_level() > 1)
1.146     nicm      195:                tty_create_log();
1.151     nicm      196:        if (pledge("stdio rpath wpath cpath fattr unix getpw recvfd proc exec "
                    197:            "tty ps", NULL) != 0)
1.143     nicm      198:                fatal("pledge failed");
1.1       nicm      199:
1.121     nicm      200:        RB_INIT(&windows);
1.102     nicm      201:        RB_INIT(&all_window_panes);
1.122     nicm      202:        TAILQ_INIT(&clients);
1.97      nicm      203:        RB_INIT(&sessions);
1.1       nicm      204:        key_bindings_init();
                    205:
1.153     nicm      206:        gettimeofday(&start_time, NULL);
1.16      nicm      207:
1.178     nicm      208:        server_fd = server_create_socket(&cause);
                    209:        if (server_fd != -1)
                    210:                server_update_socket();
                    211:        c = server_client_create(pair[1]);
1.103     nicm      212:
1.154     nicm      213:        if (lockfd >= 0) {
                    214:                unlink(lockfile);
                    215:                free(lockfile);
                    216:                close(lockfd);
                    217:        }
1.16      nicm      218:
1.178     nicm      219:        if (cause != NULL) {
                    220:                cmdq_append(c, cmdq_get_callback(server_start_error, cause));
                    221:                c->flags |= CLIENT_EXIT;
                    222:        }
                    223:
1.139     nicm      224:        start_cfg();
1.104     nicm      225:        server_add_accept(0);
1.66      nicm      226:
1.144     nicm      227:        proc_loop(server_proc, server_loop);
1.167     nicm      228:
1.183   ! nicm      229:        job_kill_all();
        !           230:        status_prompt_save_history();
1.167     nicm      231:
1.66      nicm      232:        exit(0);
1.1       nicm      233: }
                    234:
1.144     nicm      235: /* Server loop callback. */
1.160     nicm      236: static int
1.66      nicm      237: server_loop(void)
1.1       nicm      238: {
1.144     nicm      239:        struct client   *c;
1.162     nicm      240:        u_int            items;
                    241:
                    242:        do {
                    243:                items = cmdq_next(NULL);
1.164     nicm      244:                TAILQ_FOREACH(c, &clients, entry) {
                    245:                        if (c->flags & CLIENT_IDENTIFIED)
                    246:                                items += cmdq_next(c);
                    247:                }
1.162     nicm      248:        } while (items != 0);
1.31      nicm      249:
1.144     nicm      250:        server_client_loop();
1.179     nicm      251:
                    252:        if (!options_get_number(global_options, "exit-empty") && !server_exit)
                    253:                return (0);
1.1       nicm      254:
1.145     nicm      255:        if (!options_get_number(global_options, "exit-unattached")) {
1.97      nicm      256:                if (!RB_EMPTY(&sessions))
                    257:                        return (0);
1.1       nicm      258:        }
1.116     nicm      259:
1.122     nicm      260:        TAILQ_FOREACH(c, &clients, entry) {
                    261:                if (c->session != NULL)
1.116     nicm      262:                        return (0);
                    263:        }
                    264:
                    265:        /*
                    266:         * No attached clients therefore want to exit - flush any waiting
                    267:         * clients but don't actually exit until they've gone.
                    268:         */
                    269:        cmd_wait_for_flush();
1.122     nicm      270:        if (!TAILQ_EMPTY(&clients))
                    271:                return (0);
1.116     nicm      272:
1.183   ! nicm      273:        if (job_still_running())
        !           274:                return (0);
1.180     nicm      275:
1.66      nicm      276:        return (1);
1.1       nicm      277: }
                    278:
1.141     nicm      279: /* Exit the server by killing all clients and windows. */
1.160     nicm      280: static void
1.141     nicm      281: server_send_exit(void)
1.1       nicm      282: {
1.122     nicm      283:        struct client   *c, *c1;
                    284:        struct session  *s, *s1;
1.116     nicm      285:
                    286:        cmd_wait_for_flush();
1.1       nicm      287:
1.122     nicm      288:        TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
1.144     nicm      289:                if (c->flags & CLIENT_SUSPENDED)
1.122     nicm      290:                        server_client_lost(c);
1.180     nicm      291:                else {
                    292:                        if (c->flags & CLIENT_ATTACHED)
                    293:                                notify_client("client-detached", c);
1.144     nicm      294:                        proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0);
1.180     nicm      295:                }
1.122     nicm      296:                c->session = NULL;
1.42      nicm      297:        }
                    298:
1.122     nicm      299:        RB_FOREACH_SAFE(s, sessions, &sessions, s1)
1.173     nicm      300:                session_destroy(s, __func__);
1.66      nicm      301: }
                    302:
                    303: /* Update socket execute permissions based on whether sessions are attached. */
1.75      nicm      304: void
1.66      nicm      305: server_update_socket(void)
1.42      nicm      306: {
1.66      nicm      307:        struct session  *s;
                    308:        static int       last = -1;
1.93      nicm      309:        int              n, mode;
                    310:        struct stat      sb;
1.1       nicm      311:
1.66      nicm      312:        n = 0;
1.97      nicm      313:        RB_FOREACH(s, sessions, &sessions) {
1.182     nicm      314:                if (s->attached != 0) {
1.66      nicm      315:                        n++;
                    316:                        break;
                    317:                }
                    318:        }
                    319:
                    320:        if (n != last) {
                    321:                last = n;
1.93      nicm      322:
                    323:                if (stat(socket_path, &sb) != 0)
                    324:                        return;
1.159     semarie   325:                mode = sb.st_mode & ACCESSPERMS;
1.93      nicm      326:                if (n != 0) {
                    327:                        if (mode & S_IRUSR)
                    328:                                mode |= S_IXUSR;
                    329:                        if (mode & S_IRGRP)
                    330:                                mode |= S_IXGRP;
                    331:                        if (mode & S_IROTH)
                    332:                                mode |= S_IXOTH;
                    333:                } else
                    334:                        mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
                    335:                chmod(socket_path, mode);
1.66      nicm      336:        }
                    337: }
                    338:
                    339: /* Callback for server socket. */
1.160     nicm      340: static void
1.150     nicm      341: server_accept(int fd, short events, __unused void *data)
1.66      nicm      342: {
                    343:        struct sockaddr_storage sa;
                    344:        socklen_t               slen = sizeof sa;
                    345:        int                     newfd;
                    346:
1.104     nicm      347:        server_add_accept(0);
1.66      nicm      348:        if (!(events & EV_READ))
                    349:                return;
                    350:
                    351:        newfd = accept(fd, (struct sockaddr *) &sa, &slen);
                    352:        if (newfd == -1) {
                    353:                if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
                    354:                        return;
1.104     nicm      355:                if (errno == ENFILE || errno == EMFILE) {
                    356:                        /* Delete and don't try again for 1 second. */
                    357:                        server_add_accept(1);
                    358:                        return;
                    359:                }
1.66      nicm      360:                fatal("accept failed");
                    361:        }
1.141     nicm      362:        if (server_exit) {
1.66      nicm      363:                close(newfd);
                    364:                return;
1.42      nicm      365:        }
1.66      nicm      366:        server_client_create(newfd);
                    367: }
                    368:
1.104     nicm      369: /*
                    370:  * Add accept event. If timeout is nonzero, add as a timeout instead of a read
                    371:  * event - used to backoff when running out of file descriptors.
                    372:  */
                    373: void
                    374: server_add_accept(int timeout)
                    375: {
                    376:        struct timeval tv = { timeout, 0 };
                    377:
                    378:        if (event_initialized(&server_ev_accept))
                    379:                event_del(&server_ev_accept);
                    380:
                    381:        if (timeout == 0) {
1.144     nicm      382:                event_set(&server_ev_accept, server_fd, EV_READ, server_accept,
                    383:                    NULL);
1.104     nicm      384:                event_add(&server_ev_accept, NULL);
                    385:        } else {
1.144     nicm      386:                event_set(&server_ev_accept, server_fd, EV_TIMEOUT,
                    387:                    server_accept, NULL);
1.104     nicm      388:                event_add(&server_ev_accept, &tv);
                    389:        }
                    390: }
                    391:
1.66      nicm      392: /* Signal handler. */
1.160     nicm      393: static void
1.144     nicm      394: server_signal(int sig)
1.66      nicm      395: {
1.119     nicm      396:        int     fd;
1.120     nicm      397:
1.173     nicm      398:        log_debug("%s: %s", __func__, strsignal(sig));
1.66      nicm      399:        switch (sig) {
                    400:        case SIGTERM:
1.141     nicm      401:                server_exit = 1;
                    402:                server_send_exit();
1.66      nicm      403:                break;
                    404:        case SIGCHLD:
                    405:                server_child_signal();
                    406:                break;
                    407:        case SIGUSR1:
                    408:                event_del(&server_ev_accept);
1.178     nicm      409:                fd = server_create_socket(NULL);
1.119     nicm      410:                if (fd != -1) {
                    411:                        close(server_fd);
                    412:                        server_fd = fd;
                    413:                        server_update_socket();
                    414:                }
1.104     nicm      415:                server_add_accept(0);
1.171     nicm      416:                break;
                    417:        case SIGUSR2:
                    418:                proc_toggle_log(server_proc);
1.66      nicm      419:                break;
1.1       nicm      420:        }
                    421: }
                    422:
                    423: /* Handle SIGCHLD. */
1.160     nicm      424: static void
1.1       nicm      425: server_child_signal(void)
                    426: {
1.66      nicm      427:        int      status;
                    428:        pid_t    pid;
1.1       nicm      429:
                    430:        for (;;) {
                    431:                switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
                    432:                case -1:
                    433:                        if (errno == ECHILD)
                    434:                                return;
1.39      nicm      435:                        fatal("waitpid failed");
1.1       nicm      436:                case 0:
                    437:                        return;
                    438:                }
1.66      nicm      439:                if (WIFSTOPPED(status))
                    440:                        server_child_stopped(pid, status);
1.79      nicm      441:                else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66      nicm      442:                        server_child_exited(pid, status);
                    443:        }
                    444: }
                    445:
                    446: /* Handle exited children. */
1.160     nicm      447: static void
1.66      nicm      448: server_child_exited(pid_t pid, int status)
                    449: {
1.121     nicm      450:        struct window           *w, *w1;
1.66      nicm      451:        struct window_pane      *wp;
                    452:
1.121     nicm      453:        RB_FOREACH_SAFE(w, windows, &windows, w1) {
1.66      nicm      454:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    455:                        if (wp->pid == pid) {
1.118     nicm      456:                                wp->status = status;
1.177     nicm      457:                                wp->flags |= PANE_STATUSREADY;
1.172     nicm      458:
                    459:                                log_debug("%%%u exited", wp->id);
                    460:                                wp->flags |= PANE_EXITED;
                    461:
                    462:                                if (window_pane_destroy_ready(wp))
                    463:                                        server_destroy_pane(wp, 1);
1.77      nicm      464:                                break;
1.53      nicm      465:                        }
                    466:                }
1.80      nicm      467:        }
1.183   ! nicm      468:        job_check_died(pid, status);
1.53      nicm      469: }
                    470:
1.66      nicm      471: /* Handle stopped children. */
1.160     nicm      472: static void
1.66      nicm      473: server_child_stopped(pid_t pid, int status)
1.31      nicm      474: {
1.66      nicm      475:        struct window           *w;
                    476:        struct window_pane      *wp;
1.31      nicm      477:
1.66      nicm      478:        if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
                    479:                return;
1.31      nicm      480:
1.121     nicm      481:        RB_FOREACH(w, windows, &windows) {
1.66      nicm      482:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    483:                        if (wp->pid == pid) {
                    484:                                if (killpg(pid, SIGCONT) != 0)
                    485:                                        kill(pid, SIGCONT);
                    486:                        }
1.1       nicm      487:                }
                    488:        }
                    489: }