summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2023-09-21 13:13:46 -0400
committerAndrew Gallant <jamslam@gmail.com>2023-09-25 14:39:54 -0400
commit25a7145c79492e8dc89b6ece9c5214c37d60d23d (patch)
treef60e7169da7ab9fa3051be11015601fbee5a548c
parent19a08bee8a99e8c725b9781568013a9f686605a3 (diff)
cli: add new 'hostname' function
This will enable us to query for the current system's hostname in both Unix and Windows environments. We could have pulled in the 'gethostname' crate for this, but: 1. I'm not a huge fan of micro-crates. 2. The 'gethostname' crate panics if an error occurs. (Which, to be fair, an error should never occur, but it seems plausible on borked systems? ripgrep runs in a lot of places, so I'd rather not take the chance of a panic bringing down ripgrep for an optional convenience feature.) 3. The 'gethostname' crate uses the 'windows-targets' crate from Microsoft. This is arguably the "right" thing to do, but ripgrep doesn't use them yet and they appear high-churn. So I just added a safe wrapper to do this to winapi-util[1] and then inlined the Unix version here. This brings in no extra dependencies and the routine is fallible so that callers can recover from potentially strange failures. [1]: https://github.com/BurntSushi/winapi-util/pull/14
-rw-r--r--.github/workflows/ci.yml4
-rw-r--r--Cargo.lock1
-rw-r--r--crates/cli/Cargo.toml3
-rw-r--r--crates/cli/src/hostname.rs85
-rw-r--r--crates/cli/src/lib.rs2
5 files changed, 95 insertions, 0 deletions
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index bf8c2004..a98a2f56 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -193,6 +193,10 @@ jobs:
shell: bash
run: ci/test-complete
+ - name: Print hostname detected by grep-cli crate
+ shell: bash
+ run: ${{ env.CARGO }} test --manifest-path crates/cli/Cargo.toml ${{ env.TARGET_FLAGS }} --lib print_hostname -- --nocapture
+
rustfmt:
runs-on: ubuntu-latest
steps:
diff --git a/Cargo.lock b/Cargo.lock
index 2f30fbfd..f2019025 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -187,6 +187,7 @@ version = "0.1.9"
dependencies = [
"bstr",
"globset",
+ "libc",
"log",
"termcolor",
"winapi-util",
diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml
index 0ce69873..8e576b66 100644
--- a/crates/cli/Cargo.toml
+++ b/crates/cli/Cargo.toml
@@ -21,3 +21,6 @@ termcolor = "1.3.0"
[target.'cfg(windows)'.dependencies.winapi-util]
version = "0.1.6"
+
+[target.'cfg(unix)'.dependencies.libc]
+version = "0.2.148"
diff --git a/crates/cli/src/hostname.rs b/crates/cli/src/hostname.rs
new file mode 100644
index 00000000..37ad54c7
--- /dev/null
+++ b/crates/cli/src/hostname.rs
@@ -0,0 +1,85 @@
+use std::{ffi::OsString, io};
+
+/// Returns the hostname of the current system.
+///
+/// It is unusual, although technically possible, for this routine to return
+/// an error. It is difficult to list out the error conditions, but one such
+/// possibility is platform support.
+///
+/// # Platform specific behavior
+///
+/// On Windows, this currently uses the "physical DNS hostname" computer name.
+/// This may change in the future.
+///
+/// On Unix, this returns the result of the `gethostname` function from the
+/// `libc` linked into the program.
+pub fn hostname() -> io::Result<OsString> {
+ #[cfg(windows)]
+ {
+ use winapi_util::sysinfo::{get_computer_name, ComputerNameKind};
+ get_computer_name(ComputerNameKind::PhysicalDnsHostname)
+ }
+ #[cfg(unix)]
+ {
+ gethostname()
+ }
+ #[cfg(not(any(windows, unix)))]
+ {
+ io::Error::new(
+ io::ErrorKind::Other,
+ "hostname could not be found on unsupported platform",
+ )
+ }
+}
+
+#[cfg(unix)]
+fn gethostname() -> io::Result<OsString> {
+ use std::os::unix::ffi::OsStringExt;
+
+ // SAFETY: There don't appear to be any safety requirements for calling
+ // sysconf.
+ let limit = unsafe { libc::sysconf(libc::_SC_HOST_NAME_MAX) };
+ if limit == -1 {
+ // It is in theory possible for sysconf to return -1 for a limit but
+ // *not* set errno, in which case, io::Error::last_os_error is
+ // indeterminate. But untangling that is super annoying because std
+ // doesn't expose any unix-specific APIs for inspecting the errno. (We
+ // could do it ourselves, but it just doesn't seem worth doing?)
+ return Err(io::Error::last_os_error());
+ }
+ let Ok(maxlen) = usize::try_from(limit) else {
+ let msg = format!("host name max limit ({}) overflowed usize", limit);
+ return Err(io::Error::new(io::ErrorKind::Other, msg));
+ };
+ // maxlen here includes the NUL terminator.
+ let mut buf = vec![0; maxlen];
+ // SAFETY: The pointer we give is valid as it is derived directly from a
+ // Vec. Similarly, `maxlen` is the length of our Vec, and is thus valid
+ // to write to.
+ let rc = unsafe {
+ libc::gethostname(buf.as_mut_ptr().cast::<libc::c_char>(), maxlen)
+ };
+ if rc == -1 {
+ return Err(io::Error::last_os_error());
+ }
+ // POSIX says that if the hostname is bigger than `maxlen`, then it may
+ // write a truncate name back that is not necessarily NUL terminated (wtf,
+ // lol). So if we can't find a NUL terminator, then just give up.
+ let Some(zeropos) = buf.iter().position(|&b| b == 0) else {
+ let msg = "could not find NUL terminator in hostname";
+ return Err(io::Error::new(io::ErrorKind::Other, msg));
+ };
+ buf.truncate(zeropos);
+ buf.shrink_to_fit();
+ Ok(OsString::from_vec(buf))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn print_hostname() {
+ println!("{:?}", hostname().unwrap());
+ }
+}
diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs
index a16d4c7d..b335a3f5 100644
--- a/crates/cli/src/lib.rs
+++ b/crates/cli/src/lib.rs
@@ -144,6 +144,7 @@ error message is crafted that typically tells the user how to fix the problem.
mod decompress;
mod escape;
+mod hostname;
mod human;
mod pattern;
mod process;
@@ -155,6 +156,7 @@ pub use crate::{
DecompressionReader, DecompressionReaderBuilder,
},
escape::{escape, escape_os, unescape, unescape_os},
+ hostname::hostname,
human::{parse_human_readable_size, ParseSizeError},
pattern::{
pattern_from_bytes, pattern_from_os, patterns_from_path,