Skip to content

Commit

Permalink
Fix dup of pipe (emscripten-core#23217)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoodmane authored Dec 18, 2024
1 parent dea1bf9 commit 79177cc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/library_pipefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ addToLibrary({

return 0;
},
dup(stream) {
stream.node.pipe.refcnt++;
},
ioctl(stream, request, varargs) {
return {{{ cDefs.EINVAL }}};
},
Expand Down
35 changes: 34 additions & 1 deletion test/unistd/pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

0 comments on commit 79177cc

Please sign in to comment.