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

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