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

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