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

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