Currently, tmux complains if you create or attach a session within
another tmux session (that is, if $TMUX is set). This is true even if
the new session being created is on a new _server_ being created (e.g.,
"tmux -L new").

This patch addresses this issue by silently succeeding when the new
session is on a new server. The relevant check has been moved from the
server to the client, which complains only after it has successfully
connected to a preexisting server, _and_ determined that server to be
the same one named by $TMUX (that is, the same one in which we're
already running).

The $TMUX == socket path comparison is a string comparison, so it may
now allow you to connect to what is actually still the same server.
Especially when you consider that, at least on a Linux system, default
$TMUX is "/tmp//tmux-XXXX/default", so even just "tmux -S
/tmp/tmux-XXXX/default" would be permitted to nest.

If you don't like that, you could just strip the $TMUX == socket path
comparison from client.c, leaving it as just a "is the server a
preexisting one, or a new one (and is $TMUX set)?" check, by replacing
the body of the outer "if" by the one in the inner "if". It's a choice
between being a little too permissive, and a little too restrictive (but
not as restrictive as it currently is).

Of course, a more precise approach would be to stat() the $TMUX path and
the connected socket, and compare to see if they're the same file.

Word of advice: I recommend that you stop _all_ running tmux servers
before testing this. Otherwise it's just too easy to confuse yourself.
Is the error message coming from the client, or the server? Unless you
are absolutely certain that no pre-patch server is running, things can
get complicated.

Or maybe it was just complicated for me because it's getting late... but
I screwed myself up by all of (1) accidentally using the older client
(2) accidentally using the new client on the older server when nesting
(and thus getting the unconditional no-nest message from the server),
(3) accidentally using an alias which I'd forgotten sets TMUX= to
explicitly allow nesting no matter what. :p

-- 
Micah J. Cowan
http://micah.cowan.name/
Index: client.c
===================================================================
--- client.c.orig
+++ client.c
@@ -50,11 +50,12 @@
 struct imsgbuf *
 client_init(char *path, int cmdflags, int flags)
 {
-	struct sockaddr_un	sa;
-	size_t			size;
-	int			fd, mode;
+	struct sockaddr_un	 sa;
+	size_t			 size;
+	int			 fd, mode;
+	char			*env_path;
 #ifdef HAVE_SETPROCTITLE
-	char		        rpathbuf[MAXPATHLEN];
+	char		         rpathbuf[MAXPATHLEN];
 #endif
 
 #ifdef HAVE_SETPROCTITLE
@@ -88,6 +89,16 @@
 			goto server_started;
 		}
 		goto not_found;
+	} else if ((cmdflags & CMD_CANTNEST)
+		   && ((env_path = getenv("TMUX")) != NULL)) {
+		env_path = xstrdup(env_path);
+		env_path[strcspn(env_path, ",")] = '\0';
+		if (strcmp(path, env_path) == 0) {
+			log_warnx("sessions should be nested with care. "
+				  "unset $TMUX to force");
+			return (NULL);
+		}
+		xfree(env_path);
 	}
 
 server_started:
Index: server-client.c
===================================================================
--- server-client.c.orig
+++ server-client.c
@@ -692,17 +692,6 @@
 	}
 	cmd_free_argv(argc, argv);
 
-	if (data->pid != -1) {
-		TAILQ_FOREACH(cmd, cmdlist, qentry) {
-			if (cmd->entry->flags & CMD_CANTNEST) {
-				server_client_msg_error(&ctx,
-				    "sessions should be nested with care. "
-				    "unset $TMUX to force");
-				goto error;
-			}
-		}
-	}
-
 	if (cmd_list_exec(cmdlist, &ctx) != 1)
 		server_write_client(c, MSG_EXIT, NULL, 0);
 	cmd_list_free(cmdlist);
Index: tmux.c
===================================================================
--- tmux.c.orig
+++ tmux.c
@@ -505,7 +505,7 @@
 	if (shellcmd != NULL)
 		cmdflags |= CMD_STARTSERVER;
 	else if (argc == 0)	/* new-session is the default */
-		cmdflags |= CMD_STARTSERVER|CMD_SENDENVIRON;
+		cmdflags |= CMD_STARTSERVER|CMD_SENDENVIRON|CMD_CANTNEST;
 	else {
 		/*
 		 * It sucks parsing the command string twice (in client and
@@ -522,6 +522,8 @@
 				cmdflags |= CMD_STARTSERVER;
 			if (cmd->entry->flags & CMD_SENDENVIRON)
 				cmdflags |= CMD_SENDENVIRON;
+			if (cmd->entry->flags & CMD_CANTNEST)
+				cmdflags |= CMD_CANTNEST;
 		}
 		cmd_list_free(cmdlist);
 	}
------------------------------------------------------------------------------
The Planet: dedicated and managed hosting, cloud storage, colocation
Stay online with enterprise data centers and the best network in the business
Choose flexible plans and management services without long-term contracts
Personal 24x7 support from experience hosting pros just a phone call away.
http://p.sf.net/sfu/theplanet-com
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to