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

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