changeset 50336:cf4d2f31660d stable

chg: populate CHGHG if not set Normally, chg determines which `hg` executable to use by first consulting the `$CHGHG` and `$HG` environment variables, and if neither are present defaults to the `hg` found in the user's `$PATH`. If built with the `HGPATHREL` compiler flag, chg will instead assume that there exists an `hg` executable in the same directory as the `chg` binary and attempt to use that. This can cause problems in situations where there are multiple actively-used Mercurial installations on the same system. When a `chg` client connects to a running command server, the server process performs some basic validation to determine whether a new command server needs to be spawned. These checks include things like checking certain "sensitive" environment variables and config sections, as well as checking whether the mtime of the extensions, hg's `__version__.py` module, and the Python interpreter have changed. Crucially, the command server doesn't explicitly check whether the executable it is running from matches the executable that the `chg` client would have otherwise invoked had there been no existing command server process. Without `HGPATHREL`, this still gets implicitly checked during the validation step, because the only way to specify an alternate hg executable (apart from `$PATH`) is via the `$CHGHG` and `$HG` environment variables, both of which are checked. With `HGPATHREL`, however, the command server has no way of knowing which hg executable the client would have run. This means that a client located at `/version_B/bin/chg` will happily connect to a command server running `/version_A/bin/hg` instead of `/version_B/bin/hg` as expected. A simple solution is to have the client set `$CHGHG` itself, which then allows the command server's environment validation to work as intended. I have tested this manually using two locally built hg installations and it seems to work with no ill effects. That said, I'm not sure how to write an automated test for this since the `chg` available to the tests isn't even built with the `HGPATHREL` compiler flag to begin with.
author Arun Kulshreshtha <akulshreshtha@janestreet.com>
date Mon, 27 Mar 2023 17:30:14 -0400
parents 787e7caf887a
children 47686726545d
files contrib/chg/chg.c
diffstat 1 files changed, 18 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/contrib/chg/chg.c	Fri Apr 07 12:11:44 2023 +0200
+++ b/contrib/chg/chg.c	Mon Mar 27 17:30:14 2023 -0400
@@ -232,12 +232,17 @@
 			hgcmd = "hg";
 #endif
 	}
+	/* Set $CHGHG to the path to the seleted hg executable if it wasn't
+	 * already set. This has the effect of ensuring that a new command
+	 * server will be spawned if the existing command server is running from
+	 * an executable at a different path. */
+	if (setenv("CHGHG", hgcmd, 1) != 0)
+		abortmsgerrno("failed to setenv");
 	return hgcmd;
 }
 
-static void execcmdserver(const struct cmdserveropts *opts)
+static void execcmdserver(const char *hgcmd, const struct cmdserveropts *opts)
 {
-	const char *hgcmd = gethgcmd();
 
 	const char *baseargv[] = {
 	    hgcmd,     "serve",     "--no-profile",     "--cmdserver",
@@ -375,11 +380,16 @@
 
 	debugmsg("start cmdserver at %s", opts->initsockname);
 
+	/* Get the path to the hg executable before we fork because this
+	 * function might update the environment, and we want this to be
+	 * reflected in both the parent and child processes. */
+	const char *hgcmd = gethgcmd();
+
 	pid_t pid = fork();
 	if (pid < 0)
 		abortmsg("failed to fork cmdserver process");
 	if (pid == 0) {
-		execcmdserver(opts);
+		execcmdserver(hgcmd, opts);
 	} else {
 		hgc = retryconnectcmdserver(opts, pid);
 	}
@@ -484,7 +494,7 @@
 		abortmsgerrno("failed to exec original hg");
 }
 
-int main(int argc, const char *argv[], const char *envp[])
+int main(int argc, const char *argv[])
 {
 	if (getenv("CHGDEBUG"))
 		enabledebugmsg();
@@ -519,7 +529,10 @@
 		hgc = connectcmdserver(&opts);
 		if (!hgc)
 			abortmsg("cannot open hg client");
-		hgc_setenv(hgc, envp);
+		/* Use `environ(7)` instead of the optional `envp` argument to
+		 * `main` because `envp` does not update when the environment
+		 * changes, but `environ` does. */
+		hgc_setenv(hgc, (const char *const *)environ);
 		const char **insts = hgc_validate(hgc, argv + 1, argc - 1);
 		int needreconnect = runinstructions(&opts, insts);
 		free(insts);