Skip to content

feat(builtins): implement uptime builtin#562

Draft
san-jos wants to merge 5 commits into
mainfrom
feat/uptime-builtin
Draft

feat(builtins): implement uptime builtin#562
san-jos wants to merge 5 commits into
mainfrom
feat/uptime-builtin

Conversation

@san-jos

@san-jos san-jos commented Jul 22, 2026

Copy link
Copy Markdown

Summary

  • Adds the `uptime` builtin command with cross-platform support (Linux, macOS, Windows)
  • Default output: current time, uptime duration, and load averages (1/5/15 min); user count intentionally omitted (reading utmp would expose session data)
  • Flags: `-p`/`--pretty` (human-readable duration), `-s`/`--since` (boot time as `YYYY-MM-DD HH:MM:SS`), `-h`/`--help`

Data sources

All bypass `AllowedPaths` (fixed kernel paths — same precedent as `ss`, `ip route`, `df`):

Platform Uptime Load avg
Linux `/proc/uptime` `/proc/loadavg`
macOS `sysctl kern.boottime` `sysctl vm.loadavg`
Windows `GetTickCount64` via kernel32.dll not available

Windows 32-bit (386): `GetTickCount64` returns a 64-bit ULONGLONG split across lo (EAX) and hi (EDX) — combined in `sysinfo_windows_386.go`. On 64-bit targets hi is XMM0 (unspecified) and is ignored in `sysinfo_windows_64bit.go`.

Test plan

  • Unit tests: `builtins/uptime/uptime_test.go` — `formatDuration`, `formatPretty`, `formatDefault` with deterministic fixed inputs (no OS calls)
  • Integration tests: `builtins/tests/uptime/uptime_test.go` — 6 handler-level cases with injected fake provider (default output, `-p`, `-s`, `-p` beats `-s`, generic error, `ErrNotSupported`)
  • Scenario tests: flag rejection, extra operands, `--help`, `--` end-of-flags separator (error cases only; happy-path scenarios omitted as `sysinfo.Get()` may return EPERM in restricted CI environments)
  • Cross-compile verified: `GOOS=windows GOARCH={amd64,386,arm64}` all build clean
  • All analysis/symbol allowlist tests pass

🤖 Generated with Claude Code

san-jos and others added 4 commits July 22, 2026 12:09
Adds the uptime builtin command with cross-platform support (Linux,
macOS, Windows). Outputs current time, system uptime duration, and
load averages (1/5/15 min). User count is intentionally omitted to
avoid reading utmp session data.

Flags: -p/--pretty (human-readable duration), -s/--since (boot time
as YYYY-MM-DD HH:MM:SS), -h/--help.

Data sources bypass AllowedPaths (fixed kernel paths, same precedent
as ss, ip route, df): /proc/uptime + /proc/loadavg on Linux; sysctl
kern.boottime + vm.loadavg on Darwin; GetTickCount64 on Windows.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
- Windows 32-bit: combine lo/hi uintptr from GetTickCount64.Call() into
  a uint64 to avoid the ~49.7-day rollover on windows/386 where the
  return value is split across the low and high words

- macOS reliability: remove four happy-path scenario tests that call
  sysinfo.Get() directly and fail with EPERM in restricted environments;
  replace with deterministic unit tests for formatDuration, formatPretty,
  and formatDefault in builtins/uptime/uptime_test.go (pure functions,
  no OS calls)

- README: add uptime to the AllowedPaths bypass note alongside ss, ip
  route, and df

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Remove unused fmt import (sysinfo_windows.go no longer calls fmt.Errorf).

Split the tick-count extraction into build-tagged helpers to fix 64-bit
corruption: on amd64/arm64 Proc.Call's second return (hi) is the XMM0
floating-point register, not guaranteed zero, so combining lo|hi<<32
would corrupt the result. The split:

  sysinfo_windows_386.go     (implicit GOOS=windows GOARCH=386):
    ticksToMs combines lo and hi to reconstruct the full ULONGLONG from
    the Win32 32-bit calling convention (EDX:EAX pair).

  sysinfo_windows_64bit.go   (build: windows && !386):
    ticksToMs returns lo only; hi is XMM0 and must be ignored.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Add New(getInfo) constructor to uptime.go so tests can inject a
deterministic sysinfo.Info without OS syscalls.

builtins/tests/uptime/uptime_test.go covers the six handler-level
behaviors not exercised by the existing unit tests or scenario tests:

- default output wired end-to-end (provider -> formatDefault -> stdout)
- -p routes to formatPretty
- -s formats BootTime via time.Unix(...).Local()
- -p takes precedence over -s when both flags are set
- generic provider error -> stderr message, exit 1
- ErrNotSupported -> platform-specific message, exit 1

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
@san-jos
san-jos force-pushed the feat/uptime-builtin branch from 253947f to c966c1a Compare July 22, 2026 10:14
Replace with neutral phrasing to avoid any licensing ambiguity.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant