Home | History | Annotate | Line # | Download | only in dist
tmux.c revision 1.1.1.11
      1 /* $OpenBSD$ */
      2 
      3 /*
      4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott (at) 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 <errno.h>
     23 #include <event.h>
     24 #include <fcntl.h>
     25 #include <langinfo.h>
     26 #include <locale.h>
     27 #include <pwd.h>
     28 #include <stdlib.h>
     29 #include <string.h>
     30 #include <time.h>
     31 #include <unistd.h>
     32 
     33 #include "tmux.h"
     34 
     35 struct options	*global_options;	/* server options */
     36 struct options	*global_s_options;	/* session options */
     37 struct options	*global_w_options;	/* window options */
     38 struct environ	*global_environ;
     39 
     40 struct timeval	 start_time;
     41 const char	*socket_path;
     42 int		 ptm_fd = -1;
     43 const char	*shell_command;
     44 
     45 static __dead void	 usage(void);
     46 static char		*make_label(const char *, char **);
     47 
     48 static const char	*getshell(void);
     49 static int		 checkshell(const char *);
     50 
     51 static __dead void
     52 usage(void)
     53 {
     54 	fprintf(stderr,
     55 	    "usage: %s [-2CluvV] [-c shell-command] [-f file] [-L socket-name]\n"
     56 	    "            [-S socket-path] [command [flags]]\n",
     57 	    getprogname());
     58 	exit(1);
     59 }
     60 
     61 static const char *
     62 getshell(void)
     63 {
     64 	struct passwd	*pw;
     65 	const char	*shell;
     66 
     67 	shell = getenv("SHELL");
     68 	if (checkshell(shell))
     69 		return (shell);
     70 
     71 	pw = getpwuid(getuid());
     72 	if (pw != NULL && checkshell(pw->pw_shell))
     73 		return (pw->pw_shell);
     74 
     75 	return (_PATH_BSHELL);
     76 }
     77 
     78 static int
     79 checkshell(const char *shell)
     80 {
     81 	if (shell == NULL || *shell != '/')
     82 		return (0);
     83 	if (areshell(shell))
     84 		return (0);
     85 	if (access(shell, X_OK) != 0)
     86 		return (0);
     87 	return (1);
     88 }
     89 
     90 int
     91 areshell(const char *shell)
     92 {
     93 	const char	*progname, *ptr;
     94 
     95 	if ((ptr = strrchr(shell, '/')) != NULL)
     96 		ptr++;
     97 	else
     98 		ptr = shell;
     99 	progname = getprogname();
    100 	if (*progname == '-')
    101 		progname++;
    102 	if (strcmp(ptr, progname) == 0)
    103 		return (1);
    104 	return (0);
    105 }
    106 
    107 static char *
    108 make_label(const char *label, char **cause)
    109 {
    110 	char		*base, resolved[PATH_MAX], *path, *s;
    111 	struct stat	 sb;
    112 	uid_t		 uid;
    113 
    114 	*cause = NULL;
    115 
    116 	if (label == NULL)
    117 		label = "default";
    118 	uid = getuid();
    119 
    120 	if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0')
    121 		xasprintf(&base, "%s/tmux-%ld", s, (long)uid);
    122 	else
    123 		xasprintf(&base, "%s/tmux-%ld", _PATH_TMP, (long)uid);
    124 	if (realpath(base, resolved) == NULL &&
    125 	    strlcpy(resolved, base, sizeof resolved) >= sizeof resolved) {
    126 		errno = ERANGE;
    127 		free(base);
    128 		goto fail;
    129 	}
    130 
    131 	if (mkdir(resolved, S_IRWXU) != 0 && errno != EEXIST)
    132 		goto fail;
    133 	if (lstat(resolved, &sb) != 0)
    134 		goto fail;
    135 	if (!S_ISDIR(sb.st_mode)) {
    136 		errno = ENOTDIR;
    137 		goto fail;
    138 	}
    139 	if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) {
    140 		errno = EACCES;
    141 		goto fail;
    142 	}
    143 	xasprintf(&path, "%s/%s", resolved, label);
    144 	return (path);
    145 
    146 fail:
    147 	xasprintf(cause, "error creating %s (%s)", resolved, strerror(errno));
    148 	return (NULL);
    149 }
    150 
    151 void
    152 setblocking(int fd, int state)
    153 {
    154 	int mode;
    155 
    156 	if ((mode = fcntl(fd, F_GETFL)) != -1) {
    157 		if (!state)
    158 			mode |= O_NONBLOCK;
    159 		else
    160 			mode &= ~O_NONBLOCK;
    161 		fcntl(fd, F_SETFL, mode);
    162 	}
    163 }
    164 
    165 const char *
    166 find_cwd(void)
    167 {
    168 	char		 resolved1[PATH_MAX], resolved2[PATH_MAX];
    169 	static char	 cwd[PATH_MAX];
    170 	const char	*pwd;
    171 
    172 	if (getcwd(cwd, sizeof cwd) == NULL)
    173 		return (NULL);
    174 	if ((pwd = getenv("PWD")) == NULL || *pwd == '\0')
    175 		return (cwd);
    176 
    177 	/*
    178 	 * We want to use PWD so that symbolic links are maintained,
    179 	 * but only if it matches the actual working directory.
    180 	 */
    181 	if (realpath(pwd, resolved1) == NULL)
    182 		return (cwd);
    183 	if (realpath(cwd, resolved2) == NULL)
    184 		return (cwd);
    185 	if (strcmp(resolved1, resolved2) != 0)
    186 		return (cwd);
    187 	return (pwd);
    188 }
    189 
    190 const char *
    191 find_home(void)
    192 {
    193 	struct passwd		*pw;
    194 	static const char	*home;
    195 
    196 	if (home != NULL)
    197 		return (home);
    198 
    199 	home = getenv("HOME");
    200 	if (home == NULL || *home == '\0') {
    201 		pw = getpwuid(getuid());
    202 		if (pw != NULL)
    203 			home = pw->pw_dir;
    204 		else
    205 			home = NULL;
    206 	}
    207 
    208 	return (home);
    209 }
    210 
    211 int
    212 main(int argc, char **argv)
    213 {
    214 	char					*path, *label, *cause, **var;
    215 	const char				*s, *shell, *cwd;
    216 	int					 opt, flags, keys;
    217 	const struct options_table_entry	*oe;
    218 
    219 	if (setlocale(LC_CTYPE, "en_US.UTF-8") == NULL &&
    220 	    setlocale(LC_CTYPE, "C.UTF-8") == NULL) {
    221 		if (setlocale(LC_CTYPE, "") == NULL)
    222 			errx(1, "invalid LC_ALL, LC_CTYPE or LANG");
    223 		s = nl_langinfo(CODESET);
    224 		if (strcasecmp(s, "UTF-8") != 0 && strcasecmp(s, "UTF8") != 0)
    225 			errx(1, "need UTF-8 locale (LC_CTYPE) but have %s", s);
    226 	}
    227 
    228 	setlocale(LC_TIME, "");
    229 	tzset();
    230 
    231 	if (**argv == '-')
    232 		flags = CLIENT_LOGIN;
    233 	else
    234 		flags = 0;
    235 
    236 	label = path = NULL;
    237 	while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:uUVv")) != -1) {
    238 		switch (opt) {
    239 		case '2':
    240 			flags |= CLIENT_256COLOURS;
    241 			break;
    242 		case 'c':
    243 			shell_command = optarg;
    244 			break;
    245 		case 'C':
    246 			if (flags & CLIENT_CONTROL)
    247 				flags |= CLIENT_CONTROLCONTROL;
    248 			else
    249 				flags |= CLIENT_CONTROL;
    250 			break;
    251 		case 'V':
    252 			printf("%s %s\n", getprogname(), VERSION);
    253 			exit(0);
    254 		case 'f':
    255 			set_cfg_file(optarg);
    256 			break;
    257 		case 'l':
    258 			flags |= CLIENT_LOGIN;
    259 			break;
    260 		case 'L':
    261 			free(label);
    262 			label = xstrdup(optarg);
    263 			break;
    264 		case 'q':
    265 			break;
    266 		case 'S':
    267 			free(path);
    268 			path = xstrdup(optarg);
    269 			break;
    270 		case 'u':
    271 			flags |= CLIENT_UTF8;
    272 			break;
    273 		case 'v':
    274 			log_add_level();
    275 			break;
    276 		default:
    277 			usage();
    278 		}
    279 	}
    280 	argc -= optind;
    281 	argv += optind;
    282 
    283 	if (shell_command != NULL && argc != 0)
    284 		usage();
    285 
    286 	if ((ptm_fd = getptmfd()) == -1)
    287 		err(1, "getptmfd");
    288 	if (pledge("stdio rpath wpath cpath flock fattr unix getpw sendfd "
    289 	    "recvfd proc exec tty ps", NULL) != 0)
    290 		err(1, "pledge");
    291 
    292 	/*
    293 	 * tmux is a UTF-8 terminal, so if TMUX is set, assume UTF-8.
    294 	 * Otherwise, if the user has set LC_ALL, LC_CTYPE or LANG to contain
    295 	 * UTF-8, it is a safe assumption that either they are using a UTF-8
    296 	 * terminal, or if not they know that output from UTF-8-capable
    297 	 * programs may be wrong.
    298 	 */
    299 	if (getenv("TMUX") != NULL)
    300 		flags |= CLIENT_UTF8;
    301 	else {
    302 		s = getenv("LC_ALL");
    303 		if (s == NULL || *s == '\0')
    304 			s = getenv("LC_CTYPE");
    305 		if (s == NULL || *s == '\0')
    306 			s = getenv("LANG");
    307 		if (s == NULL || *s == '\0')
    308 			s = "";
    309 		if (strcasestr(s, "UTF-8") != NULL ||
    310 		    strcasestr(s, "UTF8") != NULL)
    311 			flags |= CLIENT_UTF8;
    312 	}
    313 
    314 	global_environ = environ_create();
    315 	for (var = environ; *var != NULL; var++)
    316 		environ_put(global_environ, *var);
    317 	if ((cwd = find_cwd()) != NULL)
    318 		environ_set(global_environ, "PWD", "%s", cwd);
    319 
    320 	global_options = options_create(NULL);
    321 	global_s_options = options_create(NULL);
    322 	global_w_options = options_create(NULL);
    323 	for (oe = options_table; oe->name != NULL; oe++) {
    324 		if (oe->scope & OPTIONS_TABLE_SERVER)
    325 			options_default(global_options, oe);
    326 		if (oe->scope & OPTIONS_TABLE_SESSION)
    327 			options_default(global_s_options, oe);
    328 		if (oe->scope & OPTIONS_TABLE_WINDOW)
    329 			options_default(global_w_options, oe);
    330 	}
    331 
    332 	/*
    333 	 * The default shell comes from SHELL or from the user's passwd entry
    334 	 * if available.
    335 	 */
    336 	shell = getshell();
    337 	options_set_string(global_s_options, "default-shell", 0, "%s", shell);
    338 
    339 	/* Override keys to vi if VISUAL or EDITOR are set. */
    340 	if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) {
    341 		if (strrchr(s, '/') != NULL)
    342 			s = strrchr(s, '/') + 1;
    343 		if (strstr(s, "vi") != NULL)
    344 			keys = MODEKEY_VI;
    345 		else
    346 			keys = MODEKEY_EMACS;
    347 		options_set_number(global_s_options, "status-keys", keys);
    348 		options_set_number(global_w_options, "mode-keys", keys);
    349 	}
    350 
    351 	/*
    352 	 * If socket is specified on the command-line with -S or -L, it is
    353 	 * used. Otherwise, $TMUX is checked and if that fails "default" is
    354 	 * used.
    355 	 */
    356 	if (path == NULL && label == NULL) {
    357 		s = getenv("TMUX");
    358 		if (s != NULL && *s != '\0' && *s != ',') {
    359 			path = xstrdup(s);
    360 			path[strcspn(path, ",")] = '\0';
    361 		}
    362 	}
    363 	if (path == NULL && (path = make_label(label, &cause)) == NULL) {
    364 		if (cause != NULL) {
    365 			fprintf(stderr, "%s\n", cause);
    366 			free(cause);
    367 		}
    368 		exit(1);
    369 	}
    370 	socket_path = path;
    371 	free(label);
    372 
    373 	/* Pass control to the client. */
    374 	exit(client_main(osdep_event_init(), argc, argv, flags));
    375 }
    376