all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#41707: [PATCH] Try $TMPDIR if $XDG_RUNTIME_DIR has no socket
@ 2020-06-04 13:05 Adam Edge
  2020-08-05 16:30 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 2+ messages in thread
From: Adam Edge @ 2020-06-04 13:05 UTC (permalink / raw)
  To: 41707; +Cc: Adam Edge

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






^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-08-05 16:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-06-04 13:05 bug#41707: [PATCH] Try $TMPDIR if $XDG_RUNTIME_DIR has no socket Adam Edge
2020-08-05 16:30 ` Lars Ingebrigtsen

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.