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

1.88    ! nicm        1: /* $OpenBSD: client.c,v 1.87 2015/04/21 22:21:41 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>
1.51      nicm       20: #include <sys/file.h>
1.1       nicm       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.30      nicm       27: #include <event.h>
1.80      nicm       28: #include <fcntl.h>
1.85      nicm       29: #include <signal.h>
1.1       nicm       30: #include <stdlib.h>
                     31: #include <string.h>
                     32: #include <unistd.h>
                     33:
                     34: #include "tmux.h"
                     35:
1.25      nicm       36: struct imsgbuf client_ibuf;
1.30      nicm       37: struct event   client_event;
1.54      nicm       38: struct event   client_stdin;
1.52      nicm       39: enum {
                     40:        CLIENT_EXIT_NONE,
                     41:        CLIENT_EXIT_DETACHED,
                     42:        CLIENT_EXIT_DETACHED_HUP,
                     43:        CLIENT_EXIT_LOST_TTY,
                     44:        CLIENT_EXIT_TERMINATED,
                     45:        CLIENT_EXIT_LOST_SERVER,
                     46:        CLIENT_EXIT_EXITED,
                     47:        CLIENT_EXIT_SERVER_EXITED,
                     48: } client_exitreason = CLIENT_EXIT_NONE;
1.32      nicm       49: int            client_exitval;
1.48      nicm       50: enum msgtype   client_exittype;
1.73      nicm       51: const char     *client_exitsession;
1.46      nicm       52: int            client_attached;
1.1       nicm       53:
1.49      nicm       54: int            client_get_lock(char *);
1.46      nicm       55: int            client_connect(char *, int);
1.30      nicm       56: void           client_send_identify(int);
1.70      nicm       57: int            client_write_one(enum msgtype, int, const void *, size_t);
                     58: int            client_write_server(enum msgtype, const void *, size_t);
1.31      nicm       59: void           client_update_event(void);
1.30      nicm       60: void           client_signal(int, short, void *);
1.54      nicm       61: void           client_stdin_callback(int, short, void *);
1.57      nicm       62: void           client_write(int, const char *, size_t);
1.30      nicm       63: void           client_callback(int, short, void *);
1.46      nicm       64: int            client_dispatch_attached(void);
                     65: int            client_dispatch_wait(void *);
1.53      nicm       66: const char     *client_exit_message(void);
1.25      nicm       67:
1.49      nicm       68: /*
                     69:  * Get server create lock. If already held then server start is happening in
                     70:  * another client, so block until the lock is released and return -1 to
                     71:  * retry. Ignore other errors - just continue and start the server without the
                     72:  * lock.
                     73:  */
                     74: int
                     75: client_get_lock(char *lockfile)
                     76: {
                     77:        int lockfd;
                     78:
                     79:        if ((lockfd = open(lockfile, O_WRONLY|O_CREAT, 0600)) == -1)
                     80:                fatal("open failed");
1.82      nicm       81:        log_debug("lock file is %s", lockfile);
1.49      nicm       82:
1.82      nicm       83:        if (flock(lockfd, LOCK_EX|LOCK_NB) == -1) {
                     84:                log_debug("flock failed: %s", strerror(errno));
                     85:                if (errno != EAGAIN)
                     86:                        return (lockfd);
                     87:                while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
1.49      nicm       88:                        /* nothing */;
                     89:                close(lockfd);
                     90:                return (-1);
                     91:        }
1.82      nicm       92:        log_debug("flock succeeded");
1.49      nicm       93:
                     94:        return (lockfd);
                     95: }
                     96:
1.46      nicm       97: /* Connect client to server. */
                     98: int
                     99: client_connect(char *path, int start_server)
1.1       nicm      100: {
1.26      nicm      101:        struct sockaddr_un      sa;
                    102:        size_t                  size;
1.82      nicm      103:        int                     fd, lockfd = -1, locked = 0;
                    104:        char                   *lockfile = NULL;
1.1       nicm      105:
                    106:        memset(&sa, 0, sizeof sa);
                    107:        sa.sun_family = AF_UNIX;
                    108:        size = strlcpy(sa.sun_path, path, sizeof sa.sun_path);
                    109:        if (size >= sizeof sa.sun_path) {
                    110:                errno = ENAMETOOLONG;
1.46      nicm      111:                return (-1);
1.1       nicm      112:        }
1.82      nicm      113:        log_debug("socket is %s", path);
1.1       nicm      114:
1.49      nicm      115: retry:
1.10      nicm      116:        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.18      nicm      117:                fatal("socket failed");
1.1       nicm      118:
1.82      nicm      119:        log_debug("trying connect");
1.10      nicm      120:        if (connect(fd, (struct sockaddr *) &sa, SUN_LEN(&sa)) == -1) {
1.82      nicm      121:                log_debug("connect failed: %s", strerror(errno));
1.49      nicm      122:                if (errno != ECONNREFUSED && errno != ENOENT)
                    123:                        goto failed;
1.46      nicm      124:                if (!start_server)
                    125:                        goto failed;
1.49      nicm      126:                close(fd);
                    127:
1.82      nicm      128:                if (!locked) {
                    129:                        xasprintf(&lockfile, "%s.lock", path);
                    130:                        if ((lockfd = client_get_lock(lockfile)) == -1) {
                    131:                                log_debug("didn't get lock");
                    132:                                free(lockfile);
                    133:                                goto retry;
                    134:                        }
                    135:                        log_debug("got lock");
                    136:
                    137:                        /*
                    138:                         * Always retry at least once, even if we got the lock,
                    139:                         * because another client could have taken the lock,
                    140:                         * started the server and released the lock between our
                    141:                         * connect() and flock().
                    142:                         */
                    143:                        locked = 1;
1.49      nicm      144:                        goto retry;
1.78      nicm      145:                }
1.82      nicm      146:
1.78      nicm      147:                if (unlink(path) != 0 && errno != ENOENT) {
                    148:                        free(lockfile);
                    149:                        close(lockfd);
1.49      nicm      150:                        return (-1);
1.78      nicm      151:                }
1.49      nicm      152:                fd = server_start(lockfd, lockfile);
1.82      nicm      153:        }
                    154:        if (locked) {
1.58      nicm      155:                free(lockfile);
1.49      nicm      156:                close(lockfd);
1.1       nicm      157:        }
                    158:
1.47      nicm      159:        setblocking(fd, 0);
1.46      nicm      160:        return (fd);
                    161:
                    162: failed:
                    163:        close(fd);
                    164:        return (-1);
                    165: }
                    166:
1.52      nicm      167: /* Get exit string from reason number. */
                    168: const char *
                    169: client_exit_message(void)
                    170: {
1.73      nicm      171:        static char msg[256];
                    172:
1.52      nicm      173:        switch (client_exitreason) {
                    174:        case CLIENT_EXIT_NONE:
                    175:                break;
                    176:        case CLIENT_EXIT_DETACHED:
1.73      nicm      177:                if (client_exitsession != NULL) {
                    178:                        xsnprintf(msg, sizeof msg, "detached "
                    179:                            "(from session %s)", client_exitsession);
                    180:                        return (msg);
                    181:                }
1.52      nicm      182:                return ("detached");
                    183:        case CLIENT_EXIT_DETACHED_HUP:
1.73      nicm      184:                if (client_exitsession != NULL) {
                    185:                        xsnprintf(msg, sizeof msg, "detached and SIGHUP "
                    186:                            "(from session %s)", client_exitsession);
                    187:                        return (msg);
                    188:                }
1.52      nicm      189:                return ("detached and SIGHUP");
                    190:        case CLIENT_EXIT_LOST_TTY:
                    191:                return ("lost tty");
                    192:        case CLIENT_EXIT_TERMINATED:
                    193:                return ("terminated");
                    194:        case CLIENT_EXIT_LOST_SERVER:
                    195:                return ("lost server");
                    196:        case CLIENT_EXIT_EXITED:
                    197:                return ("exited");
                    198:        case CLIENT_EXIT_SERVER_EXITED:
                    199:                return ("server exited");
                    200:        }
                    201:        return ("unknown reason");
                    202: }
                    203:
1.46      nicm      204: /* Client main loop. */
                    205: int
                    206: client_main(int argc, char **argv, int flags)
                    207: {
                    208:        struct cmd              *cmd;
                    209:        struct cmd_list         *cmdlist;
1.70      nicm      210:        struct msg_command_data *data;
                    211:        int                      cmdflags, fd, i;
1.48      nicm      212:        pid_t                    ppid;
1.46      nicm      213:        enum msgtype             msg;
                    214:        char                    *cause;
1.56      nicm      215:        struct termios           tio, saved_tio;
1.70      nicm      216:        size_t                   size;
1.46      nicm      217:
                    218:        /* Set up the initial command. */
                    219:        cmdflags = 0;
                    220:        if (shell_cmd != NULL) {
                    221:                msg = MSG_SHELL;
                    222:                cmdflags = CMD_STARTSERVER;
                    223:        } else if (argc == 0) {
                    224:                msg = MSG_COMMAND;
1.67      nicm      225:                cmdflags = CMD_STARTSERVER|CMD_CANTNEST;
1.46      nicm      226:        } else {
                    227:                msg = MSG_COMMAND;
                    228:
                    229:                /*
                    230:                 * It sucks parsing the command string twice (in client and
                    231:                 * later in server) but it is necessary to get the start server
                    232:                 * flag.
                    233:                 */
1.62      nicm      234:                cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause);
                    235:                if (cmdlist == NULL) {
1.59      nicm      236:                        fprintf(stderr, "%s\n", cause);
1.46      nicm      237:                        return (1);
                    238:                }
                    239:                cmdflags &= ~CMD_STARTSERVER;
                    240:                TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
                    241:                        if (cmd->entry->flags & CMD_STARTSERVER)
                    242:                                cmdflags |= CMD_STARTSERVER;
                    243:                        if (cmd->entry->flags & CMD_CANTNEST)
                    244:                                cmdflags |= CMD_CANTNEST;
                    245:                }
                    246:                cmd_list_free(cmdlist);
                    247:        }
                    248:
                    249:        /*
                    250:         * Check if this could be a nested session, if the command can't nest:
                    251:         * if the socket path matches $TMUX, this is probably the same server.
                    252:         */
                    253:        if (shell_cmd == NULL && environ_path != NULL &&
1.52      nicm      254:            (cmdflags & CMD_CANTNEST) &&
                    255:            strcmp(socket_path, environ_path) == 0) {
1.55      nicm      256:                fprintf(stderr, "sessions should be nested with care, "
                    257:                    "unset $TMUX to force\n");
1.46      nicm      258:                return (1);
                    259:        }
                    260:
1.82      nicm      261:        /* Set process title, log and signals now this is the client. */
                    262:        setproctitle("client (%s)", socket_path);
                    263:        logfile("client");
                    264:
1.88    ! nicm      265:        /* Establish signal handlers. */
        !           266:        set_signals(client_signal);
        !           267:
1.82      nicm      268:        /* Initialize the client socket and start the server. */
1.46      nicm      269:        fd = client_connect(socket_path, cmdflags & CMD_STARTSERVER);
                    270:        if (fd == -1) {
1.87      nicm      271:                if (errno == ECONNREFUSED) {
                    272:                        fprintf(stderr, "no server running on %s\n",
                    273:                            socket_path);
                    274:                } else {
                    275:                        fprintf(stderr, "error connecting to %s (%s)\n",
                    276:                            socket_path, strerror(errno));
                    277:                }
1.46      nicm      278:                return (1);
                    279:        }
                    280:
                    281:        /* Create imsg. */
1.25      nicm      282:        imsg_init(&client_ibuf, fd);
1.46      nicm      283:        event_set(&client_event, fd, EV_READ, client_callback, shell_cmd);
                    284:
1.54      nicm      285:        /* Create stdin handler. */
                    286:        setblocking(STDIN_FILENO, 0);
                    287:        event_set(&client_stdin, STDIN_FILENO, EV_READ|EV_PERSIST,
                    288:            client_stdin_callback, NULL);
1.68      nicm      289:        if (flags & CLIENT_CONTROLCONTROL) {
1.56      nicm      290:                if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) {
                    291:                        fprintf(stderr, "tcgetattr failed: %s\n",
                    292:                            strerror(errno));
                    293:                        return (1);
                    294:                }
                    295:                cfmakeraw(&tio);
                    296:                tio.c_iflag = ICRNL|IXANY;
                    297:                tio.c_oflag = OPOST|ONLCR;
                    298:                tio.c_lflag = NOKERNINFO;
                    299:                tio.c_cflag = CREAD|CS8|HUPCL;
                    300:                tio.c_cc[VMIN] = 1;
                    301:                tio.c_cc[VTIME] = 0;
                    302:                cfsetispeed(&tio, cfgetispeed(&saved_tio));
                    303:                cfsetospeed(&tio, cfgetospeed(&saved_tio));
                    304:                tcsetattr(STDIN_FILENO, TCSANOW, &tio);
                    305:        }
1.1       nicm      306:
1.71      nicm      307:        /* Send identify messages. */
1.42      nicm      308:        client_send_identify(flags);
1.1       nicm      309:
1.46      nicm      310:        /* Send first command. */
                    311:        if (msg == MSG_COMMAND) {
1.70      nicm      312:                /* How big is the command? */
                    313:                size = 0;
                    314:                for (i = 0; i < argc; i++)
                    315:                        size += strlen(argv[i]) + 1;
                    316:                data = xmalloc((sizeof *data) + size);
1.46      nicm      317:
                    318:                /* Prepare command for server. */
1.70      nicm      319:                data->argc = argc;
1.83      nicm      320:                if (cmd_pack_argv(argc, argv, (char *)(data + 1), size) != 0) {
1.55      nicm      321:                        fprintf(stderr, "command too long\n");
1.70      nicm      322:                        free(data);
1.46      nicm      323:                        return (1);
                    324:                }
1.70      nicm      325:                size += sizeof *data;
1.46      nicm      326:
1.70      nicm      327:                /* Send the command. */
                    328:                if (client_write_server(msg, data, size) != 0) {
                    329:                        fprintf(stderr, "failed to send command\n");
                    330:                        free(data);
                    331:                        return (1);
                    332:                }
                    333:                free(data);
1.46      nicm      334:        } else if (msg == MSG_SHELL)
                    335:                client_write_server(msg, NULL, 0);
1.1       nicm      336:
1.46      nicm      337:        /* Set the event and dispatch. */
                    338:        client_update_event();
                    339:        event_dispatch();
                    340:
                    341:        /* Print the exit message, if any, and exit. */
1.48      nicm      342:        if (client_attached) {
1.52      nicm      343:                if (client_exitreason != CLIENT_EXIT_NONE && !login_shell)
                    344:                        printf("[%s]\n", client_exit_message());
1.48      nicm      345:
                    346:                ppid = getppid();
                    347:                if (client_exittype == MSG_DETACHKILL && ppid > 1)
                    348:                        kill(ppid, SIGHUP);
1.68      nicm      349:        } else if (flags & CLIENT_CONTROLCONTROL) {
                    350:                if (client_exitreason != CLIENT_EXIT_NONE)
                    351:                        printf("%%exit %s\n", client_exit_message());
                    352:                else
                    353:                        printf("%%exit\n");
                    354:                printf("\033\\");
1.56      nicm      355:                tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
1.63      nicm      356:        }
1.54      nicm      357:        setblocking(STDIN_FILENO, 1);
1.46      nicm      358:        return (client_exitval);
1.1       nicm      359: }
                    360:
1.71      nicm      361: /* Send identify messages to server. */
1.11      nicm      362: void
1.26      nicm      363: client_send_identify(int flags)
                    364: {
1.71      nicm      365:        const char      *s;
                    366:        char            **ss;
                    367:        int              fd;
                    368:
                    369:        client_write_one(MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags);
                    370:
                    371:        if ((s = getenv("TERM")) == NULL)
                    372:                s = "";
                    373:        client_write_one(MSG_IDENTIFY_TERM, -1, s, strlen(s) + 1);
                    374:
                    375:        if ((s = ttyname(STDIN_FILENO)) == NULL)
                    376:                s = "";
                    377:        client_write_one(MSG_IDENTIFY_TTYNAME, -1, s, strlen(s) + 1);
                    378:
                    379:        if ((fd = open(".", O_RDONLY)) == -1)
                    380:                fd = open("/", O_RDONLY);
                    381:        client_write_one(MSG_IDENTIFY_CWD, fd, NULL, 0);
1.26      nicm      382:
1.50      nicm      383:        if ((fd = dup(STDIN_FILENO)) == -1)
                    384:                fatal("dup failed");
1.71      nicm      385:        client_write_one(MSG_IDENTIFY_STDIN, fd, NULL, 0);
1.26      nicm      386:
1.71      nicm      387:        for (ss = environ; *ss != NULL; ss++)
                    388:                client_write_one(MSG_IDENTIFY_ENVIRON, -1, *ss, strlen(*ss) + 1);
1.75      nicm      389:
1.71      nicm      390:        client_write_one(MSG_IDENTIFY_DONE, -1, NULL, 0);
1.11      nicm      391:
1.71      nicm      392:        client_update_event();
1.11      nicm      393: }
                    394:
1.70      nicm      395: /* Helper to send one message. */
                    396: int
                    397: client_write_one(enum msgtype type, int fd, const void *buf, size_t len)
                    398: {
                    399:        int     retval;
                    400:
                    401:        retval = imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, fd,
1.83      nicm      402:            (void *)buf, len);
1.70      nicm      403:        if (retval != 1)
                    404:                return (-1);
                    405:        return (0);
                    406: }
                    407:
1.46      nicm      408: /* Write a message to the server without a file descriptor. */
1.70      nicm      409: int
                    410: client_write_server(enum msgtype type, const void *buf, size_t len)
1.25      nicm      411: {
1.70      nicm      412:        int     retval;
                    413:
                    414:        retval = client_write_one(type, -1, buf, len);
                    415:        if (retval == 0)
                    416:                client_update_event();
                    417:        return (retval);
1.25      nicm      418: }
                    419:
1.46      nicm      420: /* Update client event based on whether it needs to read or read and write. */
1.31      nicm      421: void
                    422: client_update_event(void)
                    423: {
                    424:        short   events;
                    425:
                    426:        event_del(&client_event);
                    427:        events = EV_READ;
                    428:        if (client_ibuf.w.queued > 0)
                    429:                events |= EV_WRITE;
1.46      nicm      430:        event_set(
                    431:            &client_event, client_ibuf.fd, events, client_callback, shell_cmd);
1.31      nicm      432:        event_add(&client_event, NULL);
                    433: }
                    434:
1.46      nicm      435: /* Callback to handle signals in the client. */
1.30      nicm      436: void
1.31      nicm      437: client_signal(int sig, unused short events, unused void *data)
1.30      nicm      438: {
1.46      nicm      439:        struct sigaction sigact;
                    440:        int              status;
1.30      nicm      441:
1.84      nicm      442:        if (sig == SIGCHLD)
                    443:                waitpid(WAIT_ANY, &status, WNOHANG);
                    444:        else if (!client_attached) {
                    445:                if (sig == SIGTERM)
1.46      nicm      446:                        event_loopexit(NULL);
                    447:        } else {
                    448:                switch (sig) {
                    449:                case SIGHUP:
1.52      nicm      450:                        client_exitreason = CLIENT_EXIT_LOST_TTY;
1.46      nicm      451:                        client_exitval = 1;
                    452:                        client_write_server(MSG_EXITING, NULL, 0);
                    453:                        break;
                    454:                case SIGTERM:
1.52      nicm      455:                        client_exitreason = CLIENT_EXIT_TERMINATED;
1.46      nicm      456:                        client_exitval = 1;
                    457:                        client_write_server(MSG_EXITING, NULL, 0);
                    458:                        break;
                    459:                case SIGWINCH:
                    460:                        client_write_server(MSG_RESIZE, NULL, 0);
                    461:                        break;
                    462:                case SIGCONT:
                    463:                        memset(&sigact, 0, sizeof sigact);
                    464:                        sigemptyset(&sigact.sa_mask);
                    465:                        sigact.sa_flags = SA_RESTART;
                    466:                        sigact.sa_handler = SIG_IGN;
                    467:                        if (sigaction(SIGTSTP, &sigact, NULL) != 0)
                    468:                                fatal("sigaction failed");
                    469:                        client_write_server(MSG_WAKEUP, NULL, 0);
                    470:                        break;
                    471:                }
1.30      nicm      472:        }
                    473:
1.31      nicm      474:        client_update_event();
1.30      nicm      475: }
                    476:
1.46      nicm      477: /* Callback for client imsg read events. */
1.30      nicm      478: void
1.46      nicm      479: client_callback(unused int fd, short events, void *data)
1.30      nicm      480: {
1.33      nicm      481:        ssize_t n;
1.46      nicm      482:        int     retval;
1.30      nicm      483:
                    484:        if (events & EV_READ) {
                    485:                if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
                    486:                        goto lost_server;
1.46      nicm      487:                if (client_attached)
                    488:                        retval = client_dispatch_attached();
                    489:                else
                    490:                        retval = client_dispatch_wait(data);
                    491:                if (retval != 0) {
1.30      nicm      492:                        event_loopexit(NULL);
                    493:                        return;
1.35      nicm      494:                }
1.30      nicm      495:        }
1.35      nicm      496:
1.30      nicm      497:        if (events & EV_WRITE) {
1.81      krw       498:                if (msgbuf_write(&client_ibuf.w) <= 0 && errno != EAGAIN)
1.30      nicm      499:                        goto lost_server;
                    500:        }
                    501:
1.31      nicm      502:        client_update_event();
1.30      nicm      503:        return;
                    504:
                    505: lost_server:
1.52      nicm      506:        client_exitreason = CLIENT_EXIT_LOST_SERVER;
1.32      nicm      507:        client_exitval = 1;
1.30      nicm      508:        event_loopexit(NULL);
                    509: }
                    510:
1.54      nicm      511: /* Callback for client stdin read events. */
                    512: void
                    513: client_stdin_callback(unused int fd, unused short events, unused void *data1)
                    514: {
                    515:        struct msg_stdin_data   data;
                    516:
                    517:        data.size = read(STDIN_FILENO, data.data, sizeof data.data);
                    518:        if (data.size < 0 && (errno == EINTR || errno == EAGAIN))
                    519:                return;
                    520:
                    521:        client_write_server(MSG_STDIN, &data, sizeof data);
                    522:        if (data.size <= 0)
                    523:                event_del(&client_stdin);
                    524:        client_update_event();
                    525: }
                    526:
1.57      nicm      527: /* Force write to file descriptor. */
                    528: void
                    529: client_write(int fd, const char *data, size_t size)
                    530: {
                    531:        ssize_t used;
                    532:
                    533:        while (size != 0) {
                    534:                used = write(fd, data, size);
                    535:                if (used == -1) {
                    536:                        if (errno == EINTR || errno == EAGAIN)
                    537:                                continue;
                    538:                        break;
                    539:                }
                    540:                data += used;
                    541:                size -= used;
                    542:        }
                    543: }
                    544:
1.46      nicm      545: /* Dispatch imsgs when in wait state (before MSG_READY). */
                    546: int
1.69      nicm      547: client_dispatch_wait(void *data0)
1.46      nicm      548: {
1.69      nicm      549:        struct imsg              imsg;
                    550:        char                    *data;
                    551:        ssize_t                  n, datalen;
                    552:        struct msg_stdout_data   stdoutdata;
                    553:        struct msg_stderr_data   stderrdata;
                    554:        int                      retval;
1.46      nicm      555:
                    556:        for (;;) {
                    557:                if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
                    558:                        fatalx("imsg_get failed");
                    559:                if (n == 0)
                    560:                        return (0);
1.69      nicm      561:
                    562:                data = imsg.data;
1.46      nicm      563:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
                    564:
1.86      nicm      565:                log_debug("got %u from server", imsg.hdr.type);
1.46      nicm      566:                switch (imsg.hdr.type) {
                    567:                case MSG_EXIT:
                    568:                case MSG_SHUTDOWN:
1.69      nicm      569:                        if (datalen != sizeof retval && datalen != 0)
                    570:                                fatalx("bad MSG_EXIT size");
                    571:                        if (datalen == sizeof retval) {
                    572:                                memcpy(&retval, data, sizeof retval);
                    573:                                client_exitval = retval;
1.46      nicm      574:                        }
                    575:                        imsg_free(&imsg);
                    576:                        return (-1);
                    577:                case MSG_READY:
                    578:                        if (datalen != 0)
                    579:                                fatalx("bad MSG_READY size");
                    580:
1.54      nicm      581:                        event_del(&client_stdin);
1.46      nicm      582:                        client_attached = 1;
1.65      nicm      583:                        client_write_server(MSG_RESIZE, NULL, 0);
1.60      nicm      584:                        break;
                    585:                case MSG_STDIN:
                    586:                        if (datalen != 0)
                    587:                                fatalx("bad MSG_STDIN size");
                    588:
                    589:                        event_add(&client_stdin, NULL);
1.46      nicm      590:                        break;
1.54      nicm      591:                case MSG_STDOUT:
                    592:                        if (datalen != sizeof stdoutdata)
1.69      nicm      593:                                fatalx("bad MSG_STDOUT size");
                    594:                        memcpy(&stdoutdata, data, sizeof stdoutdata);
1.54      nicm      595:
1.70      nicm      596:                        client_write(STDOUT_FILENO, stdoutdata.data,
                    597:                            stdoutdata.size);
1.54      nicm      598:                        break;
                    599:                case MSG_STDERR:
                    600:                        if (datalen != sizeof stderrdata)
1.69      nicm      601:                                fatalx("bad MSG_STDERR size");
                    602:                        memcpy(&stderrdata, data, sizeof stderrdata);
1.54      nicm      603:
1.70      nicm      604:                        client_write(STDERR_FILENO, stderrdata.data,
                    605:                            stderrdata.size);
1.54      nicm      606:                        break;
1.46      nicm      607:                case MSG_VERSION:
                    608:                        if (datalen != 0)
                    609:                                fatalx("bad MSG_VERSION size");
                    610:
1.54      nicm      611:                        fprintf(stderr, "protocol version mismatch "
1.86      nicm      612:                            "(client %d, server %u)\n", PROTOCOL_VERSION,
1.54      nicm      613:                            imsg.hdr.peerid);
1.46      nicm      614:                        client_exitval = 1;
                    615:
                    616:                        imsg_free(&imsg);
                    617:                        return (-1);
                    618:                case MSG_SHELL:
1.72      nicm      619:                        if (datalen == 0 || data[datalen - 1] != '\0')
1.69      nicm      620:                                fatalx("bad MSG_SHELL string");
1.46      nicm      621:
                    622:                        clear_signals(0);
1.69      nicm      623:                        shell_exec(data, data0);
1.46      nicm      624:                        /* NOTREACHED */
1.56      nicm      625:                case MSG_DETACH:
1.73      nicm      626:                case MSG_DETACHKILL:
1.56      nicm      627:                        client_write_server(MSG_EXITING, NULL, 0);
                    628:                        break;
                    629:                case MSG_EXITED:
                    630:                        imsg_free(&imsg);
                    631:                        return (-1);
1.46      nicm      632:                }
                    633:
                    634:                imsg_free(&imsg);
                    635:        }
                    636: }
                    637:
                    638: /* Dispatch imsgs in attached state (after MSG_READY). */
1.9       nicm      639: int
1.46      nicm      640: client_dispatch_attached(void)
1.9       nicm      641: {
1.69      nicm      642:        struct imsg              imsg;
                    643:        struct sigaction         sigact;
                    644:        char                    *data;
                    645:        ssize_t                  n, datalen;
1.9       nicm      646:
                    647:        for (;;) {
1.25      nicm      648:                if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
1.12      nicm      649:                        fatalx("imsg_get failed");
                    650:                if (n == 0)
1.9       nicm      651:                        return (0);
1.69      nicm      652:
                    653:                data = imsg.data;
1.12      nicm      654:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.9       nicm      655:
1.86      nicm      656:                log_debug("got %u from server", imsg.hdr.type);
1.12      nicm      657:                switch (imsg.hdr.type) {
1.73      nicm      658:                case MSG_DETACH:
1.48      nicm      659:                case MSG_DETACHKILL:
1.73      nicm      660:                        if (datalen == 0 || data[datalen - 1] != '\0')
                    661:                                fatalx("bad MSG_DETACH string");
1.9       nicm      662:
1.73      nicm      663:                        client_exitsession = xstrdup(data);
1.48      nicm      664:                        client_exittype = imsg.hdr.type;
                    665:                        if (imsg.hdr.type == MSG_DETACHKILL)
1.52      nicm      666:                                client_exitreason = CLIENT_EXIT_DETACHED_HUP;
1.48      nicm      667:                        else
1.52      nicm      668:                                client_exitreason = CLIENT_EXIT_DETACHED;
1.25      nicm      669:                        client_write_server(MSG_EXITING, NULL, 0);
1.9       nicm      670:                        break;
                    671:                case MSG_EXIT:
1.69      nicm      672:                        if (datalen != 0 && datalen != sizeof (int))
1.9       nicm      673:                                fatalx("bad MSG_EXIT size");
1.12      nicm      674:
1.25      nicm      675:                        client_write_server(MSG_EXITING, NULL, 0);
1.52      nicm      676:                        client_exitreason = CLIENT_EXIT_EXITED;
1.9       nicm      677:                        break;
                    678:                case MSG_EXITED:
1.12      nicm      679:                        if (datalen != 0)
1.9       nicm      680:                                fatalx("bad MSG_EXITED size");
                    681:
1.12      nicm      682:                        imsg_free(&imsg);
1.9       nicm      683:                        return (-1);
                    684:                case MSG_SHUTDOWN:
1.12      nicm      685:                        if (datalen != 0)
1.9       nicm      686:                                fatalx("bad MSG_SHUTDOWN size");
                    687:
1.25      nicm      688:                        client_write_server(MSG_EXITING, NULL, 0);
1.52      nicm      689:                        client_exitreason = CLIENT_EXIT_SERVER_EXITED;
1.32      nicm      690:                        client_exitval = 1;
1.9       nicm      691:                        break;
                    692:                case MSG_SUSPEND:
1.12      nicm      693:                        if (datalen != 0)
1.9       nicm      694:                                fatalx("bad MSG_SUSPEND size");
                    695:
1.30      nicm      696:                        memset(&sigact, 0, sizeof sigact);
                    697:                        sigemptyset(&sigact.sa_mask);
                    698:                        sigact.sa_flags = SA_RESTART;
                    699:                        sigact.sa_handler = SIG_DFL;
                    700:                        if (sigaction(SIGTSTP, &sigact, NULL) != 0)
                    701:                                fatal("sigaction failed");
                    702:                        kill(getpid(), SIGTSTP);
1.21      nicm      703:                        break;
                    704:                case MSG_LOCK:
1.72      nicm      705:                        if (datalen == 0 || data[datalen - 1] != '\0')
1.69      nicm      706:                                fatalx("bad MSG_LOCK string");
1.35      nicm      707:
1.69      nicm      708:                        system(data);
1.25      nicm      709:                        client_write_server(MSG_UNLOCK, NULL, 0);
1.9       nicm      710:                        break;
                    711:                }
1.12      nicm      712:
                    713:                imsg_free(&imsg);
1.9       nicm      714:        }
1.1       nicm      715: }