unofficial mirror of bug-gnu-emacs@gnu.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

* bug#41707: [PATCH] Try $TMPDIR if $XDG_RUNTIME_DIR has no socket
  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
  0 siblings, 0 replies; 2+ messages in thread
From: Lars Ingebrigtsen @ 2020-08-05 16:30 UTC (permalink / raw)
  To: Adam Edge; +Cc: 41707

Adam Edge <baronedge@airmail.cc> writes:

> 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.

As far as I can see, Emacs uses the XDG directory:

;; We do not use `temporary-file-directory' here, because emacsclient
;; does not read the init file.
(defvar server-socket-dir
  (if internal--daemon-sockname
      (file-name-directory internal--daemon-sockname)
    (and (featurep 'make-network-process '(:family local))
	 (let ((xdg_runtime_dir (getenv "XDG_RUNTIME_DIR")))
	   (if xdg_runtime_dir
	       (format "%s/emacs" xdg_runtime_dir)
	     (format "%s/emacs%d" (or (getenv "TMPDIR") "/tmp") (user-uid))))))
  "The directory in which to place the server socket.
If local sockets are not supported, this is nil.")

If your Emacs doesn't, then that seems like a bug that should be fixed,
instead of changing emacsclient to check both directories?

Or perhaps you're running a different version of Emacs and emacsclient?
That often has problems and isn't recommended.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).