[PATCH 2 of 2] chg: respect XDG_RUNTIME_DIR

Jun Wu quark at fb.com
Sun Dec 25 19:04:56 EST 2016


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1482710562 0
#      Mon Dec 26 00:02:42 2016 +0000
# Node ID 09c45fafe96004cdf761ea6740af03c2671be1a9
# Parent  92eb460b9c9af3e5a6251ed6b3924824030de906
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 09c45fafe960
chg: respect XDG_RUNTIME_DIR

$XDG_RUNTIME_DIR [1] is a better place for user daemons. Let's use it and
fallback to $TMPDIR.

After this patch, chg will try to socket paths in the following order:

  1. $CHGSOCKNAME
  2. $XDG_RUNTIME_DIR/chg/server
  3. ${TMPDIR:-tmp}/chg$UID/server

[1]: https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html

diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
--- a/contrib/chg/chg.c
+++ b/contrib/chg/chg.c
@@ -130,9 +130,16 @@ static void preparesockdir(const char *s
 static void getdefaultsockdir(char sockdir[], size_t size) {
 	/* by default, put socket file in secure directory
+	 * (${XDG_RUNTIME_DIR}/chg, or /${TMPDIR:-tmp}/chg$UID)
 	 * (permission of socket file may be ignored on some Unices) */
-	const char *tmpdir = getenv("TMPDIR");
-	if (!tmpdir)
-		tmpdir = "/tmp";
-	int r = snprintf(sockdir, size, "%s/chg%d", tmpdir, geteuid());
+	const char *runtimedir = getenv("XDG_RUNTIME_DIR");
+	int r;
+	if (runtimedir) {
+		r = snprintf(sockdir, size, "%s/chg", runtimedir);
+	} else {
+		const char *tmpdir = getenv("TMPDIR");
+		if (!tmpdir)
+			tmpdir = "/tmp";
+		r = snprintf(sockdir, size, "%s/chg%d", tmpdir, geteuid());
+	}
 	if (r < 0 || (size_t)r >= size)
 		abortmsg("too long TMPDIR (r = %d)", r);


More information about the Mercurial-devel mailing list