comparison contrib/chg/chg.c @ 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 8fcc0a829f3d
children d06e43cd393f
comparison
equal deleted inserted replaced
50335:787e7caf887a 50336:cf4d2f31660d
230 hgcmd = (HGPATH); 230 hgcmd = (HGPATH);
231 #else 231 #else
232 hgcmd = "hg"; 232 hgcmd = "hg";
233 #endif 233 #endif
234 } 234 }
235 /* Set $CHGHG to the path to the seleted hg executable if it wasn't
236 * already set. This has the effect of ensuring that a new command
237 * server will be spawned if the existing command server is running from
238 * an executable at a different path. */
239 if (setenv("CHGHG", hgcmd, 1) != 0)
240 abortmsgerrno("failed to setenv");
235 return hgcmd; 241 return hgcmd;
236 } 242 }
237 243
238 static void execcmdserver(const struct cmdserveropts *opts) 244 static void execcmdserver(const char *hgcmd, const struct cmdserveropts *opts)
239 { 245 {
240 const char *hgcmd = gethgcmd();
241 246
242 const char *baseargv[] = { 247 const char *baseargv[] = {
243 hgcmd, "serve", "--no-profile", "--cmdserver", 248 hgcmd, "serve", "--no-profile", "--cmdserver",
244 "chgunix", "--address", opts->initsockname, "--daemon-postexec", 249 "chgunix", "--address", opts->initsockname, "--daemon-postexec",
245 "chdir:/", 250 "chdir:/",
373 if (sockname == opts->redirectsockname) 378 if (sockname == opts->redirectsockname)
374 unlink(opts->sockname); 379 unlink(opts->sockname);
375 380
376 debugmsg("start cmdserver at %s", opts->initsockname); 381 debugmsg("start cmdserver at %s", opts->initsockname);
377 382
383 /* Get the path to the hg executable before we fork because this
384 * function might update the environment, and we want this to be
385 * reflected in both the parent and child processes. */
386 const char *hgcmd = gethgcmd();
387
378 pid_t pid = fork(); 388 pid_t pid = fork();
379 if (pid < 0) 389 if (pid < 0)
380 abortmsg("failed to fork cmdserver process"); 390 abortmsg("failed to fork cmdserver process");
381 if (pid == 0) { 391 if (pid == 0) {
382 execcmdserver(opts); 392 execcmdserver(hgcmd, opts);
383 } else { 393 } else {
384 hgc = retryconnectcmdserver(opts, pid); 394 hgc = retryconnectcmdserver(opts, pid);
385 } 395 }
386 396
387 return hgc; 397 return hgc;
482 debugmsg("execute original hg"); 492 debugmsg("execute original hg");
483 if (execvp(gethgcmd(), (char **)argv) < 0) 493 if (execvp(gethgcmd(), (char **)argv) < 0)
484 abortmsgerrno("failed to exec original hg"); 494 abortmsgerrno("failed to exec original hg");
485 } 495 }
486 496
487 int main(int argc, const char *argv[], const char *envp[]) 497 int main(int argc, const char *argv[])
488 { 498 {
489 if (getenv("CHGDEBUG")) 499 if (getenv("CHGDEBUG"))
490 enabledebugmsg(); 500 enabledebugmsg();
491 501
492 if (!getenv("HGPLAIN") && isatty(fileno(stderr))) 502 if (!getenv("HGPLAIN") && isatty(fileno(stderr)))
517 size_t retry = 0; 527 size_t retry = 0;
518 while (1) { 528 while (1) {
519 hgc = connectcmdserver(&opts); 529 hgc = connectcmdserver(&opts);
520 if (!hgc) 530 if (!hgc)
521 abortmsg("cannot open hg client"); 531 abortmsg("cannot open hg client");
522 hgc_setenv(hgc, envp); 532 /* Use `environ(7)` instead of the optional `envp` argument to
533 * `main` because `envp` does not update when the environment
534 * changes, but `environ` does. */
535 hgc_setenv(hgc, (const char *const *)environ);
523 const char **insts = hgc_validate(hgc, argv + 1, argc - 1); 536 const char **insts = hgc_validate(hgc, argv + 1, argc - 1);
524 int needreconnect = runinstructions(&opts, insts); 537 int needreconnect = runinstructions(&opts, insts);
525 free(insts); 538 free(insts);
526 if (!needreconnect) 539 if (!needreconnect)
527 break; 540 break;