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

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