about summary refs log tree commit diff
path: root/src/libstd/sys/unix/process/process_common.rs
diff options
context:
space:
mode:
authorJeremy Fitzhardinge <jeremy@goop.org>2019-11-17 22:56:13 -0800
committerJeremy Fitzhardinge <jsgf@fb.com>2019-11-19 11:01:52 -0800
commit6dee1a5a9f0e5a85ac294c7473c247e33b63284c (patch)
tree5b83f12570bea1b09ce9d53b56798dd2b5c28a04 /src/libstd/sys/unix/process/process_common.rs
parent361791bb5fdd714bdc39f8af835f6468dd18331d (diff)
downloadrust-6dee1a5a9f0e5a85ac294c7473c247e33b63284c.tar.gz
rust-6dee1a5a9f0e5a85ac294c7473c247e33b63284c.zip
Add unix::process::CommandExt::arg0
This allows argv[0] to be overridden on the executable's command-line. This also makes the program
executed independent of argv[0].

Does Fuchsia have the same semantics?

Addresses: #66510
Diffstat (limited to 'src/libstd/sys/unix/process/process_common.rs')
-rw-r--r--src/libstd/sys/unix/process/process_common.rs23
1 files changed, 16 insertions, 7 deletions
diff --git a/src/libstd/sys/unix/process/process_common.rs b/src/libstd/sys/unix/process/process_common.rs
index 4edd2ebf8c5..0e6f96bb228 100644
--- a/src/libstd/sys/unix/process/process_common.rs
+++ b/src/libstd/sys/unix/process/process_common.rs
@@ -1,6 +1,6 @@
 use crate::os::unix::prelude::*;
 
-use crate::ffi::{OsString, OsStr, CString};
+use crate::ffi::{OsString, OsStr, CString, CStr};
 use crate::fmt;
 use crate::io;
 use crate::ptr;
@@ -11,10 +11,7 @@ use crate::sys_common::process::CommandEnv;
 use crate::collections::BTreeMap;
 
 #[cfg(not(target_os = "fuchsia"))]
-use {
-    crate::ffi::CStr,
-    crate::sys::fs::OpenOptions,
-};
+use crate::sys::fs::OpenOptions;
 
 use libc::{c_int, gid_t, uid_t, c_char, EXIT_SUCCESS, EXIT_FAILURE};
 
@@ -135,8 +132,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,
@@ -149,11 +146,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
@@ -178,6 +183,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