Skip to content

Commit f1d44da

Browse files
authored
Miscellaneous cleanups. (#551)
* Miscellaneous cleanups. Avoid using the `linux_raw` flag to determine public APIs, make `mremap` available on emscripten, make `getrandom` available on android, and other minor cleanups. * Fix compilation on Emscripten. * More miscellaneous cleanups. * rustfmt * Handle `EINVAL` in the `mlock` test. * More miscellaneous fixes. * Use the new OS groups in the new kqueue and ports APIs. * Update termios for the new OS groups. * rustfmt * Fix compilation on fuchsia. * chore: Release rustix version 0.37.0-alpha.1
1 parent 6a8341a commit f1d44da

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+499
-981
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rustix"
3-
version = "0.37.0-alpha.0"
3+
version = "0.37.0-alpha.1"
44
authors = [
55
"Dan Gohman <dev@sunfishcode.online>",
66
"Jakub Konka <kubkon@jakubkonka.com>",
@@ -39,7 +39,7 @@ once_cell = { version = "1.5.2", optional = true }
3939
# libc backend can be selected via adding `--cfg=rustix_use_libc` to
4040
# `RUSTFLAGS` or enabling the `use-libc` cargo feature.
4141
[target.'cfg(all(not(rustix_use_libc), not(miri), target_os = "linux", any(target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"), all(target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "powerpc64", target_arch = "riscv64", target_arch = "mips", target_arch = "mips64")))))'.dependencies]
42-
linux-raw-sys = { version = "0.2.1", default-features = false, features = ["general", "errno", "ioctl", "no_std"] }
42+
linux-raw-sys = { version = "0.3.0", default-features = false, features = ["general", "errno", "ioctl", "no_std"] }
4343
libc_errno = { package = "errno", version = "0.3.0", default-features = false, optional = true }
4444
libc = { version = "0.2.133", features = ["extra_traits"], optional = true }
4545

@@ -56,7 +56,7 @@ libc = { version = "0.2.133", features = ["extra_traits"] }
5656
# Some syscalls do not have libc wrappers, such as in `io_uring`. For these,
5757
# the libc backend uses the linux-raw-sys ABI and `libc::syscall`.
5858
[target.'cfg(all(any(target_os = "android", target_os = "linux"), any(rustix_use_libc, miri, not(all(target_os = "linux", any(target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"), all(target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "powerpc64", target_arch = "riscv64", target_arch = "mips", target_arch = "mips64"))))))))'.dependencies]
59-
linux-raw-sys = { version = "0.2.1", default-features = false, features = ["general", "no_std"] }
59+
linux-raw-sys = { version = "0.3.0", default-features = false, features = ["general", "no_std"] }
6060

6161
# For the libc backend on Windows, use the Winsock2 API in windows-sys.
6262
[target.'cfg(windows)'.dependencies.windows-sys]

build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ fn use_feature(feature: &str) {
218218

219219
/// Test whether the rustc at `var("RUSTC")` supports the given feature.
220220
fn has_feature(feature: &str) -> bool {
221-
can_compile(&format!(
221+
can_compile(format!(
222222
"#![allow(stable_features)]\n#![feature({})]",
223223
feature
224224
))

src/backend/libc/fs/inotify.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ pub fn inotify_add_watch<P: crate::path::Arg>(
9696
flags: WatchFlags,
9797
) -> io::Result<i32> {
9898
let path = path.as_cow_c_str().unwrap();
99-
// Safety: The fd and path we are passing is guranteed valid by the type system.
99+
// Safety: The fd and path we are passing is guranteed valid by the type
100+
// system.
100101
unsafe {
101102
ret_c_int(c::inotify_add_watch(
102103
borrowed_fd(inot),
@@ -109,9 +110,9 @@ pub fn inotify_add_watch<P: crate::path::Arg>(
109110
/// `inotify_rm_watch(self, wd)`-Removes a watch from this inotify
110111
///
111112
/// The watch descriptor provided should have previously been returned
112-
/// by [`Self::add_watch()`] and not previously have been removed.
113+
/// by [`inotify_add_watch`] and not previously have been removed.
113114
#[doc(alias = "inotify_rm_watch")]
114-
pub fn inotify_remove_watch<P: crate::path::Arg>(inot: BorrowedFd<'_>, wd: i32) -> io::Result<()> {
115+
pub fn inotify_remove_watch(inot: BorrowedFd<'_>, wd: i32) -> io::Result<()> {
115116
// Android's `inotify_rm_watch` takes u32 despite `inotify_add_watch` is i32.
116117
#[cfg(target_os = "android")]
117118
let wd = wd as u32;

src/backend/libc/fs/syscalls.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,8 @@ pub(crate) fn fallocate(
11371137
};
11381138
unsafe {
11391139
if c::fcntl(borrowed_fd(fd), c::F_PREALLOCATE, &store) == -1 {
1140+
// Unable to allocate contiguous disk space; attempt to allocate
1141+
// non-contiguously.
11401142
store.fst_flags = c::F_ALLOCATEALL;
11411143
let _ = ret_c_int(c::fcntl(borrowed_fd(fd), c::F_PREALLOCATE, &store))?;
11421144
}

src/backend/libc/io/epoll.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
//! # #[cfg(feature = "net")]
1212
//! # fn main() -> std::io::Result<()> {
1313
//! use io_lifetimes::AsFd;
14-
//! use rustix::io::epoll;
15-
//! use rustix::io::{ioctl_fionbio, read, write};
14+
//! use rustix::io::{epoll, ioctl_fionbio, read, write};
1615
//! use rustix::net::{
1716
//! accept, bind_v4, listen, socket, AddressFamily, Ipv4Addr, Protocol, SocketAddrV4,
1817
//! SocketType,
@@ -46,8 +45,13 @@
4645
//! // register to be notified when it's ready to write to.
4746
//! let conn_sock = accept(&listen_sock)?;
4847
//! ioctl_fionbio(&conn_sock, true)?;
49-
//! epoll::epoll_add(&epoll, &conn_sock, next_id, epoll::EventFlags::OUT | epoll::EventFlags::ET)?;
50-
//!
48+
//! epoll::epoll_add(
49+
//! &epoll,
50+
//! &conn_sock,
51+
//! next_id,
52+
//! epoll::EventFlags::OUT | epoll::EventFlags::ET,
53+
//! )?;
54+
//!
5155
//! // Keep track of the socket.
5256
//! sockets.insert(next_id, conn_sock);
5357
//! next_id += 1;

src/backend/libc/io/syscalls.rs

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use super::super::offset::{libc_preadv, libc_pwritev};
1212
#[cfg(all(target_os = "linux", target_env = "gnu"))]
1313
use super::super::offset::{libc_preadv2, libc_pwritev2};
1414
use crate::fd::{AsFd, BorrowedFd, OwnedFd, RawFd};
15-
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
15+
#[cfg(bsd)]
16+
use crate::io::kqueue::Event;
17+
#[cfg(solarish)]
1618
use crate::io::port::Event;
1719
#[cfg(not(any(target_os = "aix", target_os = "wasi")))]
1820
use crate::io::DupFlags;
@@ -29,18 +31,6 @@ use core::ptr;
2931
#[cfg(all(feature = "fs", feature = "net"))]
3032
use libc_errno::errno;
3133

32-
#[cfg(any(
33-
target_os = "macos",
34-
target_os = "ios",
35-
target_os = "tvos",
36-
target_os = "watchos",
37-
target_os = "freebsd",
38-
target_os = "dragonfly",
39-
target_os = "openbsd",
40-
target_os = "netbsd"
41-
))]
42-
use crate::io::kqueue::Event;
43-
4434
pub(crate) fn read(fd: BorrowedFd<'_>, buf: &mut [u8]) -> io::Result<usize> {
4535
let nread = unsafe {
4636
ret_ssize_t(c::read(
@@ -464,30 +454,12 @@ pub(crate) fn ioctl_tiocnxcl(fd: BorrowedFd) -> io::Result<()> {
464454
unsafe { ret(c::ioctl(borrowed_fd(fd), c::TIOCNXCL as _)) }
465455
}
466456

467-
#[cfg(any(
468-
target_os = "macos",
469-
target_os = "ios",
470-
target_os = "tvos",
471-
target_os = "watchos",
472-
target_os = "freebsd",
473-
target_os = "dragonfly",
474-
target_os = "openbsd",
475-
target_os = "netbsd"
476-
))]
457+
#[cfg(bsd)]
477458
pub(crate) fn kqueue() -> io::Result<OwnedFd> {
478459
unsafe { ret_owned_fd(c::kqueue()) }
479460
}
480461

481-
#[cfg(any(
482-
target_os = "macos",
483-
target_os = "ios",
484-
target_os = "tvos",
485-
target_os = "watchos",
486-
target_os = "freebsd",
487-
target_os = "dragonfly",
488-
target_os = "openbsd",
489-
target_os = "netbsd"
490-
))]
462+
#[cfg(bsd)]
491463
pub(crate) unsafe fn kevent(
492464
kq: BorrowedFd<'_>,
493465
changelist: &[Event],
@@ -582,12 +554,12 @@ pub unsafe fn vmsplice(
582554
.map(|spliced| spliced as usize)
583555
}
584556

585-
#[cfg(any(target_os = "illumos", target_os = "solaris"))]
557+
#[cfg(solarish)]
586558
pub(crate) fn port_create() -> io::Result<OwnedFd> {
587559
unsafe { ret_owned_fd(c::port_create()) }
588560
}
589561

590-
#[cfg(any(target_os = "illumos", target_os = "solaris"))]
562+
#[cfg(solarish)]
591563
pub(crate) unsafe fn port_associate(
592564
port: BorrowedFd<'_>,
593565
source: c::c_int,
@@ -604,7 +576,7 @@ pub(crate) unsafe fn port_associate(
604576
))
605577
}
606578

607-
#[cfg(any(target_os = "illumos", target_os = "solaris"))]
579+
#[cfg(solarish)]
608580
pub(crate) unsafe fn port_dissociate(
609581
port: BorrowedFd<'_>,
610582
source: c::c_int,
@@ -613,7 +585,7 @@ pub(crate) unsafe fn port_dissociate(
613585
ret(c::port_dissociate(borrowed_fd(port), source, object))
614586
}
615587

616-
#[cfg(any(target_os = "illumos", target_os = "solaris"))]
588+
#[cfg(solarish)]
617589
pub(crate) fn port_get(
618590
port: BorrowedFd<'_>,
619591
timeout: Option<&mut c::timespec>,
@@ -629,7 +601,7 @@ pub(crate) fn port_get(
629601
Ok(Event(unsafe { event.assume_init() }))
630602
}
631603

632-
#[cfg(any(target_os = "illumos", target_os = "solaris"))]
604+
#[cfg(solarish)]
633605
pub(crate) fn port_getn(
634606
port: BorrowedFd<'_>,
635607
timeout: Option<&mut c::timespec>,
@@ -655,7 +627,7 @@ pub(crate) fn port_getn(
655627
Ok(())
656628
}
657629

658-
#[cfg(any(target_os = "illumos", target_os = "solaris"))]
630+
#[cfg(solarish)]
659631
pub(crate) fn port_send(
660632
port: BorrowedFd<'_>,
661633
events: c::c_int,

src/backend/libc/io/types.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ pub(crate) const STDOUT_FILENO: c::c_int = c::STDOUT_FILENO;
116116
pub(crate) const STDERR_FILENO: c::c_int = c::STDERR_FILENO;
117117

118118
/// A buffer type used with `vmsplice`.
119-
/// It is guaranteed to be ABI compatible with the iovec type on Unix platforms and WSABUF on Windows.
120-
/// Unlike `IoSlice` and `IoSliceMut` it is semantically like a raw pointer,
121-
/// and therefore can be shared or mutated as needed.
119+
/// It is guaranteed to be ABI compatible with the iovec type on Unix platforms
120+
/// and `WSABUF` on Windows. Unlike `IoSlice` and `IoSliceMut` it is
121+
/// semantically like a raw pointer, and therefore can be shared or mutated as
122+
/// needed.
122123
#[cfg(any(target_os = "android", target_os = "linux"))]
123124
#[repr(transparent)]
124125
pub struct IoSliceRaw<'a> {

src/backend/libc/io_lifetimes.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ pub use io_lifetimes::AsSocket;
1313

1414
/// A version of [`AsRawFd`] for use with Winsock2 API.
1515
///
16-
/// [`AsRawFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.AsRawFd.html
16+
/// [`AsRawFd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.AsRawFd.html
1717
pub trait AsRawFd {
1818
/// A version of [`as_raw_fd`] for use with Winsock2 API.
1919
///
20-
/// [`as_raw_fd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.FromRawFd.html#tymethod.as_raw_fd
20+
/// [`as_raw_fd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.html#tymethod.as_raw_fd
2121
fn as_raw_fd(&self) -> RawFd;
2222
}
2323
#[cfg(feature = "std")]
@@ -30,11 +30,11 @@ impl<T: std::os::windows::io::AsRawSocket> AsRawFd for T {
3030

3131
/// A version of [`IntoRawFd`] for use with Winsock2 API.
3232
///
33-
/// [`IntoRawFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.IntoRawFd.html
33+
/// [`IntoRawFd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.IntoRawFd.html
3434
pub trait IntoRawFd {
3535
/// A version of [`into_raw_fd`] for use with Winsock2 API.
3636
///
37-
/// [`into_raw_fd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.FromRawFd.html#tymethod.into_raw_fd
37+
/// [`into_raw_fd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.html#tymethod.into_raw_fd
3838
fn into_raw_fd(self) -> RawFd;
3939
}
4040
#[cfg(feature = "std")]
@@ -47,11 +47,16 @@ impl<T: std::os::windows::io::IntoRawSocket> IntoRawFd for T {
4747

4848
/// A version of [`FromRawFd`] for use with Winsock2 API.
4949
///
50-
/// [`FromRawFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.FromRawFd.html
50+
/// [`FromRawFd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.html
5151
pub trait FromRawFd {
5252
/// A version of [`from_raw_fd`] for use with Winsock2 API.
5353
///
54-
/// [`from_raw_fd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.FromRawFd.html#tymethod.from_raw_fd
54+
/// # Safety
55+
///
56+
/// See the [safety requirements] for [`from_raw_fd`].
57+
///
58+
/// [`from_raw_fd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.html#tymethod.from_raw_fd
59+
/// [safety requirements]: https://doc.rust-lang.org/stable/std/os/fd/trait.FromRawFd.html#safety
5560
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self;
5661
}
5762
#[cfg(feature = "std")]
@@ -64,7 +69,7 @@ impl<T: std::os::windows::io::FromRawSocket> FromRawFd for T {
6469

6570
/// A version of [`AsFd`] for use with Winsock2 API.
6671
///
67-
/// [`AsFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.AsFd.html
72+
/// [`AsFd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.AsFd.html
6873
pub trait AsFd {
6974
/// An `as_fd` function for Winsock2, where a `Fd` is a `Socket`.
7075
fn as_fd(&self) -> BorrowedFd;

src/backend/libc/mm/syscalls.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::super::conv::{borrowed_fd, no_fd, ret};
77
use super::super::offset::libc_mmap;
88
#[cfg(not(target_os = "redox"))]
99
use super::types::Advice;
10-
#[cfg(target_os = "linux")]
10+
#[cfg(any(target_os = "emscripten", target_os = "linux"))]
1111
use super::types::MremapFlags;
1212
use super::types::{MapFlags, MprotectFlags, MsyncFlags, ProtFlags};
1313
#[cfg(any(target_os = "android", target_os = "linux"))]
@@ -130,7 +130,7 @@ pub(crate) unsafe fn munmap(ptr: *mut c::c_void, len: usize) -> io::Result<()> {
130130
///
131131
/// `mremap` is primarily unsafe due to the `old_address` parameter, as
132132
/// anything working with memory pointed to by raw pointers is unsafe.
133-
#[cfg(target_os = "linux")]
133+
#[cfg(any(target_os = "emscripten", target_os = "linux"))]
134134
pub(crate) unsafe fn mremap(
135135
old_address: *mut c::c_void,
136136
old_size: usize,
@@ -150,7 +150,7 @@ pub(crate) unsafe fn mremap(
150150
/// `mremap_fixed` is primarily unsafe due to the `old_address` and
151151
/// `new_address` parameters, as anything working with memory pointed to by raw
152152
/// pointers is unsafe.
153-
#[cfg(target_os = "linux")]
153+
#[cfg(any(target_os = "emscripten", target_os = "linux"))]
154154
pub(crate) unsafe fn mremap_fixed(
155155
old_address: *mut c::c_void,
156156
old_size: usize,

src/backend/libc/mm/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ bitflags! {
180180
}
181181
}
182182

183-
#[cfg(target_os = "linux")]
183+
#[cfg(any(target_os = "emscripten", target_os = "linux"))]
184184
bitflags! {
185185
/// `MREMAP_*` flags for use with [`mremap`].
186186
///

0 commit comments

Comments
 (0)