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