bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getdomainname: fix several problems


From: Bruno Haible
Subject: getdomainname: fix several problems
Date: Sun, 28 Nov 2010 17:56:04 +0100
User-agent: KMail/1.9.9

Hi,

When I saw this configure output on a Solaris 10 machine

  checking for getdomainname... no

I couldn't believe it. The situation on Solaris is that is has the
getdomainname() function in libnsl, not in libc. And it is not
declared. So what the gnulib replacement was doing, was to return a
dummy string instead of calling the system's function. It is better
to call the system's function. The price to pay is that using
getdomainname() now requires linking flags (like for gethostname()).

The function's definition (from the OpenSolaris sources) looks like this:

  int
  getdomainname(name, namelen)
     char *name;
     int namelen;

So the declaration would have 'int' as second argument type, not 'size_t'
as in glibc.

A couple of other platforms do have the declaration in <unistd.h>, but also
with 'int' as second argument type.

And some other platforms (AIX, OSF/1 5.1) declare the function in <netdb.h>,
not in <unistd.h>.

Here's the collection of fixes. On Solaris, configure now prints:

  checking for getdomainname... no
  checking for getdomainname in -lnsl... yes
  checking whether getdomainname is declared... no
  checking for getdomainname's second argument type... int

Tested with a unit test on glibc, MacOS X 10.5, AIX 5.1, OSF/1 5.1, Solaris 10,
mingw. OK to commit?


2010-11-28  Bruno Haible  <address@hidden>

        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>.
        (getdomainname): When the system has getdomainname, call the system
        function.
        * 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'.
        (gl_PREREQ_GETDOMAINNAME): Define HAVE_GETDOMAINNAME.
        * modules/getdomainname (Depends-on): Add netdb, sys_socket.
        (Link): New section.
        * 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.
        * NEWS: Document the need to use $(GETDOMAINNAME_LIB).

--- NEWS.orig   Sun Nov 28 17:27:51 2010
+++ NEWS        Sun Nov 28 14:47:23 2010
@@ -12,6 +12,9 @@
 
 Date        Modules         Changes
 
+2010-11-28  getdomainname   The use of this module now requires linking with
+                            $(GETDOMAINNAME_LIB).
+
 2010-10-05  getdate         This module is deprecated. Please use the new
                             parse-datetime module for the replacement
                             function parse_datetime(), or help us write
--- doc/glibc-functions/getdomainname.texi.orig Sun Nov 28 17:27:51 2010
+++ doc/glibc-functions/getdomainname.texi      Sun Nov 28 16:18:25 2010
@@ -8,7 +8,21 @@
 @itemize
 @item
 This function is missing on some platforms:
-AIX 5.1, mingw, Interix 3.5, BeOS.
+mingw, Interix 3.5, BeOS.
address@hidden
+This function is not declared on some platforms:
+Solaris 11 2010-11.
address@hidden
+This function is declared in @code{netdb.h}, not in @code{unistd.h}, on
+some platforms:
+AIX 7.1.
address@hidden
+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.
address@hidden
+The second argument is of type @code{int}, not @code{size_t}, on some 
platforms:
+AIX 7.1, MacOS X 10.5, FreeBSD 6.4, IRIX 6.5, Solaris 11 2010-11.
 @end itemize
 
 Portability problems not fixed by Gnulib:
--- lib/getdomainname.c.orig    Sun Nov 28 17:27:51 2010
+++ lib/getdomainname.c Sun Nov 28 16:23:21 2010
@@ -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,6 +22,7 @@
 /* Specification. */
 #include <unistd.h>
 
+#include <limits.h>
 #include <string.h>
 #include <errno.h>
 
@@ -37,7 +38,15 @@
    Return 0 if successful, otherwise set errno and return -1.  */
 int
 getdomainname (char *name, size_t len)
+#undef getdomainname
 {
+#if HAVE_GETDOMAINNAME
+  extern int getdomainname (char *, int);
+
+  if (len > INT_MAX)
+    len = INT_MAX;
+  return getdomainname (name, (int) len);
+#else
   const char *result = "";      /* Hardcode your domain name if you want.  */
   size_t result_len = strlen (result);
 
@@ -50,4 +59,5 @@
   if (result_len < len)
     name[result_len] = '\0';
   return 0;
+#endif
 }
--- lib/unistd.in.h.orig        Sun Nov 28 17:27:51 2010
+++ lib/unistd.in.h     Sun Nov 28 16:22:03 2010
@@ -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 address@hidden@
+# 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 address@hidden@
 _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
--- m4/getdomainname.m4.orig    Sun Nov 28 17:27:51 2010
+++ m4/getdomainname.m4 Sun Nov 28 16:33:22 2010
@@ -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,95 @@
 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.
+  dnl - Otherwise is is in libc.
+  GETDOMAINNAME_LIB=
+  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"
+      ])
+    if test $gl_cv_func_getdomainname_in_libnsl = yes; then
+      GETDOMAINNAME_LIB="-lnsl"
+    fi
+  ])
+  AC_SUBST([GETDOMAINNAME_LIB])
+
+  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, IRIX.
+  dnl - It's missing, but the function is defined as
+  dnl         int getdomainname(char *, int)
+  dnl   on Solaris.
+  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_func_getdomainname_in_libnsl = yes; then
+    dnl The function getdomainname() exists.
+    if test $gl_cv_decl_getdomainname_argtype2 != size_t; then
+      REPLACE_GETDOMAINNAME=1
+    fi
+  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], [
-  :
+  AC_DEFINE_UNQUOTED([HAVE_GETDOMAINNAME], [$REPLACE_GETDOMAINNAME],
+    [Define if the getdomainname() function is present in some library.])
 ])
--- m4/unistd_h.m4.orig Sun Nov 28 17:27:51 2010
+++ m4/unistd_h.m4      Sun Nov 28 15:34:17 2010
@@ -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])
--- modules/getdomainname.orig  Sun Nov 28 17:27:51 2010
+++ modules/getdomainname       Sun Nov 28 16:27:38 2010
@@ -8,6 +8,8 @@
 Depends-on:
 unistd
 extensions
+netdb
+sys_socket
 
 configure.ac:
 gl_FUNC_GETDOMAINNAME
@@ -18,6 +20,9 @@
 Include:
 <unistd.h>
 
+Link:
+$(GETDOMAINNAME_LIB)
+
 License:
 GPL
 
--- modules/unistd.orig Sun Nov 28 17:27:51 2010
+++ modules/unistd      Sun Nov 28 15:18:20 2010
@@ -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' \



reply via email to

[Prev in Thread] Current Thread [Next in Thread]