aboutsummaryrefslogtreecommitdiffstats
path: root/src/server.c
diff options
context:
space:
mode:
authorPeter Geissler <peter@haxx.in>2026-08-28 00:11:24 +0200
committerPeter Geissler <peter@haxx.in>2026-08-28 00:23:19 +0200
commitd1b5a64c3855b07074b3c958739df4e16f6084a5 (patch)
tree10579e84bd972661738f93fdd8c221674f09b857 /src/server.c
parentmacos zip: carry the same paperwork as the linux tarballs (diff)
downloadslosh-d1b5a64c3855b07074b3c958739df4e16f6084a5.tar.gz
slosh-d1b5a64c3855b07074b3c958739df4e16f6084a5.tar.xz
slosh-d1b5a64c3855b07074b3c958739df4e16f6084a5.zip
windows: a native port, on ConPTY and AF_UNIX
slosh builds and runs on Windows 10/11, x86-64 and ARM64, as one exe with no runtime dependency beyond the OS: panes, splits, tabs, detachable sessions, the config file and its live reload, the control socket, the mouse. The port is a compatibility layer rather than a fork. compat/win holds stub <poll.h>, <sys/socket.h>, <termios.h> and friends that redirect to include/compat_win.h, which is force-included ahead of every translation unit -- so the existing sources keep their POSIX includes and their POSIX call sites, and the platform differences are answered in one place. Two decisions carry most of it. Everything the event loop waits on is a socket. On POSIX a pty master, an inotify descriptor and a unix socket are all file descriptors, so one poll() covers them; Windows has no such union, and WSAPoll waits on SOCKETs and nothing else. Rather than rewrite server.c's loop around WaitForMultipleObjects -- which caps at 64 objects and cannot express POLLOUT -- each non-socket source gets a pump thread that copies it into one end of a loopback socketpair. The loop is then the POSIX one unchanged, and only what sits behind the descriptor differs. Socket descriptors are tagged rather than guessed: a wrapped SOCKET comes back as SL_SOCK_BASE + slot, so read, write, close and poll can tell a socket from a CRT descriptor with certainty instead of inferring it from the numeric range. That is why socket() and accept() are shimmed too -- an untagged descriptor escaping into the loop is the one failure mode this scheme has, and it is worth closing completely. Three things bit hard enough to be worth naming. A pseudoconsole does not stop the child inheriting your stdio. CreateProcess copies the parent's standard handles into the child when STARTF_USESTDHANDLES is absent, and those win. With slosh's own stdout redirected -- a pipe under ssh, a file under --script, a log under the daemon -- the shell wrote its banner into slosh's output instead of into the pane, and the pane had a correct title (ConPTY emits that itself) over an empty body. The three handles are cleared for the duration of the call, leaving the child nothing to inherit. A ConPTY never reports EOF. The output pipe belongs to the pseudoconsole, not to the child, so it stays open after the child exits and pane_pump()'s read() -- the entire mechanism by which a pane learns its program is gone -- never returns 0. A pane whose shell had been told `exit` sat there looking alive forever. So the exit is waited for on the process handle, and ClosePseudoConsole is what then produces the EOF. That third thread is also why the per-pty state is reference counted: a pump blocked on a dead pipe must not be waited for, or pty_close would hang the session on the one program that misbehaves, so whoever finishes last frees. cmd.exe does not use CommandLineToArgvW's quoting. A literal quote is \" to an ordinary program and a backslash-then-end-of-quoted-section to cmd, so building `cmd /c notepad "C:\...\config.kdl"` the first way handed the editor \"C:\...\config.kdl\", backslashes and all, and notepad answered "Not a valid file name" about a path that existed. cmd /c now gets the rest of the line verbatim -- the caller composed a complete command with its own quoting, exactly as it does for sh -c. Paths are mixed, and only some programs mind. slosh joins with '/' while the environment supplies '\', so the config landed at C:\Users\you/.config/slosh/config.kdl. The CRT opens that without complaint, which is why --check reported it fine, but a GUI file dialog rejects it outright. Anything handed to another program now goes through sl_path_native() first; the internal representation is left alone. The rest is small. Panes run %ComSpec%, sessions live in %LOCALAPPDATA%\slosh rather than $XDG_RUNTIME_DIR because a session socket must not follow a roaming profile to another host, there is no SIGWINCH so a resize is noticed by comparing the console size on a bounded poll, no fork so the daemon is a fresh detached process that reconstructs its arguments on the command line, and no process group on a pty so the foreground program is the youngest descendant of the pane's shell. C-a e prefers whichever console editor is actually installed -- notepad is a GUI program, so a pane running it draws nothing and looks broken -- and says so when it has to fall back. make -f Makefile.windows ARCH=x86_64 (or aarch64) cross-compiles from a mac or Linux, and builds natively on the box with zig on PATH.
Diffstat (limited to 'src/server.c')
-rw-r--r--src/server.c73
1 files changed, 68 insertions, 5 deletions
diff --git a/src/server.c b/src/server.c
index 3a7a366..3a3669b 100644
--- a/src/server.c
+++ b/src/server.c
@@ -112,6 +112,11 @@ static void conn_close(server_t *s, size_t i) {
* the peer is gone, or it is so far behind that keeping up is not the word for
* what we would be doing. EAGAIN is not a failure: it is the socket saying to
* come back when poll() says so. */
+#ifdef _WIN32
+/* There is no SIGPIPE to suppress, so there is nothing for MSG_NOSIGNAL to
+ * ask for: a send to a closed socket already just returns an error. */
+#define MSG_NOSIGNAL 0
+#endif
static bool conn_flush(conn_t *c) {
while (c->out_off < c->out_len) {
ssize_t w =
@@ -277,7 +282,10 @@ typedef struct {
static void watch_config(watchset_t *w) {
if (w->fd < 0) return;
-#ifdef __APPLE__
+#ifdef _WIN32
+ /* The watcher owns its directory handles and re-arms them itself, so there
+ * is no per-file descriptor to drop here. */
+#elif defined(__APPLE__)
/* kqueue drops a vnode filter when its fd closes, so closing is the unwatch. */
for (size_t i = 0; i < w->nwds; i++) close(w->wds[i]);
#else
@@ -329,6 +337,11 @@ static void watch_config(watchset_t *w) {
else
close(vfd);
}
+#elif defined(_WIN32)
+ /* Watch the directory and let the debounced re-read decide, which is the
+ * same conclusion the kqueue branch reaches for the same reason: an editor
+ * that saves by rename would slip out from under a watch on the file. */
+ sl_watch_add(w->fd, dir);
#else
int wd =
inotify_add_watch(w->fd, dir, IN_CLOSE_WRITE | IN_MOVED_TO | IN_CREATE);
@@ -346,7 +359,9 @@ static void watch_config(watchset_t *w) {
}
}
-#ifndef __APPLE__
+/* Only inotify reports which file changed; the kqueue and Windows watchers
+ * both re-read and let the debounce decide. */
+#if !defined(__APPLE__) && !defined(_WIN32)
static bool watch_hit(const watchset_t *w, const char *name) {
for (size_t i = 0; i < w->nnames; i++)
if (strcmp(w->names[i], name) == 0) return true;
@@ -359,7 +374,9 @@ static bool watch_hit(const watchset_t *w, const char *name) {
* debounces, because one save is not always one event. */
static bool watch_drain(watchset_t *w) {
bool touched = false;
-#ifdef __APPLE__
+#ifdef _WIN32
+ touched = sl_watch_drain(w->fd) != 0;
+#elif defined(__APPLE__)
struct kevent evs[16];
struct timespec zero = {0, 0};
for (;;) {
@@ -411,7 +428,9 @@ void session_env(const char *name) {
else
unsetenv("SLOSH_SESSION");
char self[512];
-#ifdef __APPLE__
+#ifdef _WIN32
+ if (sl_self_exe(self, sizeof self)) setenv("SLOSH_BIN", self, 1);
+#elif defined(__APPLE__)
/* Darwin's answer to /proc/self/exe. What it hands back can still contain
* symlinks and .., so realpath() finishes the job. */
char raw[512];
@@ -439,6 +458,12 @@ int server_run(const char *name, const char *const argv[], uint16_t cols,
session_env(name);
+#ifdef _WIN32
+ /* Windows delivers Ctrl-C and shutdown through a console control handler
+ * rather than as signals, and has no SIGPIPE or SIGCHLD to arrange for. */
+ signal(SIGINT, on_sig);
+ signal(SIGTERM, on_sig);
+#else
struct sigaction sa = {.sa_handler = on_sig};
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
@@ -451,6 +476,7 @@ int server_run(const char *name, const char *const argv[], uint16_t cols,
* looked at. SIGCHLD's default action is to be discarded, so nothing is
* interrupted and the only difference is that a status waits to be read. */
signal(SIGCHLD, SIG_DFL);
+#endif
server_t s = {0};
s.app = app_new(argv, cols, rows);
@@ -491,7 +517,9 @@ int server_run(const char *name, const char *const argv[], uint16_t cols,
int inofd = -1;
watchset_t watches = {.fd = -1};
if (watch) {
-#ifdef __APPLE__
+#ifdef _WIN32
+ inofd = sl_watch_init();
+#elif defined(__APPLE__)
inofd = kqueue();
if (inofd >= 0) fcntl(inofd, F_SETFD, FD_CLOEXEC);
#else
@@ -745,6 +773,40 @@ int server_spawn(const char *name, const char *const argv[], uint16_t cols,
if (session_socket_path(name, path, sizeof path) != 0) return -1;
session_log_path(name, logp, sizeof logp);
+#ifdef _WIN32
+ /* No fork: the daemon is started as a fresh detached process rather than as
+ * a copy of this one, reconstructing on its command line what the forked
+ * child would have inherited. `--server` is the flag main.c uses to run
+ * server_run() directly instead of spawning again. */
+ {
+ char self[512];
+ if (!sl_self_exe(self, sizeof self)) return -1;
+ const char *av[16];
+ char colbuf[16], rowbuf[16];
+ snprintf(colbuf, sizeof colbuf, "%u", (unsigned)cols);
+ snprintf(rowbuf, sizeof rowbuf, "%u", (unsigned)rows);
+ size_t n = 0;
+ av[n++] = self;
+ av[n++] = "--server";
+ av[n++] = "-s";
+ av[n++] = name;
+ av[n++] = "--cols";
+ av[n++] = colbuf;
+ av[n++] = "--rows";
+ av[n++] = rowbuf;
+ if (layout) {
+ av[n++] = "--layout";
+ av[n++] = layout;
+ }
+ if (!watch) av[n++] = "--no-reload";
+ if (argv && argv[0]) {
+ av[n++] = "--";
+ for (int i = 0; argv[i] && n < 15; i++) av[n++] = argv[i];
+ }
+ av[n] = NULL;
+ if (sl_spawn_detached(av, logp) < 0) return -1;
+ }
+#else
pid_t pid = fork();
if (pid < 0) return -1;
if (pid == 0) {
@@ -760,6 +822,7 @@ int server_spawn(const char *name, const char *const argv[], uint16_t cols,
_exit(server_run(name, argv, cols, rows, layout, watch));
}
waitpid(pid, NULL, 0); /* the intermediate exits immediately */
+#endif
/* Wait for the socket to accept, rather than sleeping and hoping. */
for (int i = 0; i < 400; i++) {