Annotation of src/usr.bin/tmux/tmux.c, Revision 1.199
1.199 ! nicm 1: /* $OpenBSD: tmux.c,v 1.198 2020/04/20 13:25:36 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>
1.191 nicm 21: #include <sys/utsname.h>
1.1 nicm 22:
1.145 nicm 23: #include <err.h>
1.1 nicm 24: #include <errno.h>
1.55 nicm 25: #include <event.h>
1.91 nicm 26: #include <fcntl.h>
1.168 nicm 27: #include <langinfo.h>
1.119 nicm 28: #include <locale.h>
1.1 nicm 29: #include <paths.h>
30: #include <pwd.h>
1.197 nicm 31: #include <signal.h>
1.1 nicm 32: #include <stdlib.h>
33: #include <string.h>
1.147 nicm 34: #include <time.h>
1.1 nicm 35: #include <unistd.h>
1.181 nicm 36: #include <util.h>
1.1 nicm 37:
38: #include "tmux.h"
39:
1.148 nicm 40: struct options *global_options; /* server options */
41: struct options *global_s_options; /* session options */
42: struct options *global_w_options; /* window options */
1.149 nicm 43: struct environ *global_environ;
1.1 nicm 44:
1.158 nicm 45: struct timeval start_time;
1.160 nicm 46: const char *socket_path;
1.176 nicm 47: int ptm_fd = -1;
1.184 nicm 48: const char *shell_command;
1.70 nicm 49:
1.171 nicm 50: static __dead void usage(void);
1.186 nicm 51: static char *make_label(const char *, char **);
1.55 nicm 52:
1.194 nicm 53: static int areshell(const char *);
1.172 nicm 54: static const char *getshell(void);
55:
1.171 nicm 56: static __dead void
1.1 nicm 57: usage(void)
58: {
1.4 sobrado 59: fprintf(stderr,
1.192 nicm 60: "usage: %s [-2CluvV] [-c shell-command] [-f file] [-L socket-name]\n"
1.198 nicm 61: " [-S socket-path] [-T features] [command [flags]]\n",
1.170 nicm 62: getprogname());
1.1 nicm 63: exit(1);
64: }
65:
1.172 nicm 66: static const char *
1.39 nicm 67: getshell(void)
68: {
69: struct passwd *pw;
70: const char *shell;
71:
72: shell = getenv("SHELL");
73: if (checkshell(shell))
74: return (shell);
75:
76: pw = getpwuid(getuid());
77: if (pw != NULL && checkshell(pw->pw_shell))
78: return (pw->pw_shell);
79:
80: return (_PATH_BSHELL);
81: }
82:
1.194 nicm 83: int
1.39 nicm 84: checkshell(const char *shell)
85: {
1.177 nicm 86: if (shell == NULL || *shell != '/')
1.105 nicm 87: return (0);
88: if (areshell(shell))
1.39 nicm 89: return (0);
90: if (access(shell, X_OK) != 0)
91: return (0);
92: return (1);
93: }
94:
1.194 nicm 95: static int
1.39 nicm 96: areshell(const char *shell)
97: {
98: const char *progname, *ptr;
99:
100: if ((ptr = strrchr(shell, '/')) != NULL)
101: ptr++;
102: else
103: ptr = shell;
1.170 nicm 104: progname = getprogname();
1.39 nicm 105: if (*progname == '-')
106: progname++;
107: if (strcmp(ptr, progname) == 0)
108: return (1);
109: return (0);
1.107 nicm 110: }
111:
1.160 nicm 112: static char *
1.199 ! nicm 113: expand_path(const char *path, const char *home)
! 114: {
! 115: char *expanded, *name;
! 116: const char *end;
! 117: struct environ_entry *value;
! 118:
! 119: if (strncmp(path, "~/", 2) == 0) {
! 120: if (home == NULL)
! 121: return (NULL);
! 122: xasprintf(&expanded, "%s%s", home, path + 1);
! 123: return (expanded);
! 124: }
! 125:
! 126: if (*path == '$') {
! 127: end = strchr(path, '/');
! 128: if (end == NULL)
! 129: name = xstrdup(path + 1);
! 130: else
! 131: name = xstrndup(path + 1, end - path - 1);
! 132: value = environ_find(global_environ, name);
! 133: free(name);
! 134: if (value == NULL)
! 135: return (NULL);
! 136: if (end == NULL)
! 137: end = "";
! 138: xasprintf(&expanded, "%s%s", value->value, end);
! 139: return (expanded);
! 140: }
! 141:
! 142: return (xstrdup(path));
! 143: }
! 144:
! 145: void
! 146: expand_paths(const char *s, char ***paths, u_int *n)
! 147: {
! 148: const char *home = find_home();
! 149: char *copy, *next, *tmp, resolved[PATH_MAX], *expanded;
! 150: u_int i;
! 151:
! 152: *paths = NULL;
! 153: *n = 0;
! 154:
! 155: copy = tmp = xstrdup(s);
! 156: while ((next = strsep(&tmp, ":")) != NULL) {
! 157: expanded = expand_path(next, home);
! 158: if (expanded == NULL) {
! 159: log_debug("%s: invalid path: %s", __func__, next);
! 160: continue;
! 161: }
! 162: if (realpath(expanded, resolved) == NULL) {
! 163: log_debug("%s: realpath(\"%s\") failed: %s", __func__,
! 164: expanded, strerror(errno));
! 165: free(expanded);
! 166: continue;
! 167: }
! 168: free(expanded);
! 169: for (i = 0; i < *n; i++) {
! 170: if (strcmp(resolved, (*paths)[i]) == 0)
! 171: break;
! 172: }
! 173: if (i != *n) {
! 174: log_debug("%s: duplicate path: %s", __func__, resolved);
! 175: continue;
! 176: }
! 177: *paths = xreallocarray(*paths, (*n) + 1, sizeof *paths);
! 178: (*paths)[(*n)++] = xstrdup(resolved);
! 179: }
! 180: free(copy);
! 181: }
! 182:
! 183: static char *
1.186 nicm 184: make_label(const char *label, char **cause)
1.1 nicm 185: {
1.199 ! nicm 186: char **paths, *path, *base;
! 187: u_int i, n;
! 188: struct stat sb;
! 189: uid_t uid;
1.186 nicm 190:
191: *cause = NULL;
1.160 nicm 192: if (label == NULL)
193: label = "default";
1.1 nicm 194: uid = getuid();
1.160 nicm 195:
1.199 ! nicm 196: expand_paths(TMUX_SOCK, &paths, &n);
! 197: if (n == 0) {
! 198: xasprintf(cause, "no suitable socket path");
! 199: return (NULL);
1.186 nicm 200: }
1.199 ! nicm 201: path = paths[0]; /* can only have one socket! */
! 202: for (i = 1; i < n; i++)
! 203: free(paths[i]);
! 204: free(paths);
1.1 nicm 205:
1.199 ! nicm 206: xasprintf(&base, "%s/tmux-%ld", path, (long)uid);
! 207: if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
1.160 nicm 208: goto fail;
1.199 ! nicm 209: if (lstat(base, &sb) != 0)
1.160 nicm 210: goto fail;
1.1 nicm 211: if (!S_ISDIR(sb.st_mode)) {
212: errno = ENOTDIR;
1.160 nicm 213: goto fail;
1.1 nicm 214: }
1.134 nicm 215: if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) {
1.1 nicm 216: errno = EACCES;
1.160 nicm 217: goto fail;
1.1 nicm 218: }
1.199 ! nicm 219: xasprintf(&path, "%s/%s", base, label);
! 220: free(base);
1.160 nicm 221: return (path);
1.113 nicm 222:
1.160 nicm 223: fail:
1.199 ! nicm 224: xasprintf(cause, "error creating %s (%s)", base, strerror(errno));
! 225: free(base);
1.160 nicm 226: return (NULL);
1.1 nicm 227: }
228:
1.101 nicm 229: void
230: setblocking(int fd, int state)
231: {
232: int mode;
233:
234: if ((mode = fcntl(fd, F_GETFL)) != -1) {
235: if (!state)
236: mode |= O_NONBLOCK;
237: else
238: mode &= ~O_NONBLOCK;
239: fcntl(fd, F_SETFL, mode);
240: }
1.197 nicm 241: }
242:
243: const char *
244: sig2name(int signo)
245: {
246: static char s[11];
247:
248: if (signo > 0 && signo < NSIG)
249: return (sys_signame[signo]);
250: xsnprintf(s, sizeof s, "%d", signo);
251: return (s);
1.101 nicm 252: }
253:
1.138 nicm 254: const char *
1.187 nicm 255: find_cwd(void)
256: {
257: char resolved1[PATH_MAX], resolved2[PATH_MAX];
258: static char cwd[PATH_MAX];
259: const char *pwd;
260:
261: if (getcwd(cwd, sizeof cwd) == NULL)
262: return (NULL);
263: if ((pwd = getenv("PWD")) == NULL || *pwd == '\0')
264: return (cwd);
265:
266: /*
267: * We want to use PWD so that symbolic links are maintained,
268: * but only if it matches the actual working directory.
269: */
270: if (realpath(pwd, resolved1) == NULL)
271: return (cwd);
272: if (realpath(cwd, resolved2) == NULL)
273: return (cwd);
274: if (strcmp(resolved1, resolved2) != 0)
275: return (cwd);
276: return (pwd);
277: }
278:
279: const char *
1.137 nicm 280: find_home(void)
281: {
1.142 nicm 282: struct passwd *pw;
283: static const char *home;
284:
285: if (home != NULL)
286: return (home);
1.137 nicm 287:
288: home = getenv("HOME");
289: if (home == NULL || *home == '\0') {
290: pw = getpwuid(getuid());
291: if (pw != NULL)
292: home = pw->pw_dir;
293: else
294: home = NULL;
295: }
296:
1.138 nicm 297: return (home);
1.137 nicm 298: }
299:
1.191 nicm 300: const char *
301: getversion(void)
302: {
303: static char *version;
304: struct utsname u;
305:
306: if (version == NULL) {
307: if (uname(&u) < 0)
308: fatalx("uname failed");
309: xasprintf(&version, "openbsd-%s", u.release);
310: }
1.196 nicm 311: return (version);
1.191 nicm 312: }
313:
1.1 nicm 314: int
315: main(int argc, char **argv)
316: {
1.198 nicm 317: char *path = NULL, *label = NULL;
318: char *cause, **var;
1.185 nicm 319: const char *s, *shell, *cwd;
1.198 nicm 320: int opt, flags = 0, keys;
321: int feat = 0;
1.175 nicm 322: const struct options_table_entry *oe;
1.165 nicm 323:
1.183 nicm 324: if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL &&
325: setlocale(LC_CTYPE, "C.UTF-8") == NULL) {
1.168 nicm 326: if (setlocale(LC_CTYPE, "") == NULL)
327: errx(1, "invalid LC_ALL, LC_CTYPE or LANG");
328: s = nl_langinfo(CODESET);
1.169 nicm 329: if (strcasecmp(s, "UTF-8") != 0 && strcasecmp(s, "UTF8") != 0)
1.168 nicm 330: errx(1, "need UTF-8 locale (LC_CTYPE) but have %s", s);
331: }
1.167 nicm 332:
1.119 nicm 333: setlocale(LC_TIME, "");
1.144 nicm 334: tzset();
1.1 nicm 335:
1.140 nicm 336: if (**argv == '-')
337: flags = CLIENT_LOGIN;
338:
1.198 nicm 339: while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:T:uUvV")) != -1) {
1.41 nicm 340: switch (opt) {
1.1 nicm 341: case '2':
1.198 nicm 342: tty_add_features(&feat, "256", ":,");
1.1 nicm 343: break;
1.46 nicm 344: case 'c':
1.184 nicm 345: shell_command = optarg;
1.111 nicm 346: break;
347: case 'C':
1.124 nicm 348: if (flags & CLIENT_CONTROL)
349: flags |= CLIENT_CONTROLCONTROL;
1.111 nicm 350: else
1.124 nicm 351: flags |= CLIENT_CONTROL;
1.37 nicm 352: break;
1.1 nicm 353: case 'f':
1.142 nicm 354: set_cfg_file(optarg);
1.41 nicm 355: break;
1.191 nicm 356: case 'V':
357: printf("%s %s\n", getprogname(), getversion());
358: exit(0);
1.41 nicm 359: case 'l':
1.140 nicm 360: flags |= CLIENT_LOGIN;
1.1 nicm 361: break;
362: case 'L':
1.112 nicm 363: free(label);
1.1 nicm 364: label = xstrdup(optarg);
365: break;
1.37 nicm 366: case 'q':
367: break;
1.1 nicm 368: case 'S':
1.112 nicm 369: free(path);
1.1 nicm 370: path = xstrdup(optarg);
371: break;
1.198 nicm 372: case 'T':
373: tty_add_features(&feat, optarg, ":,");
374: break;
1.1 nicm 375: case 'u':
1.124 nicm 376: flags |= CLIENT_UTF8;
1.1 nicm 377: break;
378: case 'v':
1.157 nicm 379: log_add_level();
1.1 nicm 380: break;
1.53 deraadt 381: default:
1.1 nicm 382: usage();
1.53 deraadt 383: }
384: }
1.1 nicm 385: argc -= optind;
386: argv += optind;
387:
1.184 nicm 388: if (shell_command != NULL && argc != 0)
1.46 nicm 389: usage();
1.145 nicm 390:
1.181 nicm 391: if ((ptm_fd = getptmfd()) == -1)
392: err(1, "getptmfd");
1.156 nicm 393: if (pledge("stdio rpath wpath cpath flock fattr unix getpw sendfd "
394: "recvfd proc exec tty ps", NULL) != 0)
1.145 nicm 395: err(1, "pledge");
1.46 nicm 396:
1.152 nicm 397: /*
398: * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8.
399: * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain
400: * UTF-8, it is a safe assumption that either they are using a UTF-8
401: * terminal, or if not they know that output from UTF-8-capable
402: * programs may be wrong.
403: */
404: if (getenv("TMUX") != NULL)
405: flags |= CLIENT_UTF8;
406: else {
407: s = getenv("LC_ALL");
408: if (s == NULL || *s == '\0')
409: s = getenv("LC_CTYPE");
410: if (s == NULL || *s == '\0')
411: s = getenv("LANG");
412: if (s == NULL || *s == '\0')
413: s = "";
414: if (strcasestr(s, "UTF-8") != NULL ||
415: strcasestr(s, "UTF8") != NULL)
1.124 nicm 416: flags |= CLIENT_UTF8;
1.15 nicm 417: }
418:
1.149 nicm 419: global_environ = environ_create();
1.62 nicm 420: for (var = environ; *var != NULL; var++)
1.195 nicm 421: environ_put(global_environ, *var, 0);
1.187 nicm 422: if ((cwd = find_cwd()) != NULL)
1.195 nicm 423: environ_set(global_environ, "PWD", 0, "%s", cwd);
1.63 nicm 424:
1.148 nicm 425: global_options = options_create(NULL);
426: global_s_options = options_create(NULL);
1.175 nicm 427: global_w_options = options_create(NULL);
428: for (oe = options_table; oe->name != NULL; oe++) {
1.189 nicm 429: if (oe->scope & OPTIONS_TABLE_SERVER)
1.175 nicm 430: options_default(global_options, oe);
1.189 nicm 431: if (oe->scope & OPTIONS_TABLE_SESSION)
1.175 nicm 432: options_default(global_s_options, oe);
1.189 nicm 433: if (oe->scope & OPTIONS_TABLE_WINDOW)
1.175 nicm 434: options_default(global_w_options, oe);
435: }
1.44 nicm 436:
1.175 nicm 437: /*
438: * The default shell comes from SHELL or from the user's passwd entry
439: * if available.
440: */
441: shell = getshell();
442: options_set_string(global_s_options, "default-shell", 0, "%s", shell);
1.94 nicm 443:
1.99 nicm 444: /* Override keys to vi if VISUAL or EDITOR are set. */
1.94 nicm 445: if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) {
446: if (strrchr(s, '/') != NULL)
447: s = strrchr(s, '/') + 1;
448: if (strstr(s, "vi") != NULL)
449: keys = MODEKEY_VI;
1.99 nicm 450: else
451: keys = MODEKEY_EMACS;
1.148 nicm 452: options_set_number(global_s_options, "status-keys", keys);
453: options_set_number(global_w_options, "mode-keys", keys);
1.1 nicm 454: }
1.62 nicm 455:
1.68 nicm 456: /*
1.160 nicm 457: * If socket is specified on the command-line with -S or -L, it is
458: * used. Otherwise, $TMUX is checked and if that fails "default" is
459: * used.
1.68 nicm 460: */
1.160 nicm 461: if (path == NULL && label == NULL) {
462: s = getenv("TMUX");
463: if (s != NULL && *s != '\0' && *s != ',') {
464: path = xstrdup(s);
1.173 nicm 465: path[strcspn(path, ",")] = '\0';
1.68 nicm 466: }
1.1 nicm 467: }
1.193 nicm 468: if (path == NULL) {
469: if ((path = make_label(label, &cause)) == NULL) {
470: if (cause != NULL) {
471: fprintf(stderr, "%s\n", cause);
472: free(cause);
473: }
474: exit(1);
1.186 nicm 475: }
1.193 nicm 476: flags |= CLIENT_DEFAULTSOCKET;
1.127 nicm 477: }
1.160 nicm 478: socket_path = path;
479: free(label);
1.1 nicm 480:
1.92 nicm 481: /* Pass control to the client. */
1.198 nicm 482: exit(client_main(event_init(), argc, argv, flags, feat));
1.1 nicm 483: }