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

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