Annotation of src/usr.bin/tmux/tmux.c, Revision 1.209
1.209 ! nicm 1: /* $OpenBSD: tmux.c,v 1.208 2021/07/06 08:26:00 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.204 nicm 60: "usage: %s [-2CDlNuvV] [-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:
1.205 nicm 145: static void
146: expand_paths(const char *s, char ***paths, u_int *n, int ignore_errors)
1.199 nicm 147: {
148: const char *home = find_home();
149: char *copy, *next, *tmp, resolved[PATH_MAX], *expanded;
1.205 nicm 150: char *path;
1.199 nicm 151: u_int i;
152:
153: *paths = NULL;
154: *n = 0;
155:
156: copy = tmp = xstrdup(s);
157: while ((next = strsep(&tmp, ":")) != NULL) {
158: expanded = expand_path(next, home);
159: if (expanded == NULL) {
160: log_debug("%s: invalid path: %s", __func__, next);
161: continue;
162: }
163: if (realpath(expanded, resolved) == NULL) {
164: log_debug("%s: realpath(\"%s\") failed: %s", __func__,
165: expanded, strerror(errno));
1.205 nicm 166: if (ignore_errors) {
167: free(expanded);
168: continue;
169: }
170: path = expanded;
171: } else {
172: path = xstrdup(resolved);
1.199 nicm 173: free(expanded);
174: }
175: for (i = 0; i < *n; i++) {
1.205 nicm 176: if (strcmp(path, (*paths)[i]) == 0)
1.199 nicm 177: break;
178: }
179: if (i != *n) {
1.205 nicm 180: log_debug("%s: duplicate path: %s", __func__, path);
181: free(path);
1.199 nicm 182: continue;
183: }
184: *paths = xreallocarray(*paths, (*n) + 1, sizeof *paths);
1.205 nicm 185: (*paths)[(*n)++] = path;
1.199 nicm 186: }
187: free(copy);
188: }
189:
190: static char *
1.186 nicm 191: make_label(const char *label, char **cause)
1.1 nicm 192: {
1.199 nicm 193: char **paths, *path, *base;
194: u_int i, n;
195: struct stat sb;
196: uid_t uid;
1.186 nicm 197:
198: *cause = NULL;
1.160 nicm 199: if (label == NULL)
200: label = "default";
1.1 nicm 201: uid = getuid();
1.160 nicm 202:
1.205 nicm 203: expand_paths(TMUX_SOCK, &paths, &n, 1);
1.199 nicm 204: if (n == 0) {
205: xasprintf(cause, "no suitable socket path");
206: return (NULL);
1.186 nicm 207: }
1.199 nicm 208: path = paths[0]; /* can only have one socket! */
209: for (i = 1; i < n; i++)
210: free(paths[i]);
211: free(paths);
1.1 nicm 212:
1.199 nicm 213: xasprintf(&base, "%s/tmux-%ld", path, (long)uid);
1.209 ! nicm 214: free(path);
1.208 nicm 215: if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST) {
216: xasprintf(cause, "couldn't create directory %s (%s)", base,
217: strerror(errno));
1.160 nicm 218: goto fail;
1.208 nicm 219: }
220: if (lstat(base, &sb) != 0) {
221: xasprintf(cause, "couldn't read directory %s (%s)", base,
222: strerror(errno));
1.160 nicm 223: goto fail;
1.208 nicm 224: }
1.1 nicm 225: if (!S_ISDIR(sb.st_mode)) {
1.208 nicm 226: xasprintf(cause, "%s is not a directory", base);
1.160 nicm 227: goto fail;
1.1 nicm 228: }
1.134 nicm 229: if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) {
1.208 nicm 230: xasprintf(cause, "directory %s has unsafe permissions", base);
1.160 nicm 231: goto fail;
1.1 nicm 232: }
1.199 nicm 233: xasprintf(&path, "%s/%s", base, label);
234: free(base);
1.160 nicm 235: return (path);
1.113 nicm 236:
1.160 nicm 237: fail:
1.199 nicm 238: free(base);
1.160 nicm 239: return (NULL);
1.1 nicm 240: }
241:
1.101 nicm 242: void
243: setblocking(int fd, int state)
244: {
245: int mode;
246:
247: if ((mode = fcntl(fd, F_GETFL)) != -1) {
248: if (!state)
249: mode |= O_NONBLOCK;
250: else
251: mode &= ~O_NONBLOCK;
252: fcntl(fd, F_SETFL, mode);
253: }
1.202 nicm 254: }
255:
256: uint64_t
257: get_timer(void)
258: {
259: struct timespec ts;
260:
261: /*
262: * We want a timestamp in milliseconds suitable for time measurement,
263: * so prefer the monotonic clock.
264: */
265: if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
266: clock_gettime(CLOCK_REALTIME, &ts);
267: return ((ts.tv_sec * 1000ULL) + (ts.tv_nsec / 1000000ULL));
1.197 nicm 268: }
269:
270: const char *
271: sig2name(int signo)
272: {
273: static char s[11];
274:
275: if (signo > 0 && signo < NSIG)
276: return (sys_signame[signo]);
277: xsnprintf(s, sizeof s, "%d", signo);
278: return (s);
1.101 nicm 279: }
280:
1.138 nicm 281: const char *
1.187 nicm 282: find_cwd(void)
283: {
284: char resolved1[PATH_MAX], resolved2[PATH_MAX];
285: static char cwd[PATH_MAX];
286: const char *pwd;
287:
288: if (getcwd(cwd, sizeof cwd) == NULL)
289: return (NULL);
290: if ((pwd = getenv("PWD")) == NULL || *pwd == '\0')
291: return (cwd);
292:
293: /*
294: * We want to use PWD so that symbolic links are maintained,
295: * but only if it matches the actual working directory.
296: */
297: if (realpath(pwd, resolved1) == NULL)
298: return (cwd);
299: if (realpath(cwd, resolved2) == NULL)
300: return (cwd);
301: if (strcmp(resolved1, resolved2) != 0)
302: return (cwd);
303: return (pwd);
304: }
305:
306: const char *
1.137 nicm 307: find_home(void)
308: {
1.142 nicm 309: struct passwd *pw;
310: static const char *home;
311:
312: if (home != NULL)
313: return (home);
1.137 nicm 314:
315: home = getenv("HOME");
316: if (home == NULL || *home == '\0') {
317: pw = getpwuid(getuid());
318: if (pw != NULL)
319: home = pw->pw_dir;
320: else
321: home = NULL;
322: }
323:
1.138 nicm 324: return (home);
1.137 nicm 325: }
326:
1.191 nicm 327: const char *
328: getversion(void)
329: {
330: static char *version;
331: struct utsname u;
332:
333: if (version == NULL) {
334: if (uname(&u) < 0)
335: fatalx("uname failed");
336: xasprintf(&version, "openbsd-%s", u.release);
337: }
1.196 nicm 338: return (version);
1.191 nicm 339: }
340:
1.1 nicm 341: int
342: main(int argc, char **argv)
343: {
1.198 nicm 344: char *path = NULL, *label = NULL;
345: char *cause, **var;
1.205 nicm 346: const char *s, *cwd;
1.207 nicm 347: int opt, keys, feat = 0, fflag = 0;
1.203 nicm 348: uint64_t flags = 0;
1.175 nicm 349: const struct options_table_entry *oe;
1.205 nicm 350: u_int i;
1.165 nicm 351:
1.183 nicm 352: if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL &&
353: setlocale(LC_CTYPE, "C.UTF-8") == NULL) {
1.168 nicm 354: if (setlocale(LC_CTYPE, "") == NULL)
355: errx(1, "invalid LC_ALL, LC_CTYPE or LANG");
356: s = nl_langinfo(CODESET);
1.169 nicm 357: if (strcasecmp(s, "UTF-8") != 0 && strcasecmp(s, "UTF8") != 0)
1.168 nicm 358: errx(1, "need UTF-8 locale (LC_CTYPE) but have %s", s);
359: }
1.167 nicm 360:
1.119 nicm 361: setlocale(LC_TIME, "");
1.144 nicm 362: tzset();
1.1 nicm 363:
1.140 nicm 364: if (**argv == '-')
365: flags = CLIENT_LOGIN;
1.206 nicm 366:
367: global_environ = environ_create();
368: for (var = environ; *var != NULL; var++)
369: environ_put(global_environ, *var, 0);
370: if ((cwd = find_cwd()) != NULL)
371: environ_set(global_environ, "PWD", 0, "%s", cwd);
1.205 nicm 372: expand_paths(TMUX_CONF, &cfg_files, &cfg_nfiles, 1);
1.140 nicm 373:
1.204 nicm 374: while ((opt = getopt(argc, argv, "2c:CDdf:lL:NqS:T:uUvV")) != -1) {
1.41 nicm 375: switch (opt) {
1.1 nicm 376: case '2':
1.198 nicm 377: tty_add_features(&feat, "256", ":,");
1.1 nicm 378: break;
1.46 nicm 379: case 'c':
1.184 nicm 380: shell_command = optarg;
1.111 nicm 381: break;
1.201 nicm 382: case 'D':
383: flags |= CLIENT_NOFORK;
384: break;
1.111 nicm 385: case 'C':
1.124 nicm 386: if (flags & CLIENT_CONTROL)
387: flags |= CLIENT_CONTROLCONTROL;
1.111 nicm 388: else
1.124 nicm 389: flags |= CLIENT_CONTROL;
1.37 nicm 390: break;
1.1 nicm 391: case 'f':
1.207 nicm 392: if (!fflag) {
393: fflag = 1;
394: for (i = 0; i < cfg_nfiles; i++)
395: free(cfg_files[i]);
396: cfg_nfiles = 0;
397: }
398: cfg_files = xreallocarray(cfg_files, cfg_nfiles + 1,
399: sizeof *cfg_files);
400: cfg_files[cfg_nfiles++] = xstrdup(optarg);
1.205 nicm 401: cfg_quiet = 0;
1.41 nicm 402: break;
1.191 nicm 403: case 'V':
404: printf("%s %s\n", getprogname(), getversion());
405: exit(0);
1.41 nicm 406: case 'l':
1.140 nicm 407: flags |= CLIENT_LOGIN;
1.1 nicm 408: break;
409: case 'L':
1.112 nicm 410: free(label);
1.1 nicm 411: label = xstrdup(optarg);
1.204 nicm 412: break;
413: case 'N':
414: flags |= CLIENT_NOSTARTSERVER;
1.1 nicm 415: break;
1.37 nicm 416: case 'q':
417: break;
1.1 nicm 418: case 'S':
1.112 nicm 419: free(path);
1.1 nicm 420: path = xstrdup(optarg);
421: break;
1.198 nicm 422: case 'T':
423: tty_add_features(&feat, optarg, ":,");
424: break;
1.1 nicm 425: case 'u':
1.124 nicm 426: flags |= CLIENT_UTF8;
1.1 nicm 427: break;
428: case 'v':
1.157 nicm 429: log_add_level();
1.1 nicm 430: break;
1.53 deraadt 431: default:
1.1 nicm 432: usage();
1.53 deraadt 433: }
434: }
1.1 nicm 435: argc -= optind;
436: argv += optind;
437:
1.184 nicm 438: if (shell_command != NULL && argc != 0)
1.201 nicm 439: usage();
440: if ((flags & CLIENT_NOFORK) && argc != 0)
1.46 nicm 441: usage();
1.145 nicm 442:
1.181 nicm 443: if ((ptm_fd = getptmfd()) == -1)
444: err(1, "getptmfd");
1.156 nicm 445: if (pledge("stdio rpath wpath cpath flock fattr unix getpw sendfd "
446: "recvfd proc exec tty ps", NULL) != 0)
1.145 nicm 447: err(1, "pledge");
1.46 nicm 448:
1.152 nicm 449: /*
450: * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8.
451: * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain
452: * UTF-8, it is a safe assumption that either they are using a UTF-8
453: * terminal, or if not they know that output from UTF-8-capable
454: * programs may be wrong.
455: */
456: if (getenv("TMUX") != NULL)
457: flags |= CLIENT_UTF8;
458: else {
459: s = getenv("LC_ALL");
460: if (s == NULL || *s == '\0')
461: s = getenv("LC_CTYPE");
462: if (s == NULL || *s == '\0')
463: s = getenv("LANG");
464: if (s == NULL || *s == '\0')
465: s = "";
466: if (strcasestr(s, "UTF-8") != NULL ||
467: strcasestr(s, "UTF8") != NULL)
1.124 nicm 468: flags |= CLIENT_UTF8;
1.15 nicm 469: }
1.63 nicm 470:
1.148 nicm 471: global_options = options_create(NULL);
472: global_s_options = options_create(NULL);
1.175 nicm 473: global_w_options = options_create(NULL);
474: for (oe = options_table; oe->name != NULL; oe++) {
1.189 nicm 475: if (oe->scope & OPTIONS_TABLE_SERVER)
1.175 nicm 476: options_default(global_options, oe);
1.189 nicm 477: if (oe->scope & OPTIONS_TABLE_SESSION)
1.175 nicm 478: options_default(global_s_options, oe);
1.189 nicm 479: if (oe->scope & OPTIONS_TABLE_WINDOW)
1.175 nicm 480: options_default(global_w_options, oe);
481: }
1.44 nicm 482:
1.175 nicm 483: /*
484: * The default shell comes from SHELL or from the user's passwd entry
485: * if available.
486: */
1.205 nicm 487: options_set_string(global_s_options, "default-shell", 0, "%s",
488: getshell());
1.94 nicm 489:
1.99 nicm 490: /* Override keys to vi if VISUAL or EDITOR are set. */
1.94 nicm 491: if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) {
1.200 nicm 492: options_set_string(global_options, "editor", 0, "%s", s);
1.94 nicm 493: if (strrchr(s, '/') != NULL)
494: s = strrchr(s, '/') + 1;
495: if (strstr(s, "vi") != NULL)
496: keys = MODEKEY_VI;
1.99 nicm 497: else
498: keys = MODEKEY_EMACS;
1.148 nicm 499: options_set_number(global_s_options, "status-keys", keys);
500: options_set_number(global_w_options, "mode-keys", keys);
1.1 nicm 501: }
1.62 nicm 502:
1.68 nicm 503: /*
1.160 nicm 504: * If socket is specified on the command-line with -S or -L, it is
505: * used. Otherwise, $TMUX is checked and if that fails "default" is
506: * used.
1.68 nicm 507: */
1.160 nicm 508: if (path == NULL && label == NULL) {
509: s = getenv("TMUX");
510: if (s != NULL && *s != '\0' && *s != ',') {
511: path = xstrdup(s);
1.173 nicm 512: path[strcspn(path, ",")] = '\0';
1.68 nicm 513: }
1.1 nicm 514: }
1.193 nicm 515: if (path == NULL) {
516: if ((path = make_label(label, &cause)) == NULL) {
517: if (cause != NULL) {
518: fprintf(stderr, "%s\n", cause);
519: free(cause);
520: }
521: exit(1);
1.186 nicm 522: }
1.193 nicm 523: flags |= CLIENT_DEFAULTSOCKET;
1.127 nicm 524: }
1.160 nicm 525: socket_path = path;
526: free(label);
1.1 nicm 527:
1.92 nicm 528: /* Pass control to the client. */
1.198 nicm 529: exit(client_main(event_init(), argc, argv, flags, feat));
1.1 nicm 530: }