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

1.166   ! nicm        1: /* $OpenBSD: server.c,v 1.165 2017/01/24 21:50:22 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.160     nicm       47: static int              server_fd;
                     48: static int              server_exit;
                     49: static struct event     server_ev_accept;
1.155     nicm       50:
                     51: struct cmd_find_state   marked_pane;
1.126     nicm       52:
1.160     nicm       53: static int     server_create_socket(void);
                     54: static int     server_loop(void);
                     55: static void    server_send_exit(void);
                     56: static void    server_accept(int, short, void *);
                     57: static void    server_signal(int);
                     58: static void    server_child_signal(void);
                     59: static void    server_child_exited(pid_t, int);
                     60: static void    server_child_stopped(pid_t, int);
1.126     nicm       61:
                     62: /* Set marked pane. */
                     63: void
                     64: server_set_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
                     65: {
1.155     nicm       66:        cmd_find_clear_state(&marked_pane, NULL, 0);
                     67:        marked_pane.s = s;
                     68:        marked_pane.wl = wl;
                     69:        marked_pane.w = wl->window;
                     70:        marked_pane.wp = wp;
1.126     nicm       71: }
                     72:
                     73: /* Clear marked pane. */
                     74: void
                     75: server_clear_marked(void)
                     76: {
1.155     nicm       77:        cmd_find_clear_state(&marked_pane, NULL, 0);
1.126     nicm       78: }
                     79:
                     80: /* Is this the marked pane? */
                     81: int
                     82: server_is_marked(struct session *s, struct winlink *wl, struct window_pane *wp)
                     83: {
                     84:        if (s == NULL || wl == NULL || wp == NULL)
                     85:                return (0);
1.155     nicm       86:        if (marked_pane.s != s || marked_pane.wl != wl)
1.126     nicm       87:                return (0);
1.155     nicm       88:        if (marked_pane.wp != wp)
1.126     nicm       89:                return (0);
                     90:        return (server_check_marked());
                     91: }
                     92:
                     93: /* Check if the marked pane is still valid. */
                     94: int
                     95: server_check_marked(void)
                     96: {
1.155     nicm       97:        return (cmd_find_valid_state(&marked_pane));
1.126     nicm       98: }
1.45      nicm       99:
1.60      nicm      100: /* Create server socket. */
1.160     nicm      101: static int
1.60      nicm      102: server_create_socket(void)
1.1       nicm      103: {
1.60      nicm      104:        struct sockaddr_un      sa;
                    105:        size_t                  size;
                    106:        mode_t                  mask;
1.100     nicm      107:        int                     fd;
1.60      nicm      108:
                    109:        memset(&sa, 0, sizeof sa);
                    110:        sa.sun_family = AF_UNIX;
                    111:        size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
                    112:        if (size >= sizeof sa.sun_path) {
                    113:                errno = ENAMETOOLONG;
1.119     nicm      114:                return (-1);
1.60      nicm      115:        }
                    116:        unlink(sa.sun_path);
                    117:
                    118:        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.119     nicm      119:                return (-1);
1.60      nicm      120:
1.89      nicm      121:        mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
1.140     guenther  122:        if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == -1)
1.119     nicm      123:                return (-1);
1.60      nicm      124:        umask(mask);
                    125:
1.158     nicm      126:        if (listen(fd, 128) == -1)
1.119     nicm      127:                return (-1);
1.100     nicm      128:        setblocking(fd, 0);
1.1       nicm      129:
1.60      nicm      130:        return (fd);
                    131: }
                    132:
1.1       nicm      133: /* Fork new server. */
                    134: int
1.138     nicm      135: server_start(struct event_base *base, int lockfd, char *lockfile)
1.1       nicm      136: {
1.139     nicm      137:        int     pair[2];
1.1       nicm      138:
                    139:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
                    140:                fatal("socketpair failed");
                    141:
1.144     nicm      142:        server_proc = proc_start("server", base, 1, server_signal);
                    143:        if (server_proc == NULL) {
1.1       nicm      144:                close(pair[1]);
                    145:                return (pair[0]);
                    146:        }
                    147:        close(pair[0]);
1.143     nicm      148:
1.152     nicm      149:        if (log_get_level() > 3)
1.146     nicm      150:                tty_create_log();
1.151     nicm      151:        if (pledge("stdio rpath wpath cpath fattr unix getpw recvfd proc exec "
                    152:            "tty ps", NULL) != 0)
1.143     nicm      153:                fatal("pledge failed");
1.1       nicm      154:
1.121     nicm      155:        RB_INIT(&windows);
1.102     nicm      156:        RB_INIT(&all_window_panes);
1.122     nicm      157:        TAILQ_INIT(&clients);
1.97      nicm      158:        RB_INIT(&sessions);
1.166   ! nicm      159:        RB_INIT(&session_groups);
1.1       nicm      160:        key_bindings_init();
                    161:
1.153     nicm      162:        gettimeofday(&start_time, NULL);
1.16      nicm      163:
1.66      nicm      164:        server_fd = server_create_socket();
1.119     nicm      165:        if (server_fd == -1)
                    166:                fatal("couldn't create socket");
                    167:        server_update_socket();
1.60      nicm      168:        server_client_create(pair[1]);
1.103     nicm      169:
1.154     nicm      170:        if (lockfd >= 0) {
                    171:                unlink(lockfile);
                    172:                free(lockfile);
                    173:                close(lockfd);
                    174:        }
1.16      nicm      175:
1.139     nicm      176:        start_cfg();
                    177:
1.129     nicm      178:        status_prompt_load_history();
1.66      nicm      179:
1.104     nicm      180:        server_add_accept(0);
1.66      nicm      181:
1.144     nicm      182:        proc_loop(server_proc, server_loop);
1.129     nicm      183:        status_prompt_save_history();
1.66      nicm      184:        exit(0);
1.1       nicm      185: }
                    186:
1.144     nicm      187: /* Server loop callback. */
1.160     nicm      188: static int
1.66      nicm      189: server_loop(void)
1.1       nicm      190: {
1.144     nicm      191:        struct client   *c;
1.162     nicm      192:        u_int            items;
                    193:
                    194:        do {
                    195:                items = cmdq_next(NULL);
1.164     nicm      196:                TAILQ_FOREACH(c, &clients, entry) {
                    197:                        if (c->flags & CLIENT_IDENTIFIED)
                    198:                                items += cmdq_next(c);
                    199:                }
1.162     nicm      200:        } while (items != 0);
1.31      nicm      201:
1.144     nicm      202:        server_client_loop();
1.1       nicm      203:
1.145     nicm      204:        if (!options_get_number(global_options, "exit-unattached")) {
1.97      nicm      205:                if (!RB_EMPTY(&sessions))
                    206:                        return (0);
1.1       nicm      207:        }
1.116     nicm      208:
1.122     nicm      209:        TAILQ_FOREACH(c, &clients, entry) {
                    210:                if (c->session != NULL)
1.116     nicm      211:                        return (0);
                    212:        }
                    213:
                    214:        /*
                    215:         * No attached clients therefore want to exit - flush any waiting
                    216:         * clients but don't actually exit until they've gone.
                    217:         */
                    218:        cmd_wait_for_flush();
1.122     nicm      219:        if (!TAILQ_EMPTY(&clients))
                    220:                return (0);
1.116     nicm      221:
1.66      nicm      222:        return (1);
1.1       nicm      223: }
                    224:
1.141     nicm      225: /* Exit the server by killing all clients and windows. */
1.160     nicm      226: static void
1.141     nicm      227: server_send_exit(void)
1.1       nicm      228: {
1.122     nicm      229:        struct client   *c, *c1;
                    230:        struct session  *s, *s1;
1.116     nicm      231:
                    232:        cmd_wait_for_flush();
1.1       nicm      233:
1.122     nicm      234:        TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
1.144     nicm      235:                if (c->flags & CLIENT_SUSPENDED)
1.122     nicm      236:                        server_client_lost(c);
                    237:                else
1.144     nicm      238:                        proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0);
1.122     nicm      239:                c->session = NULL;
1.42      nicm      240:        }
                    241:
1.122     nicm      242:        RB_FOREACH_SAFE(s, sessions, &sessions, s1)
1.97      nicm      243:                session_destroy(s);
1.66      nicm      244: }
                    245:
                    246: /* Update socket execute permissions based on whether sessions are attached. */
1.75      nicm      247: void
1.66      nicm      248: server_update_socket(void)
1.42      nicm      249: {
1.66      nicm      250:        struct session  *s;
                    251:        static int       last = -1;
1.93      nicm      252:        int              n, mode;
                    253:        struct stat      sb;
1.1       nicm      254:
1.66      nicm      255:        n = 0;
1.97      nicm      256:        RB_FOREACH(s, sessions, &sessions) {
                    257:                if (!(s->flags & SESSION_UNATTACHED)) {
1.66      nicm      258:                        n++;
                    259:                        break;
                    260:                }
                    261:        }
                    262:
                    263:        if (n != last) {
                    264:                last = n;
1.93      nicm      265:
                    266:                if (stat(socket_path, &sb) != 0)
                    267:                        return;
1.159     semarie   268:                mode = sb.st_mode & ACCESSPERMS;
1.93      nicm      269:                if (n != 0) {
                    270:                        if (mode & S_IRUSR)
                    271:                                mode |= S_IXUSR;
                    272:                        if (mode & S_IRGRP)
                    273:                                mode |= S_IXGRP;
                    274:                        if (mode & S_IROTH)
                    275:                                mode |= S_IXOTH;
                    276:                } else
                    277:                        mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
                    278:                chmod(socket_path, mode);
1.66      nicm      279:        }
                    280: }
                    281:
                    282: /* Callback for server socket. */
1.160     nicm      283: static void
1.150     nicm      284: server_accept(int fd, short events, __unused void *data)
1.66      nicm      285: {
                    286:        struct sockaddr_storage sa;
                    287:        socklen_t               slen = sizeof sa;
                    288:        int                     newfd;
                    289:
1.104     nicm      290:        server_add_accept(0);
1.66      nicm      291:        if (!(events & EV_READ))
                    292:                return;
                    293:
                    294:        newfd = accept(fd, (struct sockaddr *) &sa, &slen);
                    295:        if (newfd == -1) {
                    296:                if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
                    297:                        return;
1.104     nicm      298:                if (errno == ENFILE || errno == EMFILE) {
                    299:                        /* Delete and don't try again for 1 second. */
                    300:                        server_add_accept(1);
                    301:                        return;
                    302:                }
1.66      nicm      303:                fatal("accept failed");
                    304:        }
1.141     nicm      305:        if (server_exit) {
1.66      nicm      306:                close(newfd);
                    307:                return;
1.42      nicm      308:        }
1.66      nicm      309:        server_client_create(newfd);
                    310: }
                    311:
1.104     nicm      312: /*
                    313:  * Add accept event. If timeout is nonzero, add as a timeout instead of a read
                    314:  * event - used to backoff when running out of file descriptors.
                    315:  */
                    316: void
                    317: server_add_accept(int timeout)
                    318: {
                    319:        struct timeval tv = { timeout, 0 };
                    320:
                    321:        if (event_initialized(&server_ev_accept))
                    322:                event_del(&server_ev_accept);
                    323:
                    324:        if (timeout == 0) {
1.144     nicm      325:                event_set(&server_ev_accept, server_fd, EV_READ, server_accept,
                    326:                    NULL);
1.104     nicm      327:                event_add(&server_ev_accept, NULL);
                    328:        } else {
1.144     nicm      329:                event_set(&server_ev_accept, server_fd, EV_TIMEOUT,
                    330:                    server_accept, NULL);
1.104     nicm      331:                event_add(&server_ev_accept, &tv);
                    332:        }
                    333: }
                    334:
1.66      nicm      335: /* Signal handler. */
1.160     nicm      336: static void
1.144     nicm      337: server_signal(int sig)
1.66      nicm      338: {
1.119     nicm      339:        int     fd;
1.120     nicm      340:
1.66      nicm      341:        switch (sig) {
                    342:        case SIGTERM:
1.141     nicm      343:                server_exit = 1;
                    344:                server_send_exit();
1.66      nicm      345:                break;
                    346:        case SIGCHLD:
                    347:                server_child_signal();
                    348:                break;
                    349:        case SIGUSR1:
                    350:                event_del(&server_ev_accept);
1.119     nicm      351:                fd = server_create_socket();
                    352:                if (fd != -1) {
                    353:                        close(server_fd);
                    354:                        server_fd = fd;
                    355:                        server_update_socket();
                    356:                }
1.104     nicm      357:                server_add_accept(0);
1.66      nicm      358:                break;
1.1       nicm      359:        }
                    360: }
                    361:
                    362: /* Handle SIGCHLD. */
1.160     nicm      363: static void
1.1       nicm      364: server_child_signal(void)
                    365: {
1.66      nicm      366:        int      status;
                    367:        pid_t    pid;
1.1       nicm      368:
                    369:        for (;;) {
                    370:                switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
                    371:                case -1:
                    372:                        if (errno == ECHILD)
                    373:                                return;
1.39      nicm      374:                        fatal("waitpid failed");
1.1       nicm      375:                case 0:
                    376:                        return;
                    377:                }
1.66      nicm      378:                if (WIFSTOPPED(status))
                    379:                        server_child_stopped(pid, status);
1.79      nicm      380:                else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66      nicm      381:                        server_child_exited(pid, status);
                    382:        }
                    383: }
                    384:
                    385: /* Handle exited children. */
1.160     nicm      386: static void
1.66      nicm      387: server_child_exited(pid_t pid, int status)
                    388: {
1.121     nicm      389:        struct window           *w, *w1;
1.66      nicm      390:        struct window_pane      *wp;
                    391:        struct job              *job;
                    392:
1.121     nicm      393:        RB_FOREACH_SAFE(w, windows, &windows, w1) {
1.66      nicm      394:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    395:                        if (wp->pid == pid) {
1.118     nicm      396:                                wp->status = status;
1.156     nicm      397:                                server_destroy_pane(wp, 1);
1.77      nicm      398:                                break;
1.53      nicm      399:                        }
                    400:                }
1.80      nicm      401:        }
1.1       nicm      402:
1.101     nicm      403:        LIST_FOREACH(job, &all_jobs, lentry) {
1.66      nicm      404:                if (pid == job->pid) {
1.67      nicm      405:                        job_died(job, status);  /* might free job */
                    406:                        break;
1.1       nicm      407:                }
1.53      nicm      408:        }
                    409: }
                    410:
1.66      nicm      411: /* Handle stopped children. */
1.160     nicm      412: static void
1.66      nicm      413: server_child_stopped(pid_t pid, int status)
1.31      nicm      414: {
1.66      nicm      415:        struct window           *w;
                    416:        struct window_pane      *wp;
1.31      nicm      417:
1.66      nicm      418:        if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
                    419:                return;
1.31      nicm      420:
1.121     nicm      421:        RB_FOREACH(w, windows, &windows) {
1.66      nicm      422:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    423:                        if (wp->pid == pid) {
                    424:                                if (killpg(pid, SIGCONT) != 0)
                    425:                                        kill(pid, SIGCONT);
                    426:                        }
1.1       nicm      427:                }
                    428:        }
                    429: }