Patchwork D9058: chg: fallback to original hg if stdio fds are missing

login
register
mail settings
Submitter phabricator
Date Sept. 19, 2020, 12:07 a.m.
Message ID <differential-rev-PHID-DREV-3q5v5hzqhvcudk6inklq-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47233/
State Superseded
Headers show

Comments

phabricator - Sept. 19, 2020, 12:07 a.m.
quark created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  If stdio fds are missing (ex. fd 0 is not present), chg might open
  fds that take the numbers 0, and attachio would send the wrong fds
  to the client, which might cause unwanted behaviors. Avoid that by
  detecting the missing fds and falling back to the original hg.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9058

AFFECTED FILES
  contrib/chg/chg.c
  tests/test-chg.t

CHANGE DETAILS




To: quark, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/tests/test-chg.t b/tests/test-chg.t
--- a/tests/test-chg.t
+++ b/tests/test-chg.t
@@ -197,6 +197,14 @@ 
 
   $ cd ..
 
+missing stdio
+-------------
+
+  $ CHGDEBUG=1 chg version -q 0<&-
+  chg: debug: * stdio fds are missing (glob)
+  chg: debug: * execute original hg (glob)
+  Mercurial Distributed SCM * (glob)
+
 server lifecycle
 ----------------
 
diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
--- a/contrib/chg/chg.c
+++ b/contrib/chg/chg.c
@@ -373,8 +373,15 @@ 
 }
 
 /*
- * Test whether the command is unsupported or not. This is not designed to
- * cover all cases. But it's fast, does not depend on the server.
+ * Test whether the command and the environment is unsupported or not.
+ *
+ * If any of the stdio file descriptors are not present (rare, but some tools
+ * might spawn new processes without stdio instead of redirecting them to the
+ * null device), then mark it as not supported because attachio won't work
+ * correctly.
+ *
+ * The command list is not designed to cover all cases. But it's fast, and does
+ * not depend on the server.
  */
 static int isunsupported(int argc, const char *argv[])
 {
@@ -384,6 +391,13 @@ 
 	};
 	unsigned int state = 0;
 	int i;
+	/* use fcntl to test missing stdio fds */
+	if (fcntl(STDIN_FILENO, F_GETFD) == -1 ||
+	    fcntl(STDOUT_FILENO, F_GETFD) == -1 ||
+	    fcntl(STDERR_FILENO, F_GETFD) == -1) {
+		debugmsg("stdio fds are missing");
+		return 1;
+	}
 	for (i = 0; i < argc; ++i) {
 		if (strcmp(argv[i], "--") == 0)
 			break;