pub fn current_exe() -> Result<PathBuf>
Returns the full filesystem path of the current running executable.
If the executable was invoked through a symbolic link, some platforms will return the path of the symbolic link and other platforms will return the path of the symbolic link’s target.
If the executable is renamed while it is running, platforms may return the path at the time it was loaded instead of the new path.
Acquiring the path of the current executable is a platform-specific operation that can fail for a good number of reasons. Some errors can include, but not be limited to, filesystem operations failing or general syscall failures.
The output of this function should not be trusted for anything that might have security implications. Basically, if users can run the executable, they can change the output arbitrarily.
As an example, you can easily introduce a race condition. It goes like this:
current_exe(), and store it in a variable.You expected to safely execute the current executable, but you’re instead executing something completely different. The code you just executed run with your privileges.
This sort of behavior has been known to lead to privilege escalation when used incorrectly.
use std::env;
match env::current_exe() {
Ok(exe_path) => println!("Path of this executable is: {}",
exe_path.display()),
Err(e) => println!("failed to get current exe path: {e}"),
};
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/env/fn.current_exe.html