Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fcntl doesn't implement O_NONBLOCK for wasi:socket handles #536

Open
pavelsavara opened this issue Sep 16, 2024 · 2 comments
Open

fcntl doesn't implement O_NONBLOCK for wasi:socket handles #536

pavelsavara opened this issue Sep 16, 2024 · 2 comments

Comments

@pavelsavara
Copy link

fcntl doesn't implement O_NONBLOCK for wasi:socket handles

See

int fcntl(int fildes, int cmd, ...) {
switch (cmd) {
case F_GETFD:
// Act as if the close-on-exec flag is always set.
return FD_CLOEXEC;
case F_SETFD:
// The close-on-exec flag is ignored.
return 0;
case F_GETFL: {
// Obtain the flags and the rights of the descriptor.
__wasi_fdstat_t fds;
__wasi_errno_t error = __wasi_fd_fdstat_get(fildes, &fds);
if (error != 0) {
errno = error;
return -1;
}
// Roughly approximate the access mode by converting the rights.
int oflags = fds.fs_flags;
if ((fds.fs_rights_base &
(__WASI_RIGHTS_FD_READ | __WASI_RIGHTS_FD_READDIR)) != 0) {
if ((fds.fs_rights_base & __WASI_RIGHTS_FD_WRITE) != 0)
oflags |= O_RDWR;
else
oflags |= O_RDONLY;
} else if ((fds.fs_rights_base & __WASI_RIGHTS_FD_WRITE) != 0) {
oflags |= O_WRONLY;
} else {
oflags |= O_SEARCH;
}
return oflags;
}
case F_SETFL: {
// Set new file descriptor flags.
va_list ap;
va_start(ap, cmd);
int flags = va_arg(ap, int);
va_end(ap);
__wasi_fdflags_t fs_flags = flags & 0xfff;
__wasi_errno_t error =
__wasi_fd_fdstat_set_flags(fildes, fs_flags);
if (error != 0) {
errno = error;
return -1;
}
return 0;
}
default:
errno = EINVAL;
return -1;
}

@dicej
Copy link
Contributor

dicej commented Sep 16, 2024

We do already support both blocking and non-blocking sockets (e.g.

#define SOCK_NONBLOCK (0x00004000)
and
bool blocking = (type & SOCK_NONBLOCK) == 0;
), so presumably adding O_NONBLOCK support for toggling that should be pretty trivial.

@dicej
Copy link
Contributor

dicej commented Sep 23, 2024

BTW, we do already support setting FIONBIO on sockets via ioctl. I don't think there's a way to read the current state yet, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants