summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuki Okushi <huyuumi.dev@gmail.com>2019-12-06 15:36:56 +0900
committerGitHub <noreply@github.com>2019-12-06 15:36:56 +0900
commit617b07e7302065d8aaed18ab0baf88cfb9e8e054 (patch)
treebcd96e96f0627d7a5feb976836ce9ff212838c72
parentAuto merge of #66911 - eddyb:nicer-rustc_regions, r=matthewjasper (diff)
parentensure that access to the environment is synchronized (diff)
downloadrust-617b07e7302065d8aaed18ab0baf88cfb9e8e054.tar.gz
rust-617b07e7302065d8aaed18ab0baf88cfb9e8e054.tar.bz2
rust-617b07e7302065d8aaed18ab0baf88cfb9e8e054.tar.xz
Rollup merge of #66649 - Wind-River:master_xyz, r=alexcrichton
VxWorks: fix issues in accessing environment variables
-rw-r--r--src/libstd/sys/vxworks/os.rs19
-rw-r--r--src/libstd/sys/vxworks/process/process_vxworks.rs12
2 files changed, 22 insertions, 9 deletions
diff --git a/src/libstd/sys/vxworks/os.rs b/src/libstd/sys/vxworks/os.rs
index baa6c42..71e1d16 100644
--- a/src/libstd/sys/vxworks/os.rs
+++ b/src/libstd/sys/vxworks/os.rs
@@ -11,14 +11,12 @@ use crate::path::{self, PathBuf, Path};
11use crate::ptr; 11use crate::ptr;
12use crate::slice; 12use crate::slice;
13use crate::str; 13use crate::str;
14use crate::sys_common::mutex::Mutex; 14use crate::sys_common::mutex::{Mutex, MutexGuard};
15use crate::sys::cvt; 15use crate::sys::cvt;
16/*use sys::fd; this one is probably important */ 16/*use sys::fd; this one is probably important */
17use crate::vec; 17use crate::vec;
18 18
19const TMPBUF_SZ: usize = 128; 19const TMPBUF_SZ: usize = 128;
20static ENV_LOCK: Mutex = Mutex::new();
21
22 20
23// This is a terrible fix 21// This is a terrible fix
24use crate::sys::os_str::Buf; 22use crate::sys::os_str::Buf;
@@ -200,11 +198,18 @@ pub unsafe fn environ() -> *mut *const *const c_char {
200 &mut environ 198 &mut environ
201} 199}
202 200
201pub unsafe fn env_lock() -> MutexGuard<'static> {
202 // We never call `ENV_LOCK.init()`, so it is UB to attempt to
203 // acquire this mutex reentrantly!
204 static ENV_LOCK: Mutex = Mutex::new();
205 ENV_LOCK.lock()
206}
207
203/// Returns a vector of (variable, value) byte-vector pairs for all the 208/// Returns a vector of (variable, value) byte-vector pairs for all the
204/// environment variables of the current process. 209/// environment variables of the current process.
205pub fn env() -> Env { 210pub fn env() -> Env {
206 unsafe { 211 unsafe {
207 let _guard = ENV_LOCK.lock(); 212 let _guard = env_lock();
208 let mut environ = *environ(); 213 let mut environ = *environ();
209 if environ == ptr::null() { 214 if environ == ptr::null() {
210 panic!("os::env() failure getting env string from OS: {}", 215 panic!("os::env() failure getting env string from OS: {}",
@@ -244,7 +249,7 @@ pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
244 // always None as well 249 // always None as well
245 let k = CString::new(k.as_bytes())?; 250 let k = CString::new(k.as_bytes())?;
246 unsafe { 251 unsafe {
247 let _guard = ENV_LOCK.lock(); 252 let _guard = env_lock();
248 let s = libc::getenv(k.as_ptr()) as *const libc::c_char; 253 let s = libc::getenv(k.as_ptr()) as *const libc::c_char;
249 let ret = if s.is_null() { 254 let ret = if s.is_null() {
250 None 255 None
@@ -260,7 +265,7 @@ pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
260 let v = CString::new(v.as_bytes())?; 265 let v = CString::new(v.as_bytes())?;
261 266
262 unsafe { 267 unsafe {
263 let _guard = ENV_LOCK.lock(); 268 let _guard = env_lock();
264 cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(|_| ()) 269 cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(|_| ())
265 } 270 }
266} 271}
@@ -269,7 +274,7 @@ pub fn unsetenv(n: &OsStr) -> io::Result<()> {
269 let nbuf = CString::new(n.as_bytes())?; 274 let nbuf = CString::new(n.as_bytes())?;
270 275
271 unsafe { 276 unsafe {
272 let _guard = ENV_LOCK.lock(); 277 let _guard = env_lock();
273 cvt(libc::unsetenv(nbuf.as_ptr())).map(|_| ()) 278 cvt(libc::unsetenv(nbuf.as_ptr())).map(|_| ())
274 } 279 }
275} 280}
diff --git a/src/libstd/sys/vxworks/process/process_vxworks.rs b/src/libstd/sys/vxworks/process/process_vxworks.rs
index 7446471..79bfd77 100644
--- a/src/libstd/sys/vxworks/process/process_vxworks.rs
+++ b/src/libstd/sys/vxworks/process/process_vxworks.rs
@@ -15,6 +15,7 @@ impl Command {
15 -> io::Result<(Process, StdioPipes)> { 15 -> io::Result<(Process, StdioPipes)> {
16 use crate::sys::{cvt_r}; 16 use crate::sys::{cvt_r};
17 const CLOEXEC_MSG_FOOTER: &'static [u8] = b"NOEX"; 17 const CLOEXEC_MSG_FOOTER: &'static [u8] = b"NOEX";
18 let envp = self.capture_env();
18 19
19 if self.saw_nul() { 20 if self.saw_nul() {
20 return Err(io::Error::new(ErrorKind::InvalidInput, 21 return Err(io::Error::new(ErrorKind::InvalidInput,
@@ -52,12 +53,19 @@ impl Command {
52 t!(cvt(libc::chdir(cwd.as_ptr()))); 53 t!(cvt(libc::chdir(cwd.as_ptr())));
53 } 54 }
54 55
56 let c_envp = envp.as_ref().map(|c| c.as_ptr())
57 .unwrap_or_else(|| *sys::os::environ() as *const _);
58 let stack_size = thread::min_stack();
59
60 // ensure that access to the environment is synchronized
61 let _lock = sys::os::env_lock();
62
55 let ret = libc::rtpSpawn( 63 let ret = libc::rtpSpawn(
56 self.get_argv()[0], // executing program 64 self.get_argv()[0], // executing program
57 self.get_argv().as_ptr() as *mut *const c_char, // argv 65 self.get_argv().as_ptr() as *mut *const c_char, // argv
58 *sys::os::environ() as *mut *const c_char, 66 c_envp as *mut *const c_char,
59 100 as c_int, // initial priority 67 100 as c_int, // initial priority
60 thread::min_stack(), // initial stack size. 68 stack_size, // initial stack size.
61 0, // options 69 0, // options
62 0 // task options 70 0 // task options
63 ); 71 );