Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: add method to unix::process::CommandExt to set argv[0] #66510

Closed
jsgf opened this issue Nov 18, 2019 · 9 comments
Closed

Feature request: add method to unix::process::CommandExt to set argv[0] #66510

jsgf opened this issue Nov 18, 2019 · 9 comments
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@jsgf
Copy link
Contributor

jsgf commented Nov 18, 2019

In Unix, the first process argument is the executable name by convention. However, there are various occasions where you might want to set argv[0] to something other than the full executable path:

  • to set a normalized program name
  • to put multiple functions into one executable, selected by argv[0]
  • to put commentary on the execution context for a process
  • etc

I suggest adding a set_argv_0<S: AsRef<OsStr>(&mut self, arg: S) -> process::Command to std::os::unix::process::CommandExt to allow argv[0] to be overridden.

The default behaviour would remain unchanged.

It would require a little more implementation change - currently it always executes self.get_argv()[0], but this should be changed to self.program.as_ptr().

@jsgf

This comment has been minimized.

@jonas-schievink jonas-schievink added C-feature-request Category: A feature request, i.e: not implemented / a PR. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Nov 18, 2019
jsgf added a commit to jsgf/rust that referenced this issue Nov 18, 2019
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: rust-lang#66510
JohnTitor added a commit to JohnTitor/rust that referenced this issue Nov 19, 2019
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? I'm assuming so.

Addresses: rust-lang#66510
jsgf added a commit to jsgf/rust that referenced this issue Nov 19, 2019
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: rust-lang#66510
pietroalbini added a commit to pietroalbini/rust that referenced this issue Nov 25, 2019
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? I'm assuming so.

Addresses: rust-lang#66510
pietroalbini added a commit to pietroalbini/rust that referenced this issue Nov 25, 2019
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? I'm assuming so.

Addresses: rust-lang#66510
@bstrie
Copy link
Contributor

bstrie commented Jan 17, 2020

This can be closed in light of #66512 , correct?

@jsgf
Copy link
Contributor Author

jsgf commented Jan 17, 2020

yup

@jsgf jsgf closed this as completed Jan 17, 2020
@glandium
Copy link
Contributor

2 remarks:

  • The doc points to this issue, should this be reopen and become a tracking issue?
  • This is not a Unix-only feature. As a matter of fact, this also works on Windows (as playing with the executable argument of subprocess.Popen in python shows). So this should really be in std::process rather than std::os::unix::process.

@jsgf
Copy link
Contributor Author

jsgf commented Jan 20, 2020

@glandium I don't know much about Windows, and my assumption was that it wouldn't apply, at least not directly.

Looking at

let mut cmd_str = make_command_line(program, &self.args)?;
it doesn't seem program is passed to the system separately from the command-line at all, so passing a different value for the first argument will just override the executable being executed. In the Unix model, the path to the executable and the first argument passed to it are independent (though conventionally related).

@glandium
Copy link
Contributor

libstd passes null as first argument to CreateProcess

but it could be set to the actual executable, in which case the first "argument" in the command line would become argv0 in the launched process.
https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw

@JohnTitor
Copy link
Member

The doc points to this issue, should this be reopen and become a tracking issue?

I believe it should, re-opening.

@JohnTitor JohnTitor reopened this Feb 28, 2020
@JohnTitor JohnTitor added B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC and removed C-feature-request Category: A feature request, i.e: not implemented / a PR. labels Feb 28, 2020
@jsgf
Copy link
Contributor Author

jsgf commented May 11, 2020

@glandium I had a quick look at implementing it for Windows, but it seems pretty complex.

  • If we pass lpApplicationName, then Windows does minimal processing to it (nothing aside from adding the current dir if none is supplied). It doesn't add an extension.
  • The existing Rust impl does a path search and adds a .exe extension, but none of that used if it can't find the file. In that case it falls back to whatever processing Windows does on lpCommandLine to find the executable.
  • lpApplicationName must not be used for "16 bit applications" (.com files?). I don't know what happens with .bat files and the like - does it need to explicitly use cmd.exe as the lpApplicationName?

I had been hoping to spend a little bit of time to bring Windows to feature parity with Unix here, but I'm way out of my depth. I think this needs to be implemented and stabilized for Windows separately.

jsgf added a commit to jsgf/rust that referenced this issue May 12, 2020
This stabilizes process_set_argv0 targeting 1.45.0. It has been
useful in practice and seems useful as-is.

The equivalent feature could be implemented for Windows, but as far as I
know nobody has. That can be done separately.

Tracking issue: rust-lang#66510
RalfJung added a commit to RalfJung/rust that referenced this issue May 22, 2020
Stabilize process_set_argv0 feature for Unix

This stabilizes process_set_argv0 targeting 1.45.0. It has been
useful in practice and seems useful as-is.

The equivalent feature could be implemented for Windows, but as far as I
know nobody has. That can be done separately.

Tracking issue: rust-lang#66510
@KodrAus
Copy link
Contributor

KodrAus commented Jul 29, 2020

Since it looks like Windows support requires some more exploration and the feature as described has been stabilized, I'll go ahead and close this tracking issue. If anybody wants to drive Windows support that would be most welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants