shims: add a shim for gethostname#5145
Open
faukah wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a shim for gethostname().
Closes #5138.
Behavior differs between targets, so I had to match on Os and on Glibc/Musl. I don't know whether
throw_unsup_format!()is the correct way of handling all other cases.I'm not quite sure how Solaris behaves, but I assume it matches the behavior of Illumos. Also not quite sure if MacOS behavior is correct. It can in theory return
ENAMETOOLONG, but docs and source clash a bit here:Manpage:
Source code:
As far as I can see, if
sysctl()succeeds, i.e. the hostname is copied intolocal_buf, andnameis too small, it still would put a NULL-terminated part of the hostname intonameand return successfully.A few notes on the
matchstatement:@ Android
Android returns error code
ENAMETOOLONGwithout writing anything toname, see here:@ GNU libc, FreeBSD
GNU libc and FreeBSD behave the same, see libc source and FreeBSD source. Both GNU libc and FreeBSD write a trunctuated, non-NULL-terminated prefix of the hostname before returning:
GNU libc:
FreeBSD:
@ Musl, MacOS, Solaris, Illumos
Musl libc's implementation is actually different from GNU libc, it adds a NULL byte at the end:
In cases where hostname length == buffer length, we get different behavior:
$ ./gethostname_musl.o ret=0 errno=0 (No error information) bytes=74 65 6d 00 chars=tem\0 $ ./gethostname_glibc.o ret=-1 errno=36 (File name too long) bytes=74 65 6d 70 chars=tempMacOS also NULL-terminates the name:
Illumos uses the systeminfo syscall:
And systeminfo also NULL-terminates the buffer when it's too small:
Relevant manpages: