Subject: Re: bug#33847: 27.0.50; emacsclient does not find server socket

From: Ulrich Mueller <[email-masked]>
Date: Sun, 20 Jan 2019 18:59:39 +0100

>>>>> On Sun, 30 Dec 2018, Paul Eggert wrote:

> Because we're not absolutists. On older systems that do not have
> adequate provisions for security, Emacs does the best it can: that's
> better than not doing anything, and people who run older, less-secure
> systems are likely to not care all that much about security anyway so
> this is OK. On newer systems that are more secure, though, Emacs can
> be more secure.

Since there doesn't seem to be any progress here, please find below
the patch that I am using since some time.

>From f8d87a0a89b91c120935bd5be802604b2c749767 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ulrich=20M=C3=BCller?= <[email-masked]>
Date: Sun, 20 Jan 2019 18:48:45 +0100
Subject: [PATCH] Add a fallback for the socket location in emacsclient
 (bug#33847)

* lib-src/emacsclient.c (set_local_socket): Fall back to TMPDIR
if the socket is not found under XDG_RUNTIME_DIR.
---
 lib-src/emacsclient.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c
index f476840898..27b945133e 100644
--- a/lib-src/emacsclient.c
+++ b/lib-src/emacsclient.c
@@ -1356,6 +1356,7 @@ set_local_socket (char const *server_name)
   enum { socknamesize = sizeof server.un.sun_path };
   int tmpdirlen = -1;
   int socknamelen = -1;
+  int sock_status = -1;
   uid_t uid = geteuid ();
 
   if (strchr (server_name, '/')
@@ -1366,9 +1367,15 @@ 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 if sockname is valid and if the socket exists. */
+	  if (0 <= socknamelen && socknamelen < socknamesize)
+	    sock_status = socket_status (sockname, uid);
+	}
+
+      if (sock_status)
 	{
 	  char const *tmpdir = egetenv ("TMPDIR");
 	  if (tmpdir)
@@ -1399,7 +1406,9 @@ set_local_socket (char const *server_name)
     }
 
   /* See if the socket exists, and if it's owned by us. */
-  int sock_status = socket_status (sockname, uid);
+  if (sock_status)
+    sock_status = socket_status (sockname, uid);
+
   if (sock_status)
     {
       /* Failing that, see if LOGNAME or USER exist and differ from
-- 
2.19.1