mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 09:26:05 +00:00 
			
		
		
		
	Merge branch 'obsd-master'
Conflicts: log.c proc.c tmux.c
This commit is contained in:
		
							
								
								
									
										116
									
								
								tmux.c
									
									
									
									
									
								
							
							
						
						
									
										116
									
								
								tmux.c
									
									
									
									
									
								
							@@ -41,13 +41,11 @@ struct options	*global_s_options;	/* session options */
 | 
			
		||||
struct options	*global_w_options;	/* window options */
 | 
			
		||||
struct environ	*global_environ;
 | 
			
		||||
 | 
			
		||||
char		*shell_cmd;
 | 
			
		||||
int		 debug_level;
 | 
			
		||||
time_t		 start_time;
 | 
			
		||||
char		 socket_path[PATH_MAX];
 | 
			
		||||
struct timeval	 start_time;
 | 
			
		||||
const char	*socket_path;
 | 
			
		||||
 | 
			
		||||
__dead void	 usage(void);
 | 
			
		||||
char 		*makesocketpath(const char *);
 | 
			
		||||
static char	*make_label(const char *);
 | 
			
		||||
 | 
			
		||||
#ifndef HAVE___PROGNAME
 | 
			
		||||
char      *__progname = (char *) "tmux";
 | 
			
		||||
@@ -63,18 +61,6 @@ usage(void)
 | 
			
		||||
	exit(1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
logfile(const char *name)
 | 
			
		||||
{
 | 
			
		||||
	char	*path;
 | 
			
		||||
 | 
			
		||||
	if (debug_level > 0) {
 | 
			
		||||
		xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid());
 | 
			
		||||
		log_open(path);
 | 
			
		||||
		free(path);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *
 | 
			
		||||
getshell(void)
 | 
			
		||||
{
 | 
			
		||||
@@ -121,38 +107,48 @@ areshell(const char *shell)
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char *
 | 
			
		||||
makesocketpath(const char *label)
 | 
			
		||||
static char *
 | 
			
		||||
make_label(const char *label)
 | 
			
		||||
{
 | 
			
		||||
	char		base[PATH_MAX], realbase[PATH_MAX], *path, *s;
 | 
			
		||||
	struct stat	sb;
 | 
			
		||||
	u_int		uid;
 | 
			
		||||
	char		*base, resolved[PATH_MAX], *path, *s;
 | 
			
		||||
	struct stat	 sb;
 | 
			
		||||
	u_int		 uid;
 | 
			
		||||
	int		 saved_errno;
 | 
			
		||||
 | 
			
		||||
	if (label == NULL)
 | 
			
		||||
		label = "default";
 | 
			
		||||
 | 
			
		||||
	uid = getuid();
 | 
			
		||||
 | 
			
		||||
	if ((s = getenv("TMUX_TMPDIR")) != NULL && *s != '\0')
 | 
			
		||||
		xsnprintf(base, sizeof base, "%s/tmux-%u", s, uid);
 | 
			
		||||
		xasprintf(&base, "%s/tmux-%u", s, uid);
 | 
			
		||||
	else
 | 
			
		||||
		xsnprintf(base, sizeof base, "%s/tmux-%u", _PATH_TMP, uid);
 | 
			
		||||
		xasprintf(&base, "%s/tmux-%u", _PATH_TMP, uid);
 | 
			
		||||
 | 
			
		||||
	if (mkdir(base, S_IRWXU) != 0 && errno != EEXIST)
 | 
			
		||||
		return (NULL);
 | 
			
		||||
		goto fail;
 | 
			
		||||
 | 
			
		||||
	if (lstat(base, &sb) != 0)
 | 
			
		||||
		return (NULL);
 | 
			
		||||
		goto fail;
 | 
			
		||||
	if (!S_ISDIR(sb.st_mode)) {
 | 
			
		||||
		errno = ENOTDIR;
 | 
			
		||||
		return (NULL);
 | 
			
		||||
		goto fail;
 | 
			
		||||
	}
 | 
			
		||||
	if (sb.st_uid != uid || (sb.st_mode & S_IRWXO) != 0) {
 | 
			
		||||
		errno = EACCES;
 | 
			
		||||
		return (NULL);
 | 
			
		||||
		goto fail;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (realpath(base, realbase) == NULL)
 | 
			
		||||
		strlcpy(realbase, base, sizeof realbase);
 | 
			
		||||
 | 
			
		||||
	xasprintf(&path, "%s/%s", realbase, label);
 | 
			
		||||
	if (realpath(base, resolved) == NULL)
 | 
			
		||||
		strlcpy(resolved, base, sizeof resolved);
 | 
			
		||||
	xasprintf(&path, "%s/%s", resolved, label);
 | 
			
		||||
	return (path);
 | 
			
		||||
 | 
			
		||||
fail:
 | 
			
		||||
	saved_errno = errno;
 | 
			
		||||
	free(base);
 | 
			
		||||
	errno = saved_errno;
 | 
			
		||||
	return (NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
@@ -193,7 +189,7 @@ find_home(void)
 | 
			
		||||
int
 | 
			
		||||
main(int argc, char **argv)
 | 
			
		||||
{
 | 
			
		||||
	char		*path, *label, **var, tmp[PATH_MAX];
 | 
			
		||||
	char		*path, *label, **var, tmp[PATH_MAX], *shellcmd = NULL;
 | 
			
		||||
	const char	*s;
 | 
			
		||||
	int		 opt, flags, keys;
 | 
			
		||||
 | 
			
		||||
@@ -216,8 +212,8 @@ main(int argc, char **argv)
 | 
			
		||||
			flags |= CLIENT_256COLOURS;
 | 
			
		||||
			break;
 | 
			
		||||
		case 'c':
 | 
			
		||||
			free(shell_cmd);
 | 
			
		||||
			shell_cmd = xstrdup(optarg);
 | 
			
		||||
			free(shellcmd);
 | 
			
		||||
			shellcmd = xstrdup(optarg);
 | 
			
		||||
			break;
 | 
			
		||||
		case 'C':
 | 
			
		||||
			if (flags & CLIENT_CONTROL)
 | 
			
		||||
@@ -248,7 +244,7 @@ main(int argc, char **argv)
 | 
			
		||||
			flags |= CLIENT_UTF8;
 | 
			
		||||
			break;
 | 
			
		||||
		case 'v':
 | 
			
		||||
			debug_level++;
 | 
			
		||||
			log_add_level();
 | 
			
		||||
			break;
 | 
			
		||||
		default:
 | 
			
		||||
			usage();
 | 
			
		||||
@@ -257,7 +253,7 @@ main(int argc, char **argv)
 | 
			
		||||
	argc -= optind;
 | 
			
		||||
	argv += optind;
 | 
			
		||||
 | 
			
		||||
	if (shell_cmd != NULL && argc != 0)
 | 
			
		||||
	if (shellcmd != NULL && argc != 0)
 | 
			
		||||
		usage();
 | 
			
		||||
 | 
			
		||||
#ifdef __OpenBSD__
 | 
			
		||||
@@ -292,7 +288,7 @@ main(int argc, char **argv)
 | 
			
		||||
	for (var = environ; *var != NULL; var++)
 | 
			
		||||
		environ_put(global_environ, *var);
 | 
			
		||||
	if (getcwd(tmp, sizeof tmp) != NULL)
 | 
			
		||||
		environ_set(global_environ, "PWD", tmp);
 | 
			
		||||
		environ_set(global_environ, "PWD", "%s", tmp);
 | 
			
		||||
 | 
			
		||||
	global_options = options_create(NULL);
 | 
			
		||||
	options_table_populate_tree(OPTIONS_TABLE_SERVER, global_options);
 | 
			
		||||
@@ -317,42 +313,24 @@ main(int argc, char **argv)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * Figure out the socket path. If specified on the command-line with -S
 | 
			
		||||
	 * or -L, use it, otherwise try $TMUX or assume -L default.
 | 
			
		||||
	 * If socket is specified on the command-line with -S or -L, it is
 | 
			
		||||
	 * used. Otherwise, $TMUX is checked and if that fails "default" is
 | 
			
		||||
	 * used.
 | 
			
		||||
	 */
 | 
			
		||||
	if (path == NULL) {
 | 
			
		||||
		/* If no -L, use the environment. */
 | 
			
		||||
		if (label == NULL) {
 | 
			
		||||
			s = getenv("TMUX");
 | 
			
		||||
			if (s != NULL) {
 | 
			
		||||
				path = xstrdup(s);
 | 
			
		||||
				path[strcspn (path, ",")] = '\0';
 | 
			
		||||
				if (*path == '\0') {
 | 
			
		||||
					free(path);
 | 
			
		||||
					label = xstrdup("default");
 | 
			
		||||
				}
 | 
			
		||||
			} else
 | 
			
		||||
				label = xstrdup("default");
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		/* -L or default set. */
 | 
			
		||||
		if (label != NULL) {
 | 
			
		||||
			if ((path = makesocketpath(label)) == NULL) {
 | 
			
		||||
				fprintf(stderr, "can't create socket: %s\n",
 | 
			
		||||
				    strerror(errno));
 | 
			
		||||
				exit(1);
 | 
			
		||||
			}
 | 
			
		||||
	if (path == NULL && label == NULL) {
 | 
			
		||||
		s = getenv("TMUX");
 | 
			
		||||
		if (s != NULL && *s != '\0' && *s != ',') {
 | 
			
		||||
			path = xstrdup(s);
 | 
			
		||||
			path[strcspn (path, ",")] = '\0';
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	free(label);
 | 
			
		||||
 | 
			
		||||
	if (strlcpy(socket_path, path, sizeof socket_path) >=
 | 
			
		||||
	    sizeof socket_path) {
 | 
			
		||||
		fprintf(stderr, "socket path too long: %s\n", path);
 | 
			
		||||
	if (path == NULL && (path = make_label(label)) == NULL) {
 | 
			
		||||
		fprintf(stderr, "can't create socket: %s\n", strerror(errno));
 | 
			
		||||
		exit(1);
 | 
			
		||||
	}
 | 
			
		||||
	free(path);
 | 
			
		||||
	socket_path = path;
 | 
			
		||||
	free(label);
 | 
			
		||||
 | 
			
		||||
	/* Pass control to the client. */
 | 
			
		||||
	exit(client_main(osdep_event_init(), argc, argv, flags));
 | 
			
		||||
	exit(client_main(event_init(), argc, argv, flags, shellcmd));
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user