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

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