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

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