diff options
author | Kent Overstreet <kent.overstreet@linux.dev> | 2024-04-25 00:43:47 -0400 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2024-04-25 00:43:47 -0400 |
commit | ec2d118738facd1456a25bb67015e67094010b99 (patch) | |
tree | dafa92aceec7e64b0dcc4dfe06d1c74a5cd32c17 | |
parent | b3677f3040fe51cc2226a99a755af8dbc3d373a6 (diff) |
fix splice_fd_to_stdinout() when stdin is closed
We need to check when stdin has been closed - otherwise we'll spin
because select() will return immediately.
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
-rw-r--r-- | c_src/cmd_fsck.c | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/c_src/cmd_fsck.c b/c_src/cmd_fsck.c index 0d4070a3..05582d4a 100644 --- a/c_src/cmd_fsck.c +++ b/c_src/cmd_fsck.c @@ -60,14 +60,18 @@ static int splice_fd_to_stdinout(int fd) setnonblocking(STDIN_FILENO); setnonblocking(fd); + bool stdin_closed = false; + while (true) { fd_set fds; FD_ZERO(&fds); - FD_SET(STDIN_FILENO, &fds); FD_SET(fd, &fds); + if (!stdin_closed) + FD_SET(STDIN_FILENO, &fds); - select(fd + 1, &fds, NULL, NULL, NULL); + if (select(fd + 1, &fds, NULL, NULL, NULL) < 0) + die("select error: %m"); int r = do_splice(fd, STDOUT_FILENO); if (r < 0) @@ -78,6 +82,8 @@ static int splice_fd_to_stdinout(int fd) r = do_splice(STDIN_FILENO, fd); if (r < 0) return r; + if (r) + stdin_closed = true; } return close(fd); |