uutils / uutils/coreutils

`dd iflag=nonblock` doesn't work with stdin (fd 0)

Open
#11,543 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

U - dd
Dominant language
Rust
Stars
24.1k
Forks
2k
Avg merge
1d 5h
Merged PRs (30d)
365

Description

The following invocation (which should slurp all pending characters on the tty and immediately exit) hangs until <enter> is sent:

% rust-dd iflag=nonblock bs=1G count=1 of=/dev/null < /dev/tty
^C

In contrast, gnudd works:

% gnudd iflag=nonblock bs=1G count=1 of=/dev/null < /dev/tty
gnudd: error reading 'standard input': Resource temporarily unavailable
0+0 records in
0+0 records out
0 bytes copied, 8.7214e-05 s, 0.0 kB/s
%

As a workaround, it works with if=/dev/tty:

% dd iflag=nonblock bs=1G count=1 of=/dev/null if=/dev/tty
dd: IO error: Would block
%

The reason this seems to happen is because rust-dd seems to lack the fcntl(0, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) call that gnudd does, as verified with strace:

     0.000075 fcntl(0, F_GETFL)         = 0x8000 (flags O_RDONLY|O_LARGEFILE)
     0.000042 fcntl(0, F_SETFL, O_RDONLY|O_NONBLOCK|O_LARGEFILE) = 0                     <---- this line is present in gnudd but missing in rust-dd
     0.000060 lseek(0, 0, SEEK_CUR)     = -1 ESPIPE (Illegal seek)
     0.000066 mmap(NULL, 1073750016, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7e0d941fe000
     0.000071 read(0, 0x7e0d941ff000, 1073741824) = -1 EAGAIN (Resource temporarily unavailable)
     0.000058 write(2, "gnudd: ", 7gnudd: )    = 7
     0.000046 write(2, "error reading 'standard input'", 30error reading 'standard input') = 30

This was discovered because rust-dd breaks fzf-tab like so: https://github.com/Aloxaf/fzf-tab/issues/549

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the dd handling for the iflag=nonblock option when standard input is file descriptor 0, and compare its behavior with the strace shown for gnudd. Reproduce the /dev/tty invocation, then verify that pending input returns promptly with the expected EAGAIN-style error and add a regression test if the existing test structure supports it.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.