Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,7 @@ jobs:
wasm2js3.test_memorygrowth_2
wasm2js2.test_pthread_proxying
wasmfs.test_hello_world
wasmfs.test_pipe_select
wasmfs.test_unistd_links*
wasmfs.test_atexit_standalone
wasmfs.test_emscripten_get_now
Expand Down
11 changes: 10 additions & 1 deletion src/lib/libpipefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,16 @@ addToLibrary({
dup(stream) {
stream.node.pipe.refcnt++;
},
ioctl(stream, request, varargs) {
ioctl(stream, request, argp) {
if (request == {{{ cDefs.FIONREAD }}}) {
var pipe = stream.node.pipe;
var currentLength = 0;
for (var bucket of pipe.buckets) {
currentLength += bucket.offset - bucket.roffset;
}
{{{ makeSetValue('argp', 0, 'currentLength', 'i32') }}};
return 0;
}
return {{{ cDefs.EINVAL }}};
},
fsync(stream) {
Expand Down
23 changes: 22 additions & 1 deletion system/lib/wasmfs/syscalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,28 @@ int __syscall_ioctl(int fd, int request, ...) {
if (!openFile) {
return -EBADF;
}
if (!isTTY(openFile->locked().getFile())) {

va_list args;
va_start(args, request);
void* argp = va_arg(args, void*);
va_end(args);

auto openHandle = openFile->locked();
auto file = openHandle.getFile();

if (request == FIONREAD) {
off_t size = file->locked().getSize();
if (size < 0) {
return (int)size;
}
if (file->isSeekable()) {
size -= openHandle.getPosition();
}
*static_cast<int*>(argp) = static_cast<int>(size);
return 0;
}

if (!isTTY(file)) {
return -ENOTTY;
}
// TODO: Full TTY support. For now this is limited, and matches the old FS.
Expand Down
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_hello_dylink_all.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"a.out.js": 244260,
"a.out.js": 244350,
"a.out.nodebug.wasm": 577664,
"total": 821924,
"total": 822014,
"sent": [
"IMG_Init",
"IMG_Load",
Expand Down
19 changes: 18 additions & 1 deletion test/core/test_pipe_select.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <sys/select.h>
#include <sys/ioctl.h>
#include <time.h>
#include <assert.h>
#include <stdio.h>
Expand All @@ -7,18 +8,34 @@

int pipe_a[2];

int get_available(int fd) {
int avail = 0;
assert(ioctl(fd, FIONREAD, &avail) == 0);
printf("FIONREAD %d -> %d\n", fd, avail);
return avail;
}

int main() {
fd_set readfds;
const char *t = "test\n";

assert(pipe(pipe_a) == 0);
assert(get_available(pipe_a[0]) == 0);
assert(get_available(pipe_a[1]) == 0);

FD_ZERO(&readfds);
FD_SET(pipe_a[0], &readfds);
write(pipe_a[1], t, strlen(t));
assert(select(pipe_a[0] + 1, &readfds, NULL, NULL, NULL) == 1);
assert(FD_ISSET(pipe_a[0], &readfds));

close(pipe_a[0]); close(pipe_a[1]);
// Slightly strange behavior here that we can use FIONREAD
// on either the read or the write end of the pipe and get
// the same result, but this matches linux.
assert(get_available(pipe_a[0]) == strlen(t));
assert(get_available(pipe_a[1]) == strlen(t));

close(pipe_a[0]);
close(pipe_a[1]);
return 0;
}
Loading