-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fallback to 256 w/ no host name max on unix #16
Conversation
POSIX allows sysconf(_SC_HOST_NAME_MAX) to return -1 in case no limit is set; in such cases, fallback to an hardcoded 256 as limit for the buffer used for libc::gethostname(), which should be more than good enough in most/all the cases. This fixes gethostname() on GNU/Hurd, where there is no limit defined by sysconf(_SC_HOST_NAME_MAX).
How do you know that 256 is "enough in most cases"? If we're into POSIX, what does POSIX say about the size of a hostname? What does POSIX say how one's supposed to determine the size of a hostname if sysconf returns -1 for the size of a hostname? |
The
So it cannot be relied on, and it is not exposed by the libc crate.
Looking at the
Hence, the value I hardcoded generally fits the general situation. A non-hardcoding way for this would be:
[1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/gethostname.html |
So POSIX says in absence of a sysconf value we should fall back to If this turns out to be a proper mess, I'd rather just go for rustix (see #10), which abstracts the whole thing for us 🙂 |
In practice, yes.
See my comment above: it seems not.
It seems that it does not provide
IMHO my simple fallback is a good enough solution for corner cases; otherwise, switching this crate to |
Another possibility could be to use |
See #17 for rustix. |
POSIX allows
sysconf(_SC_HOST_NAME_MAX)
to return -1 in case no limit is set; in such cases, fallback to an hardcoded 256 as limit for the buffer used forlibc::gethostname()
, which should be more than good enough in most/all the cases.This fixes
gethostname()
on GNU/Hurd, where there is no limit defined bysysconf(_SC_HOST_NAME_MAX)
.