Skip to content

Commit

Permalink
Rollup merge of rust-lang#68515 - Wind-River:master_2020, r=alexcrichton
Browse files Browse the repository at this point in the history
Support feature process_set_argv0 for VxWorks

r? @alexcrichton
  • Loading branch information
JohnTitor authored Jan 24, 2020
2 parents b9c86a3 + 06af442 commit f998e27
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/libstd/sys/vxworks/ext/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![stable(feature = "rust1", since = "1.0.0")]

use crate::ffi::OsStr;
use crate::io;
use crate::process;
use crate::sys;
Expand Down Expand Up @@ -105,6 +106,15 @@ pub trait CommandExt {
/// cross-platform `spawn` instead.
#[stable(feature = "process_exec2", since = "1.9.0")]
fn exec(&mut self) -> io::Error;

/// Set executable argument
///
/// Set the first process argument, `argv[0]`, to something other than the
/// default executable path.
#[unstable(feature = "process_set_argv0", issue = "66510")]
fn arg0<S>(&mut self, arg: S) -> &mut process::Command
where
S: AsRef<OsStr>;
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -130,6 +140,14 @@ impl CommandExt for process::Command {
fn exec(&mut self) -> io::Error {
self.as_inner_mut().exec(sys::process::Stdio::Inherit)
}

fn arg0<S>(&mut self, arg: S) -> &mut process::Command
where
S: AsRef<OsStr>,
{
self.as_inner_mut().set_arg_0(arg.as_ref());
self
}
}

/// Unix-specific extensions to [`process::ExitStatus`].
Expand Down
24 changes: 20 additions & 4 deletions src/libstd/sys/vxworks/process/process_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ impl Command {
let program = os2c(program, &mut saw_nul);
Command {
argv: Argv(vec![program.as_ptr(), ptr::null()]),
args: vec![program.clone()],
program,
args: Vec::new(),
env: Default::default(),
cwd: None,
uid: None,
Expand All @@ -104,11 +104,19 @@ impl Command {
}
}

pub fn set_arg_0(&mut self, arg: &OsStr) {
// Set a new arg0
let arg = os2c(arg, &mut self.saw_nul);
debug_assert!(self.argv.0.len() > 1);
self.argv.0[0] = arg.as_ptr();
self.args[0] = arg;
}

pub fn arg(&mut self, arg: &OsStr) {
// Overwrite the trailing NULL pointer in `argv` and then add a new null
// pointer.
let arg = os2c(arg, &mut self.saw_nul);
self.argv.0[self.args.len() + 1] = arg.as_ptr();
self.argv.0[self.args.len()] = arg.as_ptr();
self.argv.0.push(ptr::null());

// Also make sure we keep track of the owned value to schedule a
Expand All @@ -133,6 +141,10 @@ impl Command {
&self.argv.0
}

pub fn get_program(&self) -> &CStr {
&*self.program
}

#[allow(dead_code)]
pub fn get_cwd(&self) -> &Option<CString> {
&self.cwd
Expand Down Expand Up @@ -315,8 +327,12 @@ impl ChildStdio {

impl fmt::Debug for Command {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.program)?;
for arg in &self.args {
if self.program != self.args[0] {
write!(f, "[{:?}] ", self.program)?;
}
write!(f, "{:?}", self.args[0])?;

for arg in &self.args[1..] {
write!(f, " {:?}", arg)?;
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/vxworks/process/process_vxworks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Command {
let _lock = sys::os::env_lock();

let ret = libc::rtpSpawn(
self.get_argv()[0], // executing program
self.get_program().as_ptr(),
self.get_argv().as_ptr() as *mut *const c_char, // argv
c_envp as *mut *const c_char,
100 as c_int, // initial priority
Expand Down

0 comments on commit f998e27

Please sign in to comment.