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 .\" @(#)gethostname.3 8.1 (Berkeley) 6/4/93
.\" $FreeBSD$ .\" $FreeBSD$
.\" .\"
.Dd June 4, 1993 .Dd August 18, 2003
.Dt GETHOSTNAME 3 .Dt GETHOSTNAME 3
.Os .Os
.Sh NAME .Sh NAME
@ -44,7 +44,7 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.In unistd.h .In unistd.h
.Ft int .Ft int
.Fn gethostname "char *name" "int namelen" .Fn gethostname "char *name" "size_t namelen"
.Ft int .Ft int
.Fn sethostname "const char *name" "int namelen" .Fn sethostname "const char *name" "int namelen"
.Sh DESCRIPTION .Sh DESCRIPTION
@ -59,8 +59,8 @@ The
argument argument
specifies the size of the specifies the size of the
.Fa name .Fa name
array. The returned name is null-terminated unless insufficient array.
space is provided. The returned name is null-terminated unless insufficient space is provided.
.Pp .Pp
The The
.Fn sethostname .Fn sethostname
@ -71,6 +71,10 @@ which has length
.Fa namelen . .Fa namelen .
This call is restricted to the super-user and This call is restricted to the super-user and
is normally used only when the system is bootstrapped. 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 .Sh RETURN VALUES
.Rv -std .Rv -std
.Sh ERRORS .Sh ERRORS
@ -83,22 +87,48 @@ or
.Fa namelen .Fa namelen
argument gave an argument gave an
invalid address. invalid address.
.It Bq Er ENAMETOOLONG
The current host name is longer than
.Fa namelen . (For
.Fn gethostname
only.)
.It Bq Er EPERM .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 .El
.Sh SEE ALSO .Sh SEE ALSO
.Xr gethostid 3 , .Xr sysconf 3 ,
.Xr sysctl 3 .Xr sysctl 3
.Sh BUGS .Sh STANDARDS
Host names are limited to The
.Dv MAXHOSTNAMELEN .Fn gethostname
(from function conforms to
.Ao Pa sys/param.h Ac ) .St -p1003.1-2001 .
characters, currently 256. Callers should be aware that
This includes the trailing .Brq Dv HOST_NAME_MAX
.Dv NUL . 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 .Sh HISTORY
The The
.Fn gethostname .Fn gethostname
function appeared in function appeared in
.Bx 4.2 . .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/param.h>
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <errno.h>
int int
gethostname(name, namelen) gethostname(name, namelen)
char *name; char *name;
int namelen; size_t namelen;
{ {
int mib[2]; int mib[2];
size_t size;
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
mib[1] = KERN_HOSTNAME; mib[1] = KERN_HOSTNAME;
size = namelen; if (sysctl(mib, 2, name, &namelen, NULL, 0) == -1) {
if (sysctl(mib, 2, name, &size, NULL, 0) == -1) if (errno == ENOMEM)
errno = ENAMETOOLONG;
return (-1); return (-1);
}
return (0); return (0);
} }