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

1.108   ! nicm        1: /* $OpenBSD: client.c,v 1.107 2015/11/24 22:04:36 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.99      nicm       29: #include <imsg.h>
1.85      nicm       30: #include <signal.h>
1.1       nicm       31: #include <stdlib.h>
                     32: #include <string.h>
                     33: #include <unistd.h>
                     34:
                     35: #include "tmux.h"
                     36:
1.99      nicm       37: struct tmuxproc        *client_proc;
                     38: struct tmuxpeer        *client_peer;
                     39: int             client_flags;
                     40: struct event    client_stdin;
1.52      nicm       41: enum {
                     42:        CLIENT_EXIT_NONE,
                     43:        CLIENT_EXIT_DETACHED,
                     44:        CLIENT_EXIT_DETACHED_HUP,
                     45:        CLIENT_EXIT_LOST_TTY,
                     46:        CLIENT_EXIT_TERMINATED,
                     47:        CLIENT_EXIT_LOST_SERVER,
                     48:        CLIENT_EXIT_EXITED,
                     49:        CLIENT_EXIT_SERVER_EXITED,
                     50: } client_exitreason = CLIENT_EXIT_NONE;
1.99      nicm       51: int             client_exitval;
                     52: enum msgtype    client_exittype;
                     53: const char     *client_exitsession;
                     54: int             client_attached;
1.1       nicm       55:
1.93      nicm       56: __dead void    client_exec(const char *);
1.49      nicm       57: int            client_get_lock(char *);
1.108   ! nicm       58: int            client_connect(struct event_base *, const char *, int);
1.98      nicm       59: void           client_send_identify(const char *, const char *);
1.54      nicm       60: void           client_stdin_callback(int, short, void *);
1.57      nicm       61: void           client_write(int, const char *, size_t);
1.99      nicm       62: void           client_signal(int);
                     63: void           client_dispatch(struct imsg *, void *);
                     64: void           client_dispatch_attached(struct imsg *);
                     65: void           client_dispatch_wait(struct imsg *);
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
1.108   ! nicm       99: client_connect(struct event_base *base, const 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.96      guenther  120:        if (connect(fd, (struct sockaddr *) &sa, sizeof(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.92      nicm      152:                fd = server_start(base, lockfd, lockfile);
1.82      nicm      153:        }
1.95      nicm      154:
1.82      nicm      155:        if (locked) {
1.58      nicm      156:                free(lockfile);
1.49      nicm      157:                close(lockfd);
1.1       nicm      158:        }
1.47      nicm      159:        setblocking(fd, 0);
1.46      nicm      160:        return (fd);
                    161:
                    162: failed:
1.95      nicm      163:        if (locked) {
                    164:                free(lockfile);
                    165:                close(lockfd);
                    166:        }
1.46      nicm      167:        close(fd);
                    168:        return (-1);
                    169: }
                    170:
1.52      nicm      171: /* Get exit string from reason number. */
                    172: const char *
                    173: client_exit_message(void)
                    174: {
1.73      nicm      175:        static char msg[256];
                    176:
1.52      nicm      177:        switch (client_exitreason) {
                    178:        case CLIENT_EXIT_NONE:
                    179:                break;
                    180:        case CLIENT_EXIT_DETACHED:
1.73      nicm      181:                if (client_exitsession != NULL) {
                    182:                        xsnprintf(msg, sizeof msg, "detached "
                    183:                            "(from session %s)", client_exitsession);
                    184:                        return (msg);
                    185:                }
1.52      nicm      186:                return ("detached");
                    187:        case CLIENT_EXIT_DETACHED_HUP:
1.73      nicm      188:                if (client_exitsession != NULL) {
                    189:                        xsnprintf(msg, sizeof msg, "detached and SIGHUP "
                    190:                            "(from session %s)", client_exitsession);
                    191:                        return (msg);
                    192:                }
1.52      nicm      193:                return ("detached and SIGHUP");
                    194:        case CLIENT_EXIT_LOST_TTY:
                    195:                return ("lost tty");
                    196:        case CLIENT_EXIT_TERMINATED:
                    197:                return ("terminated");
                    198:        case CLIENT_EXIT_LOST_SERVER:
                    199:                return ("lost server");
                    200:        case CLIENT_EXIT_EXITED:
                    201:                return ("exited");
                    202:        case CLIENT_EXIT_SERVER_EXITED:
                    203:                return ("server exited");
                    204:        }
                    205:        return ("unknown reason");
                    206: }
                    207:
1.46      nicm      208: /* Client main loop. */
                    209: int
1.92      nicm      210: client_main(struct event_base *base, int argc, char **argv, int flags)
1.46      nicm      211: {
                    212:        struct cmd              *cmd;
                    213:        struct cmd_list         *cmdlist;
1.70      nicm      214:        struct msg_command_data *data;
1.98      nicm      215:        int                      cmdflags, fd, i;
                    216:        const char              *ttynam, *cwd;
1.48      nicm      217:        pid_t                    ppid;
1.46      nicm      218:        enum msgtype             msg;
1.98      nicm      219:        char                    *cause, path[PATH_MAX];
1.56      nicm      220:        struct termios           tio, saved_tio;
1.70      nicm      221:        size_t                   size;
1.46      nicm      222:
1.99      nicm      223:        /* Ignore SIGCHLD now or daemon() in the server will leave a zombie. */
                    224:        signal(SIGCHLD, SIG_IGN);
                    225:
1.93      nicm      226:        /* Save the flags. */
                    227:        client_flags = flags;
                    228:
1.46      nicm      229:        /* Set up the initial command. */
                    230:        cmdflags = 0;
                    231:        if (shell_cmd != NULL) {
                    232:                msg = MSG_SHELL;
                    233:                cmdflags = CMD_STARTSERVER;
                    234:        } else if (argc == 0) {
                    235:                msg = MSG_COMMAND;
1.89      nicm      236:                cmdflags = CMD_STARTSERVER;
1.46      nicm      237:        } else {
                    238:                msg = MSG_COMMAND;
                    239:
                    240:                /*
                    241:                 * It sucks parsing the command string twice (in client and
                    242:                 * later in server) but it is necessary to get the start server
                    243:                 * flag.
                    244:                 */
1.62      nicm      245:                cmdlist = cmd_list_parse(argc, argv, NULL, 0, &cause);
                    246:                if (cmdlist == NULL) {
1.59      nicm      247:                        fprintf(stderr, "%s\n", cause);
1.46      nicm      248:                        return (1);
                    249:                }
                    250:                cmdflags &= ~CMD_STARTSERVER;
                    251:                TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
                    252:                        if (cmd->entry->flags & CMD_STARTSERVER)
                    253:                                cmdflags |= CMD_STARTSERVER;
                    254:                }
                    255:                cmd_list_free(cmdlist);
                    256:        }
                    257:
1.82      nicm      258:        /* Initialize the client socket and start the server. */
1.92      nicm      259:        fd = client_connect(base, socket_path, cmdflags & CMD_STARTSERVER);
1.46      nicm      260:        if (fd == -1) {
1.87      nicm      261:                if (errno == ECONNREFUSED) {
                    262:                        fprintf(stderr, "no server running on %s\n",
                    263:                            socket_path);
                    264:                } else {
                    265:                        fprintf(stderr, "error connecting to %s (%s)\n",
                    266:                            socket_path, strerror(errno));
                    267:                }
1.46      nicm      268:                return (1);
                    269:        }
1.97      nicm      270:
1.99      nicm      271:        /* Build process state. */
                    272:        client_proc = proc_start("client", base, 0, client_signal);
                    273:        client_peer = proc_add_peer(client_proc, fd, client_dispatch, NULL);
                    274:
1.97      nicm      275:        /* Save these before pledge(). */
1.102     nicm      276:        if ((cwd = getcwd(path, sizeof path)) == NULL) {
                    277:                if ((cwd = find_home()) == NULL)
                    278:                        cwd = "/";
                    279:        }
1.97      nicm      280:        if ((ttynam = ttyname(STDIN_FILENO)) == NULL)
                    281:                ttynam = "";
                    282:
                    283:        /*
                    284:         * Drop privileges for client. "proc exec" is needed for -c and for
                    285:         * locking (which uses system(3)).
                    286:         *
                    287:         * "tty" is needed to restore termios(4) and also for some reason -CC
                    288:         * does not work properly without it (input is not recognised).
                    289:         *
                    290:         * "sendfd" is dropped later in client_dispatch_wait().
                    291:         */
1.105     nicm      292:        if (pledge("stdio unix sendfd proc exec tty", NULL) != 0)
1.97      nicm      293:                fatal("pledge failed");
                    294:
                    295:        /* Free stuff that is not used in the client. */
1.100     nicm      296:        options_free(global_options);
                    297:        options_free(global_s_options);
                    298:        options_free(global_w_options);
1.101     nicm      299:        environ_free(global_environ);
1.46      nicm      300:
1.54      nicm      301:        /* Create stdin handler. */
                    302:        setblocking(STDIN_FILENO, 0);
                    303:        event_set(&client_stdin, STDIN_FILENO, EV_READ|EV_PERSIST,
                    304:            client_stdin_callback, NULL);
1.93      nicm      305:        if (client_flags & CLIENT_CONTROLCONTROL) {
1.107     nicm      306:                if (tcgetattr(STDIN_FILENO, &saved_tio) != 0)
                    307:                        fatal("tcgetattr failed");
1.56      nicm      308:                cfmakeraw(&tio);
                    309:                tio.c_iflag = ICRNL|IXANY;
                    310:                tio.c_oflag = OPOST|ONLCR;
                    311:                tio.c_lflag = NOKERNINFO;
                    312:                tio.c_cflag = CREAD|CS8|HUPCL;
                    313:                tio.c_cc[VMIN] = 1;
                    314:                tio.c_cc[VTIME] = 0;
                    315:                cfsetispeed(&tio, cfgetispeed(&saved_tio));
                    316:                cfsetospeed(&tio, cfgetospeed(&saved_tio));
                    317:                tcsetattr(STDIN_FILENO, TCSANOW, &tio);
                    318:        }
1.1       nicm      319:
1.71      nicm      320:        /* Send identify messages. */
1.98      nicm      321:        client_send_identify(ttynam, cwd);
1.1       nicm      322:
1.46      nicm      323:        /* Send first command. */
                    324:        if (msg == MSG_COMMAND) {
1.70      nicm      325:                /* How big is the command? */
                    326:                size = 0;
                    327:                for (i = 0; i < argc; i++)
                    328:                        size += strlen(argv[i]) + 1;
                    329:                data = xmalloc((sizeof *data) + size);
1.46      nicm      330:
                    331:                /* Prepare command for server. */
1.70      nicm      332:                data->argc = argc;
1.83      nicm      333:                if (cmd_pack_argv(argc, argv, (char *)(data + 1), size) != 0) {
1.55      nicm      334:                        fprintf(stderr, "command too long\n");
1.70      nicm      335:                        free(data);
1.46      nicm      336:                        return (1);
                    337:                }
1.70      nicm      338:                size += sizeof *data;
1.46      nicm      339:
1.70      nicm      340:                /* Send the command. */
1.99      nicm      341:                if (proc_send(client_peer, msg, -1, data, size) != 0) {
1.70      nicm      342:                        fprintf(stderr, "failed to send command\n");
                    343:                        free(data);
                    344:                        return (1);
                    345:                }
                    346:                free(data);
1.46      nicm      347:        } else if (msg == MSG_SHELL)
1.99      nicm      348:                proc_send(client_peer, msg, -1, NULL, 0);
1.1       nicm      349:
1.99      nicm      350:        /* Start main loop. */
                    351:        proc_loop(client_proc, NULL);
1.46      nicm      352:
                    353:        /* Print the exit message, if any, and exit. */
1.48      nicm      354:        if (client_attached) {
1.93      nicm      355:                if (client_exitreason != CLIENT_EXIT_NONE)
1.52      nicm      356:                        printf("[%s]\n", client_exit_message());
1.48      nicm      357:
                    358:                ppid = getppid();
                    359:                if (client_exittype == MSG_DETACHKILL && ppid > 1)
                    360:                        kill(ppid, SIGHUP);
1.93      nicm      361:        } else if (client_flags & CLIENT_CONTROLCONTROL) {
1.68      nicm      362:                if (client_exitreason != CLIENT_EXIT_NONE)
                    363:                        printf("%%exit %s\n", client_exit_message());
                    364:                else
                    365:                        printf("%%exit\n");
                    366:                printf("\033\\");
1.56      nicm      367:                tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
1.63      nicm      368:        }
1.54      nicm      369:        setblocking(STDIN_FILENO, 1);
1.46      nicm      370:        return (client_exitval);
1.1       nicm      371: }
                    372:
1.71      nicm      373: /* Send identify messages to server. */
1.11      nicm      374: void
1.98      nicm      375: client_send_identify(const char *ttynam, const char *cwd)
1.26      nicm      376: {
1.90      nicm      377:        const char       *s;
1.71      nicm      378:        char            **ss;
1.91      nicm      379:        size_t            sslen;
1.93      nicm      380:        int               fd, flags = client_flags;
1.90      nicm      381:        pid_t             pid;
1.71      nicm      382:
1.99      nicm      383:        proc_send(client_peer, MSG_IDENTIFY_FLAGS, -1, &flags, sizeof flags);
1.71      nicm      384:
                    385:        if ((s = getenv("TERM")) == NULL)
                    386:                s = "";
1.99      nicm      387:        proc_send(client_peer, MSG_IDENTIFY_TERM, -1, s, strlen(s) + 1);
1.71      nicm      388:
1.107     nicm      389:        proc_send(client_peer, MSG_IDENTIFY_TTYNAME, -1, ttynam,
                    390:            strlen(ttynam) + 1);
1.99      nicm      391:        proc_send(client_peer, MSG_IDENTIFY_CWD, -1, cwd, strlen(cwd) + 1);
1.26      nicm      392:
1.50      nicm      393:        if ((fd = dup(STDIN_FILENO)) == -1)
                    394:                fatal("dup failed");
1.99      nicm      395:        proc_send(client_peer, MSG_IDENTIFY_STDIN, fd, NULL, 0);
1.90      nicm      396:
                    397:        pid = getpid();
1.99      nicm      398:        proc_send(client_peer, MSG_IDENTIFY_CLIENTPID, -1, &pid, sizeof pid);
1.26      nicm      399:
1.91      nicm      400:        for (ss = environ; *ss != NULL; ss++) {
                    401:                sslen = strlen(*ss) + 1;
1.107     nicm      402:                if (sslen > MAX_IMSGSIZE - IMSG_HEADER_SIZE)
                    403:                        continue;
                    404:                proc_send(client_peer, MSG_IDENTIFY_ENVIRON, -1, *ss, sslen);
1.91      nicm      405:        }
1.75      nicm      406:
1.99      nicm      407:        proc_send(client_peer, MSG_IDENTIFY_DONE, -1, NULL, 0);
1.30      nicm      408: }
                    409:
1.54      nicm      410: /* Callback for client stdin read events. */
                    411: void
1.106     nicm      412: client_stdin_callback(__unused int fd, __unused short events,
                    413:     __unused void *arg)
1.54      nicm      414: {
                    415:        struct msg_stdin_data   data;
                    416:
                    417:        data.size = read(STDIN_FILENO, data.data, sizeof data.data);
                    418:        if (data.size < 0 && (errno == EINTR || errno == EAGAIN))
                    419:                return;
                    420:
1.99      nicm      421:        proc_send(client_peer, MSG_STDIN, -1, &data, sizeof data);
1.54      nicm      422:        if (data.size <= 0)
                    423:                event_del(&client_stdin);
                    424: }
                    425:
1.57      nicm      426: /* Force write to file descriptor. */
                    427: void
                    428: client_write(int fd, const char *data, size_t size)
                    429: {
                    430:        ssize_t used;
                    431:
                    432:        while (size != 0) {
                    433:                used = write(fd, data, size);
                    434:                if (used == -1) {
                    435:                        if (errno == EINTR || errno == EAGAIN)
                    436:                                continue;
                    437:                        break;
                    438:                }
                    439:                data += used;
                    440:                size -= used;
                    441:        }
                    442: }
                    443:
1.93      nicm      444: /* Run command in shell; used for -c. */
                    445: __dead void
                    446: client_exec(const char *shell)
                    447: {
                    448:        const char      *name, *ptr;
                    449:        char            *argv0;
                    450:
                    451:        log_debug("shell %s, command %s", shell, shell_cmd);
                    452:
                    453:        ptr = strrchr(shell, '/');
                    454:        if (ptr != NULL && *(ptr + 1) != '\0')
                    455:                name = ptr + 1;
                    456:        else
                    457:                name = shell;
                    458:        if (client_flags & CLIENT_LOGIN)
                    459:                xasprintf(&argv0, "-%s", name);
                    460:        else
                    461:                xasprintf(&argv0, "%s", name);
                    462:        setenv("SHELL", shell, 1);
                    463:
                    464:        setblocking(STDIN_FILENO, 1);
                    465:        setblocking(STDOUT_FILENO, 1);
                    466:        setblocking(STDERR_FILENO, 1);
                    467:        closefrom(STDERR_FILENO + 1);
                    468:
                    469:        execl(shell, argv0, "-c", shell_cmd, (char *) NULL);
                    470:        fatal("execl failed");
                    471: }
                    472:
1.99      nicm      473: /* Callback to handle signals in the client. */
                    474: void
                    475: client_signal(int sig)
                    476: {
                    477:        struct sigaction sigact;
                    478:        int              status;
                    479:
                    480:        if (sig == SIGCHLD)
                    481:                waitpid(WAIT_ANY, &status, WNOHANG);
                    482:        else if (!client_attached) {
                    483:                if (sig == SIGTERM)
                    484:                        proc_exit(client_proc);
                    485:        } else {
                    486:                switch (sig) {
                    487:                case SIGHUP:
                    488:                        client_exitreason = CLIENT_EXIT_LOST_TTY;
                    489:                        client_exitval = 1;
                    490:                        proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
                    491:                        break;
                    492:                case SIGTERM:
                    493:                        client_exitreason = CLIENT_EXIT_TERMINATED;
                    494:                        client_exitval = 1;
                    495:                        proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
                    496:                        break;
                    497:                case SIGWINCH:
                    498:                        proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
                    499:                        break;
                    500:                case SIGCONT:
                    501:                        memset(&sigact, 0, sizeof sigact);
                    502:                        sigemptyset(&sigact.sa_mask);
                    503:                        sigact.sa_flags = SA_RESTART;
                    504:                        sigact.sa_handler = SIG_IGN;
                    505:                        if (sigaction(SIGTSTP, &sigact, NULL) != 0)
                    506:                                fatal("sigaction failed");
                    507:                        proc_send(client_peer, MSG_WAKEUP, -1, NULL, 0);
                    508:                        break;
                    509:                }
                    510:        }
                    511: }
                    512:
                    513: /* Callback for client read events. */
                    514: void
1.106     nicm      515: client_dispatch(struct imsg *imsg, __unused void *arg)
1.99      nicm      516: {
                    517:        if (imsg == NULL) {
                    518:                client_exitreason = CLIENT_EXIT_LOST_SERVER;
                    519:                client_exitval = 1;
                    520:        } else if (client_attached)
                    521:                client_dispatch_attached(imsg);
                    522:        else
                    523:                client_dispatch_wait(imsg);
                    524: }
                    525:
1.46      nicm      526: /* Dispatch imsgs when in wait state (before MSG_READY). */
1.99      nicm      527: void
                    528: client_dispatch_wait(struct imsg *imsg)
1.46      nicm      529: {
1.69      nicm      530:        char                    *data;
1.99      nicm      531:        ssize_t                  datalen;
1.69      nicm      532:        struct msg_stdout_data   stdoutdata;
                    533:        struct msg_stderr_data   stderrdata;
                    534:        int                      retval;
1.97      nicm      535:        static int               pledge_applied;
                    536:
                    537:        /*
                    538:         * "sendfd" is no longer required once all of the identify messages
                    539:         * have been sent. We know the server won't send us anything until that
                    540:         * point (because we don't ask it to), so we can drop "sendfd" once we
                    541:         * get the first message from the server.
                    542:         */
                    543:        if (!pledge_applied) {
1.105     nicm      544:                if (pledge("stdio unix proc exec tty", NULL) != 0)
1.97      nicm      545:                        fatal("pledge failed");
                    546:                pledge_applied = 1;
                    547:        };
1.46      nicm      548:
1.99      nicm      549:        data = imsg->data;
                    550:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.60      nicm      551:
1.99      nicm      552:        switch (imsg->hdr.type) {
                    553:        case MSG_EXIT:
                    554:        case MSG_SHUTDOWN:
                    555:                if (datalen != sizeof retval && datalen != 0)
                    556:                        fatalx("bad MSG_EXIT size");
                    557:                if (datalen == sizeof retval) {
                    558:                        memcpy(&retval, data, sizeof retval);
                    559:                        client_exitval = retval;
                    560:                }
                    561:                proc_exit(client_proc);
                    562:                break;
                    563:        case MSG_READY:
                    564:                if (datalen != 0)
                    565:                        fatalx("bad MSG_READY size");
1.54      nicm      566:
1.99      nicm      567:                event_del(&client_stdin);
                    568:                client_attached = 1;
                    569:                proc_send(client_peer, MSG_RESIZE, -1, NULL, 0);
                    570:                break;
                    571:        case MSG_STDIN:
                    572:                if (datalen != 0)
                    573:                        fatalx("bad MSG_STDIN size");
1.54      nicm      574:
1.99      nicm      575:                event_add(&client_stdin, NULL);
                    576:                break;
                    577:        case MSG_STDOUT:
                    578:                if (datalen != sizeof stdoutdata)
                    579:                        fatalx("bad MSG_STDOUT size");
                    580:                memcpy(&stdoutdata, data, sizeof stdoutdata);
1.46      nicm      581:
1.99      nicm      582:                client_write(STDOUT_FILENO, stdoutdata.data,
                    583:                    stdoutdata.size);
                    584:                break;
                    585:        case MSG_STDERR:
                    586:                if (datalen != sizeof stderrdata)
                    587:                        fatalx("bad MSG_STDERR size");
                    588:                memcpy(&stderrdata, data, sizeof stderrdata);
1.46      nicm      589:
1.99      nicm      590:                client_write(STDERR_FILENO, stderrdata.data,
                    591:                    stderrdata.size);
                    592:                break;
                    593:        case MSG_VERSION:
                    594:                if (datalen != 0)
                    595:                        fatalx("bad MSG_VERSION size");
                    596:
                    597:                fprintf(stderr, "protocol version mismatch "
                    598:                    "(client %d, server %u)\n", PROTOCOL_VERSION,
1.103     nicm      599:                    imsg->hdr.peerid & 0xff);
1.99      nicm      600:                client_exitval = 1;
                    601:                proc_exit(client_proc);
                    602:                break;
                    603:        case MSG_SHELL:
                    604:                if (datalen == 0 || data[datalen - 1] != '\0')
                    605:                        fatalx("bad MSG_SHELL string");
                    606:
                    607:                clear_signals(0);
                    608:                client_exec(data);
                    609:                /* NOTREACHED */
                    610:        case MSG_DETACH:
                    611:        case MSG_DETACHKILL:
                    612:                proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
                    613:                break;
                    614:        case MSG_EXITED:
                    615:                proc_exit(client_proc);
                    616:                break;
1.46      nicm      617:        }
                    618: }
                    619:
                    620: /* Dispatch imsgs in attached state (after MSG_READY). */
1.99      nicm      621: void
                    622: client_dispatch_attached(struct imsg *imsg)
1.9       nicm      623: {
1.69      nicm      624:        struct sigaction         sigact;
                    625:        char                    *data;
1.99      nicm      626:        ssize_t                  datalen;
1.9       nicm      627:
1.99      nicm      628:        data = imsg->data;
                    629:        datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1.12      nicm      630:
1.99      nicm      631:        switch (imsg->hdr.type) {
                    632:        case MSG_DETACH:
                    633:        case MSG_DETACHKILL:
                    634:                if (datalen == 0 || data[datalen - 1] != '\0')
                    635:                        fatalx("bad MSG_DETACH string");
                    636:
                    637:                client_exitsession = xstrdup(data);
                    638:                client_exittype = imsg->hdr.type;
                    639:                if (imsg->hdr.type == MSG_DETACHKILL)
                    640:                        client_exitreason = CLIENT_EXIT_DETACHED_HUP;
                    641:                else
                    642:                        client_exitreason = CLIENT_EXIT_DETACHED;
                    643:                proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
                    644:                break;
                    645:        case MSG_EXIT:
                    646:                if (datalen != 0 && datalen != sizeof (int))
                    647:                        fatalx("bad MSG_EXIT size");
1.9       nicm      648:
1.99      nicm      649:                proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
                    650:                client_exitreason = CLIENT_EXIT_EXITED;
                    651:                break;
                    652:        case MSG_EXITED:
                    653:                if (datalen != 0)
                    654:                        fatalx("bad MSG_EXITED size");
1.9       nicm      655:
1.99      nicm      656:                proc_exit(client_proc);
                    657:                break;
                    658:        case MSG_SHUTDOWN:
                    659:                if (datalen != 0)
                    660:                        fatalx("bad MSG_SHUTDOWN size");
                    661:
                    662:                proc_send(client_peer, MSG_EXITING, -1, NULL, 0);
                    663:                client_exitreason = CLIENT_EXIT_SERVER_EXITED;
                    664:                client_exitval = 1;
                    665:                break;
                    666:        case MSG_SUSPEND:
                    667:                if (datalen != 0)
                    668:                        fatalx("bad MSG_SUSPEND size");
                    669:
                    670:                memset(&sigact, 0, sizeof sigact);
                    671:                sigemptyset(&sigact.sa_mask);
                    672:                sigact.sa_flags = SA_RESTART;
                    673:                sigact.sa_handler = SIG_DFL;
                    674:                if (sigaction(SIGTSTP, &sigact, NULL) != 0)
                    675:                        fatal("sigaction failed");
                    676:                kill(getpid(), SIGTSTP);
                    677:                break;
                    678:        case MSG_LOCK:
                    679:                if (datalen == 0 || data[datalen - 1] != '\0')
                    680:                        fatalx("bad MSG_LOCK string");
1.9       nicm      681:
1.99      nicm      682:                system(data);
                    683:                proc_send(client_peer, MSG_UNLOCK, -1, NULL, 0);
                    684:                break;
1.9       nicm      685:        }
1.1       nicm      686: }