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