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