The helper
This page describes the helper: the small program /.boxfork/guest that the daemon puts in each sandbox.
It is for a contributor who changes the helper, its protocol, or the daemon code that talks to the helper.
After you read this page, you can add a mode or a frame type and keep the sandboxes that run now.
This page uses one more name.
An exec is one process that the engine starts in a container for the daemon.
The method Engine.Exec starts an exec. See engine.md.
The page commands-and-files.md describes commands and file operations from the side of the daemon. The page decisions.md gives the reason for the helper in decision 5.
Why the helper exists
Section titled “Why the helper exists”The daemon does not open a network connection to a sandbox. For each operation, the daemon tells the engine to start the helper as an exec. The daemon and the helper talk through the standard input and the standard output of the exec.
This design has three results:
- A sandbox with networking off has all functions. The daemon controls it only through the engine.
- An idle sandbox runs only its first process. No server listens in the sandbox.
- The helper is one static program. It needs a shell from the image only for a command given as
cmd.
The design has a cost.
The engine needs time to start each exec.
On the test machine, a command with its own exec took a median of 13 ms.
The serve mode decreases this cost: the daemon keeps one exec open and sends many commands through it.
On the test machine, a command after the first in a sandbox took a median of 4 ms.
Source files
Section titled “Source files”| File | Contents |
|---|---|
| cmd/boxfork-guest/main.go | The main function. It calls guest.Main. |
| internal/guest/main.go | Main (the switch of modes), Version, runInit, runDial. |
| internal/guest/proto.go | Frame types, WriteFrame, ReadFrame, WriteMux, ReadMux, the JSON types, Path. |
| internal/guest/run.go | start, command.wait, command.lose, runCmd (run), runServe (serve), ParseSignal. |
| internal/guest/fs.go | runFS, fsRead, fsWrite, EncodeFSSpec. |
| internal/guest/procs.go | runProcs, countProcs. |
| internal/guest/gpus.go | runGPUs, visibleGPUs, deviceFiles. |
| internal/guest/resolv.go | runDNS, fixResolv, usable. |
| internal/guest/own.go | runOwn, giveVolumes. |
| internal/guestbin/guestbin.go | The embedded helper programs and For. |
| internal/sandbox/run.go | The daemon side of run: Manager.Exec, Process, Manager.Run. |
| internal/sandbox/kept.go | The daemon side of serve: openKept, cmdConn, Manager.runKept. |
| internal/sandbox/files.go | The daemon side of fs: Manager.fsOp, answer, Manager.WriteFile. |
| internal/sandbox/manager.go | Manager.boot, Manager.probe, Manager.askAs, Manager.setDNS, Manager.giveVolumes, Manager.checkGPUs. |
How the build makes the helper for two chip types
Section titled “How the build makes the helper for two chip types”The guest target of the Makefile builds the helper two times: for linux/amd64 and for linux/arm64.
Each build sets CGO_ENABLED=0, thus the program has no dynamic library.
The flags are -trimpath -ldflags "-s -w".
The two files go to internal/guestbin/bin:
boxfork-guest-linux-amd64boxfork-guest-linux-arm64
The package guestbin embeds that directory in the daemon with the line //go:embed all:bin.
The prefix all: lets the package compile when the directory contains only .gitkeep.
The function guestbin.For gets an architecture name and gives the bytes of the correct helper.
It accepts amd64 and x86_64 for one file, and arm64 and aarch64 for the other file.
If the file is not there, For gives an error that tells you to run make guest.
The build target depends on the guest target.
A plain go build ./cmd/boxfork does not build the helper again.
It embeds the files that are in internal/guestbin/bin at that time.
How the daemon puts the helper in a sandbox
Section titled “How the daemon puts the helper in a sandbox”Manager.boot in internal/sandbox/manager.go starts each container.
It reads the architecture of the image and calls Config.Helper, which is guestbin.For in the daemon.
sequenceDiagram participant D as Daemon participant E as Engine participant C as Container D->>E: EnsureImage or LocalImage E-->>D: Image with Arch D->>D: Config.Helper(Arch) D->>E: Create, Entrypoint = /.boxfork/guest init D->>E: CopyIn /.boxfork/guest, mode 0755 D->>E: Start E->>C: start the first process (init) D->>E: Exec /.boxfork/guest version C-->>D: one line: the value of VersionThe daemon copies the file before it starts the container, because the first process is the helper.
After the start, Manager.probe runs the version mode and waits a maximum of 30 seconds for one line.
If no line comes, boot removes the container and the create operation fails.
boot runs for a new sandbox, for a sandbox from a snapshot, and for a sandbox that wakes.
Thus each of these sandboxes gets the helper of the daemon build that started its container.
A volume cannot have a mount path in /.boxfork: see noMountUnder in internal/sandbox/volume.go.
Two other functions install the helper in a container for a short test.
tryRuntime does it for the check of gVisor.
Manager.checkSnapshots in internal/sandbox/snapshot.go does it for the check of snapshots.
The first argument of the helper is the mode.
Main in internal/guest/main.go selects the function.
With no argument, the helper writes a usage line to the standard error and exits with status 2.
With a mode that it does not know, it writes guest: unknown command "<mode>" and exits with status 2.
| Mode | Arguments | Input | Output | Caller in the daemon |
|---|---|---|---|---|
init |
none | The signals SIGTERM, SIGINT, SIGCHLD |
Nothing | The Entrypoint in Manager.boot, tryRuntime, Manager.checkSnapshots |
run |
none | Plain frames | Plain frames | Manager.Exec |
serve |
none | Multiplexed frames | Multiplexed frames | openKept |
fs |
One spec, see “File operations” | For write: plain frames |
One JSON line. For read: then the bytes of the file |
Manager.fsOp, helperFS |
dial |
A port, or host:port |
Bytes for the TCP connection | One line, OK or ERR <reason>, then bytes from the TCP connection |
None in this tree |
procs |
none | The directory /proc |
One line: a number | Manager.suspend |
gpus |
none | The output of nvidia-smi, or the directory /dev |
One line | Manager.checkGPUs |
dns |
Zero or more IP addresses | The file /etc/resolv.conf |
It writes the file again. One line | Manager.setDNS, as user 0 |
own |
Zero or more paths | The owner of /proc/1, each path |
It changes the owner of a path. One line | Manager.giveVolumes, as user 0 |
version |
none | Nothing | One line: Version |
Manager.probe |
The modes procs, gpus, dns, own and version give one line.
The daemon reads that line with Manager.askAs.
askAs reads a maximum of 16 KiB and waits for a time that the caller gives.
If the line is empty, askAs gives the text of the standard error as the error.
runInit is the first process of the sandbox.
It gets the exit status of each process that lost its parent, thus the sandbox keeps no dead processes.
On SIGCHLD it calls Wait4 with WNOHANG until no child is left to collect.
On SIGTERM or SIGINT it exits with status 0.
It does not send signals to other processes.
runCmd runs one command and exits.
The first frame from the daemon must be FrameSpec.
If it is not, the helper sends FrameExit with exit code 127 and an error text.
The exit status of the helper is not the exit code of the command.
The exit code of the command goes in FrameExit.
The daemon uses this mode for a command that it streams, and when it cannot use the kept connection.
runServe runs many commands through one exec.
It writes FrameHello first, with the command number 0.
Then it reads multiplexed frames until the input ends.
The daemon gives each command a number, and each frame contains that number.
See “The kept connection” below.
runDial connects to a TCP port in the sandbox and copies bytes in the two directions.
An argument without : is a port on 127.0.0.1, from 1 to 65535.
The helper waits a maximum of 5 seconds for the connection.
It writes OK or ERR <reason> as the first line, thus the caller knows if the port refused the connection.
The copy stops when one of the two sides ends.
countProcs counts the processes in /proc.
It does not count process 1, the helper itself, and a process in state Z.
It does not count a process with the command line /.boxfork/guest serve, because that process belongs to the daemon.
It counts a command that the serve helper started.
If /proc shows no process at all, the helper gives an error and exit status 1.
The daemon suspends a sandbox only when the answer is 0.
gpus, dns, own
Section titled “gpus, dns, own”These modes have their own pages:
gpus: see gpu.md.dns: see network.md.own: it gives each empty volume that root owns to the user of the first process. The answers areOK <number>,KEPTandERR <reason>.
The frame protocol
Section titled “The frame protocol”A frame is one message with a type, a length and a payload.
Each number in a header is an unsigned integer of 32 bits, big-endian.
The sender writes one frame with one Write call.
The code is in internal/guest/proto.go.
Plain frames
Section titled “Plain frames”The modes run and fs use plain frames.
The functions are WriteFrame and ReadFrame.
The header has 5 bytes.
| Byte | Contents |
|---|---|
| 0 | The frame type |
| 1 to 4 | The length of the payload in bytes |
| 5 and up | The payload |
Multiplexed frames
Section titled “Multiplexed frames”The mode serve uses multiplexed frames.
The functions are WriteMux and ReadMux.
The header has 9 bytes: the constant muxHeader.
| Byte | Contents |
|---|---|
| 0 | The frame type |
| 1 to 4 | The number of the command |
| 5 to 8 | The length of the payload in bytes |
| 9 and up | The payload |
The daemon selects the number of a command.
The number 0 belongs to the helper: only FrameHello uses it.
The daemon does not use a number again while its command runs.
Size limit
Section titled “Size limit”The constant MaxFrame is 4 MiB (4 << 20).
It is the maximum length of one payload, for the two frame formats.
WriteFrame and WriteMux give an error for a longer payload and write nothing.
ReadFrame and ReadMux give an error when a header gives a longer length.
The reader then stops, and the connection ends.
The read functions give io.EOF only when the input ends between two frames.
If the input ends in a frame, they give io.ErrUnexpectedEOF.
Frame types
Section titled “Frame types”A type below 0x10 goes from the daemon to the helper.
A type of 0x10 or more goes from the helper to the daemon.
| Constant | Value | Sender | Payload | Meaning |
|---|---|---|---|---|
FrameSpec |
0x01 |
Daemon | JSON RunSpec |
Start a command. In run, it must be the first frame. |
FrameStdin |
0x02 |
Daemon | Bytes | Data for the standard input of the command. In fs, data for the file. |
FrameStdinEOF |
0x03 |
Daemon | Empty | Close the standard input of the command. In fs, the end of the file. |
FrameSignal |
0x04 |
Daemon | Text: a signal name or a number | Send the signal to the process group of the command. |
FrameClose |
0x05 |
Daemon | Empty | Only in serve. Kill the command and its process group. The helper sends no FrameExit for it. |
FrameHello |
0x10 |
Helper | JSON Hello |
Only in serve. The first frame, with the number 0. |
FrameStarted |
0x11 |
Helper | JSON Started |
The process of the command exists. |
FrameStdout |
0x12 |
Helper | Bytes, a maximum of 32 KiB | Output of the command. |
FrameStderr |
0x13 |
Helper | Bytes, a maximum of 32 KiB | Error output of the command. |
FrameExit |
0x14 |
Helper | JSON Exit |
The last frame for a command. |
A receiver ignores a frame type that it does not know.
The helper does this in runCmd, runServe and fsWrite.
The daemon does this in Process.Next and cmdConn.read.
In run, the helper ignores FrameClose: the daemon closes the connection to stop the command.
JSON payloads
Section titled “JSON payloads”RunSpec, the payload of FrameSpec:
| Field | Type | Meaning |
|---|---|---|
cmd |
text | A command line. The helper runs it with /bin/bash -c, or /bin/sh -c if /bin/bash is not there. |
argv |
list of text | A program and its arguments. The helper runs it with no shell. |
cwd |
text | The directory of the command. Empty: the directory of the helper. |
env |
object | Variables that the helper adds to its own environment. A given name replaces the same name. |
timeout_ms |
number | The helper kills the command after this time. 0: no limit. |
stdin |
true or false | True: the command gets a pipe as its standard input. False: the command reads /dev/null. |
The daemon refuses a spec that does not have exactly one of cmd and argv.
If the helper gets a spec with the two fields, it uses argv.
If it gets a spec with none of them, it sends FrameExit with exit code 127.
Started, the payload of FrameStarted:
| Field | Type | Meaning |
|---|---|---|
pid |
number | The process ID of the command in the sandbox. |
Exit, the payload of FrameExit:
| Field | Type | Meaning |
|---|---|---|
exit_code |
number | The exit status. For a command that a signal killed: 128 plus the signal number. 127: the command did not start. |
signal |
text | The name of the signal that killed the command, for example SIGKILL. |
timed_out |
true or false | True when the timer of timeout_ms killed the command. |
error |
text | The reason, when the helper cannot start the command or cannot get its status. |
Hello, the payload of FrameHello:
| Field | Type | Meaning |
|---|---|---|
version |
text | The constant Version of the helper. |
The life of a command
Section titled “The life of a command”The function start in internal/guest/run.go starts a command for run and for serve.
The method command.wait waits for its end.
sequenceDiagram participant D as Daemon participant H as Helper participant P as Command D->>H: FrameSpec (RunSpec) H->>P: start in a new session H-->>D: FrameStarted (pid) D->>H: FrameStdin, FrameStdinEOF H->>P: bytes to the standard input P-->>H: output H-->>D: FrameStdout, FrameStderr D->>H: FrameSignal (TERM) H->>P: signal to the process group P-->>H: exit H->>H: collect output for a maximum of 300 ms H-->>D: FrameExit (Exit)Process group
Section titled “Process group”start sets Setsid for the command.
The command thus starts a new session and has its own process group.
The ID of the group is the process ID of the command.
command.signal sends each signal to the group, not to the one process.
Thus a signal also gets to the processes that the command started.
Standard input
Section titled “Standard input”If stdin is false, the command reads /dev/null, thus a read of its input gets the end immediately.
If stdin is true, a separate goroutine writes to the pipe from the queue stdinQ.
The queue holds 1024 items.
The read loop of the helper thus does not wait for a command that does not read.
The two modes fill the queue differently:
- In
run, the read loop waits when the queue is full. - In
serve, the functionfeeddoes not wait. If the queue is full, the helper kills the command withSIGKILL.
The reason for the rule in serve is that one read loop serves all commands of the sandbox.
Signals
Section titled “Signals”The payload of FrameSignal is text.
ParseSignal accepts a name with or without SIG, in upper case or lower case, or a number from 1 to 64.
The names are HUP, INT, QUIT, KILL, TERM, USR1, USR2, STOP and CONT.
The helper ignores a frame with a signal that it does not know.
The daemon does the same check first: Process.Signal calls guest.ParseSignal and refuses a bad value.
Timeout
Section titled “Timeout”If timeout_ms is more than 0, start sets a timer.
When the time ends, the helper sets timed_out and sends SIGKILL to the process group.
The helper sends no other signal first.
The Exit then has exit_code 137, signal SIGKILL and timed_out true.
Output after the exit
Section titled “Output after the exit”Two goroutines read the output pipes in blocks of 32 KiB and send FrameStdout and FrameStderr.
When the command exits, command.wait waits for these goroutines for a maximum of drainGrace, 300 ms.
Then it closes the read ends of the pipes and sends FrameExit.
FrameExit is always the last frame for the command.
The reason for the limit is a child process in the background. Such a child can keep the output pipe open for as long as it lives. With no limit, the command of the client does not end. The cost: the helper drops output that a child writes later than 300 ms after the exit.
When the connection ends
Section titled “When the connection ends”The helper learns that the daemon went away when a read of its input fails.
The method command.lose then handles each command that did not exit:
- It marks the command as lost.
- It sends
SIGKILLto the process group of the command. - It sends no
FrameExitfor the command.
In run, the helper then exits with status 1.
In serve, the helper waits for all commands and exits with status 0.
This does not change a command that exited before.
A child that such a command left in the background continues, and the first process collects it later.
The states of one command in the helper:
stateDiagram-v2 [*] --> Started: FrameSpec, the process starts [*] --> NotStarted: the process does not start NotStarted --> [*]: FrameExit, exit code 127 Started --> Exited: the process exits Started --> Lost: the input ends, or FrameClose Lost --> [*]: SIGKILL to the group, no FrameExit Exited --> [*]: output for a maximum of 300 ms, then FrameExitThe kept connection
Section titled “The kept connection”The daemon side of serve is in internal/sandbox/kept.go.
Manager.Run tries Manager.runKept first.
If runKept does not handle the command, Manager.Run uses Manager.Exec and the run mode.
runKept does not use the kept connection in these cases:
- The daemon runs with
--connection-idle 0.Config.ConnectionIdleis then negative. - The standard input of the command is larger than
keptStdin, 1 MiB. - The spec is not valid. The
runpath then gives the error text. openKeptfails, or the helper is from an earlier build.
openKept starts the exec /.boxfork/guest serve and waits for FrameHello.
The context of the request controls only the start of the exec. The stream stays open after the request, because the connection lives longer than one request.
Manager.keptFor holds the place in record.connGate, thus only one request opens the connection of a sandbox.
cmdConn.run sends one command:
- It selects the next free number. It does not select 0.
- It sends
FrameSpec. - It sends the standard input in frames of 256 KiB, then
FrameStdinEOF. - It waits for
FrameExit, for the end of the request, or for the end of the connection.
The daemon protects itself from a helper that does not answer:
| Event | Constant | Result |
|---|---|---|
No FrameHello after the exec starts |
keptHello, 15 s |
The daemon closes the exec. This command gets its own exec. |
| The helper does not take a frame | keptStall, 10 s |
cmdConn.send ends the connection. |
No frame about a command after its FrameSpec |
keptStall, 10 s |
The command gets an error. The daemon sends FrameClose and retires the connection. |
| The client stops the request | none | The daemon sends FrameClose and gives the error of the context. The connection stays. |
The connection ends before FrameExit |
none | The command gets the error “the command’s connection to the sandbox was lost”. |
FrameExit with bad JSON |
none | cmdConn.read ends the connection. |
A retired connection accepts no new command. The commands that run through it continue, and the daemon closes it after the last one. The next command opens a new connection.
The daemon sends a command a second time only when it knows that the helper did not get it.
cmdConn.run gives started false in that case, and runKept tries one more connection.
Manager.closeIdleConns runs in each sweep.
It closes a connection that had no command for Config.ConnectionIdle.
The default is DefaultConnectionIdle, one minute.
The helper then sees the end of its input and exits.
File operations
Section titled “File operations”runFS in internal/guest/fs.go does one file operation and exits.
The one argument is an FSSpec as JSON, encoded as base64 with the URL alphabet and without = characters at the end.
EncodeFSSpec makes it.
The helper makes each path absolute with filepath.Abs.
FSSpec:
| Field | Type | Meaning |
|---|---|---|
op |
text | read, write, list, stat, mkdir, remove or rename. |
path |
text | The path. It is necessary. |
to |
text | The target of rename. |
mode |
number | For write: the permission bits. 0: keep the bits of the file, or use 0644 for a new file. |
The helper answers with one JSON line, the FSHeader:
| Field | Type | Meaning |
|---|---|---|
ok |
true or false | True when the operation was successful. |
code |
text | An error code, when ok is false. |
message |
text | An error text, when ok is false. |
size |
number | read: the number of bytes that come after the line. write: the number of bytes in the file. |
mode |
number | read: the permission bits of the file. |
entry |
Entry |
stat: the description of the path. |
entries |
list of Entry |
list: one item for each name in the directory. |
An Entry has the fields name, path, type, size, mode and mtime.
The type is file, dir, symlink or other.
| Operation | What the helper does |
|---|---|
read |
It accepts only a regular file. It writes the header, then exactly size bytes. |
write |
It reads FrameStdin frames until FrameStdinEOF. It writes to a temporary file .boxfork-write-* in the same directory, then renames it. |
stat |
It describes the path with Lstat. It does not follow a link at the end of the path. |
list |
It lists a directory. |
mkdir |
It creates the directory and each parent that is not there, with mode 0755. |
remove |
It removes a file, or a directory with its contents. It refuses /. A path that is not there gives not_found. |
rename |
It creates the parent directories of to, then renames. |
The error codes are constants in internal/guest/proto.go:
| Constant | Value |
|---|---|
CodeNotFound |
not_found |
CodePermission |
permission_denied |
CodeIsDir |
is_directory |
CodeNotDir |
not_directory |
CodeInvalid |
invalid_argument |
CodeIO |
io_error |
Three details of read and write are important:
writemakes the file complete or not at all. If the connection ends beforeFrameStdinEOF, the helper deletes the temporary file.- If
writefails early,drainFramesreads the frames up toFrameStdinEOFfirst. The daemon can then complete its send and read the error. - A file in
/procor/sysreports size 0.fsReadthen reads the file to its end, to a maximum ofmaxSizeless, 16 MiB.
Limits and their constants
Section titled “Limits and their constants”| Limit | Constant | Value | File |
|---|---|---|---|
| Payload of one frame | MaxFrame |
4 MiB | proto.go |
| Header of a multiplexed frame | muxHeader |
9 bytes | proto.go |
| Wait for output after the exit | drainGrace |
300 ms | run.go |
| One block of output | none, in start |
32 KiB | run.go |
| Queue of the standard input | none, in start |
1024 items | run.go |
| A file that reports size 0 | maxSizeless |
16 MiB | fs.go |
Wait for nvidia-smi |
gpuQuery |
20 s | gpus.go |
| Reason text in a one-line answer | none, in oneLine |
300 bytes | gpus.go |
Read of /etc/resolv.conf |
maxResolv |
64 KiB | resolv.go |
| Name servers in the file | maxNameServers |
3 | resolv.go |
Wait for a TCP connection in dial |
none, in runDial |
5 s | main.go |
| Standard input through the kept connection | keptStdin |
1 MiB | kept.go |
Wait for FrameHello |
keptHello |
15 s | kept.go |
| Wait for the helper to take or answer a frame | keptStall |
10 s | kept.go |
| Idle time of the kept connection | DefaultConnectionIdle |
1 minute | kept.go |
| One frame of standard input from the daemon | none | 256 KiB | run.go, kept.go, files.go |
Answer line of fs |
maxAnswer |
32 MiB | files.go |
| Answer line of a one-line mode | none, in Manager.askAs |
16 KiB | manager.go |
| Error text that the daemon keeps from one exec | maxStderr |
16 KiB | docker.go |
| Output that the API keeps for each stream of a command | Server.MaxOutput |
4 MiB | server.go |
The version constant and a helper of an earlier build
Section titled “The version constant and a helper of an earlier build”The constant Version in internal/guest/main.go is "2".
The helper reports it in two places: the version mode and FrameHello.
The daemon does not compare the value at this time.
Manager.probe only makes sure that a line comes.
openKept only makes sure that FrameHello contains valid JSON.
A sandbox keeps the helper file that it got when its container started. Thus a new daemon build can meet a helper of an earlier build.
| Sandbox | Helper that it has |
|---|---|
| Created by this build of the daemon | This build |
| Its container started before an upgrade of the daemon | The earlier build |
| Suspended before the upgrade, woke after it | This build: Manager.boot copies the helper again |
| Created from a snapshot of an earlier build | This build: Manager.boot copies the helper again |
| A waiting sandbox from an earlier build | The daemon replaces the sandbox |
The last row comes from Manager.setupOf in internal/sandbox/pool.go.
setupOf makes a hash that includes the bytes of the two helper programs.
The daemon does not hand out a waiting sandbox with a different hash.
The daemon handles a helper that has no serve mode in openKept:
- The earlier helper writes
guest: unknown command "serve"and exits with status 2. - The daemon reads the end of the output and finds
unknown commandin the error text. openKeptgivesoldas true.Manager.keptForputs the id of the container inrecord.noConnIn.- Each command of that container gets its own exec with the
runmode.
The daemon does not save noConnIn in the state file.
After a restart, the daemon asks each such sandbox one time more. After a wake, the daemon asks the helper of the new container.
Invariants
Section titled “Invariants”A change must keep these rules.
- Do not trust the helper. The helper runs in the sandbox, and code in the sandbox can replace it. The daemon puts a limit on each read from the helper.
- Keep the numbers. Do not change the value, the direction or the payload of a frame type that exists.
- Keep the headers. Do not change the 5 bytes and the 9 bytes of the two headers.
- Ignore what you do not know. Each receiver must ignore a frame type that it does not know.
FrameExitis last. The helper sends no frame about a command after itsFrameExit.- One frame, one
Write. A spec that the daemon did not write completely did not start.cmdConn.rundepends on this. - A started command runs one time. The daemon does not send a command again after the helper possibly got it.
- No wait in the
serveread loop. One loop serves all commands of the sandbox. - No
FrameExitfor a lost command. AfterFrameCloseor the end of the input, the helper says nothing more about the command. - An end of the input kills. The helper kills each command that runs when its input ends.
- The mode names stay. A daemon of a later build runs
run,fsandprocsin a helper of an earlier build. - One line for a one-line mode. The daemon reads only the first line and a maximum of 16 KiB.
Failure cases
Section titled “Failure cases”| Failure | What the helper does | What the daemon does |
|---|---|---|
| The command does not start | FrameExit, exit code 127, error |
It gives the result to the client. |
| The spec is bad JSON | FrameExit, exit code 127, error |
It gives the result to the client. |
The first frame of run is not FrameSpec |
FrameExit, exit code 127, error |
It gives the result to the client. |
A FrameSpec in serve has a number in use |
It drops the frame | It does not send such a frame: cmdConn.run selects a free number. |
A frame is larger than MaxFrame |
The read fails. It kills the commands | The read fails. The connection ends. |
Too much standard input in serve |
It kills the command with SIGKILL |
It gives the Exit with the signal to the client. |
Code in the sandbox kills the serve helper |
Nothing | The commands that ran get an error. The next command opens a new connection. |
Code in the sandbox stops the serve helper |
Nothing | The next command gets an error after 10 s. The command after that opens a new connection. |
| The client kills the sandbox while a command runs | The container goes | Manager.cutOff changes the error to ErrNotFound. |
| The daemon stops | It kills each command that runs | After a restart, the next command opens a new connection. |
| The helper file is not there or cannot run | Nothing | Engine.Start or Manager.probe fails, and the create operation fails. |
How to change it
Section titled “How to change it”Add a mode
Section titled “Add a mode”- Write the function in a new file in
internal/guest. - Add a
casefor the mode to the switch inMain, in internal/guest/main.go. - Add the mode to the usage line in
Main. - If the mode gives one line, start the line with a fixed word, as
GPUsOKorDNSErrdo. - Call the mode from the daemon with
Manager.askorManager.askAs. - Decide what the daemon does when the helper does not know the mode.
- Find that case from the error text
unknown command, asopenKeptdoes. - If the mode reads or changes the machine, add a stand-in to
Fake.Execin fake.go. - Add tests in
internal/guestand ininternal/sandbox. - Change
Versionto a new value, thus a later daemon can identify the helper. - Run
make build, thus the daemon embeds the new helper.
Step 6 is necessary because a sandbox of an earlier build has no such mode.
Step 8 is necessary because the test engine runs guest.Main in the test process, on your machine.
The modes dns and own change files, thus the test engine does not run them.
Add a frame type
Section titled “Add a frame type”- Select a value that is not in use: below
0x10from the daemon,0x10or more from the helper. - Add the constant to internal/guest/proto.go with a comment that gives the payload.
- Handle the type in
runCmd, inrunServe, or in the two. - Handle the type in
Process.NextandcmdConn.readif the helper sends it. - Make sure that the system is correct when the receiver ignores the frame.
- If the daemon must know that the helper has the type, read
Hello.VersioninopenKept. - Change
Versionto a new value. - Add a row to the table “Frame types” on this page.
- Add tests: see “Where the tests are”.
Step 5 is the important one. A helper of an earlier build ignores the new frame from the daemon. After a downgrade, a daemon of an earlier build ignores the new frame from a later helper.
Add a field to a JSON payload
Section titled “Add a field to a JSON payload”- Add the field with
omitempty. - Make sure that the zero value means the behavior of the earlier build.
- Remember that a helper of an earlier build ignores a field that it does not know.
- If the helper must not ignore the field, compare
Versionfirst.
Where the tests are
Section titled “Where the tests are”| File | What it tests |
|---|---|
| internal/guest/guest_test.go | Plain frames, run (output, exit code, argv, cwd, env, standard input, timeout, signals to the group, loss of the connection, children in the background), fs, dial. |
| internal/guest/serve_test.go | Multiplexed frames, serve (commands apart, standard input, signals, FrameClose, loss of the connection, a number in use, too much standard input, open files after each command). |
| internal/guest/procs_test.go | countProcs on a directory that the test makes. |
| internal/guest/gpus_test.go | gpus with a script as a stand-in for nvidia-smi, and with a directory as a stand-in for /dev. |
| internal/guest/resolv_test.go | dns on a file that the test makes. |
| internal/guest/own_test.go | own with a stand-in for the system. |
| internal/sandbox/kept_test.go | The daemon side of serve: one exec for many commands, the idle close, a helper of an earlier build, a helper that does not answer, the limits on what the helper says. |
| internal/sandbox/manager_test.go | TestAHostileHelperCannotExhaustMemory, and the commands and file operations through the test engine. |
| scripts/proof/conn.sh | The proof script for the kept connection on a real engine. |
The unit tests run the code of the helper in the test process. They do not run the two embedded programs.
Not proven
Section titled “Not proven”- The helper for
linux/amd64did not run. Nothing ran on x86_64. - The embedded helper ran only on Docker with gVisor on arm64, in a nested virtual machine, and on Podman.
- The
gpusmode ran only against stand-ins. Nothing ran on NVIDIA GPU hardware. - No code of the daemon calls the
dialmode. Only its unit test runs it. - The times on this page come from the test machine. See performance.md.
Limits
Section titled “Limits”- Code in the sandbox can kill, stop or replace the helper. The daemon limits the result to that sandbox.
- A timeout kills with
SIGKILL. The command gets no time to stop cleanly. - The helper drops output that a child in the background writes later than 300 ms after the exit of the command.
- In
run, the read loop waits when 1024 blocks of standard input are in the queue. A signal frame then waits also. - The signal names are only the nine names in
signals. Use a number for other signals. - When the daemon stops, each connection ends, and the helper kills the commands that run.
- The daemon does not read
Version. A change of the protocol that needs it must add that check.