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

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