Subject: changing sysctl_string() and sysctl_rdstring()
To: None <tech-kern@netbsd.org>
From: Simon Burge <simonb@netbsd.org>
List: tech-kern
Date: 06/29/1999 11:55:23
I've logged PR kern/7836, where gethostname() fails if the
buffer passed is shorter than the real hostname. Both the
man page for gethostname() and Single Unix Specification (at
http://www.opengroup.org/onlinepubs/007908799/xns/gethostname.html -
thanks Zdenek!) say the hostname should be truncated if the buffer is
too short.
I think the cleanest way to fix this is to change sysctl_string() (and
sysctl_rdstring() for completeness) to take an extra flags argument, and
define a SYSCTL_TRUNC_RESULT value that some callers of sysctl_string()
can pass. There would also have to be a mention in the sysctl(3)
manpage that some calls return truncated data if the buffer is too small
rather than erroring with ENOMEM. This however means that you don't
know if you got the full hostname.
From the reading the SUS and tests on Ultrix and Solaris, gethostname()
returns success even when truncating the hostname. From a quick look,
it doesn't seem possible for sysctl() to return success but still set
errno to ENOMEM if the hostname is truncated. Is this either (a) a
desired way to work out of the hostname is truncated or (b) a valid
thing to do?
Portable code should check for MAXHOSTNAMELEN and use that, but I
discovered the problem with an old program I wrote that wasn't exactly
portable...
Simon.