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

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