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

git pushed a commit to branch tymux-10-quiet-head
in repository terminology.

View the commit online.

commit f2fbad8c00b7907b69ed89a748ba7f1ac8561e39
Author: Cedric BAIL <[email protected]>
AuthorDate: Wed Nov 18 22:24:16 2026 -0600

    termd_tymux: publish the backlog in memfd pages
    
    Feed every row that enters the daemon's backlog to the page store, and
    let clients ask for the pages: ATTACHED and NOTIFY say which pages
    exist, PAGE_REQ returns a page memfd (held for that client until
    PAGE_RELEASE or disconnect), MAP_REQ the entry widths of a range. A
    client can then map the scrollback instead of receiving a copy of it.
    
    The page reply goes through the client's output queue like any other
    message, its memfd (a duplicate) queued with it and passed on the first
    byte of that message, so a full socket only delays it. A page that
    exists but cannot be sent with its memfd, for example when the dup
    fails, gets a PAGE without one, so the client does not wait for it
    forever. An unknown or evicted page gets no reply.
    
    The TSRV_MSG_BACKLOG streaming stays until the client switches to the
    pages, so tymux-07 clients keep working.
---
 src/bin/termd_backlog.c |   9 ++
 src/bin/termd_backlog.h |   3 +
 src/bin/termd_tymux.c   | 267 +++++++++++++++++++++++++++++++++++++--
 src/bin/termsrv.h       |   3 +-
 src/bin/tytest.c        |   2 +
 src/bin/tytest_tymux.c  | 325 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/bin/tytest_tymux.h  |   2 +
 tests/meson.build       |   3 +-
 8 files changed, 601 insertions(+), 13 deletions(-)

diff --git a/src/bin/termd_backlog.c b/src/bin/termd_backlog.c
index 87c18dd5..5b2c9971 100644
--- a/src/bin/termd_backlog.c
+++ b/src/bin/termd_backlog.c
@@ -245,6 +245,13 @@ termd_backlog_page_fd(TermDBacklog *bl, uint32_t page_id)
    return page ? page->fd : -1;
 }
 
+Eina_Bool
+termd_backlog_page_exists(TermDBacklog *bl, uint32_t page_id)
+{
+   EINA_SAFETY_ON_NULL_RETURN_VAL(bl, EINA_FALSE);
+   return _page_find(bl, page_id) != NULL;
+}
+
 void
 termd_backlog_hold(TermDBacklog *bl, uint32_t page_id)
 {
@@ -402,6 +409,8 @@ tytest_termd_backlog_pages(void)
      }
 
    assert(termd_backlog_page_fd(bl, 3) < 0);
+   assert(termd_backlog_page_exists(bl, 2));
+   assert(!termd_backlog_page_exists(bl, 3));
    termd_backlog_hold(bl, 3);
    termd_backlog_release(bl, 3);
    termd_backlog_free(bl);
diff --git a/src/bin/termd_backlog.h b/src/bin/termd_backlog.h
index e0407083..eeaf9869 100644
--- a/src/bin/termd_backlog.h
+++ b/src/bin/termd_backlog.h
@@ -29,6 +29,9 @@ uint32_t termd_backlog_count(const TermDBacklog *bl);
  * Returns -1 for an unknown or evicted page. */
 int termd_backlog_page_fd(TermDBacklog *bl, uint32_t page_id);
 
+/* Whether page_id is held, whatever state it is in. */
+Eina_Bool termd_backlog_page_exists(TermDBacklog *bl, uint32_t page_id);
+
 /* Count a client holding page_id, or one letting go of it. Unknown ids are
  * ignored. */
 void termd_backlog_hold(TermDBacklog *bl, uint32_t page_id);
diff --git a/src/bin/termd_tymux.c b/src/bin/termd_tymux.c
index ca6860ca..0ed2a8e7 100644
--- a/src/bin/termd_tymux.c
+++ b/src/bin/termd_tymux.c
@@ -11,6 +11,7 @@
 #include "config.h"
 #include "termpty.h"
 #include "termsrv.h"
+#include "termd_backlog.h"
 #include "backlog.h"
 #include "termd_tymux.h"
 
@@ -39,6 +40,14 @@ typedef enum
  * TSRV_MSG_BACKLOG_RESET and a fresh snapshot to replace what it missed. */
 #define CLIENT_BACKLOG_LOW  (CLIENT_OUT_MAX / 16)
 
+typedef struct
+{
+   size_t offset;
+   /* bytes of the message: the fd must not go out with more than that */
+   size_t length;
+   int    fd;
+} QueuedFd;
+
 typedef struct
 {
    int               fd;
@@ -53,11 +62,17 @@ typedef struct
    /* TSRV_MSG_BACKLOG is being withheld until the queue drains, see
     * CLIENT_BACKLOG_HIGH/LOW */
    Eina_Bool         backlog_out_of_sync : 1;
+   /* ids of the backlog pages this client holds, as uintptr_t, ascending */
+   Eina_List        *held_pages;
+   /* memfds waiting to go out with their message, in order: each rides on
+    * the first byte of the message that starts at offset in out */
+   Eina_List        *queued_fds;
 } Client;
 
 struct tag_TermDTymux
 {
    Termpty          *pty;
+   TermDBacklog     *backlog;
    Config           *config;
    int               shm_fd;
    TermSrvShmHeader *shm;
@@ -69,6 +84,7 @@ struct tag_TermDTymux
     * broadcast, packed with termsrv_backlog_line_append() */
    Eina_Binbuf      *backlog_pending;
    Eina_Bool         dirty : 1;
+   Eina_Bool         backlog_append_failed : 1;
 };
 
 static uint64_t
@@ -98,6 +114,7 @@ _screen_flush(TermDTymux *td)
 }
 
 static void _client_del(Client *client);
+static void _client_pages_prune(Client *client);
 static void _client_free(Client *client);
 static void _client_attach_backlog_send(Client *client);
 static Eina_Bool _client_send(Client *client, TermSrvMsgType type,
@@ -137,26 +154,81 @@ _client_hang_up(Client *client)
    shutdown(client->fd, SHUT_RDWR);
 }
 
+/* Send the first bytes of the output queue, up to bytes_max: with the memfd
+ * queued for its start if there is one, which then goes out on the first
+ * byte taken. Returns the bytes taken, 0 if the socket is full, -1 if the
+ * client is gone. */
+static ssize_t
+_client_out_send(Client *client, size_t bytes_max, QueuedFd *queued)
+{
+   const char *bytes = (const char *)eina_binbuf_string_get(client->out);
+   struct iovec iov = { .iov_base = (void *)bytes, .iov_len = bytes_max };
+   union {
+      char buf[CMSG_SPACE(sizeof(int))];
+      struct cmsghdr align;
+   } control;
+   struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1 };
+   ssize_t n;
+
+   if (queued)
+     {
+        struct cmsghdr *cm;
+
+        memset(&control, 0, sizeof(control));
+        msg.msg_control = control.buf;
+        msg.msg_controllen = sizeof(control.buf);
+        cm = CMSG_FIRSTHDR(&msg);
+        cm->cmsg_level = SOL_SOCKET;
+        cm->cmsg_type = SCM_RIGHTS;
+        cm->cmsg_len = CMSG_LEN(sizeof(int));
+        memcpy(CMSG_DATA(cm), &queued->fd, sizeof(int));
+     }
+   do
+     n = sendmsg(client->fd, &msg, MSG_DONTWAIT | MSG_NOSIGNAL);
+   while (n < 0 && errno == EINTR);
+   if (n < 0)
+     return (errno == EAGAIN || errno == EWOULDBLOCK) ? 0 : -1;
+   return n;
+}
+
 /* Send what the socket takes without blocking, watching for it to take
  * more if needed. Returns EINA_FALSE if the client is gone. */
 static Eina_Bool
 _client_flush(Client *client)
 {
    size_t len = eina_binbuf_length_get(client->out);
-   ssize_t n;
 
-   if (len > 0)
+   while (len > 0)
      {
-        n = send(client->fd, eina_binbuf_string_get(client->out), len,
-                 MSG_DONTWAIT | MSG_NOSIGNAL);
-        if (n < 0)
+        QueuedFd *head = eina_list_data_get(client->queued_fds);
+        QueuedFd *with_fd = NULL;
+        size_t bytes_max = len;
+        ssize_t n;
+        Eina_List *l;
+
+        if (head && head->offset == 0)
           {
-             if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
-               return EINA_FALSE;
-             n = 0;
+             with_fd = head;
+             bytes_max = MIN(head->length, len);
+          }
+        else if (head)
+          bytes_max = head->offset;
+        n = _client_out_send(client, bytes_max, with_fd);
+        if (n < 0)
+          return EINA_FALSE;
+        if (n == 0)
+          break;
+        if (with_fd)
+          {
+             close(with_fd->fd);
+             free(with_fd);
+             client->queued_fds = eina_list_remove_list(client->queued_fds,
+                                                        client->queued_fds);
           }
         eina_binbuf_remove(client->out, 0, n);
         len -= n;
+        EINA_LIST_FOREACH(client->queued_fds, l, head)
+          head->offset -= n;
      }
    ecore_main_fd_handler_active_set(client->handler,
                                     ECORE_FD_READ | (len ? ECORE_FD_WRITE : 0));
@@ -255,6 +327,8 @@ static void
 _flush_and_notify(TermDTymux *td)
 {
    Termpty *ty = td->pty;
+   Eina_List *l;
+   Client *client;
    TermSrvMsgNotify notify = {
         .appcursor = ty->termstate.appcursor,
         .appkeypad = ty->termstate.alt_kp,
@@ -275,6 +349,11 @@ _flush_and_notify(TermDTymux *td)
     * its backlog before the frame that no longer shows them */
    _backlog_pending_flush(td);
 
+   notify.first_page_id = termd_backlog_first_id(td->backlog);
+   notify.page_count = termd_backlog_count(td->backlog);
+   EINA_LIST_FOREACH(td->clients, l, client)
+     _client_pages_prune(client);
+
    notify.write_seq = _screen_flush(td);
    _broadcast(td, TSRV_MSG_NOTIFY, &notify, sizeof(notify));
 }
@@ -324,7 +403,9 @@ _td_has_backlog_client(TermDTymux *td)
 
 /* Called synchronously whenever a line permanently enters the daemon's own
  * backlog (termpty_text_save_top()), i.e. potentially many times between
- * two animator ticks under a flood such as `seq 1 1000000`. Pack it
+ * two animator ticks under a flood such as `seq 1 1000000`. It always
+ * goes to the page store, which clients map; then, for the clients that
+ * still read the stream, pack it
  * straight into td->backlog_pending (no intermediate copy: CELLS is
  * exactly TermSrvCell laid out, per the _Static_assert above), flushing
  * what is there first if the line would not fit in one TSRV_MSG_BACKLOG
@@ -337,12 +418,18 @@ _td_has_backlog_client(TermDTymux *td)
  * TERMSRV_MAX_COLS by _client_resize(), so it always fits in one message
  * on its own. */
 static void
-_cb_pty_backlog(void *data, Termpty *ty EINA_UNUSED, const Termcell *cells, ssize_t w, Eina_Bool extends EINA_UNUSED)
+_cb_pty_backlog(void *data, Termpty *ty EINA_UNUSED, const Termcell *cells, ssize_t w, Eina_Bool extends)
 {
    TermDTymux *td = data;
    uint32_t off, cols = (uint32_t)w;
    uint64_t need = sizeof(cols) + (uint64_t)cols * sizeof(TermSrvCell);
 
+   if (!termd_backlog_append(td->backlog, cells, w, extends) &&
+       !td->backlog_append_failed)
+     {
+        ERR("cannot store the backlog in pages");
+        td->backlog_append_failed = EINA_TRUE;
+     }
    if (!_td_has_backlog_client(td))
      return;
 
@@ -571,7 +658,9 @@ _client_attach(Client *client, TermSrvAttachMode mode)
    Client *other;
 
    if (termsrv_send_attached(client->fd, td->shm_fd, ty->w, ty->h,
-                             ty->pid > 0 ? ty->pid : -1, 0, 0) < 0)
+                             ty->pid > 0 ? ty->pid : -1,
+                             termd_backlog_first_id(td->backlog),
+                             termd_backlog_count(td->backlog)) < 0)
      {
         _client_del(client);
         return;
@@ -626,6 +715,132 @@ _client_resize(Client *client, const TermSrvMsgResize *resize)
    _flush_and_notify(td);
 }
 
+static Eina_Bool
+_client_page_held(const Client *client, uint32_t page_id)
+{
+   /* page ids start at 0, so look for the node, not the data */
+   return eina_list_data_find_list(client->held_pages,
+                                   (void *)(uintptr_t)page_id) != NULL;
+}
+
+/* Drop the holds on pages the store evicted since: the client has no reason
+ * to release them, and the list must not grow with the session. */
+static void
+_client_pages_prune(Client *client)
+{
+   uint32_t first = termd_backlog_first_id(client->td->backlog);
+
+   while (client->held_pages &&
+          (uintptr_t)eina_list_data_get(client->held_pages) < first)
+     client->held_pages = eina_list_remove_list(client->held_pages,
+                                                client->held_pages);
+}
+
+static int
+_page_id_cmp(const void *a, const void *b)
+{
+   uintptr_t ia = (uintptr_t)a, ib = (uintptr_t)b;
+
+   return (ia > ib) - (ia < ib);
+}
+
+static void
+_client_page_refuse(Client *client, const TermSrvMsgBacklogPageId *reply)
+{
+   if (!_client_send(client, TSRV_MSG_BACKLOG_PAGE, reply, sizeof(*reply)))
+     _client_hang_up(client);
+}
+
+/* Queue the reply in the output, ahead of what comes later, with a duplicate
+ * of the page memfd that _client_flush() passes along with it, so that a
+ * full socket only delays the page. A page that exists but cannot be
+ * exported is answered without a memfd, so the client stops waiting for
+ * it. An unknown or evicted page gets no reply. */
+static void
+_client_page_send(Client *client, uint32_t page_id)
+{
+   TermSrvMsgBacklogPageId reply = { page_id };
+   TermDTymux *td = client->td;
+   size_t length;
+   int page_fd;
+   QueuedFd *queued;
+
+   _client_pages_prune(client);
+   if (!termd_backlog_page_exists(td->backlog, page_id))
+     return;
+   page_fd = termd_backlog_page_fd(td->backlog, page_id);
+   if (page_fd < 0)
+     {
+        _client_page_refuse(client, &reply);
+        return;
+     }
+   if (eina_list_count(client->queued_fds) >= TERMSRV_READER_FDS_MAX)
+     {
+        WRN("client %d asks for too many pages", client->fd);
+        _client_hang_up(client);
+        return;
+     }
+   length = sizeof(TermSrvMsgHdr) + sizeof(reply);
+   if (eina_binbuf_length_get(client->out) + length > CLIENT_OUT_MAX)
+     {
+        WRN("client %d does not keep up", client->fd);
+        _client_hang_up(client);
+        return;
+     }
+   queued = malloc(sizeof(*queued));
+   if (!queued)
+     {
+        _client_page_refuse(client, &reply);
+        return;
+     }
+   queued->fd = fcntl(page_fd, F_DUPFD_CLOEXEC, 0);
+   if (queued->fd < 0)
+     {
+        free(queued);
+        _client_page_refuse(client, &reply);
+        return;
+     }
+   /* in the FIFO before the message: sending it already tries to flush, and
+    * queued may be gone when that returns */
+   queued->offset = eina_binbuf_length_get(client->out);
+   queued->length = length;
+   client->queued_fds = eina_list_append(client->queued_fds, queued);
+   if (!_client_send(client, TSRV_MSG_BACKLOG_PAGE, &reply, sizeof(reply)))
+     {
+        _client_hang_up(client);
+        return;
+     }
+   /* the flush may have deleted the client, which let go of its holds */
+   if (client->dead || _client_page_held(client, page_id))
+     return;
+   client->held_pages = eina_list_sorted_insert(client->held_pages,
+                                                _page_id_cmp,
+                                                (void *)(uintptr_t)page_id);
+   termd_backlog_hold(td->backlog, page_id);
+}
+
+static void
+_client_page_release(Client *client, uint32_t page_id)
+{
+   if (!_client_page_held(client, page_id))
+     return;
+   client->held_pages = eina_list_remove(client->held_pages,
+                                         (void *)(uintptr_t)page_id);
+   termd_backlog_release(client->td->backlog, page_id);
+}
+
+static void
+_client_map_send(Client *client, const TermSrvMsgBacklogMapReq *req)
+{
+   unsigned char buf[TERMSRV_MAX_MSG_PAYLOAD];
+   uint32_t len = termd_backlog_map_write(client->td->backlog,
+                                          req->first_page_id, req->count,
+                                          buf, sizeof(buf));
+
+   if (len > 0 && !_client_send(client, TSRV_MSG_BACKLOG_MAP, buf, len))
+     _client_hang_up(client);
+}
+
 /* Returns EINA_FALSE if the client must go */
 static Eina_Bool
 _client_msg_handle(Client *client, int type, void *payload, uint32_t size)
@@ -655,6 +870,20 @@ _client_msg_handle(Client *client, int type, void *payload, uint32_t size)
          if (size == sizeof(TermSrvMsgResize))
            _client_resize(client, payload);
          break;
+      case TSRV_MSG_BACKLOG_PAGE_REQ:
+         if (size == sizeof(TermSrvMsgBacklogPageId))
+           _client_page_send(client,
+                             ((TermSrvMsgBacklogPageId *)payload)->page_id);
+         break;
+      case TSRV_MSG_BACKLOG_PAGE_RELEASE:
+         if (size == sizeof(TermSrvMsgBacklogPageId))
+           _client_page_release(client,
+                                ((TermSrvMsgBacklogPageId *)payload)->page_id);
+         break;
+      case TSRV_MSG_BACKLOG_MAP_REQ:
+         if (size == sizeof(TermSrvMsgBacklogMapReq))
+           _client_map_send(client, payload);
+         break;
       default:
          break;
      }
@@ -708,11 +937,19 @@ _cb_client_data(void *data, Ecore_Fd_Handler *fd_handler)
 static void
 _client_free(Client *client)
 {
+   QueuedFd *queued;
+
    if (client->handler)
      ecore_main_fd_handler_del(client->handler);
    close(client->fd);
    eina_binbuf_free(client->out);
    termsrv_reader_clear(&client->reader);
+   eina_list_free(client->held_pages);
+   EINA_LIST_FREE(client->queued_fds, queued)
+     {
+        close(queued->fd);
+        free(queued);
+     }
    free(client);
 }
 
@@ -724,6 +961,9 @@ _client_del(Client *client)
    if (client->dead)
      return;
    client->dead = EINA_TRUE;
+   while (client->held_pages)
+     _client_page_release(client,
+                          (uintptr_t)eina_list_data_get(client->held_pages));
    td->clients = eina_list_remove(td->clients, client);
    if (client->role == CLIENT_PAUSED)
      td->paused = eina_list_remove(td->paused, client);
@@ -787,6 +1027,10 @@ termd_tymux_new(const char *name, const char *cmd)
    td->config->scrollback = 10000;
    td->config->term_type = TERM_TYPE_XTERM_256COLOR;
 
+   td->backlog = termd_backlog_new(name, td->config->scrollback);
+   if (!td->backlog)
+     goto err;
+
    snprintf(memfd_name, sizeof(memfd_name), "tymux-%s", name);
    td->shm_fd = memfd_create(memfd_name, MFD_CLOEXEC);
    if (td->shm_fd < 0)
@@ -863,6 +1107,7 @@ termd_tymux_free(TermDTymux *td)
      munmap(td->shm, TERMSRV_SHM_TOTAL_SIZE);
    if (td->shm_fd >= 0)
      close(td->shm_fd);
+   termd_backlog_free(td->backlog);
    free(td->config);
    free(td);
 }
diff --git a/src/bin/termsrv.h b/src/bin/termsrv.h
index 7eeab01c..56551694 100644
--- a/src/bin/termsrv.h
+++ b/src/bin/termsrv.h
@@ -150,7 +150,8 @@ typedef enum
    /* client: ask for a backlog page, TermSrvMsgBacklogPageId. An unknown
     * or evicted page gets no reply. */
    TSRV_MSG_BACKLOG_PAGE_REQ = 14,
-   /* daemon: TermSrvMsgBacklogPageId + the page memfd (SCM_RIGHTS) */
+   /* daemon: TermSrvMsgBacklogPageId, then the page memfd over SCM_RIGHTS,
+    * or no fd if the page exists but cannot be served */
    TSRV_MSG_BACKLOG_PAGE = 15,
    /* client: done with a page, TermSrvMsgBacklogPageId */
    TSRV_MSG_BACKLOG_PAGE_RELEASE = 16,
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 5d91f353..15b59932 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -205,6 +205,8 @@ static const struct {
 } _tytests_live[] = {
 #if defined(HAVE_TYMUX)
        { "tymux_live_backlog", tytest_tymux_live_backlog},
+       { "tymux_live_pages", tytest_tymux_live_pages},
+       { "tymux_live_pages_busy", tytest_tymux_live_pages_busy},
 #endif
        { NULL, NULL},
 };
diff --git a/src/bin/tytest_tymux.c b/src/bin/tytest_tymux.c
index e500b1d8..df2f92b6 100644
--- a/src/bin/tytest_tymux.c
+++ b/src/bin/tytest_tymux.c
@@ -6,8 +6,10 @@
 #include <signal.h>
 #include <stdlib.h>
 #include <string.h>
+#include <poll.h>
 #include <time.h>
 #include <unistd.h>
+#include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/wait.h>
 #include <Elementary.h>
@@ -461,3 +463,326 @@ tytest_tymux_live_backlog(void)
 {
    return tytest_tymux_live_run(_live_backlog_body);
 }
+
+/* Next message from fd within timeout_ms: its type, or 0 if none came, -1
+ * on error. *payload and *pass_fd are the caller's to free and close. */
+static int
+_pages_recv(TermSrvReader *reader, int fd, int timeout_ms,
+            void **payload, uint32_t *size, int *pass_fd)
+{
+   struct pollfd pfd = { .fd = fd, .events = POLLIN };
+   int ignored_fd = -1, type;
+
+   if (!pass_fd)
+     pass_fd = &ignored_fd;
+   type = termsrv_reader_next_fd(reader, payload, size, pass_fd);
+   while (type == 0)
+     {
+        if (poll(&pfd, 1, timeout_ms) <= 0)
+          return 0;
+        if (termsrv_reader_fill(reader, fd) < 0 && !reader->eof)
+          return -1;
+        type = termsrv_reader_next_fd(reader, payload, size, pass_fd);
+        if (type == 0 && reader->eof)
+          return -1;
+     }
+   if (pass_fd == &ignored_fd && ignored_fd >= 0)
+     close(ignored_fd);
+   return type;
+}
+
+/* The number a backlog entry spells, -1 if it is not just digits. */
+static long
+_pages_entry_number(const TermSrvCell *cells, uint32_t w)
+{
+   long value = 0;
+   uint32_t i;
+
+   for (i = 0; i < w && cells[i].codepoint != ' ' && cells[i].codepoint != 0; i++)
+     {
+        if (cells[i].codepoint < '0' || cells[i].codepoint > '9')
+          return -1;
+        value = value * 10 + (cells[i].codepoint - '0');
+     }
+   return (i == 0) ? -1 : value;
+}
+
+static Eina_Bool
+_pages_map_entries(int fd, TermSrvReader *reader, uint32_t first,
+                   uint32_t count, uint32_t *oldest_entries, uint32_t *total)
+{
+   uint32_t next = first;
+
+   *total = 0;
+   while (next - first < count)
+     {
+        TermSrvMsgBacklogMapReq req = { next, first + count - next };
+        TermSrvMsgBacklogMapReq *echo;
+        void *payload;
+        uint32_t size, off = sizeof(req), page_id, num;
+        const uint32_t *widths;
+        int type;
+
+        if (termsrv_msg_send(fd, TSRV_MSG_BACKLOG_MAP_REQ, &req, sizeof(req)) < 0)
+          return EINA_FALSE;
+        for (;;)
+          {
+             type = _pages_recv(reader, fd, 5000, &payload, &size, NULL);
+             if (type <= 0 || type == TSRV_MSG_BACKLOG_MAP)
+               break;
+             free(payload);
+          }
+        if (type != TSRV_MSG_BACKLOG_MAP || size < sizeof(req))
+          return EINA_FALSE;
+        echo = payload;
+        if (echo->first_page_id != next || echo->count == 0)
+          {
+             free(payload);
+             return EINA_FALSE;
+          }
+        while (termsrv_backlog_map_page_next(payload, size, &off, &page_id,
+                                             &num, &widths) > 0)
+          {
+             if (next == first && page_id == first)
+               *oldest_entries = num;
+             *total += num;
+          }
+        next += echo->count;
+        free(payload);
+     }
+   return EINA_TRUE;
+}
+
+static Eina_Bool
+_pages_check_oldest(int fd, TermSrvReader *reader, uint32_t first,
+                    uint32_t num_entries)
+{
+   TermSrvMsgBacklogPageId req = { first };
+   const TermSrvBacklogPage *page;
+   const TermSrvCell *cells;
+   TermSrvMsgBacklogPageId *reply;
+   void *payload;
+   uint32_t size, i;
+   int type, page_fd = -1;
+   long expected = -1;
+   Eina_Bool ok = EINA_TRUE;
+   size_t map_size = TERMSRV_BACKLOG_PAGE_SIZE;
+
+   if (termsrv_msg_send(fd, TSRV_MSG_BACKLOG_PAGE_REQ, &req, sizeof(req)) < 0)
+     return EINA_FALSE;
+   do
+     {
+        type = _pages_recv(reader, fd, 5000, &payload, &size, &page_fd);
+        if (type > 0 && type != TSRV_MSG_BACKLOG_PAGE)
+          {
+             free(payload);
+             if (page_fd >= 0) close(page_fd);
+             page_fd = -1;
+          }
+     }
+   while (type > 0 && type != TSRV_MSG_BACKLOG_PAGE);
+   if (type != TSRV_MSG_BACKLOG_PAGE || page_fd < 0 ||
+       size != sizeof(*reply))
+     return EINA_FALSE;
+   reply = payload;
+   ok = reply->page_id == first;
+   free(payload);
+   page = mmap(NULL, map_size, PROT_READ, MAP_SHARED, page_fd, 0);
+   close(page_fd);
+   if (page == MAP_FAILED)
+     return EINA_FALSE;
+   cells = (const TermSrvCell *)((const unsigned char *)page +
+                                 TERMSRV_BACKLOG_HEADER_SIZE);
+   ok = ok && page->magic == TERMSRV_BACKLOG_MAGIC &&
+      page->page_id == first && page->num_entries == num_entries;
+   for (i = 0; ok && i < num_entries; i++)
+     {
+        long n = _pages_entry_number(cells + page->entry_off[i],
+                                     page->entry_w[i]);
+
+        if (expected < 0)
+          expected = (n >= 0) ? n + 1 : -1;
+        else
+          {
+             ok = (n == expected);
+             expected++;
+          }
+     }
+   /* the oldest page must have held a run of numbers, not only prompts */
+   ok = ok && expected > 0;
+   munmap((void *)page, map_size);
+   return ok;
+}
+
+static int
+_live_pages_body(void)
+{
+   TermTymux *ts;
+   Termpty *ty;
+   TermSrvReader reader = { 0 };
+   TermSrvMsgAttached attached;
+   TermSrvMsgBacklogPageId unknown;
+   TermSrvMsgBacklogMapReq req;
+   void *payload;
+   uint32_t size, total = 0, oldest_entries = 0;
+   int fd, shm_fd, type, pass_fd = -1;
+   uint8_t mode = TSRV_ATTACH_READONLY;
+
+   assert(tytest_tymux_start(SESSION, 80, 24, &ts, &ty));
+   termtymux_resize(ts, 80, SCREEN_ROWS);
+   assert(tytest_tymux_wait(_screen_rows_done, ty, 5.0));
+   assert(tytest_tymux_type(ts, ty, "seq 1 3000\n"));
+   assert(tytest_tymux_wait(_seq_done, ty, 20.0));
+
+   fd = termsrv_connect(SESSION);
+   assert(fd >= 0);
+   assert(termsrv_msg_send(fd, TSRV_MSG_ATTACH, &mode, sizeof(mode)) == 0);
+   assert(termsrv_recv_attached(fd, &shm_fd, &attached) == 0);
+   close(shm_fd);
+   assert(attached.page_count >= 2);
+
+   assert(_pages_map_entries(fd, &reader, attached.first_page_id,
+                             attached.page_count, &oldest_entries, &total));
+   assert(total >= SEQ_CHECKED);
+   assert(_pages_check_oldest(fd, &reader, attached.first_page_id,
+                              oldest_entries));
+
+   unknown.page_id = attached.first_page_id - 1;
+   assert(termsrv_msg_send(fd, TSRV_MSG_BACKLOG_PAGE_REQ, &unknown,
+                           sizeof(unknown)) == 0);
+   do
+     {
+        type = _pages_recv(&reader, fd, 1000, &payload, &size, &pass_fd);
+        assert(type >= 0);
+        if (type > 0)
+          {
+             assert(type != TSRV_MSG_BACKLOG_PAGE);
+             free(payload);
+             if (pass_fd >= 0)
+               {
+                  close(pass_fd);
+                  pass_fd = -1;
+               }
+          }
+     }
+   while (type > 0);
+   req.first_page_id = attached.first_page_id;
+   req.count = 1;
+   assert(termsrv_msg_send(fd, TSRV_MSG_BACKLOG_MAP_REQ, &req,
+                           sizeof(req)) == 0);
+   do
+     {
+        type = _pages_recv(&reader, fd, 5000, &payload, &size, NULL);
+        if (type > 0 && type != TSRV_MSG_BACKLOG_MAP)
+          free(payload);
+     }
+   while (type > 0 && type != TSRV_MSG_BACKLOG_MAP);
+   assert(type == TSRV_MSG_BACKLOG_MAP);
+   free(payload);
+
+   termsrv_reader_clear(&reader);
+   close(fd);
+   tytest_tymux_stop(ts);
+   return 0;
+}
+
+int
+tytest_tymux_live_pages(void)
+{
+   return tytest_tymux_live_run(_live_pages_body);
+}
+
+static Eina_Bool
+_never_done(void *data EINA_UNUSED)
+{
+   return EINA_FALSE;
+}
+
+/* The flood stays under the scrollback of the daemon (10000 entries), so
+ * that the page asked for is not evicted while the client is not reading. A
+ * full socket, and a queue past CLIENT_BACKLOG_HIGH, are still reached: the
+ * 4000 lines of the first flood are some 2 MB of backlog messages. The 3000
+ * lines of the second one only add screen updates behind the page.
+ *
+ * A client that does not read while a flood goes by has a full socket and
+ * a growing queue in the daemon: a page it asks for then must wait in that
+ * queue, with its memfd, and still arrive when the client reads again. */
+static int
+_live_pages_busy_body(void)
+{
+   TermTymux *ts;
+   Termpty *ty;
+   TermSrvReader reader = { 0 };
+   TermSrvMsgAttached attached;
+   TermSrvMsgBacklogPageId req;
+   const TermSrvBacklogPage *page;
+   void *payload;
+   uint32_t size;
+   int fd, shm_fd, type, page_fd = -1;
+   uint8_t mode = TSRV_ATTACH_READONLY;
+   size_t after_page = 0;
+
+   assert(tytest_tymux_start(SESSION, 80, 24, &ts, &ty));
+   termtymux_resize(ts, 80, SCREEN_ROWS);
+   assert(tytest_tymux_wait(_screen_rows_done, ty, 5.0));
+
+   fd = termsrv_connect(SESSION);
+   assert(fd >= 0);
+   assert(termsrv_msg_send(fd, TSRV_MSG_ATTACH, &mode, sizeof(mode)) == 0);
+   assert(termsrv_recv_attached(fd, &shm_fd, &attached) == 0);
+   close(shm_fd);
+
+   /* fd is not read from now on, until the flood is over */
+   assert(tytest_tymux_type(ts, ty, "seq 1 4000\n"));
+   tytest_tymux_wait(_never_done, NULL, 3.0);
+   req.page_id = attached.first_page_id;
+   assert(termsrv_msg_send(fd, TSRV_MSG_BACKLOG_PAGE_REQ, &req,
+                           sizeof(req)) == 0);
+   /* more screen updates, which queue behind the page in the daemon: more
+    * than the 4096 bytes a read takes, so that a read that picks up the
+    * page memfd ends well after the page message */
+   assert(tytest_tymux_type(ts, ty, "seq 1 3000\n"));
+   tytest_tymux_wait(_never_done, NULL, 3.0);
+
+   for (;;)
+     {
+        int msg_fd = -1;
+
+        type = _pages_recv(&reader, fd, 1000, &payload, &size, &msg_fd);
+        assert(type >= 0);
+        if (type == 0)
+          break;
+        free(payload);
+        if (type == TSRV_MSG_BACKLOG_PAGE)
+          {
+             assert(page_fd < 0 && msg_fd >= 0);
+             page_fd = msg_fd;
+             continue;
+          }
+        /* no other message has a memfd */
+        assert(msg_fd < 0);
+        if (page_fd >= 0)
+          after_page += sizeof(TermSrvMsgHdr) + size;
+     }
+   assert(after_page > 4096);
+   assert(page_fd >= 0);
+   page = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE, PROT_READ, MAP_SHARED,
+               page_fd, 0);
+   assert(page != MAP_FAILED);
+   assert(page->magic == TERMSRV_BACKLOG_MAGIC);
+   assert(page->page_id == attached.first_page_id);
+   munmap((void *)page, TERMSRV_BACKLOG_PAGE_SIZE);
+   close(page_fd);
+   assert(!reader.eof);
+
+   termsrv_reader_clear(&reader);
+   close(fd);
+   tytest_tymux_stop(ts);
+   return 0;
+}
+
+int
+tytest_tymux_live_pages_busy(void)
+{
+   return tytest_tymux_live_run(_live_pages_busy_body);
+}
diff --git a/src/bin/tytest_tymux.h b/src/bin/tytest_tymux.h
index b168999d..95fa83ba 100644
--- a/src/bin/tytest_tymux.h
+++ b/src/bin/tytest_tymux.h
@@ -43,5 +43,7 @@ void tytest_tymux_stop(TermTymux *ts);
 int tytest_tymux_daemon_memfds(TermTymux *ts, const char *prefix);
 
 int tytest_tymux_live_backlog(void);
+int tytest_tymux_live_pages(void);
+int tytest_tymux_live_pages_busy(void);
 
 #endif
diff --git a/tests/meson.build b/tests/meson.build
index de9726eb..b5b19b18 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -100,7 +100,8 @@ if tests
     # Drives a real tymuxd through the native client built into tytest.
     test('tymux-live',
          tytest,
-         args: ['tymux_live_backlog'],
+         args: ['tymux_live_backlog', 'tymux_live_pages',
+                'tymux_live_pages_busy'],
          depends: [tymuxd],
          timeout: 120)
   endif

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

Reply via email to