-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
std: allocate less memory in current_exe for OpenBSD
#158183
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -234,18 +234,31 @@ pub fn current_exe() -> io::Result<PathBuf> { | |
| unsafe { | ||
| let mut mib = [libc::CTL_KERN, libc::KERN_PROC_ARGS, libc::getpid(), libc::KERN_PROC_ARGV]; | ||
| let mib = mib.as_mut_ptr(); | ||
| let mut argv_len = 0; | ||
| cvt(libc::sysctl(mib, 4, ptr::null_mut(), &mut argv_len, ptr::null_mut(), 0))?; | ||
| let mut argv = Vec::<*const libc::c_char>::with_capacity(argv_len as usize); | ||
| cvt(libc::sysctl(mib, 4, argv.as_mut_ptr() as *mut _, &mut argv_len, ptr::null_mut(), 0))?; | ||
| argv.set_len(argv_len as usize); | ||
|
|
||
| // Determine the required size (in bytes) for the argument array ... | ||
| let mut argv_size = 0; | ||
| cvt(libc::sysctl(mib, 4, ptr::null_mut(), &mut argv_size, ptr::null_mut(), 0))?; | ||
|
|
||
| // ... allocate a buffer for it ... | ||
| let argc = argv_size.div_exact(size_of::<*const libc::c_char>()).unwrap(); | ||
| let mut argv = Vec::<*const libc::c_char>::with_capacity(argc); | ||
|
|
||
| // ... and retrieve the argument array. | ||
| cvt(libc::sysctl(mib, 4, argv.as_mut_ptr() as *mut _, &mut argv_size, ptr::null_mut(), 0))?; | ||
| let argc = argv_size.div_exact(size_of::<*const libc::c_char>()).unwrap(); | ||
| argv.set_len(argc); | ||
|
|
||
| if argv[0].is_null() { | ||
| return Err(io::const_error!(io::ErrorKind::Uncategorized, "no current exe available")); | ||
| } | ||
| let argv0 = CStr::from_ptr(argv[0]).to_bytes(); | ||
| if argv0[0] == b'.' || argv0.iter().any(|b| *b == b'/') { | ||
| if argv0.iter().any(|b| *b == b'/') { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess technically we should FCP this breakage, let me nominate for libs-api to decide.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We discussed this in the @rust-lang/libs-api meeting and consider this a bug fix, not something that needs an FCP. There's no reason to check for a leading |
||
| // The program name is path-like, so try to canonicalize it. | ||
| crate::fs::canonicalize(OsStr::from_bytes(argv0)) | ||
| } else { | ||
| // The program was probably found in the PATH. Instead of trying to | ||
| // find it again (which might not succeed if PATH has changed), just | ||
| // return the program name – this function is best-effort anyway. | ||
| Ok(PathBuf::from(OsStr::from_bytes(argv0))) | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Studying https://man.openbsd.org/sysctl.2#KERN_PROC_ARGS
...So uh is there a reason, since we have to make two
sysctlcalls, that we don't just ask forKERN_PROC_NARGVthe first time?...though "
KERN_PROC_NARGVandKERN_PROC_NENVreturn the number of elements as anintin the argv or env array." has got to be the most curious phrasing possible for that.View changes since the review