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

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