all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Adam Edge <baronedge@airmail.cc>
To: 41707@debbugs.gnu.org
Cc: Adam Edge <baronedge@airmail.cc>
Subject: bug#41707: [PATCH] Try $TMPDIR if $XDG_RUNTIME_DIR has no socket
Date: Thu,  4 Jun 2020 16:05:00 +0300	[thread overview]
Message-ID: <20200604130500.438983-1-baronedge@airmail.cc> (raw)

Emacsclient currently checks whether $XDG_RUNTIME_DIR exists in
the environment, and if it does, it uses that as a base for the
socket directory.  However, Emacs seems to still use $TMPDIR
when the daemon is started (both via emacs --daemon and
M-x start-server).  This commit makes Emacsclient first check
whether the socket exists in $XDG_RUNTIME_DIR, and if it doesn't,
fall back to $TMPDIR.
---
 lib-src/emacsclient.c | 39 ++++++++++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index 380be95222..926d6cdd45 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -1365,6 +1365,19 @@ local_sockname (char *sockname, int socknamesize, int tmpdirlen,
   return -1;
 }
 
+/* Check the result the sockname snprintf, and fail () if
+   it's invalid. */
+
+static void
+check_sockname_length (const char *name, int len, int size)
+{
+  if (! (0 <= len && len < size))
+    {
+      message (true, "%s: socket-name %s... too long\n", progname, name);
+      fail ();
+    }
+}
+
 /* Create a local socket for SERVER_NAME and connect it to Emacs.  If
    SERVER_NAME is a file name component, the local socket name
    relative to a well-known location in a temporary directory.
@@ -1383,6 +1396,7 @@ set_local_socket (char const *server_name)
   int socknamelen = -1;
   uid_t uid = geteuid ();
   bool tmpdir_used = false;
+  int sock_status = 0;
 
   if (strchr (server_name, '/')
       || (ISSLASH ('\\') && strchr (server_name, '\\')))
@@ -1392,9 +1406,17 @@ set_local_socket (char const *server_name)
       /* socket_name is a file name component.  */
       char const *xdg_runtime_dir = egetenv ("XDG_RUNTIME_DIR");
       if (xdg_runtime_dir)
-	socknamelen = snprintf (sockname, socknamesize, "%s/emacs/%s",
-				xdg_runtime_dir, server_name);
-      else
+        {
+	  socknamelen = snprintf (sockname, socknamesize, "%s/emacs/%s",
+				  xdg_runtime_dir, server_name);
+	  check_sockname_length (sockname, socknamelen, socknamesize);
+	  /* See if the socket exists, and if it's owned by us. */
+	  sock_status = socket_status (sockname, uid);
+	}
+
+      /* If there wasn't a socket in XDG_RUNTIME_DIR, Emacs probably
+	 created a socket in TMPDIR instead. */
+      if (sock_status == ENOENT)
 	{
 	  char const *tmpdir = egetenv ("TMPDIR");
 	  if (tmpdir)
@@ -1415,18 +1437,13 @@ set_local_socket (char const *server_name)
 	    }
 	  socknamelen = local_sockname (sockname, socknamesize, tmpdirlen,
 					uid, server_name);
+	  check_sockname_length (sockname, socknamelen, socknamesize);
+	  /* See if the socket exists, and if it's owned by us. */
+	  sock_status = socket_status (sockname, uid);
 	  tmpdir_used = true;
 	}
     }
 
-  if (! (0 <= socknamelen && socknamelen < socknamesize))
-    {
-      message (true, "%s: socket-name %s... too long\n", progname, sockname);
-      fail ();
-    }
-
-  /* See if the socket exists, and if it's owned by us. */
-  int sock_status = socket_status (sockname, uid);
   if (sock_status)
     {
       /* Failing that, see if LOGNAME or USER exist and differ from
-- 
2.26.2






             reply	other threads:[~2020-06-04 13:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-04 13:05 Adam Edge [this message]
2020-08-05 16:30 ` bug#41707: [PATCH] Try $TMPDIR if $XDG_RUNTIME_DIR has no socket Lars Ingebrigtsen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200604130500.438983-1-baronedge@airmail.cc \
    --to=baronedge@airmail.cc \
    --cc=41707@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.