Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 9 additions & 2 deletions examples/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {
print!(" IMAXBEL");
}
#[cfg(not(any(
bsd,
freebsdlike,
netbsdlike,
solarish,
target_os = "aix",
target_os = "emscripten",
Expand All @@ -127,7 +128,13 @@ fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {
if (term.c_oflag & OPOST) != 0 {
print!(" OPOST");
}
#[cfg(not(any(bsd, target_os = "aix", target_os = "redox")))]
#[cfg(not(any(
apple,
freebsdlike,
target_os = "aix",
target_os = "netbsd",
target_os = "redox"
)))]
if (term.c_oflag & OLCUC) != 0 {
print!(" OLCUC");
}
Expand Down
24 changes: 11 additions & 13 deletions src/backend/libc/fs/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

use super::super::c;
use super::super::conv::{borrowed_fd, c_str, ret, ret_c_int, ret_off_t, ret_owned_fd, ret_usize};
#[cfg(any(target_os = "android", target_os = "linux"))]
use super::super::conv::{syscall_ret, syscall_ret_owned_fd, syscall_ret_usize};
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
use super::super::offset::libc_fallocate;
#[cfg(not(any(
Expand Down Expand Up @@ -83,10 +81,6 @@ use crate::fs::SealFlags;
target_os = "wasi",
)))]
use crate::fs::StatFs;
#[cfg(any(apple, target_os = "android", target_os = "linux"))]
use crate::fs::XattrFlags;
#[cfg(any(target_os = "android", target_os = "linux"))]
use crate::fs::{cwd, RenameFlags, ResolveFlags, Statx, StatxFlags};
use crate::fs::{Access, Mode, OFlags, Stat, Timestamps};
#[cfg(not(any(apple, target_os = "redox", target_os = "wasi")))]
use crate::fs::{Dev, FileType};
Expand All @@ -100,21 +94,25 @@ use crate::process::{Gid, Uid};
target_env = "gnu",
)))]
use crate::utils::as_ptr;
#[cfg(apple)]
use alloc::vec;
use core::convert::TryInto;
#[cfg(any(apple, target_os = "android", target_os = "linux"))]
use core::mem::size_of;
use core::mem::MaybeUninit;
#[cfg(any(target_os = "android", target_os = "linux"))]
use core::ptr::null;
#[cfg(any(apple, target_os = "android", target_os = "linux"))]
use core::ptr::null_mut;
#[cfg(apple)]
use {
super::super::conv::nonnegative_ret,
crate::fs::{copyfile_state_t, CloneFlags, CopyfileFlags},
};
#[cfg(any(target_os = "android", target_os = "linux"))]
use {
super::super::conv::{syscall_ret, syscall_ret_owned_fd, syscall_ret_usize},
crate::fs::{cwd, RenameFlags, ResolveFlags, Statx, StatxFlags},
core::ptr::null,
};
#[cfg(not(target_os = "redox"))]
use {super::super::offset::libc_openat, crate::fs::AtFlags};
#[cfg(any(apple, target_os = "android", target_os = "linux"))]
use {crate::fs::XattrFlags, core::mem::size_of, core::ptr::null_mut};

#[cfg(all(
any(target_arch = "arm", target_arch = "mips", target_arch = "x86"),
Expand Down Expand Up @@ -1635,7 +1633,7 @@ pub(crate) fn getpath(fd: BorrowedFd<'_>) -> io::Result<CString> {
// `F_GETPATH` in terms of `MAXPATHLEN`, and there are no
// alternatives. If a better method is invented, it should be used
// instead.
let mut buf = alloc::vec![0; c::PATH_MAX as usize];
let mut buf = vec![0; c::PATH_MAX as usize];

// From the [macOS `fcntl` manual page]:
// `F_GETPATH` - Get the path of the file descriptor `Fildes`. The argument
Expand Down
30 changes: 15 additions & 15 deletions src/backend/libc/io/syscalls.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! libc syscalls supporting `rustix::io`.

use super::super::c;
#[cfg(any(target_os = "android", target_os = "linux"))]
use super::super::conv::syscall_ret_owned_fd;
#[cfg(any(
target_os = "android",
all(target_os = "linux", not(target_env = "gnu")),
Expand All @@ -15,10 +13,6 @@ use super::super::offset::{libc_preadv, libc_pwritev};
#[cfg(all(target_os = "linux", target_env = "gnu"))]
use super::super::offset::{libc_preadv2, libc_pwritev2};
use crate::fd::{AsFd, BorrowedFd, OwnedFd, RawFd};
#[cfg(bsd)]
use crate::io::kqueue::Event;
#[cfg(solarish)]
use crate::io::port::Event;
#[cfg(not(any(target_os = "aix", target_os = "wasi")))]
use crate::io::DupFlags;
#[cfg(any(
Expand All @@ -31,15 +25,21 @@ use crate::io::EventfdFlags;
#[cfg(not(any(apple, target_os = "aix", target_os = "haiku", target_os = "wasi")))]
use crate::io::PipeFlags;
use crate::io::{self, FdFlags, IoSlice, IoSliceMut, PollFd};
#[cfg(any(target_os = "android", target_os = "linux"))]
use crate::io::{IoSliceRaw, ReadWriteFlags, SpliceFlags};
use core::cmp::min;
use core::convert::TryInto;
use core::mem::MaybeUninit;
#[cfg(any(target_os = "android", target_os = "linux"))]
use core::ptr;
#[cfg(all(feature = "fs", feature = "net"))]
use libc_errno::errno;
#[cfg(any(target_os = "android", target_os = "linux"))]
use {
super::super::conv::syscall_ret_owned_fd,
crate::io::{IoSliceRaw, ReadWriteFlags, SpliceFlags},
core::ptr,
};
#[cfg(bsd)]
use {crate::io::kqueue::Event, crate::utils::as_ptr, core::ptr::null};
#[cfg(solarish)]
use {crate::io::port::Event, crate::utils::as_mut_ptr, core::ptr::null_mut};

pub(crate) fn read(fd: BorrowedFd<'_>, buf: &mut [u8]) -> io::Result<usize> {
unsafe {
Expand Down Expand Up @@ -531,17 +531,17 @@ pub(crate) unsafe fn kevent(
) -> io::Result<c::c_int> {
ret_c_int(c::kevent(
borrowed_fd(kq),
changelist.as_ptr() as *const _,
changelist.as_ptr().cast(),
changelist
.len()
.try_into()
.map_err(|_| io::Errno::OVERFLOW)?,
eventlist.as_mut_ptr() as *mut _,
eventlist.as_mut_ptr().cast(),
eventlist
.len()
.try_into()
.map_err(|_| io::Errno::OVERFLOW)?,
timeout.map_or(core::ptr::null(), |t| t as *const _),
timeout.map_or(null(), as_ptr),
))
}

Expand Down Expand Up @@ -658,7 +658,7 @@ pub(crate) fn port_get(
timeout: Option<&mut c::timespec>,
) -> io::Result<Event> {
let mut event = MaybeUninit::<c::port_event>::uninit();
let timeout = timeout.map_or(core::ptr::null_mut(), |t| t as *mut _);
let timeout = timeout.map_or(null_mut(), as_mut_ptr);

unsafe {
ret(c::port_get(borrowed_fd(port), event.as_mut_ptr(), timeout))?;
Expand All @@ -675,7 +675,7 @@ pub(crate) fn port_getn(
events: &mut Vec<Event>,
mut nget: u32,
) -> io::Result<()> {
let timeout = timeout.map_or(core::ptr::null_mut(), |t| t as *mut _);
let timeout = timeout.map_or(null_mut(), as_mut_ptr);
unsafe {
ret(c::port_getn(
borrowed_fd(port),
Expand Down
28 changes: 14 additions & 14 deletions src/backend/libc/net/addr.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//! IPv4, IPv6, and Socket addresses.
//! Socket address utilities.

use super::super::c;
#[cfg(unix)]
use crate::ffi::CStr;
#[cfg(unix)]
use crate::io;
#[cfg(unix)]
use crate::path;
#[cfg(not(windows))]
use core::convert::TryInto;
#[cfg(unix)]
use core::fmt;
#[cfg(unix)]
use core::slice;
use {
crate::ffi::CStr,
crate::io,
crate::path,
core::cmp::Ordering,
core::fmt,
core::hash::{Hash, Hasher},
core::slice,
};

/// `struct sockaddr_un`
#[cfg(unix)]
Expand Down Expand Up @@ -159,7 +159,7 @@ impl Eq for SocketAddrUnix {}
#[cfg(unix)]
impl PartialOrd for SocketAddrUnix {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
let self_len = self.len() - offsetof_sun_path();
let other_len = other.len() - offsetof_sun_path();
self.unix.sun_path[..self_len].partial_cmp(&other.unix.sun_path[..other_len])
Expand All @@ -169,17 +169,17 @@ impl PartialOrd for SocketAddrUnix {
#[cfg(unix)]
impl Ord for SocketAddrUnix {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
fn cmp(&self, other: &Self) -> Ordering {
let self_len = self.len() - offsetof_sun_path();
let other_len = other.len() - offsetof_sun_path();
self.unix.sun_path[..self_len].cmp(&other.unix.sun_path[..other_len])
}
}

#[cfg(unix)]
impl core::hash::Hash for SocketAddrUnix {
impl Hash for SocketAddrUnix {
#[inline]
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
fn hash<H: Hasher>(&self, state: &mut H) {
let self_len = self.len() - offsetof_sun_path();
self.unix.sun_path[..self_len].hash(state)
}
Expand Down
24 changes: 12 additions & 12 deletions src/backend/libc/net/msghdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use super::super::c;
use super::super::conv::{msg_control_len, msg_iov_len};
use super::super::net::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6};

use crate::io;
use crate::net::{SocketAddrV4, SocketAddrV6};
use crate::io::{IoSlice, IoSliceMut};
use crate::net::{RecvAncillaryBuffer, SendAncillaryBuffer, SocketAddrV4, SocketAddrV6};
use crate::utils::as_ptr;

use core::convert::TryInto;
Expand All @@ -17,8 +17,8 @@ use core::mem::{size_of, zeroed, MaybeUninit};
/// Create a message header intended to receive a datagram.
pub(crate) fn with_recv_msghdr<R>(
name: &mut MaybeUninit<c::sockaddr_storage>,
iov: &mut [io::IoSliceMut<'_>],
control: &mut crate::net::RecvAncillaryBuffer<'_>,
iov: &mut [IoSliceMut<'_>],
control: &mut RecvAncillaryBuffer<'_>,
f: impl FnOnce(&mut c::msghdr) -> R,
) -> R {
let namelen = size_of::<c::sockaddr_storage>() as c::socklen_t;
Expand All @@ -45,8 +45,8 @@ pub(crate) fn with_recv_msghdr<R>(

/// Create a message header intended to send without an address.
pub(crate) fn with_noaddr_msghdr<R>(
iov: &[io::IoSlice<'_>],
control: &mut crate::net::SendAncillaryBuffer<'_, '_, '_>,
iov: &[IoSlice<'_>],
control: &mut SendAncillaryBuffer<'_, '_, '_>,
f: impl FnOnce(c::msghdr) -> R,
) -> R {
f({
Expand All @@ -62,8 +62,8 @@ pub(crate) fn with_noaddr_msghdr<R>(
/// Create a message header intended to send with an IPv4 address.
pub(crate) fn with_v4_msghdr<R>(
addr: &SocketAddrV4,
iov: &[io::IoSlice<'_>],
control: &mut crate::net::SendAncillaryBuffer<'_, '_, '_>,
iov: &[IoSlice<'_>],
control: &mut SendAncillaryBuffer<'_, '_, '_>,
f: impl FnOnce(c::msghdr) -> R,
) -> R {
let encoded = unsafe { encode_sockaddr_v4(addr) };
Expand All @@ -83,8 +83,8 @@ pub(crate) fn with_v4_msghdr<R>(
/// Create a message header intended to send with an IPv6 address.
pub(crate) fn with_v6_msghdr<R>(
addr: &SocketAddrV6,
iov: &[io::IoSlice<'_>],
control: &mut crate::net::SendAncillaryBuffer<'_, '_, '_>,
iov: &[IoSlice<'_>],
control: &mut SendAncillaryBuffer<'_, '_, '_>,
f: impl FnOnce(c::msghdr) -> R,
) -> R {
let encoded = unsafe { encode_sockaddr_v6(addr) };
Expand All @@ -105,8 +105,8 @@ pub(crate) fn with_v6_msghdr<R>(
#[cfg(all(unix, not(target_os = "redox")))]
pub(crate) fn with_unix_msghdr<R>(
addr: &crate::net::SocketAddrUnix,
iov: &[io::IoSlice<'_>],
control: &mut crate::net::SendAncillaryBuffer<'_, '_, '_>,
iov: &[IoSlice<'_>],
control: &mut SendAncillaryBuffer<'_, '_, '_>,
f: impl FnOnce(c::msghdr) -> R,
) -> R {
f({
Expand Down
23 changes: 9 additions & 14 deletions src/backend/libc/net/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,26 @@ use super::super::conv::{borrowed_fd, ret, ret_owned_fd, ret_send_recv, send_rec
#[cfg(unix)]
use super::addr::SocketAddrUnix;
use super::ext::{in6_addr_new, in_addr_new};
#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
use super::msghdr::{with_noaddr_msghdr, with_recv_msghdr, with_v4_msghdr, with_v6_msghdr};
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
use super::read_sockaddr::initialize_family_to_unspec;
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
use super::read_sockaddr::{maybe_read_sockaddr_os, read_sockaddr_os};
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
use super::send_recv::{RecvFlags, SendFlags};
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
use super::types::{AddressFamily, Protocol, Shutdown, SocketFlags, SocketType};
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
use super::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6};
use crate::fd::{BorrowedFd, OwnedFd};
use crate::io;
use crate::net::{SocketAddrAny, SocketAddrV4, SocketAddrV6};
use crate::utils::as_ptr;
use core::convert::TryInto;
use core::mem::{size_of, MaybeUninit};
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
use core::ptr::null_mut;
#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
use {
super::msghdr::{with_noaddr_msghdr, with_recv_msghdr, with_v4_msghdr, with_v6_msghdr},
crate::io::{IoSlice, IoSliceMut},
crate::net::{RecvAncillaryBuffer, RecvMsgReturn, SendAncillaryBuffer},
};
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
use {
super::read_sockaddr::{initialize_family_to_unspec, maybe_read_sockaddr_os, read_sockaddr_os},
super::send_recv::{RecvFlags, SendFlags},
super::types::{AddressFamily, Protocol, Shutdown, SocketFlags, SocketType},
super::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6},
core::ptr::null_mut,
};

#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
pub(crate) fn recv(fd: BorrowedFd<'_>, buf: &mut [u8], flags: RecvFlags) -> io::Result<usize> {
Expand Down
12 changes: 6 additions & 6 deletions src/backend/libc/termios/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ use crate::fd::BorrowedFd;
#[cfg(feature = "procfs")]
#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
use crate::ffi::CStr;
#[cfg(not(target_os = "wasi"))]
use crate::io;
#[cfg(not(target_os = "wasi"))]
use crate::process::{Pid, RawNonZeroPid};
#[cfg(not(target_os = "wasi"))]
use crate::termios::{Action, OptionalActions, QueueSelector, Speed, Termios, Winsize};
use core::mem::MaybeUninit;
#[cfg(not(target_os = "wasi"))]
use {
crate::io,
crate::process::{Pid, RawNonZeroPid},
crate::termios::{Action, OptionalActions, QueueSelector, Speed, Termios, Winsize},
};

#[cfg(not(target_os = "wasi"))]
pub(crate) fn tcgetattr(fd: BorrowedFd<'_>) -> io::Result<Termios> {
Expand Down
Loading