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

1.187   ! nicm        1: /* $OpenBSD: tmux.c,v 1.186 2018/01/12 10:22:02 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.164     nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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:
1.145     nicm       22: #include <err.h>
1.1       nicm       23: #include <errno.h>
1.55      nicm       24: #include <event.h>
1.91      nicm       25: #include <fcntl.h>
1.168     nicm       26: #include <langinfo.h>
1.119     nicm       27: #include <locale.h>
1.1       nicm       28: #include <paths.h>
                     29: #include <pwd.h>
                     30: #include <stdlib.h>
                     31: #include <string.h>
1.147     nicm       32: #include <time.h>
1.1       nicm       33: #include <unistd.h>
1.181     nicm       34: #include <util.h>
1.1       nicm       35:
                     36: #include "tmux.h"
                     37:
1.148     nicm       38: struct options *global_options;        /* server options */
                     39: struct options *global_s_options;      /* session options */
                     40: struct options *global_w_options;      /* window options */
1.149     nicm       41: struct environ *global_environ;
1.163     nicm       42: struct hooks   *global_hooks;
1.1       nicm       43:
1.158     nicm       44: struct timeval  start_time;
1.160     nicm       45: const char     *socket_path;
1.176     nicm       46: int             ptm_fd = -1;
1.184     nicm       47: const char     *shell_command;
1.70      nicm       48:
1.171     nicm       49: static __dead void      usage(void);
1.186     nicm       50: static char            *make_label(const char *, char **);
1.55      nicm       51:
1.172     nicm       52: static const char      *getshell(void);
                     53: static int              checkshell(const char *);
                     54:
1.171     nicm       55: static __dead void
1.1       nicm       56: usage(void)
                     57: {
1.4       sobrado    58:        fprintf(stderr,
1.136     jmc        59:            "usage: %s [-2Cluv] [-c shell-command] [-f file] [-L socket-name]\n"
1.41      nicm       60:            "            [-S socket-path] [command [flags]]\n",
1.170     nicm       61:            getprogname());
1.1       nicm       62:        exit(1);
                     63: }
                     64:
1.172     nicm       65: static const char *
1.39      nicm       66: getshell(void)
                     67: {
                     68:        struct passwd   *pw;
                     69:        const char      *shell;
                     70:
                     71:        shell = getenv("SHELL");
                     72:        if (checkshell(shell))
                     73:                return (shell);
                     74:
                     75:        pw = getpwuid(getuid());
                     76:        if (pw != NULL && checkshell(pw->pw_shell))
                     77:                return (pw->pw_shell);
                     78:
                     79:        return (_PATH_BSHELL);
                     80: }
                     81:
1.172     nicm       82: static int
1.39      nicm       83: checkshell(const char *shell)
                     84: {
1.177     nicm       85:        if (shell == NULL || *shell != '/')
1.105     nicm       86:                return (0);
                     87:        if (areshell(shell))
1.39      nicm       88:                return (0);
                     89:        if (access(shell, X_OK) != 0)
                     90:                return (0);
                     91:        return (1);
                     92: }
                     93:
                     94: int
                     95: areshell(const char *shell)
                     96: {
                     97:        const char      *progname, *ptr;
                     98:
                     99:        if ((ptr = strrchr(shell, '/')) != NULL)
                    100:                ptr++;
                    101:        else
                    102:                ptr = shell;
1.170     nicm      103:        progname = getprogname();
1.39      nicm      104:        if (*progname == '-')
                    105:                progname++;
                    106:        if (strcmp(ptr, progname) == 0)
                    107:                return (1);
                    108:        return (0);
1.107     nicm      109: }
                    110:
1.160     nicm      111: static char *
1.186     nicm      112: make_label(const char *label, char **cause)
1.1       nicm      113: {
1.160     nicm      114:        char            *base, resolved[PATH_MAX], *path, *s;
                    115:        struct stat      sb;
1.178     nicm      116:        uid_t            uid;
1.186     nicm      117:
                    118:        *cause = NULL;
1.160     nicm      119:
                    120:        if (label == NULL)
                    121:                label = "default";
1.1       nicm      122:        uid = getuid();
1.160     nicm      123:
1.118     nicm      124:        if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0')
1.180     nicm      125:                xasprintf(&base, "%s/tmux-%ld", s, (long)uid);
1.118     nicm      126:        else
1.178     nicm      127:                xasprintf(&base, "%s/tmux-%ld", _PATH_TMP, (long)uid);
1.186     nicm      128:        if (realpath(base, resolved) == NULL &&
                    129:            strlcpy(resolved, base, sizeof resolved) >= sizeof resolved) {
                    130:                errno = ERANGE;
                    131:                free(base);
                    132:                goto fail;
                    133:        }
1.1       nicm      134:
1.186     nicm      135:        if (mkdir(resolved, S_IRWXU) != 0 && errno != EEXIST)
1.160     nicm      136:                goto fail;
1.186     nicm      137:        if (lstat(resolved, &sb) != 0)
1.160     nicm      138:                goto fail;
1.1       nicm      139:        if (!S_ISDIR(sb.st_mode)) {
                    140:                errno = ENOTDIR;
1.160     nicm      141:                goto fail;
1.1       nicm      142:        }
1.134     nicm      143:        if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) {
1.1       nicm      144:                errno = EACCES;
1.160     nicm      145:                goto fail;
1.1       nicm      146:        }
1.160     nicm      147:        xasprintf(&path, "%s/%s", resolved, label);
                    148:        return (path);
1.113     nicm      149:
1.160     nicm      150: fail:
1.186     nicm      151:        xasprintf(cause, "error creating %s (%s)", resolved, strerror(errno));
1.160     nicm      152:        return (NULL);
1.1       nicm      153: }
                    154:
1.101     nicm      155: void
                    156: setblocking(int fd, int state)
                    157: {
                    158:        int mode;
                    159:
                    160:        if ((mode = fcntl(fd, F_GETFL)) != -1) {
                    161:                if (!state)
                    162:                        mode |= O_NONBLOCK;
                    163:                else
                    164:                        mode &= ~O_NONBLOCK;
                    165:                fcntl(fd, F_SETFL, mode);
                    166:        }
                    167: }
                    168:
1.138     nicm      169: const char *
1.187   ! nicm      170: find_cwd(void)
        !           171: {
        !           172:        char             resolved1[PATH_MAX], resolved2[PATH_MAX];
        !           173:        static char      cwd[PATH_MAX];
        !           174:        const char      *pwd;
        !           175:
        !           176:        if (getcwd(cwd, sizeof cwd) == NULL)
        !           177:                return (NULL);
        !           178:        if ((pwd = getenv("PWD")) == NULL || *pwd == '\0')
        !           179:                return (cwd);
        !           180:
        !           181:        /*
        !           182:         * We want to use PWD so that symbolic links are maintained,
        !           183:         * but only if it matches the actual working directory.
        !           184:         */
        !           185:        if (realpath(pwd, resolved1) == NULL)
        !           186:                return (cwd);
        !           187:        if (realpath(cwd, resolved2) == NULL)
        !           188:                return (cwd);
        !           189:        if (strcmp(resolved1, resolved2) != 0)
        !           190:                return (cwd);
        !           191:        return (pwd);
        !           192: }
        !           193:
        !           194: const char *
1.137     nicm      195: find_home(void)
                    196: {
1.142     nicm      197:        struct passwd           *pw;
                    198:        static const char       *home;
                    199:
                    200:        if (home != NULL)
                    201:                return (home);
1.137     nicm      202:
                    203:        home = getenv("HOME");
                    204:        if (home == NULL || *home == '\0') {
                    205:                pw = getpwuid(getuid());
                    206:                if (pw != NULL)
                    207:                        home = pw->pw_dir;
                    208:                else
                    209:                        home = NULL;
                    210:        }
                    211:
1.138     nicm      212:        return (home);
1.137     nicm      213: }
                    214:
1.1       nicm      215: int
                    216: main(int argc, char **argv)
                    217: {
1.186     nicm      218:        char                                    *path, *label, *cause, **var;
1.185     nicm      219:        const char                              *s, *shell, *cwd;
1.175     nicm      220:        int                                      opt, flags, keys;
                    221:        const struct options_table_entry        *oe;
1.165     nicm      222:
1.183     nicm      223:        if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL &&
                    224:            setlocale(LC_CTYPE, "C.UTF-8") == NULL) {
1.168     nicm      225:                if (setlocale(LC_CTYPE, "") == NULL)
                    226:                        errx(1, "invalid LC_ALL, LC_CTYPE or LANG");
                    227:                s = nl_langinfo(CODESET);
1.169     nicm      228:                if (strcasecmp(s, "UTF-8") != 0 && strcasecmp(s, "UTF8") != 0)
1.168     nicm      229:                        errx(1, "need UTF-8 locale (LC_CTYPE) but have %s", s);
                    230:        }
1.167     nicm      231:
1.119     nicm      232:        setlocale(LC_TIME, "");
1.144     nicm      233:        tzset();
1.1       nicm      234:
1.140     nicm      235:        if (**argv == '-')
                    236:                flags = CLIENT_LOGIN;
                    237:        else
                    238:                flags = 0;
                    239:
1.92      nicm      240:        label = path = NULL;
1.117     nicm      241:        while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:uUv")) != -1) {
1.41      nicm      242:                switch (opt) {
1.1       nicm      243:                case '2':
1.124     nicm      244:                        flags |= CLIENT_256COLOURS;
1.1       nicm      245:                        break;
1.46      nicm      246:                case 'c':
1.184     nicm      247:                        shell_command = optarg;
1.111     nicm      248:                        break;
                    249:                case 'C':
1.124     nicm      250:                        if (flags & CLIENT_CONTROL)
                    251:                                flags |= CLIENT_CONTROLCONTROL;
1.111     nicm      252:                        else
1.124     nicm      253:                                flags |= CLIENT_CONTROL;
1.37      nicm      254:                        break;
1.1       nicm      255:                case 'f':
1.142     nicm      256:                        set_cfg_file(optarg);
1.41      nicm      257:                        break;
                    258:                case 'l':
1.140     nicm      259:                        flags |= CLIENT_LOGIN;
1.1       nicm      260:                        break;
                    261:                case 'L':
1.112     nicm      262:                        free(label);
1.1       nicm      263:                        label = xstrdup(optarg);
                    264:                        break;
1.37      nicm      265:                case 'q':
                    266:                        break;
1.1       nicm      267:                case 'S':
1.112     nicm      268:                        free(path);
1.1       nicm      269:                        path = xstrdup(optarg);
                    270:                        break;
                    271:                case 'u':
1.124     nicm      272:                        flags |= CLIENT_UTF8;
1.1       nicm      273:                        break;
                    274:                case 'v':
1.157     nicm      275:                        log_add_level();
1.1       nicm      276:                        break;
1.53      deraadt   277:                default:
1.1       nicm      278:                        usage();
1.53      deraadt   279:                }
                    280:        }
1.1       nicm      281:        argc -= optind;
                    282:        argv += optind;
                    283:
1.184     nicm      284:        if (shell_command != NULL && argc != 0)
1.46      nicm      285:                usage();
1.145     nicm      286:
1.181     nicm      287:        if ((ptm_fd = getptmfd()) == -1)
                    288:                err(1, "getptmfd");
1.156     nicm      289:        if (pledge("stdio rpath wpath cpath flock fattr unix getpw sendfd "
                    290:            "recvfd proc exec tty ps", NULL) != 0)
1.145     nicm      291:                err(1, "pledge");
1.46      nicm      292:
1.152     nicm      293:        /*
                    294:         * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8.
                    295:         * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain
                    296:         * UTF-8, it is a safe assumption that either they are using a UTF-8
                    297:         * terminal, or if not they know that output from UTF-8-capable
                    298:         * programs may be wrong.
                    299:         */
                    300:        if (getenv("TMUX") != NULL)
                    301:                flags |= CLIENT_UTF8;
                    302:        else {
                    303:                s = getenv("LC_ALL");
                    304:                if (s == NULL || *s == '\0')
                    305:                        s = getenv("LC_CTYPE");
                    306:                if (s == NULL || *s == '\0')
                    307:                        s = getenv("LANG");
                    308:                if (s == NULL || *s == '\0')
                    309:                        s = "";
                    310:                if (strcasestr(s, "UTF-8") != NULL ||
                    311:                    strcasestr(s, "UTF8") != NULL)
1.124     nicm      312:                        flags |= CLIENT_UTF8;
1.15      nicm      313:        }
1.163     nicm      314:
                    315:        global_hooks = hooks_create(NULL);
1.15      nicm      316:
1.149     nicm      317:        global_environ = environ_create();
1.62      nicm      318:        for (var = environ; *var != NULL; var++)
1.149     nicm      319:                environ_put(global_environ, *var);
1.187   ! nicm      320:        if ((cwd = find_cwd()) != NULL)
1.185     nicm      321:                environ_set(global_environ, "PWD", "%s", cwd);
1.63      nicm      322:
1.148     nicm      323:        global_options = options_create(NULL);
                    324:        global_s_options = options_create(NULL);
1.175     nicm      325:        global_w_options = options_create(NULL);
                    326:        for (oe = options_table; oe->name != NULL; oe++) {
                    327:                if (oe->scope == OPTIONS_TABLE_SERVER)
                    328:                        options_default(global_options, oe);
                    329:                if (oe->scope == OPTIONS_TABLE_SESSION)
                    330:                        options_default(global_s_options, oe);
                    331:                if (oe->scope == OPTIONS_TABLE_WINDOW)
                    332:                        options_default(global_w_options, oe);
                    333:        }
1.44      nicm      334:
1.175     nicm      335:        /*
                    336:         * The default shell comes from SHELL or from the user's passwd entry
                    337:         * if available.
                    338:         */
                    339:        shell = getshell();
                    340:        options_set_string(global_s_options, "default-shell", 0, "%s", shell);
1.94      nicm      341:
1.99      nicm      342:        /* Override keys to vi if VISUAL or EDITOR are set. */
1.94      nicm      343:        if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) {
                    344:                if (strrchr(s, '/') != NULL)
                    345:                        s = strrchr(s, '/') + 1;
                    346:                if (strstr(s, "vi") != NULL)
                    347:                        keys = MODEKEY_VI;
1.99      nicm      348:                else
                    349:                        keys = MODEKEY_EMACS;
1.148     nicm      350:                options_set_number(global_s_options, "status-keys", keys);
                    351:                options_set_number(global_w_options, "mode-keys", keys);
1.1       nicm      352:        }
1.62      nicm      353:
1.68      nicm      354:        /*
1.160     nicm      355:         * If socket is specified on the command-line with -S or -L, it is
                    356:         * used. Otherwise, $TMUX is checked and if that fails "default" is
                    357:         * used.
1.68      nicm      358:         */
1.160     nicm      359:        if (path == NULL && label == NULL) {
                    360:                s = getenv("TMUX");
                    361:                if (s != NULL && *s != '\0' && *s != ',') {
                    362:                        path = xstrdup(s);
1.173     nicm      363:                        path[strcspn(path, ",")] = '\0';
1.68      nicm      364:                }
1.1       nicm      365:        }
1.186     nicm      366:        if (path == NULL && (path = make_label(label, &cause)) == NULL) {
                    367:                if (cause != NULL) {
                    368:                        fprintf(stderr, "%s\n", cause);
                    369:                        free(cause);
                    370:                }
1.127     nicm      371:                exit(1);
                    372:        }
1.160     nicm      373:        socket_path = path;
                    374:        free(label);
1.1       nicm      375:
1.92      nicm      376:        /* Pass control to the client. */
1.184     nicm      377:        exit(client_main(event_init(), argc, argv, flags));
1.1       nicm      378: }