This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch master
in repository terminology.
View the commit online.
commit 5625bdba2464cf9457747896d4278299080bbb2c
Author: Boris Faure <[email protected]>
AuthorDate: Sat Nov 28 20:17:37 2026 +0000
termsrv/tymuxd: harden socket setup and bound client input
Three fixes from the review of this branch, squashed into one:
- The listening socket is created atomically: bind a
NAME.sock.<pid>.tmp, listen, then link() it to NAME.sock. The final
path appears only once the socket accepts, so termsrv_connect()'s
unlink-on-ECONNREFUSED can no longer hit a daemon between bind() and
listen() - which used to unlink its socket, fail the spawn after
10 s and leave an unreachable daemon running. A second daemon on
the same name is refused with EEXIST, a stale file is replaced.
- A client whose INPUT the shell never drains is detached once the
pty's write buffer passes 1 MiB, as the output queue already did:
in raw mode the pty stops accepting and the daemon's memory grew
without limit with what a client sent.
- termsrv_recv_attached() closes every fd of an invalid ATTACHED
instead of leaving the extras open until exit.
Adapted to e/master while rebasing: termpty_new() gained the cell
size arguments for the pixel winsize (0, 0 for a headless daemon),
and the XTWINOPS CSI 14;2 t report needs a termio_window_size_get()
stub in tymuxd_stubs.c (0x0, there is no window).
CI fix (clang-release dropped the primary client in the stuck-client
test): the output queue of a client is now capped in two tiers - past
1 MiB the client is dropped once it has made no progress for 2 s,
past 8 MiB at once - so a burst cannot drop a client that is merely
slow, while a stopped one is still dropped. The proxy also wrote to
the outer terminal with blocking writes, so a slow terminal stalled
its main loop, it stopped reading the session socket, and the daemon
dropped it: its output now goes through a queue written without
blocking.
Tests: termsrv_listen_socket and termsrv_attached_extra_fds unit
tests, and tymux.py scenarios flooding titles at a stopped client
and flooding INPUT at a sleeping shell. Each fails without its fix;
the clang-release suite passes repeatedly with them.
---
src/bin/termd_tymux.c | 62 ++++++++++++-
src/bin/termpty.h | 6 ++
src/bin/termsrv.c | 229 +++++++++++++++++++++++++++++++++++++++++++++++--
src/bin/termsrv.h | 9 ++
src/bin/tymux.c | 109 +++++++++++++++++++----
src/bin/tymuxd.c | 57 ++----------
src/bin/tymuxd_stubs.c | 8 ++
src/bin/tytest.c | 2 +
src/bin/unit_tests.h | 2 +
tests/tymux.py | 49 ++++++++++-
10 files changed, 453 insertions(+), 80 deletions(-)
diff --git a/src/bin/termd_tymux.c b/src/bin/termd_tymux.c
index e2d093b0..e4573d9c 100644
--- a/src/bin/termd_tymux.c
+++ b/src/bin/termd_tymux.c
@@ -27,8 +27,18 @@ typedef enum
/* What may wait in the output queue of a client that does not keep up
* before it is dropped: it must not stall the shell and the other
- * clients. */
+ * clients. Past CLIENT_OUT_MAX a client that has sent nothing for
+ * CLIENT_OUT_GRACE seconds is dropped; past CLIENT_OUT_HARD_MAX it is
+ * dropped at once, so even a burst can never grow the queue without
+ * bound. A client that is merely slow keeps flushing and is never
+ * dropped for a burst. */
#define CLIENT_OUT_MAX (1 << 20)
+#define CLIENT_OUT_HARD_MAX (8 << 20)
+#define CLIENT_OUT_GRACE 2.0
+/* Same bound on the input side: what a client sent that the shell has
+ * not read yet waits in the pty's write buffer, and one that floods it
+ * is detached rather than allowed to grow it without limit. */
+#define CLIENT_IN_MAX (1 << 20)
typedef struct
{
@@ -39,6 +49,7 @@ typedef struct
/* messages the socket did not take yet */
Eina_Binbuf *out;
TermSrvReader reader;
+ double last_progress; /* last time the socket took bytes */
Eina_Bool dispatching : 1;
Eina_Bool dead : 1;
} Client;
@@ -53,6 +64,7 @@ struct tag_TermDTymux
/* clients paused by a hijack, most recent first */
Eina_List *paused;
Ecore_Animator *notify_anim;
+ Ecore_Timer *queue_timer;
Eina_Bool dirty : 1;
};
@@ -114,6 +126,8 @@ _client_flush(Client *client)
}
eina_binbuf_remove(client->out, 0, n);
len -= n;
+ if (n > 0)
+ client->last_progress = ecore_loop_time_get();
}
ecore_main_fd_handler_active_set(client->handler,
ECORE_FD_READ | (len ? ECORE_FD_WRITE : 0));
@@ -128,7 +142,7 @@ _client_send(Client *client, TermSrvMsgType type,
TermSrvMsgHdr hdr = { .type = type, .size = size };
if (eina_binbuf_length_get(client->out) + sizeof(hdr) + size >
- CLIENT_OUT_MAX)
+ CLIENT_OUT_HARD_MAX)
{
WRN("client %d does not keep up", client->fd);
return EINA_FALSE;
@@ -314,7 +328,16 @@ _client_msg_handle(Client *client, int type, void *payload, uint32_t size)
{
case TSRV_MSG_INPUT:
if (client->role == CLIENT_PRIMARY || client->role == CLIENT_SHARED)
- termpty_write(client->td->pty, payload, size);
+ {
+ if (termpty_write_buffer_len(client->td->pty) + size >
+ CLIENT_IN_MAX)
+ {
+ WRN("client %d floods input the shell does not read",
+ client->fd);
+ return EINA_FALSE;
+ }
+ termpty_write(client->td->pty, payload, size);
+ }
break;
case TSRV_MSG_RESIZE:
if (size == sizeof(TermSrvMsgResize))
@@ -405,6 +428,30 @@ _client_del(Client *client)
_client_free(client);
}
+/* Drop the clients whose output queue is past the soft limit while they
+ * made no progress for the grace period: a stopped client frees its
+ * memory, a slow one that keeps reading is left alone. Runs on a timer
+ * so a client is dropped even when no more messages arrive. */
+static Eina_Bool
+_cb_queue_check(void *data)
+{
+ TermDTymux *td = data;
+ Eina_List *l, *l_next;
+ Client *client;
+ double now = ecore_loop_time_get();
+
+ EINA_LIST_FOREACH_SAFE(td->clients, l, l_next, client)
+ {
+ if (eina_binbuf_length_get(client->out) > CLIENT_OUT_MAX &&
+ now - client->last_progress > CLIENT_OUT_GRACE)
+ {
+ WRN("client %d does not keep up", client->fd);
+ _client_del(client);
+ }
+ }
+ return ECORE_CALLBACK_RENEW;
+}
+
void
termd_tymux_client_add(TermDTymux *td, int cfd)
{
@@ -419,6 +466,7 @@ termd_tymux_client_add(TermDTymux *td, int cfd)
client->fd = cfd;
client->td = td;
client->role = CLIENT_PENDING;
+ client->last_progress = ecore_loop_time_get();
client->out = eina_binbuf_new();
client->handler = ecore_main_fd_handler_add(cfd, ECORE_FD_READ,
_cb_client_data, client,
@@ -475,7 +523,9 @@ termd_tymux_new(const char *name, const char *cmd)
td->shm->magic = TERMSRV_SHM_MAGIC;
td->shm->version = TERMSRV_SHM_VERSION;
- td->pty = termpty_new(cmd, EINA_FALSE, NULL, 80, 24, td->config, name, 0);
+ /* no font in a headless daemon: the pixel size of the winsize stays 0 */
+ td->pty = termpty_new(cmd, EINA_FALSE, NULL, 80, 24, 0, 0,
+ td->config, name, 0);
if (!td->pty)
goto err;
td->pty->cb.change.func = _cb_pty_change;
@@ -487,6 +537,8 @@ termd_tymux_new(const char *name, const char *cmd)
td->pty->cb.exited.func = _cb_pty_exited;
td->pty->cb.exited.data = ""
+ td->queue_timer = ecore_timer_add(1.0, _cb_queue_check, td);
+
_screen_flush(td);
return td;
@@ -504,6 +556,8 @@ termd_tymux_free(TermDTymux *td)
return;
if (td->notify_anim)
ecore_animator_del(td->notify_anim);
+ if (td->queue_timer)
+ ecore_timer_del(td->queue_timer);
EINA_LIST_FREE(td->clients, client)
{
struct timeval timeout = { .tv_sec = 0, .tv_usec = 200 * 1000 };
diff --git a/src/bin/termpty.h b/src/bin/termpty.h
index 9693f5c8..4a5326ec 100644
--- a/src/bin/termpty.h
+++ b/src/bin/termpty.h
@@ -423,6 +423,12 @@ ssize_t termpty_row_length(Termpty *ty, int y);
#define TERMPTY_WRITE_STR(S_) \
termpty_write(ty, S_, strlen(S_))
void termpty_write(Termpty *ty, const char *input, int len);
+/* Bytes queued for the pty that the child has not read yet. */
+static inline size_t
+termpty_write_buffer_len(const Termpty *ty)
+{
+ return ty->write_buffer.len;
+}
void termpty_resize(Termpty *ty, int new_w, int new_h);
/* store the cell size in pixels; the winsize follows on the next resize */
diff --git a/src/bin/termsrv.c b/src/bin/termsrv.c
index 3e59ec5f..bea5398c 100644
--- a/src/bin/termsrv.c
+++ b/src/bin/termsrv.c
@@ -184,6 +184,76 @@ termsrv_connect(const char *name)
return fd;
}
+int
+termsrv_listen_socket_new(const char *name, struct sockaddr_un *sa)
+{
+ char tmp[sizeof(sa->sun_path)];
+ struct sockaddr_un sa_tmp = { .sun_family = AF_UNIX };
+ int fd, probe, n, err;
+
+ if (termsrv_socket_path(name, sa->sun_path, sizeof(sa->sun_path)) < 0)
+ {
+ errno = EINVAL;
+ return -1;
+ }
+ /* the temporary name carries our pid: a file left behind by a dead
+ * daemon with the same name cannot be ours */
+ n = snprintf(tmp, sizeof(tmp), "%s.%ld.tmp", sa->sun_path, (long)getpid());
+ if (n < 0 || (size_t)n >= sizeof(tmp))
+ {
+ errno = ENAMETOOLONG;
+ return -1;
+ }
+ memcpy(sa_tmp.sun_path, tmp, (size_t)n + 1);
+
+ unlink(tmp);
+ fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
+ if (fd < 0)
+ return -1;
+ if (bind(fd, (struct sockaddr *)&sa_tmp, sizeof(sa_tmp)) < 0)
+ goto err;
+ if (listen(fd, 8) < 0)
+ goto err;
+ /* link() fails with EEXIST when the final path is taken, atomically:
+ * it lands only once the socket accepts, never between bind() and
+ * listen(), the window in which a peer would unlink it as stale */
+ for (;;)
+ {
+ if (link(tmp, sa->sun_path) == 0)
+ break;
+ if (errno != EEXIST)
+ goto err;
+ probe = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ if (probe < 0)
+ goto err;
+ if (connect(probe, (struct sockaddr *)sa, sizeof(*sa)) == 0)
+ {
+ close(probe);
+ errno = EEXIST;
+ goto err;
+ }
+ err = errno;
+ close(probe);
+ if (err == ECONNREFUSED)
+ {
+ /* left behind by a daemon that did not exit cleanly */
+ unlink(sa->sun_path);
+ continue;
+ }
+ errno = err;
+ goto err;
+ }
+ unlink(tmp);
+ return fd;
+
+err:
+ err = errno;
+ unlink(tmp);
+ close(fd);
+ errno = err;
+ return -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. */
@@ -581,12 +651,37 @@ termsrv_recv_attached(int fd, int *shm_fd_out,
if (n <= 0)
return -1;
- cm = CMSG_FIRSTHDR(&msg);
- if (cm && cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SCM_RIGHTS &&
- cm->cmsg_len == CMSG_LEN(sizeof(int)))
- memcpy(&shm_fd, CMSG_DATA(cm), sizeof(int));
- if (shm_fd < 0 || (msg.msg_flags & MSG_CTRUNC))
- goto err;
+ {
+ int n_fds = 0;
+
+ for (cm = CMSG_FIRSTHDR(&msg); cm; cm = CMSG_NXTHDR(&msg, cm))
+ {
+ unsigned char *data;
+ size_t len;
+
+ if (cm->cmsg_level != SOL_SOCKET || cm->cmsg_type != SCM_RIGHTS)
+ continue;
+ /* the protocol passes exactly one fd: keep the first, close
+ * the rest whatever becomes of the message */
+ data = ""
+ len = cm->cmsg_len - CMSG_LEN(0);
+ while (len >= sizeof(int))
+ {
+ int fd;
+
+ memcpy(&fd, data, sizeof(int));
+ data += sizeof(int);
+ len -= sizeof(int);
+ if (shm_fd < 0)
+ shm_fd = fd;
+ else
+ close(fd);
+ n_fds++;
+ }
+ }
+ if (shm_fd < 0 || n_fds != 1 || (msg.msg_flags & MSG_CTRUNC))
+ goto err;
+ }
if ((size_t)n < sizeof(msg_buf) &&
_recv_all(fd, (char *)&msg_buf + n, sizeof(msg_buf) - (size_t)n) < 0)
@@ -607,6 +702,7 @@ err:
#if defined(BINARY_TYTEST)
#include <assert.h>
+#include <dirent.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
@@ -800,6 +896,61 @@ tytest_termsrv_daemon_kill(void)
return 0;
}
+/* The listening socket's file appears at its final path only once it
+ * accepts, so a peer connecting right after never sees it as stale; a
+ * second listener on the same name is refused, a stale file is replaced. */
+int
+tytest_termsrv_listen_socket(void)
+{
+ char base[] = "/tmp/tytest-termsrv-listen-XXXXXX";
+ char dir[4096], parent[4096];
+ struct sockaddr_un sa = { .sun_family = AF_UNIX };
+ char *old_runtime = getenv("XDG_RUNTIME_DIR");
+ char *saved_runtime = old_runtime ? strdup(old_runtime) : NULL;
+ int fd, c, stale;
+
+ assert(mkdtemp(base));
+ setenv("XDG_RUNTIME_DIR", base, 1);
+ assert(termsrv_ensure_tymux_dir() == 0);
+
+ fd = termsrv_listen_socket_new("work", &sa);
+ assert(fd >= 0);
+ assert(!access(sa.sun_path, F_OK));
+ c = termsrv_connect("work");
+ assert(c >= 0);
+ close(c);
+
+ errno = 0;
+ assert(termsrv_listen_socket_new("work", &sa) < 0 && errno == EEXIST);
+
+ /* a socket file nobody listens on is stale and gets replaced */
+ close(fd);
+ unlink(sa.sun_path);
+ stale = socket(AF_UNIX, SOCK_STREAM, 0);
+ assert(stale >= 0);
+ assert(bind(stale, (struct sockaddr *)&sa, sizeof(sa)) == 0);
+ close(stale);
+ fd = termsrv_listen_socket_new("work", &sa);
+ assert(fd >= 0);
+ c = termsrv_connect("work");
+ assert(c >= 0);
+ close(c);
+ close(fd);
+ unlink(sa.sun_path);
+
+ assert(termsrv_tymux_dir(dir, sizeof(dir)) == 0);
+ snprintf(parent, sizeof(parent), "%s/terminology", base);
+ assert(rmdir(dir) == 0);
+ assert(rmdir(parent) == 0);
+ assert(rmdir(base) == 0);
+ if (saved_runtime)
+ setenv("XDG_RUNTIME_DIR", saved_runtime, 1);
+ else
+ unsetenv("XDG_RUNTIME_DIR");
+ free(saved_runtime);
+ return 0;
+}
+
int
tytest_termsrv_attach_mode(void)
{
@@ -1057,6 +1208,72 @@ tytest_termsrv_attached(void)
return 0;
}
+static int
+_fd_count(void)
+{
+ DIR *d = opendir("/proc/self/fd");
+ struct dirent *de;
+ int n = 0;
+
+ assert(d);
+ while ((de = readdir(d)))
+ n++;
+ closedir(d);
+ return n - 2; /* "." and ".." */
+}
+
+/* ATTACHED carrying more than the one fd the protocol passes: the
+ * message is refused and every fd it carried is closed, not left open
+ * until the process exits. */
+int
+tytest_termsrv_attached_extra_fds(void)
+{
+ TermSrvMsgHdr hdr = {
+ .type = TSRV_MSG_ATTACHED,
+ .size = sizeof(TermSrvMsgAttached),
+ };
+ TermSrvMsgAttached attached = { .cols = 80, .rows = 24, .shell_pid = -1 };
+ struct iovec iov[2] = {
+ { .iov_base = &hdr, .iov_len = sizeof(hdr) },
+ { .iov_base = &attached, .iov_len = sizeof(attached) },
+ };
+ union {
+ char buf[CMSG_SPACE(sizeof(int) * 2)];
+ struct cmsghdr align;
+ } control;
+ struct msghdr msg = {
+ .msg_iov = iov,
+ .msg_iovlen = 2,
+ .msg_control = control.buf,
+ .msg_controllen = sizeof(control.buf),
+ };
+ struct cmsghdr *cm;
+ int sv[2], pipefd[2], shm_fd = -1, before;
+ TermSrvMsgAttached out;
+
+ assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
+ assert(pipe(pipefd) == 0);
+ memset(&control, 0, sizeof(control));
+ cm = CMSG_FIRSTHDR(&msg);
+ cm->cmsg_level = SOL_SOCKET;
+ cm->cmsg_type = SCM_RIGHTS;
+ cm->cmsg_len = CMSG_LEN(sizeof(int) * 2);
+ memcpy(CMSG_DATA(cm), pipefd, sizeof(int) * 2);
+ assert(sendmsg(sv[0], &msg, 0) ==
+ (ssize_t)(sizeof(hdr) + sizeof(attached)));
+
+ before = _fd_count();
+ assert(termsrv_recv_attached(sv[1], &shm_fd, &out) < 0);
+ assert(shm_fd == -1);
+ assert(_fd_count() == before);
+
+ close(sv[0]);
+ close(sv[1]);
+ close(pipefd[0]);
+ close(pipefd[1]);
+ return 0;
+}
+
typedef struct
{
TermSrvShmHeader *shm;
diff --git a/src/bin/termsrv.h b/src/bin/termsrv.h
index f6dd6f3d..3dd8a37c 100644
--- a/src/bin/termsrv.h
+++ b/src/bin/termsrv.h
@@ -11,6 +11,7 @@
#include <stdint.h>
#include <stddef.h>
#include <stdatomic.h>
+#include <sys/un.h>
#include <Eina.h>
#define TERMSRV_SHM_MAGIC 0x544D5348u /* "TMSH" */
@@ -205,6 +206,14 @@ int termsrv_ensure_tymux_dir(void);
* removed. Returns the connected socket, or -1 with errno set. */
int termsrv_connect(const char *name);
+/* Create the listening socket of the tymux called name and fill sa with
+ * its final address, for unlink() on shutdown. The socket file appears at
+ * its final path only once listen() has been called, atomically, so a peer
+ * connecting to it never sees it bound but not listening and mistakes it
+ * for a stale socket to unlink. Returns the socket, or -1 with errno set:
+ * EEXIST when a daemon already listens on the name. */
+int termsrv_listen_socket_new(const char *name, struct sockaddr_un *sa);
+
/* Ask the tymuxd listening on sock, a socket termsrv_connect() returned,
* to end its session by sending it SIGTERM. Returns 0 on success, -1 on
* error with errno set, EPERM if the daemon belongs to another user. */
diff --git a/src/bin/tymux.c b/src/bin/tymux.c
index 1adff2be..ae78517e 100644
--- a/src/bin/tymux.c
+++ b/src/bin/tymux.c
@@ -56,6 +56,66 @@ static bool _raw_mode = false;
* handler only wakes the main loop through this self-pipe */
static int _winch_pipe_write_fd = -1;
+/* Output for the outer terminal is queued and written without blocking:
+ * a slow terminal must not stall the main loop, which also has to read the
+ * session's socket, or the daemon sees this client stop keeping up and
+ * drops it. */
+static Eina_Strbuf *_out_queue = NULL;
+static Ecore_Fd_Handler *_out_fdh = NULL;
+static Proxy *_proxy = NULL;
+/* past this the outer terminal is not consuming anything at all */
+#define OUT_QUEUE_MAX (8 << 20)
+
+static void _out_pump(void);
+
+static Eina_Bool
+_cb_out_writable(void *data EINA_UNUSED, Ecore_Fd_Handler *fdh EINA_UNUSED)
+{
+ _out_pump();
+ return ECORE_CALLBACK_RENEW;
+}
+
+static void
+_out_pump(void)
+{
+ size_t len = eina_strbuf_length_get(_out_queue);
+
+ if (len > 0)
+ {
+ ssize_t n = write(STDOUT_FILENO,
+ eina_strbuf_string_get(_out_queue), len);
+
+ if (n < 0)
+ {
+ if (errno == EINTR)
+ return;
+ if (errno != EAGAIN && errno != EWOULDBLOCK)
+ {
+ /* the outer terminal is gone */
+ if (_proxy)
+ _proxy->running = false;
+ return;
+ }
+ n = 0;
+ }
+ eina_strbuf_remove(_out_queue, 0, n);
+ len -= n;
+ }
+ if (len > 0)
+ {
+ if (!_out_fdh)
+ _out_fdh = ecore_main_fd_handler_add(STDOUT_FILENO,
+ ECORE_FD_WRITE,
+ _cb_out_writable, NULL,
+ NULL, NULL);
+ }
+ else if (_out_fdh)
+ {
+ ecore_main_fd_handler_del(_out_fdh);
+ _out_fdh = NULL;
+ }
+}
+
static void
_raw_mode_enter(void)
{
@@ -91,19 +151,10 @@ _cb_sigwinch(int sig __attribute__((unused)))
static void
_write_all(const char *buf, size_t len)
{
- while (len > 0)
- {
- ssize_t n = write(STDOUT_FILENO, buf, len);
-
- if (n < 0)
- {
- if (errno == EINTR)
- continue;
- return;
- }
- buf += n;
- len -= (size_t)n;
- }
+ eina_strbuf_append_length(_out_queue, buf, len);
+ if (eina_strbuf_length_get(_out_queue) > OUT_QUEUE_MAX && _proxy)
+ _proxy->running = false;
+ _out_pump();
}
static void
@@ -570,15 +621,21 @@ _proxy_run(int sock)
Proxy proxy = { .sock = sock, .running = true };
TermSrvMsgAttached attached;
void *shm;
- int shm_fd;
+ int shm_fd, out_flags;
struct timeval timeout = { .tv_sec = 5, .tv_usec = 0 };
+ _out_queue = eina_strbuf_new();
+ _proxy = &proxy;
+ out_flags = fcntl(STDOUT_FILENO, F_GETFL, 0);
+ if (out_flags >= 0)
+ fcntl(STDOUT_FILENO, F_SETFL, out_flags | O_NONBLOCK);
+
/* do not hang on a daemon that does not answer */
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
if (termsrv_recv_attached(sock, &shm_fd, &attached) < 0)
{
fprintf(stderr, "tymux: no valid answer from tymuxd\n");
- return 1;
+ goto err_early;
}
timeout.tv_sec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
@@ -587,7 +644,7 @@ _proxy_run(int sock)
if (shm == MAP_FAILED)
{
perror("tymux: mmap");
- return 1;
+ goto err_early;
}
proxy.shm = shm;
if (proxy.shm->magic != TERMSRV_SHM_MAGIC ||
@@ -595,7 +652,7 @@ _proxy_run(int sock)
{
fprintf(stderr, "tymux: tymuxd speaks another protocol version\n");
munmap(shm, TERMSRV_SHM_TOTAL_SIZE);
- return 1;
+ goto err_early;
}
if (pipe2(proxy.winch_pipe, O_CLOEXEC | O_NONBLOCK) < 0)
@@ -604,7 +661,7 @@ _proxy_run(int sock)
free(proxy.cur);
free(proxy.prev);
munmap(shm, TERMSRV_SHM_TOTAL_SIZE);
- return 1;
+ goto err_early;
}
_winch_pipe_write_fd = proxy.winch_pipe[1];
@@ -634,11 +691,16 @@ _proxy_run(int sock)
ecore_main_loop_begin();
_raw_mode_leave();
+ /* blocking again for the final write, so it cannot be cut short */
+ if (out_flags >= 0)
+ fcntl(STDOUT_FILENO, F_SETFL, out_flags);
_winch_handler_set(SIG_DFL);
_winch_pipe_write_fd = -1;
if (proxy.fdh_stdin) ecore_main_fd_handler_del(proxy.fdh_stdin);
if (proxy.fdh_sock) ecore_main_fd_handler_del(proxy.fdh_sock);
if (proxy.fdh_winch) ecore_main_fd_handler_del(proxy.fdh_winch);
+ if (_out_fdh) ecore_main_fd_handler_del(_out_fdh);
+ _out_fdh = NULL;
if (proxy.eh_exit) ecore_event_handler_del(proxy.eh_exit);
if (proxy.eh_hup) ecore_event_handler_del(proxy.eh_hup);
close(proxy.winch_pipe[0]);
@@ -648,7 +710,18 @@ _proxy_run(int sock)
free(proxy.prev);
termsrv_reader_clear(&proxy.reader);
munmap(shm, TERMSRV_SHM_TOTAL_SIZE);
+ eina_strbuf_free(_out_queue);
+ _out_queue = NULL;
+ _proxy = NULL;
return proxy.exit_code;
+
+err_early:
+ if (out_flags >= 0)
+ fcntl(STDOUT_FILENO, F_SETFL, out_flags);
+ eina_strbuf_free(_out_queue);
+ _out_queue = NULL;
+ _proxy = NULL;
+ return 1;
}
static int
diff --git a/src/bin/tymuxd.c b/src/bin/tymuxd.c
index 982eb1ad..274d316b 100644
--- a/src/bin/tymuxd.c
+++ b/src/bin/tymuxd.c
@@ -41,52 +41,6 @@ _cb_signal_exit(void *data EINA_UNUSED, int type EINA_UNUSED,
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;
-}
-
int
main(int argc, char **argv)
{
@@ -121,14 +75,15 @@ 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)
+ _listen_fd = termsrv_listen_socket_new(name, &sa);
+ if (_listen_fd < 0)
{
- ERR("socket path too long for '%s'", name);
+ if (errno == EEXIST)
+ fprintf(stderr, "tymuxd: '%s' is already running\n", name);
+ else
+ ERR("could not listen on '%s': %s", sa.sun_path, strerror(errno));
goto end;
}
- _listen_fd = _listen_socket_new(&sa, name);
- if (_listen_fd < 0)
- goto end;
setenv("TYMUX_NAME", name, 1);
_tymux = termd_tymux_new(name, NULL);
diff --git a/src/bin/tymuxd_stubs.c b/src/bin/tymuxd_stubs.c
index 31e0d2f7..eb483f8a 100644
--- a/src/bin/tymuxd_stubs.c
+++ b/src/bin/tymuxd_stubs.c
@@ -48,6 +48,14 @@ termio_textgrid_get(const Evas_Object *obj EINA_UNUSED)
return NULL;
}
+/* No window in a headless daemon: CSI 14;2 t reports 0x0. */
+void
+termio_window_size_get(const Evas_Object *obj EINA_UNUSED, int *w, int *h)
+{
+ if (w) *w = 0;
+ if (h) *h = 0;
+}
+
/* The parser only hands these to edje_object_color_class_get() and
* edje_object_color_class_set(), which the build of tymuxd renames to the
* functions below: any non-NULL pointer does. */
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 8147ce70..3e864bea 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -159,6 +159,7 @@ static struct {
{ "termsrv_socket_path", tytest_termsrv_socket_path},
{ "termsrv_tymux_dir_ensure", tytest_termsrv_tymux_dir_ensure},
{ "termsrv_daemon_kill", tytest_termsrv_daemon_kill},
+ { "termsrv_listen_socket", tytest_termsrv_listen_socket},
{ "termsrv_attach_mode", tytest_termsrv_attach_mode},
{ "termsrv_msg_round_trip", tytest_termsrv_msg_round_trip},
{ "termsrv_msg_invalid", tytest_termsrv_msg_invalid},
@@ -166,6 +167,7 @@ static struct {
{ "termsrv_msg_send_full", tytest_termsrv_msg_send_full},
{ "termsrv_shm_seqlock", tytest_termsrv_shm_seqlock},
{ "termsrv_attached", tytest_termsrv_attached},
+ { "termsrv_attached_extra_fds", tytest_termsrv_attached_extra_fds},
{ "termsrv_cell_layout", tytest_termsrv_cell_layout},
#endif
{ NULL, NULL},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index b08e6e9e..ab21211b 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -117,6 +117,7 @@ int tytest_termsrv_name(void);
int tytest_termsrv_socket_path(void);
int tytest_termsrv_tymux_dir_ensure(void);
int tytest_termsrv_daemon_kill(void);
+int tytest_termsrv_listen_socket(void);
int tytest_termsrv_attach_mode(void);
int tytest_termsrv_msg_round_trip(void);
int tytest_termsrv_msg_invalid(void);
@@ -124,6 +125,7 @@ int tytest_termsrv_msg_split(void);
int tytest_termsrv_msg_send_full(void);
int tytest_termsrv_shm_seqlock(void);
int tytest_termsrv_attached(void);
+int tytest_termsrv_attached_extra_fds(void);
int tytest_termsrv_cell_layout(void);
#endif
diff --git a/tests/tymux.py b/tests/tymux.py
index 382a670a..3ea5c37d 100644
--- a/tests/tymux.py
+++ b/tests/tymux.py
@@ -317,10 +317,14 @@ def main():
check(stuck.expect("$ "), "stuck: attached")
os.kill(stuck.pid, signal.SIGSTOP)
stopped.append(stuck)
- a.send("i=0; while [ $i -lt 3000 ]; do printf '\\033]2;%0500d\\007' $i;"
+ a.send("i=0; while [ $i -lt 6000 ]; do printf '\\033]2;%0500d\\007' $i;"
" i=$((i+1)); done; echo flood-$((9*9))\r")
check(a.expect("flood-81", timeout=30),
"a stopped client does not block the others")
+ # the daemon drops the stopped client once its queue has been past the
+ # soft limit for the grace period without any progress: wait that out
+ # before reviving it, or it drains and is never dropped
+ time.sleep(4)
os.kill(stuck.pid, signal.SIGCONT)
stopped.remove(stuck)
check(stuck.wait(10) is not None, "the stopped client was dropped")
@@ -352,6 +356,49 @@ def main():
a.send("exit\r")
check(a.wait() == 0, "exit after the stuck client test")
+ # input the shell never drains is capped: the client that pushes the
+ # pty's write buffer past 1 MiB is detached instead of growing it
+ a = Client(["new", NAME])
+ check(a.expect("$ "), "new for the input cap test")
+ # raw mode: in canonical mode the line discipline eats the flood, the
+ # pty write never blocks and the daemon's buffer never grows
+ a.send("stty raw -echo; sleep 30\r")
+ a.idle(0.5)
+ path = os.path.join(XDG, "terminology", "tymux", NAME + ".sock")
+ flooder = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+ flooder.connect(path)
+ flooder.send(struct.pack("=IIB", 1, 1, 1)) # ATTACH, shared
+ _, fds, _, _ = socket.recv_fds(flooder, 20, 1)
+ for fd in fds:
+ os.close(fd)
+ flooder.setblocking(False)
+ hdr = struct.pack("=II", 4, 65536) # INPUT, 64 KiB
+ chunk = b"x" * 65536
+ detached = False
+ for _ in range(40): # 2.5 MiB in all, past the 1 MiB cap
+ try:
+ flooder.send(hdr + chunk)
+ except (BrokenPipeError, ConnectionResetError):
+ detached = True
+ break
+ except BlockingIOError:
+ pass
+ if select.select([flooder], [], [], 0.2)[0]:
+ try:
+ if flooder.recv(65536) == b"":
+ detached = True
+ break
+ except ConnectionResetError:
+ detached = True
+ break
+ check(detached, "a client flooding input the shell never reads"
+ " is detached")
+ flooder.close()
+ check(len(daemons()) == 1, "tymuxd is still alive after the flood")
+ result = tymux("kill", NAME)
+ check(result.returncode == 0, "kill after the input cap test")
+ check(a.wait(10) is not None, "the primary leaves on kill")
+
# clients hanging up together are seen by tymuxd in one loop iteration.
# Reading a freed ecore fd handler only crashes once the memory is
# overwritten: have glibc fill freed memory, and have ecore free its
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.