This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch tymux-12-session-parity
in repository terminology.

View the commit online.

commit afed7481d06f933aae1a7b0bc9bf81cb66d2dcf3
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 22 19:24:23 2026 -0600

    tymuxd: own the session name with a lock file
    
    A client that connected between the daemon's bind() and listen() got
    ECONNREFUSED, took the socket for a stale one and removed it, leaving
    the new daemon running but unreachable. The live tests hit this
    intermittently under load.
    
    A daemon now holds a lock on NAME.lock for as long as it serves the
    name. Holding it proves that an existing socket is stale, so only the
    owner removes one, and clients never do. The lock file stays behind
    on purpose: removing it would race with the next owner.
---
 man/tymux.1          |   4 ++
 src/bin/termsrv.c    | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/bin/termsrv.h    |  25 +++++++-
 src/bin/tymuxd.c     |  69 +++------------------
 src/bin/tytest.c     |   1 +
 src/bin/unit_tests.h |   1 +
 6 files changed, 202 insertions(+), 64 deletions(-)

diff --git a/man/tymux.1 b/man/tymux.1
index 4dc1448a..2ccfacfd 100644
--- a/man/tymux.1
+++ b/man/tymux.1
@@ -104,6 +104,10 @@ The sockets of the daemons are in
 or in
 .I /tmp/terminology/tymux/
 when it is not set.
+A
+.I NAME.lock
+file stays beside each socket: the daemon holds a lock on it for as long
+as it runs, which is what gives it the name.
 .TP
 .B TYMUX_NAME
 Set in the shells started by
diff --git a/src/bin/termsrv.c b/src/bin/termsrv.c
index 69302cb9..ae07cb0e 100644
--- a/src/bin/termsrv.c
+++ b/src/bin/termsrv.c
@@ -10,6 +10,7 @@
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <sys/file.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
 #include <sys/syscall.h>
@@ -175,8 +176,6 @@ termsrv_connect(const char *name)
      {
         int err = errno;
 
-        if (err == ECONNREFUSED)
-          unlink(sa.sun_path);
         close(fd);
         errno = err;
         return -1;
@@ -184,6 +183,77 @@ termsrv_connect(const char *name)
    return fd;
 }
 
+int
+termsrv_listener_open(const char *name, TermSrvListener *listener)
+{
+   struct sockaddr_un sa = { .sun_family = AF_UNIX };
+   char lock_path[sizeof(sa.sun_path)];
+   size_t len;
+   int err;
+
+   listener->fd = listener->lock_fd = -1;
+   if (termsrv_socket_path(name, listener->path, sizeof(listener->path)) < 0)
+     {
+        errno = EINVAL;
+        return -1;
+     }
+   len = strlen(listener->path) - strlen(".sock");
+   snprintf(lock_path, sizeof(lock_path), "%.*s.lock", (int)len,
+            listener->path);
+   listener->lock_fd = open(lock_path, O_RDWR | O_CREAT | O_CLOEXEC, 0600);
+   if (listener->lock_fd < 0)
+     return -1;
+   if (flock(listener->lock_fd, LOCK_EX | LOCK_NB) < 0)
+     {
+        err = (errno == EWOULDBLOCK) ? EADDRINUSE : errno;
+        goto fail;
+     }
+   /* holding the lock, a socket file left here belongs to nobody */
+   unlink(listener->path);
+   listener->fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
+                         0);
+   if (listener->fd < 0)
+     {
+        err = errno;
+        goto fail;
+     }
+   strcpy(sa.sun_path, listener->path);
+   if (bind(listener->fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
+     {
+        err = errno;
+        goto fail;
+     }
+   if (listen(listener->fd, 8) < 0)
+     {
+        err = errno;
+        unlink(listener->path);
+        goto fail;
+     }
+   return 0;
+
+fail:
+   if (listener->fd >= 0)
+     close(listener->fd);
+   close(listener->lock_fd);
+   listener->fd = listener->lock_fd = -1;
+   errno = err;
+   return -1;
+}
+
+/* The lock is released last, so that a daemon starting for the same name
+ * cannot publish its socket before the unlink below. NAME.lock stays:
+ * removing it would race with the next owner. */
+void
+termsrv_listener_close(TermSrvListener *listener)
+{
+   if (listener->fd < 0)
+     return;
+   close(listener->fd);
+   unlink(listener->path);
+   close(listener->lock_fd);
+   listener->fd = listener->lock_fd = -1;
+}
+
 /* SO_PEERPIDFD and pidfd_send_signal() are Linux 6.5 and 5.1 additions that
  * only the headers of a recent enough libc define, and whose numbers differ
  * between architectures. */
@@ -2086,4 +2156,96 @@ tytest_termsrv_backlog_map(void)
    assert(page_id == 5 && num_entries == 1 && got[0] == 1);
    return 0;
 }
+
+static void
+_socket_file_make(const char *path)
+{
+   struct sockaddr_un sa = { .sun_family = AF_UNIX };
+   int fd = socket(AF_UNIX, SOCK_STREAM, 0);
+
+   strcpy(sa.sun_path, path);
+   assert(fd >= 0 && bind(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0);
+   close(fd);
+}
+
+static int
+_accepts(const char *path)
+{
+   struct sockaddr_un sa = { .sun_family = AF_UNIX };
+   int fd = socket(AF_UNIX, SOCK_STREAM, 0), ok;
+
+   strcpy(sa.sun_path, path);
+   ok = connect(fd, (struct sockaddr *)&sa, sizeof(sa)) == 0;
+   close(fd);
+   return ok;
+}
+
+/* Test: a TermSrvListener owns its name for its whole life, and
+ * termsrv_connect() never removes a socket file. */
+int
+tytest_termsrv_listener(void)
+{
+   char runtime[] = "/tmp/tytest-listener-XXXXXX";
+   char path[PATH_MAX], dir[PATH_MAX], lock[PATH_MAX + 16];
+   char *old = getenv("XDG_RUNTIME_DIR");
+   char *saved = old ? strdup(old) : NULL;
+   TermSrvListener first, second;
+   int sock, flags;
+
+   assert(mkdtemp(runtime));
+   setenv("XDG_RUNTIME_DIR", runtime, 1);
+   assert(termsrv_ensure_tymux_dir() == 0);
+   assert(termsrv_socket_path("own", path, sizeof(path)) == 0);
+   assert(termsrv_tymux_dir(dir, sizeof(dir)) == 0);
+   snprintf(lock, sizeof(lock), "%s/own.lock", dir);
+
+   assert(termsrv_listener_open("own", &first) == 0);
+   flags = fcntl(first.fd, F_GETFL);
+   assert(flags >= 0 && (flags & O_NONBLOCK));
+   assert((fcntl(first.fd, F_GETFD) & FD_CLOEXEC) &&
+          (fcntl(first.lock_fd, F_GETFD) & FD_CLOEXEC));
+   assert(_accepts(path));
+   sock = termsrv_connect("own");
+   assert(sock >= 0);
+   close(sock);
+
+   assert(termsrv_listener_open("own", &second) == -1 &&
+          errno == EADDRINUSE);
+   assert(_accepts(path));
+
+   termsrv_listener_close(&first);
+   assert(access(path, F_OK) == -1);
+   assert(access(lock, F_OK) == 0);
+
+   /* a stale file: connect fails and leaves it, a new owner replaces it */
+   _socket_file_make(path);
+   assert(termsrv_connect("own") == -1 && errno == ECONNREFUSED);
+   assert(access(path, F_OK) == 0);
+   assert(termsrv_listener_open("own", &second) == 0);
+   assert(_accepts(path));
+   termsrv_listener_close(&second);
+
+   /* a socket bound but not listening yet, as in the window of the
+    * original bug, survives the connects of the clients polling for it */
+   _socket_file_make(path);
+   assert(termsrv_connect("own") == -1 && errno == ECONNREFUSED);
+   assert(termsrv_connect("own") == -1);
+   assert(access(path, F_OK) == 0);
+   unlink(path);
+
+   assert(termsrv_listener_open("own", &first) == 0);
+   termsrv_listener_close(&first);
+   unlink(lock);
+   rmdir(dir);
+   snprintf(lock, sizeof(lock), "%s/terminology", runtime);
+   rmdir(lock);
+   rmdir(runtime);
+   if (saved)
+     setenv("XDG_RUNTIME_DIR", saved, 1);
+   else
+     unsetenv("XDG_RUNTIME_DIR");
+   free(saved);
+   return 0;
+}
+
 #endif
diff --git a/src/bin/termsrv.h b/src/bin/termsrv.h
index b8e127df..d1ab3514 100644
--- a/src/bin/termsrv.h
+++ b/src/bin/termsrv.h
@@ -12,6 +12,7 @@
 #include <stddef.h>
 #include <string.h>
 #include <sys/types.h>
+#include <sys/un.h>
 #include <stdatomic.h>
 #include <Eina.h>
 
@@ -393,10 +394,30 @@ int termsrv_socket_path(const char *name, char *buf, size_t bufsz);
  * errno set. */
 int termsrv_ensure_tymux_dir(void);
 
-/* Connect to the tymux called name. A socket nobody listens on anymore is
- * removed. Returns the connected socket, or -1 with errno set. */
+/* Connect to the tymux called name. A socket file nobody listens on is
+ * left alone: it either belongs to a daemon not listening yet or to a dead
+ * one, whose successor removes it. Returns the connected socket, or -1
+ * with errno set. */
 int termsrv_connect(const char *name);
 
+/* The socket a tymuxd serves, and the lock that gives it the name. */
+typedef struct
+{
+   int fd;
+   int lock_fd;
+   char path[sizeof(((struct sockaddr_un *)0)->sun_path)];
+} TermSrvListener;
+
+/* Take the name for this process and listen on its socket, a
+ * non-blocking, close-on-exec one. The lock on NAME.lock is held until
+ * termsrv_listener_close() or the process exits, so a socket file found
+ * under it is stale and is replaced. Returns 0, or -1 with errno set:
+ * EADDRINUSE if another tymuxd owns the name. */
+int termsrv_listener_open(const char *name, TermSrvListener *listener);
+
+/* Stop listening, remove the socket and give the name up. */
+void termsrv_listener_close(TermSrvListener *listener);
+
 /* The pid of the tymuxd listening on sock, a socket termsrv_connect()
  * returned, as the kernel recorded it when the daemon started listening.
  * Returns -1 with errno set on error, EPERM if the daemon belongs to
diff --git a/src/bin/tymuxd.c b/src/bin/tymuxd.c
index fa702b32..3723ccd0 100644
--- a/src/bin/tymuxd.c
+++ b/src/bin/tymuxd.c
@@ -18,7 +18,7 @@
 int _log_domain = -1;
 
 static TermDTymux *_tymux = NULL;
-static int _listen_fd = -1;
+static TermSrvListener _listener = { .fd = -1, .lock_fd = -1 };
 
 #define ACCEPT_RETRY_MIN_SECONDS 0.1
 #define ACCEPT_RETRY_MAX_SECONDS 1.0
@@ -67,7 +67,7 @@ static Eina_Bool
 _cb_client_connect(void *data EINA_UNUSED,
                    Ecore_Fd_Handler *fd_handler EINA_UNUSED)
 {
-   int cfd = accept4(_listen_fd, NULL, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK);
+   int cfd = accept4(_listener.fd, NULL, NULL, SOCK_CLOEXEC | SOCK_NONBLOCK);
 
    if (cfd < 0)
      {
@@ -108,52 +108,6 @@ _cb_signal_exit(void *data EINA_UNUSED, int type EINA_UNUSED, void *event)
    return ECORE_CALLBACK_DONE;
 }
 
-/* Bind the listening socket at sa, unless a daemon is already serving it.
- * Returns the socket, or -1. */
-static int
-_listen_socket_new(const struct sockaddr_un *sa, const char *name)
-{
-   int fd;
-
-   fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
-   if (fd < 0)
-     {
-        ERR("socket: %s", strerror(errno));
-        return -1;
-     }
-   if (connect(fd, (const struct sockaddr *)sa, sizeof(*sa)) == 0)
-     {
-        fprintf(stderr, "tymuxd: '%s' is already running\n", name);
-        close(fd);
-        return -1;
-     }
-   /* left behind by a daemon that did not exit cleanly */
-   if (errno == ECONNREFUSED)
-     unlink(sa->sun_path);
-   close(fd);
-
-   fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
-   if (fd < 0)
-     {
-        ERR("socket: %s", strerror(errno));
-        return -1;
-     }
-   if (bind(fd, (const struct sockaddr *)sa, sizeof(*sa)) < 0)
-     {
-        ERR("bind '%s': %s", sa->sun_path, strerror(errno));
-        close(fd);
-        return -1;
-     }
-   if (listen(fd, 8) < 0)
-     {
-        ERR("listen: %s", strerror(errno));
-        unlink(sa->sun_path);
-        close(fd);
-        return -1;
-     }
-   return fd;
-}
-
 /* Raw scrollback pages each hold a memfd: keep three quarters of the
  * descriptors for the clients' sockets and everything else. */
 static void
@@ -173,7 +127,6 @@ _fd_budget_set(void)
 int
 main(int argc, char **argv)
 {
-   struct sockaddr_un sa = { .sun_family = AF_UNIX };
    const char *name;
    int ret = 1;
 
@@ -205,14 +158,14 @@ main(int argc, char **argv)
         ERR("could not create the tymux directory: %s", strerror(errno));
         goto end;
      }
-   if (termsrv_socket_path(name, sa.sun_path, sizeof(sa.sun_path)) < 0)
+   if (termsrv_listener_open(name, &_listener) < 0)
      {
-        ERR("socket path too long for '%s'", name);
+        if (errno == EADDRINUSE)
+          fprintf(stderr, "tymuxd: '%s' is already running\n", name);
+        else
+          ERR("could not listen for '%s': %s", name, strerror(errno));
         goto end;
      }
-   _listen_fd = _listen_socket_new(&sa, name);
-   if (_listen_fd < 0)
-     goto end;
 
    /* A session never inherits the hand-off token of the terminal that
     * spawned tymuxd, nor gets one of its own. */
@@ -223,7 +176,7 @@ main(int argc, char **argv)
    if (!_tymux)
      goto end;
 
-   _listen_handler = ecore_main_fd_handler_add(_listen_fd, ECORE_FD_READ,
+   _listen_handler = ecore_main_fd_handler_add(_listener.fd, ECORE_FD_READ,
                                               _cb_client_connect, NULL,
                                               NULL, NULL);
    if (!_listen_handler)
@@ -237,11 +190,7 @@ end:
      ecore_timer_del(_accept_retry);
    if (_listen_handler)
      ecore_main_fd_handler_del(_listen_handler);
-   if (_listen_fd >= 0)
-     {
-        close(_listen_fd);
-        unlink(sa.sun_path);
-     }
+   termsrv_listener_close(&_listener);
    termd_tymux_free(_tymux);
    if (_log_domain >= 0)
      eina_log_domain_unregister(_log_domain);
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 873f1506..c4aaddf9 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -261,6 +261,7 @@ static struct {
        { "backlog_compress", tytest_backlog_compress},
 #if defined(HAVE_TYMUX)
        { "termsrv_name", tytest_termsrv_name},
+       { "termsrv_listener", tytest_termsrv_listener},
        { "termsrv_reply_strip", tytest_termsrv_reply_strip},
        { "termsrv_handoff_reply", tytest_termsrv_handoff_reply},
        { "termsrv_chord_filter", tytest_termsrv_chord_filter},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 400624cd..178ca770 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -201,6 +201,7 @@ int tytest_backlog_store_anon_no_fd(void);
 int tytest_backlog_compress(void);
 #if defined(HAVE_TYMUX)
 int tytest_termsrv_name(void);
+int tytest_termsrv_listener(void);
 int tytest_termsrv_reply_strip(void);
 int tytest_termsrv_handoff_reply(void);
 int tytest_termsrv_chord_filter(void);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to