bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#41707: [PATCH] Try $TMPDIR if $XDG_RUNTIME_DIR has no socket


From: Adam Edge
Subject: bug#41707: [PATCH] Try $TMPDIR if $XDG_RUNTIME_DIR has no socket
Date: Thu, 4 Jun 2020 16:05:00 +0300

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 via email to

[Prev in Thread] Current Thread [Next in Thread]