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

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