From b4417f755d2a8e39cb2052dbe949cae5d6971faf Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 30 Nov 2010 21:27:21 +0100 Subject: [PATCH] getdomainname: Use the system function when possible. * lib/unistd.in.h: Include , 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 and . (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 . 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 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. --- ChangeLog | 26 ++++++++++ doc/glibc-functions/getdomainname.texi | 13 ++++- lib/getdomainname.c | 32 ++++++++++++- lib/unistd.in.h | 23 +++++++-- m4/getdomainname.m4 | 86 ++++++++++++++++++++++++++++++++-- m4/unistd_h.m4 | 5 +- modules/getdomainname | 2 + modules/unistd | 3 +- 8 files changed, 176 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6c541e6a4..99ad3ee4e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,30 @@ 2010-11-28 Bruno Haible + Paul Eggert + + getdomainname: Use the system function when possible. + * lib/unistd.in.h: Include , 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 and . + (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 . 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 + 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 sys_socket: Ensure ss_family field on AIX. * lib/sys_socket.in.h (ss_family): New macro definition. diff --git a/doc/glibc-functions/getdomainname.texi b/doc/glibc-functions/getdomainname.texi index e4952298e..6c022bd73 100644 --- a/doc/glibc-functions/getdomainname.texi +++ b/doc/glibc-functions/getdomainname.texi @@ -8,7 +8,18 @@ Portability problems fixed by Gnulib: @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: diff --git a/lib/getdomainname.c b/lib/getdomainname.c index 95ca39947..a6f7e3aa6 100644 --- 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 +#include #include #include +#if HAVE_SYSINFO && HAVE_SYS_SYSTEMINFO_H /* IRIX, OSF/1, Solaris */ +# include +#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 @@ getdomainname (char *name, size_t len) if (result_len < len) name[result_len] = '\0'; return 0; +#endif } diff --git a/lib/unistd.in.h b/lib/unistd.in.h index ea74d6311..4834e3749 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -88,6 +88,13 @@ # include #endif +/* AIX and OSF/1 5.1 declare getdomainname in , not in . */ +/* But avoid namespace pollution on glibc systems. */ +#if @GNULIB_GETDOMAINNAME@ && (defined _AIX || defined __osf__) \ + && !defined __GLIBC__ +# include +#endif + #if (@GNULIB_WRITE@ || @GNULIB_READLINK@ || @GNULIB_READLINKAT@ \ || @GNULIB_PREAD@ || @GNULIB_PWRITE@ || defined GNULIB_POSIXCHECK) /* Get ssize_t. */ @@ -551,13 +558,21 @@ _GL_WARN_ON_USE (getcwd, "getcwd is unportable - " 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 diff --git a/m4/getdomainname.m4 b/m4/getdomainname.m4 index 6ce943d4f..86bd73dc1 100644 --- 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 @@ dnl with or without modifications, as long as this notice is preserved. 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 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 . + 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 + 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 + #ifdef HAVE_SYS_SOCKET_H + #include + #endif + #ifdef HAVE_NETDB_H + #include + #endif + #include + ]) + 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 + #ifdef HAVE_SYS_SOCKET_H + #include + #endif + #ifdef HAVE_NETDB_H + #include + #endif + #include + 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]) ]) diff --git a/m4/unistd_h.m4 b/m4/unistd_h.m4 index 01dfb0481..e2f7f2bf4 100644 --- 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 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS], 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 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS], 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 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS], 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]) diff --git a/modules/getdomainname b/modules/getdomainname index 4f37a0cc2..5a9d23f58 100644 --- a/modules/getdomainname +++ b/modules/getdomainname @@ -8,6 +8,8 @@ m4/getdomainname.m4 Depends-on: unistd extensions +netdb +sys_socket configure.ac: gl_FUNC_GETDOMAINNAME diff --git a/modules/unistd b/modules/unistd index eab8ce9a5..344a18932 100644 --- a/modules/unistd +++ b/modules/unistd @@ -78,7 +78,6 @@ unistd.h: unistd.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H) -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 @@ unistd.h: unistd.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H) -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 @@ unistd.h: unistd.in.h $(CXXDEFS_H) $(ARG_NONNULL_H) $(WARN_ON_USE_H) -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' \ -- 2.11.0