Add --list-processes to list running -r/--run sessions

Lists every session pid file found under
$XDG_STATE_HOME/slocker-lite/run/, showing pid, container name, and
status (running or exited). Status is determined the same way any
external tool could check it: a non-blocking exclusive flock() on the
file that succeeds means it's actually stale (nothing holds it), so
the session is reported as exited in that case; the lock is always
released again immediately, never left held by the check itself.

list_sessions() (pid_file.{h,cpp}) reads the real pid from each file's
own contents rather than parsing it out of the filename, which would
be ambiguous for container names that themselves contain '-'.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Gv3s5jckJKzh6JkMoi2Akz
This commit is contained in:
2026-08-22 09:05:27 +00:00
parent 23f380e180
commit 8c55e5c288
5 changed files with 158 additions and 12 deletions
+22 -3
View File
@@ -18,7 +18,13 @@ Source layout (all under `src/`):
- `main.cpp` — CLI entry point, dependency checks, orchestration (`mount_image()`,
`run_container()`, `cleanup_image()`, `unmount_image()`, `list_images_command()`,
`inspect_image_command()`, `create_volume_command()`, `list_volumes_command()`,
`delete_volume_command()`). `inspect_image_command()` implements `-i/--inspect
`delete_volume_command()`, `list_processes_command()`). `list_processes_command()`
implements `--list-processes` (long-option only): calls `list_sessions()`
(`pid_file.{h,cpp}`, see below) and prints one tab-aligned `pid`, `container
name`, `running`/`exited` row per entry (same two-column tab-alignment scheme
as `list_images_command()`/`list_volumes_command()`, extended to a third
column), no header row, silent success on an empty list.
`inspect_image_command()` implements `-i/--inspect
<image.tar>`: prints every `OciImageConfig` field (user/group, exposed ports, env,
volumes, default command) without mounting or running the image — extend it
whenever `OciImageConfig` gains a new field (see `oci_image.{h,cpp}` below).
@@ -188,7 +194,19 @@ Source layout (all under `src/`):
closes the fd (releasing the flock immediately) and removes the file. Every
failure path here (can't create the directory/file, can't lock, can't remove)
is a `spdlog::warn`, never fatal — session tracking is best-effort and must
never block or fail `-r/--run` itself.
never block or fail `-r/--run` itself. `list_sessions()` implements
`--list-processes` (`main.cpp`'s `list_processes_command()`): scans the same
`run/` directory and reports one `SessionInfo {pid, container_name, running}`
per readable pid file. `pid` is read from the file's own contents, not parsed
from the filename (ambiguous for names that themselves contain `-`);
`container_name` is then recovered by stripping that exact `-<pid>` suffix
back off the filename. `running` reuses the same liveness check any external
tool would do — a non-blocking exclusive `flock()` that succeeds means the
file is actually stale, so `running` is false in that case; the lock is always
released again immediately either way, never left held by the check itself. A
file that can't be opened or doesn't parse as a pid (e.g. removed mid-scan) is
silently skipped, not reported as an error — scanning a live directory is
inherently racy.
- `config_file.{h,cpp}``load_config_file()` reads and parses (via libyaml's
document API, `<yaml.h>`) the `global` and `volumes` sections of the local YAML
config file located by `config_file_path()` (`$XDG_CONFIG_HOME/slocker-lite/config.yaml`,
@@ -267,7 +285,8 @@ Build directory is `buildDir/` (already configured).
full flag list: `-m/--mount`, `-r/--run`, `-u/--umount`, `-c/--cleanup`,
`-l/--list-images`, `-i/--inspect`, `-n/--no-nsenter`, `--user`, `--group`,
`--hostname`, `-v/--volume`, `--list-volumes`, `--delete-volume`,
`--delete-volume-full`, `-t/--test`, `--log-level`, `-h/--help`, `-V/--version`)
`--delete-volume-full`, `--list-processes`, `-t/--test`, `--log-level`,
`-h/--help`, `-V/--version`)
- Run tests: `meson test -C buildDir`
## Code style