tmux.c revision 1.153
1/* $OpenBSD: tmux.c,v 1.153 2015/11/14 09:41:07 nicm Exp $ */
2
3/*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 <locale.h>
28#include <paths.h>
29#include <pwd.h>
30#include <stdlib.h>
31#include <string.h>
32#include <time.h>
33#include <unistd.h>
34
35#include "tmux.h"
36
37#ifdef DEBUG
38extern char	*malloc_options;
39#endif
40
41struct options	*global_options;	/* server options */
42struct options	*global_s_options;	/* session options */
43struct options	*global_w_options;	/* window options */
44struct environ	*global_environ;
45
46char		*shell_cmd;
47int		 debug_level;
48time_t		 start_time;
49char		 socket_path[PATH_MAX];
50
51__dead void	 usage(void);
52char 		*makesocketpath(const char *);
53
54__dead void
55usage(void)
56{
57	fprintf(stderr,
58	    "usage: %s [-2Cluv] [-c shell-command] [-f file] [-L socket-name]\n"
59	    "            [-S socket-path] [command [flags]]\n",
60	    __progname);
61	exit(1);
62}
63
64void
65logfile(const char *name)
66{
67	char	*path;
68
69	if (debug_level > 0) {
70		xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid());
71		log_open(path);
72		free(path);
73	}
74}
75
76const char *
77getshell(void)
78{
79	struct passwd	*pw;
80	const char	*shell;
81
82	shell = getenv("SHELL");
83	if (checkshell(shell))
84		return (shell);
85
86	pw = getpwuid(getuid());
87	if (pw != NULL && checkshell(pw->pw_shell))
88		return (pw->pw_shell);
89
90	return (_PATH_BSHELL);
91}
92
93int
94checkshell(const char *shell)
95{
96	if (shell == NULL || *shell == '\0' || *shell != '/')
97		return (0);
98	if (areshell(shell))
99		return (0);
100	if (access(shell, X_OK) != 0)
101		return (0);
102	return (1);
103}
104
105int
106areshell(const char *shell)
107{
108	const char	*progname, *ptr;
109
110	if ((ptr = strrchr(shell, '/')) != NULL)
111		ptr++;
112	else
113		ptr = shell;
114	progname = __progname;
115	if (*progname == '-')
116		progname++;
117	if (strcmp(ptr, progname) == 0)
118		return (1);
119	return (0);
120}
121
122char *
123makesocketpath(const char *label)
124{
125	char		base[PATH_MAX], realbase[PATH_MAX], *path, *s;
126	struct stat	sb;
127	u_int		uid;
128
129	uid = getuid();
130	if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0')
131		xsnprintf(base, sizeof base, "%s/tmux-%u", s, uid);
132	else
133		xsnprintf(base, sizeof base, "%s/tmux-%u", _PATH_TMP, uid);
134
135	if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
136		return (NULL);
137
138	if (lstat(base, &sb) != 0)
139		return (NULL);
140	if (!S_ISDIR(sb.st_mode)) {
141		errno = ENOTDIR;
142		return (NULL);
143	}
144	if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) {
145		errno = EACCES;
146		return (NULL);
147	}
148
149	if (realpath(base, realbase) == NULL)
150		strlcpy(realbase, base, sizeof realbase);
151
152	xasprintf(&path, "%s/%s", realbase, label);
153	return (path);
154}
155
156void
157setblocking(int fd, int state)
158{
159	int mode;
160
161	if ((mode = fcntl(fd, F_GETFL)) != -1) {
162		if (!state)
163			mode |= O_NONBLOCK;
164		else
165			mode &= ~O_NONBLOCK;
166		fcntl(fd, F_SETFL, mode);
167	}
168}
169
170const char *
171find_home(void)
172{
173	struct passwd		*pw;
174	static const char	*home;
175
176	if (home != NULL)
177		return (home);
178
179	home = getenv("HOME");
180	if (home == NULL || *home == '\0') {
181		pw = getpwuid(getuid());
182		if (pw != NULL)
183			home = pw->pw_dir;
184		else
185			home = NULL;
186	}
187
188	return (home);
189}
190
191int
192main(int argc, char **argv)
193{
194	char		*path, *label, **var, tmp[PATH_MAX];
195	const char	*s;
196	int		 opt, flags, keys;
197
198#ifdef DEBUG
199	malloc_options = (char *) "AFGJPX";
200#endif
201
202	setlocale(LC_TIME, "");
203	tzset();
204
205	if (**argv == '-')
206		flags = CLIENT_LOGIN;
207	else
208		flags = 0;
209
210	label = path = NULL;
211	while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:uUv")) != -1) {
212		switch (opt) {
213		case '2':
214			flags |= CLIENT_256COLOURS;
215			break;
216		case 'c':
217			free(shell_cmd);
218			shell_cmd = xstrdup(optarg);
219			break;
220		case 'C':
221			if (flags & CLIENT_CONTROL)
222				flags |= CLIENT_CONTROLCONTROL;
223			else
224				flags |= CLIENT_CONTROL;
225			break;
226		case 'f':
227			set_cfg_file(optarg);
228			break;
229		case 'l':
230			flags |= CLIENT_LOGIN;
231			break;
232		case 'L':
233			free(label);
234			label = xstrdup(optarg);
235			break;
236		case 'q':
237			break;
238		case 'S':
239			free(path);
240			path = xstrdup(optarg);
241			break;
242		case 'u':
243			flags |= CLIENT_UTF8;
244			break;
245		case 'v':
246			debug_level++;
247			break;
248		default:
249			usage();
250		}
251	}
252	argc -= optind;
253	argv += optind;
254
255	if (shell_cmd != NULL && argc != 0)
256		usage();
257
258	if (0 && pledge("stdio rpath wpath cpath flock fattr unix sendfd recvfd "
259	    "proc exec tty ps", NULL) != 0)
260		err(1, "pledge");
261
262	/*
263	 * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8.
264	 * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain
265	 * UTF-8, it is a safe assumption that either they are using a UTF-8
266	 * terminal, or if not they know that output from UTF-8-capable
267	 * programs may be wrong.
268	 */
269	if (getenv("TMUX") != NULL)
270		flags |= CLIENT_UTF8;
271	else {
272		s = getenv("LC_ALL");
273		if (s == NULL || *s == '\0')
274			s = getenv("LC_CTYPE");
275		if (s == NULL || *s == '\0')
276			s = getenv("LANG");
277		if (s == NULL || *s == '\0')
278			s = "";
279		if (strcasestr(s, "UTF-8") != NULL ||
280		    strcasestr(s, "UTF8") != NULL)
281			flags |= CLIENT_UTF8;
282	}
283
284	global_environ = environ_create();
285	for (var = environ; *var != NULL; var++)
286		environ_put(global_environ, *var);
287	if (getcwd(tmp, sizeof tmp) != NULL)
288		environ_set(global_environ, "PWD", tmp);
289
290	global_options = options_create(NULL);
291	options_table_populate_tree(server_options_table, global_options);
292
293	global_s_options = options_create(NULL);
294	options_table_populate_tree(session_options_table, global_s_options);
295	options_set_string(global_s_options, "default-shell", "%s", getshell());
296
297	global_w_options = options_create(NULL);
298	options_table_populate_tree(window_options_table, global_w_options);
299
300	/* Override keys to vi if VISUAL or EDITOR are set. */
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;
306		else
307			keys = MODEKEY_EMACS;
308		options_set_number(global_s_options, "status-keys", keys);
309		options_set_number(global_w_options, "mode-keys", keys);
310	}
311
312	/*
313	 * Figure out the socket path. If specified on the command-line with -S
314	 * or -L, use it, otherwise try $TMUX or assume -L default.
315	 */
316	if (path == NULL) {
317		/* If no -L, use the environment. */
318		if (label == NULL) {
319			s = getenv("TMUX");
320			if (s != NULL) {
321				path = xstrdup(s);
322				path[strcspn (path, ",")] = '\0';
323				if (*path == '\0') {
324					free(path);
325					label = xstrdup("default");
326				}
327			} else
328				label = xstrdup("default");
329		}
330
331		/* -L or default set. */
332		if (label != NULL) {
333			if ((path = makesocketpath(label)) == NULL) {
334				fprintf(stderr, "can't create socket: %s\n",
335				    strerror(errno));
336				exit(1);
337			}
338		}
339	}
340	free(label);
341
342	if (strlcpy(socket_path, path, sizeof socket_path) >=
343	    sizeof socket_path) {
344		fprintf(stderr, "socket path too long: %s\n", path);
345		exit(1);
346	}
347	free(path);
348
349	/* Pass control to the client. */
350	exit(client_main(event_init(), argc, argv, flags));
351}
352