Skip to content
New issue

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

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

Already on GitHub? # to your account

NetBSD: add sysctl backend for std::env::current_exe #46373

Merged
merged 1 commit into from
Dec 2, 2017

Conversation

jakllsch
Copy link
Contributor

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.

@rust-highfive
Copy link
Collaborator

r? @kennytm

(rust_highfive has picked a reviewer for you, use r? to override)

@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 29, 2017
}
Err(io::Error::new(io::ErrorKind::Other, "/proc/curproc/exe doesn't point to regular file."))
}
sysctl().or(procfs())
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK

}
}
fn procfs() -> io::Result<PathBuf> {
let curproc_exe = PathBuf::from("/proc/curproc/exe");
Copy link
Member

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?

Copy link
Contributor Author

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
Copy link
Member

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).

Copy link
Contributor Author

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.

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."))
Copy link
Member

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

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'll wrap that line.

@jakllsch
Copy link
Contributor Author

CI might have missed this:

error[E0277]: the trait bound core::result::Result<path::PathBuf, io::error::Error>: core::ops::FnOnce<(io::error::Error,)> is not satisfied
--> /local/jakllsch/rust/rust/src/libstd/sys/unix/os.rs:253:14
|
253 | sysctl().or_else(procfs())
| ^^^^^^^ the trait core::ops::FnOnce<(io::error::Error,)> is not implemented for core::result::Result<path::PathBuf, io::error::Error>

@kennytm
Copy link
Member

kennytm commented Nov 29, 2017

The CI for PR only runs on Linux, so it will definitely miss any change made to NetBSD.

Err(io::Error::new(io::ErrorKind::Other,
"/proc/curproc/exe doesn't point to regular file."))
}
sysctl().or_else(procfs())
Copy link
Member

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 ().

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 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..

Copy link
Member

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.

@kennytm
Copy link
Member

kennytm commented Nov 30, 2017

@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.
@jakllsch jakllsch force-pushed the netbsd-kern_proc_pathname branch from 8de0cde to ccef969 Compare November 30, 2017 18:18
@jakllsch
Copy link
Contributor Author

@kennytm I've squashed it.

@kennytm
Copy link
Member

kennytm commented Nov 30, 2017

Thanks for the contribution!

@bors r+

@bors
Copy link
Contributor

bors commented Nov 30, 2017

📌 Commit ccef969 has been approved by kennytm

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 30, 2017
kennytm added a commit to kennytm/rust that referenced this pull request Dec 1, 2017
… 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.
bors added a commit that referenced this pull request Dec 1, 2017
Rollup of 13 pull requests

- Successful merges: #45880, #46280, #46373, #46376, #46385, #46386, #46387, #46392, #46400, #46401, #46405, #46412, #46421
- Failed merges:
@bors bors merged commit ccef969 into rust-lang:master Dec 2, 2017
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants