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

1.56    ! nicm        1: /* $OpenBSD: client.c,v 1.55 2012/05/25 08:28:10 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 <fcntl.h>
                     29: #include <pwd.h>
                     30: #include <stdlib.h>
                     31: #include <string.h>
                     32: #include <unistd.h>
                     33:
                     34: #include "tmux.h"
                     35:
1.25      nicm       36: struct imsgbuf client_ibuf;
1.30      nicm       37: struct event   client_event;
1.54      nicm       38: struct event   client_stdin;
1.52      nicm       39: enum {
                     40:        CLIENT_EXIT_NONE,
                     41:        CLIENT_EXIT_DETACHED,
                     42:        CLIENT_EXIT_DETACHED_HUP,
                     43:        CLIENT_EXIT_LOST_TTY,
                     44:        CLIENT_EXIT_TERMINATED,
                     45:        CLIENT_EXIT_LOST_SERVER,
                     46:        CLIENT_EXIT_EXITED,
                     47:        CLIENT_EXIT_SERVER_EXITED,
                     48: } client_exitreason = CLIENT_EXIT_NONE;
1.32      nicm       49: int            client_exitval;
1.48      nicm       50: enum msgtype   client_exittype;
1.46      nicm       51: int            client_attached;
1.1       nicm       52:
1.49      nicm       53: int            client_get_lock(char *);
1.46      nicm       54: int            client_connect(char *, int);
1.30      nicm       55: void           client_send_identify(int);
                     56: void           client_send_environ(void);
                     57: void           client_write_server(enum msgtype, void *, size_t);
1.31      nicm       58: void           client_update_event(void);
1.30      nicm       59: void           client_signal(int, short, void *);
1.54      nicm       60: void           client_stdin_callback(int, short, void *);
1.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:
                     80:        if (flock(lockfd, LOCK_EX|LOCK_NB) == -1 && errno == EWOULDBLOCK) {
                     81:                while (flock(lockfd, LOCK_EX) == -1 && errno == EINTR)
                     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);
                    119:                if ((lockfd = client_get_lock(lockfile)) == -1)
                    120:                        goto retry;
                    121:                if (unlink(path) != 0 && errno != ENOENT)
                    122:                        return (-1);
                    123:                fd = server_start(lockfd, lockfile);
                    124:                xfree(lockfile);
                    125:                close(lockfd);
1.1       nicm      126:        }
                    127:
1.47      nicm      128:        setblocking(fd, 0);
1.46      nicm      129:        return (fd);
                    130:
                    131: failed:
                    132:        close(fd);
                    133:        return (-1);
                    134: }
                    135:
1.52      nicm      136: /* Get exit string from reason number. */
                    137: const char *
                    138: client_exit_message(void)
                    139: {
                    140:        switch (client_exitreason) {
                    141:        case CLIENT_EXIT_NONE:
                    142:                break;
                    143:        case CLIENT_EXIT_DETACHED:
                    144:                return ("detached");
                    145:        case CLIENT_EXIT_DETACHED_HUP:
                    146:                return ("detached and SIGHUP");
                    147:        case CLIENT_EXIT_LOST_TTY:
                    148:                return ("lost tty");
                    149:        case CLIENT_EXIT_TERMINATED:
                    150:                return ("terminated");
                    151:        case CLIENT_EXIT_LOST_SERVER:
                    152:                return ("lost server");
                    153:        case CLIENT_EXIT_EXITED:
                    154:                return ("exited");
                    155:        case CLIENT_EXIT_SERVER_EXITED:
                    156:                return ("server exited");
                    157:        }
                    158:        return ("unknown reason");
                    159: }
                    160:
1.46      nicm      161: /* Client main loop. */
                    162: int
                    163: client_main(int argc, char **argv, int flags)
                    164: {
                    165:        struct cmd              *cmd;
                    166:        struct cmd_list         *cmdlist;
                    167:        struct msg_command_data  cmddata;
                    168:        int                      cmdflags, fd;
1.48      nicm      169:        pid_t                    ppid;
1.46      nicm      170:        enum msgtype             msg;
                    171:        char                    *cause;
1.56    ! nicm      172:        struct termios           tio, saved_tio;
1.46      nicm      173:
                    174:        /* Set up the initial command. */
                    175:        cmdflags = 0;
                    176:        if (shell_cmd != NULL) {
                    177:                msg = MSG_SHELL;
                    178:                cmdflags = CMD_STARTSERVER;
                    179:        } else if (argc == 0) {
                    180:                msg = MSG_COMMAND;
                    181:                cmdflags = CMD_STARTSERVER|CMD_SENDENVIRON|CMD_CANTNEST;
                    182:        } else {
                    183:                msg = MSG_COMMAND;
                    184:
                    185:                /*
                    186:                 * It sucks parsing the command string twice (in client and
                    187:                 * later in server) but it is necessary to get the start server
                    188:                 * flag.
                    189:                 */
                    190:                if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
                    191:                        log_warnx("%s", cause);
                    192:                        return (1);
                    193:                }
                    194:                cmdflags &= ~CMD_STARTSERVER;
                    195:                TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
                    196:                        if (cmd->entry->flags & CMD_STARTSERVER)
                    197:                                cmdflags |= CMD_STARTSERVER;
                    198:                        if (cmd->entry->flags & CMD_SENDENVIRON)
                    199:                                cmdflags |= CMD_SENDENVIRON;
                    200:                        if (cmd->entry->flags & CMD_CANTNEST)
                    201:                                cmdflags |= CMD_CANTNEST;
                    202:                }
                    203:                cmd_list_free(cmdlist);
                    204:        }
                    205:
                    206:        /*
                    207:         * Check if this could be a nested session, if the command can't nest:
                    208:         * if the socket path matches $TMUX, this is probably the same server.
                    209:         */
                    210:        if (shell_cmd == NULL && environ_path != NULL &&
1.52      nicm      211:            (cmdflags & CMD_CANTNEST) &&
                    212:            strcmp(socket_path, environ_path) == 0) {
1.55      nicm      213:                fprintf(stderr, "sessions should be nested with care, "
                    214:                    "unset $TMUX to force\n");
1.46      nicm      215:                return (1);
                    216:        }
                    217:
                    218:        /* Initialise the client socket and start the server. */
                    219:        fd = client_connect(socket_path, cmdflags & CMD_STARTSERVER);
                    220:        if (fd == -1) {
1.55      nicm      221:                fprintf(stderr, "failed to connect to server\n");
1.46      nicm      222:                return (1);
                    223:        }
                    224:
                    225:        /* Set process title, log and signals now this is the client. */
                    226:        setproctitle("client (%s)", socket_path);
                    227:        logfile("client");
                    228:
                    229:        /* Create imsg. */
1.25      nicm      230:        imsg_init(&client_ibuf, fd);
1.46      nicm      231:        event_set(&client_event, fd, EV_READ, client_callback, shell_cmd);
                    232:
1.54      nicm      233:        /* Create stdin handler. */
                    234:        setblocking(STDIN_FILENO, 0);
                    235:        event_set(&client_stdin, STDIN_FILENO, EV_READ|EV_PERSIST,
                    236:            client_stdin_callback, NULL);
1.56    ! nicm      237:        if (flags & IDENTIFY_TERMIOS) {
        !           238:                if (tcgetattr(STDIN_FILENO, &saved_tio) != 0) {
        !           239:                        fprintf(stderr, "tcgetattr failed: %s\n",
        !           240:                            strerror(errno));
        !           241:                        return (1);
        !           242:                }
        !           243:                cfmakeraw(&tio);
        !           244:                tio.c_iflag = ICRNL|IXANY;
        !           245:                tio.c_oflag = OPOST|ONLCR;
        !           246:                tio.c_lflag = NOKERNINFO;
        !           247:                tio.c_cflag = CREAD|CS8|HUPCL;
        !           248:                tio.c_cc[VMIN] = 1;
        !           249:                tio.c_cc[VTIME] = 0;
        !           250:                cfsetispeed(&tio, cfgetispeed(&saved_tio));
        !           251:                cfsetospeed(&tio, cfgetospeed(&saved_tio));
        !           252:                tcsetattr(STDIN_FILENO, TCSANOW, &tio);
        !           253:        }
1.54      nicm      254:
1.46      nicm      255:        /* Establish signal handlers. */
                    256:        set_signals(client_signal);
1.1       nicm      257:
1.46      nicm      258:        /* Send initial environment. */
1.11      nicm      259:        if (cmdflags & CMD_SENDENVIRON)
1.25      nicm      260:                client_send_environ();
1.42      nicm      261:        client_send_identify(flags);
1.1       nicm      262:
1.46      nicm      263:        /* Send first command. */
                    264:        if (msg == MSG_COMMAND) {
                    265:                /* Fill in command line arguments. */
                    266:                cmddata.pid = environ_pid;
                    267:                cmddata.idx = environ_idx;
                    268:
                    269:                /* Prepare command for server. */
                    270:                cmddata.argc = argc;
                    271:                if (cmd_pack_argv(
                    272:                    argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
1.55      nicm      273:                        fprintf(stderr, "command too long\n");
1.46      nicm      274:                        return (1);
                    275:                }
                    276:
                    277:                client_write_server(msg, &cmddata, sizeof cmddata);
                    278:        } else if (msg == MSG_SHELL)
                    279:                client_write_server(msg, NULL, 0);
1.1       nicm      280:
1.46      nicm      281:        /* Set the event and dispatch. */
                    282:        client_update_event();
1.54      nicm      283:        event_add (&client_stdin, NULL);
1.46      nicm      284:        event_dispatch();
                    285:
                    286:        /* Print the exit message, if any, and exit. */
1.48      nicm      287:        if (client_attached) {
1.52      nicm      288:                if (client_exitreason != CLIENT_EXIT_NONE && !login_shell)
                    289:                        printf("[%s]\n", client_exit_message());
1.48      nicm      290:
                    291:                ppid = getppid();
                    292:                if (client_exittype == MSG_DETACHKILL && ppid > 1)
                    293:                        kill(ppid, SIGHUP);
1.56    ! nicm      294:        } else if (flags & IDENTIFY_TERMIOS)
        !           295:                tcsetattr(STDOUT_FILENO, TCSAFLUSH, &saved_tio);
1.54      nicm      296:        setblocking(STDIN_FILENO, 1);
1.46      nicm      297:        return (client_exitval);
1.1       nicm      298: }
                    299:
1.46      nicm      300: /* Send identify message to server with the file descriptors. */
1.11      nicm      301: void
1.26      nicm      302: client_send_identify(int flags)
                    303: {
                    304:        struct msg_identify_data        data;
                    305:        char                           *term;
                    306:        int                             fd;
                    307:
                    308:        data.flags = flags;
                    309:
                    310:        if (getcwd(data.cwd, sizeof data.cwd) == NULL)
                    311:                *data.cwd = '\0';
1.35      nicm      312:
1.26      nicm      313:        term = getenv("TERM");
                    314:        if (term == NULL ||
                    315:            strlcpy(data.term, term, sizeof data.term) >= sizeof data.term)
                    316:                *data.term = '\0';
                    317:
1.50      nicm      318:        if ((fd = dup(STDIN_FILENO)) == -1)
                    319:                fatal("dup failed");
                    320:        imsg_compose(&client_ibuf,
                    321:            MSG_IDENTIFY, PROTOCOL_VERSION, -1, fd, &data, sizeof data);
1.54      nicm      322:        client_update_event();
1.26      nicm      323: }
                    324:
1.46      nicm      325: /* Forward entire environment to server. */
1.26      nicm      326: void
1.25      nicm      327: client_send_environ(void)
1.11      nicm      328: {
1.26      nicm      329:        struct msg_environ_data data;
1.11      nicm      330:        char                  **var;
                    331:
1.35      nicm      332:        for (var = environ; *var != NULL; var++) {
1.11      nicm      333:                if (strlcpy(data.var, *var, sizeof data.var) >= sizeof data.var)
                    334:                        continue;
1.25      nicm      335:                client_write_server(MSG_ENVIRON, &data, sizeof data);
1.11      nicm      336:        }
                    337: }
                    338:
1.46      nicm      339: /* Write a message to the server without a file descriptor. */
1.25      nicm      340: void
                    341: client_write_server(enum msgtype type, void *buf, size_t len)
                    342: {
1.35      nicm      343:        imsg_compose(&client_ibuf, type, PROTOCOL_VERSION, -1, -1, buf, len);
1.54      nicm      344:        client_update_event();
1.25      nicm      345: }
                    346:
1.46      nicm      347: /* Update client event based on whether it needs to read or read and write. */
1.31      nicm      348: void
                    349: client_update_event(void)
                    350: {
                    351:        short   events;
                    352:
                    353:        event_del(&client_event);
                    354:        events = EV_READ;
                    355:        if (client_ibuf.w.queued > 0)
                    356:                events |= EV_WRITE;
1.46      nicm      357:        event_set(
                    358:            &client_event, client_ibuf.fd, events, client_callback, shell_cmd);
1.31      nicm      359:        event_add(&client_event, NULL);
                    360: }
                    361:
1.46      nicm      362: /* Callback to handle signals in the client. */
1.34      nicm      363: /* ARGSUSED */
1.30      nicm      364: void
1.31      nicm      365: client_signal(int sig, unused short events, unused void *data)
1.30      nicm      366: {
1.46      nicm      367:        struct sigaction sigact;
                    368:        int              status;
1.30      nicm      369:
1.46      nicm      370:        if (!client_attached) {
                    371:                switch (sig) {
                    372:                case SIGCHLD:
                    373:                        waitpid(WAIT_ANY, &status, WNOHANG);
                    374:                        break;
                    375:                case SIGTERM:
                    376:                        event_loopexit(NULL);
                    377:                        break;
                    378:                }
                    379:        } else {
                    380:                switch (sig) {
                    381:                case SIGHUP:
1.52      nicm      382:                        client_exitreason = CLIENT_EXIT_LOST_TTY;
1.46      nicm      383:                        client_exitval = 1;
                    384:                        client_write_server(MSG_EXITING, NULL, 0);
                    385:                        break;
                    386:                case SIGTERM:
1.52      nicm      387:                        client_exitreason = CLIENT_EXIT_TERMINATED;
1.46      nicm      388:                        client_exitval = 1;
                    389:                        client_write_server(MSG_EXITING, NULL, 0);
                    390:                        break;
                    391:                case SIGWINCH:
                    392:                        client_write_server(MSG_RESIZE, NULL, 0);
                    393:                        break;
                    394:                case SIGCONT:
                    395:                        memset(&sigact, 0, sizeof sigact);
                    396:                        sigemptyset(&sigact.sa_mask);
                    397:                        sigact.sa_flags = SA_RESTART;
                    398:                        sigact.sa_handler = SIG_IGN;
                    399:                        if (sigaction(SIGTSTP, &sigact, NULL) != 0)
                    400:                                fatal("sigaction failed");
                    401:                        client_write_server(MSG_WAKEUP, NULL, 0);
                    402:                        break;
                    403:                }
1.30      nicm      404:        }
                    405:
1.31      nicm      406:        client_update_event();
1.30      nicm      407: }
                    408:
1.46      nicm      409: /* Callback for client imsg read events. */
1.34      nicm      410: /* ARGSUSED */
1.30      nicm      411: void
1.46      nicm      412: client_callback(unused int fd, short events, void *data)
1.30      nicm      413: {
1.33      nicm      414:        ssize_t n;
1.46      nicm      415:        int     retval;
1.30      nicm      416:
                    417:        if (events & EV_READ) {
                    418:                if ((n = imsg_read(&client_ibuf)) == -1 || n == 0)
                    419:                        goto lost_server;
1.46      nicm      420:                if (client_attached)
                    421:                        retval = client_dispatch_attached();
                    422:                else
                    423:                        retval = client_dispatch_wait(data);
                    424:                if (retval != 0) {
1.30      nicm      425:                        event_loopexit(NULL);
                    426:                        return;
1.35      nicm      427:                }
1.30      nicm      428:        }
1.35      nicm      429:
1.30      nicm      430:        if (events & EV_WRITE) {
                    431:                if (msgbuf_write(&client_ibuf.w) < 0)
                    432:                        goto lost_server;
                    433:        }
                    434:
1.31      nicm      435:        client_update_event();
1.30      nicm      436:        return;
                    437:
                    438: lost_server:
1.52      nicm      439:        client_exitreason = CLIENT_EXIT_LOST_SERVER;
1.32      nicm      440:        client_exitval = 1;
1.30      nicm      441:        event_loopexit(NULL);
                    442: }
                    443:
1.54      nicm      444: /* Callback for client stdin read events. */
                    445: /* ARGSUSED */
                    446: void
                    447: client_stdin_callback(unused int fd, unused short events, unused void *data1)
                    448: {
                    449:        struct msg_stdin_data   data;
                    450:
                    451:        data.size = read(STDIN_FILENO, data.data, sizeof data.data);
                    452:        if (data.size < 0 && (errno == EINTR || errno == EAGAIN))
                    453:                return;
                    454:
                    455:        client_write_server(MSG_STDIN, &data, sizeof data);
                    456:        if (data.size <= 0)
                    457:                event_del(&client_stdin);
                    458:        client_update_event();
                    459: }
                    460:
1.46      nicm      461: /* Dispatch imsgs when in wait state (before MSG_READY). */
                    462: int
                    463: client_dispatch_wait(void *data)
                    464: {
                    465:        struct imsg             imsg;
                    466:        ssize_t                 n, datalen;
                    467:        struct msg_shell_data   shelldata;
                    468:        struct msg_exit_data    exitdata;
1.54      nicm      469:        struct msg_stdout_data  stdoutdata;
                    470:        struct msg_stderr_data  stderrdata;
1.46      nicm      471:        const char             *shellcmd = data;
                    472:
                    473:        for (;;) {
                    474:                if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
                    475:                        fatalx("imsg_get failed");
                    476:                if (n == 0)
                    477:                        return (0);
                    478:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
                    479:
1.54      nicm      480:                log_debug("got %d from server", imsg.hdr.type);
1.46      nicm      481:                switch (imsg.hdr.type) {
                    482:                case MSG_EXIT:
                    483:                case MSG_SHUTDOWN:
                    484:                        if (datalen != sizeof exitdata) {
                    485:                                if (datalen != 0)
                    486:                                        fatalx("bad MSG_EXIT size");
                    487:                        } else {
                    488:                                memcpy(&exitdata, imsg.data, sizeof exitdata);
                    489:                                client_exitval = exitdata.retcode;
                    490:                        }
                    491:                        imsg_free(&imsg);
                    492:                        return (-1);
                    493:                case MSG_READY:
                    494:                        if (datalen != 0)
                    495:                                fatalx("bad MSG_READY size");
                    496:
1.54      nicm      497:                        event_del(&client_stdin);
1.46      nicm      498:                        client_attached = 1;
                    499:                        break;
1.54      nicm      500:                case MSG_STDOUT:
                    501:                        if (datalen != sizeof stdoutdata)
                    502:                                fatalx("bad MSG_STDOUT");
                    503:                        memcpy(&stdoutdata, imsg.data, sizeof stdoutdata);
                    504:
                    505:                        fwrite(stdoutdata.data, stdoutdata.size, 1, stdout);
                    506:                        break;
                    507:                case MSG_STDERR:
                    508:                        if (datalen != sizeof stderrdata)
                    509:                                fatalx("bad MSG_STDERR");
                    510:                        memcpy(&stderrdata, imsg.data, sizeof stderrdata);
                    511:
                    512:                        fwrite(stderrdata.data, stderrdata.size, 1, stderr);
                    513:                        break;
1.46      nicm      514:                case MSG_VERSION:
                    515:                        if (datalen != 0)
                    516:                                fatalx("bad MSG_VERSION size");
                    517:
1.54      nicm      518:                        fprintf(stderr, "protocol version mismatch "
                    519:                            "(client %u, server %u)\n", PROTOCOL_VERSION,
                    520:                            imsg.hdr.peerid);
1.46      nicm      521:                        client_exitval = 1;
                    522:
                    523:                        imsg_free(&imsg);
                    524:                        return (-1);
                    525:                case MSG_SHELL:
                    526:                        if (datalen != sizeof shelldata)
                    527:                                fatalx("bad MSG_SHELL size");
                    528:                        memcpy(&shelldata, imsg.data, sizeof shelldata);
                    529:                        shelldata.shell[(sizeof shelldata.shell) - 1] = '\0';
                    530:
                    531:                        clear_signals(0);
                    532:
                    533:                        shell_exec(shelldata.shell, shellcmd);
                    534:                        /* NOTREACHED */
1.56    ! nicm      535:                case MSG_DETACH:
        !           536:                        client_write_server(MSG_EXITING, NULL, 0);
        !           537:                        break;
        !           538:                case MSG_EXITED:
        !           539:                        imsg_free(&imsg);
        !           540:                        return (-1);
1.46      nicm      541:                default:
                    542:                        fatalx("unexpected message");
                    543:                }
                    544:
                    545:                imsg_free(&imsg);
                    546:        }
                    547: }
                    548:
                    549: /* Dispatch imsgs in attached state (after MSG_READY). */
                    550: /* ARGSUSED */
1.9       nicm      551: int
1.46      nicm      552: client_dispatch_attached(void)
1.9       nicm      553: {
1.30      nicm      554:        struct imsg             imsg;
                    555:        struct msg_lock_data    lockdata;
                    556:        struct sigaction        sigact;
                    557:        ssize_t                 n, datalen;
1.9       nicm      558:
                    559:        for (;;) {
1.25      nicm      560:                if ((n = imsg_get(&client_ibuf, &imsg)) == -1)
1.12      nicm      561:                        fatalx("imsg_get failed");
                    562:                if (n == 0)
1.9       nicm      563:                        return (0);
1.12      nicm      564:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.9       nicm      565:
1.55      nicm      566:                log_debug("got %d from server", imsg.hdr.type);
1.12      nicm      567:                switch (imsg.hdr.type) {
1.48      nicm      568:                case MSG_DETACHKILL:
1.9       nicm      569:                case MSG_DETACH:
1.12      nicm      570:                        if (datalen != 0)
1.9       nicm      571:                                fatalx("bad MSG_DETACH size");
                    572:
1.48      nicm      573:                        client_exittype = imsg.hdr.type;
                    574:                        if (imsg.hdr.type == MSG_DETACHKILL)
1.52      nicm      575:                                client_exitreason = CLIENT_EXIT_DETACHED_HUP;
1.48      nicm      576:                        else
1.52      nicm      577:                                client_exitreason = CLIENT_EXIT_DETACHED;
1.25      nicm      578:                        client_write_server(MSG_EXITING, NULL, 0);
1.9       nicm      579:                        break;
                    580:                case MSG_EXIT:
1.43      nicm      581:                        if (datalen != 0 &&
                    582:                            datalen != sizeof (struct msg_exit_data))
1.9       nicm      583:                                fatalx("bad MSG_EXIT size");
1.12      nicm      584:
1.25      nicm      585:                        client_write_server(MSG_EXITING, NULL, 0);
1.52      nicm      586:                        client_exitreason = CLIENT_EXIT_EXITED;
1.9       nicm      587:                        break;
                    588:                case MSG_EXITED:
1.12      nicm      589:                        if (datalen != 0)
1.9       nicm      590:                                fatalx("bad MSG_EXITED size");
                    591:
1.12      nicm      592:                        imsg_free(&imsg);
1.9       nicm      593:                        return (-1);
                    594:                case MSG_SHUTDOWN:
1.12      nicm      595:                        if (datalen != 0)
1.9       nicm      596:                                fatalx("bad MSG_SHUTDOWN size");
                    597:
1.25      nicm      598:                        client_write_server(MSG_EXITING, NULL, 0);
1.52      nicm      599:                        client_exitreason = CLIENT_EXIT_SERVER_EXITED;
1.32      nicm      600:                        client_exitval = 1;
1.9       nicm      601:                        break;
                    602:                case MSG_SUSPEND:
1.12      nicm      603:                        if (datalen != 0)
1.9       nicm      604:                                fatalx("bad MSG_SUSPEND size");
                    605:
1.30      nicm      606:                        memset(&sigact, 0, sizeof sigact);
                    607:                        sigemptyset(&sigact.sa_mask);
                    608:                        sigact.sa_flags = SA_RESTART;
                    609:                        sigact.sa_handler = SIG_DFL;
                    610:                        if (sigaction(SIGTSTP, &sigact, NULL) != 0)
                    611:                                fatal("sigaction failed");
                    612:                        kill(getpid(), SIGTSTP);
1.21      nicm      613:                        break;
                    614:                case MSG_LOCK:
                    615:                        if (datalen != sizeof lockdata)
                    616:                                fatalx("bad MSG_LOCK size");
                    617:                        memcpy(&lockdata, imsg.data, sizeof lockdata);
1.35      nicm      618:
1.21      nicm      619:                        lockdata.cmd[(sizeof lockdata.cmd) - 1] = '\0';
                    620:                        system(lockdata.cmd);
1.25      nicm      621:                        client_write_server(MSG_UNLOCK, NULL, 0);
1.9       nicm      622:                        break;
                    623:                default:
                    624:                        fatalx("unexpected message");
                    625:                }
1.12      nicm      626:
                    627:                imsg_free(&imsg);
1.9       nicm      628:        }
1.1       nicm      629: }