summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBaoshanPang <pangbw@gmail.com>2019-11-20 14:24:59 -0800
committerBaoshanPang <pangbw@gmail.com>2019-11-22 14:36:54 -0800
commitde362b41468f71ab7b1843007f97d4c1ca93638c (patch)
tree8b1d729ff9bc4108df3cb8c11344f7a633bd8e20
parentpass the captured environment variables to rtpSpawn (diff)
downloadrust-de362b41468f71ab7b1843007f97d4c1ca93638c.tar.gz
rust-de362b41468f71ab7b1843007f97d4c1ca93638c.tar.bz2
rust-de362b41468f71ab7b1843007f97d4c1ca93638c.tar.xz
ensure that access to the environment is synchronized
-rw-r--r--src/libstd/sys/vxworks/os.rs19
-rw-r--r--src/libstd/sys/vxworks/process/process_vxworks.rs8
2 files changed, 18 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 1ec318e..79bfd77 100644
--- a/src/libstd/sys/vxworks/process/process_vxworks.rs
+++ b/src/libstd/sys/vxworks/process/process_vxworks.rs
@@ -55,13 +55,17 @@ impl Command {
55 55
56 let c_envp = envp.as_ref().map(|c| c.as_ptr()) 56 let c_envp = envp.as_ref().map(|c| c.as_ptr())
57 .unwrap_or_else(|| *sys::os::environ() as *const _); 57 .unwrap_or_else(|| *sys::os::environ() as *const _);
58 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
59 let ret = libc::rtpSpawn( 63 let ret = libc::rtpSpawn(
60 self.get_argv()[0], // executing program 64 self.get_argv()[0], // executing program
61 self.get_argv().as_ptr() as *mut *const c_char, // argv 65 self.get_argv().as_ptr() as *mut *const c_char, // argv
62 c_envp as *mut *const c_char, 66 c_envp as *mut *const c_char,
63 100 as c_int, // initial priority 67 100 as c_int, // initial priority
64 thread::min_stack(), // initial stack size. 68 stack_size, // initial stack size.
65 0, // options 69 0, // options
66 0 // task options 70 0 // task options
67 ); 71 );