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