-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
NetBSD: add sysctl backend for std::env::current_exe #46373
Conversation
r? @kennytm (rust_highfive has picked a reviewer for you, use r? to override) |
src/libstd/sys/unix/os.rs
Outdated
} | ||
Err(io::Error::new(io::ErrorKind::Other, "/proc/curproc/exe doesn't point to regular file.")) | ||
} | ||
sysctl().or(procfs()) |
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.
Use sysctl().or_else(procfs)
here, otherwise procfs()
will always be called.
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.
OK
src/libstd/sys/unix/os.rs
Outdated
} | ||
} | ||
fn procfs() -> io::Result<PathBuf> { | ||
let curproc_exe = PathBuf::from("/proc/curproc/exe"); |
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.
Why a PathBuf
instead of Path
?
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.
For no reason other than to match the linux,android,emscripten current_exe(). I had path::Path::new() in a previous version of this. I'll restore it.
cvt(libc::sysctl(mib.as_ptr(), mib.len() as ::libc::c_uint, | ||
path.as_ptr() as *mut libc::c_void, &mut path_len, | ||
ptr::null(), 0))?; | ||
path.set_len(path_len - 1); // chop off NUL |
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.
Do you need to check path_len == 0
just like the FreeBSD implementation above? Or is it possible to merge the implementations of the BSDs (only the mib
differ as far as I can see).
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.
Possibly could even check for path_len <= 1 for additional safety. There are a few very subtle differences between the FreeBSD & DragonFly block and what I implemented, but I imagine they could be merged if thought best. While they are nearly identical, I have this purist view that as there is no shared lineage in the kernel-side implementation of these sysctls, they may as well stay seperate here.
src/libstd/sys/unix/os.rs
Outdated
if curproc_exe.is_file() { | ||
return ::fs::read_link(curproc_exe); | ||
} | ||
Err(io::Error::new(io::ErrorKind::Other, "/proc/curproc/exe doesn't point to regular file.")) |
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.
This line is too long, causing CI to fail :(
[00:04:06] tidy error: /checkout/src/libstd/sys/unix/os.rs:246: line longer than 100 chars
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.
I'll wrap that line.
CI might have missed this: error[E0277]: the trait bound |
The CI for PR only runs on Linux, so it will definitely miss any change made to NetBSD. |
src/libstd/sys/unix/os.rs
Outdated
Err(io::Error::new(io::ErrorKind::Other, | ||
"/proc/curproc/exe doesn't point to regular file.")) | ||
} | ||
sysctl().or_else(procfs()) |
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.
sysctl().or_else(procfs)
without the last ()
.
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.
I had tried that and got:
error[E0593]: function is expected to take 1 argument, but it takes 0 arguments
--> /local/jakllsch/rust/rust/src/libstd/sys/unix/os.rs:253:14
|
253 | sysctl().or_else(procfs)
| ^^^^^^^ expected function that takes 1 argument
error: aborting due to previous error
I'd really like to keep the prototypes/signatures of sysctl() and procfs() identical..
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.
@jakllsch Oh ok. Call through a closure sysctl().or_else(|_| procfs())
then.
@jakllsch Are we supposed to wait for you to squash the commits or can these be merged as-is? |
Use the CTL_KERN.KERN_PROC_ARGS.-1.KERN_PROC_PATHNAME sysctl in preference over the /proc/curproc/exe symlink. Additionally, perform more validation of aformentioned symlink. Particularly on pre-8.x NetBSD this symlink will point to '/' when accurate information is unavailable.
8de0cde
to
ccef969
Compare
@kennytm I've squashed it. |
Thanks for the contribution! @bors r+ |
📌 Commit ccef969 has been approved by |
… r=kennytm NetBSD: add sysctl backend for std::env::current_exe Use the CTL_KERN.KERN_PROC_ARGS.-1.KERN_PROC_PATHNAME sysctl in preference over the /proc/curproc/exe symlink. Additionally, perform more validation of aformentioned symlink. Particularly on pre-8.x NetBSD this symlink will point to '/' when accurate information is unavailable.
Use the CTL_KERN.KERN_PROC_ARGS.-1.KERN_PROC_PATHNAME sysctl in
preference over the /proc/curproc/exe symlink.
Additionally, perform more validation of aformentioned symlink.
Particularly on pre-8.x NetBSD this symlink will point to '/' when
accurate information is unavailable.