From 79177cc016fcc2b0aff7a4b927448aa6832043b5 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Wed, 18 Dec 2024 21:29:48 +0100 Subject: [PATCH] Fix dup of pipe (#23217) Resolves #22030 --- src/library_pipefs.js | 3 +++ test/unistd/pipe.c | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/library_pipefs.js b/src/library_pipefs.js index 88a0ededa9c8d..48c0d06757593 100644 --- a/src/library_pipefs.js +++ b/src/library_pipefs.js @@ -77,6 +77,9 @@ addToLibrary({ return 0; }, + dup(stream) { + stream.node.pipe.refcnt++; + }, ioctl(stream, request, varargs) { return {{{ cDefs.EINVAL }}}; }, diff --git a/test/unistd/pipe.c b/test/unistd/pipe.c index d4d135ff6e16f..af7b336b060a4 100644 --- a/test/unistd/pipe.c +++ b/test/unistd/pipe.c @@ -58,7 +58,7 @@ void test_poll(int *fd, int data_available) { assert(pfds[1].revents == POLLOUT); } -int main() { +int test_most() { int fd[2]; unsigned char wchar = 0; unsigned char rchar = 0; @@ -154,3 +154,36 @@ int main() { puts("success"); return 0; } + +int test_redirect_stderr_to_pipe() { + int stderrfd = fileno(stderr); + int pipefd[2]; + int original_fd = dup(stderrfd); // duplicate stderr to original_fd, and original_fd is used to restore stderr later + assert(original_fd >= 0); + assert(pipe(pipefd) == 0); + + int read_end_fd = pipefd[0]; + int write_end_fd = pipefd[1]; + + assert(dup2(write_end_fd, stderrfd) == stderrfd); // now things write to fd(stderr) is redirected to write_end_fd + assert(close(write_end_fd) == 0); // close the write end of the pipe after duplicating + + assert(write(stderrfd, "xyz", 3) == 3); // write to the stderr, expected to be read from pipe + + assert(dup2(original_fd, stderrfd) == stderrfd); // restore fd (stderr) to its original state + assert(close(original_fd) == 0); + + char buffer[10]; + memset(buffer, 0, 10); + assert(read(read_end_fd, buffer, 10) == 3); + assert(strcmp(buffer, "xyz") == 0); + assert(close(read_end_fd) == 0); // Close the read end of the pipe + printf("success\n"); + return 0; +} + +int main() { + test_most(); + test_redirect_stderr_to_pipe(); + return 0; +}