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