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

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