#![allow(unsafe_op_in_unsafe_fn)] use core::slice::memchr; use libc::c_char; use crate::ffi::{CStr, OsStr, OsString}; use crate::os::unix::prelude::*; use crate::sync::{PoisonError, RwLock}; use crate::sys::common::small_c_string::run_with_cstr; use crate::sys::cvt; use crate::{fmt, io, vec}; pub struct Env { iter: vec::IntoIter<(OsString, OsString)>, } // FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when ::fmt matches ::fmt. pub struct EnvStrDebug<'a> { slice: &'a [(OsString, OsString)], } impl fmt::Debug for EnvStrDebug<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let Self { slice } = self; f.debug_list() .entries(slice.iter().map(|(a, b)| (a.to_str().unwrap(), b.to_str().unwrap()))) .finish() } } impl Env { pub fn str_debug(&self) -> impl fmt::Debug + '_ { let Self { iter } = self; EnvStrDebug { slice: iter.as_slice() } } } impl fmt::Debug for Env { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let Self { iter } = self; f.debug_list().entries(iter.as_slice()).finish() } } impl !Send for Env {} impl !Sync for Env {} impl Iterator for Env { type Item = (OsString, OsString); fn next(&mut self) -> Option<(OsString, OsString)> { self.iter.next() } fn size_hint(&self) -> (usize, Option) { self.iter.size_hint() } } // Use `_NSGetEnviron` on Apple platforms. // // `_NSGetEnviron` is the documented alternative (see `man environ`), and has // been available since the first versions of both macOS and iOS. // // Nowadays, specifically since macOS 10.8, `environ` has been exposed through // `libdyld.dylib`, which is linked via. `libSystem.dylib`: // // // So in the end, it likely doesn't really matter which option we use, but the // performance cost of using `_NSGetEnviron` is extremely miniscule, and it // might be ever so slightly more supported, so let's just use that. // // NOTE: The header where this is defined (`crt_externs.h`) was added to the // iOS 13.0 SDK, which has been the source of a great deal of confusion in the // past about the availability of this API. // // NOTE(madsmtm): Neither this nor using `environ` has been verified to not // cause App Store rejections; if this is found to be the case, an alternative // implementation of this is possible using `[NSProcessInfo environment]` // - which internally uses `_NSGetEnviron` and a system-wide lock on the // environment variables to protect against `setenv`, so using that might be // desirable anyhow? Though it also means that we have to link to Foundation. #[cfg(target_vendor = "apple")] pub unsafe fn environ() -> *mut *const *const c_char { libc::_NSGetEnviron() as *mut *const *const c_char } // Use the `environ` static which is part of POSIX. #[cfg(not(target_vendor = "apple"))] pub unsafe fn environ() -> *mut *const *const c_char { unsafe extern "C" { static mut environ: *const *const c_char; } &raw mut environ } static ENV_LOCK: RwLock<()> = RwLock::new(()); pub fn env_read_lock() -> impl Drop { ENV_LOCK.read().unwrap_or_else(PoisonError::into_inner) } /// Returns a vector of (variable, value) byte-vector pairs for all the /// environment variables of the current process. pub fn env() -> Env { unsafe { let _guard = env_read_lock(); let mut environ = *environ(); let mut result = Vec::new(); if !environ.is_null() { while !(*environ).is_null() { if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) { result.push(key_value); } environ = environ.add(1); } } return Env { iter: result.into_iter() }; } fn parse(input: &[u8]) -> Option<(OsString, OsString)> { // Strategy (copied from glibc): Variable name and value are separated // by an ASCII equals sign '='. Since a variable name must not be // empty, allow variable names starting with an equals sign. Skip all // malformed lines. if input.is_empty() { return None; } let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1); pos.map(|p| { ( OsStringExt::from_vec(input[..p].to_vec()), OsStringExt::from_vec(input[p + 1..].to_vec()), ) }) } } pub fn getenv(k: &OsStr) -> Option { // environment variables with a nul byte can't be set, so their value is // always None as well run_with_cstr(k.as_bytes(), &|k| { let _guard = env_read_lock(); let v = unsafe { libc::getenv(k.as_ptr()) } as *const libc::c_char; if v.is_null() { Ok(None) } else { // SAFETY: `v` cannot be mutated while executing this line since we've a read lock let bytes = unsafe { CStr::from_ptr(v) }.to_bytes().to_vec(); Ok(Some(OsStringExt::from_vec(bytes))) } }) .ok() .flatten() } pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> { run_with_cstr(k.as_bytes(), &|k| { run_with_cstr(v.as_bytes(), &|v| { let _guard = ENV_LOCK.write(); cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(drop) }) }) } pub unsafe fn unsetenv(n: &OsStr) -> io::Result<()> { run_with_cstr(n.as_bytes(), &|nbuf| { let _guard = ENV_LOCK.write(); cvt(libc::unsetenv(nbuf.as_ptr())).map(drop) }) }