summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Gohman <dev@sunfishcode.online>2021-11-20 10:35:01 -0800
committerDan Gohman <dev@sunfishcode.online>2021-12-02 13:03:33 -0800
commit68999c30aa80da51fa0c9e402f0b8121ba633081 (patch)
tree413028f6a8abd981026e9ff0ebc525fe697cdf09
parente58976045fae8a937484332d1d37788fdfd0f02d (diff)
downloadrust-68999c30aa80da51fa0c9e402f0b8121ba633081.tar.gz
Port several more things to rustix.
-rw-r--r--library/std/src/sys/unix/condvar.rs6
-rw-r--r--library/std/src/sys/unix/mod.rs32
-rw-r--r--library/std/src/sys/unix/net.rs4
-rw-r--r--library/std/src/sys/unix/os.rs44
-rw-r--r--library/std/src/sys/unix/process/process_unix.rs8
-rw-r--r--library/std/src/sys/unix/stack_overflow.rs31
6 files changed, 48 insertions, 77 deletions
diff --git a/library/std/src/sys/unix/condvar.rs b/library/std/src/sys/unix/condvar.rs
index 61261c0aa84..3fcca8f69a1 100644
--- a/library/std/src/sys/unix/condvar.rs
+++ b/library/std/src/sys/unix/condvar.rs
@@ -94,11 +94,7 @@ impl Condvar {
target_os = "espidf"
)))]
pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
- use crate::mem;
-
- let mut now: libc::timespec = mem::zeroed();
- let r = libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut now);
- assert_eq!(r, 0);
+ let now = rustix::time::clock_gettime(rustix::time::ClockId::Monotonic);
// Nanosecond calculations can't overflow because both values are below 1e9.
let nsec = dur.subsec_nanos() + now.tv_nsec as u32;
diff --git a/library/std/src/sys/unix/mod.rs b/library/std/src/sys/unix/mod.rs
index 2ba6c8d830e..b5025e21300 100644
--- a/library/std/src/sys/unix/mod.rs
+++ b/library/std/src/sys/unix/mod.rs
@@ -81,29 +81,23 @@ pub unsafe fn init(argc: isize, argv: *const *const u8) {
target_os = "ios",
target_os = "redox",
)))] {
- use crate::sys::os::errno;
- let pfds: &mut [_] = &mut [
- libc::pollfd { fd: 0, events: 0, revents: 0 },
- libc::pollfd { fd: 1, events: 0, revents: 0 },
- libc::pollfd { fd: 2, events: 0, revents: 0 },
+ use rustix::io::{PollFd, PollFlags};
+ use rustix::fs::{OFlags, Mode};
+ let mut pfds = [
+ PollFd::from_borrowed_fd(rustix::io::stdin(), PollFlags::empty()),
+ PollFd::from_borrowed_fd(rustix::io::stdout(), PollFlags::empty()),
+ PollFd::from_borrowed_fd(rustix::io::stderr(), PollFlags::empty()),
];
- while libc::poll(pfds.as_mut_ptr(), 3, 0) == -1 {
- if errno() == libc::EINTR {
- continue;
- }
- libc::abort();
- }
+ rustix::io::with_retrying(|| rustix::io::poll(&mut pfds, 0)).unwrap();
for pfd in pfds {
- if pfd.revents & libc::POLLNVAL == 0 {
+ if !pfd.revents().contains(PollFlags::NVAL) {
continue;
}
- if libc::open("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
- // If the stream is closed but we failed to reopen it, abort the
- // process. Otherwise we wouldn't preserve the safety of
- // operations on the corresponding Rust object Stdin, Stdout, or
- // Stderr.
- libc::abort();
- }
+ // If the stream is closed but we failed to reopen it, abort the
+ // process. Otherwise we wouldn't preserve the safety of
+ // operations on the corresponding Rust object Stdin, Stdout, or
+ // Stderr.
+ rustix::fs::openat(&rustix::fs::cwd(), rustix::zstr!("/dev/null"), OFlags::RDWR, Mode::empty()).unwrap();
}
} else if #[cfg(any(target_os = "macos", target_os = "ios", target_os = "redox"))] {
use crate::sys::os::errno;
diff --git a/library/std/src/sys/unix/net.rs b/library/std/src/sys/unix/net.rs
index a82a0172126..de2e99ca8d8 100644
--- a/library/std/src/sys/unix/net.rs
+++ b/library/std/src/sys/unix/net.rs
@@ -421,8 +421,8 @@ impl Socket {
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
- let mut nonblocking = nonblocking as libc::c_int;
- cvt(unsafe { libc::ioctl(self.as_raw_fd(), libc::FIONBIO, &mut nonblocking) }).map(drop)
+ rustix::io::ioctl_fionbio(self, nonblocking)?;
+ Ok(())
}
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
diff --git a/library/std/src/sys/unix/os.rs b/library/std/src/sys/unix/os.rs
index 3d61f2daf6c..1a9c33e4629 100644
--- a/library/std/src/sys/unix/os.rs
+++ b/library/std/src/sys/unix/os.rs
@@ -135,29 +135,8 @@ pub fn getcwd() -> io::Result<PathBuf> {
#[cfg(not(target_os = "espidf"))]
pub fn getcwd() -> io::Result<PathBuf> {
- let mut buf = Vec::with_capacity(512);
- loop {
- unsafe {
- let ptr = buf.as_mut_ptr() as *mut libc::c_char;
- if !libc::getcwd(ptr, buf.capacity()).is_null() {
- let len = CStr::from_ptr(buf.as_ptr() as *const libc::c_char).to_bytes().len();
- buf.set_len(len);
- buf.shrink_to_fit();
- return Ok(PathBuf::from(OsString::from_vec(buf)));
- } else {
- let error = io::Error::last_os_error();
- if error.raw_os_error() != Some(libc::ERANGE) {
- return Err(error);
- }
- }
-
- // Trigger the internal buffer resizing logic of `Vec` by requiring
- // more space than the current capacity.
- let cap = buf.capacity();
- buf.set_len(cap);
- buf.reserve(1);
- }
- }
+ let buf = rustix::process::getcwd(Vec::new())?;
+ Ok(PathBuf::from(OsString::from_vec(buf.into_bytes())))
}
#[cfg(target_os = "espidf")]
@@ -167,11 +146,7 @@ pub fn chdir(p: &path::Path) -> io::Result<()> {
#[cfg(not(target_os = "espidf"))]
pub fn chdir(p: &path::Path) -> io::Result<()> {
- let p: &OsStr = p.as_ref();
- let p = CString::new(p.as_bytes())?;
- if unsafe { libc::chdir(p.as_ptr()) } != 0 {
- return Err(io::Error::last_os_error());
- }
+ rustix::process::chdir(p)?;
Ok(())
}
@@ -327,7 +302,12 @@ pub fn current_exe() -> io::Result<PathBuf> {
#[cfg(target_os = "openbsd")]
pub fn current_exe() -> io::Result<PathBuf> {
unsafe {
- let mut mib = [libc::CTL_KERN, libc::KERN_PROC_ARGS, libc::getpid(), libc::KERN_PROC_ARGV];
+ let mut mib = [
+ libc::CTL_KERN,
+ libc::KERN_PROC_ARGS,
+ rustix::process::getpid().as_raw(),
+ libc::KERN_PROC_ARGV,
+ ];
let mib = mib.as_mut_ptr();
let mut argv_len = 0;
cvt(libc::sysctl(mib, 4, ptr::null_mut(), &mut argv_len, ptr::null_mut(), 0))?;
@@ -564,7 +544,7 @@ pub fn unsetenv(n: &OsStr) -> io::Result<()> {
#[cfg(not(target_os = "espidf"))]
pub fn page_size() -> usize {
- unsafe { libc::sysconf(libc::_SC_PAGESIZE) as usize }
+ rustix::process::page_size()
}
pub fn temp_dir() -> PathBuf {
@@ -608,7 +588,7 @@ pub fn home_dir() -> Option<PathBuf> {
let mut passwd: libc::passwd = mem::zeroed();
let mut result = ptr::null_mut();
match libc::getpwuid_r(
- libc::getuid(),
+ rustix::process::getuid().as_raw(),
&mut passwd,
buf.as_mut_ptr(),
buf.capacity(),
@@ -633,7 +613,7 @@ pub fn getpid() -> u32 {
}
pub fn getppid() -> u32 {
- unsafe { libc::getppid() as u32 }
+ rustix::process::getppid().as_raw()
}
#[cfg(all(target_os = "linux", target_env = "gnu"))]
diff --git a/library/std/src/sys/unix/process/process_unix.rs b/library/std/src/sys/unix/process/process_unix.rs
index bce35b380e6..359f35fe015 100644
--- a/library/std/src/sys/unix/process/process_unix.rs
+++ b/library/std/src/sys/unix/process/process_unix.rs
@@ -304,13 +304,15 @@ impl Command {
if let Some(u) = self.get_uid() {
// When dropping privileges from root, the `setgroups` call
// will remove any extraneous groups. We only drop groups
- // if the current uid is 0 and we weren't given an explicit
+ // if the current uid is ROOT and we weren't given an explicit
// set of groups. If we don't call this, then even though our
// uid has dropped, we may still have groups that enable us to
// do super-user things.
//FIXME: Redox kernel does not support setgroups yet
#[cfg(not(target_os = "redox"))]
- if libc::getuid() == 0 && self.get_groups().is_none() {
+ if rustix::process::getuid() == rustix::process::Uid::ROOT
+ && self.get_groups().is_none()
+ {
cvt(libc::setgroups(0, ptr::null()))?;
}
cvt(libc::setuid(u as uid_t))?;
@@ -513,7 +515,7 @@ impl Command {
))?;
}
if let Some((f, cwd)) = addchdir {
- cvt_nz(f(file_actions.0.as_mut_ptr(), cwd.as_ptr()))?;
+ cvt_nz(f(file_actions.0.as_mut_ptr(), cwd.as_ptr().cast()))?;
}
let mut set = MaybeUninit::<libc::sigset_t>::uninit();
diff --git a/library/std/src/sys/unix/stack_overflow.rs b/library/std/src/sys/unix/stack_overflow.rs
index 1e8d1137ac8..c578f36942d 100644
--- a/library/std/src/sys/unix/stack_overflow.rs
+++ b/library/std/src/sys/unix/stack_overflow.rs
@@ -39,16 +39,13 @@ impl Drop for Handler {
))]
mod imp {
use super::Handler;
- use crate::io;
use crate::mem;
use crate::ptr;
use crate::thread;
- use libc::MAP_FAILED;
- use libc::{mmap, munmap};
+ use libc::SIGSEGV;
use libc::{sigaction, sighandler_t, SA_ONSTACK, SA_SIGINFO, SIGBUS, SIG_DFL};
use libc::{sigaltstack, SIGSTKSZ, SS_DISABLE};
- use libc::{MAP_ANON, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE, SIGSEGV};
use crate::sync::atomic::{AtomicBool, AtomicPtr, Ordering};
use crate::sys::unix::os::page_size;
@@ -143,22 +140,24 @@ mod imp {
}
unsafe fn get_stackp() -> *mut libc::c_void {
+ use rustix::io::{MapFlags, MprotectFlags, ProtFlags};
+
// OpenBSD requires this flag for stack mapping
// otherwise the said mapping will fail as a no-op on most systems
// and has a different meaning on FreeBSD
#[cfg(any(target_os = "openbsd", target_os = "netbsd", target_os = "linux",))]
- let flags = MAP_PRIVATE | MAP_ANON | libc::MAP_STACK;
+ let flags = MapFlags::PRIVATE | MapFlags::STACK;
#[cfg(not(any(target_os = "openbsd", target_os = "netbsd", target_os = "linux",)))]
- let flags = MAP_PRIVATE | MAP_ANON;
- let stackp =
- mmap(ptr::null_mut(), SIGSTKSZ + page_size(), PROT_READ | PROT_WRITE, flags, -1, 0);
- if stackp == MAP_FAILED {
- panic!("failed to allocate an alternative stack: {}", io::Error::last_os_error());
- }
- let guard_result = libc::mprotect(stackp, page_size(), PROT_NONE);
- if guard_result != 0 {
- panic!("failed to set up alternative stack guard page: {}", io::Error::last_os_error());
- }
+ let flags = MapFlags::PRIVATE;
+ let stackp = rustix::io::mmap_anonymous(
+ ptr::null_mut(),
+ SIGSTKSZ + page_size(),
+ ProtFlags::READ | ProtFlags::WRITE,
+ flags,
+ )
+ .expect("failed to allocate an alternative stack");
+ rustix::io::mprotect(stackp, page_size(), MprotectFlags::empty())
+ .expect("failed to set up alternative stack guard page");
stackp.add(page_size())
}
@@ -196,7 +195,7 @@ mod imp {
sigaltstack(&stack, ptr::null_mut());
// We know from `get_stackp` that the alternate stack we installed is part of a mapping
// that started one page earlier, so walk back a page and unmap from there.
- munmap(data.sub(page_size()), SIGSTKSZ + page_size());
+ rustix::io::munmap(data.sub(page_size()), SIGSTKSZ + page_size()).ok();
}
}
}