Skip to content

Commit f8e6e4b

Browse files
authored
[syscall] Accept O_NONBLOCK flag in pipe2 (#26501)
Accept O_NONBLOCK and set it on the created pipe streams, it is a no-op in practice, but there is no reason to not allow it.
1 parent 4c5a890 commit f8e6e4b

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

src/lib/libsyscall.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,18 @@ var SyscallsLibrary = {
199199
if (fdPtr == 0) {
200200
throw new FS.ErrnoError({{{ cDefs.EFAULT }}});
201201
}
202-
if (flags && flags != {{{ cDefs.O_CLOEXEC }}}) {
202+
var validFlags = {{{ cDefs.O_CLOEXEC }}} | {{{ cDefs.O_NONBLOCK }}};
203+
if (flags & ~validFlags) {
203204
throw new FS.ErrnoError({{{ cDefs.ENOTSUP }}});
204205
}
205206
206207
var res = PIPEFS.createPipe();
207208
209+
if (flags & {{{ cDefs.O_NONBLOCK }}}) {
210+
FS.getStream(res.readable_fd).flags |= {{{ cDefs.O_NONBLOCK }}};
211+
FS.getStream(res.writable_fd).flags |= {{{ cDefs.O_NONBLOCK }}};
212+
}
213+
208214
{{{ makeSetValue('fdPtr', 0, 'res.readable_fd', 'i32') }}};
209215
{{{ makeSetValue('fdPtr', 4, 'res.writable_fd', 'i32') }}};
210216

test/codesize/test_codesize_hello_dylink_all.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"a.out.js": 244300,
2+
"a.out.js": 244343,
33
"a.out.nodebug.wasm": 577444,
4-
"total": 821744,
4+
"total": 821787,
55
"sent": [
66
"IMG_Init",
77
"IMG_Load",

test/unistd/misc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ int main() {
8686
printf("pipe2(bad): %d", pipe2(0, 0));
8787
printf(", errno: %d\n", errno);
8888
errno = 0;
89+
printf("pipe2(O_NONBLOCK): %d", pipe2(pipe_arg, O_NONBLOCK));
90+
printf(", errno: %d\n", errno);
91+
printf("pipe2(O_NONBLOCK) read flags: %d\n", (fcntl(pipe_arg[0], F_GETFL) & O_NONBLOCK) != 0);
92+
printf("pipe2(O_NONBLOCK) write flags: %d\n", (fcntl(pipe_arg[1], F_GETFL) & O_NONBLOCK) != 0);
93+
errno = 0;
94+
printf("pipe2(O_CLOEXEC|O_NONBLOCK): %d", pipe2(pipe_arg, O_CLOEXEC | O_NONBLOCK));
95+
printf(", errno: %d\n", errno);
96+
errno = 0;
8997

9098
char* exec_argv[] = {"arg", 0};
9199
char* exec_env[] = {"a=b", 0};

test/unistd/misc.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ pipe(good): 0, errno: 0
1515
pipe(bad): -1, errno: 21
1616
pipe2(good): 0, errno: 0
1717
pipe2(bad): -1, errno: 21
18+
pipe2(O_NONBLOCK): 0, errno: 0
19+
pipe2(O_NONBLOCK) read flags: 1
20+
pipe2(O_NONBLOCK) write flags: 1
21+
pipe2(O_CLOEXEC|O_NONBLOCK): 0, errno: 0
1822
execl: -1, errno: 45
1923
execle: -1, errno: 45
2024
execlp: -1, errno: 45

0 commit comments

Comments
 (0)