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

1.100   ! nicm        1: /* $OpenBSD: server.c,v 1.99 2011/01/01 01:12:09 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 <syslog.h>
                     35: #include <termios.h>
                     36: #include <time.h>
                     37: #include <unistd.h>
                     38:
                     39: #include "tmux.h"
                     40:
                     41: /*
                     42:  * Main server functions.
                     43:  */
                     44:
                     45: /* Client list. */
                     46: struct clients  clients;
1.31      nicm       47: struct clients  dead_clients;
1.1       nicm       48:
1.66      nicm       49: int             server_fd;
                     50: int             server_shutdown;
                     51: struct event    server_ev_accept;
                     52: struct event    server_ev_second;
1.45      nicm       53:
1.98      nicm       54: struct paste_stack global_buffers;
                     55:
1.1       nicm       56: int             server_create_socket(void);
1.66      nicm       57: void            server_loop(void);
1.42      nicm       58: int             server_should_shutdown(void);
1.66      nicm       59: void            server_send_shutdown(void);
                     60: void            server_clean_dead(void);
                     61: void            server_accept_callback(int, short, void *);
                     62: void            server_signal_callback(int, short, void *);
1.1       nicm       63: void            server_child_signal(void);
1.66      nicm       64: void            server_child_exited(pid_t, int);
                     65: void            server_child_stopped(pid_t, int);
                     66: void            server_second_callback(int, short, void *);
1.46      nicm       67: void            server_lock_server(void);
                     68: void            server_lock_sessions(void);
1.45      nicm       69:
1.60      nicm       70: /* Create server socket. */
                     71: int
                     72: server_create_socket(void)
1.1       nicm       73: {
1.60      nicm       74:        struct sockaddr_un      sa;
                     75:        size_t                  size;
                     76:        mode_t                  mask;
1.100   ! nicm       77:        int                     fd;
1.60      nicm       78:
                     79:        memset(&sa, 0, sizeof sa);
                     80:        sa.sun_family = AF_UNIX;
                     81:        size = strlcpy(sa.sun_path, socket_path, sizeof sa.sun_path);
                     82:        if (size >= sizeof sa.sun_path) {
                     83:                errno = ENAMETOOLONG;
                     84:                fatal("socket failed");
                     85:        }
                     86:        unlink(sa.sun_path);
                     87:
                     88:        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
                     89:                fatal("socket failed");
                     90:
1.89      nicm       91:        mask = umask(S_IXUSR|S_IXGRP|S_IRWXO);
1.60      nicm       92:        if (bind(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1)
                     93:                fatal("bind failed");
                     94:        umask(mask);
                     95:
                     96:        if (listen(fd, 16) == -1)
                     97:                fatal("listen failed");
1.100   ! nicm       98:        setblocking(fd, 0);
1.1       nicm       99:
1.75      nicm      100:        server_update_socket();
                    101:
1.60      nicm      102:        return (fd);
                    103: }
                    104:
1.1       nicm      105: /* Fork new server. */
                    106: int
1.96      nicm      107: server_start(void)
1.1       nicm      108: {
1.82      nicm      109:        struct window_pane      *wp;
1.84      nicm      110:        int                      pair[2];
1.96      nicm      111:        char                    *cause;
1.82      nicm      112:        struct timeval           tv;
                    113:        u_int                    i;
1.1       nicm      114:
                    115:        /* The first client is special and gets a socketpair; create it. */
                    116:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pair) != 0)
                    117:                fatal("socketpair failed");
                    118:
                    119:        switch (fork()) {
                    120:        case -1:
                    121:                fatal("fork failed");
                    122:        case 0:
                    123:                break;
                    124:        default:
                    125:                close(pair[1]);
                    126:                return (pair[0]);
                    127:        }
                    128:        close(pair[0]);
                    129:
                    130:        /*
                    131:         * Must daemonise before loading configuration as the PID changes so
                    132:         * $TMUX would be wrong for sessions created in the config file.
                    133:         */
1.16      nicm      134:        if (daemon(1, 0) != 0)
1.1       nicm      135:                fatal("daemon failed");
                    136:
1.88      nicm      137:        /* event_init() was called in our parent, need to reinit. */
                    138:        if (event_reinit(ev_base) != 0)
                    139:                fatal("event_reinit failed");
1.92      nicm      140:        clear_signals(0);
1.88      nicm      141:
1.16      nicm      142:        logfile("server");
                    143:        log_debug("server started, pid %ld", (long) getpid());
                    144:
1.1       nicm      145:        ARRAY_INIT(&windows);
                    146:        ARRAY_INIT(&clients);
1.31      nicm      147:        ARRAY_INIT(&dead_clients);
1.97      nicm      148:        RB_INIT(&sessions);
                    149:        RB_INIT(&dead_sessions);
1.47      nicm      150:        TAILQ_INIT(&session_groups);
1.98      nicm      151:        ARRAY_INIT(&global_buffers);
1.15      nicm      152:        mode_key_init_trees();
1.1       nicm      153:        key_bindings_init();
                    154:        utf8_build();
                    155:
                    156:        start_time = time(NULL);
1.16      nicm      157:        log_debug("socket path %s", socket_path);
1.96      nicm      158:        setproctitle("server (%s)", socket_path);
1.16      nicm      159:
1.66      nicm      160:        server_fd = server_create_socket();
1.60      nicm      161:        server_client_create(pair[1]);
1.16      nicm      162:
1.82      nicm      163:        if (access(SYSTEM_CFG, R_OK) == 0)
1.83      nicm      164:                load_cfg(SYSTEM_CFG, NULL, &cfg_causes);
1.82      nicm      165:        else if (errno != ENOENT) {
1.83      nicm      166:                cfg_add_cause(
                    167:                    &cfg_causes, "%s: %s", strerror(errno), SYSTEM_CFG);
1.64      nicm      168:        }
1.82      nicm      169:        if (cfg_file != NULL)
1.83      nicm      170:                load_cfg(cfg_file, NULL, &cfg_causes);
1.82      nicm      171:
                    172:        /*
                    173:         * If there is a session already, put the current window and pane into
                    174:         * more mode.
                    175:         */
1.97      nicm      176:        if (!RB_EMPTY(&sessions) && !ARRAY_EMPTY(&cfg_causes)) {
                    177:                wp = RB_MIN(sessions, &sessions)->curw->window->active;
1.85      nicm      178:                window_pane_set_mode(wp, &window_copy_mode);
                    179:                window_copy_init_for_output(wp);
1.83      nicm      180:                for (i = 0; i < ARRAY_LENGTH(&cfg_causes); i++) {
                    181:                        cause = ARRAY_ITEM(&cfg_causes, i);
1.85      nicm      182:                        window_copy_add(wp, "%s", cause);
1.83      nicm      183:                        xfree(cause);
1.82      nicm      184:                }
1.83      nicm      185:                ARRAY_FREE(&cfg_causes);
1.82      nicm      186:        }
                    187:        cfg_finished = 1;
1.66      nicm      188:
                    189:        event_set(&server_ev_accept,
                    190:            server_fd, EV_READ|EV_PERSIST, server_accept_callback, NULL);
                    191:        event_add(&server_ev_accept, NULL);
                    192:
                    193:        memset(&tv, 0, sizeof tv);
                    194:        tv.tv_sec = 1;
                    195:        evtimer_set(&server_ev_second, server_second_callback, NULL);
                    196:        evtimer_add(&server_ev_second, &tv);
                    197:
1.88      nicm      198:        set_signals(server_signal_callback);
1.66      nicm      199:        server_loop();
                    200:        exit(0);
1.1       nicm      201: }
                    202:
                    203: /* Main server loop. */
1.66      nicm      204: void
                    205: server_loop(void)
1.1       nicm      206: {
1.66      nicm      207:        while (!server_should_shutdown()) {
                    208:                event_loop(EVLOOP_ONCE);
1.1       nicm      209:
1.60      nicm      210:                server_window_loop();
                    211:                server_client_loop();
1.1       nicm      212:
1.8       nicm      213:                key_bindings_clean();
1.66      nicm      214:                server_clean_dead();
1.80      nicm      215:        }
1.66      nicm      216: }
1.31      nicm      217:
1.94      nicm      218: /* Check if the server should be shutting down (no more clients or sessions). */
1.66      nicm      219: int
                    220: server_should_shutdown(void)
                    221: {
                    222:        u_int   i;
1.1       nicm      223:
1.94      nicm      224:        if (!options_get_number(&global_options, "exit-unattached")) {
1.97      nicm      225:                if (!RB_EMPTY(&sessions))
                    226:                        return (0);
1.1       nicm      227:        }
                    228:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    229:                if (ARRAY_ITEM(&clients, i) != NULL)
1.66      nicm      230:                        return (0);
1.1       nicm      231:        }
1.66      nicm      232:        return (1);
1.1       nicm      233: }
                    234:
1.66      nicm      235: /* Shutdown the server by killing all clients and windows. */
1.1       nicm      236: void
1.66      nicm      237: server_send_shutdown(void)
1.1       nicm      238: {
1.66      nicm      239:        struct client   *c;
1.97      nicm      240:        struct session  *s, *next_s;
1.66      nicm      241:        u_int            i;
1.1       nicm      242:
1.42      nicm      243:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    244:                c = ARRAY_ITEM(&clients, i);
                    245:                if (c != NULL) {
                    246:                        if (c->flags & (CLIENT_BAD|CLIENT_SUSPENDED))
1.60      nicm      247:                                server_client_lost(c);
1.42      nicm      248:                        else
                    249:                                server_write_client(c, MSG_SHUTDOWN, NULL, 0);
1.66      nicm      250:                        c->session = NULL;
1.42      nicm      251:                }
                    252:        }
                    253:
1.97      nicm      254:        s = RB_MIN(sessions, &sessions);
                    255:        while (s != NULL) {
                    256:                next_s = RB_NEXT(sessions, &sessions, s);
                    257:                session_destroy(s);
                    258:                s = next_s;
1.1       nicm      259:        }
1.42      nicm      260: }
                    261:
1.66      nicm      262: /* Free dead, unreferenced clients and sessions. */
                    263: void
                    264: server_clean_dead(void)
                    265: {
1.97      nicm      266:        struct session  *s, *next_s;
1.66      nicm      267:        struct client   *c;
                    268:        u_int            i;
                    269:
1.97      nicm      270:        s = RB_MIN(sessions, &dead_sessions);
                    271:        while (s != NULL) {
                    272:                next_s = RB_NEXT(sessions, &dead_sessions, s);
                    273:                if (s->references == 0) {
                    274:                        RB_REMOVE(sessions, &dead_sessions, s);
                    275:                        xfree(s->name);
                    276:                        xfree(s);
                    277:                }
                    278:                s = next_s;
1.66      nicm      279:        }
                    280:
                    281:        for (i = 0; i < ARRAY_LENGTH(&dead_clients); i++) {
                    282:                c = ARRAY_ITEM(&dead_clients, i);
                    283:                if (c == NULL || c->references != 0)
                    284:                        continue;
                    285:                ARRAY_SET(&dead_clients, i, NULL);
                    286:                xfree(c);
                    287:        }
                    288: }
                    289:
                    290: /* Update socket execute permissions based on whether sessions are attached. */
1.75      nicm      291: void
1.66      nicm      292: server_update_socket(void)
1.42      nicm      293: {
1.66      nicm      294:        struct session  *s;
                    295:        static int       last = -1;
1.93      nicm      296:        int              n, mode;
                    297:        struct stat      sb;
1.1       nicm      298:
1.66      nicm      299:        n = 0;
1.97      nicm      300:        RB_FOREACH(s, sessions, &sessions) {
                    301:                if (!(s->flags & SESSION_UNATTACHED)) {
1.66      nicm      302:                        n++;
                    303:                        break;
                    304:                }
                    305:        }
                    306:
                    307:        if (n != last) {
                    308:                last = n;
1.93      nicm      309:
                    310:                if (stat(socket_path, &sb) != 0)
                    311:                        return;
                    312:                mode = sb.st_mode;
                    313:                if (n != 0) {
                    314:                        if (mode & S_IRUSR)
                    315:                                mode |= S_IXUSR;
                    316:                        if (mode & S_IRGRP)
                    317:                                mode |= S_IXGRP;
                    318:                        if (mode & S_IROTH)
                    319:                                mode |= S_IXOTH;
                    320:                } else
                    321:                        mode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
                    322:                chmod(socket_path, mode);
1.66      nicm      323:        }
                    324: }
                    325:
                    326: /* Callback for server socket. */
1.78      nicm      327: /* ARGSUSED */
1.66      nicm      328: void
                    329: server_accept_callback(int fd, short events, unused void *data)
                    330: {
                    331:        struct sockaddr_storage sa;
                    332:        socklen_t               slen = sizeof sa;
                    333:        int                     newfd;
                    334:
                    335:        if (!(events & EV_READ))
                    336:                return;
                    337:
                    338:        newfd = accept(fd, (struct sockaddr *) &sa, &slen);
                    339:        if (newfd == -1) {
                    340:                if (errno == EAGAIN || errno == EINTR || errno == ECONNABORTED)
                    341:                        return;
                    342:                fatal("accept failed");
                    343:        }
                    344:        if (server_shutdown) {
                    345:                close(newfd);
                    346:                return;
1.42      nicm      347:        }
1.66      nicm      348:        server_client_create(newfd);
                    349: }
                    350:
                    351: /* Signal handler. */
1.78      nicm      352: /* ARGSUSED */
1.66      nicm      353: void
                    354: server_signal_callback(int sig, unused short events, unused void *data)
                    355: {
                    356:        switch (sig) {
                    357:        case SIGTERM:
                    358:                server_shutdown = 1;
                    359:                server_send_shutdown();
                    360:                break;
                    361:        case SIGCHLD:
                    362:                server_child_signal();
                    363:                break;
                    364:        case SIGUSR1:
                    365:                event_del(&server_ev_accept);
                    366:                close(server_fd);
                    367:                server_fd = server_create_socket();
                    368:                event_set(&server_ev_accept, server_fd,
                    369:                    EV_READ|EV_PERSIST, server_accept_callback, NULL);
                    370:                event_add(&server_ev_accept, NULL);
                    371:                break;
1.1       nicm      372:        }
                    373: }
                    374:
                    375: /* Handle SIGCHLD. */
                    376: void
                    377: server_child_signal(void)
                    378: {
1.66      nicm      379:        int      status;
                    380:        pid_t    pid;
1.1       nicm      381:
                    382:        for (;;) {
                    383:                switch (pid = waitpid(WAIT_ANY, &status, WNOHANG|WUNTRACED)) {
                    384:                case -1:
                    385:                        if (errno == ECHILD)
                    386:                                return;
1.39      nicm      387:                        fatal("waitpid failed");
1.1       nicm      388:                case 0:
                    389:                        return;
                    390:                }
1.66      nicm      391:                if (WIFSTOPPED(status))
                    392:                        server_child_stopped(pid, status);
1.79      nicm      393:                else if (WIFEXITED(status) || WIFSIGNALED(status))
1.66      nicm      394:                        server_child_exited(pid, status);
                    395:        }
                    396: }
                    397:
                    398: /* Handle exited children. */
                    399: void
                    400: server_child_exited(pid_t pid, int status)
                    401: {
                    402:        struct window           *w;
                    403:        struct window_pane      *wp;
                    404:        struct job              *job;
                    405:        u_int                    i;
                    406:
                    407:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    408:                if ((w = ARRAY_ITEM(&windows, i)) == NULL)
                    409:                        continue;
                    410:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    411:                        if (wp->pid == pid) {
1.77      nicm      412:                                server_destroy_pane(wp);
                    413:                                break;
1.53      nicm      414:                        }
                    415:                }
1.80      nicm      416:        }
1.1       nicm      417:
1.66      nicm      418:        SLIST_FOREACH(job, &all_jobs, lentry) {
                    419:                if (pid == job->pid) {
1.67      nicm      420:                        job_died(job, status);  /* might free job */
                    421:                        break;
1.1       nicm      422:                }
1.53      nicm      423:        }
                    424: }
                    425:
1.66      nicm      426: /* Handle stopped children. */
1.31      nicm      427: void
1.66      nicm      428: server_child_stopped(pid_t pid, int status)
1.31      nicm      429: {
1.66      nicm      430:        struct window           *w;
                    431:        struct window_pane      *wp;
                    432:        u_int                    i;
1.31      nicm      433:
1.66      nicm      434:        if (WSTOPSIG(status) == SIGTTIN || WSTOPSIG(status) == SIGTTOU)
                    435:                return;
1.31      nicm      436:
1.66      nicm      437:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    438:                if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1.31      nicm      439:                        continue;
1.66      nicm      440:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    441:                        if (wp->pid == pid) {
                    442:                                if (killpg(pid, SIGCONT) != 0)
                    443:                                        kill(pid, SIGCONT);
                    444:                        }
                    445:                }
1.31      nicm      446:        }
1.1       nicm      447: }
                    448:
1.66      nicm      449: /* Handle once-per-second timer events. */
1.78      nicm      450: /* ARGSUSED */
1.1       nicm      451: void
1.66      nicm      452: server_second_callback(unused int fd, unused short events, unused void *arg)
1.1       nicm      453: {
1.60      nicm      454:        struct window           *w;
                    455:        struct window_pane      *wp;
1.66      nicm      456:        struct timeval           tv;
1.35      nicm      457:        u_int                    i;
1.1       nicm      458:
1.60      nicm      459:        if (options_get_number(&global_s_options, "lock-server"))
                    460:                server_lock_server();
                    461:        else
                    462:                server_lock_sessions();
1.1       nicm      463:
1.60      nicm      464:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    465:                w = ARRAY_ITEM(&windows, i);
                    466:                if (w == NULL)
1.1       nicm      467:                        continue;
                    468:
1.60      nicm      469:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    470:                        if (wp->mode != NULL && wp->mode->timer != NULL)
                    471:                                wp->mode->timer(wp);
1.1       nicm      472:                }
                    473:        }
1.72      nicm      474:
                    475:        server_client_status_timer();
1.66      nicm      476:
                    477:        evtimer_del(&server_ev_second);
                    478:        memset(&tv, 0, sizeof tv);
                    479:        tv.tv_sec = 1;
                    480:        evtimer_add(&server_ev_second, &tv);
1.1       nicm      481: }
                    482:
1.46      nicm      483: /* Lock the server if ALL sessions have hit the time limit. */
                    484: void
                    485: server_lock_server(void)
                    486: {
                    487:        struct session  *s;
                    488:        int              timeout;
                    489:        time_t           t;
                    490:
                    491:        t = time(NULL);
1.97      nicm      492:        RB_FOREACH(s, sessions, &sessions) {
1.99      nicm      493:                if (s->flags & SESSION_UNATTACHED)
1.59      nicm      494:                        continue;
1.46      nicm      495:                timeout = options_get_number(&s->options, "lock-after-time");
1.65      nicm      496:                if (timeout <= 0 || t <= s->activity_time.tv_sec + timeout)
1.46      nicm      497:                        return; /* not timed out */
                    498:        }
                    499:
                    500:        server_lock();
                    501:        recalculate_sizes();
                    502: }
                    503:
                    504: /* Lock any sessions which have timed out. */
                    505: void
                    506: server_lock_sessions(void)
                    507: {
1.80      nicm      508:        struct session  *s;
1.46      nicm      509:        int              timeout;
1.62      deraadt   510:        time_t           t;
1.46      nicm      511:
1.62      deraadt   512:        t = time(NULL);
1.97      nicm      513:        RB_FOREACH(s, sessions, &sessions) {
1.99      nicm      514:                if (s->flags & SESSION_UNATTACHED)
1.59      nicm      515:                        continue;
1.46      nicm      516:                timeout = options_get_number(&s->options, "lock-after-time");
1.65      nicm      517:                if (timeout > 0 && t > s->activity_time.tv_sec + timeout) {
1.46      nicm      518:                        server_lock_session(s);
                    519:                        recalculate_sizes();
1.1       nicm      520:                }
                    521:        }
                    522: }