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

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