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

1.200   ! nicm        1: /* $OpenBSD: server.c,v 1.199 2022/03/25 06:14:42 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.200   ! nicm      233:                } else {
        !           234:                        fprintf(stderr, "%s\n", cause);
        !           235:                        exit(1);
        !           236:                }
1.187     nicm      237:        }
1.197     nicm      238:
                    239:        evtimer_set(&server_ev_tidy, server_tidy_event, NULL);
                    240:        evtimer_add(&server_ev_tidy, &tv);
1.178     nicm      241:
1.104     nicm      242:        server_add_accept(0);
1.144     nicm      243:        proc_loop(server_proc, server_loop);
1.167     nicm      244:
1.183     nicm      245:        job_kill_all();
                    246:        status_prompt_save_history();
1.167     nicm      247:
1.66      nicm      248:        exit(0);
1.1       nicm      249: }
                    250:
1.144     nicm      251: /* Server loop callback. */
1.160     nicm      252: static int
1.66      nicm      253: server_loop(void)
1.1       nicm      254: {
1.144     nicm      255:        struct client   *c;
1.162     nicm      256:        u_int            items;
                    257:
                    258:        do {
                    259:                items = cmdq_next(NULL);
1.164     nicm      260:                TAILQ_FOREACH(c, &clients, entry) {
                    261:                        if (c->flags & CLIENT_IDENTIFIED)
                    262:                                items += cmdq_next(c);
                    263:                }
1.162     nicm      264:        } while (items != 0);
1.31      nicm      265:
1.144     nicm      266:        server_client_loop();
1.179     nicm      267:
                    268:        if (!options_get_number(global_options, "exit-empty") && !server_exit)
                    269:                return (0);
1.1       nicm      270:
1.145     nicm      271:        if (!options_get_number(global_options, "exit-unattached")) {
1.97      nicm      272:                if (!RB_EMPTY(&sessions))
                    273:                        return (0);
1.1       nicm      274:        }
1.116     nicm      275:
1.122     nicm      276:        TAILQ_FOREACH(c, &clients, entry) {
                    277:                if (c->session != NULL)
1.116     nicm      278:                        return (0);
                    279:        }
                    280:
                    281:        /*
                    282:         * No attached clients therefore want to exit - flush any waiting
                    283:         * clients but don't actually exit until they've gone.
                    284:         */
                    285:        cmd_wait_for_flush();
1.122     nicm      286:        if (!TAILQ_EMPTY(&clients))
                    287:                return (0);
1.116     nicm      288:
1.183     nicm      289:        if (job_still_running())
                    290:                return (0);
1.180     nicm      291:
1.66      nicm      292:        return (1);
1.1       nicm      293: }
                    294:
1.141     nicm      295: /* Exit the server by killing all clients and windows. */
1.160     nicm      296: static void
1.141     nicm      297: server_send_exit(void)
1.1       nicm      298: {
1.122     nicm      299:        struct client   *c, *c1;
                    300:        struct session  *s, *s1;
1.116     nicm      301:
                    302:        cmd_wait_for_flush();
1.1       nicm      303:
1.122     nicm      304:        TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
1.144     nicm      305:                if (c->flags & CLIENT_SUSPENDED)
1.122     nicm      306:                        server_client_lost(c);
1.180     nicm      307:                else {
1.193     nicm      308:                        c->flags |= CLIENT_EXIT;
                    309:                        c->exit_type = CLIENT_EXIT_SHUTDOWN;
1.180     nicm      310:                }
1.122     nicm      311:                c->session = NULL;
1.42      nicm      312:        }
                    313:
1.122     nicm      314:        RB_FOREACH_SAFE(s, sessions, &sessions, s1)
1.184     nicm      315:                session_destroy(s, 1, __func__);
1.66      nicm      316: }
                    317:
                    318: /* Update socket execute permissions based on whether sessions are attached. */
1.75      nicm      319: void
1.66      nicm      320: server_update_socket(void)
1.42      nicm      321: {
1.66      nicm      322:        struct session  *s;
                    323:        static int       last = -1;
1.93      nicm      324:        int              n, mode;
                    325:        struct stat      sb;
1.1       nicm      326:
1.66      nicm      327:        n = 0;
1.97      nicm      328:        RB_FOREACH(s, sessions, &sessions) {
1.182     nicm      329:                if (s->attached != 0) {
1.66      nicm      330:                        n++;
                    331:                        break;
                    332:                }
                    333:        }
                    334:
                    335:        if (n != last) {
                    336:                last = n;
1.93      nicm      337:
                    338:                if (stat(socket_path, &sb) != 0)
                    339:                        return;
1.159     semarie   340:                mode = sb.st_mode & ACCESSPERMS;
1.93      nicm      341:                if (n != 0) {
                    342:                        if (mode & S_IRUSR)
                    343:                                mode |= S_IXUSR;
                    344:                        if (mode & S_IRGRP)
                    345:                                mode |= S_IXGRP;
                    346:                        if (mode & S_IROTH)
                    347:                                mode |= S_IXOTH;
                    348:                } else
                    349:                        mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
                    350:                chmod(socket_path, mode);
1.66      nicm      351:        }
                    352: }
                    353:
                    354: /* Callback for server socket. */
1.160     nicm      355: static void
1.150     nicm      356: server_accept(int fd, short events, __unused void *data)
1.66      nicm      357: {
                    358:        struct sockaddr_storage sa;
                    359:        socklen_t               slen = sizeof sa;
                    360:        int                     newfd;
                    361:
1.104     nicm      362:        server_add_accept(0);
1.66      nicm      363:        if (!(events & EV_READ))
                    364:                return;
                    365:
                    366:        newfd = accept(fd, (struct sockaddr *) &sa, &slen);
                    367:        if (newfd == -1) {
                    368:                if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
                    369:                        return;
1.104     nicm      370:                if (errno == ENFILE || errno == EMFILE) {
                    371:                        /* Delete and don't try again for 1 second. */
                    372:                        server_add_accept(1);
                    373:                        return;
                    374:                }
1.66      nicm      375:                fatal("accept failed");
                    376:        }
1.141     nicm      377:        if (server_exit) {
1.66      nicm      378:                close(newfd);
                    379:                return;
1.42      nicm      380:        }
1.66      nicm      381:        server_client_create(newfd);
                    382: }
                    383:
1.104     nicm      384: /*
                    385:  * Add accept event. If timeout is nonzero, add as a timeout instead of a read
                    386:  * event - used to backoff when running out of file descriptors.
                    387:  */
                    388: void
                    389: server_add_accept(int timeout)
                    390: {
                    391:        struct timeval tv = { timeout, 0 };
1.186     nicm      392:
                    393:        if (server_fd == -1)
                    394:                return;
1.104     nicm      395:
                    396:        if (event_initialized(&server_ev_accept))
                    397:                event_del(&server_ev_accept);
                    398:
                    399:        if (timeout == 0) {
1.144     nicm      400:                event_set(&server_ev_accept, server_fd, EV_READ, server_accept,
                    401:                    NULL);
1.104     nicm      402:                event_add(&server_ev_accept, NULL);
                    403:        } else {
1.144     nicm      404:                event_set(&server_ev_accept, server_fd, EV_TIMEOUT,
                    405:                    server_accept, NULL);
1.104     nicm      406:                event_add(&server_ev_accept, &tv);
                    407:        }
                    408: }
                    409:
1.66      nicm      410: /* Signal handler. */
1.160     nicm      411: static void
1.144     nicm      412: server_signal(int sig)
1.66      nicm      413: {
1.119     nicm      414:        int     fd;
1.120     nicm      415:
1.173     nicm      416:        log_debug("%s: %s", __func__, strsignal(sig));
1.66      nicm      417:        switch (sig) {
1.191     nicm      418:        case SIGINT:
1.66      nicm      419:        case SIGTERM:
1.141     nicm      420:                server_exit = 1;
                    421:                server_send_exit();
1.66      nicm      422:                break;
                    423:        case SIGCHLD:
                    424:                server_child_signal();
                    425:                break;
                    426:        case SIGUSR1:
                    427:                event_del(&server_ev_accept);
1.188     nicm      428:                fd = server_create_socket(server_client_flags, NULL);
1.119     nicm      429:                if (fd != -1) {
                    430:                        close(server_fd);
                    431:                        server_fd = fd;
                    432:                        server_update_socket();
                    433:                }
1.104     nicm      434:                server_add_accept(0);
1.171     nicm      435:                break;
                    436:        case SIGUSR2:
                    437:                proc_toggle_log(server_proc);
1.66      nicm      438:                break;
1.1       nicm      439:        }
                    440: }
                    441:
                    442: /* Handle SIGCHLD. */
1.160     nicm      443: static void
1.1       nicm      444: server_child_signal(void)
                    445: {
1.66      nicm      446:        int      status;
                    447:        pid_t    pid;
1.1       nicm      448:
                    449:        for (;;) {
                    450:                switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
                    451:                case -1:
                    452:                        if (errno == ECHILD)
                    453:                                return;
1.39      nicm      454:                        fatal("waitpid failed");
1.1       nicm      455:                case 0:
                    456:                        return;
                    457:                }
1.66      nicm      458:                if (WIFSTOPPED(status))
                    459:                        server_child_stopped(pid, status);
1.79      nicm      460:                else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66      nicm      461:                        server_child_exited(pid, status);
                    462:        }
                    463: }
                    464:
                    465: /* Handle exited children. */
1.160     nicm      466: static void
1.66      nicm      467: server_child_exited(pid_t pid, int status)
                    468: {
1.121     nicm      469:        struct window           *w, *w1;
1.66      nicm      470:        struct window_pane      *wp;
                    471:
1.121     nicm      472:        RB_FOREACH_SAFE(w, windows, &windows, w1) {
1.66      nicm      473:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    474:                        if (wp->pid == pid) {
1.118     nicm      475:                                wp->status = status;
1.177     nicm      476:                                wp->flags |= PANE_STATUSREADY;
1.172     nicm      477:
                    478:                                log_debug("%%%u exited", wp->id);
                    479:                                wp->flags |= PANE_EXITED;
                    480:
                    481:                                if (window_pane_destroy_ready(wp))
                    482:                                        server_destroy_pane(wp, 1);
1.77      nicm      483:                                break;
1.53      nicm      484:                        }
                    485:                }
1.80      nicm      486:        }
1.183     nicm      487:        job_check_died(pid, status);
1.53      nicm      488: }
                    489:
1.66      nicm      490: /* Handle stopped children. */
1.160     nicm      491: static void
1.66      nicm      492: server_child_stopped(pid_t pid, int status)
1.31      nicm      493: {
1.66      nicm      494:        struct window           *w;
                    495:        struct window_pane      *wp;
1.31      nicm      496:
1.66      nicm      497:        if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
                    498:                return;
1.31      nicm      499:
1.121     nicm      500:        RB_FOREACH(w, windows, &windows) {
1.66      nicm      501:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    502:                        if (wp->pid == pid) {
                    503:                                if (killpg(pid, SIGCONT) != 0)
                    504:                                        kill(pid, SIGCONT);
                    505:                        }
1.1       nicm      506:                }
                    507:        }
1.189     nicm      508:        job_check_died(pid, status);
1.190     nicm      509: }
                    510:
                    511: /* Add to message log. */
                    512: void
                    513: server_add_message(const char *fmt, ...)
                    514: {
                    515:        struct message_entry    *msg, *msg1;
                    516:        char                    *s;
                    517:        va_list                  ap;
                    518:        u_int                    limit;
                    519:
                    520:        va_start(ap, fmt);
                    521:        xvasprintf(&s, fmt, ap);
                    522:        va_end(ap);
                    523:
                    524:        log_debug("message: %s", s);
                    525:
                    526:        msg = xcalloc(1, sizeof *msg);
                    527:        gettimeofday(&msg->msg_time, NULL);
                    528:        msg->msg_num = message_next++;
                    529:        msg->msg = s;
                    530:        TAILQ_INSERT_TAIL(&message_log, msg, entry);
                    531:
                    532:        limit = options_get_number(global_options, "message-limit");
                    533:        TAILQ_FOREACH_SAFE(msg, &message_log, entry, msg1) {
                    534:                if (msg->msg_num + limit >= message_next)
                    535:                        break;
                    536:                free(msg->msg);
                    537:                TAILQ_REMOVE(&message_log, msg, entry);
                    538:                free(msg);
                    539:        }
1.1       nicm      540: }