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
16 changes: 6 additions & 10 deletions libc-bottom-half/cloudlibc/src/libc/poll/poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ int poll(struct pollfd* fds, nfds_t nfds, int timeout)
for (size_t i = 0; i < nfds; ++i) {
descriptor_table_entry_t* entry;
if (descriptor_table_get_ref(fds[i].fd, &entry)) {
found_socket = true;
if (entry->tag == DESCRIPTOR_TABLE_ENTRY_TCP_SOCKET
|| entry->tag == DESCRIPTOR_TABLE_ENTRY_UDP_SOCKET)
found_socket = true;
else
found_non_socket = true;
} else {
found_non_socket = true;
}
Expand All @@ -155,16 +159,8 @@ int poll(struct pollfd* fds, nfds_t nfds, int timeout)
errno = ENOTSUP;
return -1;
}

return poll_wasip2(fds, nfds, timeout);
} else if (found_non_socket) {
return poll_wasip1(fds, nfds, timeout);
} else if (timeout >= 0) {
return poll_wasip2(fds, nfds, timeout);
} else {
errno = ENOTSUP;
return -1;
}
return poll_wasip2(fds, nfds, timeout);
}
#else // not __wasilibc_use_wasip2
int poll(struct pollfd* fds, nfds_t nfds, int timeout)
Expand Down
9 changes: 2 additions & 7 deletions libc-bottom-half/cloudlibc/src/libc/sys/ioctl/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ int ioctl(int fildes, int request, ...) {
return -1;
}
}

// TODO: In particular, this doesn't support using FIONREAD
// with file descriptors
default:
errno = ENOPROTOOPT;
return -1;
Expand Down Expand Up @@ -111,11 +112,6 @@ int ioctl(int fildes, int request, ...) {
return 0;
}
case FIONBIO: {
#ifdef __wasilibc_use_wasip2
// wasip2 doesn't support setting the non-blocking flag
errno = ENOTSUP;
return -1;
#else
// Obtain the current file descriptor flags.
__wasi_fdstat_t fds;
__wasi_errno_t error = __wasi_fd_fdstat_get(fildes, &fds);
Expand All @@ -140,7 +136,6 @@ int ioctl(int fildes, int request, ...) {
return -1;
}
return 0;
#endif
}
default:
// Invalid request.
Expand Down
30 changes: 14 additions & 16 deletions libc-bottom-half/cloudlibc/src/libc/time/clock_nanosleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,22 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
if ((flags & ~TIMER_ABSTIME) != 0)
return EINVAL;

// Prepare polling subscription.
__wasi_subscription_t sub = {
.u.tag = __WASI_EVENTTYPE_CLOCK,
.u.u.clock.id = clock_id->id,
.u.u.clock.flags = flags,
};
// Convert to wall_clock_datetime_t
wall_clock_datetime_t timeout;
if (!timespec_to_timestamp_clamp(rqtp, &timeout))
return EINVAL;
sub.u.u.clock.timeout = (timeout.seconds * NSEC_PER_SEC)
+ timeout.nanoseconds;
// Note: rmtp is ignored

if (clock_id != CLOCK_MONOTONIC) {
// wasip2 only provides a pollable for monotonic clocks
return ENOTSUP;
}

// Prepare pollable
int64_t duration = (rqtp->tv_sec * NSEC_PER_SEC) + rqtp->tv_nsec;
monotonic_clock_own_pollable_t pollable = monotonic_clock_subscribe_duration(duration);

// Block until polling event is triggered.
size_t nevents;
__wasi_event_t ev;
__wasi_errno_t error = __wasi_poll_oneoff(&sub, &ev, 1, &nevents);
return error == 0 && ev.error == 0 ? 0 : ENOTSUP;
poll_method_pollable_block(poll_borrow_pollable(pollable));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pollable gets leaked here - need to drop it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in ca323cc


poll_pollable_drop_own(pollable);
return 0;
}
#else
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp,
Expand Down
12 changes: 7 additions & 5 deletions libc-bottom-half/cloudlibc/src/libc/unistd/lseek.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ off_t __lseek(int fildes, off_t offset, int whence) {
}
}
// Drop the existing streams
if (entry->stream.read_pollable_is_initialized)
poll_pollable_drop_own(entry->stream.read_pollable);
if (entry->stream.write_pollable_is_initialized)
poll_pollable_drop_own(entry->stream.write_pollable);
if (entry->stream.file_info.readable)
streams_input_stream_drop_borrow(entry->stream.read_stream);
if (entry->stream.file_info.writable) {
if (entry->stream.pollable_is_initialized)
poll_pollable_drop_own(entry->stream.pollable);
if (entry->stream.file_info.writable)
streams_output_stream_drop_borrow(entry->stream.write_stream);
}

// Open a new stream with the right offset
if (entry->stream.file_info.readable) {
Expand Down Expand Up @@ -106,9 +107,10 @@ off_t __lseek(int fildes, off_t offset, int whence) {

// Update output_stream.stream with the new stream
entry->stream.write_stream = streams_borrow_output_stream(new_stream);
entry->stream.pollable_is_initialized = false;
}

entry->stream.read_pollable_is_initialized = false;
entry->stream.write_pollable_is_initialized = false;
// Update offset
entry->stream.offset = offset_to_use;
} else {
Expand Down
4 changes: 3 additions & 1 deletion libc-bottom-half/cloudlibc/src/libc/unistd/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ssize_t read(int fildes, void *buf, size_t nbyte) {
// Get the input stream
filesystem_error_code_t error_code;
streams_own_input_stream_t input_stream_own;

ok = filesystem_method_descriptor_read_via_stream(entry->file.file_handle,
0,
&input_stream_own,
Expand Down Expand Up @@ -62,7 +63,8 @@ ssize_t read(int fildes, void *buf, size_t nbyte) {
// for this file
new_entry.tag = DESCRIPTOR_TABLE_ENTRY_FILE_STREAM;
new_entry.stream.read_stream = input_stream;
new_entry.stream.pollable_is_initialized = false;
new_entry.stream.read_pollable_is_initialized = false;
new_entry.stream.write_pollable_is_initialized = false;
new_entry.stream.offset = 0;
new_entry.stream.file_info.readable = entry->file.readable;
new_entry.stream.file_info.writable = entry->file.writable;
Expand Down
13 changes: 7 additions & 6 deletions libc-bottom-half/cloudlibc/src/libc/unistd/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) {
return -1;
}
output_stream = entry->stream.write_stream;
if (!entry->stream.pollable_is_initialized) {
pollable = entry->stream.pollable = streams_method_output_stream_subscribe(output_stream);
entry->stream.pollable_is_initialized = true;
if (!entry->stream.write_pollable_is_initialized) {
pollable = entry->stream.write_pollable = streams_method_output_stream_subscribe(output_stream);
entry->stream.write_pollable_is_initialized = true;
} else
pollable = entry->stream.pollable;
pollable = entry->stream.write_pollable;
} else {
errno = EBADF;
return -1;
Expand Down Expand Up @@ -114,8 +114,9 @@ ssize_t write(int fildes, const void *buf, size_t nbyte) {
}
new_entry.stream.read_stream = streams_borrow_input_stream(read_stream);
}
new_entry.stream.pollable = pollable;
new_entry.stream.pollable_is_initialized = true;
new_entry.stream.write_pollable = pollable;
new_entry.stream.read_pollable_is_initialized = false;
new_entry.stream.write_pollable_is_initialized = true;
new_entry.stream.write_stream = output_stream;
new_entry.stream.offset = contents.len;
new_entry.stream.file_info.readable = entry->file.readable;
Expand Down
8 changes: 5 additions & 3 deletions libc-bottom-half/headers/private/wasi/descriptor_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ typedef struct {
streams_borrow_output_stream_t write_stream;
// Current position in stream, relative to the beginning of the *file*, measured in bytes
off_t offset;
// Used for checking readiness to write to stream. Lazily initialized.
streams_own_pollable_t pollable;
bool pollable_is_initialized;
// Used for checking readiness to read/write to stream. Lazily initialized
streams_own_pollable_t read_pollable;
streams_own_pollable_t write_pollable;
bool read_pollable_is_initialized;
bool write_pollable_is_initialized;
// When the stream is closed, the caller should
// replace this entry in the table with the file handle
file_t file_info;
Expand Down
8 changes: 6 additions & 2 deletions libc-bottom-half/headers/private/wasi/file_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ static bool init_stdin() {
entry.tag = DESCRIPTOR_TABLE_ENTRY_FILE_STREAM;
entry.stream.read_stream = streams_borrow_input_stream(stdin_get_stdin());
entry.stream.offset = 0;
entry.stream.read_pollable_is_initialized = false;
entry.stream.write_pollable_is_initialized = false;
entry.stream.file_info.readable = true;
entry.stream.file_info.writable = false;
// entry.stream.file_info.file_handle is uninitialized, but it will never be used
Expand All @@ -81,7 +83,8 @@ static bool init_stdout() {
entry.tag = DESCRIPTOR_TABLE_ENTRY_FILE_STREAM;
entry.stream.write_stream = streams_borrow_output_stream(stdout_get_stdout());
entry.stream.offset = 0;
entry.stream.pollable = streams_method_output_stream_subscribe(entry.stream.write_stream);
entry.stream.read_pollable_is_initialized = false;
entry.stream.write_pollable_is_initialized = false;
entry.stream.file_info.readable = false;
entry.stream.file_info.writable = true;
// entry.stream.file_info.file_handle is uninitialized, but it will never be used
Expand All @@ -95,7 +98,8 @@ static bool init_stderr() {
entry.tag = DESCRIPTOR_TABLE_ENTRY_FILE_STREAM;
entry.stream.write_stream = streams_borrow_output_stream(stderr_get_stderr());
entry.stream.offset = 0;
entry.stream.pollable = streams_method_output_stream_subscribe(entry.stream.write_stream);
entry.stream.read_pollable_is_initialized = false;
entry.stream.write_pollable_is_initialized = false;
entry.stream.file_info.readable = false;
entry.stream.file_info.writable = true;
// entry.stream.file_info.file_handle is uninitialized, but it will never be used
Expand Down
6 changes: 4 additions & 2 deletions libc-bottom-half/sources/__wasilibc_fd_renumber.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ int close(int fd) {
drop_directory_stream(entry.directory_stream_info.directory_stream);
break;
case DESCRIPTOR_TABLE_ENTRY_FILE_STREAM:
if (entry.stream.pollable_is_initialized)
poll_pollable_drop_own(entry.stream.pollable);
if (entry.stream.read_pollable_is_initialized)
poll_pollable_drop_own(entry.stream.read_pollable);
if (entry.stream.write_pollable_is_initialized)
poll_pollable_drop_own(entry.stream.write_pollable);
if (entry.stream.file_info.readable)
streams_input_stream_drop_borrow(entry.stream.read_stream);
if (entry.stream.file_info.writable)
Expand Down
Loading