Home | History | Annotate | Line # | Download | only in dist
tmux.c revision 1.1.1.3
      1  1.1.1.3  christos /* $Id: tmux.c,v 1.1.1.3 2014/07/24 14:22:53 christos Exp $ */
      2      1.1      jmmv 
      3      1.1      jmmv /*
      4      1.1      jmmv  * Copyright (c) 2007 Nicholas Marriott <nicm (at) users.sourceforge.net>
      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.1.1.3  christos #include <locale.h>
     26      1.1      jmmv #include <pwd.h>
     27      1.1      jmmv #include <stdlib.h>
     28      1.1      jmmv #include <string.h>
     29      1.1      jmmv #include <unistd.h>
     30      1.1      jmmv 
     31      1.1      jmmv #include "tmux.h"
     32      1.1      jmmv 
     33      1.1      jmmv #if defined(DEBUG) && defined(__OpenBSD__)
     34      1.1      jmmv extern char	*malloc_options;
     35      1.1      jmmv #endif
     36      1.1      jmmv 
     37      1.1      jmmv struct options	 global_options;	/* server options */
     38      1.1      jmmv struct options	 global_s_options;	/* session options */
     39      1.1      jmmv struct options	 global_w_options;	/* window options */
     40      1.1      jmmv struct environ	 global_environ;
     41      1.1      jmmv 
     42      1.1      jmmv struct event_base *ev_base;
     43      1.1      jmmv 
     44      1.1      jmmv char		*cfg_file;
     45      1.1      jmmv char		*shell_cmd;
     46      1.1      jmmv int		 debug_level;
     47      1.1      jmmv time_t		 start_time;
     48      1.1      jmmv char		 socket_path[MAXPATHLEN];
     49      1.1      jmmv int		 login_shell;
     50      1.1      jmmv char		*environ_path;
     51      1.1      jmmv 
     52      1.1      jmmv __dead void	 usage(void);
     53      1.1      jmmv char 		*makesocketpath(const char *);
     54      1.1      jmmv 
     55  1.1.1.2      jmmv #ifndef HAVE___PROGNAME
     56      1.1      jmmv char      *__progname = (char *) "tmux";
     57      1.1      jmmv #endif
     58      1.1      jmmv 
     59      1.1      jmmv __dead void
     60      1.1      jmmv usage(void)
     61      1.1      jmmv {
     62      1.1      jmmv 	fprintf(stderr,
     63  1.1.1.3  christos 	    "usage: %s [-2lquvV] [-c shell-command] [-f file] [-L socket-name]\n"
     64      1.1      jmmv 	    "            [-S socket-path] [command [flags]]\n",
     65      1.1      jmmv 	    __progname);
     66      1.1      jmmv 	exit(1);
     67      1.1      jmmv }
     68      1.1      jmmv 
     69      1.1      jmmv void
     70      1.1      jmmv logfile(const char *name)
     71      1.1      jmmv {
     72      1.1      jmmv 	char	*path;
     73      1.1      jmmv 
     74      1.1      jmmv 	if (debug_level > 0) {
     75      1.1      jmmv 		xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid());
     76  1.1.1.3  christos 		log_open(debug_level, path);
     77  1.1.1.3  christos 		free(path);
     78      1.1      jmmv 	}
     79      1.1      jmmv }
     80      1.1      jmmv 
     81      1.1      jmmv const char *
     82      1.1      jmmv getshell(void)
     83      1.1      jmmv {
     84      1.1      jmmv 	struct passwd	*pw;
     85      1.1      jmmv 	const char	*shell;
     86      1.1      jmmv 
     87      1.1      jmmv 	shell = getenv("SHELL");
     88      1.1      jmmv 	if (checkshell(shell))
     89      1.1      jmmv 		return (shell);
     90      1.1      jmmv 
     91      1.1      jmmv 	pw = getpwuid(getuid());
     92      1.1      jmmv 	if (pw != NULL && checkshell(pw->pw_shell))
     93      1.1      jmmv 		return (pw->pw_shell);
     94      1.1      jmmv 
     95      1.1      jmmv 	return (_PATH_BSHELL);
     96      1.1      jmmv }
     97      1.1      jmmv 
     98      1.1      jmmv int
     99      1.1      jmmv checkshell(const char *shell)
    100      1.1      jmmv {
    101  1.1.1.3  christos 	if (shell == NULL || *shell == '\0' || *shell != '/')
    102  1.1.1.3  christos 		return (0);
    103  1.1.1.3  christos 	if (areshell(shell))
    104      1.1      jmmv 		return (0);
    105      1.1      jmmv 	if (access(shell, X_OK) != 0)
    106      1.1      jmmv 		return (0);
    107      1.1      jmmv 	return (1);
    108      1.1      jmmv }
    109      1.1      jmmv 
    110      1.1      jmmv int
    111      1.1      jmmv areshell(const char *shell)
    112      1.1      jmmv {
    113      1.1      jmmv 	const char	*progname, *ptr;
    114      1.1      jmmv 
    115      1.1      jmmv 	if ((ptr = strrchr(shell, '/')) != NULL)
    116      1.1      jmmv 		ptr++;
    117      1.1      jmmv 	else
    118      1.1      jmmv 		ptr = shell;
    119      1.1      jmmv 	progname = __progname;
    120      1.1      jmmv 	if (*progname == '-')
    121      1.1      jmmv 		progname++;
    122      1.1      jmmv 	if (strcmp(ptr, progname) == 0)
    123      1.1      jmmv 		return (1);
    124      1.1      jmmv 	return (0);
    125      1.1      jmmv }
    126      1.1      jmmv 
    127      1.1      jmmv char *
    128      1.1      jmmv makesocketpath(const char *label)
    129      1.1      jmmv {
    130  1.1.1.3  christos 	char		base[MAXPATHLEN], realbase[MAXPATHLEN], *path, *s;
    131      1.1      jmmv 	struct stat	sb;
    132      1.1      jmmv 	u_int		uid;
    133      1.1      jmmv 
    134      1.1      jmmv 	uid = getuid();
    135  1.1.1.3  christos 	if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0')
    136  1.1.1.3  christos 		xsnprintf(base, sizeof base, "%s/", s);
    137  1.1.1.3  christos 	else if ((s = getenv("TMPDIR")) != NULL && *s != '\0')
    138  1.1.1.2      jmmv 		xsnprintf(base, sizeof base, "%s/tmux-%u", s, uid);
    139  1.1.1.3  christos 	else
    140  1.1.1.3  christos 		xsnprintf(base, sizeof base, "%s/tmux-%u", _PATH_TMP, uid);
    141      1.1      jmmv 
    142      1.1      jmmv 	if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
    143      1.1      jmmv 		return (NULL);
    144      1.1      jmmv 
    145      1.1      jmmv 	if (lstat(base, &sb) != 0)
    146      1.1      jmmv 		return (NULL);
    147      1.1      jmmv 	if (!S_ISDIR(sb.st_mode)) {
    148      1.1      jmmv 		errno = ENOTDIR;
    149      1.1      jmmv 		return (NULL);
    150      1.1      jmmv 	}
    151  1.1.1.3  christos 	if (sb.st_uid != uid || (!S_ISDIR(sb.st_mode) &&
    152  1.1.1.3  christos 		sb.st_mode & (S_IRWXG|S_IRWXO)) != 0) {
    153      1.1      jmmv 		errno = EACCES;
    154      1.1      jmmv 		return (NULL);
    155      1.1      jmmv 	}
    156      1.1      jmmv 
    157  1.1.1.3  christos 	if (realpath(base, realbase) == NULL)
    158  1.1.1.3  christos 		strlcpy(realbase, base, sizeof realbase);
    159  1.1.1.3  christos 
    160  1.1.1.3  christos 	xasprintf(&path, "%s/%s", realbase, label);
    161      1.1      jmmv 	return (path);
    162      1.1      jmmv }
    163      1.1      jmmv 
    164  1.1.1.2      jmmv void
    165  1.1.1.2      jmmv setblocking(int fd, int state)
    166  1.1.1.2      jmmv {
    167  1.1.1.2      jmmv 	int mode;
    168  1.1.1.2      jmmv 
    169  1.1.1.2      jmmv 	if ((mode = fcntl(fd, F_GETFL)) != -1) {
    170  1.1.1.2      jmmv 		if (!state)
    171  1.1.1.2      jmmv 			mode |= O_NONBLOCK;
    172  1.1.1.2      jmmv 		else
    173  1.1.1.2      jmmv 			mode &= ~O_NONBLOCK;
    174  1.1.1.2      jmmv 		fcntl(fd, F_SETFL, mode);
    175  1.1.1.2      jmmv 	}
    176  1.1.1.2      jmmv }
    177  1.1.1.2      jmmv 
    178      1.1      jmmv __dead void
    179      1.1      jmmv shell_exec(const char *shell, const char *shellcmd)
    180      1.1      jmmv {
    181      1.1      jmmv 	const char	*shellname, *ptr;
    182      1.1      jmmv 	char		*argv0;
    183      1.1      jmmv 
    184      1.1      jmmv 	ptr = strrchr(shell, '/');
    185      1.1      jmmv 	if (ptr != NULL && *(ptr + 1) != '\0')
    186      1.1      jmmv 		shellname = ptr + 1;
    187      1.1      jmmv 	else
    188      1.1      jmmv 		shellname = shell;
    189      1.1      jmmv 	if (login_shell)
    190      1.1      jmmv 		xasprintf(&argv0, "-%s", shellname);
    191      1.1      jmmv 	else
    192      1.1      jmmv 		xasprintf(&argv0, "%s", shellname);
    193      1.1      jmmv 	setenv("SHELL", shell, 1);
    194      1.1      jmmv 
    195  1.1.1.2      jmmv 	setblocking(STDIN_FILENO, 1);
    196  1.1.1.2      jmmv 	setblocking(STDOUT_FILENO, 1);
    197  1.1.1.2      jmmv 	setblocking(STDERR_FILENO, 1);
    198      1.1      jmmv 	closefrom(STDERR_FILENO + 1);
    199      1.1      jmmv 
    200      1.1      jmmv 	execl(shell, argv0, "-c", shellcmd, (char *) NULL);
    201      1.1      jmmv 	fatal("execl failed");
    202      1.1      jmmv }
    203      1.1      jmmv 
    204      1.1      jmmv int
    205      1.1      jmmv main(int argc, char **argv)
    206      1.1      jmmv {
    207      1.1      jmmv 	struct passwd	*pw;
    208  1.1.1.3  christos 	char		*s, *path, *label, **var, tmp[MAXPATHLEN];
    209  1.1.1.3  christos 	char		 in[256];
    210  1.1.1.3  christos 	const char	*home;
    211  1.1.1.3  christos 	long long	 pid;
    212  1.1.1.3  christos 	int	 	 opt, flags, quiet, keys, session;
    213      1.1      jmmv 
    214      1.1      jmmv #if defined(DEBUG) && defined(__OpenBSD__)
    215      1.1      jmmv 	malloc_options = (char *) "AFGJPX";
    216      1.1      jmmv #endif
    217      1.1      jmmv 
    218  1.1.1.3  christos 	setlocale(LC_TIME, "");
    219  1.1.1.3  christos 
    220      1.1      jmmv 	quiet = flags = 0;
    221      1.1      jmmv 	label = path = NULL;
    222      1.1      jmmv 	login_shell = (**argv == '-');
    223  1.1.1.3  christos 	while ((opt = getopt(argc, argv, "2c:Cdf:lL:qS:uUVv")) != -1) {
    224      1.1      jmmv 		switch (opt) {
    225      1.1      jmmv 		case '2':
    226  1.1.1.3  christos 			flags |= CLIENT_256COLOURS;
    227      1.1      jmmv 			break;
    228      1.1      jmmv 		case 'c':
    229  1.1.1.3  christos 			free(shell_cmd);
    230      1.1      jmmv 			shell_cmd = xstrdup(optarg);
    231      1.1      jmmv 			break;
    232  1.1.1.3  christos 		case 'C':
    233  1.1.1.3  christos 			if (flags & CLIENT_CONTROL)
    234  1.1.1.3  christos 				flags |= CLIENT_CONTROLCONTROL;
    235  1.1.1.3  christos 			else
    236  1.1.1.3  christos 				flags |= CLIENT_CONTROL;
    237  1.1.1.3  christos 			break;
    238      1.1      jmmv 		case 'V':
    239  1.1.1.2      jmmv 			printf("%s %s\n", __progname, VERSION);
    240      1.1      jmmv 			exit(0);
    241      1.1      jmmv 		case 'f':
    242  1.1.1.3  christos 			free(cfg_file);
    243      1.1      jmmv 			cfg_file = xstrdup(optarg);
    244      1.1      jmmv 			break;
    245      1.1      jmmv 		case 'l':
    246      1.1      jmmv 			login_shell = 1;
    247      1.1      jmmv 			break;
    248      1.1      jmmv 		case 'L':
    249  1.1.1.3  christos 			free(label);
    250      1.1      jmmv 			label = xstrdup(optarg);
    251      1.1      jmmv 			break;
    252      1.1      jmmv 		case 'q':
    253      1.1      jmmv 			quiet = 1;
    254      1.1      jmmv 			break;
    255      1.1      jmmv 		case 'S':
    256  1.1.1.3  christos 			free(path);
    257      1.1      jmmv 			path = xstrdup(optarg);
    258      1.1      jmmv 			break;
    259      1.1      jmmv 		case 'u':
    260  1.1.1.3  christos 			flags |= CLIENT_UTF8;
    261      1.1      jmmv 			break;
    262      1.1      jmmv 		case 'v':
    263      1.1      jmmv 			debug_level++;
    264      1.1      jmmv 			break;
    265      1.1      jmmv 		default:
    266      1.1      jmmv 			usage();
    267      1.1      jmmv 		}
    268      1.1      jmmv 	}
    269      1.1      jmmv 	argc -= optind;
    270      1.1      jmmv 	argv += optind;
    271      1.1      jmmv 
    272      1.1      jmmv 	if (shell_cmd != NULL && argc != 0)
    273      1.1      jmmv 		usage();
    274      1.1      jmmv 
    275  1.1.1.3  christos 	if (!(flags & CLIENT_UTF8)) {
    276      1.1      jmmv 		/*
    277      1.1      jmmv 		 * If the user has set whichever of LC_ALL, LC_CTYPE or LANG
    278      1.1      jmmv 		 * exist (in that order) to contain UTF-8, it is a safe
    279      1.1      jmmv 		 * assumption that either they are using a UTF-8 terminal, or
    280      1.1      jmmv 		 * if not they know that output from UTF-8-capable programs may
    281      1.1      jmmv 		 * be wrong.
    282      1.1      jmmv 		 */
    283  1.1.1.3  christos 		if ((s = getenv("LC_ALL")) == NULL || *s == '\0') {
    284  1.1.1.3  christos 			if ((s = getenv("LC_CTYPE")) == NULL || *s == '\0')
    285      1.1      jmmv 				s = getenv("LANG");
    286      1.1      jmmv 		}
    287      1.1      jmmv 		if (s != NULL && (strcasestr(s, "UTF-8") != NULL ||
    288      1.1      jmmv 		    strcasestr(s, "UTF8") != NULL))
    289  1.1.1.3  christos 			flags |= CLIENT_UTF8;
    290      1.1      jmmv 	}
    291      1.1      jmmv 
    292      1.1      jmmv 	environ_init(&global_environ);
    293      1.1      jmmv 	for (var = environ; *var != NULL; var++)
    294      1.1      jmmv 		environ_put(&global_environ, *var);
    295  1.1.1.3  christos 	if (getcwd(tmp, sizeof tmp) != NULL)
    296  1.1.1.3  christos 		environ_set(&global_environ, "PWD", tmp);
    297      1.1      jmmv 
    298      1.1      jmmv 	options_init(&global_options, NULL);
    299  1.1.1.2      jmmv 	options_table_populate_tree(server_options_table, &global_options);
    300  1.1.1.2      jmmv 	options_set_number(&global_options, "quiet", quiet);
    301      1.1      jmmv 
    302      1.1      jmmv 	options_init(&global_s_options, NULL);
    303  1.1.1.2      jmmv 	options_table_populate_tree(session_options_table, &global_s_options);
    304  1.1.1.2      jmmv 	options_set_string(&global_s_options, "default-shell", "%s", getshell());
    305      1.1      jmmv 
    306  1.1.1.2      jmmv 	options_init(&global_w_options, NULL);
    307  1.1.1.2      jmmv 	options_table_populate_tree(window_options_table, &global_w_options);
    308  1.1.1.2      jmmv 
    309  1.1.1.2      jmmv 	/* Enable UTF-8 if the first client is on UTF-8 terminal. */
    310  1.1.1.3  christos 	if (flags & CLIENT_UTF8) {
    311  1.1.1.2      jmmv 		options_set_number(&global_s_options, "status-utf8", 1);
    312  1.1.1.2      jmmv 		options_set_number(&global_s_options, "mouse-utf8", 1);
    313  1.1.1.2      jmmv 		options_set_number(&global_w_options, "utf8", 1);
    314      1.1      jmmv 	}
    315      1.1      jmmv 
    316  1.1.1.2      jmmv 	/* Override keys to vi if VISUAL or EDITOR are set. */
    317      1.1      jmmv 	if ((s = getenv("VISUAL")) != NULL || (s = getenv("EDITOR")) != NULL) {
    318      1.1      jmmv 		if (strrchr(s, '/') != NULL)
    319      1.1      jmmv 			s = strrchr(s, '/') + 1;
    320      1.1      jmmv 		if (strstr(s, "vi") != NULL)
    321      1.1      jmmv 			keys = MODEKEY_VI;
    322  1.1.1.2      jmmv 		else
    323  1.1.1.2      jmmv 			keys = MODEKEY_EMACS;
    324  1.1.1.2      jmmv 		options_set_number(&global_s_options, "status-keys", keys);
    325  1.1.1.2      jmmv 		options_set_number(&global_w_options, "mode-keys", keys);
    326      1.1      jmmv 	}
    327      1.1      jmmv 
    328      1.1      jmmv 	/* Locate the configuration file. */
    329      1.1      jmmv 	if (cfg_file == NULL) {
    330      1.1      jmmv 		home = getenv("HOME");
    331      1.1      jmmv 		if (home == NULL || *home == '\0') {
    332      1.1      jmmv 			pw = getpwuid(getuid());
    333      1.1      jmmv 			if (pw != NULL)
    334      1.1      jmmv 				home = pw->pw_dir;
    335  1.1.1.3  christos 			else
    336  1.1.1.3  christos 				home = NULL;
    337      1.1      jmmv 		}
    338  1.1.1.3  christos 		if (home != NULL) {
    339  1.1.1.3  christos 			xasprintf(&cfg_file, "%s/.tmux.conf", home);
    340  1.1.1.3  christos 			if (access(cfg_file, R_OK) != 0 && errno == ENOENT) {
    341  1.1.1.3  christos 				free(cfg_file);
    342  1.1.1.3  christos 				cfg_file = NULL;
    343  1.1.1.3  christos 			}
    344      1.1      jmmv 		}
    345      1.1      jmmv 	}
    346      1.1      jmmv 
    347  1.1.1.3  christos 	/* Get path from environment. */
    348  1.1.1.3  christos 	s = getenv("TMUX");
    349  1.1.1.3  christos 	if (s != NULL && sscanf(s, "%255[^,],%lld,%d", in, &pid, &session) == 3)
    350  1.1.1.3  christos 		environ_path = xstrdup(in);
    351  1.1.1.3  christos 
    352      1.1      jmmv 	/*
    353      1.1      jmmv 	 * Figure out the socket path. If specified on the command-line with -S
    354      1.1      jmmv 	 * or -L, use it, otherwise try $TMUX or assume -L default.
    355      1.1      jmmv 	 */
    356      1.1      jmmv 	if (path == NULL) {
    357      1.1      jmmv 		/* If no -L, use the environment. */
    358      1.1      jmmv 		if (label == NULL) {
    359      1.1      jmmv 			if (environ_path != NULL)
    360      1.1      jmmv 				path = xstrdup(environ_path);
    361      1.1      jmmv 			else
    362      1.1      jmmv 				label = xstrdup("default");
    363      1.1      jmmv 		}
    364      1.1      jmmv 
    365      1.1      jmmv 		/* -L or default set. */
    366      1.1      jmmv 		if (label != NULL) {
    367      1.1      jmmv 			if ((path = makesocketpath(label)) == NULL) {
    368  1.1.1.3  christos 				fprintf(stderr, "can't create socket: %s\n",
    369  1.1.1.3  christos 					strerror(errno));
    370      1.1      jmmv 				exit(1);
    371      1.1      jmmv 			}
    372      1.1      jmmv 		}
    373      1.1      jmmv 	}
    374  1.1.1.3  christos 	free(label);
    375  1.1.1.3  christos 
    376  1.1.1.3  christos 	if (strlcpy(socket_path, path, sizeof socket_path) >= sizeof socket_path) {
    377  1.1.1.3  christos 		fprintf(stderr, "socket path too long: %s\n", path);
    378  1.1.1.3  christos 		exit(1);
    379  1.1.1.3  christos 	}
    380  1.1.1.3  christos 	free(path);
    381      1.1      jmmv 
    382      1.1      jmmv #ifdef HAVE_SETPROCTITLE
    383      1.1      jmmv 	/* Set process title. */
    384      1.1      jmmv 	setproctitle("%s (%s)", __progname, socket_path);
    385      1.1      jmmv #endif
    386      1.1      jmmv 
    387      1.1      jmmv 	/* Pass control to the client. */
    388  1.1.1.2      jmmv 	ev_base = osdep_event_init();
    389      1.1      jmmv 	exit(client_main(argc, argv, flags));
    390      1.1      jmmv }
    391