]> git.lizzy.rs Git - rust.git/commitdiff
Support feature process_set_argv0 for VxWorks
authorBaoshanPang <pangbw@gmail.com>
Fri, 17 Jan 2020 17:29:09 +0000 (09:29 -0800)
committerBaoshanPang <pangbw@gmail.com>
Fri, 24 Jan 2020 17:59:59 +0000 (09:59 -0800)
src/libstd/sys/vxworks/ext/process.rs
src/libstd/sys/vxworks/process/process_common.rs
src/libstd/sys/vxworks/process/process_vxworks.rs

index e535c4aa122f0fe6d034e6b833eee0cfcf6da834..31e691dd1360b53df94ed31b3793d521f4efc812 100644 (file)
@@ -2,6 +2,7 @@
 
 #![stable(feature = "rust1", since = "1.0.0")]
 
+use crate::ffi::OsStr;
 use crate::io;
 use crate::process;
 use crate::sys;
@@ -105,6 +106,15 @@ fn before_exec<F>(&mut self, f: F) -> &mut process::Command
     /// 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")]
@@ -130,6 +140,14 @@ unsafe fn pre_exec<F>(&mut self, f: F) -> &mut 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`].
index a8139a27537a973611a6abe0eeea927118872b1d..6d5506bec5f7dbceaa14217dafa77ce009f7b59b 100644 (file)
@@ -90,8 +90,8 @@ pub fn new(program: &OsStr) -> 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,
@@ -104,11 +104,19 @@ pub fn new(program: &OsStr) -> 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
@@ -133,6 +141,10 @@ pub fn get_argv(&self) -> &Vec<*const c_char> {
         &self.argv.0
     }
 
+    pub fn get_program(&self) -> &CStr {
+        &*self.program
+    }
+
     #[allow(dead_code)]
     pub fn get_cwd(&self) -> &Option<CString> {
         &self.cwd
@@ -315,8 +327,12 @@ pub fn fd(&self) -> Option<c_int> {
 
 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(())
index ced70dea27f9952921bd74d8911abb0f8f91c6fc..f7e84ae3de9c78ab328b2e9eba61a3e75fde2430 100644 (file)
@@ -67,7 +67,7 @@ macro_rules! t {
             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