# enboot protocol (version 1)

`enboot` is a minimal resident parent process that keeps open file descriptors alive while a tenant child process replaces its own executable image via `execve`. `enboot` inspects nothing inside the descriptors it retains.

The protocol vocabulary is strictly limited to seven concepts: `enboot`, `tenant`, `name`, `descriptor`, `path`, `pid`, and `status`.

## Transport and framing

Before starting the tenant, `enboot` creates one `AF_UNIX` `SOCK_STREAM` socket pair. Both ends are marked close-on-exec in the parent; the tenant inherits one end as file descriptor `3` and receives `ENBOOT_FD=3` in its environment. There is no listening socket, no filesystem path, and at most one active tenant.

Communication is strictly lockstep: the tenant writes one request line, reads one reply line, and never has more than one request in flight. A message attaches at most one file descriptor as `SCM_RIGHTS` ancillary data on the message's bytes. Lockstep prevents message coalescing on the stream socket, so newline termination is the only framing required.

```abnf
request = verb [ SP argument ] LF
reply   = "ok" [ SP argument ] LF / "no" SP reason LF
name    = 1*64( ALPHA / DIGIT / "." / "_" / "-" )
path    = 1*( %x01-09 / %x0B-FF ) ; absolute path, no LF or NUL
```

Maximum line length is 65,536 bytes. An unrecognized verb receives `no verb` and leaves the connection open for forward compatibility.

Immediately on startup, a tenant must mark its inherited socket close-on-exec and unset `ENBOOT_FD` and `ENBOOT_ROLLBACK` from its environment so spawned child processes never inherit the resident connection. Before calling `execve` into its next image, the tenant clears close-on-exec on the socket and re-adds `ENBOOT_FD=<fd>` to the exec environment.

## Verbs

| Verb | Ancillary FD | Reply | Semantics |
| --- | --- | --- | --- |
| `hello <n>` | none | `ok <m>` | Negotiates protocol version `m = min(n, resident_max)`. Must be the first request of every program image; resets the tenant's ready state to false |
| `put <name>` | 1 FD | `ok` or `no usage` | Retains a duplicate of the attached descriptor under `<name>`, closing any prior descriptor with the same name |
| `get <name>` | none | `ok` + 1 FD, or `no absent` | Sends a duplicate of the retained descriptor while keeping the resident's own copy open |
| `list` | none | `ok [name ...]` | Returns all retained descriptor names, space-separated (sorted in the reference implementation) |
| `drop <name>` | none | `ok` or `no absent` | Closes and removes the resident's copy for `<name>` |
| `ready <path>` | none | `ok` | Marks the running image healthy and sets `<path>` as the rollback target (clearing any active rollback flag) |
| `next <path>` | none | `ok` | One-shot directive: when the tenant exits, start `<path>` instead of exiting |
| `reaped` | none | `ok [pid:status ...]` | Returns and drains `pid:waitstatus` pairs for orphaned descendants reaped by `enboot` (decimal raw `wait(2)` status); always empty `ok` on platforms without sub-reaping |

Any unexpected file descriptor attached to a verb other than `put` is closed immediately by the resident.

## Tenant exit and rollback rules

`enboot` is the tenant's parent and detects tenant exit solely via `wait(2)`, never from socket EOF. When the tenant exits, `enboot` evaluates three rules in order:

1. If `next <path>` was set: clear `next` and start `<path>` as the new tenant.
2. Otherwise, if the exiting image never sent `ready`, a rollback target exists, and `enboot` has not already rolled back once since the last `ready`: set the rolled-back flag and start the rollback target with `ENBOOT_ROLLBACK=1` in its environment.
3. Otherwise: close all retained descriptors and exit with the tenant's exit status (or `128 + signal` if terminated by a signal).

Every process started by `enboot` receives the tenant's original argument list (with `<path>` as `argv[0]`) and the environment `enboot` itself was started with (minus any inherited `ENBOOT_FD` or `ENBOOT_ROLLBACK`), a fresh socket pair on descriptor `3`, and `enboot`'s standard streams (`0`, `1`, `2`). Retained descriptors are never inherited across `fork`/`exec`; the tenant requests them explicitly with `get`.

A tenant may replace its own image directly via `execve(2)`, preserving its PID and child processes. The new image's `hello` clears the ready state, so rule 2 triggers rollback automatically if the new image crashes before sending `ready`.

## Orphan sub-reaping

On platforms that support sub-reaping (Linux `PR_SET_CHILD_SUBREAPER`), `enboot` registers as a sub-reaper at startup. Descendants orphaned by a tenant exit are reparented to `enboot`, which reaps them in its `wait` loop and queues their `pid:status` entries for the next `reaped` request. On platforms without sub-reaping, orphans reparent to system init and `reaped` always returns `ok`.

## Deliberate omissions

- No data path: `enboot` never reads from or writes to retained descriptors.
- No auxiliary process spawning: `enboot` starts only the tenant process.
- No disk state: checkpoints belong to the tenant (a tenant that wants `enboot` to retain a checkpoint deposits an open descriptor to an unlinked file).
- No download, signature verification, or version policy: paths are supplied by the tenant.
- No server-initiated messages: `enboot` only replies to tenant requests.

## Allowed reasons to change the resident

The resident is designed to remain unchanged indefinitely. Changes are restricted to five causes:

1. Fixing a bug in `enboot` itself.
2. Adapting to an OS change in `SCM_RIGHTS`, `socketpair`, `execve`, or `wait` semantics.
3. Supporting orphan sub-reaping on a new platform.
4. Revising rollback policy (rule 2), the only policy decision the resident makes.
5. Adding a new protocol verb negotiated via `hello` (older residents reply `no verb` and tenants must degrade cleanly across that compatibility gap).
