From c85a0e524e171531770cdb04521a61033747b3c3 Mon Sep 17 00:00:00 2001 From: LinkTed Date: Mon, 26 Jul 2021 12:43:55 +0300 Subject: [PATCH] process: add arg0 method to Command (#3984) --- tokio/src/process/mod.rs | 18 ++++++++++++++++++ tokio/tests/process_arg0.rs | 13 +++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tokio/tests/process_arg0.rs diff --git a/tokio/src/process/mod.rs b/tokio/src/process/mod.rs index 96ceb6d8d..7ae503f51 100644 --- a/tokio/src/process/mod.rs +++ b/tokio/src/process/mod.rs @@ -551,6 +551,7 @@ impl Command { /// /// [1]: https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863(v=vs.85).aspx #[cfg(windows)] + #[cfg_attr(docsrs, doc(cfg(windows)))] pub fn creation_flags(&mut self, flags: u32) -> &mut Command { self.std.creation_flags(flags); self @@ -560,6 +561,7 @@ impl Command { /// `setuid` call in the child process. Failure in the `setuid` /// call will cause the spawn to fail. #[cfg(unix)] + #[cfg_attr(docsrs, doc(cfg(unix)))] pub fn uid(&mut self, id: u32) -> &mut Command { self.std.uid(id); self @@ -568,11 +570,26 @@ impl Command { /// Similar to `uid` but sets the group ID of the child process. This has /// the same semantics as the `uid` field. #[cfg(unix)] + #[cfg_attr(docsrs, doc(cfg(unix)))] pub fn gid(&mut self, id: u32) -> &mut Command { self.std.gid(id); self } + /// Set executable argument + /// + /// Set the first process argument, `argv[0]`, to something other than the + /// default executable path. + #[cfg(unix)] + #[cfg_attr(docsrs, doc(cfg(unix)))] + pub fn arg0(&mut self, arg: S) -> &mut Command + where + S: AsRef, + { + self.std.arg0(arg); + self + } + /// Schedules a closure to be run just before the `exec` function is /// invoked. /// @@ -603,6 +620,7 @@ impl Command { /// working directory have successfully been changed, so output to these /// locations may not appear where intended. #[cfg(unix)] + #[cfg_attr(docsrs, doc(cfg(unix)))] pub unsafe fn pre_exec(&mut self, f: F) -> &mut Command where F: FnMut() -> io::Result<()> + Send + Sync + 'static, diff --git a/tokio/tests/process_arg0.rs b/tokio/tests/process_arg0.rs new file mode 100644 index 000000000..4fabea0fe --- /dev/null +++ b/tokio/tests/process_arg0.rs @@ -0,0 +1,13 @@ +#![warn(rust_2018_idioms)] +#![cfg(all(feature = "full", unix))] + +use tokio::process::Command; + +#[tokio::test] +async fn arg0() { + let mut cmd = Command::new("sh"); + cmd.arg0("test_string").arg("-c").arg("echo $0"); + + let output = cmd.output().await.unwrap(); + assert_eq!(output.stdout, b"test_string\n"); +}