1
0
mirror of https://github.com/aziascreations/BakaShrot.git synced 2026-11-23 20:58:46 +01:00

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RwFeVQCBfcPDfAPqX8z2TF
This commit is contained in:
2026-11-10 19:04:03 +01:00
parent 5270f25ac1
commit 3d960bfa95
9 changed files with 293 additions and 22 deletions
+6 -2
View File
@@ -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 - `Release.ps1 [-Force]` builds the release with 6.40 into `Release\` and
smoke-tests it (`-Force` replaces an existing folder of the same name). 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 ## Test
@@ -86,8 +89,9 @@ suite: they take a few minutes in all.
`-File`. `-File`.
- **Bash tool heredocs collapse `\\`**, and PureBasic uses `\` for fields: - **Bash tool heredocs collapse `\\`**, and PureBasic uses `\` for fields:
write edit scripts to files, or use the Edit tool. write edit scripts to files, or use the Edit tool.
- **The real server needs a console** (it stops at once without input): - **The real server needs a console on Windows** (it stops at once without
tests use `BenchServer.exe` and its stop file instead. input): tests use `BenchServer.exe` and its stop file instead. On Linux
it runs on without one, SIGTERM/SIGINT stop it.
## Working rules ## Working rules
+2 -1
View File
@@ -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 Needs a way to stop it without a console: a stop file like
`BenchServer.exe`'s, or a local admin connection (`Notes.md`, console `BenchServer.exe`'s, or a local admin connection (`Notes.md`, console
commands can't be scripted). Then check it stops within a few seconds, 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**: 5. **Refusals**:
- an unknown public key, and a known one with the wrong private key: - an unknown public key, and a known one with the wrong private key:
not authenticated; not authenticated;
+3
View File
@@ -0,0 +1,3 @@
*
!Dockerfile
!*.tgz
+2
View File
@@ -0,0 +1,2 @@
# The PureBasic archive copied here to build the image: licensed, not shared.
*.tgz
+22
View File
@@ -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
+65
View File
@@ -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"]
+8
View File
@@ -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
+57 -15
View File
@@ -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/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. | | `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 - **Everything is committed**, on `timestamps-retention`: protocol 6 and
tool renames, protocols 5 and 6 (node identities, error codes), names-only the rest (`488ffe4`: the tool renames, protocols 5 and 6, node
configs, `Synchronous`, schema v8 (`browse`), `Examples/`, identities, error codes, names-only configs, `Synchronous`, schema v8
`Restart-Test.ps1`. Every suite passes on it (8, see `CLAUDE.md`), it (`browse`), `Examples/`, `Restart-Test.ps1`), the Linux fixes
compiles with 5.73 and 6.40, the push bench gives ~17,000 msg/s for 4 (`5270f25`), then the server without a console on Linux and `Docker/`.
servers with `Synchronous = FULL`. 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 - **`Release\20261109-df61ef9-dirty\` is out of date** (before
`Synchronous`, `browse` and the error codes): commit, then `Synchronous`, `browse` and the error codes): commit, then
`Release.ps1` again. `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 - **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. clean Windows is the owner's, not an item here.
## Where things stand ## 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. process loses nothing committed). Often fine internally, to decide.
- Client: one connection per process, `--follow` polls, no reconnect. - Client: one connection per process, `--follow` polls, no reconnect.
- Tests run by hand, on fixed ports, no runner, no CI. - Tests run by hand, on fixed ports, no runner, no CI.
- Linux untested; keys in plain-text inis (file permissions are enough - Linux tested, Docker images (2026-11-10); keys in plain-text inis (file
here). permissions are enough here).
**Bad** **Bad**
- The server can't run unattended: it needs a console, stops at once - 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` double-start guard, the README's Python snippet. `Start-Server.ps1 -Here`
wasn't (it needs an interactive console). 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) ## Threat model (2026-11-08)
BakaShrot is an **internal tool, on trusted machines and a trusted 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 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 `BenchServer.exe`'s, and/or a local admin connection, then a way to run
it as a service. (`Design/Release.md` smoke test #4) 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 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 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 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 two threads failing on their own connections 20,000 times each, never
once the other's message, with 5.73 and 6.40 (`/THREAD`). once the other's message, with 5.73 and 6.40 (`/THREAD`).
`sqlite3_errmsg16()` on the connection gives the same messages, kept as `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 9. **Limits:** the sync state must fit in one packet (65536 topic/node
pairs); `ReplicatingTopicIds` ranges are expanded to every id they cover. pairs); `ReplicatingTopicIds` ranges are expanded to every id they cover.
(`Design/ReplicationIssues.md` #7) (`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 17. **Review.md rules** not checked against the newer code (`.i` in
networking structures, explicit enumeration values), see below. The networking structures, explicit enumeration values), see below. The
file itself says to ignore it for now. file itself says to ignore it for now.
18. **Linux is untested** (libsodium via `-lsodium`, the query error check 18. ~~**Linux is untested**~~ Done (2026-11-10): every suite passes, see
falls back to `DatabaseError()`). `Done: Linux, Docker images`.
19. **Console commands can't be scripted** (see 1). 19. **Console commands can't be scripted** (see 1).
### Done recently (details in the sections above and `Design/`) ### 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). acknowledged lost (`Restart-Test.ps1`, 2026-11-09).
- Protocol 6: error codes everywhere, at-least-once delivery accepted - Protocol 6: error codes everywhere, at-least-once delivery accepted
(2026-11-09). (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 ## 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 - **Test the server's console commands** with a small driver that includes
`Server/Root.pbi` and calls `BakaShrot_Root_*` directly. Piping commands `Server/Root.pbi` and calls `BakaShrot_Root_*` directly. Piping commands
into the server doesn't work. into the server doesn't work.
- **With no console input,** `Input()` returns `Chr(4)` on every call, and the - **With no console input,** `Input()` returns `Chr(4)` on every call on
server stops on it. 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 - **Don't trust timestamps** from PowerShell's `Register-ObjectEvent`: lines
queue up during `Start-Sleep`. PureBasic's console output into a pipe is queue up during `Start-Sleep`. PureBasic's console output into a pipe is
also buffered until exit. Trust the order, not the times. also buffered until exit. Trust the order, not the times.
+126 -2
View File
@@ -16,6 +16,10 @@
; See Root.pbi for how the workers are stopped. ; See Root.pbi for how the workers are stopped.
; Exits with 0 on success, 1 on failure. ; 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. ; Compile as a thread-safe console program.
; ============================================================================ ; ============================================================================
@@ -32,11 +36,113 @@ Procedure PrintCommands()
EndProcedure 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 Root.BakaShrot_Root
Define Command.s, ExitCode.i 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") 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 Root\ConfigPath = #BakaShrot_Server_DefaultConfigPath
If CountProgramParameters() > 0 If CountProgramParameters() > 0
Root\ConfigPath = ProgramParameter(0) Root\ConfigPath = ProgramParameter(0)
@@ -62,13 +168,31 @@ PrintCommands()
ExitCode = 0 ExitCode = 0
Repeat 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 ; 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)) If FindString(Command, Chr(4))
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
PrintN("No more input, stopping") PrintN("No more input, stopping")
Break Break
CompilerElse
PrintN("No console input, running until SIGTERM or SIGINT")
BakaShrot_Server_NoInput = #True
Continue
CompilerEndIf
EndIf EndIf
Select Command Select Command