changeset 13911:e6744cae0421

getdomainname: Use the system function when possible. * lib/unistd.in.h: Include <netdb.h>, for getdomainname's declaration. (getdomainname): Replace if needed. Provide the declaration if it is missing. Don't use _GL_CXXALIAS_SYS_CAST. * lib/getdomainname.c: Include <limits.h> and <sys/systeminfo.h>. (getdomainname): When the system has getdomainname, call the system function. When sysinfo (SI_SRPC_DOMAIN, ...) is possible, use that. * m4/getdomainname.m4 (gl_FUNC_GETDOMAINNAME): Require gl_HEADER_SYS_SOCKET and gl_HEADER_NETDB. Test whether the function is found in libnsl. Look for the declaration also in <netdb.h>. Replace the function if its second argument is of type 'int' or if it is found in libnsl. (gl_PREREQ_GETDOMAINNAME): Define HAVE_GETDOMAINNAME. Check for <sys/systeminfo.h> and sysinfo(). * modules/getdomainname (Depends-on): Add netdb, sys_socket. * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize HAVE_DECL_GETDOMAINNAME and REPLACE_GETDOMAINNAME instead of HAVE_GETDOMAINNAME. * modules/unistd (Makefile.am): Substitute HAVE_DECL_GETDOMAINNAME and REPLACE_GETDOMAINNAME instead of HAVE_GETDOMAINNAME. * doc/glibc-functions/getdomainname.texi: Document the problems with the getdomainname declaration.
author Bruno Haible <bruno@clisp.org>
date Tue, 30 Nov 2010 21:27:21 +0100
parents d2ab80f79a05
children 99a2ba825c9a
files ChangeLog doc/glibc-functions/getdomainname.texi lib/getdomainname.c lib/unistd.in.h m4/getdomainname.m4 m4/unistd_h.m4 modules/getdomainname modules/unistd
diffstat 8 files changed, 176 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,29 @@
+2010-11-28  Bruno Haible  <bruno@clisp.org>
+            Paul Eggert  <eggert@cs.ucla.edu>
+
+	getdomainname: Use the system function when possible.
+	* lib/unistd.in.h: Include <netdb.h>, for getdomainname's declaration.
+	(getdomainname): Replace if needed. Provide the declaration if it is
+	missing. Don't use _GL_CXXALIAS_SYS_CAST.
+	* lib/getdomainname.c: Include <limits.h> and <sys/systeminfo.h>.
+	(getdomainname): When the system has getdomainname, call the system
+	function. When sysinfo (SI_SRPC_DOMAIN, ...) is possible, use that.
+	* m4/getdomainname.m4 (gl_FUNC_GETDOMAINNAME): Require
+	gl_HEADER_SYS_SOCKET and gl_HEADER_NETDB. Test whether the function is
+	found in libnsl. Look for the declaration also in <netdb.h>. Replace
+	the function if its second argument is of type 'int' or if it is found
+	in libnsl.
+	(gl_PREREQ_GETDOMAINNAME): Define HAVE_GETDOMAINNAME. Check for
+	<sys/systeminfo.h> and sysinfo().
+	* modules/getdomainname (Depends-on): Add netdb, sys_socket.
+	* m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize
+	HAVE_DECL_GETDOMAINNAME and REPLACE_GETDOMAINNAME instead of
+	HAVE_GETDOMAINNAME.
+	* modules/unistd (Makefile.am): Substitute HAVE_DECL_GETDOMAINNAME and
+	REPLACE_GETDOMAINNAME instead of HAVE_GETDOMAINNAME.
+	* doc/glibc-functions/getdomainname.texi: Document the problems with
+	the getdomainname declaration.
+
 2010-11-28  Bruno Haible  <bruno@clisp.org>
 
 	sys_socket: Ensure ss_family field on AIX.
--- a/doc/glibc-functions/getdomainname.texi
+++ b/doc/glibc-functions/getdomainname.texi
@@ -8,7 +8,18 @@
 @itemize
 @item
 This function is missing on some platforms:
-AIX 5.1, mingw, Interix 3.5, BeOS.
+Solaris 11 2010-11, mingw, Interix 3.5, BeOS.
+@item
+This function is declared in @code{netdb.h}, not in @code{unistd.h}, on
+some platforms:
+AIX 7.1.
+@item
+This function is declared in @code{netdb.h} and in @code{sys/socket.h}, not
+in @code{unistd.h}, on some platforms:
+OSF/1 5.1.
+@item
+The second argument is of type @code{int}, not @code{size_t}, on some platforms:
+MacOS X 10.5, FreeBSD 6.4, AIX 7.1, IRIX 6.5.
 @end itemize
 
 Portability problems not fixed by Gnulib:
--- a/lib/getdomainname.c
+++ b/lib/getdomainname.c
@@ -1,6 +1,6 @@
 /* getdomainname emulation for systems that doesn't have it.
 
-   Copyright (C) 2003, 2006, 2008, 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2006, 2008, 2010 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
@@ -22,9 +22,14 @@
 /* Specification. */
 #include <unistd.h>
 
+#include <limits.h>
 #include <string.h>
 #include <errno.h>
 
+#if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H /* IRIX, OSF/1, Solaris */
+# include <sys/systeminfo.h>
+#endif
+
 /* Return the NIS domain name of the machine.
    WARNING! The NIS domain name is unrelated to the fully qualified host name
             of the machine.  It is also unrelated to email addresses.
@@ -37,7 +42,31 @@
    Return 0 if successful, otherwise set errno and return -1.  */
 int
 getdomainname (char *name, size_t len)
+#undef getdomainname
 {
+#if HAVE_GETDOMAINNAME                 /* MacOS X, FreeBSD, AIX, IRIX, OSF/1 */
+  extern int getdomainname (char *, int);
+
+  if (len > INT_MAX)
+    len = INT_MAX;
+  return getdomainname (name, (int) len);
+#elif HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H && defined SI_SRPC_DOMAIN
+                                       /* Solaris */
+  int ret;
+
+  /* The third argument is a 'long', but the return value must fit in an
+     'int', therefore it's better to avoid arguments > INT_MAX.  */
+  ret = sysinfo (SI_SRPC_DOMAIN, name, len > INT_MAX ? INT_MAX : len);
+  if (ret < 0)
+    /* errno is set here.  */
+    return -1;
+  if (ret > len)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+  return 0;
+#else                                  /* HP-UX, Cygwin, mingw */
   const char *result = "";      /* Hardcode your domain name if you want.  */
   size_t result_len = strlen (result);
 
@@ -50,4 +79,5 @@
   if (result_len < len)
     name[result_len] = '\0';
   return 0;
+#endif
 }
--- a/lib/unistd.in.h
+++ b/lib/unistd.in.h
@@ -88,6 +88,13 @@
 # include <io.h>
 #endif
 
+/* AIX and OSF/1 5.1 declare getdomainname in <netdb.h>, not in <unistd.h>.  */
+/* But avoid namespace pollution on glibc systems.  */
+#if @GNULIB_GETDOMAINNAME@ && (defined _AIX || defined __osf__) \
+    && !defined __GLIBC__
+# include <netdb.h>
+#endif
+
 #if (@GNULIB_WRITE@ || @GNULIB_READLINK@ || @GNULIB_READLINKAT@ \
      || @GNULIB_PREAD@ || @GNULIB_PWRITE@ || defined GNULIB_POSIXCHECK)
 /* Get ssize_t.  */
@@ -551,13 +558,21 @@
    Null terminate it if the name is shorter than LEN.
    If the NIS domain name is longer than LEN, set errno = EINVAL and return -1.
    Return 0 if successful, otherwise set errno and return -1.  */
-# if !@HAVE_GETDOMAINNAME@
+# if @REPLACE_GETDOMAINNAME@
+#  if !(defined __cplusplus && defined GNULIB_NAMESPACE)
+#   undef getdomainname
+#   define getdomainname rpl_getdomainname
+#  endif
+_GL_FUNCDECL_RPL (getdomainname, int, (char *name, size_t len)
+                                      _GL_ARG_NONNULL ((1)));
+_GL_CXXALIAS_RPL (getdomainname, int, (char *name, size_t len));
+# else
+#  if !@HAVE_DECL_GETDOMAINNAME@
 _GL_FUNCDECL_SYS (getdomainname, int, (char *name, size_t len)
                                       _GL_ARG_NONNULL ((1)));
+#  endif
+_GL_CXXALIAS_SYS (getdomainname, int, (char *name, size_t len));
 # endif
-/* Need to cast, because on MacOS X 10.5 systems, the second parameter is
-                                                        int len.  */
-_GL_CXXALIAS_SYS_CAST (getdomainname, int, (char *name, size_t len));
 _GL_CXXALIASWARN (getdomainname);
 #elif defined GNULIB_POSIXCHECK
 # undef getdomainname
--- a/m4/getdomainname.m4
+++ b/m4/getdomainname.m4
@@ -1,4 +1,4 @@
-# getdomainname.m4 serial 4
+# getdomainname.m4 serial 5
 dnl Copyright (C) 2002-2003, 2008-2010 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -7,18 +7,94 @@
 AC_DEFUN([gl_FUNC_GETDOMAINNAME],
 [
   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
+  AC_REQUIRE([gl_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H
+  AC_REQUIRE([gl_HEADER_NETDB])dnl for HAVE_NETDB_H
 
   dnl Persuade glibc <unistd.h> to declare getdomainname().
   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
 
-  AC_REPLACE_FUNCS([getdomainname])
-  if test $ac_cv_func_getdomainname = no; then
-    HAVE_GETDOMAINNAME=0
+  dnl Where is getdomainname() defined?
+  dnl - On Solaris, it is in libnsl. But this function is not declared and
+  dnl   is discouraged, see
+  dnl   <http://www.sun.com/software/solaris/programs/abi/appcert_faq.xml#q18>.
+  dnl   We need to avoid a collision with this function.
+  dnl - Otherwise is is in libc.
+  AC_CHECK_FUNCS([getdomainname], , [
+    AC_CACHE_CHECK([for getdomainname in -lnsl],
+      [gl_cv_func_getdomainname_in_libnsl],
+      [gl_cv_func_getdomainname_in_libnsl=no
+       gl_save_LIBS="$LIBS"
+       LIBS="$LIBS -lnsl"
+       AC_LINK_IFELSE(
+         [AC_LANG_PROGRAM(
+            [[#include <stddef.h>
+              extern int getdomainname (char *, size_t);
+            ]],
+            [[getdomainname(NULL, 0);]])],
+         [gl_cv_func_getdomainname_in_libnsl=yes])
+       LIBS="$gl_save_LIBS"
+      ])
+  ])
+
+  dnl What about the declaration?
+  dnl - It's  int getdomainname(char *, size_t)  on glibc, NetBSD, OpenBSD.
+  dnl - It's  int getdomainname(char *, int)  on MacOS X, FreeBSD, AIX, IRIX,
+  dnl   OSF/1.
+  AC_CHECK_DECLS([getdomainname], , ,
+    [#include <sys/types.h>
+     #ifdef HAVE_SYS_SOCKET_H
+     #include <sys/socket.h>
+     #endif
+     #ifdef HAVE_NETDB_H
+     #include <netdb.h>
+     #endif
+     #include <unistd.h>
+    ])
+  AC_CACHE_CHECK([for getdomainname's second argument type],
+    [gl_cv_decl_getdomainname_argtype2],
+    [if test $ac_cv_have_decl_getdomainname; then
+       AC_COMPILE_IFELSE(
+         [AC_LANG_PROGRAM(
+            [[#include <sys/types.h>
+              #ifdef HAVE_SYS_SOCKET_H
+              #include <sys/socket.h>
+              #endif
+              #ifdef HAVE_NETDB_H
+              #include <netdb.h>
+              #endif
+              #include <unistd.h>
+              extern int getdomainname (char *, int);]],
+            [[]])],
+         [gl_cv_decl_getdomainname_argtype2='int'],
+         [gl_cv_decl_getdomainname_argtype2='size_t'])
+     else
+       gl_cv_decl_getdomainname_argtype2='int'
+     fi
+    ])
+
+  if test $ac_cv_have_decl_getdomainname = no; then
+    HAVE_DECL_GETDOMAINNAME=0
+  fi
+
+  if { test $ac_cv_func_getdomainname = yes \
+       && test $gl_cv_decl_getdomainname_argtype2 != size_t; \
+     } \
+     || test "$gl_cv_func_getdomainname_in_libnsl" = yes; then
+    REPLACE_GETDOMAINNAME=1
+  fi
+
+  if test $HAVE_DECL_GETDOMAINNAME = 0 || test $REPLACE_GETDOMAINNAME = 1; then
+    AC_LIBOBJ([getdomainname])
     gl_PREREQ_GETDOMAINNAME
   fi
 ])
 
 # Prerequisites of lib/getdomainname.c.
 AC_DEFUN([gl_PREREQ_GETDOMAINNAME], [
-  :
+  if test $ac_cv_func_getdomainname = yes; then
+    AC_DEFINE([HAVE_GETDOMAINNAME], [1],
+      [Define if the getdomainname() function is present and can be used.])
+  fi
+  AC_CHECK_HEADERS([sys/systeminfo.h])
+  AC_CHECK_FUNCS([sysinfo])
 ])
--- a/m4/unistd_h.m4
+++ b/m4/unistd_h.m4
@@ -1,4 +1,4 @@
-# unistd_h.m4 serial 47
+# unistd_h.m4 serial 48
 dnl Copyright (C) 2006-2010 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -104,7 +104,6 @@
   HAVE_FCHOWNAT=1;        AC_SUBST([HAVE_FCHOWNAT])
   HAVE_FSYNC=1;           AC_SUBST([HAVE_FSYNC])
   HAVE_FTRUNCATE=1;       AC_SUBST([HAVE_FTRUNCATE])
-  HAVE_GETDOMAINNAME=1;   AC_SUBST([HAVE_GETDOMAINNAME])
   HAVE_GETDTABLESIZE=1;   AC_SUBST([HAVE_GETDTABLESIZE])
   HAVE_GETGROUPS=1;       AC_SUBST([HAVE_GETGROUPS])
   HAVE_GETHOSTNAME=1;     AC_SUBST([HAVE_GETHOSTNAME])
@@ -125,6 +124,7 @@
   HAVE_UNLINKAT=1;        AC_SUBST([HAVE_UNLINKAT])
   HAVE_USLEEP=1;          AC_SUBST([HAVE_USLEEP])
   HAVE_DECL_ENVIRON=1;    AC_SUBST([HAVE_DECL_ENVIRON])
+  HAVE_DECL_GETDOMAINNAME=1; AC_SUBST([HAVE_DECL_GETDOMAINNAME])
   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])
@@ -136,6 +136,7 @@
   REPLACE_DUP2=0;         AC_SUBST([REPLACE_DUP2])
   REPLACE_FCHOWNAT=0;     AC_SUBST([REPLACE_FCHOWNAT])
   REPLACE_GETCWD=0;       AC_SUBST([REPLACE_GETCWD])
+  REPLACE_GETDOMAINNAME=0; AC_SUBST([REPLACE_GETDOMAINNAME])
   REPLACE_GETGROUPS=0;    AC_SUBST([REPLACE_GETGROUPS])
   REPLACE_GETPAGESIZE=0;  AC_SUBST([REPLACE_GETPAGESIZE])
   REPLACE_LCHOWN=0;       AC_SUBST([REPLACE_LCHOWN])
--- a/modules/getdomainname
+++ b/modules/getdomainname
@@ -8,6 +8,8 @@
 Depends-on:
 unistd
 extensions
+netdb
+sys_socket
 
 configure.ac:
 gl_FUNC_GETDOMAINNAME
--- a/modules/unistd
+++ b/modules/unistd
@@ -78,7 +78,6 @@
 	      -e 's|@''HAVE_FCHOWNAT''@|$(HAVE_FCHOWNAT)|g' \
 	      -e 's|@''HAVE_FSYNC''@|$(HAVE_FSYNC)|g' \
 	      -e 's|@''HAVE_FTRUNCATE''@|$(HAVE_FTRUNCATE)|g' \
-	      -e 's|@''HAVE_GETDOMAINNAME''@|$(HAVE_GETDOMAINNAME)|g' \
 	      -e 's|@''HAVE_GETDTABLESIZE''@|$(HAVE_GETDTABLESIZE)|g' \
 	      -e 's|@''HAVE_GETGROUPS''@|$(HAVE_GETGROUPS)|g' \
 	      -e 's|@''HAVE_GETHOSTNAME''@|$(HAVE_GETHOSTNAME)|g' \
@@ -99,6 +98,7 @@
 	      -e 's|@''HAVE_UNLINKAT''@|$(HAVE_UNLINKAT)|g' \
 	      -e 's|@''HAVE_USLEEP''@|$(HAVE_USLEEP)|g' \
 	      -e 's|@''HAVE_DECL_ENVIRON''@|$(HAVE_DECL_ENVIRON)|g' \
+	      -e 's|@''HAVE_DECL_GETDOMAINNAME''@|$(HAVE_DECL_GETDOMAINNAME)|g' \
 	      -e 's|@''HAVE_DECL_GETLOGIN_R''@|$(HAVE_DECL_GETLOGIN_R)|g' \
 	      -e 's|@''HAVE_DECL_GETPAGESIZE''@|$(HAVE_DECL_GETPAGESIZE)|g' \
 	      -e 's|@''HAVE_DECL_GETUSERSHELL''@|$(HAVE_DECL_GETUSERSHELL)|g' \
@@ -110,6 +110,7 @@
 	      -e 's|@''REPLACE_DUP2''@|$(REPLACE_DUP2)|g' \
 	      -e 's|@''REPLACE_FCHOWNAT''@|$(REPLACE_FCHOWNAT)|g' \
 	      -e 's|@''REPLACE_GETCWD''@|$(REPLACE_GETCWD)|g' \
+	      -e 's|@''REPLACE_GETDOMAINNAME''@|$(REPLACE_GETDOMAINNAME)|g' \
 	      -e 's|@''REPLACE_GETGROUPS''@|$(REPLACE_GETGROUPS)|g' \
 	      -e 's|@''REPLACE_GETPAGESIZE''@|$(REPLACE_GETPAGESIZE)|g' \
 	      -e 's|@''REPLACE_LCHOWN''@|$(REPLACE_LCHOWN)|g' \