Skip to content

Commit

Permalink
Add references
Browse files Browse the repository at this point in the history
  • Loading branch information
notgull committed Apr 29, 2023
1 parent c8a650a commit 2b5467b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 2b5467b

Please sign in to comment.