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

1.144   ! nicm        1: /* $OpenBSD: server.c,v 1.143 2015/10/23 16:07:29 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:
                    176:        if (pledge("stdio rpath wpath cpath fattr unix recvfd proc exec tty "
                    177:            "ps", NULL) != 0)
                    178:                fatal("pledge failed");
1.1       nicm      179:
1.121     nicm      180:        RB_INIT(&windows);
1.102     nicm      181:        RB_INIT(&all_window_panes);
1.122     nicm      182:        TAILQ_INIT(&clients);
1.97      nicm      183:        RB_INIT(&sessions);
1.47      nicm      184:        TAILQ_INIT(&session_groups);
1.15      nicm      185:        mode_key_init_trees();
1.1       nicm      186:        key_bindings_init();
                    187:        utf8_build();
                    188:
                    189:        start_time = time(NULL);
1.16      nicm      190:
1.66      nicm      191:        server_fd = server_create_socket();
1.119     nicm      192:        if (server_fd == -1)
                    193:                fatal("couldn't create socket");
                    194:        server_update_socket();
1.60      nicm      195:        server_client_create(pair[1]);
1.103     nicm      196:
                    197:        unlink(lockfile);
1.105     nicm      198:        free(lockfile);
1.103     nicm      199:        close(lockfd);
1.16      nicm      200:
1.139     nicm      201:        start_cfg();
                    202:
1.129     nicm      203:        status_prompt_load_history();
1.66      nicm      204:
1.104     nicm      205:        server_add_accept(0);
1.66      nicm      206:
1.144   ! nicm      207:        proc_loop(server_proc, server_loop);
1.129     nicm      208:        status_prompt_save_history();
1.66      nicm      209:        exit(0);
1.1       nicm      210: }
                    211:
1.144   ! nicm      212: /* Server loop callback. */
        !           213: int
1.66      nicm      214: server_loop(void)
1.1       nicm      215: {
1.144   ! nicm      216:        struct client   *c;
1.31      nicm      217:
1.144   ! nicm      218:        server_client_loop();
1.1       nicm      219:
1.94      nicm      220:        if (!options_get_number(&global_options, "exit-unattached")) {
1.97      nicm      221:                if (!RB_EMPTY(&sessions))
                    222:                        return (0);
1.1       nicm      223:        }
1.116     nicm      224:
1.122     nicm      225:        TAILQ_FOREACH(c, &clients, entry) {
                    226:                if (c->session != NULL)
1.116     nicm      227:                        return (0);
                    228:        }
                    229:
                    230:        /*
                    231:         * No attached clients therefore want to exit - flush any waiting
                    232:         * clients but don't actually exit until they've gone.
                    233:         */
                    234:        cmd_wait_for_flush();
1.122     nicm      235:        if (!TAILQ_EMPTY(&clients))
                    236:                return (0);
1.116     nicm      237:
1.66      nicm      238:        return (1);
1.1       nicm      239: }
                    240:
1.141     nicm      241: /* Exit the server by killing all clients and windows. */
1.1       nicm      242: void
1.141     nicm      243: server_send_exit(void)
1.1       nicm      244: {
1.122     nicm      245:        struct client   *c, *c1;
                    246:        struct session  *s, *s1;
1.116     nicm      247:
                    248:        cmd_wait_for_flush();
1.1       nicm      249:
1.122     nicm      250:        TAILQ_FOREACH_SAFE(c, &clients, entry, c1) {
1.144   ! nicm      251:                if (c->flags & CLIENT_SUSPENDED)
1.122     nicm      252:                        server_client_lost(c);
                    253:                else
1.144   ! nicm      254:                        proc_send(c->peer, MSG_SHUTDOWN, -1, NULL, 0);
1.122     nicm      255:                c->session = NULL;
1.42      nicm      256:        }
                    257:
1.122     nicm      258:        RB_FOREACH_SAFE(s, sessions, &sessions, s1)
1.97      nicm      259:                session_destroy(s);
1.66      nicm      260: }
                    261:
                    262: /* Update socket execute permissions based on whether sessions are attached. */
1.75      nicm      263: void
1.66      nicm      264: server_update_socket(void)
1.42      nicm      265: {
1.66      nicm      266:        struct session  *s;
                    267:        static int       last = -1;
1.93      nicm      268:        int              n, mode;
                    269:        struct stat      sb;
1.1       nicm      270:
1.66      nicm      271:        n = 0;
1.97      nicm      272:        RB_FOREACH(s, sessions, &sessions) {
                    273:                if (!(s->flags & SESSION_UNATTACHED)) {
1.66      nicm      274:                        n++;
                    275:                        break;
                    276:                }
                    277:        }
                    278:
                    279:        if (n != last) {
                    280:                last = n;
1.93      nicm      281:
                    282:                if (stat(socket_path, &sb) != 0)
                    283:                        return;
                    284:                mode = sb.st_mode;
                    285:                if (n != 0) {
                    286:                        if (mode & S_IRUSR)
                    287:                                mode |= S_IXUSR;
                    288:                        if (mode & S_IRGRP)
                    289:                                mode |= S_IXGRP;
                    290:                        if (mode & S_IROTH)
                    291:                                mode |= S_IXOTH;
                    292:                } else
                    293:                        mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
                    294:                chmod(socket_path, mode);
1.66      nicm      295:        }
                    296: }
                    297:
                    298: /* Callback for server socket. */
                    299: void
1.144   ! nicm      300: server_accept(int fd, short events, unused void *data)
1.66      nicm      301: {
                    302:        struct sockaddr_storage sa;
                    303:        socklen_t               slen = sizeof sa;
                    304:        int                     newfd;
                    305:
1.104     nicm      306:        server_add_accept(0);
1.66      nicm      307:        if (!(events & EV_READ))
                    308:                return;
                    309:
                    310:        newfd = accept(fd, (struct sockaddr *) &sa, &slen);
                    311:        if (newfd == -1) {
                    312:                if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
                    313:                        return;
1.104     nicm      314:                if (errno == ENFILE || errno == EMFILE) {
                    315:                        /* Delete and don't try again for 1 second. */
                    316:                        server_add_accept(1);
                    317:                        return;
                    318:                }
1.66      nicm      319:                fatal("accept failed");
                    320:        }
1.141     nicm      321:        if (server_exit) {
1.66      nicm      322:                close(newfd);
                    323:                return;
1.42      nicm      324:        }
1.66      nicm      325:        server_client_create(newfd);
                    326: }
                    327:
1.104     nicm      328: /*
                    329:  * Add accept event. If timeout is nonzero, add as a timeout instead of a read
                    330:  * event - used to backoff when running out of file descriptors.
                    331:  */
                    332: void
                    333: server_add_accept(int timeout)
                    334: {
                    335:        struct timeval tv = { timeout, 0 };
                    336:
                    337:        if (event_initialized(&server_ev_accept))
                    338:                event_del(&server_ev_accept);
                    339:
                    340:        if (timeout == 0) {
1.144   ! nicm      341:                event_set(&server_ev_accept, server_fd, EV_READ, server_accept,
        !           342:                    NULL);
1.104     nicm      343:                event_add(&server_ev_accept, NULL);
                    344:        } else {
1.144   ! nicm      345:                event_set(&server_ev_accept, server_fd, EV_TIMEOUT,
        !           346:                    server_accept, NULL);
1.104     nicm      347:                event_add(&server_ev_accept, &tv);
                    348:        }
                    349: }
                    350:
1.66      nicm      351: /* Signal handler. */
                    352: void
1.144   ! nicm      353: server_signal(int sig)
1.66      nicm      354: {
1.119     nicm      355:        int     fd;
1.120     nicm      356:
1.66      nicm      357:        switch (sig) {
                    358:        case SIGTERM:
1.141     nicm      359:                server_exit = 1;
                    360:                server_send_exit();
1.66      nicm      361:                break;
                    362:        case SIGCHLD:
                    363:                server_child_signal();
                    364:                break;
                    365:        case SIGUSR1:
                    366:                event_del(&server_ev_accept);
1.119     nicm      367:                fd = server_create_socket();
                    368:                if (fd != -1) {
                    369:                        close(server_fd);
                    370:                        server_fd = fd;
                    371:                        server_update_socket();
                    372:                }
1.104     nicm      373:                server_add_accept(0);
1.66      nicm      374:                break;
1.1       nicm      375:        }
                    376: }
                    377:
                    378: /* Handle SIGCHLD. */
                    379: void
                    380: server_child_signal(void)
                    381: {
1.66      nicm      382:        int      status;
                    383:        pid_t    pid;
1.1       nicm      384:
                    385:        for (;;) {
                    386:                switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
                    387:                case -1:
                    388:                        if (errno == ECHILD)
                    389:                                return;
1.39      nicm      390:                        fatal("waitpid failed");
1.1       nicm      391:                case 0:
                    392:                        return;
                    393:                }
1.66      nicm      394:                if (WIFSTOPPED(status))
                    395:                        server_child_stopped(pid, status);
1.79      nicm      396:                else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66      nicm      397:                        server_child_exited(pid, status);
                    398:        }
                    399: }
                    400:
                    401: /* Handle exited children. */
                    402: void
                    403: server_child_exited(pid_t pid, int status)
                    404: {
1.121     nicm      405:        struct window           *w, *w1;
1.66      nicm      406:        struct window_pane      *wp;
                    407:        struct job              *job;
                    408:
1.121     nicm      409:        RB_FOREACH_SAFE(w, windows, &windows, w1) {
1.66      nicm      410:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    411:                        if (wp->pid == pid) {
1.118     nicm      412:                                wp->status = status;
1.77      nicm      413:                                server_destroy_pane(wp);
                    414:                                break;
1.53      nicm      415:                        }
                    416:                }
1.80      nicm      417:        }
1.1       nicm      418:
1.101     nicm      419:        LIST_FOREACH(job, &all_jobs, lentry) {
1.66      nicm      420:                if (pid == job->pid) {
1.67      nicm      421:                        job_died(job, status);  /* might free job */
                    422:                        break;
1.1       nicm      423:                }
1.53      nicm      424:        }
                    425: }
                    426:
1.66      nicm      427: /* Handle stopped children. */
1.31      nicm      428: void
1.66      nicm      429: server_child_stopped(pid_t pid, int status)
1.31      nicm      430: {
1.66      nicm      431:        struct window           *w;
                    432:        struct window_pane      *wp;
1.31      nicm      433:
1.66      nicm      434:        if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
                    435:                return;
1.31      nicm      436:
1.121     nicm      437:        RB_FOREACH(w, windows, &windows) {
1.66      nicm      438:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    439:                        if (wp->pid == pid) {
                    440:                                if (killpg(pid, SIGCONT) != 0)
                    441:                                        kill(pid, SIGCONT);
                    442:                        }
1.1       nicm      443:                }
                    444:        }
                    445: }