Skip to content

Commit 617b07e

Browse files
authored
Rollup merge of rust-lang#66649 - Wind-River:master_xyz, r=alexcrichton
VxWorks: fix issues in accessing environment variables
2 parents 234c9f2 + de362b4 commit 617b07e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/libstd/sys/vxworks/os.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ use crate::path::{self, PathBuf, Path};
1111
use crate::ptr;
1212
use crate::slice;
1313
use crate::str;
14-
use crate::sys_common::mutex::Mutex;
14+
use crate::sys_common::mutex::{Mutex, MutexGuard};
1515
use crate::sys::cvt;
1616
/*use sys::fd; this one is probably important */
1717
use crate::vec;
1818

1919
const TMPBUF_SZ: usize = 128;
20-
static ENV_LOCK: Mutex = Mutex::new();
21-
2220

2321
// This is a terrible fix
2422
use crate::sys::os_str::Buf;
@@ -200,11 +198,18 @@ pub unsafe fn environ() -> *mut *const *const c_char {
200198
&mut environ
201199
}
202200

201+
pub 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+
203208
/// Returns a vector of (variable, value) byte-vector pairs for all the
204209
/// environment variables of the current process.
205210
pub fn env() -> Env {
206211
unsafe {
207-
let _guard = ENV_LOCK.lock();
212+
let _guard = env_lock();
208213
let mut environ = *environ();
209214
if environ == ptr::null() {
210215
panic!("os::env() failure getting env string from OS: {}",
@@ -244,7 +249,7 @@ pub fn getenv(k: &OsStr) -> io::Result<Option<OsString>> {
244249
// always None as well
245250
let k = CString::new(k.as_bytes())?;
246251
unsafe {
247-
let _guard = ENV_LOCK.lock();
252+
let _guard = env_lock();
248253
let s = libc::getenv(k.as_ptr()) as *const libc::c_char;
249254
let ret = if s.is_null() {
250255
None
@@ -260,7 +265,7 @@ pub fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
260265
let v = CString::new(v.as_bytes())?;
261266

262267
unsafe {
263-
let _guard = ENV_LOCK.lock();
268+
let _guard = env_lock();
264269
cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(|_| ())
265270
}
266271
}
@@ -269,7 +274,7 @@ pub fn unsetenv(n: &OsStr) -> io::Result<()> {
269274
let nbuf = CString::new(n.as_bytes())?;
270275

271276
unsafe {
272-
let _guard = ENV_LOCK.lock();
277+
let _guard = env_lock();
273278
cvt(libc::unsetenv(nbuf.as_ptr())).map(|_| ())
274279
}
275280
}

src/libstd/sys/vxworks/process/process_vxworks.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ impl Command {
1515
-> io::Result<(Process, StdioPipes)> {
1616
use crate::sys::{cvt_r};
1717
const CLOEXEC_MSG_FOOTER: &'static [u8] = b"NOEX";
18+
let envp = self.capture_env();
1819

1920
if self.saw_nul() {
2021
return Err(io::Error::new(ErrorKind::InvalidInput,
@@ -52,12 +53,19 @@ impl Command {
5253
t!(cvt(libc::chdir(cwd.as_ptr())));
5354
}
5455

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+
5563
let ret = libc::rtpSpawn(
5664
self.get_argv()[0], // executing program
5765
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,
5967
100 as c_int, // initial priority
60-
thread::min_stack(), // initial stack size.
68+
stack_size, // initial stack size.
6169
0, // options
6270
0 // task options
6371
);

0 commit comments

Comments
 (0)