tmux.c revision 1.168
1/* $OpenBSD: tmux.c,v 1.168 2016/03/05 16:08:38 nicm Exp $ */
2
3/*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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
22#include <err.h>
23#include <errno.h>
24#include <event.h>
25#include <fcntl.h>
26#include <getopt.h>
27#include <langinfo.h>
28#include <locale.h>
29#include <paths.h>
30#include <pwd.h>
31#include <stdlib.h>
32#include <string.h>
33#include <time.h>
34#include <unistd.h>
35
36#include "tmux.h"
37
38struct options	*global_options;	/* server options */
39struct options	*global_s_options;	/* session options */
40struct options	*global_w_options;	/* window options */
41struct environ	*global_environ;
42struct hooks	*global_hooks;
43
44struct timeval	 start_time;
45const char	*socket_path;
46
47__dead void	 usage(void);
48static char	*make_label(const char *);
49
50__dead void
51usage(void)
52{
53	fprintf(stderr,
54	    "usage: %s [-2Cluv] [-c shell-command] [-f file] [-L socket-name]\n"
55	    "            [-S socket-path] [command [flags]]\n",
56	    __progname);
57	exit(1);
58}
59
60const char *
61getshell(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
77int
78checkshell(const char *shell)
79{
80	if (shell == NULL || *shell == '\0' || *shell != '/')
81		return (0);
82	if (areshell(shell))
83		return (0);
84	if (access(shell, X_OK) != 0)
85		return (0);
86	return (1);
87}
88
89int
90areshell(const char *shell)
91{
92	const char	*progname, *ptr;
93
94	if ((ptr = strrchr(shell, '/')) != NULL)
95		ptr++;
96	else
97		ptr = shell;
98	progname = __progname;
99	if (*progname == '-')
100		progname++;
101	if (strcmp(ptr, progname) == 0)
102		return (1);
103	return (0);
104}
105
106static char *
107make_label(const char *label)
108{
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";
116
117	uid = getuid();
118
119	if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0')
120		xasprintf(&base, "%s/tmux-%u", s, uid);
121	else
122		xasprintf(&base, "%s/tmux-%u", _PATH_TMP, uid);
123
124	if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
125		goto fail;
126
127	if (lstat(base, &sb) != 0)
128		goto fail;
129	if (!S_ISDIR(sb.st_mode)) {
130		errno = ENOTDIR;
131		goto fail;
132	}
133	if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) {
134		errno = EACCES;
135		goto fail;
136	}
137
138	if (realpath(base, resolved) == NULL)
139		strlcpy(resolved, base, sizeof resolved);
140	xasprintf(&path, "%s/%s", resolved, label);
141	return (path);
142
143fail:
144	saved_errno = errno;
145	free(base);
146	errno = saved_errno;
147	return (NULL);
148}
149
150void
151setblocking(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
164const char *
165find_home(void)
166{
167	struct passwd		*pw;
168	static const char	*home;
169
170	if (home != NULL)
171		return (home);
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
182	return (home);
183}
184
185int
186main(int argc, char **argv)
187{
188	char		*path, *label, **var, tmp[PATH_MAX], *shellcmd = NULL;
189	const char	*s;
190	int		 opt, flags, keys;
191
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);
196		if (strcasecmp(s, "UTF-8") != 0 &&
197		    strcasecmp(s, "UTF8") != 0)
198			errx(1, "need UTF-8 locale (LC_CTYPE) but have %s", s);
199	}
200
201	setlocale(LC_TIME, "");
202	tzset();
203
204	if (**argv == '-')
205		flags = CLIENT_LOGIN;
206	else
207		flags = 0;
208
209	label = path = NULL;
210	while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:uUv")) != -1) {
211		switch (opt) {
212		case '2':
213			flags |= CLIENT_256COLOURS;
214			break;
215		case 'c':
216			free(shellcmd);
217			shellcmd = xstrdup(optarg);
218			break;
219		case 'C':
220			if (flags & CLIENT_CONTROL)
221				flags |= CLIENT_CONTROLCONTROL;
222			else
223				flags |= CLIENT_CONTROL;
224			break;
225		case 'f':
226			set_cfg_file(optarg);
227			break;
228		case 'l':
229			flags |= CLIENT_LOGIN;
230			break;
231		case 'L':
232			free(label);
233			label = xstrdup(optarg);
234			break;
235		case 'q':
236			break;
237		case 'S':
238			free(path);
239			path = xstrdup(optarg);
240			break;
241		case 'u':
242			flags |= CLIENT_UTF8;
243			break;
244		case 'v':
245			log_add_level();
246			break;
247		default:
248			usage();
249		}
250	}
251	argc -= optind;
252	argv += optind;
253
254	if (shellcmd != NULL && argc != 0)
255		usage();
256
257	if (pledge("stdio rpath wpath cpath flock fattr unix getpw sendfd "
258	    "recvfd proc exec tty ps", NULL) != 0)
259		err(1, "pledge");
260
261	/*
262	 * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8.
263	 * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain
264	 * UTF-8, it is a safe assumption that either they are using a UTF-8
265	 * terminal, or if not they know that output from UTF-8-capable
266	 * programs may be wrong.
267	 */
268	if (getenv("TMUX") != NULL)
269		flags |= CLIENT_UTF8;
270	else {
271		s = getenv("LC_ALL");
272		if (s == NULL || *s == '\0')
273			s = getenv("LC_CTYPE");
274		if (s == NULL || *s == '\0')
275			s = getenv("LANG");
276		if (s == NULL || *s == '\0')
277			s = "";
278		if (strcasestr(s, "UTF-8") != NULL ||
279		    strcasestr(s, "UTF8") != NULL)
280			flags |= CLIENT_UTF8;
281	}
282
283	global_hooks = hooks_create(NULL);
284
285	global_environ = environ_create();
286	for (var = environ; *var != NULL; var++)
287		environ_put(global_environ, *var);
288	if (getcwd(tmp, sizeof tmp) != NULL)
289		environ_set(global_environ, "PWD", "%s", tmp);
290
291	global_options = options_create(NULL);
292	options_table_populate_tree(OPTIONS_TABLE_SERVER, global_options);
293
294	global_s_options = options_create(NULL);
295	options_table_populate_tree(OPTIONS_TABLE_SESSION, global_s_options);
296	options_set_string(global_s_options, "default-shell", "%s", getshell());
297
298	global_w_options = options_create(NULL);
299	options_table_populate_tree(OPTIONS_TABLE_WINDOW, global_w_options);
300
301	/* Override keys to vi if VISUAL or EDITOR are set. */
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;
307		else
308			keys = MODEKEY_EMACS;
309		options_set_number(global_s_options, "status-keys", keys);
310		options_set_number(global_w_options, "mode-keys", keys);
311	}
312
313	/*
314	 * If socket is specified on the command-line with -S or -L, it is
315	 * used. Otherwise, $TMUX is checked and if that fails "default" is
316	 * used.
317	 */
318	if (path == NULL && label == NULL) {
319		s = getenv("TMUX");
320		if (s != NULL && *s != '\0' && *s != ',') {
321			path = xstrdup(s);
322			path[strcspn (path, ",")] = '\0';
323		}
324	}
325	if (path == NULL && (path = make_label(label)) == NULL) {
326		fprintf(stderr, "can't create socket: %s\n", strerror(errno));
327		exit(1);
328	}
329	socket_path = path;
330	free(label);
331
332	/* Pass control to the client. */
333	exit(client_main(event_init(), argc, argv, flags, shellcmd));
334}
335