Function gethostname::gethostname [−][src]
pub fn gethostname() -> OsString
Get the standard host name for the current machine.
On Unix simply wrap POSIX gethostname in a safe interface. On Windows
return the DNS host name of the local computer, as returned by
GetComputerNameExW with ComputerNamePhysicalDnsHostname
as NameType
.
This function panics if the buffer allocated for the hostname result of the operating system is too small; however we take great care to allocate a buffer of sufficient size:
- On Unix we allocate the buffer using the maximum permitted hostname size,
as returned by sysconf via
sysconf(_SC_HOST_NAME_MAX)
, plus an extra byte for the trailing NUL byte. A hostname cannot exceed this limit, so this function can’t realistically panic. - On Windows we call
GetComputerNameExW
with a NULL buffer first, which makes it return the length of the current host name. We then use this length to allocate a buffer for the actual result; this leaves a tiny tiny race condition in case the hostname changes to a longer name right in between those two calls but that’s a risk we don’t consider of any practical relevance.
Hence if this function does panic please report an issue.