From 3d960bfa95ff0a024dc72d528cae3b6146cb5714 Mon Sep 17 00:00:00 2001 From: Herwin Date: Tue, 10 Nov 2026 19:04:03 +0100 Subject: [PATCH] Run the server headless on Linux, add Docker images for it The server no longer stops when it has no console input on Linux: it reads its own dup of stdin (taken before OpenConsole(), which closes fd 0; Input() returns "" at EOF there), and SIGTERM/SIGINT stop it like "quit". Windows is unchanged. Docker/Compiler: PureBasic 6.41 on debian:trixie-slim (the archive is copied in by hand and git-ignored). Docker/Server: multi-stage, built with the C backend and the optimizer, run on debian:trixie-slim with libsodium and SQLite only, as user bakashrot; config /etc/bakashrot/server.ini, volume /var/lib/bakashrot, port 12345. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01RwFeVQCBfcPDfAPqX8z2TF --- CLAUDE.md | 8 +- Design/Release.md | 3 +- Docker/Compiler/.dockerignore | 3 + Docker/Compiler/.gitignore | 2 + Docker/Compiler/Dockerfile | 22 +++++ Docker/Server/Dockerfile | 65 +++++++++++++ Docker/Server/Dockerfile.dockerignore | 8 ++ Notes.md | 72 +++++++++++--- Server/BakaShrot_Server.pb | 132 +++++++++++++++++++++++++- 9 files changed, 293 insertions(+), 22 deletions(-) create mode 100644 Docker/Compiler/.dockerignore create mode 100644 Docker/Compiler/.gitignore create mode 100644 Docker/Compiler/Dockerfile create mode 100644 Docker/Server/Dockerfile create mode 100644 Docker/Server/Dockerfile.dockerignore diff --git a/CLAUDE.md b/CLAUDE.md index db95797..11036b9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -39,6 +39,9 @@ operations come before security hardening (`Notes.md`, "Threat model"). - `Release.ps1 [-Force]` builds the release with 6.40 into `Release\` and smoke-tests it (`-Force` replaces an existing folder of the same name). +- Linux: same sources and flags (`-t`, `-e`, `-so` for the DLL), with + `libsodium-dev`. `Docker/Compiler/` and `Docker/Server/` build the + compiler and server images, see their Dockerfiles. ## Test @@ -86,8 +89,9 @@ suite: they take a few minutes in all. `-File`. - **Bash tool heredocs collapse `\\`**, and PureBasic uses `\` for fields: write edit scripts to files, or use the Edit tool. -- **The real server needs a console** (it stops at once without input): - tests use `BenchServer.exe` and its stop file instead. +- **The real server needs a console on Windows** (it stops at once without + input): tests use `BenchServer.exe` and its stop file instead. On Linux + it runs on without one, SIGTERM/SIGINT stop it. ## Working rules diff --git a/Design/Release.md b/Design/Release.md index 24afbc6..e23b399 100644 --- a/Design/Release.md +++ b/Design/Release.md @@ -90,7 +90,8 @@ in `Tests/` already, to be pointed at the release instead. Needs a way to stop it without a console: a stop file like `BenchServer.exe`'s, or a local admin connection (`Notes.md`, console commands can't be scripted). Then check it stops within a few seconds, - with its last messages committed. + with its last messages committed. On Linux, SIGTERM does it (2026-11-10, + checked through `docker stop`). 5. **Refusals**: - an unknown public key, and a known one with the wrong private key: not authenticated; diff --git a/Docker/Compiler/.dockerignore b/Docker/Compiler/.dockerignore new file mode 100644 index 0000000..a28b0bd --- /dev/null +++ b/Docker/Compiler/.dockerignore @@ -0,0 +1,3 @@ +* +!Dockerfile +!*.tgz diff --git a/Docker/Compiler/.gitignore b/Docker/Compiler/.gitignore new file mode 100644 index 0000000..92c110c --- /dev/null +++ b/Docker/Compiler/.gitignore @@ -0,0 +1,2 @@ +# The PureBasic archive copied here to build the image: licensed, not shared. +*.tgz diff --git a/Docker/Compiler/Dockerfile b/Docker/Compiler/Dockerfile new file mode 100644 index 0000000..8e827ac --- /dev/null +++ b/Docker/Compiler/Dockerfile @@ -0,0 +1,22 @@ +# PureBasic compiler image, console programs only (no GUI libraries). +# +# PureBasic's Linux build needs glibc 2.38+, hence Debian 13 (glibc 2.41). +# The PureBasic archive isn't in git (it's licensed): copy it next to this +# file first, then: +# docker build -t purebasic:6.41 --build-arg PUREBASIC_ARCHIVE=PureBasic_Linux3_X64_6.41_xxxx.tgz Docker/Compiler +# docker run --rm -v "$PWD":/src -w /src purebasic:6.41 pbcompiler -t Server/BakaShrot_Server.pb -e Build/BakaShrot_Server +# Both backends are on the PATH: pbcompiler (asm) and pbcompilerc (C). + +FROM debian:trixie-slim + +# gcc links every program (and is the C backend), libssl-dev is for +# PureBasic's Network library. +RUN apt-get update \ + && apt-get install -y --no-install-recommends gcc libc6-dev libssl-dev \ + && rm -rf /var/lib/apt/lists/* + +ARG PUREBASIC_ARCHIVE +ADD ${PUREBASIC_ARCHIVE} /opt/ + +ENV PUREBASIC_HOME=/opt/purebasic +ENV PATH=/opt/purebasic/compilers:$PATH diff --git a/Docker/Server/Dockerfile b/Docker/Server/Dockerfile new file mode 100644 index 0000000..479d7f3 --- /dev/null +++ b/Docker/Server/Dockerfile @@ -0,0 +1,65 @@ +# BakaShrot server image. +# +# Built from the repository's root, with the compiler image (Docker/Compiler) +# already built, and the PB-MessagePack submodule checked out: +# git submodule update --init --recursive +# docker build -t bakashrot-server -f Docker/Server/Dockerfile . +# +# Run it with a config and a volume for the database: +# docker run -d --name bakashrot -p 12345:12345 \ +# -v ./server.ini:/etc/bakashrot/server.ini:ro \ +# -v bakashrot-data:/var/lib/bakashrot \ +# bakashrot-server +# In server.ini's [BakaShrot.Server]: Port = 12345, Host empty (every +# interface: 127.0.0.1 can't be reached from outside the container), and +# DatabasePath = /var/lib/bakashrot/server.sqlite (relative paths are from +# that folder too). +# +# `docker stop` stops it cleanly (SIGTERM). Console commands (names, vacuum, +# restart...): start it with -it, then `docker attach bakashrot`, and +# detach with Ctrl+P, Ctrl+Q. + + +# --- Build --------------------------------------------------------------------- + +ARG COMPILER_IMAGE=purebasic:6.41 +FROM ${COMPILER_IMAGE} AS build + +RUN apt-get update \ + && apt-get install -y --no-install-recommends libsodium-dev \ + && rm -rf /var/lib/apt/lists/* + +WORKDIR /src +# Everything Server/BakaShrot_Server.pb includes, and nothing else. +COPY Client/BakaShrot_ErrorCodes.pbi Client/ +COPY Commons Commons +COPY Includes Includes +COPY Server Server + +# The release build: C backend, optimized, thread-safe. +RUN mkdir /out && pbcompilerc --thread --optimizer Server/BakaShrot_Server.pb --executable /out/bakashrot-server + + +# --- Runtime ------------------------------------------------------------------- + +FROM debian:trixie-slim + +# libsodium for the cryptography, SQLite 3.38+ for the storage: the server +# loads libsqlite3.so.0 itself (Server/Storage/Storage.pbi). +RUN apt-get update \ + && apt-get install -y --no-install-recommends libsodium23 libsqlite3-0 \ + && rm -rf /var/lib/apt/lists/* + +RUN useradd --system --home-dir /var/lib/bakashrot --shell /usr/sbin/nologin bakashrot \ + && mkdir -p /etc/bakashrot /var/lib/bakashrot \ + && chown bakashrot:bakashrot /var/lib/bakashrot + +COPY --from=build /out/bakashrot-server /usr/local/bin/bakashrot-server + +USER bakashrot +WORKDIR /var/lib/bakashrot +VOLUME /var/lib/bakashrot +EXPOSE 12345 + +ENTRYPOINT ["/usr/local/bin/bakashrot-server"] +CMD ["/etc/bakashrot/server.ini"] diff --git a/Docker/Server/Dockerfile.dockerignore b/Docker/Server/Dockerfile.dockerignore new file mode 100644 index 0000000..d9396e1 --- /dev/null +++ b/Docker/Server/Dockerfile.dockerignore @@ -0,0 +1,8 @@ +# Only what the server is compiled from: Build/ alone can hold gigabytes of +# test databases, and private keys. +* +!Client/BakaShrot_ErrorCodes.pbi +!Commons +!Includes +!Server +**/.git diff --git a/Notes.md b/Notes.md index e18f57c..53ce6d4 100644 --- a/Notes.md +++ b/Notes.md @@ -14,19 +14,24 @@ Where things stand and what's next, as of 2026-11-09. A new session: read | `Design/Suggestions.md` | Improvements not done yet: pipelined pushes, faster replication, smaller ones. | | `Design/Release.md` | The release smoke test, what's missing for 1.0.0, the (fixed) silent query failure, notes for CI/CD. | -## Right now (2026-11-09) +## Right now (2026-11-10) -- **Nothing since `df61ef9` is committed**, on `timestamps-retention`: the - tool renames, protocols 5 and 6 (node identities, error codes), names-only - configs, `Synchronous`, schema v8 (`browse`), `Examples/`, - `Restart-Test.ps1`. Every suite passes on it (8, see `CLAUDE.md`), it - compiles with 5.73 and 6.40, the push bench gives ~17,000 msg/s for 4 - servers with `Synchronous = FULL`. +- **Everything is committed**, on `timestamps-retention`: protocol 6 and + the rest (`488ffe4`: the tool renames, protocols 5 and 6, node + identities, error codes, names-only configs, `Synchronous`, schema v8 + (`browse`), `Examples/`, `Restart-Test.ps1`), the Linux fixes + (`5270f25`), then the server without a console on Linux and `Docker/`. + Every suite passes on it (8, see `CLAUDE.md`), it compiles with 5.73 and + 6.40, the push bench gives ~17,000 msg/s for 4 servers with + `Synchronous = FULL`. - **`Release\20261109-df61ef9-dirty\` is out of date** (before `Synchronous`, `browse` and the error codes): commit, then `Release.ps1` again. +- **On Linux** (2026-11-10, `Done: Linux, Docker images`): every suite + passes, the server runs without a console (SIGTERM/SIGINT), `Docker/` + builds it into an image. - **Suggested next** (not decided by the owner): the server without a - console (item 1), then whatever the real-world test shows. The run on a + console on Windows (item 1), then whatever the real-world test shows. The run on a clean Windows is the owner's, not an item here. ## Where things stand @@ -324,8 +329,8 @@ As judged for the threat model below (internal tool, trusted machines). process loses nothing committed). Often fine internally, to decide. - Client: one connection per process, `--follow` polls, no reconnect. - Tests run by hand, on fixed ports, no runner, no CI. -- Linux untested; keys in plain-text inis (file permissions are enough - here). +- Linux tested, Docker images (2026-11-10); keys in plain-text inis (file + permissions are enough here). **Bad** - The server can't run unattended: it needs a console, stops at once @@ -644,6 +649,37 @@ still say the old names where they tell what was done then. double-start guard, the README's Python snippet. `Start-Server.ps1 -Here` wasn't (it needs an interactive console). +## Done: Linux, Docker images (2026-11-10) + +- **Every suite passes on Linux** (Ubuntu, x64) with 6.41's asm and C + backends: `Tests/*.ps1` through PowerShell 7, once `-WindowStyle Hidden` + is dropped (not supported there), the binaries named as on Windows + (`BakaShrot_Client.dll` is the `.so`), `-Build` given. 5.73 compiles and + passes the rest, but can't restart a server on the same port within + ~60 s (its listener doesn't set `SO_REUSEADDR`): Names, Restart and + AttachmentEdges. +- **Fixed on the way:** SQLite on Linux is the system's `libsqlite3.so.0` + (`#BakaShrot_Storage_SqliteLibrary`), so the query error check and the + writer's prepared statements work there too; `BakaShrot_SendPacket()` + retries when the send buffer is full (`EAGAIN`, it aborted clients + reading big answers); four tests that relied on Windows timing. +- **The server runs without a console on Linux** (item 1, there): SIGTERM + and SIGINT stop it like `quit`, and with no console input it runs on + until one comes. `Input()` can't do it: on Linux it returns `""` (quit) + at the end of the input, and PureBasic's console moves descriptor 0 + elsewhere, so `ReadCommand()` reads a `dup` of it taken before + `OpenConsole()`, `poll()`ing 250 ms at a time. Checked with no input, + piped commands (CRLF too), a terminal, `quit`, both signals, on 5.73 and + 6.41. +- **`Docker/`**: `Compiler/` (PureBasic 6.41 on `debian:trixie-slim`, the + archive copied in by hand, never committed) and `Server/` (multi-stage: + built with the C backend and the optimizer, run on `debian:trixie-slim` + with libsodium and SQLite only, as user `bakashrot`). Config + `/etc/bakashrot/server.ini`, data volume `/var/lib/bakashrot`, port 12345. + Checked: pushes, reads and queries through the port, `docker stop` clean + (~0.3 s, exit 0), data kept over a restart, commands with `-i`. Alpine was + tried: PureBasic needs glibc 2.38+. + ## Threat model (2026-11-08) BakaShrot is an **internal tool, on trusted machines and a trusted @@ -669,6 +705,8 @@ network**, not exposed to the internet. So: no clean stop from a script (tests force-kill it). Fix: a stop file like `BenchServer.exe`'s, and/or a local admin connection, then a way to run it as a service. (`Design/Release.md` smoke test #4) + Done on Linux (2026-11-10): SIGTERM/SIGINT stop it, it runs on without + console input (`Done: Linux, Docker images`). Windows still needs it. 2. **The 1.0.0 smoke test, what's left:** replication between two servers built by `Release.ps1` (the suites use `BenchServer.exe`, the same server code). Done: the release's own smoke test and the example from its zip @@ -721,7 +759,7 @@ network**, not exposed to the internet. So: two threads failing on their own connections 20,000 times each, never once the other's message, with 5.73 and 6.40 (`/THREAD`). `sqlite3_errmsg16()` on the connection gives the same messages, kept as - is. Linux untested (item 18). + is. Linux: not checked the same way, its suites pass (2026-11-10). 9. **Limits:** the sync state must fit in one packet (65536 topic/node pairs); `ReplicatingTopicIds` ranges are expanded to every id they cover. (`Design/ReplicationIssues.md` #7) @@ -764,8 +802,8 @@ network**, not exposed to the internet. So: 17. **Review.md rules** not checked against the newer code (`.i` in networking structures, explicit enumeration values), see below. The file itself says to ignore it for now. -18. **Linux is untested** (libsodium via `-lsodium`, the query error check - falls back to `DatabaseError()`). +18. ~~**Linux is untested**~~ Done (2026-11-10): every suite passes, see + `Done: Linux, Docker images`. 19. **Console commands can't be scripted** (see 1). ### Done recently (details in the sections above and `Design/`) @@ -792,6 +830,8 @@ network**, not exposed to the internet. So: acknowledged lost (`Restart-Test.ps1`, 2026-11-09). - Protocol 6: error codes everywhere, at-least-once delivery accepted (2026-11-09). +- Linux: every suite passes, the server runs without a console there, + `Docker/` images for the compiler and the server (2026-11-10). ## To check against Review.md @@ -812,8 +852,10 @@ network**, not exposed to the internet. So: - **Test the server's console commands** with a small driver that includes `Server/Root.pbi` and calls `BakaShrot_Root_*` directly. Piping commands into the server doesn't work. -- **With no console input,** `Input()` returns `Chr(4)` on every call, and the - server stops on it. +- **With no console input,** `Input()` returns `Chr(4)` on every call on + Windows, and the server stops on it. On Linux it returns `""`, and the + server reads its commands itself (`ReadCommand()`): piping commands into + it works there. - **Don't trust timestamps** from PowerShell's `Register-ObjectEvent`: lines queue up during `Start-Sleep`. PureBasic's console output into a pipe is also buffered until exit. Trust the order, not the times. diff --git a/Server/BakaShrot_Server.pb b/Server/BakaShrot_Server.pb index 9afaef7..9722bf6 100644 --- a/Server/BakaShrot_Server.pb +++ b/Server/BakaShrot_Server.pb @@ -16,6 +16,10 @@ ; See Root.pbi for how the workers are stopped. ; Exits with 0 on success, 1 on failure. ; +; Windows: it stops once there's no more console input (no console, closed +; standard input). Elsewhere, it runs on without one (a container, a +; service) and SIGTERM or SIGINT stops it the same way as "quit". +; ; Compile as a thread-safe console program. ; ============================================================================ @@ -32,11 +36,113 @@ Procedure PrintCommands() EndProcedure +CompilerIf #PB_Compiler_OS <> #PB_OS_Windows + ; Same numbers on Linux and macOS. + #BakaShrot_Server_SigInt = 2 + #BakaShrot_Server_SigTerm = 15 + #BakaShrot_Server_PollIn = $0001 + #BakaShrot_Server_FDupFd = 0 + + ; struct pollfd + Structure BakaShrot_Server_PollFd + Fd.l + Events.w + REvents.w + EndStructure + + ImportC "" + BakaShrot_Server_Signal.i(Signal.l, *Handler) As "signal" + BakaShrot_Server_Poll.l(*Fds, Count.i, TimeoutMs.l) As "poll" + BakaShrot_Server_Read.i(Fd.l, *Buffer, Size.i) As "read" + ; dup() itself can't be imported: fasm reserves the name. + BakaShrot_Server_Fcntl.l(Fd.l, Command.l, Argument.i) As "fcntl" + EndImport + + ; Longest command line kept, the rest of a longer one is dropped. + #BakaShrot_Server_MaxCommandSize = 256 + + ; The signal that asked to stop, 0 until then. Only ever set by the + ; handler (all a handler may safely do), read by ReadCommand(). + Global BakaShrot_Server_StopSignal.l = 0 + + ; #True once standard input is closed: commands can't come any more. + Global BakaShrot_Server_NoInput.i = #False + + ; Standard input, taken before OpenConsole(): it moves descriptor 0 + ; elsewhere and closes it (then reused by the next file opened). + Global BakaShrot_Server_InputFd.l = -1 + + ProcedureC BakaShrot_Server_OnSignal(Signal.l) + BakaShrot_Server_StopSignal = Signal + EndProcedure +CompilerEndIf + + +; Waits for the next command, Chr(4) once there's no more input. +; Windows: Input(). Elsewhere, Input() returns "" (quit) at the end of the +; input and blocks until a whole line came, so standard input is read here +; instead: never blocking for more than 250 ms without checking for a stop +; signal (it then returns "", see BakaShrot_Server_StopSignal). +Procedure.s ReadCommand() + CompilerIf #PB_Compiler_OS = #PB_OS_Windows + ProcedureReturn Input() + CompilerElse + Protected Fd.BakaShrot_Server_PollFd, Byte.a, BytesRead.i, Size.i + Protected Dim Chars.a(#BakaShrot_Server_MaxCommandSize) + + Repeat + If BakaShrot_Server_StopSignal + ProcedureReturn "" + EndIf + If BakaShrot_Server_NoInput + Delay(250) + Continue + EndIf + If BakaShrot_Server_InputFd < 0 + ProcedureReturn Chr(4) + EndIf + + Fd\Fd = BakaShrot_Server_InputFd + Fd\Events = #BakaShrot_Server_PollIn + Fd\REvents = 0 + If BakaShrot_Server_Poll(@Fd, 1, 250) <= 0 + Continue + EndIf + + ; Readable, or closed: one byte, so nothing is read past the line. + BytesRead = BakaShrot_Server_Read(BakaShrot_Server_InputFd, @Byte, 1) + If BytesRead = 0 Or (BytesRead < 0 And Not BakaShrot_Server_StopSignal) + ProcedureReturn Chr(4) + EndIf + If BytesRead = 1 + If Byte = 10 + ProcedureReturn PeekS(@Chars(0), Size, #PB_UTF8 | #PB_ByteLength) + EndIf + ; CR of a CRLF skipped. + If Byte <> 13 And Size < #BakaShrot_Server_MaxCommandSize + Chars(Size) = Byte + Size + 1 + EndIf + EndIf + ForEver + CompilerEndIf +EndProcedure + + Define Root.BakaShrot_Root Define Command.s, ExitCode.i +CompilerIf #PB_Compiler_OS <> #PB_OS_Windows + BakaShrot_Server_InputFd = BakaShrot_Server_Fcntl(0, #BakaShrot_Server_FDupFd, 0) +CompilerEndIf + OpenConsole("BakaShrot Server") +CompilerIf #PB_Compiler_OS <> #PB_OS_Windows + BakaShrot_Server_Signal(#BakaShrot_Server_SigTerm, @BakaShrot_Server_OnSignal()) + BakaShrot_Server_Signal(#BakaShrot_Server_SigInt, @BakaShrot_Server_OnSignal()) +CompilerEndIf + Root\ConfigPath = #BakaShrot_Server_DefaultConfigPath If CountProgramParameters() > 0 Root\ConfigPath = ProgramParameter(0) @@ -62,13 +168,31 @@ PrintCommands() ExitCode = 0 Repeat - Command = LCase(Trim(Input())) + Command = LCase(Trim(ReadCommand())) + + CompilerIf #PB_Compiler_OS <> #PB_OS_Windows + If BakaShrot_Server_StopSignal + If BakaShrot_Server_StopSignal = #BakaShrot_Server_SigTerm + PrintN("Got SIGTERM, stopping") + Else + PrintN("Got SIGINT, stopping") + EndIf + Break + EndIf + CompilerEndIf ; Once there's no more input (no console, closed standard input), every - ; Input() returns Chr(4) right away. Without this, it would loop forever. + ; ReadCommand() returns Chr(4) right away. Without this, it would loop + ; forever. If FindString(Command, Chr(4)) - PrintN("No more input, stopping") - Break + CompilerIf #PB_Compiler_OS = #PB_OS_Windows + PrintN("No more input, stopping") + Break + CompilerElse + PrintN("No console input, running until SIGTERM or SIGINT") + BakaShrot_Server_NoInput = #True + Continue + CompilerEndIf EndIf Select Command