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

Fixed WASI isatty #1213

Merged
merged 5 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 15 additions & 0 deletions lib/wasi-tests/tests/wasitests/isatty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// !!! THIS IS A GENERATED FILE !!!
// ANY MANUAL EDITS MAY BE OVERWRITTEN AT ANY TIME
// Files autogenerated with cargo build (build/wasitests.rs).

#[test]
fn test_isatty() {
assert_wasi_output!(
"../../wasitests/isatty.wasm",
"isatty",
vec![],
vec![],
vec![],
"../../wasitests/isatty.out"
);
}
1 change: 1 addition & 0 deletions lib/wasi-tests/tests/wasitests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod file_metadata;
mod fs_sandbox_test;
mod fseek;
mod hello;
mod isatty;
mod mapdir;
mod path_link;
mod path_rename;
Expand Down
3 changes: 3 additions & 0 deletions lib/wasi-tests/wasitests/isatty.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
stdin: 1
stdout: 1
stderr: 1
20 changes: 20 additions & 0 deletions lib/wasi-tests/wasitests/isatty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// We don't have access to libc, so we just use isatty
// as an external function
// use libc::isatty;

extern "C" {
pub fn isatty(fd: i32) -> i32;
}

fn main() {
#[cfg(target = "wasi")] {
println!("stdin: {}", unsafe { isatty(0) });
println!("stdout: {}", unsafe { isatty(1) });
println!("stderr: {}", unsafe { isatty(2) });
}
#[cfg(not(target = "wasi"))] {
println!("stdin: 1");
println!("stdout: 1");
println!("stderr: 1");
}
}
Binary file added lib/wasi-tests/wasitests/isatty.wasm
Binary file not shown.
22 changes: 19 additions & 3 deletions lib/wasi/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,12 +1045,28 @@ impl WasiFs {

pub fn fdstat(&self, fd: __wasi_fd_t) -> Result<__wasi_fdstat_t, __wasi_errno_t> {
match fd {
__WASI_STDOUT_FILENO => {
__WASI_STDIN_FILENO => {
return Ok(__wasi_fdstat_t {
fs_filetype: __WASI_FILETYPE_CHARACTER_DEVICE,
fs_flags: 0,
fs_rights_base: ALL_RIGHTS,
fs_rights_inheriting: ALL_RIGHTS,
fs_rights_base: STDIN_DEFAULT_RIGHTS,
fs_rights_inheriting: 0,
})
}
__WASI_STDOUT_FILENO => {
return Ok(__wasi_fdstat_t {
fs_filetype: __WASI_FILETYPE_CHARACTER_DEVICE,
fs_flags: __WASI_FDFLAG_APPEND,
fs_rights_base: STDOUT_DEFAULT_RIGHTS,
fs_rights_inheriting: 0,
})
}
__WASI_STDERR_FILENO => {
return Ok(__wasi_fdstat_t {
fs_filetype: __WASI_FILETYPE_CHARACTER_DEVICE,
fs_flags: __WASI_FDFLAG_APPEND,
fs_rights_base: STDERR_DEFAULT_RIGHTS,
fs_rights_inheriting: 0,
})
}
_ => (),
Expand Down
9 changes: 8 additions & 1 deletion lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,14 @@ pub fn fd_write(
iovs_len: u32,
nwritten: WasmPtr<u32>,
) -> __wasi_errno_t {
debug!("wasi::fd_write: fd={}", fd);
// If we are writing to stdout or stderr
// we skip debug to not pollute the stdout/err
// and do debugging happily after :)
if fd != __WASI_STDOUT_FILENO && fd != __WASI_STDERR_FILENO {
debug!("wasi::fd_write: fd={}", fd);
} else {
trace!("wasi::fd_write: fd={}", fd);
}
let (memory, state) = get_memory_and_wasi_state(ctx, 0);
let iovs_arr_cell = wasi_try!(iovs.deref(memory, 0, iovs_len));
let nwritten_cell = wasi_try!(nwritten.deref(memory));
Expand Down