From b39b2b27be6ef15e163a14f3aba068c5c95efeb9 Mon Sep 17 00:00:00 2001 From: Peter Avalos Date: Mon, 9 Mar 2009 21:54:43 -1000 Subject: [PATCH] Sync gethostname() with FreeBSD: * 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. * Update prototype to match SuS (int->size_t). --- include/unistd.h | 2 +- lib/libc/gen/gethostname.3 | 65 +++++++++++++++++++++++++++----------- lib/libc/gen/gethostname.c | 16 +++++----- 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/include/unistd.h b/include/unistd.h index 0a7b034b69..cdf0e20f41 100644 --- a/include/unistd.h +++ b/include/unistd.h @@ -153,7 +153,7 @@ int getdomainname(char *, int); int getdtablesize(void); int getgrouplist(const char *, gid_t, gid_t *, int *); long gethostid(void); -int gethostname(char *, int); +int gethostname(char *, size_t); int getlogin_r(char *, int); mode_t getmode(const void *, mode_t); int getpagesize(void) __pure2; diff --git a/lib/libc/gen/gethostname.3 b/lib/libc/gen/gethostname.3 index 7b3a4ba932..0ae0495c27 100644 --- a/lib/libc/gen/gethostname.3 +++ b/lib/libc/gen/gethostname.3 @@ -9,10 +9,6 @@ .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. -.\" 3. All advertising materials mentioning features or use of this software -.\" must display the following acknowledgement: -.\" This product includes software developed by the University of -.\" California, Berkeley and its contributors. .\" 4. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. @@ -30,10 +26,10 @@ .\" SUCH DAMAGE. .\" .\" @(#)gethostname.3 8.1 (Berkeley) 6/4/93 -.\" $FreeBSD: src/lib/libc/gen/gethostname.3,v 1.5.2.6 2003/03/13 18:05:37 trhodes Exp $ +.\" $FreeBSD: src/lib/libc/gen/gethostname.3,v 1.17 2007/01/09 00:27:54 imp Exp $ .\" $DragonFly: src/lib/libc/gen/gethostname.3,v 1.4 2006/05/26 19:39:36 swildner Exp $ .\" -.Dd June 4, 1993 +.Dd August 18, 2003 .Dt GETHOSTNAME 3 .Os .Sh NAME @@ -45,7 +41,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 @@ -60,8 +56,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 @@ -72,6 +68,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 @@ -84,22 +84,49 @@ 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 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 +.In 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 . -.Sh BUGS -Host names are limited to -.Dv MAXHOSTNAMELEN -(from -.In sys/param.h ) -characters, currently 256. -This includes the trailing -.Dv NUL . +The +.Fa namelen +argument to +.Fn gethostname +was changed to +.Vt size_t +in +.Fx 5.2 +for alignment with +.St -p1003.1-2001 . diff --git a/lib/libc/gen/gethostname.c b/lib/libc/gen/gethostname.c index af36aaa6e4..9017077b09 100644 --- a/lib/libc/gen/gethostname.c +++ b/lib/libc/gen/gethostname.c @@ -10,10 +10,6 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. @@ -31,23 +27,27 @@ * SUCH DAMAGE. * * @(#)gethostname.c 8.1 (Berkeley) 6/4/93 + * $FreeBSD: src/lib/libc/gen/gethostname.c,v 1.8 2007/01/09 00:27:54 imp Exp $ * $DragonFly: src/lib/libc/gen/gethostname.c,v 1.4 2005/11/19 22:32:53 swildner Exp $ */ #include #include + +#include #include int -gethostname(char *name, int namelen) +gethostname(char *name, 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); } -- 2.41.0