Skip to content

fix(redirect): DOS device targets — append opens, <>CON split, test(1) /dev/tty (niubash#519) - #1324

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/519-console-devices
Oct 23, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/519-console-devices

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Windows console/DOS devices reject operations that are fine on POSIX ttys — this PR fixes the three surfaces found under niubash#519 (echo >/dev/tty → Invalid argument, breaking niugit's askpass):

  • Append opens (5919d6f4): CON/CONIN$/CONOUT$/NUL/COMx/LPTx reject FILE_APPEND_DATA (ERROR_INVALID_PARAMETER). New fd::is_dos_device_path + open_redirect_append route every user-facing append open; FileFd::open_write gets the same treatment for exec N>> binds.
  • <> family (719334d5): CON also rejects GENERIC_READ|GENERIC_WRITE. New FileFd::open_readwrite_pair binds CONIN$ (read) + CONOUT$ (write) for exec N<>, {var}<>, cmd <>, background <>; probes open the CONIN$ half.
  • test(1) (87c2f7c4): test -e/-r/-w/-c /dev/tty answered before metadata("CON"), which fails on device names. GNU's node always exists + is a rw char device.

Verification

  • 25+ append-open call sites converted; echo >/dev/tty, >>, &>, &>>, 2>, exec 3>/3>>, exec 5<>, {v}<>, cmd <> all exit 0 under a real console.
  • New tests/issue519_dev_tty_redirect_regressions.rs (5 legs, console-gated skip in headless CI) + is_dos_device_path unit test.
  • cargo test --lib 789/789; ~20 redirect-related test files green; regular-file append/truncate/offset semantics unchanged.
  • Verified end-to-end via niu -c (niubash builds against this tree).
  • Filed follow-ups: rubash#1312 (noclobber on devices), rubash#1313 ({var} fd-var), rubash#1314 (residual edges).

Generated with Devin

caomengxuan666 and others added 3 commits October 23, 2026 15:14
unixwin/niubash#519: `echo >/dev/tty` failed `Invalid argument` and
broke niugit's askpass prompt. The /dev/tty -> CON translation
(executor/path.rs) reached CreateFileW with FILE_APPEND_DATA, which a
device rejects with ERROR_INVALID_PARAMETER: the redirect's own open
succeeded while every append-mode reopen — OutputTarget::Path's
deferred writes, `>>` probes, external `>>` seek-end wiring, and the
fd-table `exec N>>` bind — failed the same way.

GNU redir.c:725 redir_open does one open(O_WRONLY|O_CREAT|O_APPEND),
which succeeds on a POSIX tty because O_APPEND is a no-op on a
character device. The Windows adaptation now recognizes bare DOS
device names (CON, CONIN$, CONOUT$, NUL, PRN, AUX, COM1-9, LPT1-9) via
fd::is_dos_device_path and opens them in plain write mode — a stream
device has no file offset for append to act on. Every user-facing
append open goes through the new open_redirect_append helper;
FileFd::open_write's append arm gets the same treatment so `exec
3>>/dev/tty` binds too.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Same niubash#519 class, second surface: `exec 5<>/dev/tty`,
`{var}<>/dev/tty`, `cmd <>/dev/tty`, and background `N<>` all failed
`Invalid argument` — the console multiplex device CON is two kernel
objects, so GENERIC_READ|GENERIC_WRITE fails CreateFileW with
ERROR_INVALID_PARAMETER (verified: CON rw -> 87; CONIN$/CONOUT$ each
open read-write fine; NUL rw fine).

GNU redir.c r_input_output does open(filename, O_RDWR|O_CREAT) once —
one description for both directions. On Windows the console cannot be
one handle, so FileFd::open_readwrite_pair returns an endpoint pair:
the same Rc for regular files, or CONIN$ (read) + CONOUT$ (write) for
CON. fd-table binds (`set_fd_readwrite_file`, `{var}<>`), the
validation probe, and the background-stdio/probe/open-for-fd0 sites
use it — probes and child stdin need only the CONIN$ half.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
niubash#519 family, third surface: `test -e /dev/tty` (and -r/-w)
returned false — the virtual device table covered /dev/null and
/dev/std* but not /dev/tty, so the checks fell through to
metadata("CON"), which fails on DOS device names.

GNU's node always exists and is a readable+writable character device:
stat(2)/access(2) succeed on /dev/tty even without a controlling
terminal (only open() fails ENXIO), so the truth table is /dev/null's
— no probing needed.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.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