From 2b5467bb21ef693f0f77cf4fac1c3fde5ad215d1 Mon Sep 17 00:00:00 2001 From: jtnunley Date: Sat, 29 Apr 2023 09:39:26 -0700 Subject: [PATCH] Add references --- src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 28123cc..c436c9b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,6 +62,24 @@ pub fn gethostname() -> OsString { fn gethostname_impl() -> OsString { use std::os::unix::ffi::OsStringExt; + // libc::gethostname() is implemented in both musl and glibc by calling uname() and copying + // the nodename field into the buffer. + // + // glibc: + // https://github.com/lattera/glibc/blob/895ef79e04a953cac1493863bcae29ad85657ee1/sysdeps/posix/gethostname.c#L32-L36 + // musl: + // https://github.com/cloudius-systems/musl/blob/00733dd1cf791d13ff6155509cf139a5f7b2eecb/src/unistd/gethostname.c#L4-L13 + // + // On FreeBSD, libc::gethostname() is a sysctl call to KERN_HOSTNAME. uname() fetches the nodename + // using the same strategy. + // + // freebsd gethostname: + // https://github.com/FreeBSDDesktop/freebsd-base/blob/de1aa3dab23c06fec962a14da3e7b4755c5880cf/lib/libc/gen/gethostname.c#L47-L54 + // freebsd uname: + // https://github.com/FreeBSDDesktop/freebsd-base/blob/de1aa3dab23c06fec962a14da3e7b4755c5880cf/lib/libc/gen/__xuname.c#L73-L83 + // + // OpenBSD uses the same strategy, so I doubt that other implementations of uname() among the BSDs + // stray too far from this pattern. let bytes = rustix::process::uname().nodename().to_bytes().to_vec(); OsString::from_vec(bytes) }