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

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