Change gethostname() to set errno to ENAMETOOLONG instead of ENOMEM

when the buffer is not long enough to hold the current host name.
POSIX does not standardize error returns for gethostname(), so it
doesn't matter which one we use, but ENAMETOOLONG is at least a little
more intuitive, and mi suggests the existence of prior art.  I've been
running with this change for a while on my home machine with no
effect.  At the same time, I've updated the prototype for
gethostname() to use the correct standard type (size_t) for the
namelen argument.

All of the in-tree callers fall into one of the following categories:
1) Call perror() or equivalent when gethostname() fails.
2) Ignore gethostname()'s return value entirely, potentially resulting
in data corruption if the buffer is too small.
3) Fall back to a (possibly sensible) default value if gethostname()
fails.

Many of the callers I examined shows signs of confusion about the
correct sizing of the host name buffer.  gethostname(3) now has more
information about this, as well as updated standards information.

PR:		48114
Submitted by:	mi (in part)
This commit is contained in:
Garrett Wollman 2003-08-19 20:38:44 +00:00
parent 1e584e46be
commit effcb5eca3
2 changed files with 51 additions and 18 deletions

View File

@ -32,7 +32,7 @@
.\" @(#)gethostname.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$
.\"
.Dd June 4, 1993
.Dd August 18, 2003
.Dt GETHOSTNAME 3
.Os
.Sh NAME
@ -44,7 +44,7 @@
.Sh SYNOPSIS
.In unistd.h
.Ft int
.Fn gethostname "char *name" "int namelen"
.Fn gethostname "char *name" "size_t namelen"
.Ft int
.Fn sethostname "const char *name" "int namelen"
.Sh DESCRIPTION
@ -59,8 +59,8 @@ The
argument
specifies the size of the
.Fa name
array. The returned name is null-terminated unless insufficient
space is provided.
array.
The returned name is null-terminated unless insufficient space is provided.
.Pp
The
.Fn sethostname
@ -71,6 +71,10 @@ which has length
.Fa namelen .
This call is restricted to the super-user and
is normally used only when the system is bootstrapped.
.Pp
Host names are limited to
.Brq Dv HOST_NAME_MAX
characters, not including the trailing null, currently 255.
.Sh RETURN VALUES
.Rv -std
.Sh ERRORS
@ -83,22 +87,48 @@ or
.Fa namelen
argument gave an
invalid address.
.It Bq Er ENAMETOOLONG
The current host name is longer than
.Fa namelen . (For
.Fn gethostname
only.)
.It Bq Er EPERM
The caller tried to set the hostname and was not the super-user.
The caller tried to set the host name and was not the super-user.
.El
.Sh SEE ALSO
.Xr gethostid 3 ,
.Xr sysconf 3 ,
.Xr sysctl 3
.Sh BUGS
Host names are limited to
.Dv MAXHOSTNAMELEN
(from
.Ao Pa sys/param.h Ac )
characters, currently 256.
This includes the trailing
.Dv NUL .
.Sh STANDARDS
The
.Fn gethostname
function conforms to
.St -p1003.1-2001 .
Callers should be aware that
.Brq Dv HOST_NAME_MAX
may be variable or infinite, but is guaranteed to be no less than
.Brq Dv _POSIX_HOST_NAME_MAX .
On older systems, this limit was defined in the non-standard header
.Aq Pa sys/param.h
as
.Dv MAXHOSTNAMELEN ,
and counted the terminating null.
The
.Fn sethostname
function and the error returns for
.Fn gethostname
are not standardized.
.Sh HISTORY
The
.Fn gethostname
function appeared in
.Bx 4.2 .
The
.Fa namelen
argument to
.Fn gethostname
was changed to
.Vt size_t
in
.Fx 5.2
for alignment with
.St -p1003.1-2001 .

View File

@ -40,18 +40,21 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/sysctl.h>
#include <errno.h>
int
gethostname(name, namelen)
char *name;
int namelen;
size_t namelen;
{
int mib[2];
size_t size;
mib[0] = CTL_KERN;
mib[1] = KERN_HOSTNAME;
size = namelen;
if (sysctl(mib, 2, name, &size, NULL, 0) == -1)
if (sysctl(mib, 2, name, &namelen, NULL, 0) == -1) {
if (errno == ENOMEM)
errno = ENAMETOOLONG;
return (-1);
}
return (0);
}