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

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