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

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