[BACK]Return to client.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/client.c, Revision 1.49

1.49    ! nicm        1: /* $OpenBSD: client.c,v 1.48 2011/03/03 08:51:47 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/socket.h>
                     21: #include <sys/stat.h>
                     22: #include <sys/un.h>
                     23: #include <sys/wait.h>
                     24:
                     25: #include <errno.h>
1.30      nicm       26: #include <event.h>
1.1       nicm       27: #include <fcntl.h>
                     28: #include <pwd.h>
                     29: #include <stdlib.h>
                     30: #include <string.h>
                     31: #include <unistd.h>
                     32:
                     33: #include "tmux.h"
                     34:
1.25      nicm       35: struct imsgbuf client_ibuf;
1.30      nicm       36: struct event   client_event;
1.25      nicm       37: const char     *client_exitmsg;
1.32      nicm       38: int            client_exitval;
1.48      nicm       39: enum msgtype   client_exittype;
1.46      nicm       40: int            client_attached;
1.1       nicm       41:
1.49    ! nicm       42: int            client_get_lock(char *);
1.46      nicm       43: int            client_connect(char *, int);
1.30      nicm       44: void           client_send_identify(int);
                     45: void           client_send_environ(void);
                     46: void           client_write_server(enum msgtype, void *, size_t);
1.31      nicm       47: void           client_update_event(void);
1.30      nicm       48: void           client_signal(int, short, void *);
                     49: void           client_callback(int, short, void *);
1.46      nicm       50: int            client_dispatch_attached(void);
                     51: int            client_dispatch_wait(void *);
1.25      nicm       52:
1.49    ! nicm       53: /*
        !            54:  * Get server create lock. If already held then server start is happening in
        !            55:  * another client, so block until the lock is released and return -1 to
        !            56:  * retry. Ignore other errors - just continue and start the server without the
        !            57:  * lock.
        !            58:  */
        !            59: int
        !            60: client_get_lock(char *lockfile)
        !            61: {
        !            62:        int lockfd;
        !            63:
        !            64:        if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1)
        !            65:                fatal("open failed");
        !            66:
        !            67:        if (flock(lockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
        !            68:                while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
        !            69:                        /* nothing */;
        !            70:                close(lockfd);
        !            71:                return (-1);
        !            72:        }
        !            73:
        !            74:        return (lockfd);
        !            75: }
        !            76:
1.46      nicm       77: /* Connect client to server. */
                     78: int
                     79: client_connect(char *path, int start_server)
1.1       nicm       80: {
1.26      nicm       81:        struct sockaddr_un      sa;
                     82:        size_t                  size;
1.49    ! nicm       83:        int                     fd, lockfd;
        !            84:        char                   *lockfile;
1.1       nicm       85:
                     86:        memset(&sa, 0, sizeof sa);
                     87:        sa.sun_family = AF_UNIX;
                     88:        size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
                     89:        if (size >= sizeof sa.sun_path) {
                     90:                errno = ENAMETOOLONG;
1.46      nicm       91:                return (-1);
1.1       nicm       92:        }
                     93:
1.49    ! nicm       94: retry:
1.10      nicm       95:        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.18      nicm       96:                fatal("socket failed");
1.1       nicm       97:
1.10      nicm       98:        if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
1.49    ! nicm       99:                if (errno != ECONNREFUSED && errno != ENOENT)
        !           100:                        goto failed;
1.46      nicm      101:                if (!start_server)
                    102:                        goto failed;
1.49    ! nicm      103:                close(fd);
        !           104:
        !           105:                xasprintf(&lockfile, "%s.lock", path);
        !           106:                if ((lockfd = client_get_lock(lockfile)) == -1)
        !           107:                        goto retry;
        !           108:                if (unlink(path) != 0 && errno != ENOENT)
        !           109:                        return (-1);
        !           110:                fd = server_start(lockfd, lockfile);
        !           111:                xfree(lockfile);
        !           112:                close(lockfd);
1.1       nicm      113:        }
                    114:
1.47      nicm      115:        setblocking(fd, 0);
1.46      nicm      116:        return (fd);
                    117:
                    118: failed:
                    119:        close(fd);
                    120:        return (-1);
                    121: }
                    122:
                    123: /* Client main loop. */
                    124: int
                    125: client_main(int argc, char **argv, int flags)
                    126: {
                    127:        struct cmd              *cmd;
                    128:        struct cmd_list         *cmdlist;
                    129:        struct msg_command_data  cmddata;
                    130:        int                      cmdflags, fd;
1.48      nicm      131:        pid_t                    ppid;
1.46      nicm      132:        enum msgtype             msg;
                    133:        char                    *cause;
                    134:
                    135:        /* Set up the initial command. */
                    136:        cmdflags = 0;
                    137:        if (shell_cmd != NULL) {
                    138:                msg = MSG_SHELL;
                    139:                cmdflags = CMD_STARTSERVER;
                    140:        } else if (argc == 0) {
                    141:                msg = MSG_COMMAND;
                    142:                cmdflags = CMD_STARTSERVER|CMD_SENDENVIRON|CMD_CANTNEST;
                    143:        } else {
                    144:                msg = MSG_COMMAND;
                    145:
                    146:                /*
                    147:                 * It sucks parsing the command string twice (in client and
                    148:                 * later in server) but it is necessary to get the start server
                    149:                 * flag.
                    150:                 */
                    151:                if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
                    152:                        log_warnx("%s", cause);
                    153:                        return (1);
                    154:                }
                    155:                cmdflags &= ~CMD_STARTSERVER;
                    156:                TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
                    157:                        if (cmd->entry->flags & CMD_STARTSERVER)
                    158:                                cmdflags |= CMD_STARTSERVER;
                    159:                        if (cmd->entry->flags & CMD_SENDENVIRON)
                    160:                                cmdflags |= CMD_SENDENVIRON;
                    161:                        if (cmd->entry->flags & CMD_CANTNEST)
                    162:                                cmdflags |= CMD_CANTNEST;
                    163:                }
                    164:                cmd_list_free(cmdlist);
                    165:        }
                    166:
                    167:        /*
                    168:         * Check if this could be a nested session, if the command can't nest:
                    169:         * if the socket path matches $TMUX, this is probably the same server.
                    170:         */
                    171:        if (shell_cmd == NULL && environ_path != NULL &&
                    172:            cmdflags & CMD_CANTNEST && strcmp(socket_path, environ_path) == 0) {
                    173:                log_warnx("sessions should be nested with care. "
                    174:                    "unset $TMUX to force.");
                    175:                return (1);
                    176:        }
                    177:
                    178:        /* Initialise the client socket and start the server. */
                    179:        fd = client_connect(socket_path, cmdflags & CMD_STARTSERVER);
                    180:        if (fd == -1) {
                    181:                log_warn("failed to connect to server");
                    182:                return (1);
                    183:        }
                    184:
                    185:        /* Set process title, log and signals now this is the client. */
                    186:        setproctitle("client (%s)", socket_path);
                    187:        logfile("client");
                    188:
                    189:        /* Create imsg. */
1.25      nicm      190:        imsg_init(&client_ibuf, fd);
1.46      nicm      191:        event_set(&client_event, fd, EV_READ, client_callback, shell_cmd);
                    192:
                    193:        /* Establish signal handlers. */
                    194:        set_signals(client_signal);
1.1       nicm      195:
1.46      nicm      196:        /* Send initial environment. */
1.11      nicm      197:        if (cmdflags & CMD_SENDENVIRON)
1.25      nicm      198:                client_send_environ();
1.42      nicm      199:        client_send_identify(flags);
1.1       nicm      200:
1.46      nicm      201:        /* Send first command. */
                    202:        if (msg == MSG_COMMAND) {
                    203:                /* Fill in command line arguments. */
                    204:                cmddata.pid = environ_pid;
                    205:                cmddata.idx = environ_idx;
                    206:
                    207:                /* Prepare command for server. */
                    208:                cmddata.argc = argc;
                    209:                if (cmd_pack_argv(
                    210:                    argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
                    211:                        log_warnx("command too long");
                    212:                        return (1);
                    213:                }
                    214:
                    215:                client_write_server(msg, &cmddata, sizeof cmddata);
                    216:        } else if (msg == MSG_SHELL)
                    217:                client_write_server(msg, NULL, 0);
1.1       nicm      218:
1.46      nicm      219:        /* Set the event and dispatch. */
                    220:        client_update_event();
                    221:        event_dispatch();
                    222:
                    223:        /* Print the exit message, if any, and exit. */
1.48      nicm      224:        if (client_attached) {
                    225:                if (client_exitmsg != NULL && !login_shell)
                    226:                        printf("[%s]\n", client_exitmsg);
                    227:
                    228:                ppid = getppid();
                    229:                if (client_exittype == MSG_DETACHKILL && ppid > 1)
                    230:                        kill(ppid, SIGHUP);
                    231:        }
1.46      nicm      232:        return (client_exitval);
1.1       nicm      233: }
                    234:
1.46      nicm      235: /* Send identify message to server with the file descriptors. */
1.11      nicm      236: void
1.26      nicm      237: client_send_identify(int flags)
                    238: {
                    239:        struct msg_identify_data        data;
                    240:        char                           *term;
                    241:        int                             fd;
                    242:
                    243:        data.flags = flags;
                    244:
                    245:        if (getcwd(data.cwd, sizeof data.cwd) == NULL)
                    246:                *data.cwd = '\0';
1.35      nicm      247:
1.26      nicm      248:        term = getenv("TERM");
                    249:        if (term == NULL ||
                    250:            strlcpy(data.term, term, sizeof data.term) >= sizeof data.term)
                    251:                *data.term = '\0';
                    252:
                    253:        if ((fd = dup(STDIN_FILENO)) == -1)
                    254:                fatal("dup failed");
                    255:        imsg_compose(&client_ibuf,
                    256:            MSG_IDENTIFY, PROTOCOL_VERSION, -1, fd, &data, sizeof data);
1.42      nicm      257:
                    258:        if ((fd = dup(STDOUT_FILENO)) == -1)
                    259:                fatal("dup failed");
1.46      nicm      260:        imsg_compose(&client_ibuf,
                    261:            MSG_STDOUT, PROTOCOL_VERSION, -1, fd, NULL, 0);
1.42      nicm      262:
                    263:        if ((fd = dup(STDERR_FILENO)) == -1)
                    264:                fatal("dup failed");
1.46      nicm      265:        imsg_compose(&client_ibuf,
                    266:            MSG_STDERR, PROTOCOL_VERSION, -1, fd, NULL, 0);
1.26      nicm      267: }
                    268:
1.46      nicm      269: /* Forward entire environment to server. */
1.26      nicm      270: void
1.25      nicm      271: client_send_environ(void)
1.11      nicm      272: {
1.26      nicm      273:        struct msg_environ_data data;
1.11      nicm      274:        char                  **var;
                    275:
1.35      nicm      276:        for (var = environ; *var != NULL; var++) {
1.11      nicm      277:                if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
                    278:                        continue;
1.25      nicm      279:                client_write_server(MSG_ENVIRON, &data, sizeof data);
1.11      nicm      280:        }
                    281: }
                    282:
1.46      nicm      283: /* Write a message to the server without a file descriptor. */
1.25      nicm      284: void
                    285: client_write_server(enum msgtype type, void *buf, size_t len)
                    286: {
1.35      nicm      287:        imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, -1, buf, len);
1.25      nicm      288: }
                    289:
1.46      nicm      290: /* Update client event based on whether it needs to read or read and write. */
1.31      nicm      291: void
                    292: client_update_event(void)
                    293: {
                    294:        short   events;
                    295:
                    296:        event_del(&client_event);
                    297:        events = EV_READ;
                    298:        if (client_ibuf.w.queued > 0)
                    299:                events |= EV_WRITE;
1.46      nicm      300:        event_set(
                    301:            &client_event, client_ibuf.fd, events, client_callback, shell_cmd);
1.31      nicm      302:        event_add(&client_event, NULL);
                    303: }
                    304:
1.46      nicm      305: /* Callback to handle signals in the client. */
1.34      nicm      306: /* ARGSUSED */
1.30      nicm      307: void
1.31      nicm      308: client_signal(int sig, unused short events, unused void *data)
1.30      nicm      309: {
1.46      nicm      310:        struct sigaction sigact;
                    311:        int              status;
1.30      nicm      312:
1.46      nicm      313:        if (!client_attached) {
                    314:                switch (sig) {
                    315:                case SIGCHLD:
                    316:                        waitpid(WAIT_ANY, &status, WNOHANG);
                    317:                        break;
                    318:                case SIGTERM:
                    319:                        event_loopexit(NULL);
                    320:                        break;
                    321:                }
                    322:        } else {
                    323:                switch (sig) {
                    324:                case SIGHUP:
                    325:                        client_exitmsg = "lost tty";
                    326:                        client_exitval = 1;
                    327:                        client_write_server(MSG_EXITING, NULL, 0);
                    328:                        break;
                    329:                case SIGTERM:
                    330:                        client_exitmsg = "terminated";
                    331:                        client_exitval = 1;
                    332:                        client_write_server(MSG_EXITING, NULL, 0);
                    333:                        break;
                    334:                case SIGWINCH:
                    335:                        client_write_server(MSG_RESIZE, NULL, 0);
                    336:                        break;
                    337:                case SIGCONT:
                    338:                        memset(&sigact, 0, sizeof sigact);
                    339:                        sigemptyset(&sigact.sa_mask);
                    340:                        sigact.sa_flags = SA_RESTART;
                    341:                        sigact.sa_handler = SIG_IGN;
                    342:                        if (sigaction(SIGTSTP, &sigact, NULL) != 0)
                    343:                                fatal("sigaction failed");
                    344:                        client_write_server(MSG_WAKEUP, NULL, 0);
                    345:                        break;
                    346:                }
1.30      nicm      347:        }
                    348:
1.31      nicm      349:        client_update_event();
1.30      nicm      350: }
                    351:
1.46      nicm      352: /* Callback for client imsg read events. */
1.34      nicm      353: /* ARGSUSED */
1.30      nicm      354: void
1.46      nicm      355: client_callback(unused int fd, short events, void *data)
1.30      nicm      356: {
1.33      nicm      357:        ssize_t n;
1.46      nicm      358:        int     retval;
1.30      nicm      359:
                    360:        if (events & EV_READ) {
                    361:                if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
                    362:                        goto lost_server;
1.46      nicm      363:                if (client_attached)
                    364:                        retval = client_dispatch_attached();
                    365:                else
                    366:                        retval = client_dispatch_wait(data);
                    367:                if (retval != 0) {
1.30      nicm      368:                        event_loopexit(NULL);
                    369:                        return;
1.35      nicm      370:                }
1.30      nicm      371:        }
1.35      nicm      372:
1.30      nicm      373:        if (events & EV_WRITE) {
                    374:                if (msgbuf_write(&client_ibuf.w) < 0)
                    375:                        goto lost_server;
                    376:        }
                    377:
1.31      nicm      378:        client_update_event();
1.30      nicm      379:        return;
                    380:
                    381: lost_server:
                    382:        client_exitmsg = "lost server";
1.32      nicm      383:        client_exitval = 1;
1.30      nicm      384:        event_loopexit(NULL);
                    385: }
                    386:
1.46      nicm      387: /* Dispatch imsgs when in wait state (before MSG_READY). */
                    388: int
                    389: client_dispatch_wait(void *data)
                    390: {
                    391:        struct imsg             imsg;
                    392:        ssize_t                 n, datalen;
                    393:        struct msg_shell_data   shelldata;
                    394:        struct msg_exit_data    exitdata;
                    395:        const char             *shellcmd = data;
                    396:
                    397:        if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
                    398:                fatalx("imsg_read failed");
                    399:
                    400:        for (;;) {
                    401:                if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
                    402:                        fatalx("imsg_get failed");
                    403:                if (n == 0)
                    404:                        return (0);
                    405:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
                    406:
                    407:                switch (imsg.hdr.type) {
                    408:                case MSG_EXIT:
                    409:                case MSG_SHUTDOWN:
                    410:                        if (datalen != sizeof exitdata) {
                    411:                                if (datalen != 0)
                    412:                                        fatalx("bad MSG_EXIT size");
                    413:                        } else {
                    414:                                memcpy(&exitdata, imsg.data, sizeof exitdata);
                    415:                                client_exitval = exitdata.retcode;
                    416:                        }
                    417:                        imsg_free(&imsg);
                    418:                        return (-1);
                    419:                case MSG_READY:
                    420:                        if (datalen != 0)
                    421:                                fatalx("bad MSG_READY size");
                    422:
                    423:                        client_attached = 1;
                    424:                        break;
                    425:                case MSG_VERSION:
                    426:                        if (datalen != 0)
                    427:                                fatalx("bad MSG_VERSION size");
                    428:
                    429:                        log_warnx("protocol version mismatch (client %u, "
                    430:                            "server %u)", PROTOCOL_VERSION, imsg.hdr.peerid);
                    431:                        client_exitval = 1;
                    432:
                    433:                        imsg_free(&imsg);
                    434:                        return (-1);
                    435:                case MSG_SHELL:
                    436:                        if (datalen != sizeof shelldata)
                    437:                                fatalx("bad MSG_SHELL size");
                    438:                        memcpy(&shelldata, imsg.data, sizeof shelldata);
                    439:                        shelldata.shell[(sizeof shelldata.shell) - 1] = '\0';
                    440:
                    441:                        clear_signals(0);
                    442:
                    443:                        shell_exec(shelldata.shell, shellcmd);
                    444:                        /* NOTREACHED */
                    445:                default:
                    446:                        fatalx("unexpected message");
                    447:                }
                    448:
                    449:                imsg_free(&imsg);
                    450:        }
                    451: }
                    452:
                    453: /* Dispatch imsgs in attached state (after MSG_READY). */
                    454: /* ARGSUSED */
1.9       nicm      455: int
1.46      nicm      456: client_dispatch_attached(void)
1.9       nicm      457: {
1.30      nicm      458:        struct imsg             imsg;
                    459:        struct msg_lock_data    lockdata;
                    460:        struct sigaction        sigact;
                    461:        ssize_t                 n, datalen;
1.9       nicm      462:
                    463:        for (;;) {
1.25      nicm      464:                if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
1.12      nicm      465:                        fatalx("imsg_get failed");
                    466:                if (n == 0)
1.9       nicm      467:                        return (0);
1.12      nicm      468:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.9       nicm      469:
1.30      nicm      470:                log_debug("client got %d", imsg.hdr.type);
1.12      nicm      471:                switch (imsg.hdr.type) {
1.48      nicm      472:                case MSG_DETACHKILL:
1.9       nicm      473:                case MSG_DETACH:
1.12      nicm      474:                        if (datalen != 0)
1.9       nicm      475:                                fatalx("bad MSG_DETACH size");
                    476:
1.48      nicm      477:                        client_exittype = imsg.hdr.type;
                    478:                        if (imsg.hdr.type == MSG_DETACHKILL)
                    479:                                client_exitmsg = "detached and SIGHUP";
                    480:                        else
                    481:                                client_exitmsg = "detached";
1.25      nicm      482:                        client_write_server(MSG_EXITING, NULL, 0);
1.9       nicm      483:                        break;
                    484:                case MSG_EXIT:
1.43      nicm      485:                        if (datalen != 0 &&
                    486:                            datalen != sizeof (struct msg_exit_data))
1.9       nicm      487:                                fatalx("bad MSG_EXIT size");
1.12      nicm      488:
1.25      nicm      489:                        client_write_server(MSG_EXITING, NULL, 0);
                    490:                        client_exitmsg = "exited";
1.9       nicm      491:                        break;
                    492:                case MSG_EXITED:
1.12      nicm      493:                        if (datalen != 0)
1.9       nicm      494:                                fatalx("bad MSG_EXITED size");
                    495:
1.12      nicm      496:                        imsg_free(&imsg);
1.9       nicm      497:                        return (-1);
                    498:                case MSG_SHUTDOWN:
1.12      nicm      499:                        if (datalen != 0)
1.9       nicm      500:                                fatalx("bad MSG_SHUTDOWN size");
                    501:
1.25      nicm      502:                        client_write_server(MSG_EXITING, NULL, 0);
                    503:                        client_exitmsg = "server exited";
1.32      nicm      504:                        client_exitval = 1;
1.9       nicm      505:                        break;
                    506:                case MSG_SUSPEND:
1.12      nicm      507:                        if (datalen != 0)
1.9       nicm      508:                                fatalx("bad MSG_SUSPEND size");
                    509:
1.30      nicm      510:                        memset(&sigact, 0, sizeof sigact);
                    511:                        sigemptyset(&sigact.sa_mask);
                    512:                        sigact.sa_flags = SA_RESTART;
                    513:                        sigact.sa_handler = SIG_DFL;
                    514:                        if (sigaction(SIGTSTP, &sigact, NULL) != 0)
                    515:                                fatal("sigaction failed");
                    516:                        kill(getpid(), SIGTSTP);
1.21      nicm      517:                        break;
                    518:                case MSG_LOCK:
                    519:                        if (datalen != sizeof lockdata)
                    520:                                fatalx("bad MSG_LOCK size");
                    521:                        memcpy(&lockdata, imsg.data, sizeof lockdata);
1.35      nicm      522:
1.21      nicm      523:                        lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
                    524:                        system(lockdata.cmd);
1.25      nicm      525:                        client_write_server(MSG_UNLOCK, NULL, 0);
1.9       nicm      526:                        break;
                    527:                default:
                    528:                        fatalx("unexpected message");
                    529:                }
1.12      nicm      530:
                    531:                imsg_free(&imsg);
1.9       nicm      532:        }
1.1       nicm      533: }