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

1.162   ! nicm        1: /* $OpenBSD: tmux.c,v 1.161 2015/11/24 23:22: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:
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.132     nicm       26: #include <getopt.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>
                     34:
                     35: #include "tmux.h"
                     36:
1.148     nicm       37: struct options *global_options;        /* server options */
                     38: struct options *global_s_options;      /* session options */
                     39: struct options *global_w_options;      /* window options */
1.149     nicm       40: struct environ *global_environ;
1.1       nicm       41:
1.158     nicm       42: struct timeval  start_time;
1.160     nicm       43: const char     *socket_path;
1.70      nicm       44:
1.1       nicm       45: __dead void     usage(void);
1.160     nicm       46: static char    *make_label(const char *);
1.55      nicm       47:
1.1       nicm       48: __dead void
                     49: usage(void)
                     50: {
1.4       sobrado    51:        fprintf(stderr,
1.136     jmc        52:            "usage: %s [-2Cluv] [-c shell-command] [-f file] [-L socket-name]\n"
1.41      nicm       53:            "            [-S socket-path] [command [flags]]\n",
1.1       nicm       54:            __progname);
                     55:        exit(1);
                     56: }
                     57:
1.39      nicm       58: const char *
                     59: getshell(void)
                     60: {
                     61:        struct passwd   *pw;
                     62:        const char      *shell;
                     63:
                     64:        shell = getenv("SHELL");
                     65:        if (checkshell(shell))
                     66:                return (shell);
                     67:
                     68:        pw = getpwuid(getuid());
                     69:        if (pw != NULL && checkshell(pw->pw_shell))
                     70:                return (pw->pw_shell);
                     71:
                     72:        return (_PATH_BSHELL);
                     73: }
                     74:
                     75: int
                     76: checkshell(const char *shell)
                     77: {
1.105     nicm       78:        if (shell == NULL || *shell == '\0' || *shell != '/')
                     79:                return (0);
                     80:        if (areshell(shell))
1.39      nicm       81:                return (0);
                     82:        if (access(shell, X_OK) != 0)
                     83:                return (0);
                     84:        return (1);
                     85: }
                     86:
                     87: int
                     88: areshell(const char *shell)
                     89: {
                     90:        const char      *progname, *ptr;
                     91:
                     92:        if ((ptr = strrchr(shell, '/')) != NULL)
                     93:                ptr++;
                     94:        else
                     95:                ptr = shell;
                     96:        progname = __progname;
                     97:        if (*progname == '-')
                     98:                progname++;
                     99:        if (strcmp(ptr, progname) == 0)
                    100:                return (1);
                    101:        return (0);
1.107     nicm      102: }
                    103:
1.160     nicm      104: static char *
                    105: make_label(const char *label)
1.1       nicm      106: {
1.160     nicm      107:        char            *base, resolved[PATH_MAX], *path, *s;
                    108:        struct stat      sb;
                    109:        u_int            uid;
                    110:        int              saved_errno;
                    111:
                    112:        if (label == NULL)
                    113:                label = "default";
1.1       nicm      114:
                    115:        uid = getuid();
1.160     nicm      116:
1.118     nicm      117:        if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0')
1.160     nicm      118:                xasprintf(&base, "%s/tmux-%u", s, uid);
1.118     nicm      119:        else
1.160     nicm      120:                xasprintf(&base, "%s/tmux-%u", _PATH_TMP, uid);
1.1       nicm      121:
                    122:        if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
1.160     nicm      123:                goto fail;
1.1       nicm      124:
                    125:        if (lstat(base, &sb) != 0)
1.160     nicm      126:                goto fail;
1.1       nicm      127:        if (!S_ISDIR(sb.st_mode)) {
                    128:                errno = ENOTDIR;
1.160     nicm      129:                goto fail;
1.1       nicm      130:        }
1.134     nicm      131:        if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) {
1.1       nicm      132:                errno = EACCES;
1.160     nicm      133:                goto fail;
1.1       nicm      134:        }
                    135:
1.160     nicm      136:        if (realpath(base, resolved) == NULL)
                    137:                strlcpy(resolved, base, sizeof resolved);
                    138:        xasprintf(&path, "%s/%s", resolved, label);
                    139:        return (path);
1.113     nicm      140:
1.160     nicm      141: fail:
                    142:        saved_errno = errno;
                    143:        free(base);
                    144:        errno = saved_errno;
                    145:        return (NULL);
1.1       nicm      146: }
                    147:
1.101     nicm      148: void
                    149: setblocking(int fd, int state)
                    150: {
                    151:        int mode;
                    152:
                    153:        if ((mode = fcntl(fd, F_GETFL)) != -1) {
                    154:                if (!state)
                    155:                        mode |= O_NONBLOCK;
                    156:                else
                    157:                        mode &= ~O_NONBLOCK;
                    158:                fcntl(fd, F_SETFL, mode);
                    159:        }
                    160: }
                    161:
1.138     nicm      162: const char *
1.137     nicm      163: find_home(void)
                    164: {
1.142     nicm      165:        struct passwd           *pw;
                    166:        static const char       *home;
                    167:
                    168:        if (home != NULL)
                    169:                return (home);
1.137     nicm      170:
                    171:        home = getenv("HOME");
                    172:        if (home == NULL || *home == '\0') {
                    173:                pw = getpwuid(getuid());
                    174:                if (pw != NULL)
                    175:                        home = pw->pw_dir;
                    176:                else
                    177:                        home = NULL;
                    178:        }
                    179:
1.138     nicm      180:        return (home);
1.137     nicm      181: }
                    182:
1.1       nicm      183: int
                    184: main(int argc, char **argv)
                    185: {
1.161     nicm      186:        char            *path, *label, **var, tmp[PATH_MAX], *shellcmd = NULL;
1.152     nicm      187:        const char      *s;
                    188:        int              opt, flags, keys;
1.119     nicm      189:
                    190:        setlocale(LC_TIME, "");
1.144     nicm      191:        tzset();
1.1       nicm      192:
1.140     nicm      193:        if (**argv == '-')
                    194:                flags = CLIENT_LOGIN;
                    195:        else
                    196:                flags = 0;
                    197:
1.92      nicm      198:        label = path = NULL;
1.117     nicm      199:        while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:uUv")) != -1) {
1.41      nicm      200:                switch (opt) {
1.1       nicm      201:                case '2':
1.124     nicm      202:                        flags |= CLIENT_256COLOURS;
1.1       nicm      203:                        break;
1.46      nicm      204:                case 'c':
1.161     nicm      205:                        free(shellcmd);
                    206:                        shellcmd = xstrdup(optarg);
1.111     nicm      207:                        break;
                    208:                case 'C':
1.124     nicm      209:                        if (flags & CLIENT_CONTROL)
                    210:                                flags |= CLIENT_CONTROLCONTROL;
1.111     nicm      211:                        else
1.124     nicm      212:                                flags |= CLIENT_CONTROL;
1.37      nicm      213:                        break;
1.1       nicm      214:                case 'f':
1.142     nicm      215:                        set_cfg_file(optarg);
1.41      nicm      216:                        break;
                    217:                case 'l':
1.140     nicm      218:                        flags |= CLIENT_LOGIN;
1.1       nicm      219:                        break;
                    220:                case 'L':
1.112     nicm      221:                        free(label);
1.1       nicm      222:                        label = xstrdup(optarg);
                    223:                        break;
1.37      nicm      224:                case 'q':
                    225:                        break;
1.1       nicm      226:                case 'S':
1.112     nicm      227:                        free(path);
1.1       nicm      228:                        path = xstrdup(optarg);
                    229:                        break;
                    230:                case 'u':
1.124     nicm      231:                        flags |= CLIENT_UTF8;
1.1       nicm      232:                        break;
                    233:                case 'v':
1.157     nicm      234:                        log_add_level();
1.1       nicm      235:                        break;
1.53      deraadt   236:                default:
1.1       nicm      237:                        usage();
1.53      deraadt   238:                }
                    239:        }
1.1       nicm      240:        argc -= optind;
                    241:        argv += optind;
                    242:
1.161     nicm      243:        if (shellcmd != NULL && argc != 0)
1.46      nicm      244:                usage();
1.145     nicm      245:
1.156     nicm      246:        if (pledge("stdio rpath wpath cpath flock fattr unix getpw sendfd "
                    247:            "recvfd proc exec tty ps", NULL) != 0)
1.145     nicm      248:                err(1, "pledge");
1.46      nicm      249:
1.152     nicm      250:        /*
                    251:         * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8.
                    252:         * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain
                    253:         * UTF-8, it is a safe assumption that either they are using a UTF-8
                    254:         * terminal, or if not they know that output from UTF-8-capable
                    255:         * programs may be wrong.
                    256:         */
                    257:        if (getenv("TMUX") != NULL)
                    258:                flags |= CLIENT_UTF8;
                    259:        else {
                    260:                s = getenv("LC_ALL");
                    261:                if (s == NULL || *s == '\0')
                    262:                        s = getenv("LC_CTYPE");
                    263:                if (s == NULL || *s == '\0')
                    264:                        s = getenv("LANG");
                    265:                if (s == NULL || *s == '\0')
                    266:                        s = "";
                    267:                if (strcasestr(s, "UTF-8") != NULL ||
                    268:                    strcasestr(s, "UTF8") != NULL)
1.124     nicm      269:                        flags |= CLIENT_UTF8;
1.15      nicm      270:        }
                    271:
1.149     nicm      272:        global_environ = environ_create();
1.62      nicm      273:        for (var = environ; *var != NULL; var++)
1.149     nicm      274:                environ_put(global_environ, *var);
1.125     nicm      275:        if (getcwd(tmp, sizeof tmp) != NULL)
1.162   ! nicm      276:                environ_set(global_environ, "PWD", "%s", tmp);
1.63      nicm      277:
1.148     nicm      278:        global_options = options_create(NULL);
1.155     nicm      279:        options_table_populate_tree(OPTIONS_TABLE_SERVER, global_options);
1.42      nicm      280:
1.148     nicm      281:        global_s_options = options_create(NULL);
1.155     nicm      282:        options_table_populate_tree(OPTIONS_TABLE_SESSION, global_s_options);
1.148     nicm      283:        options_set_string(global_s_options, "default-shell", "%s", getshell());
1.44      nicm      284:
1.148     nicm      285:        global_w_options = options_create(NULL);
1.155     nicm      286:        options_table_populate_tree(OPTIONS_TABLE_WINDOW, global_w_options);
1.94      nicm      287:
1.99      nicm      288:        /* Override keys to vi if VISUAL or EDITOR are set. */
1.94      nicm      289:        if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) {
                    290:                if (strrchr(s, '/') != NULL)
                    291:                        s = strrchr(s, '/') + 1;
                    292:                if (strstr(s, "vi") != NULL)
                    293:                        keys = MODEKEY_VI;
1.99      nicm      294:                else
                    295:                        keys = MODEKEY_EMACS;
1.148     nicm      296:                options_set_number(global_s_options, "status-keys", keys);
                    297:                options_set_number(global_w_options, "mode-keys", keys);
1.1       nicm      298:        }
1.62      nicm      299:
1.68      nicm      300:        /*
1.160     nicm      301:         * If socket is specified on the command-line with -S or -L, it is
                    302:         * used. Otherwise, $TMUX is checked and if that fails "default" is
                    303:         * used.
1.68      nicm      304:         */
1.160     nicm      305:        if (path == NULL && label == NULL) {
                    306:                s = getenv("TMUX");
                    307:                if (s != NULL && *s != '\0' && *s != ',') {
                    308:                        path = xstrdup(s);
                    309:                        path[strcspn (path, ",")] = '\0';
1.68      nicm      310:                }
1.1       nicm      311:        }
1.160     nicm      312:        if (path == NULL && (path = make_label(label)) == NULL) {
                    313:                fprintf(stderr, "can't create socket: %s\n", strerror(errno));
1.127     nicm      314:                exit(1);
                    315:        }
1.160     nicm      316:        socket_path = path;
                    317:        free(label);
1.1       nicm      318:
1.92      nicm      319:        /* Pass control to the client. */
1.161     nicm      320:        exit(client_main(event_init(), argc, argv, flags, shellcmd));
1.1       nicm      321: }