[BACK]Return to tmux.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/tmux.c, Revision 1.48

1.48    ! nicm        1: /* $OpenBSD: tmux.c,v 1.47 2009/10/09 07:27:00 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>
                     20: #include <sys/stat.h>
                     21:
                     22: #include <errno.h>
                     23: #include <paths.h>
                     24: #include <pwd.h>
                     25: #include <signal.h>
                     26: #include <stdlib.h>
                     27: #include <string.h>
                     28: #include <syslog.h>
                     29: #include <unistd.h>
                     30:
                     31: #include "tmux.h"
                     32:
                     33: #ifdef DEBUG
                     34: const char     *malloc_options = "AFGJPX";
                     35: #endif
                     36:
                     37: volatile sig_atomic_t sigwinch;
                     38: volatile sig_atomic_t sigterm;
                     39: volatile sig_atomic_t sigcont;
                     40: volatile sig_atomic_t sigchld;
                     41: volatile sig_atomic_t sigusr1;
                     42: volatile sig_atomic_t sigusr2;
                     43:
                     44: char           *cfg_file;
1.12      nicm       45: struct options  global_s_options;      /* session options */
                     46: struct options  global_w_options;      /* window options */
1.29      nicm       47: struct environ  global_environ;
1.1       nicm       48:
                     49: int             debug_level;
                     50: int             be_quiet;
                     51: time_t          start_time;
                     52: char           *socket_path;
1.41      nicm       53: int             login_shell;
1.1       nicm       54:
                     55: __dead void     usage(void);
                     56: char           *makesockpath(const char *);
1.23      nicm       57: int             prepare_cmd(enum msgtype *, void **, size_t *, int, char **);
1.46      nicm       58: int             dispatch_imsg(struct client_ctx *, const char *, int *);
                     59: __dead void     shell_exec(const char *, const char *);
1.1       nicm       60:
                     61: __dead void
                     62: usage(void)
                     63: {
1.4       sobrado    64:        fprintf(stderr,
1.46      nicm       65:            "usage: %s [-28dlquv] [-c shell-command] [-f file] [-L socket-name]\n"
1.41      nicm       66:            "            [-S socket-path] [command [flags]]\n",
1.1       nicm       67:            __progname);
                     68:        exit(1);
                     69: }
                     70:
                     71: void
                     72: logfile(const char *name)
                     73: {
                     74:        char    *path;
                     75:
                     76:        log_close();
                     77:        if (debug_level > 0) {
1.32      nicm       78:                xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid());
1.1       nicm       79:                log_open_file(debug_level, path);
                     80:                xfree(path);
                     81:        }
                     82: }
                     83:
                     84: void
                     85: sighandler(int sig)
                     86: {
                     87:        int     saved_errno;
                     88:
                     89:        saved_errno = errno;
                     90:        switch (sig) {
                     91:        case SIGWINCH:
                     92:                sigwinch = 1;
                     93:                break;
                     94:        case SIGTERM:
                     95:                sigterm = 1;
                     96:                break;
                     97:        case SIGCHLD:
                     98:                sigchld = 1;
                     99:                break;
                    100:        case SIGCONT:
                    101:                sigcont = 1;
                    102:                break;
                    103:        case SIGUSR1:
                    104:                sigusr1 = 1;
                    105:                break;
                    106:        case SIGUSR2:
                    107:                sigusr2 = 1;
                    108:                break;
                    109:        }
                    110:        errno = saved_errno;
                    111: }
                    112:
                    113: void
                    114: siginit(void)
                    115: {
                    116:        struct sigaction         act;
                    117:
                    118:        memset(&act, 0, sizeof act);
                    119:        sigemptyset(&act.sa_mask);
                    120:        act.sa_flags = SA_RESTART;
                    121:
                    122:        act.sa_handler = SIG_IGN;
                    123:        if (sigaction(SIGPIPE, &act, NULL) != 0)
                    124:                fatal("sigaction failed");
                    125:        if (sigaction(SIGINT, &act, NULL) != 0)
                    126:                fatal("sigaction failed");
                    127:        if (sigaction(SIGTSTP, &act, NULL) != 0)
                    128:                fatal("sigaction failed");
                    129:        if (sigaction(SIGQUIT, &act, NULL) != 0)
                    130:                fatal("sigaction failed");
                    131:
                    132:        act.sa_handler = sighandler;
                    133:        if (sigaction(SIGWINCH, &act, NULL) != 0)
                    134:                fatal("sigaction failed");
                    135:        if (sigaction(SIGTERM, &act, NULL) != 0)
                    136:                fatal("sigaction failed");
                    137:        if (sigaction(SIGCHLD, &act, NULL) != 0)
                    138:                fatal("sigaction failed");
                    139:        if (sigaction(SIGUSR1, &act, NULL) != 0)
                    140:                fatal("sigaction failed");
                    141:        if (sigaction(SIGUSR2, &act, NULL) != 0)
                    142:                fatal("sigaction failed");
                    143: }
                    144:
                    145: void
                    146: sigreset(void)
                    147: {
                    148:        struct sigaction act;
                    149:
                    150:        memset(&act, 0, sizeof act);
                    151:        sigemptyset(&act.sa_mask);
                    152:
                    153:        act.sa_handler = SIG_DFL;
                    154:        if (sigaction(SIGPIPE, &act, NULL) != 0)
                    155:                fatal("sigaction failed");
                    156:        if (sigaction(SIGUSR1, &act, NULL) != 0)
                    157:                fatal("sigaction failed");
                    158:        if (sigaction(SIGUSR2, &act, NULL) != 0)
                    159:                fatal("sigaction failed");
                    160:        if (sigaction(SIGINT, &act, NULL) != 0)
                    161:                fatal("sigaction failed");
                    162:        if (sigaction(SIGTSTP, &act, NULL) != 0)
                    163:                fatal("sigaction failed");
                    164:        if (sigaction(SIGQUIT, &act, NULL) != 0)
                    165:                fatal("sigaction failed");
                    166:        if (sigaction(SIGWINCH, &act, NULL) != 0)
                    167:                fatal("sigaction failed");
                    168:        if (sigaction(SIGTERM, &act, NULL) != 0)
                    169:                fatal("sigaction failed");
                    170:        if (sigaction(SIGCHLD, &act, NULL) != 0)
                    171:                fatal("sigaction failed");
                    172: }
                    173:
1.39      nicm      174: const char *
                    175: getshell(void)
                    176: {
                    177:        struct passwd   *pw;
                    178:        const char      *shell;
                    179:
                    180:        shell = getenv("SHELL");
                    181:        if (checkshell(shell))
                    182:                return (shell);
                    183:
                    184:        pw = getpwuid(getuid());
                    185:        if (pw != NULL && checkshell(pw->pw_shell))
                    186:                return (pw->pw_shell);
                    187:
                    188:        return (_PATH_BSHELL);
                    189: }
                    190:
                    191: int
                    192: checkshell(const char *shell)
                    193: {
                    194:        if (shell == NULL || *shell == '\0' || areshell(shell))
                    195:                return (0);
                    196:        if (access(shell, X_OK) != 0)
                    197:                return (0);
                    198:        return (1);
                    199: }
                    200:
                    201: int
                    202: areshell(const char *shell)
                    203: {
                    204:        const char      *progname, *ptr;
                    205:
                    206:        if ((ptr = strrchr(shell, '/')) != NULL)
                    207:                ptr++;
                    208:        else
                    209:                ptr = shell;
                    210:        progname = __progname;
                    211:        if (*progname == '-')
                    212:                progname++;
                    213:        if (strcmp(ptr, progname) == 0)
                    214:                return (1);
                    215:        return (0);
                    216: }
                    217:
1.1       nicm      218: char *
                    219: makesockpath(const char *label)
                    220: {
                    221:        char            base[MAXPATHLEN], *path;
                    222:        struct stat     sb;
                    223:        u_int           uid;
                    224:
                    225:        uid = getuid();
1.32      nicm      226:        xsnprintf(base, MAXPATHLEN, "%s/tmux-%d", _PATH_TMP, uid);
1.1       nicm      227:
                    228:        if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
                    229:                return (NULL);
                    230:
                    231:        if (lstat(base, &sb) != 0)
                    232:                return (NULL);
                    233:        if (!S_ISDIR(sb.st_mode)) {
                    234:                errno = ENOTDIR;
                    235:                return (NULL);
                    236:        }
                    237:        if (sb.st_uid != uid || (sb.st_mode & (S_IRWXG|S_IRWXO)) != 0) {
                    238:                errno = EACCES;
                    239:                return (NULL);
                    240:        }
                    241:
                    242:        xasprintf(&path, "%s/%s", base, label);
                    243:        return (path);
                    244: }
                    245:
                    246: int
1.23      nicm      247: prepare_cmd(enum msgtype *msg, void **buf, size_t *len, int argc, char **argv)
1.21      nicm      248: {
                    249:        static struct msg_command_data   cmddata;
                    250:
                    251:        client_fill_session(&cmddata);
                    252:
                    253:        cmddata.argc = argc;
                    254:        if (cmd_pack_argv(argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
                    255:                log_warnx("command too long");
                    256:                return (-1);
                    257:        }
                    258:
                    259:        *buf = &cmddata;
                    260:        *len = sizeof cmddata;
                    261:
                    262:        *msg = MSG_COMMAND;
                    263:        return (0);
                    264: }
                    265:
                    266: int
1.1       nicm      267: main(int argc, char **argv)
                    268: {
                    269:        struct client_ctx        cctx;
                    270:        struct cmd_list         *cmdlist;
                    271:        struct cmd              *cmd;
                    272:        struct pollfd            pfd;
1.23      nicm      273:        enum msgtype             msg;
1.1       nicm      274:        struct passwd           *pw;
1.42      nicm      275:        struct options          *so, *wo;
1.44      nicm      276:        struct keylist          *keylist;
1.46      nicm      277:        char                    *s, *shellcmd, *path, *label, *home, *cause;
                    278:        char                     cwd[MAXPATHLEN], **var;
1.21      nicm      279:        void                    *buf;
                    280:        size_t                   len;
1.45      nicm      281:        int                      retcode, opt, flags, cmdflags = 0;
1.31      nicm      282:        int                      nfds;
1.1       nicm      283:
1.45      nicm      284:        flags = 0;
1.46      nicm      285:        shellcmd = label = path = NULL;
1.41      nicm      286:        login_shell = (**argv == '-');
1.46      nicm      287:        while ((opt = getopt(argc, argv, "28c:df:lL:qS:uUv")) != -1) {
1.41      nicm      288:                switch (opt) {
1.1       nicm      289:                case '2':
                    290:                        flags |= IDENTIFY_256COLOURS;
                    291:                        flags &= ~IDENTIFY_88COLOURS;
                    292:                        break;
                    293:                case '8':
                    294:                        flags |= IDENTIFY_88COLOURS;
                    295:                        flags &= ~IDENTIFY_256COLOURS;
                    296:                        break;
1.46      nicm      297:                case 'c':
                    298:                        if (shellcmd != NULL)
                    299:                                xfree(shellcmd);
                    300:                        shellcmd = xstrdup(optarg);
                    301:                        break;
1.37      nicm      302:                case 'd':
                    303:                        flags |= IDENTIFY_HASDEFAULTS;
                    304:                        break;
1.1       nicm      305:                case 'f':
1.42      nicm      306:                        if (cfg_file != NULL)
1.2       ray       307:                                xfree(cfg_file);
1.1       nicm      308:                        cfg_file = xstrdup(optarg);
1.41      nicm      309:                        break;
                    310:                case 'l':
                    311:                        login_shell = 1;
1.1       nicm      312:                        break;
                    313:                case 'L':
                    314:                        if (label != NULL)
                    315:                                xfree(label);
                    316:                        label = xstrdup(optarg);
                    317:                        break;
1.37      nicm      318:                case 'q':
                    319:                        be_quiet = 1;
                    320:                        break;
1.1       nicm      321:                case 'S':
                    322:                        if (path != NULL)
                    323:                                xfree(path);
                    324:                        path = xstrdup(optarg);
                    325:                        break;
                    326:                case 'u':
                    327:                        flags |= IDENTIFY_UTF8;
                    328:                        break;
                    329:                case 'v':
                    330:                        debug_level++;
                    331:                        break;
                    332:                 default:
                    333:                        usage();
                    334:                 }
                    335:         }
                    336:        argc -= optind;
                    337:        argv += optind;
                    338:
1.46      nicm      339:        if (shellcmd != NULL && argc != 0)
                    340:                usage();
                    341:
1.1       nicm      342:        log_open_tty(debug_level);
                    343:        siginit();
                    344:
1.15      nicm      345:        if (!(flags & IDENTIFY_UTF8)) {
                    346:                /*
                    347:                 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG
                    348:                 * exist (in that order) to contain UTF-8, it is a safe
                    349:                 * assumption that either they are using a UTF-8 terminal, or
                    350:                 * if not they know that output from UTF-8-capable programs may
                    351:                 * be wrong.
                    352:                 */
                    353:                if ((s = getenv("LC_ALL")) == NULL) {
                    354:                        if ((s = getenv("LC_CTYPE")) == NULL)
                    355:                                s = getenv("LANG");
                    356:                }
1.26      nicm      357:                if (s != NULL && (strcasestr(s, "UTF-8") != NULL ||
                    358:                    strcasestr(s, "UTF8") != NULL))
1.15      nicm      359:                        flags |= IDENTIFY_UTF8;
                    360:        }
                    361:
1.42      nicm      362:        environ_init(&global_environ);
                    363:        for (var = environ; *var != NULL; var++)
                    364:                environ_put(&global_environ, *var);
                    365:
1.12      nicm      366:        options_init(&global_s_options, NULL);
1.42      nicm      367:        so = &global_s_options;
                    368:        options_set_number(so, "base-index", 0);
                    369:        options_set_number(so, "bell-action", BELL_ANY);
                    370:        options_set_number(so, "buffer-limit", 9);
                    371:        options_set_string(so, "default-command", "%s", "");
                    372:        options_set_string(so, "default-shell", "%s", getshell());
                    373:        options_set_string(so, "default-terminal", "screen");
                    374:        options_set_number(so, "display-panes-colour", 4);
                    375:        options_set_number(so, "display-panes-time", 1000);
                    376:        options_set_number(so, "display-time", 750);
                    377:        options_set_number(so, "history-limit", 2000);
                    378:        options_set_number(so, "lock-after-time", 0);
1.45      nicm      379:        options_set_string(so, "lock-command", "lock -np");
1.48    ! nicm      380:        options_set_number(so, "lock-server", 1);
1.42      nicm      381:        options_set_number(so, "message-attr", 0);
                    382:        options_set_number(so, "message-bg", 3);
                    383:        options_set_number(so, "message-fg", 0);
                    384:        options_set_number(so, "repeat-time", 500);
                    385:        options_set_number(so, "set-remain-on-exit", 0);
                    386:        options_set_number(so, "set-titles", 0);
1.43      nicm      387:        options_set_string(so, "set-titles-string", "#S:#I:#W - \"#T\"");
1.42      nicm      388:        options_set_number(so, "status", 1);
                    389:        options_set_number(so, "status-attr", 0);
                    390:        options_set_number(so, "status-bg", 2);
                    391:        options_set_number(so, "status-fg", 0);
                    392:        options_set_number(so, "status-interval", 15);
                    393:        options_set_number(so, "status-justify", 0);
                    394:        options_set_number(so, "status-keys", MODEKEY_EMACS);
                    395:        options_set_string(so, "status-left", "[#S]");
                    396:        options_set_number(so, "status-left-attr", 0);
                    397:        options_set_number(so, "status-left-bg", 8);
                    398:        options_set_number(so, "status-left-fg", 8);
                    399:        options_set_number(so, "status-left-length", 10);
                    400:        options_set_string(so, "status-right", "\"#22T\" %%H:%%M %%d-%%b-%%y");
                    401:        options_set_number(so, "status-right-attr", 0);
                    402:        options_set_number(so, "status-right-bg", 8);
                    403:        options_set_number(so, "status-right-fg", 8);
                    404:        options_set_number(so, "status-right-length", 40);
                    405:        options_set_string(so, "terminal-overrides",
                    406:            "*88col*:colors=88,*256col*:colors=256");
                    407:        options_set_string(so, "update-environment", "DISPLAY "
1.35      nicm      408:            "WINDOWID SSH_ASKPASS SSH_AUTH_SOCK SSH_AGENT_PID SSH_CONNECTION");
1.42      nicm      409:        options_set_number(so, "visual-activity", 0);
                    410:        options_set_number(so, "visual-bell", 0);
                    411:        options_set_number(so, "visual-content", 0);
1.44      nicm      412:
                    413:        keylist = xmalloc(sizeof *keylist);
                    414:        ARRAY_INIT(keylist);
                    415:        ARRAY_ADD(keylist, '\002');
                    416:        options_set_data(so, "prefix", keylist, xfree);
1.1       nicm      417:
1.12      nicm      418:        options_init(&global_w_options, NULL);
1.42      nicm      419:        wo = &global_w_options;
                    420:        options_set_number(wo, "aggressive-resize", 0);
                    421:        options_set_number(wo, "automatic-rename", 1);
                    422:        options_set_number(wo, "clock-mode-colour", 4);
                    423:        options_set_number(wo, "clock-mode-style", 1);
                    424:        options_set_number(wo, "force-height", 0);
                    425:        options_set_number(wo, "force-width", 0);
                    426:        options_set_number(wo, "main-pane-height", 24);
                    427:        options_set_number(wo, "main-pane-width", 81);
                    428:        options_set_number(wo, "mode-attr", 0);
                    429:        options_set_number(wo, "mode-bg", 3);
                    430:        options_set_number(wo, "mode-fg", 0);
                    431:        options_set_number(wo, "mode-keys", MODEKEY_EMACS);
                    432:        options_set_number(wo, "mode-mouse", 0);
                    433:        options_set_number(wo, "monitor-activity", 0);
                    434:        options_set_string(wo, "monitor-content", "%s", "");
                    435:        options_set_number(wo, "window-status-attr", 0);
                    436:        options_set_number(wo, "window-status-bg", 8);
                    437:        options_set_number(wo, "window-status-current-attr", 0);
                    438:        options_set_number(wo, "window-status-current-bg", 8);
                    439:        options_set_number(wo, "window-status-current-fg", 8);
                    440:        options_set_number(wo, "window-status-fg", 8);
                    441:        options_set_number(wo, "xterm-keys", 0);
                    442:        options_set_number(wo, "remain-on-exit", 0);
1.47      nicm      443:        options_set_number(wo, "synchronize-panes", 0);
1.42      nicm      444:
                    445:        if (flags & IDENTIFY_UTF8) {
                    446:                options_set_number(so, "status-utf8", 1);
                    447:                options_set_number(wo, "utf8", 1);
                    448:        } else {
                    449:                options_set_number(so, "status-utf8", 0);
                    450:                options_set_number(wo, "utf8", 0);
                    451:        }
                    452:
                    453:        if (getcwd(cwd, sizeof cwd) == NULL) {
                    454:                pw = getpwuid(getuid());
                    455:                if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
                    456:                        strlcpy(cwd, pw->pw_dir, sizeof cwd);
                    457:                else
                    458:                        strlcpy(cwd, "/", sizeof cwd);
                    459:        }
                    460:        options_set_string(so, "default-path", "%s", cwd);
1.1       nicm      461:
                    462:        if (cfg_file == NULL) {
                    463:                home = getenv("HOME");
                    464:                if (home == NULL || *home == '\0') {
                    465:                        pw = getpwuid(getuid());
                    466:                        if (pw != NULL)
                    467:                                home = pw->pw_dir;
                    468:                }
                    469:                xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG);
                    470:                if (access(cfg_file, R_OK) != 0) {
                    471:                        xfree(cfg_file);
                    472:                        cfg_file = NULL;
                    473:                }
                    474:        } else {
                    475:                if (access(cfg_file, R_OK) != 0) {
                    476:                        log_warn("%s", cfg_file);
                    477:                        exit(1);
                    478:                }
                    479:        }
1.21      nicm      480:
1.1       nicm      481:        if (label == NULL)
                    482:                label = xstrdup("default");
                    483:        if (path == NULL && (path = makesockpath(label)) == NULL) {
                    484:                log_warn("can't create socket");
                    485:                exit(1);
                    486:        }
                    487:        xfree(label);
                    488:
1.46      nicm      489:        if (shellcmd != NULL) {
                    490:                msg = MSG_SHELL;
                    491:                buf = NULL;
                    492:                len = 0;
                    493:        } else if (prepare_cmd(&msg, &buf, &len, argc, argv) != 0)
1.45      nicm      494:                exit(1);
1.21      nicm      495:
1.46      nicm      496:        if (shellcmd != NULL)
                    497:                cmdflags |= CMD_STARTSERVER;
                    498:        else if (argc == 0)     /* new-session is the default */
1.30      nicm      499:                cmdflags |= CMD_STARTSERVER|CMD_SENDENVIRON;
1.21      nicm      500:        else {
                    501:                /*
                    502:                 * It sucks parsing the command string twice (in client and
                    503:                 * later in server) but it is necessary to get the start server
                    504:                 * flag.
                    505:                 */
                    506:                if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
                    507:                        log_warnx("%s", cause);
                    508:                        exit(1);
1.1       nicm      509:                }
1.21      nicm      510:                cmdflags &= ~CMD_STARTSERVER;
1.1       nicm      511:                TAILQ_FOREACH(cmd, cmdlist, qentry) {
1.29      nicm      512:                        if (cmd->entry->flags & CMD_STARTSERVER)
1.20      nicm      513:                                cmdflags |= CMD_STARTSERVER;
1.29      nicm      514:                        if (cmd->entry->flags & CMD_SENDENVIRON)
                    515:                                cmdflags |= CMD_SENDENVIRON;
1.1       nicm      516:                }
1.21      nicm      517:                cmd_list_free(cmdlist);
1.1       nicm      518:        }
                    519:
                    520:        memset(&cctx, 0, sizeof cctx);
1.20      nicm      521:        if (client_init(path, &cctx, cmdflags, flags) != 0)
1.1       nicm      522:                exit(1);
                    523:        xfree(path);
                    524:
1.21      nicm      525:        client_write_server(&cctx, msg, buf, len);
                    526:        memset(buf, 0, len);
1.1       nicm      527:
                    528:        retcode = 0;
                    529:        for (;;) {
1.31      nicm      530:                pfd.fd = cctx.ibuf.fd;
1.1       nicm      531:                pfd.events = POLLIN;
1.31      nicm      532:                if (cctx.ibuf.w.queued != 0)
1.1       nicm      533:                        pfd.events |= POLLOUT;
                    534:
1.31      nicm      535:                if ((nfds = poll(&pfd, 1, INFTIM)) == -1) {
1.1       nicm      536:                        if (errno == EAGAIN || errno == EINTR)
                    537:                                continue;
                    538:                        fatal("poll failed");
                    539:                }
1.31      nicm      540:                if (nfds == 0)
                    541:                        continue;
                    542:
                    543:                if (pfd.revents & (POLLERR|POLLHUP|POLLNVAL))
                    544:                        fatalx("socket error");
                    545:
                    546:                 if (pfd.revents & POLLIN) {
1.46      nicm      547:                        if (dispatch_imsg(&cctx, shellcmd, &retcode) != 0)
1.31      nicm      548:                                break;
                    549:                }
                    550:
                    551:                if (pfd.revents & POLLOUT) {
                    552:                        if (msgbuf_write(&cctx.ibuf.w) < 0)
                    553:                                fatalx("msgbuf_write failed");
                    554:                }
                    555:        }
                    556:
                    557:        options_free(&global_s_options);
                    558:        options_free(&global_w_options);
                    559:
                    560:        return (retcode);
                    561: }
                    562:
                    563: int
1.46      nicm      564: dispatch_imsg(struct client_ctx *cctx, const char *shellcmd, int *retcode)
1.31      nicm      565: {
                    566:        struct imsg             imsg;
                    567:        ssize_t                 n, datalen;
                    568:        struct msg_print_data   printdata;
1.46      nicm      569:        struct msg_shell_data   shelldata;
1.1       nicm      570:
1.31      nicm      571:         if ((n = imsg_read(&cctx->ibuf)) == -1 || n == 0)
                    572:                fatalx("imsg_read failed");
1.1       nicm      573:
1.31      nicm      574:        for (;;) {
                    575:                if ((n = imsg_get(&cctx->ibuf, &imsg)) == -1)
                    576:                        fatalx("imsg_get failed");
                    577:                if (n == 0)
                    578:                        return (0);
                    579:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.1       nicm      580:
1.31      nicm      581:                switch (imsg.hdr.type) {
1.1       nicm      582:                case MSG_EXIT:
                    583:                case MSG_SHUTDOWN:
1.31      nicm      584:                        if (datalen != 0)
                    585:                                fatalx("bad MSG_EXIT size");
                    586:
                    587:                        return (-1);
1.1       nicm      588:                case MSG_ERROR:
1.31      nicm      589:                        *retcode = 1;
1.1       nicm      590:                        /* FALLTHROUGH */
                    591:                case MSG_PRINT:
1.31      nicm      592:                        if (datalen != sizeof printdata)
1.1       nicm      593:                                fatalx("bad MSG_PRINT size");
1.31      nicm      594:                        memcpy(&printdata, imsg.data, sizeof printdata);
                    595:                        printdata.msg[(sizeof printdata.msg) - 1] = '\0';
1.21      nicm      596:
                    597:                        log_info("%s", printdata.msg);
1.31      nicm      598:                        break;
1.1       nicm      599:                case MSG_READY:
1.31      nicm      600:                        if (datalen != 0)
                    601:                                fatalx("bad MSG_READY size");
                    602:
                    603:                        *retcode = client_main(cctx);
                    604:                        return (-1);
                    605:                case MSG_VERSION:
                    606:                        if (datalen != 0)
                    607:                                fatalx("bad MSG_VERSION size");
                    608:
                    609:                        log_warnx("protocol version mismatch (client %u, "
                    610:                            "server %u)", PROTOCOL_VERSION, imsg.hdr.peerid);
                    611:                        *retcode = 1;
                    612:                        return (-1);
1.46      nicm      613:                case MSG_SHELL:
                    614:                        if (datalen != sizeof shelldata)
                    615:                                fatalx("bad MSG_SHELL size");
                    616:                        memcpy(&shelldata, imsg.data, sizeof shelldata);
                    617:                        shelldata.shell[(sizeof shelldata.shell) - 1] = '\0';
                    618:
                    619:                        shell_exec(shelldata.shell, shellcmd);
1.1       nicm      620:                default:
1.31      nicm      621:                        fatalx("unexpected message");
1.1       nicm      622:                }
1.31      nicm      623:
                    624:                imsg_free(&imsg);
1.1       nicm      625:        }
1.46      nicm      626: }
                    627:
                    628: __dead void
                    629: shell_exec(const char *shell, const char *shellcmd)
                    630: {
                    631:        const char      *shellname, *ptr;
                    632:        char            *argv0;
                    633:
                    634:        sigreset();
                    635:
                    636:        ptr = strrchr(shell, '/');
                    637:        if (ptr != NULL && *(ptr + 1) != '\0')
                    638:                shellname = ptr + 1;
                    639:        else
                    640:                shellname = shell;
                    641:        if (login_shell)
                    642:                xasprintf(&argv0, "-%s", shellname);
                    643:        else
                    644:                xasprintf(&argv0, "%s", shellname);
                    645:        setenv("SHELL", shell, 1);
                    646:
                    647:        execl(shell, argv0, "-c", shellcmd, (char *) NULL);
                    648:        fatal("execl failed");
1.1       nicm      649: }