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

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