Skip to content

perf(shell): relink bash.exe and sh.exe as hardlinks to niu.exe - #397

Merged
caomengxuan666 merged 3 commits into
masterfrom
perf/hardlink-shell-faces
Oct 17, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
perf/hardlink-shell-faces

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Supersedes #394, which GitHub closed when its base branch (#393's) was deleted on merge. Rebased onto master, where #393 now is — discovery has to work from a hardlinked entry name before the shim can be replaced.

The problem

bash.exe/sh.exe are currently a 243 KB forwarding shim (rubash's bash bin, staged by scripts/package-release.ps1) that sets NIU_INVOKED_AS and re-executes niu.exe. That is one extra process on every bash -c / sh -c invocation — measured at +12.8 ms per call on this machine (49.6 ms through the shim vs 36.8 ms for niu.exe itself) — and it is exactly the per-command tax AI agents, CI and git hooks pay (#377).

The same faces are free as hardlinks: the engine already selects $0, BASH_VERSION and POSIX mode from argv[0], and with #393 discovery finds the installation from any entry point.

What this adds

  • niubash_runtime::shell_links — creates bash.exe and sh.exe as hardlinks to the running niu.exe in <root>\bin, <root>\winuxcmd\bin and <root>\winuxcmd\usr\bin, whichever of those exist.
    • find_installation_root walks up to the directory holding niu.exe next to the bundled winuxcmd tree, so the same root is found whether the shell was reached as niu.exe, as <root>\bin\bash.exe, or from inside the winuxcmd tree (and it falls back to the executable's own directory for a portable layout).
    • Idempotent; an existing name that is not this niu.exe (the shipped shim, or a link left on the previous inode after an update replaced niu.exe) is kept unless --force. With --force it is parked first, linked, and only then dropped, so a shell running right now keeps its bytes — the technique from fix(wpm): replace held payload files by parking them, not by refusing WinuxCmd#1194.
  • niu --relink-shells [--force] [--quiet], called from the installer's [Run] right after wpm links rebuild — so an update that replaces niu.exe refreshes the names instead of leaving them stale.
  • Hardlinks are created on the user's machine, never in the distribution: a zip would store the 12 MB payload once per name, while hardlinks on disk are free. Portable layouts and filesystems without hardlink support (exFAT, network shares) keep the shipped shim, which produces the same identity through NIU_INVOKED_AS. Both paths stay supported deliberately.
  • docs/src/installer.md documents that contract instead of forbidding the argv[0] route.

Verification

Sandbox with six fake forwarding shims and a built niu.exe:

niu: shell links under …\Niubash: 6 created, 0 already current, 0 skipped, 0 failed
  before: 0/6 are niu links   after: 6/6 are niu links   leftovers: none
second run: 0 created, 6 already current, 0 skipped, 0 failed
  • $0, BASH_VERSION=5.3.0(1)-release and set -o | grep -w posix behave the same as before: bash → posix off, sh → posix on.
  • bash -c exit costs 38–41 ms (debug build; the same as niu.exe, versus +12.8 ms through the shim).
  • cargo test -p niubash-runtime --lib → 538 passed, including four new shell_links tests (planned links, root discovery from a hardlinked name, idempotence/stale refresh/parking, missing directories).

Face semantics (second commit)

A hardlinked bash.exe makes two pre-existing leaks visible, both fixed:

  • Product verbs no longer leak through a shell face. bash setup used to open Niubash's setup wizard because the subcommand table was matched under every face. GNU bash reads the first non-option word as the script to run, so bash setup now runs a file named setup and reports ENOENT (127) when there is none — verified both ways, with niu setup unchanged. The face comes from one helper (shell_face_name), which the launcher-word dispatch already needed for --help/--version.
  • The shell-path contract names the shell, not the face. Invoked through a hardlink, current_exe() is …\bash.exe, so NIU_SHELL/WINUXSH_SHELL followed the face; they now come from shell_links::resolved_shell_path (the installation root's niu.exe). $BASH/$SHELL deliberately still follow the invoked name — the GNU reading, where $BASH names the shell that is running.

Measured through the sandbox hardlinks: NIU_SHELL=<root>\niu.exe, BASH=<root>/bin/bash.exe, bash setup → rc 127 "No such file or directory", bash setup with a script of that name → runs the script, niu setup -h → unchanged, bash --version → GNU banner, sh -c 'set -o | grep posix' → on.

Not in this PR

  • The C:\Program Files\Git\bin\bash.exe slot (a 5 KB .NET launcher that currently costs 91 ms because it starts the CLR and then the shim) is being analysed separately; the same --relink-shells machinery is what that fix needs.
  • Retiring rubash's bash shim upstream is its own decision (separate release train); as long as some layouts still ship it, the shim stays the fallback.

The shell has been reached through a 243 KB forwarding shim that sets
`NIU_INVOKED_AS` and re-executes `niu.exe`, which costs one extra process on
every `bash -c` / `sh -c` - measured at +12.8 ms per call, and the per-command
tax AI agents, CI and git hooks pay constantly (#377). The same
faces are free as hardlinks: the engine already selects `$0`, `BASH_VERSION`
and the POSIX mode from `argv[0]`, and discovery now finds the installation
from any entry point, so a name in `<root>\bin` is enough.

- `niubash_runtime::shell_links` creates `bash.exe` and `sh.exe` as hardlinks
  to the running `niu.exe` in `<root>\bin`, `<root>\winuxcmd\bin` and
  `<root>\winuxcmd\usr\bin`, whichever of those exist. `find_installation_root`
  walks up to the directory that holds `niu.exe` next to the bundled winuxcmd
  tree, so the same root is found whether the shell was reached as `niu.exe`,
  as `<root>\bin\bash.exe` or from inside the winuxcmd tree.
- It is idempotent, and an existing name that is not this `niu.exe` (the
  shipped shim, or a link left on the previous inode after an update replaced
  `niu.exe`) is kept unless `--force` is given. With `--force` the name is
  parked first, linked, and only then dropped - a shell running right now keeps
  its bytes (the technique from unixwin/WinuxCmd#1194).
- Exposed as `niu --relink-shells [--force] [--quiet]`, and called from the
  installer's `[Run]` right after `wpm links rebuild`, so an update that
  replaces `niu.exe` refreshes the names instead of leaving them stale.
- Hardlinks are created on the user's machine, never in the distribution: a zip
  would store the 12 MB payload once per name. Portable layouts and filesystems
  without hardlinks keep the shipped shim, which produces the same identity
  through `NIU_INVOKED_AS` - both paths stay supported.
- `docs/src/installer.md` now documents that contract instead of forbidding the
  `argv[0]` route.

Measured in a sandbox with six fake shims: `--relink-shells --force` replaced
all six with links to `niu.exe` (no leftovers), a second run reports
`0 created, 6 already current`, and `bash -c exit` costs the same as
`niu.exe -c exit`. `sh` still selects POSIX mode and `bash` does not.
…aming the shell

Two things a hardlinked `bash.exe`/`sh.exe` makes visible, both fixed here.

**Product verbs no longer leak through a shell face.** `bash setup` used to open
Niubash's setup wizard, because the subcommand table was matched under every
face. GNU bash reads the first non-option word as the script to run, so
`bash setup` runs a file named `setup` and reports ENOENT (127) when there is
none - verified both ways, with `niu setup` unchanged. The face comes from one
helper (`shell_face_name`), which the launcher-word dispatch already needed for
`--help`/`--version`.

**The shell-path contract names the shell, not the face.** Invoked through a
hardlink, `current_exe()` is `...\bash.exe`, so `NIU_SHELL` and
`WINUXSH_SHELL` used to follow the face; rubash's shim target search and the
tooling that reads them want the real binary, so they now come from
`shell_links::resolved_shell_path` (the installation root's `niu.exe`, falling
back to the invoked path). `$BASH`/`$SHELL` deliberately still follow the
invoked name, which is the GNU reading - `$BASH` names the shell that is
running, so `bash` sees `...\bash.exe`.

Tests: `tests/host_contract.rs` gains
`shell_faces_do_not_expose_product_verbs_and_name_the_real_shell` (ENOENT
surface, the script actually running, the environment contract, and the product
verbs still present on the niu face). `cargo test --test host_contract` 48
passed, `cargo test -p niubash-runtime --lib` 538 passed.
`relink_shell_faces` was inserted between a `#[cfg(windows)]` attribute and the
function it belonged to, so the attribute attached to the new function and the
Windows terminal-profile implementation lost its gate. On unix hosts that made
the windows-only code compile (references to `shortcut::plan_for_installation`
and friends) and left `relink_shell_faces` itself unqualified, which is what the
cross-target jobs reported:

  error[E0428]: the name `install_windows_terminal_shortcuts` is defined multiple times
  error[E0425]: cannot find function `relink_shell_faces` in this scope

The helper now lives at the end of the file, where it cannot steal another
function's attributes, and the cfg attribute sits immediately above its own
implementation again. The link-planning test builds its expectations with
`join` instead of Windows literals so it does not depend on the separator.

Verified locally against the same target the CI cross jobs use:
`cargo check --tests --target x86_64-unknown-linux-gnu` and the Windows
`cargo test -p niubash-runtime` shell_links suite both pass.
@caomengxuan666
caomengxuan666 force-pushed the perf/hardlink-shell-faces branch from 14167a4 to 771acf8 Compare October 17, 2026 04:28
@caomengxuan666
caomengxuan666 merged commit 8a9470e into master Oct 17, 2026
9 checks passed
@caomengxuan666
caomengxuan666 deleted the perf/hardlink-shell-faces branch October 17, 2026 11:54
caomengxuan666 added a commit to unixwin/rubash that referenced this pull request Oct 18, 2026
…n console (#751)

`suppress_child_console_window` (niubash#379) chose CREATE_NO_WINDOW so a GUI
parent spawning the shim with piped stdio does not make Windows allocate a
*visible* console for the niu.exe child. That flag still allocates a console -
a hidden one, with conhost.exe - on every invocation in exactly the case
agents, CI and git hooks hit (all three streams redirected).

With all three streams redirected the child has no console to inherit and does
not need one, so DETACHED_PROCESS gives it that for free, and it keeps the
anti-flash property: a console-subsystem child started with DETACHED_PROCESS
gets no automatic console either. The gate is unchanged (all three streams
non-console; an interactive session keeps the real console, as before), and so
is cancellation: neither flag lets the child receive the parent's Ctrl+C, since
a CREATE_NO_WINDOW child has a console of its own rather than the parent's.

Measured here (median of 25, interleaved, all stdio redirected, NIU_SHELL
pointed at hostname.exe so only the shim is timed): 22.9 ms -> 21.8 ms. The
latency win is small - an earlier report credited this flag change with ~22 ms,
which did *not* reproduce on this machine; conhost is evidently shared or warm.
The real cost of a forwarding hop is the extra process, and the durable fix for
`bash`/`sh` is a hardlink to the shell (unixwin/niubash#397), which leaves this
shim as the fallback for layouts without hardlink support. What this change
buys is that the fallback no longer allocates a console per invocation.

Verified: piped stdin/stdout round-trip, exit-code passthrough (`exit 7` -> 7),
face selection unchanged (`$0` = bash, POSIX off; sh.exe -> POSIX on), and no
conhost process for the duration of a 2 s child (only niu.exe).
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