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

1.32    ! nicm        1: /* $OpenBSD: tmux.c,v 1.31 2009/08/11 17:18: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.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:
                    178: char *
                    179: makesockpath(const char *label)
                    180: {
                    181:        char            base[MAXPATHLEN], *path;
                    182:        struct stat     sb;
                    183:        u_int           uid;
                    184:
                    185:        uid = getuid();
1.32    ! nicm      186:        xsnprintf(base, MAXPATHLEN, "%s/tmux-%d", _PATH_TMP, uid);
1.1       nicm      187:
                    188:        if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
                    189:                return (NULL);
                    190:
                    191:        if (lstat(base, &sb) != 0)
                    192:                return (NULL);
                    193:        if (!S_ISDIR(sb.st_mode)) {
                    194:                errno = ENOTDIR;
                    195:                return (NULL);
                    196:        }
                    197:        if (sb.st_uid != uid || (sb.st_mode & (S_IRWXG|S_IRWXO)) != 0) {
                    198:                errno = EACCES;
                    199:                return (NULL);
                    200:        }
                    201:
                    202:        xasprintf(&path, "%s/%s", base, label);
                    203:        return (path);
                    204: }
                    205:
                    206: int
1.23      nicm      207: prepare_unlock(enum msgtype *msg, void **buf, size_t *len, int argc)
1.21      nicm      208: {
                    209:        static struct msg_unlock_data    unlockdata;
                    210:        char                            *pass;
                    211:
                    212:        if (argc != 0) {
                    213:                log_warnx("can't specify a command when unlocking");
                    214:                return (-1);
                    215:        }
                    216:
                    217:        if ((pass = getpass("Password: ")) == NULL)
                    218:                return (-1);
                    219:
                    220:        if (strlen(pass) >= sizeof unlockdata.pass) {
                    221:                log_warnx("password too long");
                    222:                return (-1);
                    223:        }
                    224:
                    225:        strlcpy(unlockdata.pass, pass, sizeof unlockdata.pass);
                    226:        memset(pass, 0, strlen(pass));
                    227:
                    228:        *buf = &unlockdata;
                    229:        *len = sizeof unlockdata;
                    230:
                    231:        *msg = MSG_UNLOCK;
                    232:        return (0);
                    233: }
                    234:
                    235: int
1.23      nicm      236: prepare_cmd(enum msgtype *msg, void **buf, size_t *len, int argc, char **argv)
1.21      nicm      237: {
                    238:        static struct msg_command_data   cmddata;
                    239:
                    240:        client_fill_session(&cmddata);
                    241:
                    242:        cmddata.argc = argc;
                    243:        if (cmd_pack_argv(argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
                    244:                log_warnx("command too long");
                    245:                return (-1);
                    246:        }
                    247:
                    248:        *buf = &cmddata;
                    249:        *len = sizeof cmddata;
                    250:
                    251:        *msg = MSG_COMMAND;
                    252:        return (0);
                    253: }
                    254:
                    255: int
1.1       nicm      256: main(int argc, char **argv)
                    257: {
                    258:        struct client_ctx        cctx;
                    259:        struct cmd_list         *cmdlist;
                    260:        struct cmd              *cmd;
                    261:        struct pollfd            pfd;
1.23      nicm      262:        enum msgtype             msg;
1.1       nicm      263:        struct passwd           *pw;
1.29      nicm      264:        char                    *s, *path, *label, *home, *cause, **var;
1.1       nicm      265:        char                     cwd[MAXPATHLEN];
1.21      nicm      266:        void                    *buf;
                    267:        size_t                   len;
1.20      nicm      268:        int                      retcode, opt, flags, unlock, cmdflags = 0;
1.31      nicm      269:        int                      nfds;
1.1       nicm      270:
                    271:        unlock = flags = 0;
                    272:        label = path = NULL;
                    273:         while ((opt = getopt(argc, argv, "28df:L:qS:uUv")) != -1) {
                    274:                 switch (opt) {
                    275:                case '2':
                    276:                        flags |= IDENTIFY_256COLOURS;
                    277:                        flags &= ~IDENTIFY_88COLOURS;
                    278:                        break;
                    279:                case '8':
                    280:                        flags |= IDENTIFY_88COLOURS;
                    281:                        flags &= ~IDENTIFY_256COLOURS;
                    282:                        break;
                    283:                case 'f':
1.2       ray       284:                        if (cfg_file)
                    285:                                xfree(cfg_file);
1.1       nicm      286:                        cfg_file = xstrdup(optarg);
                    287:                        break;
                    288:                case 'L':
                    289:                        if (label != NULL)
                    290:                                xfree(label);
                    291:                        label = xstrdup(optarg);
                    292:                        break;
                    293:                case 'S':
                    294:                        if (path != NULL)
                    295:                                xfree(path);
                    296:                        path = xstrdup(optarg);
                    297:                        break;
                    298:                case 'q':
                    299:                        be_quiet = 1;
                    300:                        break;
                    301:                case 'u':
                    302:                        flags |= IDENTIFY_UTF8;
                    303:                        break;
                    304:                case 'U':
                    305:                        unlock = 1;
                    306:                        break;
                    307:                case 'd':
                    308:                        flags |= IDENTIFY_HASDEFAULTS;
                    309:                        break;
                    310:                case 'v':
                    311:                        debug_level++;
                    312:                        break;
                    313:                 default:
                    314:                        usage();
                    315:                 }
                    316:         }
                    317:        argc -= optind;
                    318:        argv += optind;
                    319:
                    320:        log_open_tty(debug_level);
                    321:        siginit();
                    322:
1.29      nicm      323:        environ_init(&global_environ);
                    324:        for (var = environ; *var != NULL; var++)
                    325:                environ_put(&global_environ, *var);
                    326:
1.15      nicm      327:        if (!(flags & IDENTIFY_UTF8)) {
                    328:                /*
                    329:                 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG
                    330:                 * exist (in that order) to contain UTF-8, it is a safe
                    331:                 * assumption that either they are using a UTF-8 terminal, or
                    332:                 * if not they know that output from UTF-8-capable programs may
                    333:                 * be wrong.
                    334:                 */
                    335:                if ((s = getenv("LC_ALL")) == NULL) {
                    336:                        if ((s = getenv("LC_CTYPE")) == NULL)
                    337:                                s = getenv("LANG");
                    338:                }
1.26      nicm      339:                if (s != NULL && (strcasestr(s, "UTF-8") != NULL ||
                    340:                    strcasestr(s, "UTF8") != NULL))
1.15      nicm      341:                        flags |= IDENTIFY_UTF8;
                    342:        }
                    343:
1.12      nicm      344:        options_init(&global_s_options, NULL);
                    345:        options_set_number(&global_s_options, "bell-action", BELL_ANY);
                    346:        options_set_number(&global_s_options, "buffer-limit", 9);
1.13      nicm      347:        options_set_string(&global_s_options, "default-command", "%s", "");
1.14      nicm      348:        options_set_string(&global_s_options, "default-terminal", "screen");
1.12      nicm      349:        options_set_number(&global_s_options, "display-time", 750);
                    350:        options_set_number(&global_s_options, "history-limit", 2000);
                    351:        options_set_number(&global_s_options, "lock-after-time", 0);
1.27      nicm      352:        options_set_number(&global_s_options, "message-attr", 0);
1.12      nicm      353:        options_set_number(&global_s_options, "message-bg", 3);
                    354:        options_set_number(&global_s_options, "message-fg", 0);
                    355:        options_set_number(&global_s_options, "prefix", '\002');
                    356:        options_set_number(&global_s_options, "repeat-time", 500);
                    357:        options_set_number(&global_s_options, "set-remain-on-exit", 0);
                    358:        options_set_number(&global_s_options, "set-titles", 0);
                    359:        options_set_number(&global_s_options, "status", 1);
1.27      nicm      360:        options_set_number(&global_s_options, "status-attr", 0);
1.12      nicm      361:        options_set_number(&global_s_options, "status-bg", 2);
                    362:        options_set_number(&global_s_options, "status-fg", 0);
                    363:        options_set_number(&global_s_options, "status-interval", 15);
                    364:        options_set_number(&global_s_options, "status-keys", MODEKEY_EMACS);
1.18      nicm      365:        options_set_number(&global_s_options, "status-justify", 0);
1.28      nicm      366:        options_set_string(&global_s_options, "status-left", "[#S]");
                    367:        options_set_number(&global_s_options, "status-left-attr", 0);
                    368:        options_set_number(&global_s_options, "status-left-fg", 8);
                    369:        options_set_number(&global_s_options, "status-left-bg", 8);
1.12      nicm      370:        options_set_number(&global_s_options, "status-left-length", 10);
1.1       nicm      371:        options_set_string(
1.22      nicm      372:            &global_s_options, "status-right", "\"#22T\" %%H:%%M %%d-%%b-%%y");
1.28      nicm      373:        options_set_number(&global_s_options, "status-right-attr", 0);
                    374:        options_set_number(&global_s_options, "status-right-fg", 8);
                    375:        options_set_number(&global_s_options, "status-right-bg", 8);
                    376:        options_set_number(&global_s_options, "status-right-length", 40);
1.15      nicm      377:        if (flags & IDENTIFY_UTF8)
                    378:                options_set_number(&global_s_options, "status-utf8", 1);
                    379:        else
                    380:                options_set_number(&global_s_options, "status-utf8", 0);
1.25      nicm      381:        options_set_string(&global_s_options,
                    382:            "terminal-overrides", "*88col*:colors=88,*256col*:colors=256");
1.29      nicm      383:        options_set_string(&global_s_options, "update-environment", "DISPLAY");
1.16      nicm      384:        options_set_number(&global_s_options, "visual-activity", 0);
                    385:        options_set_number(&global_s_options, "visual-bell", 0);
                    386:        options_set_number(&global_s_options, "visual-content", 0);
1.1       nicm      387:
1.12      nicm      388:        options_init(&global_w_options, NULL);
                    389:        options_set_number(&global_w_options, "aggressive-resize", 0);
                    390:        options_set_number(&global_w_options, "automatic-rename", 1);
                    391:        options_set_number(&global_w_options, "clock-mode-colour", 4);
                    392:        options_set_number(&global_w_options, "clock-mode-style", 1);
                    393:        options_set_number(&global_w_options, "force-height", 0);
                    394:        options_set_number(&global_w_options, "force-width", 0);
                    395:        options_set_number(&global_w_options, "main-pane-width", 81);
                    396:        options_set_number(&global_w_options, "main-pane-height", 24);
1.27      nicm      397:        options_set_number(&global_w_options, "mode-attr", 0);
1.12      nicm      398:        options_set_number(&global_w_options, "mode-bg", 3);
                    399:        options_set_number(&global_w_options, "mode-fg", 0);
                    400:        options_set_number(&global_w_options, "mode-keys", MODEKEY_EMACS);
1.24      nicm      401:        options_set_number(&global_w_options, "mode-mouse", 1);
1.12      nicm      402:        options_set_number(&global_w_options, "monitor-activity", 0);
                    403:        options_set_string(&global_w_options, "monitor-content", "%s", "");
1.15      nicm      404:        if (flags & IDENTIFY_UTF8)
                    405:                options_set_number(&global_w_options, "utf8", 1);
                    406:        else
                    407:                options_set_number(&global_w_options, "utf8", 0);
1.12      nicm      408:        options_set_number(&global_w_options, "window-status-attr", 0);
                    409:        options_set_number(&global_w_options, "window-status-bg", 8);
                    410:        options_set_number(&global_w_options, "window-status-fg", 8);
1.17      nicm      411:        options_set_number(&global_w_options, "window-status-current-attr", 0);
                    412:        options_set_number(&global_w_options, "window-status-current-bg", 8);
                    413:        options_set_number(&global_w_options, "window-status-current-fg", 8);
1.12      nicm      414:        options_set_number(&global_w_options, "xterm-keys", 0);
                    415:        options_set_number(&global_w_options, "remain-on-exit", 0);
1.1       nicm      416:
                    417:        if (cfg_file == NULL) {
                    418:                home = getenv("HOME");
                    419:                if (home == NULL || *home == '\0') {
                    420:                        pw = getpwuid(getuid());
                    421:                        if (pw != NULL)
                    422:                                home = pw->pw_dir;
                    423:                }
                    424:                xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG);
                    425:                if (access(cfg_file, R_OK) != 0) {
                    426:                        xfree(cfg_file);
                    427:                        cfg_file = NULL;
                    428:                }
                    429:        } else {
                    430:                if (access(cfg_file, R_OK) != 0) {
                    431:                        log_warn("%s", cfg_file);
                    432:                        exit(1);
                    433:                }
                    434:        }
1.21      nicm      435:
1.1       nicm      436:        if (label == NULL)
                    437:                label = xstrdup("default");
                    438:        if (path == NULL && (path = makesockpath(label)) == NULL) {
                    439:                log_warn("can't create socket");
                    440:                exit(1);
                    441:        }
                    442:        xfree(label);
                    443:
                    444:        if (getcwd(cwd, sizeof cwd) == NULL) {
1.11      nicm      445:                pw = getpwuid(getuid());
                    446:                if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
                    447:                        strlcpy(cwd, pw->pw_dir, sizeof cwd);
                    448:                else
                    449:                        strlcpy(cwd, "/", sizeof cwd);
1.1       nicm      450:        }
1.12      nicm      451:        options_set_string(&global_s_options, "default-path", "%s", cwd);
1.1       nicm      452:
                    453:        if (unlock) {
1.21      nicm      454:                if (prepare_unlock(&msg, &buf, &len, argc) != 0)
1.1       nicm      455:                        exit(1);
1.21      nicm      456:        } else {
                    457:                if (prepare_cmd(&msg, &buf, &len, argc, argv) != 0)
1.1       nicm      458:                        exit(1);
1.21      nicm      459:        }
                    460:
                    461:        if (unlock)
1.20      nicm      462:                cmdflags &= ~CMD_STARTSERVER;
1.21      nicm      463:        else if (argc == 0)
1.30      nicm      464:                cmdflags |= CMD_STARTSERVER|CMD_SENDENVIRON;
1.21      nicm      465:        else {
                    466:                /*
                    467:                 * It sucks parsing the command string twice (in client and
                    468:                 * later in server) but it is necessary to get the start server
                    469:                 * flag.
                    470:                 */
                    471:                if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
                    472:                        log_warnx("%s", cause);
                    473:                        exit(1);
1.1       nicm      474:                }
1.21      nicm      475:                cmdflags &= ~CMD_STARTSERVER;
1.1       nicm      476:                TAILQ_FOREACH(cmd, cmdlist, qentry) {
1.29      nicm      477:                        if (cmd->entry->flags & CMD_STARTSERVER)
1.20      nicm      478:                                cmdflags |= CMD_STARTSERVER;
1.29      nicm      479:                        if (cmd->entry->flags & CMD_SENDENVIRON)
                    480:                                cmdflags |= CMD_SENDENVIRON;
1.1       nicm      481:                }
1.21      nicm      482:                cmd_list_free(cmdlist);
1.1       nicm      483:        }
                    484:
                    485:        memset(&cctx, 0, sizeof cctx);
1.20      nicm      486:        if (client_init(path, &cctx, cmdflags, flags) != 0)
1.1       nicm      487:                exit(1);
                    488:        xfree(path);
                    489:
1.21      nicm      490:        client_write_server(&cctx, msg, buf, len);
                    491:        memset(buf, 0, len);
1.1       nicm      492:
                    493:        retcode = 0;
                    494:        for (;;) {
1.31      nicm      495:                pfd.fd = cctx.ibuf.fd;
1.1       nicm      496:                pfd.events = POLLIN;
1.31      nicm      497:                if (cctx.ibuf.w.queued != 0)
1.1       nicm      498:                        pfd.events |= POLLOUT;
                    499:
1.31      nicm      500:                if ((nfds = poll(&pfd, 1, INFTIM)) == -1) {
1.1       nicm      501:                        if (errno == EAGAIN || errno == EINTR)
                    502:                                continue;
                    503:                        fatal("poll failed");
                    504:                }
1.31      nicm      505:                if (nfds == 0)
                    506:                        continue;
                    507:
                    508:                if (pfd.revents & (POLLERR|POLLHUP|POLLNVAL))
                    509:                        fatalx("socket error");
                    510:
                    511:                 if (pfd.revents & POLLIN) {
                    512:                        if (dispatch_imsg(&cctx, &retcode) != 0)
                    513:                                break;
                    514:                }
                    515:
                    516:                if (pfd.revents & POLLOUT) {
                    517:                        if (msgbuf_write(&cctx.ibuf.w) < 0)
                    518:                                fatalx("msgbuf_write failed");
                    519:                }
                    520:        }
                    521:
                    522:        options_free(&global_s_options);
                    523:        options_free(&global_w_options);
                    524:
                    525:        return (retcode);
                    526: }
                    527:
                    528: int
                    529: dispatch_imsg(struct client_ctx *cctx, int *retcode)
                    530: {
                    531:        struct imsg             imsg;
                    532:        ssize_t                 n, datalen;
                    533:        struct msg_print_data   printdata;
1.1       nicm      534:
1.31      nicm      535:         if ((n = imsg_read(&cctx->ibuf)) == -1 || n == 0)
                    536:                fatalx("imsg_read failed");
1.1       nicm      537:
1.31      nicm      538:        for (;;) {
                    539:                if ((n = imsg_get(&cctx->ibuf, &imsg)) == -1)
                    540:                        fatalx("imsg_get failed");
                    541:                if (n == 0)
                    542:                        return (0);
                    543:                datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1.1       nicm      544:
1.31      nicm      545:                switch (imsg.hdr.type) {
1.1       nicm      546:                case MSG_EXIT:
                    547:                case MSG_SHUTDOWN:
1.31      nicm      548:                        if (datalen != 0)
                    549:                                fatalx("bad MSG_EXIT size");
                    550:
                    551:                        return (-1);
1.1       nicm      552:                case MSG_ERROR:
1.31      nicm      553:                        *retcode = 1;
1.1       nicm      554:                        /* FALLTHROUGH */
                    555:                case MSG_PRINT:
1.31      nicm      556:                        if (datalen != sizeof printdata)
1.1       nicm      557:                                fatalx("bad MSG_PRINT size");
1.31      nicm      558:                        memcpy(&printdata, imsg.data, sizeof printdata);
                    559:                        printdata.msg[(sizeof printdata.msg) - 1] = '\0';
1.21      nicm      560:
                    561:                        log_info("%s", printdata.msg);
1.31      nicm      562:                        break;
1.1       nicm      563:                case MSG_READY:
1.31      nicm      564:                        if (datalen != 0)
                    565:                                fatalx("bad MSG_READY size");
                    566:
                    567:                        *retcode = client_main(cctx);
                    568:                        return (-1);
                    569:                case MSG_VERSION:
                    570:                        if (datalen != 0)
                    571:                                fatalx("bad MSG_VERSION size");
                    572:
                    573:                        log_warnx("protocol version mismatch (client %u, "
                    574:                            "server %u)", PROTOCOL_VERSION, imsg.hdr.peerid);
                    575:                        *retcode = 1;
                    576:                        return (-1);
1.1       nicm      577:                default:
1.31      nicm      578:                        fatalx("unexpected message");
1.1       nicm      579:                }
1.31      nicm      580:
                    581:                imsg_free(&imsg);
1.1       nicm      582:        }
                    583: }