Skip to content

Commit 2183911

Browse files
committed
syscall: Accept O_NONBLOCK flag in pipe2
pipe2 only accepted O_CLOEXEC, rejecting O_NONBLOCK with ENOTSUP. Accept O_NONBLOCK and set it on the created pipe streams. This is needed by GLib's GWakeup which uses pipe2(fds, O_CLOEXEC | O_NONBLOCK).
1 parent de5d6f0 commit 2183911

3 files changed

Lines changed: 19 additions & 1 deletion

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/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)