Fix isatty in WASI. (#3696)

WASI doesn't have an `isatty` ioctl or syscall, so wasi-libc's `isatty`
implementation uses the file descriptor type and rights to determine if
the file descriptor is likely to be a tty. The real fix here will be to
add an `isatty` call to WASI. But for now, have Wasmtime set the
filetype and rights for file descriptors so that wasi-libc's `isatty`
works as expected.
This commit is contained in:
Dan Gohman
2022-01-24 11:45:16 -08:00
committed by GitHub
parent b1ad02e43a
commit 5fc01bafc7
8 changed files with 74 additions and 8 deletions

View File

@@ -71,15 +71,31 @@ impl WasiCtx {
}
pub fn set_stdin(&mut self, f: Box<dyn WasiFile>) {
self.insert_file(0, f, FileCaps::all());
let rights = Self::stdio_rights(&*f);
self.insert_file(0, f, rights);
}
pub fn set_stdout(&mut self, f: Box<dyn WasiFile>) {
self.insert_file(1, f, FileCaps::all());
let rights = Self::stdio_rights(&*f);
self.insert_file(1, f, rights);
}
pub fn set_stderr(&mut self, f: Box<dyn WasiFile>) {
self.insert_file(2, f, FileCaps::all());
let rights = Self::stdio_rights(&*f);
self.insert_file(2, f, rights);
}
fn stdio_rights(f: &dyn WasiFile) -> FileCaps {
let mut rights = FileCaps::all();
// If `f` is a tty, restrict the `tell` and `seek` capabilities, so
// that wasi-libc's `isatty` correctly detects the file descriptor
// as a tty.
if f.isatty() {
rights &= !(FileCaps::TELL | FileCaps::SEEK);
}
rights
}
pub fn push_preopened_dir(

View File

@@ -34,6 +34,7 @@ pub trait WasiFile: Send + Sync {
async fn seek(&self, pos: std::io::SeekFrom) -> Result<u64, Error>; // file op that generates a new stream from a file will supercede this
async fn peek(&self, buf: &mut [u8]) -> Result<u64, Error>; // read op
async fn num_ready_bytes(&self) -> Result<u64, Error>; // read op
fn isatty(&self) -> bool;
async fn readable(&self) -> Result<(), Error>;
async fn writable(&self) -> Result<(), Error>;

View File

@@ -180,6 +180,9 @@ impl<R: Read + Any + Send + Sync> WasiFile for ReadPipe<R> {
async fn num_ready_bytes(&self) -> Result<u64, Error> {
Ok(0)
}
fn isatty(&self) -> bool {
false
}
async fn readable(&self) -> Result<(), Error> {
Err(Error::badf())
}
@@ -336,6 +339,9 @@ impl<W: Write + Any + Send + Sync> WasiFile for WritePipe<W> {
async fn num_ready_bytes(&self) -> Result<u64, Error> {
Ok(0)
}
fn isatty(&self) -> bool {
false
}
async fn readable(&self) -> Result<(), Error> {
Err(Error::badf())
}