about summary refs log tree commit diff
path: root/library/std/src/sys/wasi
AgeCommit message (Collapse)AuthorLines
2023-03-06Implement read_buf for a few more typesTomasz Miąsko-3/+23
Implement read_buf for TcpStream, Stdin, StdinLock, ChildStdout, ChildStderr (and internally for AnonPipe, Handle, Socket), so that it skips buffer initialization. The other provided methods like read_to_string and read_to_end are implemented in terms of read_buf and so benefit from the optimization as well. This commit also implements read_vectored and is_read_vectored where applicable.
2023-02-09Allow wasi-libc to initialize its environment variables lazily.Dan Gohman-1/+7
Use `__wasilibc_get_environ()` to read the environment variable list from wasi-libc instead of using `environ`. `environ` is a global variable which effectively requires wasi-libc to initialize the environment variables eagerly, and `__wasilibc_get_environ()` is specifically designed to be an alternative that lets wasi-libc intiailize its environment variables lazily. This should have the side effect of fixing at least some of the cases of #107635.
2022-12-14std: use a more efficient `Once` on platforms without threadsjoboet-0/+2
2022-11-24feat: implement TcpStream shutdown for wasm32-wasiHarald Hoyer-2/+8
Signed-off-by: Harald Hoyer <harald@profian.com>
2022-10-15Add `IsTerminal` trait to determine if a descriptor or handle is a terminalJosh Triplett-0/+6
The UNIX and WASI implementations use `isatty`. The Windows implementation uses the same logic the `atty` crate uses, including the hack needed to detect msys terminals. Implement this trait for `File` and for `Stdin`/`Stdout`/`Stderr` and their locked counterparts on all platforms. On UNIX and WASI, implement it for `BorrowedFd`/`OwnedFd`. On Windows, implement it for `BorrowedHandle`/`OwnedHandle`. Based on https://github.com/rust-lang/rust/pull/91121 Co-authored-by: Matt Wilkinson <mattwilki17@gmail.com>
2022-10-14Rollup merge of #102847 - joshtriplett:bugfix-impl-fd-traits-for-io-types, ↵Dylan DPC-49/+1
r=m-ou-se impl AsFd and AsRawFd for io::{Stdin, Stdout, Stderr}, not the sys versions https://github.com/rust-lang/rust/pull/100892 implemented AsFd for the sys versions, rather than for the public types. Change the implementations to apply to the public types.
2022-10-11fix: return type of single-threaded dummy lock must be droppableAndrew Brown-2/+2
2022-10-10Implement `env_lock` with `RwLock`Andrew Brown-12/+23
Copying the approach of the Unix target, this change uses the standard `RwLock` to protect against concurrent access of libc's environment. This locking is only enabled when WebAssembly's `atomics` feature is also enabled.
2022-10-10Allow compiling the `wasm32-wasi` std library with atomicsAndrew Brown-3/+11
The issue #102157 demonstrates how currently the `-Z build-std` option will fail when re-compiling the standard library with `RUSTFLAGS` like `RUSTFLAGS="-C target-feature=+atomics,+bulk-memory -C link-args=--shared-memory"`. This change attempts to resolve those build issues by depending on the the WebAssembly `futex` module and providing an implementation for `env_lock`. Fixes #102157.
2022-10-10Consolidate AsFd instances for stdio types into `library/std/src/os/fd/owned.rs`Josh Triplett-49/+1
2022-10-09impl AsFd for io::{Stdin, Stdout, Stderr}, not the sys versionsJosh Triplett-3/+3
https://github.com/rust-lang/rust/pull/100892 implemented AsFd for the sys versions, rather than for the public types. Change the implementations to apply to the public types.
2022-10-09Auto merge of #93668 - SUPERCILEX:path_alloc, r=joshtriplettbors-69/+66
Reduce CString allocations in std as much as possible Currently, every operation involving paths in `fs` allocates memory to hold the path before sending it through the syscall. This PR instead uses a stack allocation (chosen size is somewhat arbitrary) when the path is short before falling back to heap allocations for long paths. Benchmarks show that the stack allocation is ~2x faster for short paths: ``` test sys::unix::fd::tests::bench_heap_path_alloc ... bench: 34 ns/iter (+/- 2) test sys::unix::fd::tests::bench_stack_path_alloc ... bench: 15 ns/iter (+/- 1) ``` For long paths, I couldn't find any measurable difference. --- I'd be surprised if I was the first to think of this, so I didn't fully flush out the PR. If this change is desirable, I'll make use of `run_with_cstr` across all platforms in every fs method (currently just unix open for testing). I also added an `impl From<FromBytesWithNulError>` which is presumably a no-no (or at least needs to be done in another PR). --- Also see https://github.com/nix-rust/nix/pull/1655 with a bunch of discussion where I'm doing something similar.
2022-10-04Auto merge of #101768 - sunfishcode:sunfishcode/wasi-stdio-lock-asfd, ↵bors-0/+27
r=joshtriplett Add `AsFd` implementations for stdio lock types on WASI. This mirrors the implementations on Unix platforms, and also mirrors the existing `AsRawFd` impls. This is similar to #100892, but is for the `*Lock` types.
2022-10-03Reduce CString allocations in std as much as possibleAlex Saveau-69/+66
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-10-03Add SAFETY comments for AsFd implementations on stdin/stdout/stderrJosh Triplett-0/+3
2022-10-03Add stability attributes.Dan Gohman-3/+6
2022-10-01Error instead of panicking when setting file times if the passed ↵beetrees-8/+15
`SystemTime` doesn't fit into the required type
2022-09-22Add `AsFd` implementations for stdio lock types on WASI.Dan Gohman-0/+21
This mirrors the implementations on Unix platforms, and also mirrors the existing `AsRawFd` impls. This is similar to #100892, but is for the `*Lock` types.
2022-08-31Rollup merge of #100892 - sunfishcode:wasi-stdio-asfd, r=joshtriplettYuki Okushi-1/+22
Add `AsFd` implementations for stdio types on WASI. This mirrors the implementations on Unix platforms, and also mirrors the existing `AsRawFd` impls.
2022-08-22Add `AsFd` implementations for stdio types on WASI.Dan Gohman-1/+22
This mirrors the implementations on Unix platforms, and also mirrors the existing `AsRawFd` impls.
2022-08-18Address reviewer commentsNick Cameron-1/+1
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2022-08-05non-linux platformsNick Cameron-3/+3
Signed-off-by: Nick Cameron <nrc@ncameron.org>
2022-08-01Auto merge of #98246 - joshtriplett:times, r=m-ou-sebors-0/+29
Support setting file accessed/modified timestamps Add `struct FileTimes` to contain the relevant file timestamps, since most platforms require setting all of them at once. (This also allows for future platform-specific extensions such as setting creation time.) Add `File::set_file_time` to set the timestamps for a `File`. Implement the `sys` backends for UNIX, macOS (which needs to fall back to `futimes` before macOS 10.13 because it lacks `futimens`), Windows, and WASI.
2022-07-30Remove socklen_t from platforms where it's no longer usedLinus Färnstrand-2/+0
2022-07-15Support setting file accessed/modified timestampsJosh Triplett-0/+29
Add `struct FileTimes` to contain the relevant file timestamps, since most platforms require setting all of them at once. (This also allows for future platform-specific extensions such as setting creation time.) Add `File::set_file_time` to set the timestamps for a `File`. Implement the `sys` backends for UNIX, macOS (which needs to fall back to `futimes` before macOS 10.13 because it lacks `futimens`), Windows, and WASI.
2022-05-09Use Rust 2021 prelude in std itself.Mara Bos-1/+0
2022-03-22Move std::sys::{mutex, condvar, rwlock} to std::sys::locks.Mara Bos-6/+2
2022-02-13make Instant::{duration_since, elapsed, sub} saturating and remove workaroundsThe8472-8/+0
This removes all mutex/atomics based workarounds for non-monotonic clocks and makes the previously panicking methods saturating instead. Effectively this moves the monotonization from `Instant` construction to the comparisons. This has some observable effects, especially on platforms without monotonic clocks: * Incorrectly ordered Instant comparisons no longer panic. This may hide some programming errors until someone actually looks at the resulting `Duration` * `checked_duration_since` will now return `None` in more cases. Previously it only happened when one compared instants obtained in the wrong order or manually created ones. Now it also does on backslides. The upside is reduced complexity and lower overhead of `Instant::now`.
2022-02-04Hide Repr details from io::Error, and rework `io::Error::new_const`.Thom Chiovoloni-2/+2
2022-01-28wasi: enable TcpListener and TcpStreamHarald Hoyer-13/+57
With the addition of `sock_accept()` to snapshot1, simple networking via a passed `TcpListener` is possible. This patch implements the basics to make a simple server work. Signed-off-by: Harald Hoyer <harald@profian.com>
2022-01-28wasi: update to wasi 0.11.0Harald Hoyer-24/+29
To make use of `sock_accept()`, update the wasi crate to `0.11.0`. Signed-off-by: Harald Hoyer <harald@profian.com>
2022-01-19Fix CVE-2022-21658 for WASIAlex Crichton-8/+63
2021-11-02more efficent File::read_buf impl for windows and unixDrMeepster-1/+5
2021-09-28Rename `std::thread::available_onccurrency` to ↵Yoshua Wuyts-1/+1
`std::thread::available_parallelism`
2021-08-30add `TcpStream::set_linger` and `TcpStream::linger`ibraheemdev-0/+8
2021-08-19Factor out a common `RawFd`/`AsRawFd`/etc for Unix and WASI.Dan Gohman-35/+69
2021-08-19I/O safety.Dan Gohman-103/+228
Introduce `OwnedFd` and `BorrowedFd`, and the `AsFd` trait, and implementations of `AsFd`, `From<OwnedFd>` and `From<T> for OwnedFd` for relevant types, along with Windows counterparts for handles and sockets. Tracking issue: - <https://github.com/rust-lang/rust/issues/87074> RFC: - <https://github.com/rust-lang/rfcs/blob/master/text/3128-io-safety.md>
2021-08-18Rollup merge of #88012 - sunfishcode:sunfishcode/wasi-raw-fd-c-int, ↵Guillaume Gomez-47/+69
r=alexcrichton Change WASI's `RawFd` from `u32` to `c_int` (`i32`). WASI previously used `u32` as its `RawFd` type, since its "file descriptors" are unsigned table indices, and there's no fundamental reason why WASI can't have more than 2^31 handles. However, this creates myriad little incompability problems with code that also supports Unix platforms, where `RawFd` is `c_int`. While WASI isn't a Unix, it often shares code with Unix, and this difference made such shared code inconvenient. #87329 is the most recent example of such code. So, switch WASI to use `c_int`, which is `i32`. This will mean that code intending to support WASI should ideally avoid assuming that negative file descriptors are invalid, even though POSIX itself says that file descriptors are never negative. This is a breaking change, but `RawFd` is considerd an experimental feature in [the documentation]. [the documentation]: https://doc.rust-lang.org/stable/std/os/wasi/io/type.RawFd.html r? `@alexcrichton`
2021-08-13Change WASI's `RawFd` from `u32` to `c_int` (`i32`).Dan Gohman-47/+69
WASI previously used `u32` as its `RawFd` type, since its "file descriptors" are unsigned table indices, and there's no fundamental reason why WASI can't have more than 2^31 handles. However, this creates myriad little incompability problems with code that also supports Unix platforms, where `RawFd` is `c_int`. While WASI isn't a Unix, it often shares code with Unix, and this difference made such shared code inconvenient. #87329 is the most recent example of such code. So, switch WASI to use `c_int`, which is `i32`. This will mean that code intending to support WASI should ideally avoid assuming that negative file descriptors are invalid, even though POSIX itself says that file descriptors are never negative. This is a breaking change, but `RawFd` is considerd an experimental feature in [the documentation]. [the documentation]: https://doc.rust-lang.org/stable/std/os/wasi/io/type.RawFd.html
2021-08-02Rollup merge of #86509 - CDirkx:os_str, r=m-ou-seYuki Okushi-1/+2
Move `os_str_bytes` to `sys::unix` Followup to #84967, with `OsStrExt` and `OsStringExt` moved out of `sys_common`, there is no reason anymore for `os_str_bytes` to live in `sys_common` and not in sys. This pr moves it to the location `sys::unix::os_str` and reuses the code on other platforms via `#[path]` (as is common in `sys`) instead of importing.
2021-07-05Make `getenv` return an Option instead of a ResultAris Merchant-5/+4
2021-07-03Move `os_str_bytes` to `sys::unix` and reuse it on other platforms.Christiaan Dirkx-1/+2
2021-07-02Auto merge of #85746 - m-ou-se:io-error-other, r=joshtriplettbors-4/+5
Redefine `ErrorKind::Other` and stop using it in std. This implements the idea I shared yesterday in the libs meeting when we were discussing how to handle adding new `ErrorKind`s to the standard library: This redefines `Other` to be for *user defined errors only*, and changes all uses of `Other` in the standard library to a `#[doc(hidden)]` and permanently `#[unstable]` `ErrorKind` that users can not match on. This ensures that adding `ErrorKind`s at a later point in time is not a breaking change, since the user couldn't match on these errors anyway. This way, we use the `#[non_exhaustive]` property of the enum in a more effective way. Open questions: - How do we check this change doesn't cause too much breakage? Will a crate run help and be enough? - How do we ensure we don't accidentally start using `Other` again in the standard library? We don't have a `pub(not crate)` or `#[deprecated(in this crate only)]`. cc https://github.com/rust-lang/rust/pull/79965 cc `@rust-lang/libs` `@ijackson` r? `@dtolnay`
2021-06-21Use `Unsupported` on platforms where `available_concurrency` is not implemented.Christiaan Dirkx-4/+1
2021-06-21Move `available_concurrency` implementation to `sys`Christiaan Dirkx-0/+8
2021-06-15Rename ErrorKind::Unknown to Uncategorized.Mara Bos-4/+5
2021-06-15Redefine `ErrorKind::Other` and stop using it in std.Mara Bos-4/+4
2021-05-19Move the implementation of `Path::exists` to `sys_common::fs` so platforms ↵Chris Denton-1/+1
can specialize it Windows implementation of `fs::try_exists`
2021-05-03Move `std::sys::wasi::ext` to `std::os::wasi`Christiaan Dirkx-796/+0
2021-05-02Use ErrorKind::OutOfMemory in unix, windows, and wasiKornel-0/+1