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

Implement current_exe for AIX #104521

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions library/std/src/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ pub fn current_exe() -> io::Result<PathBuf> {
super::unsupported::unsupported()
}

#[cfg(target_os = "fuchsia")]
#[cfg(any(target_os = "fuchsia", target_os = "aix"))]
pub fn current_exe() -> io::Result<PathBuf> {
use crate::io::ErrorKind;

Expand All @@ -468,7 +468,35 @@ pub fn current_exe() -> io::Result<PathBuf> {
let path = PathBuf::from(exe_path);

// Prepend the current working directory to the path if it's not absolute.
if !path.is_absolute() { getcwd().map(|cwd| cwd.join(path)) } else { Ok(path) }
if cfg!(target_os = "fuchsia") {
if !path.is_absolute() {
return getcwd().map(|cwd| cwd.join(path));
} else {
return Ok(path);
}
}

if path.is_absolute() {
return path.canonicalize();
}

// Search PWD to infer current_exe.
if let Some(pstr) = path.to_str() && pstr.contains("/") {
return getcwd().map(|cwd| cwd.join(path))?.canonicalize();
}

// Search PATH to infer current_exe.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we searching PATH and PWD on other platforms today? I'm not sure we should do that here -- it increases the amount of work done and if it's not done on other platforms is somewhat inconsistent (and could have false positives much more easily).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we searching PATH and PWD on other platforms today?

I don't know much about other rare seen platforms. On AIX, we don't have information like /proc/self/exe in /proc filesystem. We are using similar method in LLVM on AIX right now, see https://github.com/llvm/llvm-project/blob/42b21ddaadd3545945f29a8ccdcc89779542c30e/llvm/lib/Support/Unix/Path.inc#L251. (Actually, we don't have /proc/curproc/file on AIX).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but you're using argv[0] just above which is what we do on other platforms (AFAICT, looking in this file, for example).

I am inclined to leave it there unless there's strong motivation for doing this cwd/path traversal. current_exe is documented as fallible for a reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I extended the one for fuchsia. Correct me if I still failed to get your point. :)

Copy link
Member

@Mark-Simulacrum Mark-Simulacrum Dec 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies if I wasn't clear. I think we should remove the PATH searching from this function, just returning the passed argument. IOW, I think if just adding aix to the fuchsia function works, then that seems like the right initial implementation here. (Not adding any other code to it).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove the PATH searching from this function, just returning the passed argument.

Could you please elaborate more about your concerns of searching in PATH here? Cuz on AIX, we don't have other methods to find the executable file of current process if argv0 is not an absolute path.

if let Some(p) = getenv(OsStr::from_bytes("PATH".as_bytes())) {
for search_path in split_paths(&p) {
let pb = search_path.join(&path);
if pb.is_file() && let Ok(metadata) = crate::fs::metadata(&pb) &&
metadata.permissions().mode() & 0o111 != 0 {
return pb.canonicalize();
}
}
}

return Err(io::const_io_error!(ErrorKind::Uncategorized, "an executable path was not found"));
}

pub struct Env {
Expand Down