use rwlock for accessing ENV
This commit is contained in:
parent
cfba499271
commit
55ca27faa7
3 changed files with 55 additions and 11 deletions
|
@ -22,6 +22,7 @@ use crate::str;
|
||||||
use crate::sys::cvt;
|
use crate::sys::cvt;
|
||||||
use crate::sys::fd;
|
use crate::sys::fd;
|
||||||
use crate::sys_common::mutex::{StaticMutex, StaticMutexGuard};
|
use crate::sys_common::mutex::{StaticMutex, StaticMutexGuard};
|
||||||
|
use crate::sys_common::rwlock::{RWLock, RWLockGuard};
|
||||||
use crate::vec;
|
use crate::vec;
|
||||||
|
|
||||||
use libc::{c_char, c_int, c_void};
|
use libc::{c_char, c_int, c_void};
|
||||||
|
@ -493,17 +494,16 @@ pub unsafe fn environ() -> *mut *const *const c_char {
|
||||||
&mut environ
|
&mut environ
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn env_lock() -> StaticMutexGuard {
|
pub unsafe fn env_rwlock(readonly: bool) -> RWLockGuard {
|
||||||
// It is UB to attempt to acquire this mutex reentrantly!
|
static ENV_LOCK: RWLock = RWLock::new();
|
||||||
static ENV_LOCK: StaticMutex = StaticMutex::new();
|
if readonly { ENV_LOCK.read_with_guard() } else { ENV_LOCK.write_with_guard() }
|
||||||
ENV_LOCK.lock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a vector of (variable, value) byte-vector pairs for all the
|
/// Returns a vector of (variable, value) byte-vector pairs for all the
|
||||||
/// environment variables of the current process.
|
/// environment variables of the current process.
|
||||||
pub fn env() -> Env {
|
pub fn env() -> Env {
|
||||||
unsafe {
|
unsafe {
|
||||||
let _guard = env_lock();
|
let _guard = env_rwlock(true);
|
||||||
let mut environ = *environ();
|
let mut environ = *environ();
|
||||||
let mut result = Vec::new();
|
let mut result = Vec::new();
|
||||||
if !environ.is_null() {
|
if !environ.is_null() {
|
||||||
|
@ -540,7 +540,7 @@ pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
|
||||||
// always None as well
|
// always None as well
|
||||||
let k = CString::new(k.as_bytes())?;
|
let k = CString::new(k.as_bytes())?;
|
||||||
unsafe {
|
unsafe {
|
||||||
let _guard = env_lock();
|
let _guard = env_rwlock(true);
|
||||||
let s = libc::getenv(k.as_ptr()) as *const libc::c_char;
|
let s = libc::getenv(k.as_ptr()) as *const libc::c_char;
|
||||||
let ret = if s.is_null() {
|
let ret = if s.is_null() {
|
||||||
None
|
None
|
||||||
|
@ -556,7 +556,7 @@ pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
|
||||||
let v = CString::new(v.as_bytes())?;
|
let v = CString::new(v.as_bytes())?;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let _guard = env_lock();
|
let _guard = env_rwlock(false);
|
||||||
cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(drop)
|
cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(drop)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -565,7 +565,7 @@ pub fn unsetenv(n: &OsStr) -> io::Result<()> {
|
||||||
let nbuf = CString::new(n.as_bytes())?;
|
let nbuf = CString::new(n.as_bytes())?;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let _guard = env_lock();
|
let _guard = env_rwlock(false);
|
||||||
cvt(libc::unsetenv(nbuf.as_ptr())).map(drop)
|
cvt(libc::unsetenv(nbuf.as_ptr())).map(drop)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ impl Command {
|
||||||
// a lock any more because the parent won't do anything and the child is
|
// a lock any more because the parent won't do anything and the child is
|
||||||
// in its own process.
|
// in its own process.
|
||||||
let result = unsafe {
|
let result = unsafe {
|
||||||
let _env_lock = sys::os::env_lock();
|
let _env_lock = sys::os::env_rwlock(true);
|
||||||
cvt(libc::fork())?
|
cvt(libc::fork())?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ impl Command {
|
||||||
// Similar to when forking, we want to ensure that access to
|
// Similar to when forking, we want to ensure that access to
|
||||||
// the environment is synchronized, so make sure to grab the
|
// the environment is synchronized, so make sure to grab the
|
||||||
// environment lock before we try to exec.
|
// environment lock before we try to exec.
|
||||||
let _lock = sys::os::env_lock();
|
let _lock = sys::os::env_rwlock(true);
|
||||||
|
|
||||||
let Err(e) = self.do_exec(theirs, envp.as_ref());
|
let Err(e) = self.do_exec(theirs, envp.as_ref());
|
||||||
e
|
e
|
||||||
|
@ -404,7 +404,7 @@ impl Command {
|
||||||
cvt_nz(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?;
|
cvt_nz(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?;
|
||||||
|
|
||||||
// Make sure we synchronize access to the global `environ` resource
|
// Make sure we synchronize access to the global `environ` resource
|
||||||
let _env_lock = sys::os::env_lock();
|
let _env_lock = sys::os::env_rwlock(true);
|
||||||
let envp = envp.map(|c| c.as_ptr()).unwrap_or_else(|| *sys::os::environ() as *const _);
|
let envp = envp.map(|c| c.as_ptr()).unwrap_or_else(|| *sys::os::environ() as *const _);
|
||||||
cvt_nz(libc::posix_spawnp(
|
cvt_nz(libc::posix_spawnp(
|
||||||
&mut p.pid,
|
&mut p.pid,
|
||||||
|
|
|
@ -1,5 +1,23 @@
|
||||||
use crate::sys::rwlock as imp;
|
use crate::sys::rwlock as imp;
|
||||||
|
|
||||||
|
enum GuardType {
|
||||||
|
Read,
|
||||||
|
Write,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RWLockGuard(&'static RWLock, GuardType);
|
||||||
|
|
||||||
|
impl Drop for RWLockGuard {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
unsafe {
|
||||||
|
match &self.1 {
|
||||||
|
GuardType::Read => self.0.read_unlock(),
|
||||||
|
GuardType::Write => self.0.write_unlock(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// An OS-based reader-writer lock.
|
/// An OS-based reader-writer lock.
|
||||||
///
|
///
|
||||||
/// This structure is entirely unsafe and serves as the lowest layer of a
|
/// This structure is entirely unsafe and serves as the lowest layer of a
|
||||||
|
@ -26,6 +44,19 @@ impl RWLock {
|
||||||
self.0.read()
|
self.0.read()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Acquires shared access to the underlying lock, blocking the current
|
||||||
|
/// thread to do so.
|
||||||
|
///
|
||||||
|
/// The lock is automatically unlocked when the returned guard is dropped.
|
||||||
|
///
|
||||||
|
/// Behavior is undefined if the rwlock has been moved between this and any
|
||||||
|
/// previous method call.
|
||||||
|
#[inline]
|
||||||
|
pub unsafe fn read_with_guard(&'static self) -> RWLockGuard {
|
||||||
|
self.read();
|
||||||
|
RWLockGuard(&self, GuardType::Read)
|
||||||
|
}
|
||||||
|
|
||||||
/// Attempts to acquire shared access to this lock, returning whether it
|
/// Attempts to acquire shared access to this lock, returning whether it
|
||||||
/// succeeded or not.
|
/// succeeded or not.
|
||||||
///
|
///
|
||||||
|
@ -48,6 +79,19 @@ impl RWLock {
|
||||||
self.0.write()
|
self.0.write()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Acquires write access to the underlying lock, blocking the current thread
|
||||||
|
/// to do so.
|
||||||
|
///
|
||||||
|
/// The lock is automatically unlocked when the returned guard is dropped.
|
||||||
|
///
|
||||||
|
/// Behavior is undefined if the rwlock has been moved between this and any
|
||||||
|
/// previous method call.
|
||||||
|
#[inline]
|
||||||
|
pub unsafe fn write_with_guard(&'static self) -> RWLockGuard {
|
||||||
|
self.write();
|
||||||
|
RWLockGuard(&self, GuardType::Write)
|
||||||
|
}
|
||||||
|
|
||||||
/// Attempts to acquire exclusive access to this lock, returning whether it
|
/// Attempts to acquire exclusive access to this lock, returning whether it
|
||||||
/// succeeded or not.
|
/// succeeded or not.
|
||||||
///
|
///
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue