Re: [sr #107896] gnutls 3.0.8 fails to compile with clang

2011-12-02 Thread Simon Josefsson
Has anyone tested the recent stdalign.h stuff with clang?  We got a
report below regarding it.  Do we need to replace clang's stdalign.h
with a more feature-ful gnulib replacement?

/Simon

Elias Pipping  writes:

> URL:
>   
>
>  Summary: gnutls 3.0.8 fails to compile with clang
>  Project: GnuTLS
> Submitted by: pipping
> Submitted on: Wed 30 Nov 2011 12:38:29 GMT
> Category: None
> Priority: 5 - Normal
> Severity: 3 - Normal
>   Status: None
>  Privacy: Public
>  Assigned to: None
> Originator Email: 
>  Open/Closed: Open
>  Discussion Lock: Any
> Operating System: None
>
> ___
>
> Details:
>
> Earlier versions worked. (My theory for) the reason this version does not
> work
> for:
>
> gl/argp-parse.c uses alignof(); for that,  is included. a usable
> stdalign.h is also shipped with gnutls for use with compilers that don't
> come with such a header yet.
>
> clang does have such a header but it only defines alignas(), not alignof().
>
> here is the actual error:
>
> argp-parse.c:490:41: error: expected expression
>   csum = alignto (gsum + clen, alignof (struct option));
> ^
>
> And this is what /usr/lib64/clang/3.1/include/stdalign.h contains for me
> (aside from the copyright header and the include guards):
>
> #define alignas _Alignas
> #define __alignas_is_defined 1
>
>
>
>
>
> ___
>
> Reply to this item at:
>
>   
>
> ___
>   Message sent via/by Savannah
>   http://savannah.gnu.org/



Re: [sr #107896] gnutls 3.0.8 fails to compile with clang

2011-12-02 Thread Paul Eggert
Thanks, I tried to fix that by pushing the following patch;
please give it a try.

I had tested with Clang before, but I was using Clang 2.x
and the problem is new to Clang 3.x (which I don't have).

It strikes me, though, that this sort of approach cannot work if
 is installed into a directory intended to be used by
shared libraries or whatnot.  This is because the compiler used in the
configure-time test for  may not be the same as the
compiler used later, when the shared library is compiled against.
I expect that this isn't the only place where gnulib has this problem,
though, and since it's purely theoretical right now for stdalign.h
I didn't pursue it.


stdalign: port to Clang 3.0
Problem reported by Simon Josefsson in
.
* doc/posix-headers/stdalign.texi (stdalign.h): Mention Clang 3.0,
which has  but which does not define alignof.
* m4/stdalign.m4 (gl_STDALIGN_H): Check for Clang 3.0's problem.
diff --git a/doc/posix-headers/stdalign.texi b/doc/posix-headers/stdalign.texi
index b3d39f0..bebc70d 100644
--- a/doc/posix-headers/stdalign.texi
+++ b/doc/posix-headers/stdalign.texi
@@ -13,6 +13,8 @@ Portability problems fixed by Gnulib:
 @itemize
 @item
 This header file is missing on most circa-2011 platforms.
+@item
+Clang 3.0's @code{} does not define @code{alignof}/@code{_Alignof}.
 @end itemize

 Portability problems not fixed by Gnulib:
diff --git a/m4/stdalign.m4 b/m4/stdalign.m4
index da64dc6..2641dce 100644
--- a/m4/stdalign.m4
+++ b/m4/stdalign.m4
@@ -9,9 +9,24 @@ dnl with or without modifications, as long as this notice is 
preserved.

 AC_DEFUN([gl_STDALIGN_H],
 [
-  AC_CHECK_HEADERS_ONCE([stdalign.h])
+  AC_CACHE_CHECK([for working stdalign.h],
+[gl_cv_header_working_stdalign_h],
+[AC_COMPILE_IFELSE(
+   [AC_LANG_PROGRAM(
+  [[#include 
+int align_int = alignof (int) + _Alignof (double);

-  if test $ac_cv_header_stdalign_h = yes; then
+/* Test _Alignas only on platforms where gnulib can help.  */
+#if \
+(__GNUC__ || __IBMC__ || __IBMCPP__ \
+ || 0x5110 <= __SUNPRO_C || 1300 <= _MSC_VER)
+  int alignas (8) alignas_int = 1;
+#endif
+  ]])],
+   [gl_cv_header_working_stdalign_h=yes],
+   [gl_cv_header_working_stdalign_h=no])])
+
+  if test $gl_cv_header_working_stdalign_h = yes; then
 STDALIGN_H=''
   else
 STDALIGN_H='stdalign.h'



Re: [PATCH] mktempd: silence dd usage

2011-12-02 Thread Jim Meyering
Eric Blake wrote:
> When running a testsuite on FreeBSD, I saw a lot of:
>
> 1+ records in
> 0+1 records out
> 10 bytes transferred in 0.63 secs (158875 bytes/sec)
>
> literring the testsuite, and traced it to the fact that we
> were silencing one, but not the other, dd usage in our fallback.
>
> * build-aux/mktempd (rand_bytes): Silence dd.
>
> Signed-off-by: Eric Blake 
> ---
>  ChangeLog |5 +
>  build-aux/mktempd |3 ++-
>  2 files changed, 7 insertions(+), 1 deletions(-)
>
> diff --git a/ChangeLog b/ChangeLog
> index fdf8b25..5a616e7 100644
> --- a/ChangeLog
> +++ b/ChangeLog
> @@ -1,1 +1,6 @@
> +2011-12-01  Eric Blake  
> +
> + mktempd: silence dd usage
> + * build-aux/mktempd (rand_bytes): Silence dd.
> +
>  2011-11-30  Simon Josefsson  
>
>   manywarnings: Don't mention gcc version in docstring.
> diff --git a/build-aux/mktempd b/build-aux/mktempd
> index 7a09a4b..b6360e4 100755
> --- a/build-aux/mktempd
> +++ b/build-aux/mktempd
> @@ -30,35 +30,36 @@
>  #  the desired directory.
>  #  - make only $MAX_TRIES attempts
>
>  ME=`basename "$0"`
>  die() { echo >&2 "$ME: $@"; exit 1; }
>
>  MAX_TRIES=4
>
>  rand_bytes()
>  {
>n=$1
>
>chars=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
>
>dev_rand=/dev/urandom
>if test -r "$dev_rand"; then
>  # Note: 256-length($chars) == 194; 3 copies of $chars is 186 + 8 = 194.
> -dd ibs=$n count=1 if="$dev_rand" | tr -c $chars 
> 01234567$chars$chars$chars
> +dd ibs=$n count=1 if="$dev_rand" 2>/dev/null \
> +  | tr -c $chars 01234567$chars$chars$chars

Thanks.

For the record, what version of FreeBSD was that?
I see that at least 9.0-BETA3 does have /usr/bin/mktemp.



spurious message about include files from `bootstrap' script

2011-12-02 Thread Stefano Lattarini
Hello gnulibers.

While boostrapping the latest coreutils from master (v8.14-79-g1a85d4a),
I've noticed some spurious lines in the messages generated (I believe)
from the `bootstrap 'script; here is an excerpt that should highlight
them:

  ...
  Creating ./lib/uniwidth/.gitignore
  Creating ./m4/.gitignore
  Finished.

  You may need to add #include directives for the following .h files.
#include 
... [SNIP]
#include 
#include 
/* Include only after all system include files.  */
GPL
License
#include "acl.h"
#include "alignof.h"
# ... [SNIP]
#include "yesno.h"

  You may need to use the following Makefile variables when linking.
  ...


The lines:

  "/* Include only after all system include files.  */"
  "GPL"
  "License"

seems spurious.


Regards,
  Stefano



Re: [PATCH] mktempd: silence dd usage

2011-12-02 Thread Eric Blake
On 12/02/2011 03:24 AM, Jim Meyering wrote:
> Eric Blake wrote:
>> When running a testsuite on FreeBSD, I saw a lot of:
>>
> 
> For the record, what version of FreeBSD was that?
> I see that at least 9.0-BETA3 does have /usr/bin/mktemp.

freebsd64 8.2-RELEASE

-- 
Eric Blake   ebl...@redhat.com+1-919-301-3266
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature


Re: [PATCH] mktempd: silence dd usage

2011-12-02 Thread Pádraig Brady
On 12/02/2011 01:06 PM, Eric Blake wrote:
> On 12/02/2011 03:24 AM, Jim Meyering wrote:
>> Eric Blake wrote:
>>> When running a testsuite on FreeBSD, I saw a lot of:
>>>
>>
>> For the record, what version of FreeBSD was that?
>> I see that at least 9.0-BETA3 does have /usr/bin/mktemp.
> 
> freebsd64 8.2-RELEASE

According to this page FreeBSD has `mktemp -d` for ages.
http://www.freebsd.org/cgi/man.cgi?query=mktemp&apropos=0&sektion=0&manpath=FreeBSD+8.2-RELEASE&arch=default&format=html
I confirmed it's available on os x too

cheers,
Pádraig.



Re: [PATCH] mktempd: silence dd usage

2011-12-02 Thread Eric Blake
On 12/02/2011 06:18 AM, Pádraig Brady wrote:
> On 12/02/2011 01:06 PM, Eric Blake wrote:
>> On 12/02/2011 03:24 AM, Jim Meyering wrote:
>>> Eric Blake wrote:
 When running a testsuite on FreeBSD, I saw a lot of:

>>>
>>> For the record, what version of FreeBSD was that?
>>> I see that at least 9.0-BETA3 does have /usr/bin/mktemp.
>>
>> freebsd64 8.2-RELEASE
> 
> According to this page FreeBSD has `mktemp -d` for ages.
> http://www.freebsd.org/cgi/man.cgi?query=mktemp&apropos=0&sektion=0&manpath=FreeBSD+8.2-RELEASE&arch=default&format=html
> I confirmed it's available on os x too

And I indeed see:

which mktemp
/usr/bin/mktemp

But the mktempd script requires -p:

  d=`env -u TMPDIR mktemp -d -t -p "$destdir" "$template" 2>/dev/null` \

and FreeBSD mktemp lacks that:

usage: mktemp [-d] [-q] [-t prefix] [-u] templat ...
   mktemp [-d] [-q] [-u] -t prefix

in other words, since the mktempd usage appears to be hard-coded to GNU
mktemp, it explains why the FreeBSD mktemp fails the test and we fall
back to dd.

Since mktemp is not standardized, perhaps we should try multiple
invocation styles, if we can make BSD's mktemp give us the results we
want without having to fall back to dd.

-- 
Eric Blake   ebl...@redhat.com+1-919-301-3266
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature


Re: [PATCH] mktempd: silence dd usage

2011-12-02 Thread Jim Meyering
Eric Blake wrote:
...
> But the mktempd script requires -p:
>
>   d=`env -u TMPDIR mktemp -d -t -p "$destdir" "$template" 2>/dev/null` \
>
> and FreeBSD mktemp lacks that:
>
> usage: mktemp [-d] [-q] [-t prefix] [-u] templat ...
>mktemp [-d] [-q] [-u] -t prefix
>
> in other words, since the mktempd usage appears to be hard-coded to GNU
> mktemp, it explains why the FreeBSD mktemp fails the test and we fall
> back to dd.
>
> Since mktemp is not standardized, perhaps we should try multiple
> invocation styles, if we can make BSD's mktemp give us the results we
> want without having to fall back to dd.

I deliberately chose not to do that, to avoid complicating the
implementation.  What would be the motivation for finding a non-GNU
mktemp with adequate semantics when we already have a work-around?
Once you find one, verifying (efficiently) that it does its job safely
and reliably may be quite hard.  If there's a security problem, we should
deal with it regardless.  Performance?  Probably not.



manywarnings and -f options

2011-12-02 Thread Eric Blake
Should gl_MANYWARN_ALL_GCC be adding some gcc -f options?  For example,
at least gcc 4.3.4 -Wdisabled-optimization would emit a warning that
several other -W warnings are useless without -funit-at-a-time also in
place (see coreutils commit 5e361387d).  Also, according to 'info gcc,
-Wsuggest-attribute=pure only works if -fipa-pure-const is turned on
(true by default for -O compilation, but apparently the -W option can
also catch a few cases even without -O if -fipa-pure-const is manually
turned on).

-- 
Eric Blake   ebl...@redhat.com+1-919-301-3266
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature


Re: manywarnings and -f options

2011-12-02 Thread Eric Blake
On 12/02/2011 11:05 AM, Eric Blake wrote:
> Should gl_MANYWARN_ALL_GCC be adding some gcc -f options?  For example,
> at least gcc 4.3.4 -Wdisabled-optimization would emit a warning that
> several other -W warnings are useless without -funit-at-a-time also in
> place (see coreutils commit 5e361387d).  Also, according to 'info gcc,
> -Wsuggest-attribute=pure only works if -fipa-pure-const is turned on
> (true by default for -O compilation, but apparently the -W option can
> also catch a few cases even without -O if -fipa-pure-const is manually
> turned on).

I also meant to add that -fdiagnostics-show-option is a must for
determining which warnings are firing, when deciding which warnings to
avoid.  Having these -f options added by default instead of making each
client add them would make maintenance easier.

-- 
Eric Blake   ebl...@redhat.com+1-919-301-3266
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature


[no subject]

2011-12-02 Thread Ben Walton
Hi Bruno,

The following series incorporates the feedback you provided and also
adds a test module for sethostname.  As this function requires
privilege to test, I chose to bail out if it didn't seem that the
privilege was held.

How does the series look now?

Thanks
-Ben




[PATCH 1/4] Split the HOST_NAME_MAX detection into a separate m4 macro

2011-12-02 Thread Ben Walton
The sethostname module will rely on this code too, so make it a
separate function.

Signed-off-by: Ben Walton 
---
 ChangeLog |5 +
 m4/gethostname.m4 |9 +++--
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 272dded..eee02b6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2011-12-01  Ben Walton  
+
+   * m4/gethostname.m4 (gl_PREREQ_HOST_NAME_MAX): Make this a separate
+   macro so it can be used by the pending sethostname module.
+
 2011-11-29  Paolo Bonzini  
 
float tests: Correct and re-enable assertion about LDBL_MIN_EXP.
diff --git a/m4/gethostname.m4 b/m4/gethostname.m4
index 8ea6329..ad93c9f 100644
--- a/m4/gethostname.m4
+++ b/m4/gethostname.m4
@@ -1,4 +1,4 @@
-# gethostname.m4 serial 12
+# gethostname.m4 serial 13
 dnl Copyright (C) 2002, 2008-2011 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -40,7 +40,12 @@ AC_DEFUN([gl_FUNC_GETHOSTNAME],
 HAVE_GETHOSTNAME=0
   fi
 
-  dnl Also provide HOST_NAME_MAX when  lacks it.
+  gl_PREREQ_HOST_NAME_MAX
+])
+
+dnl Also provide HOST_NAME_MAX when  lacks it.
+AC_DEFUN([gl_PREREQ_HOST_NAME_MAX], [
+
   dnl - On most Unix systems, use MAXHOSTNAMELEN from  instead.
   dnl - On Solaris, Cygwin, BeOS, use MAXHOSTNAMELEN from  instead.
   dnl - On mingw, use 256, because
-- 
1.7.4.1




[PATCH 3/4] Integrate the sethostname module into unistd

2011-12-02 Thread Ben Walton
Ensure that sethostname is accounted for within the unistd module.

Signed-off-by: Ben Walton 
---
 ChangeLog   |9 +
 lib/unistd.in.h |   25 +
 m4/unistd_h.m4  |7 +--
 modules/unistd  |3 +++
 4 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c6cf901..67f7f42 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2011-12-01  Ben Walton  
 
+   * lib/unistd.in.h: Integrate the SETHOSTNAME preprocessor handling
+into the unistd.h header.
+   * m4/unistd_h.m4: Setup the autoconf handling for the SETHOSTNAME
+preprocessor directives.
+   * modules/unistd: Setup the Makefile substitutions of the
+SETHOSTNAME preprocessor directives.
+
+2011-12-01  Ben Walton  
+
* lib/sethostname.c (sethostname): New file.  Provide sethostname
for systems that lack it
* m4/sethostname.m4 (gl_FUNC_SETHOSTNAME): New file.  Detection of
diff --git a/lib/unistd.in.h b/lib/unistd.in.h
index f53f34b..d01f68d 100644
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -683,6 +683,31 @@ _GL_WARN_ON_USE (getgroups, "getgroups is unportable - "
 # endif
 #endif
 
+#if @GNULIB_SETHOSTNAME@
+/* Set the host name of the machine.
+   The host name may or may not be fully qualified.
+
+   Put LEN bytes of NAME into the host name.
+   Return 0 if successful, otherwise, set errno and return -1
+
+   Platforms with no ability to set the hostname return -1 and set
+   errno = ENOSYS. */
+#  if !@HAVE_SETHOSTNAME@ || !@HAVE_DECL_SETHOSTNAME@
+_GL_FUNCDECL_SYS (sethostname, int, (const char *name, size_t len)
+_GL_ARG_NONNULL ((1)));
+#  endif
+/* Need to cast, because on Solaris 11 2011-10, MacOS X 10.5, IRIX 6.5
+   and FreeBSD 6.4 the second parameter is int.  On Solaris 11
+   2011-10, the first parameter is not const. */
+_GL_CXXALIAS_SYS_CAST (sethostname, int, (const char *name, size_t len));
+_GL_CXXALIASWARN (sethostname);
+#elif defined GNULIB_POSIXCHECK
+# undef gethostname
+# if HAVE_RAW_DECL_GETHOSTNAME
+_GL_WARN_ON_USE (sethostname, "sethostname is unportable - "
+ "use gnulib module sethostname for portability");
+# endif
+#endif
 
 #if @GNULIB_GETHOSTNAME@
 /* Return the standard host name of the machine.
diff --git a/m4/unistd_h.m4 b/m4/unistd_h.m4
index b4eca68..d3fa586 100644
--- a/m4/unistd_h.m4
+++ b/m4/unistd_h.m4
@@ -43,8 +43,8 @@ AC_DEFUN([gl_UNISTD_H],
 fdatasync fsync ftruncate getcwd getdomainname getdtablesize getgroups
 gethostname getlogin getlogin_r getpagesize getusershell setusershell
 endusershell group_member lchown link linkat lseek pipe pipe2 pread pwrite
-readlink readlinkat rmdir sleep symlink symlinkat ttyname_r unlink unlinkat
-usleep])
+readlink readlinkat rmdir sethostname sleep symlink symlinkat ttyname_r
+unlink unlinkat usleep])
 ])
 
 AC_DEFUN([gl_UNISTD_MODULE_INDICATOR],
@@ -95,6 +95,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   GNULIB_READLINKAT=0;   AC_SUBST([GNULIB_READLINKAT])
   GNULIB_RMDIR=0;AC_SUBST([GNULIB_RMDIR])
   GNULIB_SLEEP=0;AC_SUBST([GNULIB_SLEEP])
+  GNULIB_SETHOSTNAME=0;  AC_SUBST([GNULIB_SETHOSTNAME])
   GNULIB_SYMLINK=0;  AC_SUBST([GNULIB_SYMLINK])
   GNULIB_SYMLINKAT=0;AC_SUBST([GNULIB_SYMLINKAT])
   GNULIB_TTYNAME_R=0;AC_SUBST([GNULIB_TTYNAME_R])
@@ -131,6 +132,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   HAVE_READLINK=1;AC_SUBST([HAVE_READLINK])
   HAVE_READLINKAT=1;  AC_SUBST([HAVE_READLINKAT])
   HAVE_SLEEP=1;   AC_SUBST([HAVE_SLEEP])
+  HAVE_SETHOSTNAME=1; AC_SUBST([HAVE_SETHOSTNAME])
   HAVE_SYMLINK=1; AC_SUBST([HAVE_SYMLINK])
   HAVE_SYMLINKAT=1;   AC_SUBST([HAVE_SYMLINKAT])
   HAVE_UNLINKAT=1;AC_SUBST([HAVE_UNLINKAT])
@@ -142,6 +144,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   HAVE_DECL_GETLOGIN_R=1; AC_SUBST([HAVE_DECL_GETLOGIN_R])
   HAVE_DECL_GETPAGESIZE=1; AC_SUBST([HAVE_DECL_GETPAGESIZE])
   HAVE_DECL_GETUSERSHELL=1; AC_SUBST([HAVE_DECL_GETUSERSHELL])
+  HAVE_DECL_SETHOSTNAME=1; AC_SUBST([HAVE_DECL_SETHOSTNAME])
   HAVE_DECL_TTYNAME_R=1;  AC_SUBST([HAVE_DECL_TTYNAME_R])
   HAVE_OS_H=0;AC_SUBST([HAVE_OS_H])
   HAVE_SYS_PARAM_H=0; AC_SUBST([HAVE_SYS_PARAM_H])
diff --git a/modules/unistd b/modules/unistd
index 0cee9b3..a9160eb 100644
--- a/modules/unistd
+++ b/modules/unistd
@@ -66,6 +66,7 @@ unistd.h: unistd.in.h $(top_builddir)/config.status 
$(CXXDEFS_H) $(ARG_NONNULL_H
  -e 's/@''GNULIB_READLINK''@/$(GNULIB_READLINK)/g' \
  -e 's/@''GNULIB_READLINKAT''@/$(GNULIB_READLINKAT)/g' \
  -e 's/@''GNULIB_RMDIR''@/$(GNULIB_RMDIR)/g' \
+ -e 's/@''GNULIB_SETHOSTNAME''@/$(GNULIB_SETHOSTNAME)/g' \
  -e 's/@''GNULIB_SLEEP''@/$(GNULIB_SLEEP)/g' \
  -e 's/@''GNULIB_SYMLINK''@/$(GNULIB_SYMLINK)/g' \
  -e 's/@''GNULIB_SYMLINKAT''@/$(GNULIB_SYMLINK

[PATCH 2/4] Add a new sethostname module

2011-12-02 Thread Ben Walton
Define sethostname on platforms that do not provide the declaration.
Provide a function for platforms that lack it.  The general handling
of the provided function is to simply return -1 and set errno to
ENOSYS.  A specific handler is provided for Minix.

Signed-off-by: Ben Walton 
---
 ChangeLog|8 +++
 doc/glibc-functions/sethostname.texi |   16 --
 lib/sethostname.c|   87 ++
 m4/sethostname.m4|   25 ++
 modules/sethostname  |   31 
 5 files changed, 162 insertions(+), 5 deletions(-)
 create mode 100644 lib/sethostname.c
 create mode 100644 m4/sethostname.m4
 create mode 100644 modules/sethostname

diff --git a/ChangeLog b/ChangeLog
index eee02b6..c6cf901 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2011-12-01  Ben Walton  
 
+   * lib/sethostname.c (sethostname): New file.  Provide sethostname
+   for systems that lack it
+   * m4/sethostname.m4 (gl_FUNC_SETHOSTNAME): New file.  Detection of
+   sethostname declaration and function.
+   * modules/sethostname: New file.  Define the sethostname module.
+
+2011-12-01  Ben Walton  
+
* m4/gethostname.m4 (gl_PREREQ_HOST_NAME_MAX): Make this a separate
macro so it can be used by the pending sethostname module.
 
diff --git a/doc/glibc-functions/sethostname.texi 
b/doc/glibc-functions/sethostname.texi
index 75cc8ca..ec7b11a 100644
--- a/doc/glibc-functions/sethostname.texi
+++ b/doc/glibc-functions/sethostname.texi
@@ -2,18 +2,24 @@
 @subsection @code{sethostname}
 @findex sethostname
 
-Gnulib module: ---
+Gnulib module: sethostname
 
 Portability problems fixed by Gnulib:
 @itemize
-@end itemize
-
-Portability problems not fixed by Gnulib:
-@itemize
 @item
 This function is missing on some platforms:
 Minix 3.1.8, Cygwin, mingw, MSVC 9, Interix 3.5, BeOS.
 @item
 This function is not declared on some platforms:
 AIX 7.1, OSF/1 5.1, Solaris 10.
+@item
+On Solaris 10, the first argument is @code{char *} instead of
+@code{const char *} and the second parameter is @code{int} instead of
+@code{size_t}.
+@item
+On some platforms the Gnulib replacement always fails with ENOSYS.
+@end itemize
+
+Portability problems not fixed by Gnulib:
+@itemize
 @end itemize
diff --git a/lib/sethostname.c b/lib/sethostname.c
new file mode 100644
index 000..e1b7d74
--- /dev/null
+++ b/lib/sethostname.c
@@ -0,0 +1,87 @@
+/* sethostname emulation for glibc compliance.
+
+   Copyright (C) 2011 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see .  */
+
+/* Ben Walton  */
+
+#include 
+
+/* Unix API.  */
+
+/* Specification.  */
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+/* Set up to LEN chars of NAME as system hostname.
+   Return 0 if ok, set errno and return -1 on error. */
+
+int
+sethostname (const char *name, size_t len)
+{
+  /* Ensure the string isn't too long.  glibc does allow setting an
+ empty hostname so no point in enforcing a lower bound. */
+  if (len > HOST_NAME_MAX)
+{
+  errno = EINVAL;
+  return -1;
+}
+
+  /* NAME does not need to be null terminated so leave room to terminate
+ regardless of input. */
+  char hostname[HOST_NAME_MAX + 1];
+  memcpy ((void *) hostname, (const void *) name, len);
+  hostname[len] = '\0';
+
+#ifdef __minix /* Minix */
+  {
+FILE *hostf;
+int r = 0;
+
+/* glibc returns EFAULT, EINVAL, and EPERM on error.  None of
+   these are appropriate for us to set, even if they may match the
+   situation, during failed open/write/close operations, so we
+   leave errno alone and rely on what the system sets up. */
+hostf = fopen ("/etc/hostname.file", "w");
+if (hostf == NULL)
+  r = -1;
+else
+  {
+fprintf (hostf, "%s\n", hostname);
+if (ferror (hostf))
+  {
+clearerr (hostf);
+r = -1;
+  }
+
+/* use return value of fclose for function return value as it
+   matches our needs.  fclose will also set errno on
+   failure */
+r = fclose (hostf);
+  }
+
+return r;
+  }
+#else
+  /* For platforms that we don't have a better option for, simply bail
+ out */
+  errno = ENOSYS;
+  return -1;
+#endif
+}
diff --git a/m4/sethostname.m4 b/m4/sethostname.m4
new file 

[PATCH 4/4] Add a test suite for the sethostname module

2011-12-02 Thread Ben Walton
Provide a module that tests the functionality of sethostname().

Signed-off-by: Ben Walton 
---
 ChangeLog |6 ++
 modules/sethostname-tests |   13 +
 tests/test-sethostname.c  |  117 +
 3 files changed, 136 insertions(+), 0 deletions(-)
 create mode 100644 modules/sethostname-tests
 create mode 100644 tests/test-sethostname.c

diff --git a/ChangeLog b/ChangeLog
index 67f7f42..2e85f16 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2011-12-01  Ben Walton  
 
+   * modules/sethostname-tests: New file.  A test program
+   for the sethostname module.
+   * tests/test-sethostname.c: Likewise.
+   
+2011-12-01  Ben Walton  
+
* lib/unistd.in.h: Integrate the SETHOSTNAME preprocessor handling
 into the unistd.h header.
* m4/unistd_h.m4: Setup the autoconf handling for the SETHOSTNAME
diff --git a/modules/sethostname-tests b/modules/sethostname-tests
new file mode 100644
index 000..040b380
--- /dev/null
+++ b/modules/sethostname-tests
@@ -0,0 +1,13 @@
+Files:
+tests/signature.h
+tests/test-sethostname.c
+tests/macros.h
+
+Depends-on:
+sys_types
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-sethostname
+check_PROGRAMS += test-sethostname
diff --git a/tests/test-sethostname.c b/tests/test-sethostname.c
new file mode 100644
index 000..249c064
--- /dev/null
+++ b/tests/test-sethostname.c
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2011 Free Software Foundation, Inc.
+ * Written by Ben Walton.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see .  */
+
+#include 
+
+#include 
+
+#include "signature.h"
+SIGNATURE_CHECK (sethostname, int, (const char *, size_t));
+
+/* for HOST_NAME_MAX */
+#include 
+/* for uid_t */
+#include 
+/* for strlen */
+#include 
+
+#include 
+#include 
+
+#include "macros.h"
+
+#define TESTHOSTNAME "gnulib-hostname"
+
+/* mingw and MSVC 9 lack geteuid, so setup a value that will indicate
+   we don't have root privilege since we wouldn't know whether to
+   expect success or failure when setting a name anyway*/
+#if !HAVE_GETEUID
+# define geteuid() ((uid_t) -1)
+#endif
+
+int
+main (int argc, char *argv[] _GL_UNUSED)
+{
+  char origname[HOST_NAME_MAX];
+  char newname[HOST_NAME_MAX];
+  char longname[HOST_NAME_MAX + 2];
+  int rcg, rcs, i;
+
+  /* skip the tests if we don't have root privilege.  this does not
+ consider things like CAP_SYS_ADMIN (linux) or PRIV_SYS_ADMIN
+ (solaris), etc.  systems without a working geteuid (mingw, MSVC
+ 9) will always skip this test. */
+  if (geteuid() != 0)
+return 0;
+
+  /* we want to ensure we can do a get/set/get check to ensure the
+ change is accepted. record the current name so it can be restored
+ later */
+  ASSERT(gethostname (origname, sizeof (origname)) == 0);
+
+  /* try setting a valid hostname.  if it fails -1/ENOSYS, we will
+ skip the test for long names as this is an indication we're using
+ the stub function that doesn't do anything on this platform. */
+  rcs = sethostname (TESTHOSTNAME, strlen (TESTHOSTNAME));
+
+  if (rcs != 0)
+{
+  if (rcs == -1 && errno == ENOSYS)
+   return 0;
+  else
+   {
+ printf ("error setting valid hostname.\n");
+ return 1;
+   }
+}
+  else
+{
+  ASSERT (gethostname (newname, sizeof (newname)) == 0);
+
+  /* if we don't get back what we put in, there is no need to
+restore the original name as we will assume it was not
+properly changed. */
+  if (strcmp (newname, TESTHOSTNAME) != 0)
+   {
+ printf ("set/get comparison failed.\n");
+ return 1;
+   }
+}
+
+  /* glibc does allow setting a zero length name, so the lower bound
+ needs no test. validate that we are constrained by
+ HOST_NAME_MAX */
+  for (i = 0; i < (HOST_NAME_MAX + 1); i++)
+longname[i] = 'a';
+
+  longname[i] = '\0';
+
+  rcs = sethostname ((const char *) longname, (HOST_NAME_MAX + 1));
+
+  if (rcs != -1)
+{
+  /* attempt to restore the original name. */
+  sethostname (origname, strlen (origname));
+  printf ("expected failure when setting very long hostname.\n");
+  return 1;
+}
+
+  /* restore the original name. */
+  sethostname (origname, strlen (origname));
+
+  return 0;
+}
-- 
1.7.4.1