diff --git a/Cargo.toml b/Cargo.toml index c7df57e..33ccc56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ categories = ["os", "api-bindings"] rust-version = "1.64" [target.'cfg(not(windows))'.dependencies] -libc = "0.2.141" +rustix = { version = "0.37.17", default-features = false, features = ["std", "process"] } [target.'cfg(windows)'.dependencies] windows = { version = "0.48.0", features = [ diff --git a/src/lib.rs b/src/lib.rs index 08a552b..28123cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -60,29 +60,10 @@ pub fn gethostname() -> OsString { #[cfg(unix)] #[inline] fn gethostname_impl() -> OsString { - use libc::{c_char, sysconf, _SC_HOST_NAME_MAX}; use std::os::unix::ffi::OsStringExt; - // Get the maximum size of host names on this system, and account for the - // trailing NUL byte. - let hostname_max = unsafe { sysconf(_SC_HOST_NAME_MAX) }; - let mut buffer = vec![0; (hostname_max as usize) + 1]; - let returncode = unsafe { libc::gethostname(buffer.as_mut_ptr() as *mut c_char, buffer.len()) }; - if returncode != 0 { - // There are no reasonable failures, so lets panic - panic!( - "gethostname failed: {} - Please report an issue to !", - std::io::Error::last_os_error() - ); - } - // We explicitly search for the trailing NUL byte and cap at the buffer - // length: If the buffer's too small (which shouldn't happen since we - // explicitly use the max hostname size above but just in case) POSIX - // doesn't specify whether there's a NUL byte at the end, so if we didn't - // check we might read from memory that's not ours. - let end = buffer.iter().position(|&b| b == 0).unwrap_or(buffer.len()); - buffer.resize(end, 0); - OsString::from_vec(buffer) + + let bytes = rustix::process::uname().nodename().to_bytes().to_vec(); + OsString::from_vec(bytes) } #[cfg(windows)]