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

1.23    ! nicm        1: /* $OpenBSD: tmux.c,v 1.22 2009/07/29 05:36:53 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.1       nicm       47:
                     48: int             server_locked;
1.19      nicm       49: u_int           password_failures;
1.1       nicm       50: char           *server_password;
                     51: time_t          server_activity;
                     52:
                     53: int             debug_level;
                     54: int             be_quiet;
                     55: time_t          start_time;
                     56: char           *socket_path;
                     57:
                     58: __dead void     usage(void);
                     59: char           *makesockpath(const char *);
1.23    ! nicm       60: int             prepare_unlock(enum msgtype *, void **, size_t *, int);
        !            61: int             prepare_cmd(enum msgtype *, void **, size_t *, int, char **);
1.1       nicm       62:
                     63: __dead void
                     64: usage(void)
                     65: {
1.4       sobrado    66:        fprintf(stderr,
                     67:            "usage: %s [-28dqUuv] [-f file] [-L socket-name] [-S socket-path]\n"
                     68:            "            [command [flags]]\n",
1.1       nicm       69:            __progname);
                     70:        exit(1);
                     71: }
                     72:
                     73: void
                     74: logfile(const char *name)
                     75: {
                     76:        char    *path;
                     77:
                     78:        log_close();
                     79:        if (debug_level > 0) {
                     80:                xasprintf(
                     81:                    &path, "%s-%s-%ld.log", __progname, name, (long) getpid());
                     82:                log_open_file(debug_level, path);
                     83:                xfree(path);
                     84:        }
                     85: }
                     86:
                     87: void
                     88: sighandler(int sig)
                     89: {
                     90:        int     saved_errno;
                     91:
                     92:        saved_errno = errno;
                     93:        switch (sig) {
                     94:        case SIGWINCH:
                     95:                sigwinch = 1;
                     96:                break;
                     97:        case SIGTERM:
                     98:                sigterm = 1;
                     99:                break;
                    100:        case SIGCHLD:
                    101:                sigchld = 1;
                    102:                break;
                    103:        case SIGCONT:
                    104:                sigcont = 1;
                    105:                break;
                    106:        case SIGUSR1:
                    107:                sigusr1 = 1;
                    108:                break;
                    109:        case SIGUSR2:
                    110:                sigusr2 = 1;
                    111:                break;
                    112:        }
                    113:        errno = saved_errno;
                    114: }
                    115:
                    116: void
                    117: siginit(void)
                    118: {
                    119:        struct sigaction         act;
                    120:
                    121:        memset(&act, 0, sizeof act);
                    122:        sigemptyset(&act.sa_mask);
                    123:        act.sa_flags = SA_RESTART;
                    124:
                    125:        act.sa_handler = SIG_IGN;
                    126:        if (sigaction(SIGPIPE, &act, NULL) != 0)
                    127:                fatal("sigaction failed");
                    128:        if (sigaction(SIGINT, &act, NULL) != 0)
                    129:                fatal("sigaction failed");
                    130:        if (sigaction(SIGTSTP, &act, NULL) != 0)
                    131:                fatal("sigaction failed");
                    132:        if (sigaction(SIGQUIT, &act, NULL) != 0)
                    133:                fatal("sigaction failed");
                    134:
                    135:        act.sa_handler = sighandler;
                    136:        if (sigaction(SIGWINCH, &act, NULL) != 0)
                    137:                fatal("sigaction failed");
                    138:        if (sigaction(SIGTERM, &act, NULL) != 0)
                    139:                fatal("sigaction failed");
                    140:        if (sigaction(SIGCHLD, &act, NULL) != 0)
                    141:                fatal("sigaction failed");
                    142:        if (sigaction(SIGUSR1, &act, NULL) != 0)
                    143:                fatal("sigaction failed");
                    144:        if (sigaction(SIGUSR2, &act, NULL) != 0)
                    145:                fatal("sigaction failed");
                    146: }
                    147:
                    148: void
                    149: sigreset(void)
                    150: {
                    151:        struct sigaction act;
                    152:
                    153:        memset(&act, 0, sizeof act);
                    154:        sigemptyset(&act.sa_mask);
                    155:
                    156:        act.sa_handler = SIG_DFL;
                    157:        if (sigaction(SIGPIPE, &act, NULL) != 0)
                    158:                fatal("sigaction failed");
                    159:        if (sigaction(SIGUSR1, &act, NULL) != 0)
                    160:                fatal("sigaction failed");
                    161:        if (sigaction(SIGUSR2, &act, NULL) != 0)
                    162:                fatal("sigaction failed");
                    163:        if (sigaction(SIGINT, &act, NULL) != 0)
                    164:                fatal("sigaction failed");
                    165:        if (sigaction(SIGTSTP, &act, NULL) != 0)
                    166:                fatal("sigaction failed");
                    167:        if (sigaction(SIGQUIT, &act, NULL) != 0)
                    168:                fatal("sigaction failed");
                    169:        if (sigaction(SIGWINCH, &act, NULL) != 0)
                    170:                fatal("sigaction failed");
                    171:        if (sigaction(SIGTERM, &act, NULL) != 0)
                    172:                fatal("sigaction failed");
                    173:        if (sigaction(SIGCHLD, &act, NULL) != 0)
                    174:                fatal("sigaction failed");
                    175: }
                    176:
                    177: char *
                    178: makesockpath(const char *label)
                    179: {
                    180:        char            base[MAXPATHLEN], *path;
                    181:        struct stat     sb;
                    182:        u_int           uid;
                    183:
                    184:        uid = getuid();
                    185:        xsnprintf(base, MAXPATHLEN, "%s/%s-%d", _PATH_TMP, __progname, uid);
                    186:
                    187:        if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
                    188:                return (NULL);
                    189:
                    190:        if (lstat(base, &sb) != 0)
                    191:                return (NULL);
                    192:        if (!S_ISDIR(sb.st_mode)) {
                    193:                errno = ENOTDIR;
                    194:                return (NULL);
                    195:        }
                    196:        if (sb.st_uid != uid || (sb.st_mode & (S_IRWXG|S_IRWXO)) != 0) {
                    197:                errno = EACCES;
                    198:                return (NULL);
                    199:        }
                    200:
                    201:        xasprintf(&path, "%s/%s", base, label);
                    202:        return (path);
                    203: }
                    204:
                    205: int
1.23    ! nicm      206: prepare_unlock(enum msgtype *msg, void **buf, size_t *len, int argc)
1.21      nicm      207: {
                    208:        static struct msg_unlock_data    unlockdata;
                    209:        char                            *pass;
                    210:
                    211:        if (argc != 0) {
                    212:                log_warnx("can't specify a command when unlocking");
                    213:                return (-1);
                    214:        }
                    215:
                    216:        if ((pass = getpass("Password: ")) == NULL)
                    217:                return (-1);
                    218:
                    219:        if (strlen(pass) >= sizeof unlockdata.pass) {
                    220:                log_warnx("password too long");
                    221:                return (-1);
                    222:        }
                    223:
                    224:        strlcpy(unlockdata.pass, pass, sizeof unlockdata.pass);
                    225:        memset(pass, 0, strlen(pass));
                    226:
                    227:        *buf = &unlockdata;
                    228:        *len = sizeof unlockdata;
                    229:
                    230:        *msg = MSG_UNLOCK;
                    231:        return (0);
                    232: }
                    233:
                    234: int
1.23    ! nicm      235: prepare_cmd(enum msgtype *msg, void **buf, size_t *len, int argc, char **argv)
1.21      nicm      236: {
                    237:        static struct msg_command_data   cmddata;
                    238:
                    239:        client_fill_session(&cmddata);
                    240:
                    241:        cmddata.argc = argc;
                    242:        if (cmd_pack_argv(argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
                    243:                log_warnx("command too long");
                    244:                return (-1);
                    245:        }
                    246:
                    247:        *buf = &cmddata;
                    248:        *len = sizeof cmddata;
                    249:
                    250:        *msg = MSG_COMMAND;
                    251:        return (0);
                    252: }
                    253:
                    254: int
1.1       nicm      255: main(int argc, char **argv)
                    256: {
                    257:        struct client_ctx        cctx;
                    258:        struct cmd_list         *cmdlist;
                    259:        struct cmd              *cmd;
                    260:        struct pollfd            pfd;
1.23    ! nicm      261:        enum msgtype             msg;
1.1       nicm      262:        struct hdr               hdr;
                    263:        struct passwd           *pw;
1.21      nicm      264:        struct msg_print_data    printdata;
                    265:        char                    *s, *path, *label, *home, *cause;
1.1       nicm      266:        char                     cwd[MAXPATHLEN];
1.21      nicm      267:        void                    *buf;
                    268:        size_t                   len;
1.20      nicm      269:        int                      retcode, opt, flags, unlock, cmdflags = 0;
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.15      nicm      323:        if (!(flags & IDENTIFY_UTF8)) {
                    324:                /*
                    325:                 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG
                    326:                 * exist (in that order) to contain UTF-8, it is a safe
                    327:                 * assumption that either they are using a UTF-8 terminal, or
                    328:                 * if not they know that output from UTF-8-capable programs may
                    329:                 * be wrong.
                    330:                 */
                    331:                if ((s = getenv("LC_ALL")) == NULL) {
                    332:                        if ((s = getenv("LC_CTYPE")) == NULL)
                    333:                                s = getenv("LANG");
                    334:                }
                    335:                if (s != NULL && strcasestr(s, "UTF-8") != NULL)
                    336:                        flags |= IDENTIFY_UTF8;
                    337:        }
                    338:
1.12      nicm      339:        options_init(&global_s_options, NULL);
                    340:        options_set_number(&global_s_options, "bell-action", BELL_ANY);
                    341:        options_set_number(&global_s_options, "buffer-limit", 9);
1.13      nicm      342:        options_set_string(&global_s_options, "default-command", "%s", "");
1.14      nicm      343:        options_set_string(&global_s_options, "default-terminal", "screen");
1.12      nicm      344:        options_set_number(&global_s_options, "display-time", 750);
                    345:        options_set_number(&global_s_options, "history-limit", 2000);
                    346:        options_set_number(&global_s_options, "lock-after-time", 0);
                    347:        options_set_number(&global_s_options, "message-attr", GRID_ATTR_REVERSE);
                    348:        options_set_number(&global_s_options, "message-bg", 3);
                    349:        options_set_number(&global_s_options, "message-fg", 0);
                    350:        options_set_number(&global_s_options, "prefix", '\002');
                    351:        options_set_number(&global_s_options, "repeat-time", 500);
                    352:        options_set_number(&global_s_options, "set-remain-on-exit", 0);
                    353:        options_set_number(&global_s_options, "set-titles", 0);
                    354:        options_set_number(&global_s_options, "status", 1);
                    355:        options_set_number(&global_s_options, "status-attr", GRID_ATTR_REVERSE);
                    356:        options_set_number(&global_s_options, "status-bg", 2);
                    357:        options_set_number(&global_s_options, "status-fg", 0);
                    358:        options_set_number(&global_s_options, "status-interval", 15);
                    359:        options_set_number(&global_s_options, "status-keys", MODEKEY_EMACS);
1.18      nicm      360:        options_set_number(&global_s_options, "status-justify", 0);
1.12      nicm      361:        options_set_number(&global_s_options, "status-left-length", 10);
                    362:        options_set_number(&global_s_options, "status-right-length", 40);
                    363:        options_set_string(&global_s_options, "status-left", "[#S]");
1.1       nicm      364:        options_set_string(
1.22      nicm      365:            &global_s_options, "status-right", "\"#22T\" %%H:%%M %%d-%%b-%%y");
1.15      nicm      366:        if (flags & IDENTIFY_UTF8)
                    367:                options_set_number(&global_s_options, "status-utf8", 1);
                    368:        else
                    369:                options_set_number(&global_s_options, "status-utf8", 0);
1.16      nicm      370:        options_set_number(&global_s_options, "visual-activity", 0);
                    371:        options_set_number(&global_s_options, "visual-bell", 0);
                    372:        options_set_number(&global_s_options, "visual-content", 0);
1.1       nicm      373:
1.12      nicm      374:        options_init(&global_w_options, NULL);
                    375:        options_set_number(&global_w_options, "aggressive-resize", 0);
                    376:        options_set_number(&global_w_options, "automatic-rename", 1);
                    377:        options_set_number(&global_w_options, "clock-mode-colour", 4);
                    378:        options_set_number(&global_w_options, "clock-mode-style", 1);
                    379:        options_set_number(&global_w_options, "force-height", 0);
                    380:        options_set_number(&global_w_options, "force-width", 0);
                    381:        options_set_number(&global_w_options, "mode-attr", GRID_ATTR_REVERSE);
                    382:        options_set_number(&global_w_options, "main-pane-width", 81);
                    383:        options_set_number(&global_w_options, "main-pane-height", 24);
                    384:        options_set_number(&global_w_options, "mode-bg", 3);
                    385:        options_set_number(&global_w_options, "mode-fg", 0);
                    386:        options_set_number(&global_w_options, "mode-keys", MODEKEY_EMACS);
                    387:        options_set_number(&global_w_options, "monitor-activity", 0);
                    388:        options_set_string(&global_w_options, "monitor-content", "%s", "");
1.15      nicm      389:        if (flags & IDENTIFY_UTF8)
                    390:                options_set_number(&global_w_options, "utf8", 1);
                    391:        else
                    392:                options_set_number(&global_w_options, "utf8", 0);
1.12      nicm      393:        options_set_number(&global_w_options, "window-status-attr", 0);
                    394:        options_set_number(&global_w_options, "window-status-bg", 8);
                    395:        options_set_number(&global_w_options, "window-status-fg", 8);
1.17      nicm      396:        options_set_number(&global_w_options, "window-status-current-attr", 0);
                    397:        options_set_number(&global_w_options, "window-status-current-bg", 8);
                    398:        options_set_number(&global_w_options, "window-status-current-fg", 8);
1.12      nicm      399:        options_set_number(&global_w_options, "xterm-keys", 0);
                    400:        options_set_number(&global_w_options, "remain-on-exit", 0);
1.1       nicm      401:
                    402:        if (cfg_file == NULL) {
                    403:                home = getenv("HOME");
                    404:                if (home == NULL || *home == '\0') {
                    405:                        pw = getpwuid(getuid());
                    406:                        if (pw != NULL)
                    407:                                home = pw->pw_dir;
                    408:                }
                    409:                xasprintf(&cfg_file, "%s/%s", home, DEFAULT_CFG);
                    410:                if (access(cfg_file, R_OK) != 0) {
                    411:                        xfree(cfg_file);
                    412:                        cfg_file = NULL;
                    413:                }
                    414:        } else {
                    415:                if (access(cfg_file, R_OK) != 0) {
                    416:                        log_warn("%s", cfg_file);
                    417:                        exit(1);
                    418:                }
                    419:        }
1.21      nicm      420:
1.1       nicm      421:        if (label == NULL)
                    422:                label = xstrdup("default");
                    423:        if (path == NULL && (path = makesockpath(label)) == NULL) {
                    424:                log_warn("can't create socket");
                    425:                exit(1);
                    426:        }
                    427:        xfree(label);
                    428:
                    429:        if (getcwd(cwd, sizeof cwd) == NULL) {
1.11      nicm      430:                pw = getpwuid(getuid());
                    431:                if (pw->pw_dir != NULL && *pw->pw_dir != '\0')
                    432:                        strlcpy(cwd, pw->pw_dir, sizeof cwd);
                    433:                else
                    434:                        strlcpy(cwd, "/", sizeof cwd);
1.1       nicm      435:        }
1.12      nicm      436:        options_set_string(&global_s_options, "default-path", "%s", cwd);
1.1       nicm      437:
                    438:        if (unlock) {
1.21      nicm      439:                if (prepare_unlock(&msg, &buf, &len, argc) != 0)
1.1       nicm      440:                        exit(1);
1.21      nicm      441:        } else {
                    442:                if (prepare_cmd(&msg, &buf, &len, argc, argv) != 0)
1.1       nicm      443:                        exit(1);
1.21      nicm      444:        }
                    445:
                    446:        if (unlock)
1.20      nicm      447:                cmdflags &= ~CMD_STARTSERVER;
1.21      nicm      448:        else if (argc == 0)
                    449:                cmdflags |= CMD_STARTSERVER;
                    450:        else {
                    451:                /*
                    452:                 * It sucks parsing the command string twice (in client and
                    453:                 * later in server) but it is necessary to get the start server
                    454:                 * flag.
                    455:                 */
                    456:                if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
                    457:                        log_warnx("%s", cause);
                    458:                        exit(1);
1.1       nicm      459:                }
1.21      nicm      460:                cmdflags &= ~CMD_STARTSERVER;
1.1       nicm      461:                TAILQ_FOREACH(cmd, cmdlist, qentry) {
                    462:                        if (cmd->entry->flags & CMD_STARTSERVER) {
1.20      nicm      463:                                cmdflags |= CMD_STARTSERVER;
1.1       nicm      464:                                break;
                    465:                        }
                    466:                }
1.21      nicm      467:                cmd_list_free(cmdlist);
1.1       nicm      468:        }
                    469:
                    470:        memset(&cctx, 0, sizeof cctx);
1.20      nicm      471:        if (client_init(path, &cctx, cmdflags, flags) != 0)
1.1       nicm      472:                exit(1);
                    473:        xfree(path);
                    474:
1.21      nicm      475:        client_write_server(&cctx, msg, buf, len);
                    476:        memset(buf, 0, len);
1.1       nicm      477:
                    478:        retcode = 0;
                    479:        for (;;) {
                    480:                pfd.fd = cctx.srv_fd;
                    481:                pfd.events = POLLIN;
                    482:                if (BUFFER_USED(cctx.srv_out) > 0)
                    483:                        pfd.events |= POLLOUT;
                    484:
                    485:                if (poll(&pfd, 1, INFTIM) == -1) {
                    486:                        if (errno == EAGAIN || errno == EINTR)
                    487:                                continue;
                    488:                        fatal("poll failed");
                    489:                }
                    490:
                    491:                if (buffer_poll(&pfd, cctx.srv_in, cctx.srv_out) != 0)
                    492:                        goto out;
                    493:
                    494:        restart:
                    495:                if (BUFFER_USED(cctx.srv_in) < sizeof hdr)
                    496:                        continue;
                    497:                memcpy(&hdr, BUFFER_OUT(cctx.srv_in), sizeof hdr);
                    498:                if (BUFFER_USED(cctx.srv_in) < (sizeof hdr) + hdr.size)
                    499:                        continue;
                    500:                buffer_remove(cctx.srv_in, sizeof hdr);
                    501:
                    502:                switch (hdr.type) {
                    503:                case MSG_EXIT:
                    504:                case MSG_SHUTDOWN:
                    505:                        goto out;
                    506:                case MSG_ERROR:
                    507:                        retcode = 1;
                    508:                        /* FALLTHROUGH */
                    509:                case MSG_PRINT:
1.21      nicm      510:                        if (hdr.size < sizeof printdata)
1.1       nicm      511:                                fatalx("bad MSG_PRINT size");
1.21      nicm      512:                        buffer_read(cctx.srv_in, &printdata, sizeof printdata);
                    513:
                    514:                        printdata.msg[(sizeof printdata.msg) - 1] = '\0';
                    515:                        log_info("%s", printdata.msg);
1.1       nicm      516:                        goto restart;
                    517:                case MSG_READY:
                    518:                        retcode = client_main(&cctx);
                    519:                        goto out;
                    520:                default:
                    521:                        fatalx("unexpected command");
                    522:                }
                    523:        }
                    524:
                    525: out:
1.12      nicm      526:        options_free(&global_s_options);
                    527:        options_free(&global_w_options);
1.1       nicm      528:
                    529:        close(cctx.srv_fd);
                    530:        buffer_destroy(cctx.srv_in);
                    531:        buffer_destroy(cctx.srv_out);
                    532:
                    533:        return (retcode);
                    534: }