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 6bbe86388c4eb5d9ad95d6bf7e823b7115da906f
Author: [email protected] <[email protected]>
AuthorDate: Fri Sep 25 09:34:54 2026 -0600
termsrv: add backlog messages to the tymux protocol
BACKLOG carries one or more rows that entered the daemon's backlog,
each as a cell count followed by its cells, packed up to the payload
cap. BACKLOG_RESET tells a client to drop its backlog because a fresh
copy follows. Decoding checks every count against what is left of the
payload, so a malformed message is rejected instead of read past.
---
src/bin/termsrv.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++
src/bin/termsrv.h | 33 ++++++++++++++++++++-
src/bin/tytest.c | 1 +
src/bin/unit_tests.h | 1 +
4 files changed, 116 insertions(+), 1 deletion(-)
diff --git a/src/bin/termsrv.c b/src/bin/termsrv.c
index 3e59ec5f..7690fc84 100644
--- a/src/bin/termsrv.c
+++ b/src/bin/termsrv.c
@@ -420,6 +420,44 @@ termsrv_msg_send(int fd, TermSrvMsgType type,
return _sendmsg_all(fd, &msg);
}
+int
+termsrv_backlog_line_append(void *buf, uint32_t bufsz, uint32_t *off,
+ uint32_t cols, const TermSrvCell *cells)
+{
+ uint64_t need = (uint64_t)sizeof(cols) + (uint64_t)cols * sizeof(*cells);
+
+ if (*off > bufsz || need > (uint64_t)(bufsz - *off))
+ return -1;
+ memcpy((unsigned char *)buf + *off, &cols, sizeof(cols));
+ *off += sizeof(cols);
+ memcpy((unsigned char *)buf + *off, cells, cols * sizeof(*cells));
+ *off += cols * sizeof(*cells);
+ return 0;
+}
+
+int
+termsrv_backlog_line_next(const void *payload, uint32_t size,
+ uint32_t *off, uint32_t *cols_out,
+ const TermSrvCell **cells_out)
+{
+ uint32_t cols;
+ uint64_t need;
+
+ if (*off == size)
+ return 0;
+ if (*off > size || size - *off < sizeof(cols))
+ return -1;
+ memcpy(&cols, (const unsigned char *)payload + *off, sizeof(cols));
+ need = (uint64_t)sizeof(cols) + (uint64_t)cols * sizeof(TermSrvCell);
+ if (need > (uint64_t)(size - *off))
+ return -1;
+ *cols_out = cols;
+ *cells_out = (const TermSrvCell *)
+ ((const unsigned char *)payload + *off + sizeof(cols));
+ *off += (uint32_t)need;
+ return 1;
+}
+
/* What one call reads at most, so that a fast peer cannot keep the other
* end busy */
#define READER_FILL_MAX (4 * (TERMSRV_MAX_MSG_PAYLOAD + sizeof(TermSrvMsgHdr)))
@@ -1227,4 +1265,48 @@ tytest_termsrv_cell_layout(void)
assert(dst.att.link_id == 0xBEEF);
return 0;
}
+
+int
+tytest_termsrv_backlog_line(void)
+{
+ unsigned char buf[256];
+ TermSrvCell line1[3], line2[2];
+ uint32_t off = 0, cols;
+ const TermSrvCell *cells;
+
+ memset(line1, 0, sizeof(line1));
+ line1[0].codepoint = 'a';
+ line1[1].codepoint = 'b';
+ line1[2].codepoint = 'c';
+ memset(line2, 0, sizeof(line2));
+ line2[0].codepoint = 'd';
+ line2[1].codepoint = 'e';
+
+ assert(termsrv_backlog_line_append(buf, sizeof(buf), &off, 3, line1) == 0);
+ assert(termsrv_backlog_line_append(buf, sizeof(buf), &off, 2, line2) == 0);
+
+ off = 0;
+ assert(termsrv_backlog_line_next(buf, sizeof(buf), &off, &cols, &cells) == 1);
+ assert(cols == 3);
+ assert(cells[0].codepoint == 'a' && cells[2].codepoint == 'c');
+
+ assert(termsrv_backlog_line_next(buf, off + (uint32_t)sizeof(cols) +
+ 2 * (uint32_t)sizeof(TermSrvCell),
+ &off, &cols, &cells) == 1);
+ assert(cols == 2);
+ assert(cells[1].codepoint == 'e');
+
+ assert(termsrv_backlog_line_next(buf, off, &off, &cols, &cells) == 0);
+
+ /* too small a buffer to fit the line */
+ off = sizeof(buf) - 4;
+ assert(termsrv_backlog_line_append(buf, sizeof(buf), &off, 3, line1) < 0);
+
+ /* truncated payload: the header claims more cells than are there */
+ off = 0;
+ assert(termsrv_backlog_line_append(buf, sizeof(buf), &off, 3, line1) == 0);
+ assert(termsrv_backlog_line_next(buf, off - 1, &off, &cols, &cells) < 0);
+
+ return 0;
+}
#endif
diff --git a/src/bin/termsrv.h b/src/bin/termsrv.h
index f6dd6f3d..abf1c55b 100644
--- a/src/bin/termsrv.h
+++ b/src/bin/termsrv.h
@@ -105,7 +105,19 @@ typedef enum
TSRV_MSG_EXIT = 9, /* daemon: TermSrvMsgExit, the shell exited */
TSRV_MSG_PAUSE = 10, /* daemon: another client took over the input */
TSRV_MSG_RESUME = 11, /* daemon: the client that took over left */
- TSRV_MSG_LAST = TSRV_MSG_RESUME
+ /* daemon: one or more lines that entered the backlog, packed with
+ * termsrv_backlog_line_append() and read back with
+ * termsrv_backlog_line_next(); sent for lines already in the backlog
+ * right after ATTACHED, and again for every line that scrolls off the
+ * screen afterwards. */
+ TSRV_MSG_BACKLOG = 12,
+ /* daemon: this client fell behind on TSRV_MSG_BACKLOG and stopped
+ * receiving it for a while; its shadow backlog is stale and must be
+ * discarded before the fresh snapshot that immediately follows, in one
+ * or more TSRV_MSG_BACKLOG messages, is applied. NOTIFY and screen
+ * updates were not affected and need no such reset. */
+ TSRV_MSG_BACKLOG_RESET = 13,
+ TSRV_MSG_LAST = TSRV_MSG_BACKLOG_RESET
} TermSrvMsgType;
typedef enum
@@ -262,4 +274,23 @@ int termsrv_send_attached(int fd, int shm_fd,
int termsrv_recv_attached(int fd, int *shm_fd_out,
TermSrvMsgAttached *attached_out);
+/* Backlog lines are packed one after another in a TSRV_MSG_BACKLOG
+ * payload, each as a uint32_t cell count followed by that many
+ * TermSrvCell, so several lines can share one message within the
+ * TERMSRV_MAX_MSG_PAYLOAD cap.
+ *
+ * Append one line to buf (of bufsz bytes, *off already used). Returns 0 on
+ * success, -1 if it does not fit. */
+int termsrv_backlog_line_append(void *buf, uint32_t bufsz, uint32_t *off,
+ uint32_t cols, const TermSrvCell *cells);
+
+/* Take the next line out of a TSRV_MSG_BACKLOG payload of size bytes,
+ * starting at *off (0 on the first call), and advance *off past it.
+ * Returns 1 and sets cols_out and cells_out (pointing inside payload) when a
+ * line was read, 0 once *off reaches size, -1 if the payload is
+ * malformed. */
+int termsrv_backlog_line_next(const void *payload, uint32_t size,
+ uint32_t *off, uint32_t *cols_out,
+ const TermSrvCell **cells_out);
+
#endif
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 969a3487..e01e92a4 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -168,6 +168,7 @@ static struct {
{ "termsrv_shm_seqlock", tytest_termsrv_shm_seqlock},
{ "termsrv_attached", tytest_termsrv_attached},
{ "termsrv_cell_layout", tytest_termsrv_cell_layout},
+ { "termsrv_backlog_line", tytest_termsrv_backlog_line},
{ "termtymux_frame_apply_same_size", tytest_termtymux_frame_apply_same_size},
{ "termtymux_frame_apply_resize", tytest_termtymux_frame_apply_resize},
{ "termtymux_frame_apply_shrink_grow", tytest_termtymux_frame_apply_shrink_grow},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 852a4547..987cddbb 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -126,6 +126,7 @@ int tytest_termsrv_msg_send_full(void);
int tytest_termsrv_shm_seqlock(void);
int tytest_termsrv_attached(void);
int tytest_termsrv_cell_layout(void);
+int tytest_termsrv_backlog_line(void);
int tytest_termtymux_frame_apply_same_size(void);
int tytest_termtymux_frame_apply_resize(void);
int tytest_termtymux_frame_apply_shrink_grow(void);
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.