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

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