Re: gethostname on 64-bit Windows
On 06/24/2016 11:01 PM, Tatsuro MATSUOKA wrote: - Original Message - From: John W. Eaton <j...@octave.org> To: gnulib <bug-gnulib@gnu.org> Cc: j...@octave.org Date: 2016/6/25, Sat 04:46 Subject: gethostname on 64-bit Windows I needed the attached change to cross compile Octave for i686-w64-mingw32. Without it, rpl_gethostname was undefined because w64 has a gethostname function so gethostname.c is not added to the list of files to build but gethostname was still being redefined to rpl_gethostname. jwe gethostname on 64-bit Windows I needed the attached change to cross compile Octave for i686-w64-mingw32. This is not related to your patch. The hostname Mingw w64 64 bit is x86_64-w64-mingw32. The hostname Mingw w64 32 bit is i686-w64-mingw32. Sorry, I copied the wrong system ID into the message. It should have been: x86_64-w64-mingw32 I was cross compiling to x86_64-w64-mingw32, which appears to have a gethostname function (HAVE_GETHOSTNAME is defined to 1) but without my patch, the code in gnulib's unistd.in.h file will define gethostname to rpl_gethostname, and gethostname is not added to gl_LIBOBJS and gl_LTLIBOBJS. I think it is sufficient to just avoid the #define in this case. jwe
Re: gethostname on 64-bit Windows
- Original Message - > From: John W. Eaton <j...@octave.org> > To: gnulib <bug-gnulib@gnu.org> > Cc: j...@octave.org > Date: 2016/6/25, Sat 04:46 > Subject: gethostname on 64-bit Windows > > I needed the attached change to cross compile Octave for i686-w64-mingw32. > > Without it, rpl_gethostname was undefined because w64 has a gethostname > function > so gethostname.c is not added to the list of files to build but gethostname > was > still being redefined to rpl_gethostname. > > jwe > gethostname on 64-bit Windows > I needed the attached change to cross compile Octave for i686-w64-mingw32. This is not related to your patch. The hostname Mingw w64 64 bit is x86_64-w64-mingw32. The hostname Mingw w64 32 bit is i686-w64-mingw32. Tatsuro
gethostname on 64-bit Windows
I needed the attached change to cross compile Octave for i686-w64-mingw32. Without it, rpl_gethostname was undefined because w64 has a gethostname function so gethostname.c is not added to the list of files to build but gethostname was still being redefined to rpl_gethostname. jwe diff -uNr a/libgnu/unistd.in.h b/libgnu/unistd.in.h --- a/libgnu/unistd.in.h2016-06-18 18:47:32.0 -0400 +++ b/libgnu/unistd.in.h2016-06-23 18:45:42.410781867 -0400 @@ -734,7 +734,7 @@ Null terminate it if the name is shorter than LEN. If the host name is longer than LEN, set errno = EINVAL and return -1. Return 0 if successful, otherwise set errno and return -1. */ -# if @UNISTD_H_HAVE_WINSOCK2_H@ +# if !@HAVE_GETHOSTNAME@ && @UNISTD_H_HAVE_WINSOCK2_H@ # if !(defined __cplusplus && defined GNULIB_NAMESPACE) # undef gethostname # define gethostname rpl_gethostname
RE: Trouble with gethostname and mingw-w64 x86_64
I have a follow-up to this. What was happening is that in the check for `gethostname`, it tries to compile without linking against ws2_32. Note that unistd.h includes winsock2.h on my platform. Now, normally this check will fail (at linking), BUT since a check has been made before for linking to X11 (which links to ws2_32), ws2_32 is being implicitly linked: x86_64-w64-mingw32-gcc -std=gnu99 -std=gnu99 -o conftest.exe -DWIN32 -I/home/tanjj/ffbuild/target/mingw64//include -I/mingw64/include -I/usr/local/include -I/include -g -DWIN32 -I/home/tanjj/ffbuild/target/mingw64//include -I/mingw64/include -I/usr/local/include -I/include -g -I${top_builddir} -I${top_srcdir} -L/home/tanjj/ffbuild/target/mingw64//lib -L/mingw64/lib -L/usr/local/lib -L/lib -lws2_32 conftest.c -lX11 -lltdl -lws2_32 5 Which means that this check succeeds when it should not. I have attached a patch to gethostname.m4 which reorders the check - if Winsock is found, try to compile by linking to it first. If that fails, then try the normal way. The patch works for me, but I know practically nothing about m4, so I have no idea if I'm doing the right thing or not. Thanks, Jeremy From: jt...@outlook.com To: bug-gnulib@gnu.org Subject: Trouble with gethostname and mingw-w64 x86_64 Date: Wed, 12 Nov 2014 19:53:40 +0800 I've been having some difficulty building FontForge on Windows (8.1 64bit) using msys2 and mingw-w64 (64-bit target). Up until a few months ago, everything was building fine, but now I'm facing an issue which I believe has to do with Gnulib. I can't say that I understand much of how gnulib acutually works, so forgive me if this is a basic error. I'll start off by saying that building with a 32-bit target works fine. FontForge has `gethostname` as a requirement, so the `gethostname` module is set from Gnulib. When building on 32-bit and when running the configure script, the detection for `gethostname` fails (not present) and so I believe gnulib does its own implementation and everything works. However, when building for 64-bit, the `gethostname` detection succeeds (it is available http://msdn.microsoft.com/en-us/library/windows/desktop/ms738527%28v=vs.85%29.aspx but not on XP). I believe this is where things go wrong. When building, Gnulib replaces instances of `gethostname` with `rpl_gethostname`, but `rpl_gethostname` is never compiled into the support library. For that matter, even though I can see `gethostname.c` under `lib/`, it is completely ignored when compiling. Thus when it comes to the linking stage, it falls over, with: .libs/libgutils_la-gnetwork.o: In function `ff_gethostname': C:\msys64\home\box\ffbuild\work\mingw64\fontforge\gutils/gnetwork.c:57: undefined reference to `rpl_gethostname' I have attached two files - one is the build log for 32-bit (which succeeds in building) and another for 64-bit (which fails to build). So, with that said does anyone know where to go from here? Any help in resolving this issue would be greatly appreciated. Thanks, Jeremy 0001-Reorder-the-check-for-gethostname-if-Winsock-is-pres.patch Description: Binary data
Trouble with gethostname and mingw-w64 x86_64
I've been having some difficulty building FontForge on Windows (8.1 64bit) using msys2 and mingw-w64 (64-bit target). Up until a few months ago, everything was building fine, but now I'm facing an issue which I believe has to do with Gnulib. I can't say that I understand much of how gnulib acutually works, so forgive me if this is a basic error. I'll start off by saying that building with a 32-bit target works fine. FontForge has `gethostname` as a requirement, so the `gethostname` module is set from Gnulib. When building on 32-bit and when running the configure script, the detection for `gethostname` fails (not present) and so I believe gnulib does its own implementation and everything works. However, when building for 64-bit, the `gethostname` detection succeeds (it is available http://msdn.microsoft.com/en-us/library/windows/desktop/ms738527%28v=vs.85%29.aspx but not on XP). I believe this is where things go wrong. When building, Gnulib replaces instances of `gethostname` with `rpl_gethostname`, but `rpl_gethostname` is never compiled into the support library. For that matter, even though I can see `gethostname.c` under `lib/`, it is completely ignored when compiling. Thus when it comes to the linking stage, it falls over, with: .libs/libgutils_la-gnetwork.o: In function `ff_gethostname': C:\msys64\home\box\ffbuild\work\mingw64\fontforge\gutils/gnetwork.c:57: undefined reference to `rpl_gethostname' I have attached two files - one is the build log for 32-bit (which succeeds in building) and another for 64-bit (which fails to build). So, with that said does anyone know where to go from here? Any help in resolving this issue would be greatly appreciated. Thanks, Jeremy bootstrap: running: libtoolize --quiet bootstrap: running: git submodule init bootstrap: running: git submodule update bootstrap: running: gnulib/gnulib-tool --aux-dir=config --m4-base=m4 --libtool --symlink --import warnings manywarnings c-strtod gethostname getline iconv inet_ntop localeconv progname... Module list with included dependencies (indented): absolute-header alloca-opt arpa_inet c-strtod errno error exitfail extensions extern-inline fd-hook float getdelim gethostname getline gettext-h havelib iconv include_next inet_ntop intprops locale localeconv malloc-posix manywarnings memchr msvc-inval msvc-nothrow multiarch netinet_in progname realloc-posix size_max snippet/_Noreturn snippet/arg-nonnull snippet/c++defs snippet/warn-on-use socketlib sockets socklen ssize_t stdalign stdarg stddef stdint stdio stdlib strcase strdup-posix strerror strerror-override string strings strndup strnlen strtok_r sys_socket sys_types sys_uio unistd vasnprintf vasprintf verify warnings wchar xalloc xalloc-die xalloc-oversized xsize xvasprintf Notice from module error: If you are using GNU gettext version 0.16.1 or older, add the following options to XGETTEXT_OPTIONS in your po/Makevars: --flag=error:3:c-format --flag=error_at_line:5:c-format Notice from module vasprintf: If you are using GNU gettext version 0.16.1 or older, add the following options to XGETTEXT_OPTIONS in your po/Makevars: --flag=asprintf:2:c-format --flag=vasprintf:2:c-format Notice from module xvasprintf: If you are using GNU gettext version 0.16.1 or older, add the following options to XGETTEXT_OPTIONS in your po/Makevars: --flag=xasprintf:1:c-format File list: build-aux/config.rpath build-aux/snippet/_Noreturn.h build-aux/snippet/arg-nonnull.h build-aux/snippet/c++defs.h build-aux/snippet/warn-on-use.h lib/alloca.in.h lib/arpa_inet.in.h lib/asnprintf.c lib/asprintf.c lib/c-strtod.c lib/c-strtod.h lib/errno.in.h lib/error.c lib/error.h lib/exitfail.c lib/exitfail.h lib/fd-hook.c lib/fd-hook.h lib/float+.h lib/float.c lib/float.in.h lib/getdelim.c lib/gethostname.c lib/getline.c lib/gettext.h lib/inet_ntop.c lib/intprops.h lib/itold.c lib/locale.in.h lib/localeconv.c lib/malloc.c lib/memchr.c lib/memchr.valgrind lib/msvc-inval.c lib/msvc-inval.h lib/msvc-nothrow.c lib/msvc-nothrow.h lib/netinet_in.in.h lib/printf-args.c lib/printf-args.h lib/printf-parse.c lib/printf-parse.h lib/progname.c lib/progname.h lib/realloc.c lib/size_max.h lib/sockets.c lib/sockets.h lib/stdalign.in.h lib/stdarg.in.h lib/stddef.in.h lib/stdint.in.h lib/stdio.in.h lib/stdlib.in.h lib/strcasecmp.c lib/strdup.c lib/strerror-override.c lib/strerror-override.h lib/strerror.c lib/string.in.h lib/strings.in.h lib/strncasecmp.c lib/strndup.c lib/strnlen.c lib/strtok_r.c lib/sys_socket.c lib/sys_socket.in.h lib/sys_types.in.h lib/sys_uio.in.h
RE: gethostname on HP-NonStop
Thanks, that should do, I'll test -Original Message- From: Bruno Haible [mailto:br...@clisp.org] Sent: Friday, December 24, 2010 4:41 PM To: Joachim Schmitz Cc: bug-gnulib@gnu.org Subject: Re: gethostname on HP-NonStop Joachim Schmitz wrote: diff -u ./gllib/unistd.in.h.orig ./gllib/unistd.in.h --- ./gllib/unistd.in.h.orig2010-12-20 19:57:37.0 -0600 +++ ./gllib/unistd.in.h 2010-12-23 07:57:13.0 -0600 @@ -51,6 +51,14 @@ # undef _GL_INCLUDING_WINSOCK2_H #endif +#ifdef __TANDEM /* HP-Nonstop has gethostname() in netdb.h */ +#include netdb.h +/* and MAXHOSTNAMELEN in arpa/namser.h */ #include arpa/nameser.h +#undef HOST_NAME_MAX #define HOST_NAME_MAX MAXHOSTNAMELEN #endif This goes in the right direction. HOST_NAME_MAX already being dealt with, this is what I'm committing: 2010-12-24 Bruno Haible br...@clisp.org gethostname: Ensure declaration on NonStop Kernel. * lib/unistd.in.h: Include netdb.h also on NonStop Kernel systems. Reported by Joachim Schmitz j...@schmitz-digital.de. --- lib/unistd.in.h.origFri Dec 24 16:37:08 2010 +++ lib/unistd.in.h Fri Dec 24 16:36:38 2010 @@ -88,9 +88,11 @@ # include io.h #endif -/* AIX and OSF/1 5.1 declare getdomainname in netdb.h, not in unistd.h. */ +/* AIX and OSF/1 5.1 declare getdomainname in netdb.h, not in unistd.h. + NonStop Kernel declares gethostname in netdb.h, not in unistd.h. +*/ /* But avoid namespace pollution on glibc systems. */ -#if @GNULIB_GETDOMAINNAME@ (defined _AIX || defined __osf__) \ +#if ((@GNULIB_GETDOMAINNAME@ (defined _AIX || defined __osf__)) \ + || (@GNULIB_GETHOSTNAME@ defined __TANDEM)) \ !defined __GLIBC__ # include netdb.h #endif
Re: gethostname on HP-NonStop
Joachim Schmitz wrote: gethostname missing (resp. not found, it is available and declared in netdb.h), in various places. I added it to gllib/unistd.in.h source='test-fcntl-h-c++.cc' object='test-fcntl-h-c++.o' libtool=no DEPDIR=.deps depmode=none /bin/sh ./../build-aux/depcomp c++ -DHAVE_CONFIG_H -I. -DGNULIB_STRICT_CHECKING=1 -I. -I. -I.. -I./.. -I../gllib -I./../gllib -I/usr/local/include -Wnowarn -c -o test-fcntl-h-c++.o test-fcntl-h-c++.cc _GL_CXXALIAS_SYS_CAST (gethostname, int, (char *name, size_t len)); ^ /usr/local/Floss/gnulib/testdir-posix/gltests/./../gllib/unistd.h, line 972: error(363): the global scope has no gethostname gethostname ought to be declared in unistd.h, not in netdb.h. Reference: http://pubs.opengroup.org/onlinepubs/9699919799/functions/gethostname.html Bruno
RE: gethostname on HP-NonStop
Oh well, it is not here. So some kind of workaround is needed. -Original Message- From: Bruno Haible [mailto:br...@clisp.org] Sent: Thursday, December 23, 2010 1:43 PM To: Joachim Schmitz Cc: bug-gnulib@gnu.org Subject: Re: gethostname on HP-NonStop Joachim Schmitz wrote: gethostname missing (resp. not found, it is available and declared in netdb.h), in various places. I added it to gllib/unistd.in.h source='test-fcntl-h-c++.cc' object='test-fcntl-h-c++.o' libtool=no DEPDIR=.deps depmode=none /bin/sh ./../build-aux/depcomp c++ -DHAVE_CONFIG_H -I. -DGNULIB_STRICT_CHECKING=1 -I. -I. -I.. -I./.. -I../gllib -I./../gllib -I/usr/local/include -Wnowarn -c -o test-fcntl-h-c++.o test-fcntl-h-c++.cc _GL_CXXALIAS_SYS_CAST (gethostname, int, (char *name, size_t len)); ^ /usr/local/Floss/gnulib/testdir-posix/gltests/./../gllib/unistd.h, line 972: error(363): the global scope has no gethostname gethostname ought to be declared in unistd.h, not in netdb.h. Reference: http://pubs.opengroup.org/onlinepubs/9699919799/functions/gethostname.html Bruno
Re: module libposix (was: module gethostname)
Hi Bruno, On Wed, Sep 15, 2010 at 11:18 AM, Bruno Haible br...@clisp.org wrote: Have you tried to follow the approach of http://www.gnu.org/software/gnulib/manual/html_node/POSIX-Substitutes-Library.html? I haven't, but I would like to hear about success or problems that you encounter. 1. A configure dinkleberry: $ file core.18420 core.18420: ELF 64-bit LSB core file AMD x86-64, version 1 (SYSV), SVR4-style, from 'conftest' 2. It looked like it went through a full configure and build, then obliterated the build output, necessitating a reconfig rebuild. Maybe just do it once? Whatever. 3. config.h needs to be installed. Now, where was that name munger for the config.h header 4. The various other headers that are determined to be needed as replacements for the system headers should be installed, too, as $(prefix)/include/gnulib/ 5. make install installs exactly one file: libexec/dummy/pt_chown 6. Some date stamp version should be inserted into config.h Despite all the caveats about feature tests vs. version tests, the basic idea with gnulib is that it is pretty much a single branch project. Consequently, needed features appear by a particular date (version id). The whole idea is to largely remove feature tests from individual projects*. Basically, it needs to be wrapped in some project wrapper stuff to do these things, along with the .pc file. Perhaps also: 7. the GNULIB_TEST_* preprocessing defines can get stripped out. This is a guess. I haven't investigated. * Clients of this libposix thing would thus have something along the lines of this in their configure.ac: AC_CHECK_LIBPOSIX([2010.09.16], AC_MSG_ERROR([libposix is too old or not installed]))
Re: module libposix (was: module gethostname)
Hi Bruce, * Bruce Korb wrote on Thu, Sep 16, 2010 at 08:00:46PM CEST: 3. config.h needs to be installed. Now, where was that name munger for the config.h header Thinking of AX_PREFIX_CONFIG_H from the Autoconf Macro Archive? BTW, that currently has a PR (with patch) open against it, for using unportable echo. Cheers, Ralf
Re: module gethostname
Bruno Haible wrote: I am now getting undefined reference to `_gethostname_used_without_requesting_gnulib_module_gethostname' This indicates that you have included gnulib unistd.h, are using gethostname(), and have not asked for the gnulib module 'gethostname'. - If you want to use a POSIX compliant gethostname() function, you need to request the 'gethostname' module. - If you want to use Win32 gethostname() - handling errors in the Win32 way, and guaranteeing the socket library initialization call yourself - then be sure to #include unistd.h, then #undef gethostname, and then #include winsock2.h and ws2tcpip.h. thanks. now I get a linking error: Warning: resolving _gethostname by linking to _gethostn...@8 Use --enable-stdcall-fixup to disable these warnings Use --disable-stdcall-fixup to disable these fixups libgnu_cl.a(sockets.o): In function `close_fd_maybe_socket': /cygdrive/c/sds/dev/current/build-mingw-O-2/gllib/../../src/gllib/sockets.c:44: undefined reference to `_wsaenumnetworkeve...@12' collect2: ld returned 1 exit status curiously, this happens only when I link the base linking set; the boot linking set is created just fine
module gethostname
The only problem which gethostname module fixes is: On mingw, this function has a prototype that differs from that specified by POSIX, and it is defined only in the ws2_32 library. why does it have such extensive dependencies? (sockets! errno!) I have to reiterate my complaint that the gnulib dependencies mean that one has to include (almost) the whole of gnulib if one includes (almost) any part thereof. the case in question is that I am now getting undefined reference to `_gethostname_used_without_requesting_gnulib_module_gethostname' gcc errors on mingw and I have to figure out which gnulib module did this to me.
Re: module gethostname
On Wed, Sep 15, 2010 at 9:34 AM, Paul Eggert egg...@cs.ucla.edu wrote: On 09/15/2010 08:41 AM, Sam Steingold wrote: I have to reiterate my complaint that the gnulib dependencies mean that one has to include (almost) the whole of gnulib if one includes (almost) any part thereof. I agree that gnulib has too many dependencies and [...] Once upon a time, long ago and far away, I proposed a new library, libcompat that more-or-less would make all target platforms look pretty much identical. Lo and behold, over the past decade, gnulib has gradually grown up to provide all that infrastructure. The only problem with it is that gnulib is glued in to each project adding truly remarkable amounts of configure time overhead to each and every project that depends upon its glue, and that would be true even with minimal gnulib interdependency. I do think it would be Really, Really Nice if there were a gnulib.pc file one could test. If it were not there, I could just fail the configure of my package and forget the several minutes of configuration overhead. If there were some recent gnulib feature I could not live without, then I might add that one bell or whistle to my project and the configure process might be extended by a fraction of a second. So, let's start that discussion again: let's make an installable gnulib that basically does all the configure tests for the current target and cover 99% of all configure runs. If someone has a strange configuration with extra libraries et al., then it ought to be straight forward enough to have an alternate gnulib.pc file installed in an alternate PATH setup. I do not see an obvious reason why the configure process should be so ridiculous. Or is that just me? ;)
Re: module gethostname
Hi Bruce, * Bruce Korb wrote on Wed, Sep 15, 2010 at 07:01:11PM CEST: So, let's start that discussion again: let's make an installable gnulib that basically does all the configure tests for the current target and cover 99% of all configure runs. How about a config.site file? Cheers, Ralf (ducks and runs ...)
Re: module gethostname
On Wed, Sep 15, 2010 at 10:04 AM, Ralf Wildenhues ralf.wildenh...@gmx.de wrote: Hi Bruce, * Bruce Korb wrote on Wed, Sep 15, 2010 at 07:01:11PM CEST: So, let's start that discussion again: let's make an installable gnulib that basically does all the configure tests for the current target and cover 99% of all configure runs. How about a config.site file? *ANY*thing. Yes! Cheers - Bruce (stands and laughs out loud)
Re: module gethostname
On Wed, Sep 15, 2010 at 12:01 PM, Bruce Korb bk...@gnu.org wrote: So, let's start that discussion again: let's make an installable gnulib that basically does all the configure tests for the current target and cover 99% of all configure runs. If someone has a strange configuration with extra libraries et al., then it ought to be straight forward enough to have an alternate gnulib.pc file installed in an alternate PATH setup. I do not see an obvious reason why the configure process should be so ridiculous. Or is that just me? ;) There are good reasons to want very close control of which compatibility functions are -- and are not -- included in the build of a particular project. Personally, I've seen *very* odd errors pop up during gnulib upgrades in Amanda[1], and I'm glad that I control exactly which version of gnulib ships with which version of Amanda, so that I can solve these problems once and not have to worry about a full compatibility matrix between Amanda and gnulib. There are also good reasons to want a libcompat that makes every platform provide roughly the same interfaces, removing compatibility concerns entirely from the downstream projects. Would it be possible to build libcompat as a separate project that depends substantially on gnulib? That is, it contains all gnulib objects and exposes their declarations and implementations in an ABI-compatible way? I suspect that this is harder than it sounds - gnulib does a lot of Funny Stuff with header files and whatnot - but it might be a good way to satisfy both needs, and if there are corner cases that gnulib can treat, but libcompat cannot, then projects requiring those corner cases can just use gnulib directly. Just an idea.. Dustin [1] To be fair to gnulib, in most cases these were the result of gray-area coding practices in Amanda, not bugs in gnulib. -- Open Source Storage Engineer http://www.zmanda.com
Re: module gethostname
Sam Steingold wrote: The only problem which gethostname module fixes is: On mingw, this function has a prototype that differs from that specified by POSIX, and it is defined only in the ws2_32 library. why does it have such extensive dependencies? (sockets! errno!) Look at the source code (lib/gethostname.c, lib/w32sock.h, lib/errno.in.h): - It needs the 'sockets' module, because on Woe32, socket functions are not usable without a prior initialization call. - It needs to map Woe32 error values to POSIX error values. EWOULDBLOCK is one of them, but mingw does not have EWOULDBLOCK in errno.h. I am now getting undefined reference to `_gethostname_used_without_requesting_gnulib_module_gethostname' This indicates that you have included gnulib unistd.h, are using gethostname(), and have not asked for the gnulib module 'gethostname'. - If you want to use a POSIX compliant gethostname() function, you need to request the 'gethostname' module. - If you want to use Win32 gethostname() - handling errors in the Win32 way, and guaranteeing the socket library initialization call yourself - then be sure to #include unistd.h, then #undef gethostname, and then #include winsock2.h and ws2tcpip.h. gcc errors on mingw and I have to figure out which gnulib module did this to me. This is certainly better than if gnulib would let you call the wrong gethostname() function silently. Bruno
Re: module gethostname
Bruce, Once upon a time, long ago and far away, I proposed a new library, libcompat that more-or-less would make all target platforms look pretty much identical. Lo and behold, over the past decade, gnulib has gradually grown up to provide all that infrastructure. The only problem with it is that gnulib is glued in to each project ... Have you tried to follow the approach of http://www.gnu.org/software/gnulib/manual/html_node/POSIX-Substitutes-Library.html? I haven't, but I would like to hear about success or problems that you encounter. I do think it would be Really, Really Nice if there were a gnulib.pc file one could test. Forget about pkg-config *.pc files. They don't allow the user to use a different compiler than the one with which the library was built. That's why gnulib offers modules 'readline', 'libsigsegv', 'libunistring', etc. They use autoconf macros that *do* work with Solaris cc as well as GCC. Bruno
Re: module libposix (was: module gethostname)
Hi Bruno, On Wed, Sep 15, 2010 at 11:18 AM, Bruno Haible br...@clisp.org wrote: Have you tried to follow the approach of http://www.gnu.org/software/gnulib/manual/html_node/POSIX-Substitutes-Library.html? I haven't, but I would like to hear about success or problems that you encounter. That should put together all the stuff to do it, but it isn't an installable package. Were it tweaked around to be one, then it ought to be what I am suggesting. Then it becomes a matter of how to test for its existence and version, with, for example, my play time toy requiring a version not more than 5 years old or so. I do think it would be Really, Really Nice if there were a gnulib.pc file one could test. Forget about pkg-config *.pc files. They don't allow the user to use a different compiler than the one with which the library was built. A standard config for a particular environment is going to use a particular compiler. If a package builder wants a different compiler, it would mean that the builder would want to use a different .pc file. Remember, this is targeted toward folks that want a vanilla install on their current platform, and don't want to spend more time configuring than compiling projects. Vanilla would imply vanilla compiler for this site also. From this perspective, I don't see an issue with Solaris CC vs. GCC. You could even combine two libposix installs via some ``diff -Dname'' magic on the output text files. (A hand wave -- I'm sure there are more details.) - Bruce
Re: [PATCH] gethostname: fix build on mingw
On 03/21/2010 01:45 PM, Matthias Bolte wrote: 2010/3/20 Eric Blake ebl...@redhat.com: Since commit 4e86671a, gethostname failed to compile on mingw. Gnulib's sys/socket.h includes winsock2.h, which then includes unistd.h prior to declaring gethostname. Therefore, unistd.h cannot replace gethostname until winsock2.h is complete, also fixing things if a .c includes unistd.h (or even winsock2.h) prior to sys/socket.h. If I apply the attached diff on top of this patch the problem is fixed. It compiles without errors, I can include unistd.h before sys/socket.h and I can include winsock2.h before sys/socket.h as long as the winsock2.h include is wrapped in #define/#undef _GL_INCLUDING_WINSOCK2_H. The important part is -# if @UNISTD_H_HAVE_WINSOCK2_H@ +# if @UNISTD_H_HAVE_WINSOCK2_H@ !defined _GL_INCLUDING_WINSOCK2_H This stops the winsock2.h-includes-unistd.h-case from undefining _GL_INCLUDING_WINSOCK2_H too early. Well, we're getting closer. But I'd really like to avoid making all clients of winsock2.h make an adjustment; it seems like it should still be possible to make _just_ unistd.h have the smarts necessary. Here's some pseudocode I'm currently thinking about (patch to come up later...) #if [_WINSOCK2_H and !witness] /* Someone has included winsock2.h prior to unistd.h - let it run to completion, without declaring any of our overrides; to be portable, the user will have to later include unistd.h if they plan on using gethostname. */ # include_next unistd.h # define witness #else /* Normal invocation */ # if !defined _GL_UNISTD_H # include_next unistd.h /* Include everything that declares something that we might want to override */ # define _GL_INCLUDING_WINSOCK2_H # include winsock2.h # undef _GL_INCLUDING_WINSOCK2_H /* Split include guard. */ # if !defined _GL_UNISTD_H !defined _GL_INCLUDING_WINSOCK2_H # define _GL_UNISTD_H /* Declare our overrides. */ # endif /* _GL_UNISTD_H/_GL_INCLUDING_WINSOCK2_H */ # endif /* _GL_UNISTD_H */ #endif /*_WINSOCK2_H */ -- Eric Blake ebl...@redhat.com+1-801-349-2682 Libvirt virtualization library http://libvirt.org signature.asc Description: OpenPGP digital signature
[PATCHv4] gethostname: fix build on mingw
Since commit 4e86671a, gethostname failed to compile on mingw. Gnulib's sys/socket.h includes winsock2.h, which then includes unistd.h prior to declaring gethostname. The fix is to ensure that our replacement unistd.h does not declare any replacements until we are sure that winsock2.h is completely included. * lib/unistd.in.h (includes): Work around fact that mingw winsock2.h re-includes unistd.h, by avoiding any redeclarations if we are being included by winsock2.h. Reported by Matthias Bolte. Signed-off-by: Eric Blake ebl...@redhat.com --- Try 4. This time, try localizing all the changes to just our replacement unistd.h. Detect whether we are being indirectly included by winsock2.h, so that clients should not have to adjust their calling patterns (although they DO need to explcitly include unistd.h if they plan on using gethostname, rather than relying on the nonstandard winsock2.h to provide it). ChangeLog |8 lib/unistd.in.h | 28 +--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index cc3ef5a..22dfca0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2010-03-22 Eric Blake ebl...@redhat.com + + gethostname: fix build on mingw + * lib/unistd.in.h (includes): Work around fact that mingw + winsock2.h re-includes unistd.h, by avoiding any + redeclarations if we are being included by winsock2.h. + Reported by Matthias Bolte. + 2010-03-20 Bruno Haible br...@clisp.org * m4/unlink.m4 (gl_FUNC_UNLINK): Fix last commit. diff --git a/lib/unistd.in.h b/lib/unistd.in.h index b74484a..b2b92c8 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -15,7 +15,23 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef _GL_UNISTD_H +/* Special invocation convention: + - On mingw, several headers, including winsock2.h, include unistd.h, + but we need to ensure that both the system unistd.h and winsock2.h + are completely included before we replace gethostname. */ +#if @UNISTD_H_HAVE_WINSOCK2_H@ !defined _GL_WINSOCK2_H_WITNESS \ + defined _WINSOCK2_H +/* unistd.h is being indirectly included for the first time from + winsock2.h; avoid declaring any overrides. */ +# if @HAVE_UNISTD_H +# @INCLUDE_NEXT@ @NEXT_UNISTD_H@ +# else +# error unexpected; report this to bug-gnulib@gnu.org +# endif +# define _GL_WINSOCK2_H_WITNESS + +/* Normal invocation. */ +#elif !defined _GL_UNISTD_H #if __GNUC__ = 3 @PRAGMA_SYSTEM_HEADER@ @@ -26,7 +42,14 @@ # @INCLUDE_NEXT@ @NEXT_UNISTD_H@ #endif -#ifndef _GL_UNISTD_H +/* Get all possible declarations of gethostname(). */ +#if @UNISTD_H_HAVE_WINSOCK2_H@ !defined _GL_INCLUDING_WINSOCK2_H +# define _GL_INCLUDING_WINSOCK2_H +# include winsock2.h +# undef _GL_INCLUDING_WINSOCK2_H +#endif + +#if !defined _GL_UNISTD_H !defined _GL_INCLUDING_WINSOCK2_H #define _GL_UNISTD_H /* NetBSD 5.0 mis-defines NULL. Also get size_t. */ @@ -76,7 +99,6 @@ #if @GNULIB_GETHOSTNAME@ /* Get all possible declarations of gethostname(). */ # if @UNISTD_H_HAVE_WINSOCK2_H@ -# include winsock2.h # if !defined _GL_SYS_SOCKET_H # undef socket # define socket socket_used_without_including_sys_socket_h -- 1.6.6.1
Re: [PATCHv4] gethostname: fix build on mingw
Eric Blake ebl...@redhat.com writes: Since commit 4e86671a, gethostname failed to compile on mingw. Gnulib's sys/socket.h includes winsock2.h, which then includes unistd.h prior to declaring gethostname. The fix is to ensure that our replacement unistd.h does not declare any replacements until we are sure that winsock2.h is completely included. Testing the patch like this: rm -rf m gnulib-tool --create-testdir --dir m getsubopt gethostname cd m ./configure --host=i586-mingw32msvc --build=i686-pc-linux-gnu make check results in: make[3]: Entering directory `/home/jas/src/gnulib/m/gllib' i586-mingw32msvc-gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT close-hook.o -MD -MP -MF .deps/close-hook.Tpo -c -o close-hook.o close-hook.c In file included from close-hook.c:24: ./unistd.h:27:6: error: token @ is not valid in preprocessor expressions ./unistd.h:30:4: error: #error unexpected; report this to bug-gnulib@gnu.org make[3]: *** [close-hook.o] Error 1 make[3]: Leaving directory `/home/jas/src/gnulib/m/gllib' /Simon
Re: [PATCHv4] gethostname: fix build on mingw
On 03/22/2010 11:39 AM, Simon Josefsson wrote: Eric Blake ebl...@redhat.com writes: Since commit 4e86671a, gethostname failed to compile on mingw. Gnulib's sys/socket.h includes winsock2.h, which then includes unistd.h prior to declaring gethostname. The fix is to ensure that our replacement unistd.h does not declare any replacements until we are sure that winsock2.h is completely included. Testing the patch like this: rm -rf m gnulib-tool --create-testdir --dir m getsubopt gethostname cd m ./configure --host=i586-mingw32msvc --build=i686-pc-linux-gnu make check results in: make[3]: Entering directory `/home/jas/src/gnulib/m/gllib' i586-mingw32msvc-gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT close-hook.o -MD -MP -MF .deps/close-hook.Tpo -c -o close-hook.o close-hook.c In file included from close-hook.c:24: ./unistd.h:27:6: error: token @ is not valid in preprocessor expressions Typo on line 26. diff --git i/lib/unistd.in.h w/lib/unistd.in.h index 28930cb..beef6b0 100644 --- i/lib/unistd.in.h +++ w/lib/unistd.in.h @@ -23,7 +23,7 @@ defined _WINSOCK2_H /* unistd.h is being indirectly included for the first time from winsock2.h; avoid declaring any overrides. */ -# if @HAVE_UNISTD_H +# if @HAVE_UNISTD_H@ # @INCLUDE_NEXT@ @NEXT_UNISTD_H@ # else # error unexpected; report this to bug-gnulib@gnu.org -- Eric Blake ebl...@redhat.com+1-801-349-2682 Libvirt virtualization library http://libvirt.org signature.asc Description: OpenPGP digital signature
Re: [PATCHv4] gethostname: fix build on mingw
Eric Blake ebl...@redhat.com writes: On 03/22/2010 11:39 AM, Simon Josefsson wrote: Eric Blake ebl...@redhat.com writes: Since commit 4e86671a, gethostname failed to compile on mingw. Gnulib's sys/socket.h includes winsock2.h, which then includes unistd.h prior to declaring gethostname. The fix is to ensure that our replacement unistd.h does not declare any replacements until we are sure that winsock2.h is completely included. Testing the patch like this: rm -rf m gnulib-tool --create-testdir --dir m getsubopt gethostname cd m ./configure --host=i586-mingw32msvc --build=i686-pc-linux-gnu make check results in: make[3]: Entering directory `/home/jas/src/gnulib/m/gllib' i586-mingw32msvc-gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT close-hook.o -MD -MP -MF .deps/close-hook.Tpo -c -o close-hook.o close-hook.c In file included from close-hook.c:24: ./unistd.h:27:6: error: token @ is not valid in preprocessor expressions Typo on line 26. Thanks, now it works. Any reason not to push this? /Simon
Re: [PATCHv4] gethostname: fix build on mingw
On 03/22/2010 11:56 AM, Simon Josefsson wrote: Typo on line 26. Thanks, now it works. Any reason not to push this? Only that I was not set up to reproduce the failure (cross-compiling from cygwin was running into its own set of problems, because cygwin's gcc-3 compiler mistakenly picks up cygwin's sys/socket.h instead of realizing that mingw has no sys/socket.h). I was relying on a feedback loop involving testing by Matthias; but your success report also works for me. Meanwhile, this thread convinced me to download i686-pc-mingw32-gcc for Fedora, and I have just barely reproduced the issue (and the fact that it is fixed) for myself. So it is now pushed! -- Eric Blake ebl...@redhat.com+1-801-349-2682 Libvirt virtualization library http://libvirt.org signature.asc Description: OpenPGP digital signature
Re: [PATCHv4] gethostname: fix build on mingw
2010/3/22 Eric Blake ebl...@redhat.com: Since commit 4e86671a, gethostname failed to compile on mingw. Gnulib's sys/socket.h includes winsock2.h, which then includes unistd.h prior to declaring gethostname. The fix is to ensure that our replacement unistd.h does not declare any replacements until we are sure that winsock2.h is completely included. * lib/unistd.in.h (includes): Work around fact that mingw winsock2.h re-includes unistd.h, by avoiding any redeclarations if we are being included by winsock2.h. Reported by Matthias Bolte. Signed-off-by: Eric Blake ebl...@redhat.com --- Try 4. This time, try localizing all the changes to just our replacement unistd.h. Detect whether we are being indirectly included by winsock2.h, so that clients should not have to adjust their calling patterns (although they DO need to explcitly include unistd.h if they plan on using gethostname, rather than relying on the nonstandard winsock2.h to provide it). ChangeLog | 8 lib/unistd.in.h | 28 +--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index cc3ef5a..22dfca0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2010-03-22 Eric Blake ebl...@redhat.com + + gethostname: fix build on mingw + * lib/unistd.in.h (includes): Work around fact that mingw + winsock2.h re-includes unistd.h, by avoiding any + redeclarations if we are being included by winsock2.h. + Reported by Matthias Bolte. + 2010-03-20 Bruno Haible br...@clisp.org * m4/unlink.m4 (gl_FUNC_UNLINK): Fix last commit. diff --git a/lib/unistd.in.h b/lib/unistd.in.h index b74484a..b2b92c8 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -15,7 +15,23 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#ifndef _GL_UNISTD_H +/* Special invocation convention: + - On mingw, several headers, including winsock2.h, include unistd.h, + but we need to ensure that both the system unistd.h and winsock2.h + are completely included before we replace gethostname. */ +#if @UNISTD_H_HAVE_WINSOCK2_H@ !defined _GL_WINSOCK2_H_WITNESS \ + defined _WINSOCK2_H +/* unistd.h is being indirectly included for the first time from + winsock2.h; avoid declaring any overrides. */ +# if @HAVE_UNISTD_H An @ is missing here. +# �...@include_next@ @NEXT_UNISTD_H@ +# else +# error unexpected; report this to bug-gnulib@gnu.org +# endif +# define _GL_WINSOCK2_H_WITNESS + +/* Normal invocation. */ +#elif !defined _GL_UNISTD_H #if __GNUC__ = 3 �...@pragma_system_header@ @@ -26,7 +42,14 @@ # @INCLUDE_NEXT@ @NEXT_UNISTD_H@ #endif -#ifndef _GL_UNISTD_H +/* Get all possible declarations of gethostname(). */ +#if @UNISTD_H_HAVE_WINSOCK2_H@ !defined _GL_INCLUDING_WINSOCK2_H +# define _GL_INCLUDING_WINSOCK2_H +# include winsock2.h +# undef _GL_INCLUDING_WINSOCK2_H +#endif + +#if !defined _GL_UNISTD_H !defined _GL_INCLUDING_WINSOCK2_H #define _GL_UNISTD_H /* NetBSD 5.0 mis-defines NULL. Also get size_t. */ @@ -76,7 +99,6 @@ #if @GNULIB_GETHOSTNAME@ /* Get all possible declarations of gethostname(). */ # if @UNISTD_H_HAVE_WINSOCK2_H@ -# include winsock2.h # if !defined _GL_SYS_SOCKET_H # undef socket # define socket socket_used_without_including_sys_socket_h -- 1.6.6.1 ACK. This one fixes the issue. Matthias
Re: [PATCHv4] gethostname: fix build on mingw
Eric Blake wrote: +/* Get all possible declarations of gethostname(). */ +#if @UNISTD_H_HAVE_WINSOCK2_H@ !defined _GL_INCLUDING_WINSOCK2_H +# define _GL_INCLUDING_WINSOCK2_H +# include winsock2.h +# undef _GL_INCLUDING_WINSOCK2_H +#endif Would it be possible to conditionalize this with #if @GNULIB_GETHOSTNAME@ like it was before? In a package that has nothing to do with sockets and nothing to do with gethostname(), I wouldn't want to see all winsock2.h definitions in virtually all compilation units. Bruno
Re: [PATCH] gethostname: fix build on mingw
2010/3/20 Eric Blake ebl...@redhat.com: On 03/19/2010 05:35 PM, Matthias Bolte wrote: I believe this will solve the issue, but would appreciate some testing by Matthias. I also think that this is the same issue as: http://lists.gnu.org/archive/html/bug-gnulib/2010-01/msg00183.html I tested this patch and can confirm that this patch fixes the gethostname compile issue on MinGW. Thanks. I think the patch works for including sys/socket.h first, but what happens if you include unistd.h or winsock2.h first? I think there's still some circularities that need to be resolved. That is, gnulib's unistd.h needs to be smarter about ensuring that winsock2.h is completely included prior to getting to its own replacements. I'm working on a respin along these lines... The first version of the patch solves the problem for the current gnulib code. But as you already supposed, if I change sockets.c to include winsock2.h before sys/socket.h (actually before sockets.h, because it also includes sys/socket.h), then the problem is back again. On the other hand including unistd.h before sys/socket.h works. Matthias
Re: [PATCH] gethostname: fix build on mingw
2010/3/20 Eric Blake ebl...@redhat.com: Since commit 4e86671a, gethostname failed to compile on mingw. Gnulib's sys/socket.h includes winsock2.h, which then includes unistd.h prior to declaring gethostname. Therefore, unistd.h cannot replace gethostname until winsock2.h is complete, also fixing things if a .c includes unistd.h (or even winsock2.h) prior to sys/socket.h. * lib/sys_socket.in.h (includes): Set witness when including winsock2.h. * lib/unistd.in.h (includes): Likewise, after hoisting winsock2.h inclusion. Don't replace anything when included by winsock2.h. Reported by Matthias Bolte. Signed-off-by: Eric Blake ebl...@redhat.com --- I think this is more robust, but would appreciate another round of testing before I push. ChangeLog | 9 + lib/sys_socket.in.h | 7 ++- lib/unistd.in.h | 15 +-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index de054fa..2125639 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2010-03-19 Eric Blake ebl...@redhat.com + + gethostname: fix build on mingw + * lib/sys_socket.in.h (includes): Set witness when including + winsock2.h. + * lib/unistd.in.h (includes): Don't replace anything when included + by winsock2.h. + Reported by Matthias Bolte. + 2010-02-17 Eric Blake e...@byu.net manywarnings: add more warnings diff --git a/lib/sys_socket.in.h b/lib/sys_socket.in.h index 006a901..b34b506 100644 --- a/lib/sys_socket.in.h +++ b/lib/sys_socket.in.h @@ -28,7 +28,10 @@ - On Cygwin 1.5.x we have a sequence of nested includes sys/socket.h - cygwin/socket.h - asm/socket.h - cygwin/if.h, and the latter includes sys/socket.h. In this situation, the functions - are not yet declared, therefore we cannot provide the C++ aliases. */ + are not yet declared, therefore we cannot provide the C++ aliases. + - On mingw, sys/socket.h - winsock2.h - unistd.h, and the latter + includes winsock2.h. In this situation, functions like gethostname + are not yet declared, therefore we must defer. */ �...@include_next@ @NEXT_SYS_SOCKET_H@ @@ -129,7 +132,9 @@ struct sockaddr_storage # if @HAVE_WINSOCK2_H@ +# define _GL_INCLUDING_WINSOCK2_H # include winsock2.h +# undef _GL_INCLUDING_WINSOCK2_H # endif # if @HAVE_WS2TCPIP_H@ # include ws2tcpip.h diff --git a/lib/unistd.in.h b/lib/unistd.in.h index b74484a..a57ba37 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -26,7 +26,19 @@ # @INCLUDE_NEXT@ @NEXT_UNISTD_H@ #endif -#ifndef _GL_UNISTD_H +/* On mingw, winsock2.h includes unistd.h prior to declaring + gethostname, but we want to replace gethostname. We must ensure + that winsock2.h is completely included first. */ +#if @GNULIB_GETHOSTNAME@ +/* Get all possible declarations of gethostname(). */ +# if @UNISTD_H_HAVE_WINSOCK2_H@ +# define _GL_INCLUDING_WINSOCK2_H +# include winsock2.h +# undef _GL_INCLUDING_WINSOCK2_H +# endif +#endif + +#if !defined _GL_UNISTD_H !defined _GL_INCLUDING_WINSOCK2_H #define _GL_UNISTD_H /* NetBSD 5.0 mis-defines NULL. Also get size_t. */ @@ -76,7 +88,6 @@ #if @GNULIB_GETHOSTNAME@ /* Get all possible declarations of gethostname(). */ # if @UNISTD_H_HAVE_WINSOCK2_H@ -# include winsock2.h # if !defined _GL_SYS_SOCKET_H # undef socket # define socket socket_used_without_including_sys_socket_h -- 1.6.6.1 If I apply the attached diff on top of this patch the problem is fixed. It compiles without errors, I can include unistd.h before sys/socket.h and I can include winsock2.h before sys/socket.h as long as the winsock2.h include is wrapped in #define/#undef _GL_INCLUDING_WINSOCK2_H. The important part is -# if @UNISTD_H_HAVE_WINSOCK2_H@ +# if @UNISTD_H_HAVE_WINSOCK2_H@ !defined _GL_INCLUDING_WINSOCK2_H This stops the winsock2.h-includes-unistd.h-case from undefining _GL_INCLUDING_WINSOCK2_H too early. Matthias diff --git a/lib/poll.c b/lib/poll.c index 90d99d9..024ffa9 100644 --- a/lib/poll.c +++ b/lib/poll.c @@ -35,7 +35,9 @@ #if (defined _WIN32 || defined __WIN32__) ! defined __CYGWIN__ # define WIN32_NATIVE -# include winsock2.h +# define _GL_INCLUDING_WINSOCK2_H +# include winsock2.h +# undef _GL_INCLUDING_WINSOCK2_H # include windows.h # include io.h # include stdio.h diff --git a/lib/select.c b/lib/select.c index 3564566..66468bf 100644 --- a/lib/select.c +++ b/lib/select.c @@ -31,7 +31,9 @@ #include errno.h #include limits.h -#include winsock2.h +#define _GL_INCLUDING_WINSOCK2_H +# include winsock2.h +#undef _GL_INCLUDING_WINSOCK2_H #include windows.h #include io.h #include stdio.h diff --git a/lib/strerror.c b/lib/strerror.c index b0df778..c13aa1c 100644 --- a/lib/strerror.c +++ b/lib/strerror.c @@ -26,7 +26,9 @@ # if GNULIB_defined_ESOCK /* native Windows platforms */ # if HAVE_WINSOCK2_H -# include winsock2
Re: [PATCH] gethostname: fix build on mingw
On 03/19/2010 05:35 PM, Matthias Bolte wrote: I believe this will solve the issue, but would appreciate some testing by Matthias. I also think that this is the same issue as: http://lists.gnu.org/archive/html/bug-gnulib/2010-01/msg00183.html I tested this patch and can confirm that this patch fixes the gethostname compile issue on MinGW. Thanks. I think the patch works for including sys/socket.h first, but what happens if you include unistd.h or winsock2.h first? I think there's still some circularities that need to be resolved. That is, gnulib's unistd.h needs to be smarter about ensuring that winsock2.h is completely included prior to getting to its own replacements. I'm working on a respin along these lines... -- Eric Blake ebl...@redhat.com+1-801-349-2682 Libvirt virtualization library http://libvirt.org signature.asc Description: OpenPGP digital signature
[PATCH] gethostname: fix build on mingw
Since commit 4e86671a, gethostname failed to compile on mingw. Gnulib's sys/socket.h includes winsock2.h, which then includes unistd.h prior to declaring gethostname. Therefore, unistd.h cannot replace gethostname until winsock2.h is complete, also fixing things if a .c includes unistd.h (or even winsock2.h) prior to sys/socket.h. * lib/sys_socket.in.h (includes): Set witness when including winsock2.h. * lib/unistd.in.h (includes): Likewise, after hoisting winsock2.h inclusion. Don't replace anything when included by winsock2.h. Reported by Matthias Bolte. Signed-off-by: Eric Blake ebl...@redhat.com --- I think this is more robust, but would appreciate another round of testing before I push. ChangeLog |9 + lib/sys_socket.in.h |7 ++- lib/unistd.in.h | 15 +-- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index de054fa..2125639 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2010-03-19 Eric Blake ebl...@redhat.com + + gethostname: fix build on mingw + * lib/sys_socket.in.h (includes): Set witness when including + winsock2.h. + * lib/unistd.in.h (includes): Don't replace anything when included + by winsock2.h. + Reported by Matthias Bolte. + 2010-02-17 Eric Blake e...@byu.net manywarnings: add more warnings diff --git a/lib/sys_socket.in.h b/lib/sys_socket.in.h index 006a901..b34b506 100644 --- a/lib/sys_socket.in.h +++ b/lib/sys_socket.in.h @@ -28,7 +28,10 @@ - On Cygwin 1.5.x we have a sequence of nested includes sys/socket.h - cygwin/socket.h - asm/socket.h - cygwin/if.h, and the latter includes sys/socket.h. In this situation, the functions - are not yet declared, therefore we cannot provide the C++ aliases. */ + are not yet declared, therefore we cannot provide the C++ aliases. + - On mingw, sys/socket.h - winsock2.h - unistd.h, and the latter + includes winsock2.h. In this situation, functions like gethostname + are not yet declared, therefore we must defer. */ #...@include_next@ @NEXT_SYS_SOCKET_H@ @@ -129,7 +132,9 @@ struct sockaddr_storage # if @HAVE_WINSOCK2_H@ +# define _GL_INCLUDING_WINSOCK2_H # include winsock2.h +# undef _GL_INCLUDING_WINSOCK2_H # endif # if @HAVE_WS2TCPIP_H@ # include ws2tcpip.h diff --git a/lib/unistd.in.h b/lib/unistd.in.h index b74484a..a57ba37 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -26,7 +26,19 @@ # @INCLUDE_NEXT@ @NEXT_UNISTD_H@ #endif -#ifndef _GL_UNISTD_H +/* On mingw, winsock2.h includes unistd.h prior to declaring + gethostname, but we want to replace gethostname. We must ensure + that winsock2.h is completely included first. */ +#if @GNULIB_GETHOSTNAME@ +/* Get all possible declarations of gethostname(). */ +# if @UNISTD_H_HAVE_WINSOCK2_H@ +# define _GL_INCLUDING_WINSOCK2_H +# include winsock2.h +# undef _GL_INCLUDING_WINSOCK2_H +# endif +#endif + +#if !defined _GL_UNISTD_H !defined _GL_INCLUDING_WINSOCK2_H #define _GL_UNISTD_H /* NetBSD 5.0 mis-defines NULL. Also get size_t. */ @@ -76,7 +88,6 @@ #if @GNULIB_GETHOSTNAME@ /* Get all possible declarations of gethostname(). */ # if @UNISTD_H_HAVE_WINSOCK2_H@ -# include winsock2.h # if !defined _GL_SYS_SOCKET_H # undef socket # define socket socket_used_without_including_sys_socket_h -- 1.6.6.1
[PATCH] gethostname: fix build on mingw
Since commit 4e86671a, gethostname failed to compile on mingw. Gnulib's sys/socket.h includes winsock2.h, which then includes unistd.h prior to declaring gethostname. Therefore, unistd.h cannot replace gethostname until winsock2.h is complete. * lib/sys_socket.in.h (includes): Set witness when including winsock2.h. * lib/unistd.in.h (includes): Don't replace anything when included by winsock2.h. Reported by Matthias Bolte. Signed-off-by: Eric Blake ebl...@redhat.com --- Thanks to Matthias for doing the git bisect to narrow down the cause of his error: # i686-pc-mingw32-gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT sockets.o -MD -MP -MF -deps/sockets.Tpo -c -o sockets.o sockets.c In file included from ./sys/socket.h:389, from sockets.h:37, from sockets.c:23: /usr/i686-pc-mingw32/sys-root/mingw/include/winsock2.h:635: error: conflicting types for 'rpl_gethostname' ./unistd.h:824: note: previous declaration of 'rpl_gethostname' was here I believe this will solve the issue, but would appreciate some testing by Matthias. I also think that this is the same issue as: http://lists.gnu.org/archive/html/bug-gnulib/2010-01/msg00183.html ChangeLog |9 + lib/sys_socket.in.h |7 ++- lib/unistd.in.h |3 ++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index df8b7a6..71be18e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2010-03-19 Eric Blake ebl...@redhat.com + + gethostname: fix build on mingw + * lib/sys_socket.in.h (includes): Set witness when including + winsock2.h. + * lib/unistd.in.h (includes): Don't replace anything when included + by winsock2.h. + Reported by Matthias Bolte. + 2010-02-17 Eric Blake e...@byu.net manywarnings: add more warnings diff --git a/lib/sys_socket.in.h b/lib/sys_socket.in.h index 006a901..b34b506 100644 --- a/lib/sys_socket.in.h +++ b/lib/sys_socket.in.h @@ -28,7 +28,10 @@ - On Cygwin 1.5.x we have a sequence of nested includes sys/socket.h - cygwin/socket.h - asm/socket.h - cygwin/if.h, and the latter includes sys/socket.h. In this situation, the functions - are not yet declared, therefore we cannot provide the C++ aliases. */ + are not yet declared, therefore we cannot provide the C++ aliases. + - On mingw, sys/socket.h - winsock2.h - unistd.h, and the latter + includes winsock2.h. In this situation, functions like gethostname + are not yet declared, therefore we must defer. */ #...@include_next@ @NEXT_SYS_SOCKET_H@ @@ -129,7 +132,9 @@ struct sockaddr_storage # if @HAVE_WINSOCK2_H@ +# define _GL_INCLUDING_WINSOCK2_H # include winsock2.h +# undef _GL_INCLUDING_WINSOCK2_H # endif # if @HAVE_WS2TCPIP_H@ # include ws2tcpip.h diff --git a/lib/unistd.in.h b/lib/unistd.in.h index 68e1a87..c8d4339 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -26,7 +26,8 @@ # @INCLUDE_NEXT@ @NEXT_UNISTD_H@ #endif -#ifndef _GL_UNISTD_H +/* See sys_socket.in.h for mingw workaround. */ +#if !defined _GL_UNISTD_H !defined _GL_INCLUDING_WINSOCK2_H #define _GL_UNISTD_H /* NetBSD 5.0 mis-defines NULL. Also get size_t. */ -- 1.6.6.1
Re: [PATCH] gethostname: fix build on mingw
2010/3/20 Eric Blake ebl...@redhat.com: Since commit 4e86671a, gethostname failed to compile on mingw. Gnulib's sys/socket.h includes winsock2.h, which then includes unistd.h prior to declaring gethostname. Therefore, unistd.h cannot replace gethostname until winsock2.h is complete. * lib/sys_socket.in.h (includes): Set witness when including winsock2.h. * lib/unistd.in.h (includes): Don't replace anything when included by winsock2.h. Reported by Matthias Bolte. Signed-off-by: Eric Blake ebl...@redhat.com --- Thanks to Matthias for doing the git bisect to narrow down the cause of his error: # i686-pc-mingw32-gcc -DHAVE_CONFIG_H -I. -I.. -g -O2 -MT sockets.o -MD -MP -MF -deps/sockets.Tpo -c -o sockets.o sockets.c In file included from ./sys/socket.h:389, from sockets.h:37, from sockets.c:23: /usr/i686-pc-mingw32/sys-root/mingw/include/winsock2.h:635: error: conflicting types for 'rpl_gethostname' ./unistd.h:824: note: previous declaration of 'rpl_gethostname' was here I believe this will solve the issue, but would appreciate some testing by Matthias. I also think that this is the same issue as: http://lists.gnu.org/archive/html/bug-gnulib/2010-01/msg00183.html I tested this patch and can confirm that this patch fixes the gethostname compile issue on MinGW. Thanks. Matthias
Re: gethostname on Windows
Bruno Haible br...@clisp.org writes: You are right. In summary: - GetComputerNameEx(ComputerNameDnsHostname,...) returns the right hostname, but it hard to use portably: - It requires defining _WIN32_WINNT to at least 0x0500. - With mingw, it also requires #define GetComputerNameEx GetComputerNameExA. - With older versions of mingw, none of the declarations are present at all, not even of the enum value ComputerNameDnsHostname. - GetComputerName() does not return the right kind of hostname. - gethostname() requires linking with -lws2_32. I'm now convinced that linking with -lws2_32 is the least evil. I have applied your patch from 2009-03-31. Great, thanks. I fixed a minor typo in the comment and pushed that. /Simon
Re: gethostname, socket need startup code
Bruno Haible br...@clisp.org writes: Hi Simon, On mingw, the gethostname test fails for me: gethostname failed, rc -1 errno 10093 FAIL: test-gethostname.exe The reason is that the WSAStartup function has not been called. Why not make this initialization implicitly in the gethostname function? And likewise for the socket() function? With that, the unit tests (test-select.c and test-poll.c) don't need to do this initialization explicitly any more. One less portability problem when porting to mingw. Here are two proposed patches. I verified that the behaviour of test-select and test-poll does not change, and it fixes the gethostname failure. Looks fine to me, please push it. /Simon
Re: gethostname, socket need startup code
Simon Josefsson wrote: Looks fine to me, please push it. Pushed. Likewise, the 'sys_utsname' and 'uname' modules are pushed as well. Bruno
Re: gethostname on Windows
Simon Josefsson wrote: I'm undecided. Avoiding -lws2_32 is good, but if the application links to -lws2_32 anyway, there will be no saving. ... Some additional questions: 1) How do I use it? This doesn't seem to work: j...@mocca:~$ cat foo.c #define WINVER 0x0500 #include windows.h #include stdio.h int main () { char out[1024]; DWORD size = 1024; BOOL t = GetComputerNameEx(ComputerNameDnsHostname, out, size); printf(hi %d: %s, t, out); } j...@mocca:~$ i586-mingw32msvc-gcc -o foo.exe foo.c /tmp/ccDXn2yi.o:foo.c:(.text+0x37): undefined reference to `_GetComputerNameEx' collect2: ld returned 1 exit status j...@mocca:~$ 2) What is the maximum string size that GetComputerNameEx can return? For the gethostname, the max size is documented. 3) Is the GetComputerNameEx semantics right? You are right. In summary: - GetComputerNameEx(ComputerNameDnsHostname,...) returns the right hostname, but it hard to use portably: - It requires defining _WIN32_WINNT to at least 0x0500. - With mingw, it also requires #define GetComputerNameEx GetComputerNameExA. - With older versions of mingw, none of the declarations are present at all, not even of the enum value ComputerNameDnsHostname. - GetComputerName() does not return the right kind of hostname. - gethostname() requires linking with -lws2_32. I'm now convinced that linking with -lws2_32 is the least evil. I have applied your patch from 2009-03-31. Also, I'm fixing the handling of len INT_MAX. 2009-08-02 Bruno Haible br...@clisp.org * lib/gethostname.c (gethostname): Fix handling of large len argument. Add comments. --- lib/gethostname.c.orig 2009-08-02 12:02:00.0 +0200 +++ lib/gethostname.c 2009-08-02 11:59:12.0 +0200 @@ -21,6 +21,7 @@ #include config.h #if !((defined _WIN32 || defined __WIN32__) !defined __CYGWIN__) +/* Unix API. */ /* Specification. */ #include unistd.h @@ -59,6 +60,17 @@ } #else +/* Native Windows API. Which primitive to choose? + - gethostname() requires linking with -lws2_32. + - GetComputerName() does not return the right kind of hostname. + - GetComputerNameEx(ComputerNameDnsHostname,...) returns the right hostname, + but it hard to use portably: + - It requires defining _WIN32_WINNT to at least 0x0500. + - With mingw, it also requires + #define GetComputerNameEx GetComputerNameExA. + - With older versions of mingw, none of the declarations are present at + all, not even of the enum value ComputerNameDnsHostname. + So we use gethostname(). Linking with -lws2_32 is the least evil. */ #define WIN32_LEAN_AND_MEAN /* Get winsock2.h. */ @@ -70,9 +82,13 @@ #undef gethostname int -rpl_gethostname (char *name, size_t namelen) +rpl_gethostname (char *name, size_t len) { - int r = gethostname (name, (int) namelen); + int r; + + if (len INT_MAX) +len = INT_MAX; + r = gethostname (name, (int) len); if (r 0) set_winsock_errno ();
Re: gethostname, socket need startup code
Paolo Bonzini wrote: I'm wondering if some programs out there used WSAStartup autonomously without going through the sockets module... That would not hurt. WSAStartup succeeds when called repeatedly, even when called with different version numbers: $ cat foo.c #include winsock2.h #include ws2tcpip.h #include stdio.h int main() { WSADATA data; int err; err = WSAStartup (0x101, data); printf (%d\n, err); err = WSAStartup (0x101, data); printf (%d\n, err); err = WSAStartup (0x201, data); printf (%d\n, err); err = WSAStartup (0x101, data); printf (%d\n, err); return 0; } $ gcc -mno-cygwin foo.c -lws2_32 $ ./a.exe 0 0 0 0 Bruno
Re: gethostname on Windows
Martin Lambers wrote: This requires to include limits.h in gethostname.c, to get INT_MAX. Martin diff --git a/lib/gethostname.c b/lib/gethostname.c index ef58a40..422184a 100644 --- a/lib/gethostname.c +++ b/lib/gethostname.c @@ -76,6 +76,9 @@ gethostname (char *name, size_t len) /* Get winsock2.h. */ #include unistd.h +/* Get INT_MAX. */ +#include limits.h + /* Get set_winsock_errno. */ #include w32sock.h Oops, you're right. Thanks. Applied. Bruno
Re: gethostname, socket need startup code
Good. No one asks worse than 1.1 so the patch is okay. Paolo
Re: gethostname inadvertently replaced on W32
Martin Lambers mar...@marlam.de writes: On Tue, 28. Jul 2009, 23:18:09 +0200, Simon Josefsson wrote: OTOH, I haven't tested whether my old patch still works. Martin, have you? It applied cleanly (except for the ChangeLog entry) and I tested it on Debian lenny and with a MinGW cross compiler. This time I built from scratch, to avoid my earlier errors. The resulting W32 binary was also tested on Windows XP. So yes, it still works. Great. Bruno, any objections to pushing the old patch? /Simon
gethostname inadvertently replaced on W32
Hello everybody, W32 has gethostname in ws2_32.dll. This is not detected by gethostname.m4, and gethostname is inadvertently replaced by a function that always returns an empty string. The following patch fixes this for me. It works on Debian and with a MinGW cross compiler. I tried to change gethostname.m4 to do similar checks that e.g. hostent.m4 does, but I'm not sure if I got it right, since M4 and autoconf macros tend to confuse me. Regards, Martin diff --git a/m4/gethostname.m4 b/m4/gethostname.m4 index 6b6fca9..572a140 100644 --- a/m4/gethostname.m4 +++ b/m4/gethostname.m4 @@ -1,4 +1,4 @@ -# gethostname.m4 serial 5 +# gethostname.m4 serial 6 dnl Copyright (C) 2002, 2008, 2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -6,11 +6,31 @@ dnl with or without modifications, as long as this notice is preserved. AC_DEFUN([gl_FUNC_GETHOSTNAME], [ + dnl On native Windows, gethostname is in ws2_32.dll AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) gl_PREREQ_SYS_H_WINSOCK2 - AC_REPLACE_FUNCS([gethostname]) - if test $ac_cv_func_gethostname = no; then + GETHOSTNAME_LIB= + AC_CHECK_FUNCS([gethostname], , [ +AC_CACHE_CHECK([for gethostname in winsock2.h and -lws2_32], + [gl_cv_w32_gethostname], + [gl_cv_w32_gethostname=no + gl_save_LIBS=$LIBS + LIBS=$LIBS -lws2_32 + AC_TRY_LINK([ +#ifdef HAVE_WINSOCK2_H +#include winsock2.h +#endif +#include stddef.h +], [gethostname(NULL, 0);], [gl_cv_w32_gethostname=yes]) + LIBS=$gl_save_LIBS + ]) +if test $gl_cv_w32_gethostname = yes; then + GETHOSTNAME_LIB=-lws2_32 +fi + ]) + if test -z $GETHOSTNAME_LIB -a $ac_cv_func_gethostname = no; then HAVE_GETHOSTNAME=0 +AC_LIBOBJ([gethostname]) gl_PREREQ_GETHOSTNAME fi ]) diff --git a/modules/gethostname b/modules/gethostname index 796dc6f..a9a464c 100644 --- a/modules/gethostname +++ b/modules/gethostname @@ -18,6 +18,9 @@ Makefile.am: Include: unistd.h +Link: +$(GETHOSTNAME_LIB) + License: LGPLv2+
Re: gethostname inadvertently replaced on W32
On Tue, Jul 28, 2009 at 10:08:34AM +0200, Martin Lambers wrote: The following patch fixes this for me. Sorry, this is not true (I forgot to 'make clean' and did not see this earlier). The unistd.h header from gnulib defines gethostname to rpl_gethostname, but no such function is available, and this leads to a linker error. I do not know how to fix these unistd.h related things; for now I'm just using '--avoid gethostname'. Martin
Re: gethostname inadvertently replaced on W32
Hello again, I just noticed that this problem was already found and fixed by Simon in March: http://www.mail-archive.com/bug-gnulib@gnu.org/msg13671.html There was some discussion about the right approach, but no decision was made. Bruno, Simon, would you please reconsider applying the patch? Without it, the gethostname module is broken on W32. Regards, Martin
Re: gethostname inadvertently replaced on W32
Martin Lambers mar...@marlam.de writes: Hello again, I just noticed that this problem was already found and fixed by Simon in March: http://www.mail-archive.com/bug-gnulib@gnu.org/msg13671.html There was some discussion about the right approach, but no decision was made. Bruno, Simon, would you please reconsider applying the patch? Without it, the gethostname module is broken on W32. Re-reading that thread, I think my patch is a reasonable way forward. Avoiding linking to -lws2_32 is nice, but using GetComputerNameEx has some issues (see my third e-mail in that thread) and ultimately it doesn't seem worth it. I propose we push my initial patch, and if Bruno feels strongly about providing a gethostname function that works better under Windows, without -lws2_32, that can be provided in a separate module (e.g., gethostname-lean). Objections? OTOH, I haven't tested whether my old patch still works. Martin, have you? /Simon
Re: gethostname inadvertently replaced on W32
On Tue, 28. Jul 2009, 23:18:09 +0200, Simon Josefsson wrote: OTOH, I haven't tested whether my old patch still works. Martin, have you? It applied cleanly (except for the ChangeLog entry) and I tested it on Debian lenny and with a MinGW cross compiler. This time I built from scratch, to avoid my earlier errors. The resulting W32 binary was also tested on Windows XP. So yes, it still works. Martin
Re: gethostname on Windows
Bruno Haible br...@clisp.org writes: Simon Josefsson wrote: The current gethostname module will return an empty string on mingw: strcpy (name, ); /* Hardcode your system name if you want. */ This is sub-optimal since Windows has a gethostname function in -lws2_32. The following patch should make gethostname return proper values. Tested on x86 debian lenny and mingw cross-compile. Any objections to installing it? Well, I don't much like the dependency on an external library for the modules 'gethostname' and 'uname' (later, when we get an 'uname' module). I did some experiments with the GetComputerNameEx function [1], which is present in kernel32.dll. The parameters ComputerNameDnsHostname and ComputerNamePhysicalDnsHostname returned the same value as gethostname() for me, except that gethostname() returns a lowercased result (which can be simulated in 3 lines of code). OTOH, the gethostname() in -lws2_32 is a Unixy API, and it seems idiotic want to use a Windozy API instead. I'm undecided. Avoiding -lws2_32 is good, but if the application links to -lws2_32 anyway, there will be no saving. Maybe the gethostname module could detect this? I.e., if the gnulib module sys_sockets_h is also used, so the application is likely to link to ws2_32 anyway, it can use -lws2_32 otherwise it will fall back on GetComputerNameEx. I'm not sure how to implement a M4 test like that though? Anyway, that solution isn't very reliable: you may want to use gethostname in an application that doesn't use sockets otherwise. Maybe two different gnulib modules then? gethostname uses -lws2_32 and gethostname-lean uses GetComputerNameEx? Some additional questions: 1) How do I use it? This doesn't seem to work: j...@mocca:~$ cat foo.c #define WINVER 0x0500 #include windows.h #include stdio.h int main () { char out[1024]; DWORD size = 1024; BOOL t = GetComputerNameEx(ComputerNameDnsHostname, out, size); printf(hi %d: %s, t, out); } j...@mocca:~$ i586-mingw32msvc-gcc -o foo.exe foo.c /tmp/ccDXn2yi.o:foo.c:(.text+0x37): undefined reference to `_GetComputerNameEx' collect2: ld returned 1 exit status j...@mocca:~$ 2) What is the maximum string size that GetComputerNameEx can return? For the gethostname, the max size is documented. 3) Is the GetComputerNameEx semantics right? It seems the gethostname result will be different in some configurations? I'm not sure how important that is, but for gss/shishi the use of gethostname is to derive a Kerberos V5 principal name so there could be security implications if the value is not right. /Simon
Re: gethostname on Windows
I managed to rewrite the code in GSS to not need the gethostname function at all, so I don't care strongly about the patch to make gethostname work under mingw anymore. My initial patch could be used, but Bruno's concern about linking to -lws2_32 may be a reason against. /Simon
Re: gethostname on Windows
Simon Josefsson wrote: The current gethostname module will return an empty string on mingw: strcpy (name, ); /* Hardcode your system name if you want. */ This is sub-optimal since Windows has a gethostname function in -lws2_32. The following patch should make gethostname return proper values. Tested on x86 debian lenny and mingw cross-compile. Any objections to installing it? Well, I don't much like the dependency on an external library for the modules 'gethostname' and 'uname' (later, when we get an 'uname' module). I did some experiments with the GetComputerNameEx function [1], which is present in kernel32.dll. The parameters ComputerNameDnsHostname and ComputerNamePhysicalDnsHostname returned the same value as gethostname() for me, except that gethostname() returns a lowercased result (which can be simulated in 3 lines of code). OTOH, the gethostname() in -lws2_32 is a Unixy API, and it seems idiotic want to use a Windozy API instead. I'm undecided. Bruno [1] http://msdn.microsoft.com/en-us/library/ms724301(VS.85).aspx
gethostname on Windows
The current gethostname module will return an empty string on mingw: strcpy (name, );/* Hardcode your system name if you want. */ This is sub-optimal since Windows has a gethostname function in -lws2_32. The following patch should make gethostname return proper values. Tested on x86 debian lenny and mingw cross-compile. Any objections to installing it? Suggestions on improvements welcome. /Simon From 75863be5aa98f25cc29fd491235fe7a8b7356a96 Mon Sep 17 00:00:00 2001 From: Simon Josefsson si...@josefsson.org Date: Tue, 31 Mar 2009 19:32:13 +0200 Subject: [PATCH] gethostname: Make it return correct values under Windows. --- ChangeLog |8 lib/gethostname.c | 30 -- m4/gethostname.m4 | 35 +++ modules/gethostname |7 ++- modules/gethostname-tests |1 + 5 files changed, 74 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3bf424d..a862e03 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2009-03-31 Simon Josefsson si...@josefsson.org + * lib/gethostname.c: Add Windows wrapper. + * m4/gethostname.m4: Look for gethostname in -lws2_32. + * modules/gethostname: Depend on sys_socket errno, for also + added lib/w32sock.h. Add GETHOSTNAME_LIB link directive. + * modules/gethostname-tests: Link to @gethostname_...@. + +2009-03-31 Simon Josefsson si...@josefsson.org + * modules/u64-tests: New file. * tests/test-u64.c: New file. diff --git a/lib/gethostname.c b/lib/gethostname.c index acff351..782c402 100644 --- a/lib/gethostname.c +++ b/lib/gethostname.c @@ -1,6 +1,6 @@ /* gethostname emulation for SysV and POSIX.1. - Copyright (C) 1992, 2003, 2006, 2008 Free Software Foundation, Inc. + Copyright (C) 1992, 2003, 2006, 2008, 2009 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 @@ -15,10 +15,13 @@ You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. */ -/* David MacKenzie d...@gnu.ai.mit.edu */ +/* David MacKenzie d...@gnu.ai.mit.edu + Windows port by Simon Josefsson si...@josefsson.org */ #include config.h +#if !((defined _WIN32 || defined __WIN32__) !defined __CYGWIN__) + /* Specification. */ #include unistd.h @@ -54,3 +57,26 @@ gethostname (char *name, size_t len) #endif return 0; } + +#else + +#define WIN32_LEAN_AND_MEAN +/* Get winsock2.h. */ +#include unistd.h + +/* Get set_winsock_errno. */ +#include w32sock.h + +#undef gethostname + +int +rpl_gethostname (char *name, size_t namelen) +{ + int r = gethostname (name, (int) namelen); + if (r 0) +set_winsock_errno (); + + return r; +} + +#endif diff --git a/m4/gethostname.m4 b/m4/gethostname.m4 index 6b6fca9..620e023 100644 --- a/m4/gethostname.m4 +++ b/m4/gethostname.m4 @@ -1,4 +1,4 @@ -# gethostname.m4 serial 5 +# gethostname.m4 serial 6 dnl Copyright (C) 2002, 2008, 2009 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -8,8 +8,33 @@ AC_DEFUN([gl_FUNC_GETHOSTNAME], [ AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) gl_PREREQ_SYS_H_WINSOCK2 - AC_REPLACE_FUNCS([gethostname]) - if test $ac_cv_func_gethostname = no; then + + dnl Where is gethostname() defined? + dnl - On native Windows, it is in ws2_32.dll. + dnl - Otherwise is is in libc. + GETHOSTNAME_LIB= + AC_CHECK_FUNCS([gethostname], , [ +AC_CACHE_CHECK([for gethostname in winsock2.h and -lws2_32], + [gl_cv_w32_gethostname], + [gl_cv_w32_gethostname=no + gl_save_LIBS=$LIBS + LIBS=$LIBS -lws2_32 + AC_TRY_LINK([ +#ifdef HAVE_WINSOCK2_H +#include winsock2.h +#endif +#include stddef.h +], [gethostname(NULL, 0);], [gl_cv_w32_gethostname=yes]) + LIBS=$gl_save_LIBS + ]) +if test $gl_cv_w32_gethostname = yes; then + GETHOSTNAME_LIB=-lws2_32 +fi + ]) + AC_SUBST([GETHOSTNAME_LIB]) + + if test $ac_cv_func_gethostname = no; then +AC_LIBOBJ([gethostname]) HAVE_GETHOSTNAME=0 gl_PREREQ_GETHOSTNAME fi @@ -17,5 +42,7 @@ AC_DEFUN([gl_FUNC_GETHOSTNAME], # Prerequisites of lib/gethostname.c. AC_DEFUN([gl_PREREQ_GETHOSTNAME], [ - AC_CHECK_FUNCS([uname]) + if test $gl_cv_w32_gethostname != yes; then +AC_CHECK_FUNCS([uname]) + fi ]) diff --git a/modules/gethostname b/modules/gethostname index 796dc6f..e21afe6 100644 --- a/modules/gethostname +++ b/modules/gethostname @@ -4,10 +4,12 @@ gethostname() function: Return machine's hostname. Files: lib/gethostname.c m4/gethostname.m4 -m4/sys_socket_h.m4 +lib/w32sock.h Depends-on: unistd +sys_socket +errno configure.ac: gl_FUNC_GETHOSTNAME @@ -18,6 +20,9 @@ Makefile.am: Include: unistd.h +Link
gethostname: LGPL - LGPLv2+
I'd like to use the gethostname module in libvirt (LGPLv2+), for use when building for mingw. Considering the size of the module (trivial, 10-line gethostname function and minimal gethostname.m4), would anyone object to relaxing its license from LGPL to LGPLv2+? This is mainly to avoid gnulib's link-time warning: ...: undefined reference to [EMAIL PROTECTED]' In this particular case, gethostname is essentially a no-op, and always returns the empty string, since mingw has no uname function, and in that case, gethostname's body reverts to this: strcpy (name, );/* Hardcode your system name if you want. */
Re: gethostname: LGPL - LGPLv2+
Jim Meyering [EMAIL PROTECTED] writes: I'd like to use the gethostname module in libvirt (LGPLv2+), for use when building for mingw. Considering the size of the module (trivial, 10-line gethostname function and minimal gethostname.m4), would anyone object to relaxing its license from LGPL to LGPLv2+? This is mainly to avoid gnulib's link-time warning: ...: undefined reference to [EMAIL PROTECTED]' In this particular case, gethostname is essentially a no-op, and always returns the empty string, since mingw has no uname function, and in that case, gethostname's body reverts to this: strcpy (name, );/* Hardcode your system name if you want. */ FWIW, I think it would be useful to have it as LGPLv2+ as well. /Simon
Re: gethostname: LGPL - LGPLv2+
Jim Meyering [EMAIL PROTECTED] wrote: I'd like to use the gethostname module in libvirt (LGPLv2+), for use when building for mingw. Considering the size of the module (trivial, 10-line gethostname function and minimal gethostname.m4), would anyone object to relaxing its license from LGPL to LGPLv2+? This is mainly to avoid gnulib's link-time warning: ...: undefined reference to [EMAIL PROTECTED]' In this particular case, gethostname is essentially a no-op, and always returns the empty string, since mingw has no uname function, and in that case, gethostname's body reverts to this: strcpy (name, );/* Hardcode your system name if you want. */ On this basis, I've gone ahead and made it LGPLv2+.
Re: gethostname fix for mingw (was: Re: Problem with gethostbyname)
Ben Pfaff wrote: Fix gethostname on mingw. * lib/unistd.in.h: Bypass bad winsock2 prototype for gethostname. Prevent calling gethostname on mingw without using the gethostname module. * gethostname.m4: Invoke gl_PREREQ_SYS_H_WINSOCK2 if it is available. * doc/posix-functions/gethostname.texi: Update. Nearly good. I improved two things: - #include unistd.h would make the non-POSIX functions accept(), ..., socket(), select() visible. - When the module 'gethostname' is requested and the module 'sys_socket' is not, then the use of #include unistd.h #include winsock2.h would lead to a clash of different prototypes of gethostname(). In other words, every time you have an m4_ifdef() of a macro defined in another gnulib module, there are chances that it's a bug. and applied this: 2008-10-25 Ben Pfaff [EMAIL PROTECTED] Bruno Haible [EMAIL PROTECTED] * lib/unistd.in.h: Include winsock2.h. (socket, connect,accept, bind, getpeername, getsockname, getsockopt, listen, recv, send, recvfrom, sendto, setsockopt, shutdown, select): Provide dummy declarations. (gethostname): Override. * lib/sys_socket.in.h (gethostname): Provide dummy declaration. * m4/gethostname.m4 (gl_FUNC_GETHOSTNAME): Invoke gl_PREREQ_SYS_H_WINSOCK2. * modules/gethostname (Files): Add m4/sys_socket_h.m4. * doc/posix-functions/gethostname.texi: More details. --- lib/sys_socket.in.h.orig2008-10-25 23:00:21.0 +0200 +++ lib/sys_socket.in.h 2008-10-25 22:57:53.0 +0200 @@ -145,6 +145,11 @@ # define close close_used_without_including_unistd_h # endif +# if @HAVE_WINSOCK2_H@ !defined _GL_UNISTD_H +# undef gethostname +# define gethostname gethostname_used_without_including_unistd_h +# endif + # if @GNULIB_SOCKET@ # if @HAVE_WINSOCK2_H@ # undef socket --- lib/unistd.in.h.orig2008-10-25 23:00:21.0 +0200 +++ lib/unistd.in.h 2008-10-25 22:57:53.0 +0200 @@ -42,6 +42,47 @@ # include sys/types.h #endif +#if @GNULIB_GETHOSTNAME@ +/* Get all possible declarations of gethostname(). */ +# if @UNISTD_H_HAVE_WINSOCK2_H@ +# include winsock2.h +# if !defined _GL_SYS_SOCKET_H +# undef socket +# define socket socket_used_without_including_sys_socket_h +# undef connect +# define connect connect_used_without_including_sys_socket_h +# undef accept +# define accept accept_used_without_including_sys_socket_h +# undef bind +# define bindbind_used_without_including_sys_socket_h +# undef getpeername +# define getpeername getpeername_used_without_including_sys_socket_h +# undef getsockname +# define getsockname getsockname_used_without_including_sys_socket_h +# undef getsockopt +# define getsockopt getsockopt_used_without_including_sys_socket_h +# undef listen +# define listen listen_used_without_including_sys_socket_h +# undef recv +# define recvrecv_used_without_including_sys_socket_h +# undef send +# define sendsend_used_without_including_sys_socket_h +# undef recvfrom +# define recvfromrecvfrom_used_without_including_sys_socket_h +# undef sendto +# define sendto sendto_used_without_including_sys_socket_h +# undef setsockopt +# define setsockopt setsockopt_used_without_including_sys_socket_h +# undef shutdown +# define shutdownshutdown_used_without_including_sys_socket_h +# endif +# if !defined _GL_SYS_SELECT_H +# undef select +# define select select_used_without_including_sys_select_h +# endif +# endif +#endif + /* The definition of GL_LINK_WARNING is copied here. */ @@ -284,9 +325,16 @@ Null terminate it if the name is shorter than LEN. If the host name is longer than LEN, set errno = EINVAL and return -1. Return 0 if successful, otherwise set errno and return -1. */ -# if [EMAIL PROTECTED]@ +# if @UNISTD_H_HAVE_WINSOCK2_H@ +# undef gethostname +# define gethostname rpl_gethostname +# endif +# if @UNISTD_H_HAVE_WINSOCK2_H@ || [EMAIL PROTECTED]@ extern int gethostname(char *name, size_t len); # endif +#elif @UNISTD_H_HAVE_WINSOCK2_H@ +# undef gethostname +# define gethostname gethostname_used_without_requesting_gnulib_module_gethostname #elif defined GNULIB_POSIXCHECK # undef gethostname # define gethostname(n,l) \ --- m4/gethostname.m4.orig 2008-10-25 23:00:21.0 +0200 +++ m4/gethostname.m4 2008-10-25 22:57:53.0 +0200 @@ -1,4 +1,4 @@ -# gethostname.m4 serial 3 +# gethostname.m4 serial 4 dnl Copyright (C) 2002, 2008 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,6 +7,7 @@ AC_DEFUN([gl_FUNC_GETHOSTNAME
Re: gethostname fix for mingw
Ben Pfaff [EMAIL PROTECTED] writes: Michel reported on pspp-dev that PSPP fails to compile on mingw due to the following error: In file included from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/windows.h:98, from lseek.c:26: C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/winsock2.h:635: error: conflicting types for 'gethostname' ./unistd.h:313: error: previous declaration of 'gethostname' was here I've seen this as well recently. I can see what is going wrong: - The gethostname module fails to find gethostname() because it is in the ws2_32 library, which it doesn't try to link against. Thus, it tries to replace it. - Thus, it adds a prototype for gethostname() to the replacement unistd.h. This prototype has a length parameter of type size_t (per POSIX). - The lseek module needs unistd.h and windows.h. The latter in turn includes winsock2.h, which contains the Windows prototype for gethostname(), which has an length parameter of type int (who knows why). I think that the following change will fix the problem. Does it look like the right approach? If so, I will generate a new snapshot for Michel to test before I commit it to gnulib. If it works, I'm for it. /Simon
Re: gethostname on mingw, LIBS
Hi Simon, Note that this does not resolve the issue with mingw. On mingw, winsock2.h declares gethostname() also - but it requires linking with -lws2_32, right? Yes. Win32 also has GetComputerName that does not need -lws2_32. It would be easier to use, but which one is right semantically I don't know. It's easy to google for site:msdn.microsoft.com gethostname and site:msdn.microsoft.com GetComputerName. Just did that, and found that - GetComputerName is not the right thing, but GetComputerNameEx is. - While gethostname requires to link with -lws2_32, GetComputerNameEx does not. 2. Should this be done through immediate linking, or dynamic linking? getaddrinfo.m4 adds -lws2_32 to LIBS automatically now. Please don't do that. A project builds several applications in general. In gettext, just because I have one program which needs select() and another program which does network requests, I don't want all programs, from 'msgfmt' to 'xgettext', to link with unneeded libraries. It may be cleaner to define a MINGW_LIBS variable instead, and ask developers to add $(MINGW_LIBS) to automake LIBADD variables for executables that need it. sockets.m4 has been fixed in this way already. MINGW_LIBS is not specific enough. How could a developer know on which programs he needs it or not. IMO all library requirements must be attached to particular modules with a particular purpose. Like $(LIBSOCKET), $(LIBICONV), $(LIBTHREAD). Possibly the -lws2_32/LIBSOCKET/MINGW_LIBS handling should be put in a separate module? Then getaddrinfo and sockets can depend on it, rather than duplicating it. Yes, you're welcome to do this. But call it LIBSOCKET, please. gethostname.c could also use GetModuleHandle, similar to how getaddrinfo.c works, to avoid the -lws2_32 requirement. If the application would not otherwise link to -lws2_32 this approach may be nicer. But if applications are linked to -lws2_32 because of other gnulib modules anyway, it doesn't make much sense. Yes, when you need the host name, usually it's in the context of network accesses. The GetModuleHandle approach, together with a fallback replacement function, results in better portability generally. Some Windows releases does not have getaddrinfo, and if the application is linked against -lws2_32 that has it, and the binary is transferred to an old system, it won't work. For my part, I drop portability requirements to Windows98/ME, and assume Windows 2000 at least. Reason: The documentation on msdn.microsoft.com does not even mention any more which functions were defined in Windows98/ME or not. Maybe the same approach cannot be used for gethostname: ... I'm not sure. It seems pretty clear that MINGW_LIBS and GetModuleHandle are two approaches that can solve this problem. And with GetComputerNameEx, there is no link requirement at all. I propose this, together with some WINVER tricks to make it work. --- lib/gethostname.c.orig 2008-10-23 13:45:02.0 +0200 +++ lib/gethostname.c 2008-10-23 13:44:03.0 +0200 @@ -22,8 +22,10 @@ /* Specification. */ #include unistd.h -#ifdef HAVE_UNAME +#if HAVE_UNAME # include sys/utsname.h +#elif (defined _WIN32 || defined __WIN32__) ! defined __CYGWIN__ +# include windows.h #endif #include string.h @@ -37,7 +39,7 @@ int gethostname (char *name, size_t len) { -#ifdef HAVE_UNAME +#if HAVE_UNAME struct utsname uts; if (uname (uts) == -1) @@ -49,6 +51,14 @@ len = sizeof (uts.nodename); } strncpy (name, uts.nodename, len); +#elif (defined _WIN32 || defined __WIN32__) ! defined __CYGWIN__ + /* GetComputerName is not the right thing. gethostname from winsock2.h + would be right, but requires linking with -lws2_32. So we use + GetComputerNameEx. */ + DWORD size = (len = (DWORD)~0 ? len : (DWORD)~0); + + if (!GetComputerNameEx (ComputerNameDnsHostname, name, size)) +return -1; #else strcpy (name, ); /* Hardcode your system name if you want. */ #endif
Re: gethostname fix for mingw (was: Re: Problem with gethostbyname)
Ben Pfaff wrote: --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -281,8 +281,17 @@ extern int getdtablesize (void); If the host name is longer than LEN, set errno = EINVAL and return -1. Return 0 if successful, otherwise set errno and return -1. */ # if [EMAIL PROTECTED]@ +# if HAVE_WINSOCK2_H +# /* Bypass bad prototype for gethostname(). */ +# include winsock2.h +# undef gethostname +# define gethostname rpl_gethostname +# endif extern int gethostname(char *name, size_t len); # endif +#elif @UNISTD_H_HAVE_WINSOCK2_H@ +# undef gethostname +# define gethostname gethostname_used_without_requesting_gnulib_module_gethostname #elif defined GNULIB_POSIXCHECK # undef gethostname # define gethostname(n,l) \ Yes, this looks all right. Only use @UNISTD_H_HAVE_WINSOCK2_H@ instead of HAVE_WINSOCK2_H in line 284 above. Thanks! Bruno
Re: gethostname on mingw, LIBS
Bruno Haible [EMAIL PROTECTED] writes: Maybe the same approach cannot be used for gethostname: ... I'm not sure. It seems pretty clear that MINGW_LIBS and GetModuleHandle are two approaches that can solve this problem. And with GetComputerNameEx, there is no link requirement at all. I propose this, together with some WINVER tricks to make it work. Looks fine, please push it, although I don't see any WINVER tricks? /Simon --- lib/gethostname.c.orig2008-10-23 13:45:02.0 +0200 +++ lib/gethostname.c 2008-10-23 13:44:03.0 +0200 @@ -22,8 +22,10 @@ /* Specification. */ #include unistd.h -#ifdef HAVE_UNAME +#if HAVE_UNAME # include sys/utsname.h +#elif (defined _WIN32 || defined __WIN32__) ! defined __CYGWIN__ +# include windows.h #endif #include string.h @@ -37,7 +39,7 @@ int gethostname (char *name, size_t len) { -#ifdef HAVE_UNAME +#if HAVE_UNAME struct utsname uts; if (uname (uts) == -1) @@ -49,6 +51,14 @@ len = sizeof (uts.nodename); } strncpy (name, uts.nodename, len); +#elif (defined _WIN32 || defined __WIN32__) ! defined __CYGWIN__ + /* GetComputerName is not the right thing. gethostname from winsock2.h + would be right, but requires linking with -lws2_32. So we use + GetComputerNameEx. */ + DWORD size = (len = (DWORD)~0 ? len : (DWORD)~0); + + if (!GetComputerNameEx (ComputerNameDnsHostname, name, size)) +return -1; #else strcpy (name, ); /* Hardcode your system name if you want. */ #endif
Re: declare gethostname in unistd.h
Bruno, the patch causes build failures on mingw: ./unistd.h:313: error: conflicting types for 'gethostname' /usr/lib/gcc/i586-mingw32msvc/4.2.1-sjlj/../../../../i586-mingw32msvc/include/winsock2.h:635: error: previous declaration of 'gethostname' was here The problem is that the native gethostname Win API uses 'int' instead of 'size_t' as the prototype. Should we replace the system gethostname with a wrapper? Or can we use a simple #define such as #define gethostname(name, len) gethostname (name, (int) len) when using the winsock2.h gethostname definition? /Simon
gethostname fix for mingw (was: Re: Problem with gethostbyname)
Michel reported on pspp-dev that PSPP fails to compile on mingw due to the following error: In file included from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/windows.h:98, from lseek.c:26: C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/winsock2.h:635: error: conflicting types for 'gethostname' ./unistd.h:313: error: previous declaration of 'gethostname' was here I can see what is going wrong: - The gethostname module fails to find gethostname() because it is in the ws2_32 library, which it doesn't try to link against. Thus, it tries to replace it. - Thus, it adds a prototype for gethostname() to the replacement unistd.h. This prototype has a length parameter of type size_t (per POSIX). - The lseek module needs unistd.h and windows.h. The latter in turn includes winsock2.h, which contains the Windows prototype for gethostname(), which has an length parameter of type int (who knows why). I think that the following change will fix the problem. Does it look like the right approach? If so, I will generate a new snapshot for Michel to test before I commit it to gnulib. Thanks, Ben. commit ed8093c8eedcf44193b29155a9bbf49f83e0d3e7 Author: Ben Pfaff [EMAIL PROTECTED] Date: Wed Oct 22 21:54:23 2008 -0700 Fix gethostname on mingw. * lib/unistd.in.h: Bypass bad winsock2 prototype for gethostname. Prevent calling gethostname on mingw without using the gethostname module. * gethostname.m4: Invoke gl_PREREQ_SYS_H_WINSOCK2 if it is available. * doc/posix-functions/gethostname.texi: Update. diff --git a/ChangeLog b/ChangeLog index b6c2893..e47a922 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2008-10-22 Ben Pfaff [EMAIL PROTECTED] + + * lib/unistd.in.h: Bypass bad winsock2 prototype for gethostname. + Prevent calling gethostname on mingw without using the gethostname + module. + * gethostname.m4: Invoke gl_PREREQ_SYS_H_WINSOCK2 if it is + available. + * doc/posix-functions/gethostname.texi: Update. + 2008-10-22 Simon Josefsson [EMAIL PROTECTED] * lib/sys_socket.in.h (FD_ISSET): Fix warnings under mingw. diff --git a/doc/posix-functions/gethostname.texi b/doc/posix-functions/gethostname.texi index 9d32a8b..1ff45fd 100644 --- a/doc/posix-functions/gethostname.texi +++ b/doc/posix-functions/gethostname.texi @@ -9,8 +9,8 @@ Gnulib module: gethostname Portability problems fixed by Gnulib: @itemize @item -This function is missing on some platforms: -mingw. +On mingw, this function has a prototype that differs from that +specified by POSIX, and it is defined only in the ws2_32 library. @end itemize Portability problems not fixed by Gnulib: diff --git a/lib/unistd.in.h b/lib/unistd.in.h index bfa3d49..7a5db76 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -281,8 +281,17 @@ extern int getdtablesize (void); If the host name is longer than LEN, set errno = EINVAL and return -1. Return 0 if successful, otherwise set errno and return -1. */ # if [EMAIL PROTECTED]@ +# if HAVE_WINSOCK2_H +# /* Bypass bad prototype for gethostname(). */ +# include winsock2.h +# undef gethostname +# define gethostname rpl_gethostname +# endif extern int gethostname(char *name, size_t len); # endif +#elif @UNISTD_H_HAVE_WINSOCK2_H@ +# undef gethostname +# define gethostname gethostname_used_without_requesting_gnulib_module_gethostname #elif defined GNULIB_POSIXCHECK # undef gethostname # define gethostname(n,l) \ diff --git a/m4/gethostname.m4 b/m4/gethostname.m4 index b8c4e2a..7095ac3 100644 --- a/m4/gethostname.m4 +++ b/m4/gethostname.m4 @@ -6,6 +6,7 @@ dnl with or without modifications, as long as this notice is preserved. AC_DEFUN([gl_FUNC_GETHOSTNAME], [ + m4_ifdef([gl_PREREQ_SYS_H_WINSOCK2], [gl_PREREQ_SYS_H_WINSOCK2]) AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) AC_REPLACE_FUNCS(gethostname) if test $ac_cv_func_gethostname = no; then -- Welcome to the Slippery Slope. Here is your handbasket. Say, can you work 70 hours this week? --Ron Mansolino
Re: declare gethostname in unistd.h
Bruno Haible [EMAIL PROTECTED] writes: Hi Simon, Here's a proposed patch so that on platforms where gethostname() is missing and substituted by gnulib, it is declared in unistd.h. OK to commit? Hi. Yes, please do. Note that this does not resolve the issue with mingw. On mingw, winsock2.h declares gethostname() also - but it requires linking with -lws2_32, right? Yes. Win32 also has GetComputerName that does not need -lws2_32. It would be easier to use, but which one is right semantically I don't know. How should the gnulib module handle this? 1. Should gnulib's replacement call the winsock2 gethostname()? I think so. The Win32 gethostname function takes 'char*,int' so it needs to be wrapped by gnulib, because the POSIX signature is 'char*,size_t'. 2. Should this be done through immediate linking, or dynamic linking? getaddrinfo.m4 adds -lws2_32 to LIBS automatically now. A tangent: It may be cleaner to define a MINGW_LIBS variable instead, and ask developers to add $(MINGW_LIBS) to automake LIBADD variables for executables that need it. sockets.m4 has been fixed in this way already. Possibly the -lws2_32/LIBSOCKET/MINGW_LIBS handling should be put in a separate module? Then getaddrinfo and sockets can depend on it, rather than duplicating it. gethostname.c could also use GetModuleHandle, similar to how getaddrinfo.c works, to avoid the -lws2_32 requirement. If the application would not otherwise link to -lws2_32 this approach may be nicer. But if applications are linked to -lws2_32 because of other gnulib modules anyway, it doesn't make much sense. The GetModuleHandle approach, together with a fallback replacement function, results in better portability generally. Some Windows releases does not have getaddrinfo, and if the application is linked against -lws2_32 that has it, and the binary is transferred to an old system, it won't work. I suspect all relevant Windows releases has gethostname though. Actually, the getaddrinfo module supports both variants: if the applications sets an appropriate WINVER variable, getaddrinfo will detect as being in -lws2_32 by configure, and the replacement code will not be used. By default, it will not be detected as being in -lws2_32, and the replacement will be used (which tries GetModuleHandle first). Maybe the same approach cannot be used for gethostname: the signatures are different, so if the application passes a size_t and the function assumes int, it may break on platforms where int is 32 bit and size_t is 64 bit (if there are any such Windows platforms?). A wrapper function is needed to convert the data types. Or maybe a #define with a cast? I'm not sure. It seems pretty clear that MINGW_LIBS and GetModuleHandle are two approaches that can solve this problem. The GetModuleHandle solution is more complex, but I'm leaning slightly towards it because it should in theory result in better portability. OTOH, complexity can lead to less portability in practice. /Simon
declare gethostname in unistd.h
Hi Simon, Here's a proposed patch so that on platforms where gethostname() is missing and substituted by gnulib, it is declared in unistd.h. OK to commit? Note that this does not resolve the issue with mingw. On mingw, winsock2.h declares gethostname() also - but it requires linking with -lws2_32, right? How should the gnulib module handle this? 1. Should gnulib's replacement call the winsock2 gethostname()? 2. Should this be done through immediate linking, or dynamic linking? Bruno 2008-10-19 Bruno Haible [EMAIL PROTECTED] Add gethostname() declaration to unistd.h. * lib/unistd.in.h (gethostname): New declaration. * lib/gethostname.c: Include unistd.h. * m4/gethostname.m4 (gl_FUNC_GETHOSTNAME): Require gl_UNISTD_H_DEFAULTS. Set HAVE_GETHOSTNAME. * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize GNULIB_GETHOSTNAME and HAVE_GETHOSTNAME. * modules/gethostname (Depends-on): Add unistd. (configure.ac): Invoke gl_UNISTD_MODULE_INDICATOR. (Include): Specify unistd.h. * modules/unistd (Makefile.am): Substitute GNULIB_GETHOSTNAME and HAVE_GETHOSTNAME. * tests/test-gethostname.c: Include unistd.h first. --- lib/unistd.in.h.orig2008-10-19 17:24:06.0 +0200 +++ lib/unistd.in.h 2008-10-19 17:15:04.0 +0200 @@ -272,6 +272,26 @@ #endif +#if @GNULIB_GETHOSTNAME@ +/* Return the standard host name of the machine. + WARNING! The host name may or may not be fully qualified. + + Put up to LEN bytes of the host name into NAME. + Null terminate it if the name is shorter than LEN. + If the host name is longer than LEN, set errno = EINVAL and return -1. + Return 0 if successful, otherwise set errno and return -1. */ +# if [EMAIL PROTECTED]@ +extern int gethostname(char *name, size_t len); +# endif +#elif defined GNULIB_POSIXCHECK +# undef gethostname +# define gethostname(n,l) \ +(GL_LINK_WARNING (gethostname is unportable - \ + use gnulib module gethostname for portability), \ + gethostname (n, l)) +#endif + + #if @GNULIB_GETLOGIN_R@ /* Copies the user's login name to NAME. The array pointed to by NAME has room for SIZE bytes. --- lib/gethostname.c.orig 2008-10-19 17:24:06.0 +0200 +++ lib/gethostname.c 2008-10-19 17:17:31.0 +0200 @@ -19,6 +19,9 @@ #include config.h +/* Specification. */ +#include unistd.h + #ifdef HAVE_UNAME # include sys/utsname.h #endif --- m4/gethostname.m4.orig 2008-10-19 17:24:06.0 +0200 +++ m4/gethostname.m4 2008-10-19 17:21:01.0 +0200 @@ -1,13 +1,15 @@ -# gethostname.m4 serial 2 -dnl Copyright (C) 2002 Free Software Foundation, Inc. +# gethostname.m4 serial 3 +dnl Copyright (C) 2002, 2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. AC_DEFUN([gl_FUNC_GETHOSTNAME], [ + AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) AC_REPLACE_FUNCS(gethostname) if test $ac_cv_func_gethostname = no; then +HAVE_GETHOSTNAME=0 gl_PREREQ_GETHOSTNAME fi ]) --- m4/unistd_h.m4.orig 2008-10-19 17:24:06.0 +0200 +++ m4/unistd_h.m4 2008-10-19 17:15:51.0 +0200 @@ -1,4 +1,4 @@ -# unistd_h.m4 serial 15 +# unistd_h.m4 serial 16 dnl Copyright (C) 2006-2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -43,6 +43,7 @@ GNULIB_GETCWD=0; AC_SUBST([GNULIB_GETCWD]) GNULIB_GETDOMAINNAME=0;AC_SUBST([GNULIB_GETDOMAINNAME]) GNULIB_GETDTABLESIZE=0;AC_SUBST([GNULIB_GETDTABLESIZE]) + GNULIB_GETHOSTNAME=0; AC_SUBST([GNULIB_GETHOSTNAME]) GNULIB_GETLOGIN_R=0; AC_SUBST([GNULIB_GETLOGIN_R]) GNULIB_GETPAGESIZE=0; AC_SUBST([GNULIB_GETPAGESIZE]) GNULIB_GETUSERSHELL=0; AC_SUBST([GNULIB_GETUSERSHELL]) @@ -59,6 +60,7 @@ HAVE_FTRUNCATE=1; AC_SUBST([HAVE_FTRUNCATE]) HAVE_GETDOMAINNAME=1; AC_SUBST([HAVE_GETDOMAINNAME]) HAVE_GETDTABLESIZE=1; AC_SUBST([HAVE_GETDTABLESIZE]) + HAVE_GETHOSTNAME=1; AC_SUBST([HAVE_GETHOSTNAME]) HAVE_GETPAGESIZE=1; AC_SUBST([HAVE_GETPAGESIZE]) HAVE_GETUSERSHELL=1;AC_SUBST([HAVE_GETUSERSHELL]) HAVE_READLINK=1;AC_SUBST([HAVE_READLINK]) --- modules/gethostname.orig2008-10-19 17:24:06.0 +0200 +++ modules/gethostname 2008-10-19 17:22:01.0 +0200 @@ -6,13 +6,16 @@ m4/gethostname.m4 Depends-on: +unistd configure.ac: gl_FUNC_GETHOSTNAME +gl_UNISTD_MODULE_INDICATOR([gethostname]) Makefile.am: Include: +unistd.h License: LGPL --- modules/unistd.orig 2008-10-19 17:24:06.0 +0200 +++ modules/unistd 2008-10-19 17:16:27.0 +0200 @@ -35,6 +35,7 @@ -e 's|@''GNULIB_GETCWD''@|$(GNULIB_GETCWD)|g' \ -e 's
Re: gethostname
Hello Ben, I missed seeing that in the show-portability output, even though it was right there. I think that part of the reason was that the output is grouped by library. When I adjust the sorting to be by OS, it is easier for me to see that ... ... --- tmp/various-symlists/show-portability 2008-01-13 12:51:54.0 -0800 +++ various-symlists/show-portability 2008-08-12 19:46:40.0 -0700 @@ -79,7 +79,7 @@ -e 's,^librootbeos$,libc beos,' grep '^'$symbol'$' $d/* /dev/null || echo $os 13 done \ -| sed -e 's,^\(\) *,\1,' | sort -k1,80 +| sed -e 's,^\(\) *,\1,' | sort -b -k 2,2 -k 1,1 } 3 .missing missing=`cat .missing` rm -f .missing Makes sense. I applied your patch to my copy and will distribute it. Thanks. Bruno
Re: gethostname
Ben Pfaff [EMAIL PROTECTED] writes: libcsolaris-2.10 libnsl solaris-2.10 libnsl solaris-2.4 libcsolaris-2.5.1 libnsl solaris-2.5.1 ... -| sed -e 's,^\(\) *,\1,' | sort -k1,80 +| sed -e 's,^\(\) *,\1,' | sort -b -k 2,2 -k 1,1 Is there a sort option that results in the output being something like the below? libnsl solaris-2.4 libcsolaris-2.5.1 libnsl solaris-2.5.1 libcsolaris-2.10 libnsl solaris-2.10 I.e., normal alphabetical, but sort digits in numeral order. /Simon
Re: gethostname
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 According to Simon Josefsson on 8/13/2008 4:59 AM: | Is there a sort option that results in the output being something like | the below? | | libnsl solaris-2.4 | libcsolaris-2.5.1 | libnsl solaris-2.5.1 | libcsolaris-2.10 | libnsl solaris-2.10 | | I.e., normal alphabetical, but sort digits in numeral order. Sounds like you want version comparison (based on strverscmp). This has been proposed for GNU coreutils, but is not present yet as of 6.12. - -- Don't work too hard, make some time for fun as well! Eric Blake [EMAIL PROTECTED] -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.9 (Cygwin) Comment: Public key at home.comcast.net/~ericblake/eblake.gpg Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkiizLAACgkQ84KuGfSFAYDr1ACeJQpZyAzbP2hmTyIVINnzZnO4 GUUAoIv+MYqhWadUKtsZ6pk5zVXy586a =U/xG -END PGP SIGNATURE-
Re: gethostname
Sam Steingold asked: gnulib/lib/gethostname.c says: #ifdef HAVE_UNAME # include sys/utsname.h #endif 1. why aren't you testing for HAVE_SYS_UTSNAME_H instead? All systems that have uname() also have sys/utsname.h. We estimate the probability that a system with sys/utsname.h also has uname() to 99%. 2. are there really systems without uname sys/utsname.h, given that these are in posix? http://www.opengroup.org/onlinepubs/009695399/functions/uname.html Yes, some old systems did not have uname() and sys/utsname.h: especially SunOS 4. But these are not porting targets today any more. Simon Josefsson wrote: I think the right solution here should be to apply the patch below. If there are problems on any system caused by that, the solution to that problem should be to create a module for sys/utsname.h and uname. The gnulib docs (doc/posix-functions/{gethostname,uname}.texi) say that uname and gethostname are both missing on mingw systems. If your patch is applied in the current state, it causes a link error on mingw. Feel free to contribute an uname() substitute, however. With that in place, your patch can be applied without damage. The gethostname module shouldn't depend on such modules, they need to be added manually by maintainers who want compatibility with non-POSIX systems. mingw portability is requested quite often, therefore gnulib includes the dependencies needed for mingw portability. We omit dependencies only for extremely old systems (e.g. BSD 4.2 - missing memcpy()). Sam Steingold wrote: note that gethostname is just as posix as uname, http://www.opengroup.org/onlinepubs/009695399/functions/gethostname.html so the gethostname module is only needed for non-posix systems. Historically, uname came from SysV, and gethostname came from BSD. Bruno
Re: gethostname
Sam Steingold [EMAIL PROTECTED] writes: 1. why aren't you testing for HAVE_SYS_UTSNAME_H instead? 2. are there really systems without uname sys/utsname.h, given that these are in posix? http://www.opengroup.org/onlinepubs/009695399/functions/uname.html I think the right solution here should be to apply the patch below. If there are problems on any system caused by that, the solution to that problem should be to create a module for sys/utsname.h and uname. The gethostname module shouldn't depend on such modules, they need to be added manually by maintainers who want compatibility with non-POSIX systems. Thoughts? note that gethostname is just as posix as uname, http://www.opengroup.org/onlinepubs/009695399/functions/gethostname.html so the gethostname module is only needed for non-posix systems. Indeed, so I am less sure my patch is the right thing. Could you explain why the current code causes problems for you? 2. are there really systems without uname sys/utsname.h, given that these are in posix? http://www.opengroup.org/onlinepubs/009695399/functions/uname.html Yes, some old systems did not have uname() and sys/utsname.h: especially SunOS 4. But these are not porting targets today any more. Didn't SunOS 4 have gethostname? Simon Josefsson wrote: I think the right solution here should be to apply the patch below. If there are problems on any system caused by that, the solution to that problem should be to create a module for sys/utsname.h and uname. The gnulib docs (doc/posix-functions/{gethostname,uname}.texi) say that uname and gethostname are both missing on mingw systems. If your patch is applied in the current state, it causes a link error on mingw. Feel free to contribute an uname() substitute, however. With that in place, your patch can be applied without damage. I'm not sure the patch makes sense any more -- if the gethostname module is there only to make things work under mingw, my patch would break things and requires maintainers to manually also add the uname module to the mix. How could that dependency be explained? Having the gethostname module depend on a uname module would be the wrong approach, wouldn't it? Otherwise, all POSIX-replacement modules in gnulib would need to depend on modules for all POSIX features they use in its implementation -- I suspect this can quickly become a clique, causing any addition of a POSIX replacement function to drag in all POSIX replacement functions in gnulib. If there were a platform that has uname but doesn't have gethostname, I can see some merit to my patch, but are there such beasts? The gethostname module shouldn't depend on such modules, they need to be added manually by maintainers who want compatibility with non-POSIX systems. mingw portability is requested quite often, therefore gnulib includes the dependencies needed for mingw portability. We omit dependencies only for extremely old systems (e.g. BSD 4.2 - missing memcpy()). Agreed. /Simon
Re: gethostname
Simon Josefsson wrote: Sam Steingold [EMAIL PROTECTED] writes: 1. why aren't you testing for HAVE_SYS_UTSNAME_H instead? 2. are there really systems without uname sys/utsname.h, given that these are in posix? http://www.opengroup.org/onlinepubs/009695399/functions/uname.html I think the right solution here should be to apply the patch below. If there are problems on any system caused by that, the solution to that problem should be to create a module for sys/utsname.h and uname. The gethostname module shouldn't depend on such modules, they need to be added manually by maintainers who want compatibility with non-POSIX systems. Thoughts? note that gethostname is just as posix as uname, http://www.opengroup.org/onlinepubs/009695399/functions/gethostname.html so the gethostname module is only needed for non-posix systems. Indeed, so I am less sure my patch is the right thing. Could you explain why the current code causes problems for you? no problems - just questions: 1. is this module ever needed on a unix system? i.e., are there any unix systems still in use that lack gethostname? 2. are there any unix systems still in use that lack uname?
Re: gethostname
Sam Steingold [EMAIL PROTECTED] writes: 1. is this module ever needed on a unix system? i.e., are there any unix systems still in use that lack gethostname? 2. are there any unix systems still in use that lack uname? Among the platforms represented in Bruno Haible's collection of library symbols, only mingw and nsk-G06 lack either one (and they lack both of them). Solaris has gethostname in libnsl, BeOS has it in libnet, and other platforms have it in libc. BeoS has uname in libnet, and other platforms have it in libc. -- ...In the UNIX world, people tend to interpret `non-technical user' as meaning someone who's only ever written one device driver. --Daniel Pead
Re: gethostname
Ben Pfaff wrote: Sam Steingold [EMAIL PROTECTED] writes: 1. is this module ever needed on a unix system? i.e., are there any unix systems still in use that lack gethostname? 2. are there any unix systems still in use that lack uname? Among the platforms represented in Bruno Haible's collection of library symbols, only mingw and nsk-G06 lack either one (and they lack both of them). Solaris has gethostname in libnsl, BeOS has it in libnet, and other platforms have it in libc. BeoS has uname in libnet, and other platforms have it in libc. but this makes the gethostname module completely useless, doesn't it?
Re: gethostname
Sam Steingold [EMAIL PROTECTED] writes: Simon Josefsson wrote: Sam Steingold [EMAIL PROTECTED] writes: 1. why aren't you testing for HAVE_SYS_UTSNAME_H instead? 2. are there really systems without uname sys/utsname.h, given that these are in posix? http://www.opengroup.org/onlinepubs/009695399/functions/uname.html I think the right solution here should be to apply the patch below. If there are problems on any system caused by that, the solution to that problem should be to create a module for sys/utsname.h and uname. The gethostname module shouldn't depend on such modules, they need to be added manually by maintainers who want compatibility with non-POSIX systems. Thoughts? note that gethostname is just as posix as uname, http://www.opengroup.org/onlinepubs/009695399/functions/gethostname.html so the gethostname module is only needed for non-posix systems. Indeed, so I am less sure my patch is the right thing. Could you explain why the current code causes problems for you? no problems - just questions: 1. is this module ever needed on a unix system? i.e., are there any unix systems still in use that lack gethostname? 2. are there any unix systems still in use that lack uname? Ben answered those questions. Even if the code may not be optimal for some platform we aren't aware of, until we know of a real problem with it I think we could leave the code as-is. /Simon
Re: gethostname
Sam Steingold [EMAIL PROTECTED] writes: Ben Pfaff wrote: Sam Steingold [EMAIL PROTECTED] writes: 1. is this module ever needed on a unix system? i.e., are there any unix systems still in use that lack gethostname? 2. are there any unix systems still in use that lack uname? Among the platforms represented in Bruno Haible's collection of library symbols, only mingw and nsk-G06 lack either one (and they lack both of them). Solaris has gethostname in libnsl, BeOS has it in libnet, and other platforms have it in libc. BeoS has uname in libnet, and other platforms have it in libc. but this makes the gethostname module completely useless, doesn't it? To me, it looks like it has limited value: on mingw and nsk-G06 it provides a gethostname function that is otherwise missing. I don't think it will ever fall back on uname on the systems that we know about, though, except possibly on Solaris (because the m4 code doesn't check for gethostname in libnsl). Probably gethostname.m4 should check for gethostname in libnsl and in libnet? -- The sound of peacocks being shredded can't possibly be any worse than the sound of peacocks not being shredded. Tanuki the Raccoon-dog in the Monastery
Re: gethostname
Simon Josefsson wrote: Sam Steingold [EMAIL PROTECTED] writes: Simon Josefsson wrote: Sam Steingold [EMAIL PROTECTED] writes: 1. why aren't you testing for HAVE_SYS_UTSNAME_H instead? 2. are there really systems without uname sys/utsname.h, given that these are in posix? http://www.opengroup.org/onlinepubs/009695399/functions/uname.html I think the right solution here should be to apply the patch below. If there are problems on any system caused by that, the solution to that problem should be to create a module for sys/utsname.h and uname. The gethostname module shouldn't depend on such modules, they need to be added manually by maintainers who want compatibility with non-POSIX systems. Thoughts? note that gethostname is just as posix as uname, http://www.opengroup.org/onlinepubs/009695399/functions/gethostname.html so the gethostname module is only needed for non-posix systems. Indeed, so I am less sure my patch is the right thing. Could you explain why the current code causes problems for you? no problems - just questions: 1. is this module ever needed on a unix system? i.e., are there any unix systems still in use that lack gethostname? 2. are there any unix systems still in use that lack uname? Ben answered those questions. Even if the code may not be optimal for some platform we aren't aware of, until we know of a real problem with it I think we could leave the code as-is. Please name the platform(s) which can benefit from this module. Ben's answer suggests that the set is empty.
Re: gethostname
Sam Steingold [EMAIL PROTECTED] writes: Simon Josefsson wrote: Sam Steingold [EMAIL PROTECTED] writes: Simon Josefsson wrote: Sam Steingold [EMAIL PROTECTED] writes: 1. why aren't you testing for HAVE_SYS_UTSNAME_H instead? 2. are there really systems without uname sys/utsname.h, given that these are in posix? http://www.opengroup.org/onlinepubs/009695399/functions/uname.html I think the right solution here should be to apply the patch below. If there are problems on any system caused by that, the solution to that problem should be to create a module for sys/utsname.h and uname. The gethostname module shouldn't depend on such modules, they need to be added manually by maintainers who want compatibility with non-POSIX systems. Thoughts? note that gethostname is just as posix as uname, http://www.opengroup.org/onlinepubs/009695399/functions/gethostname.html so the gethostname module is only needed for non-posix systems. Indeed, so I am less sure my patch is the right thing. Could you explain why the current code causes problems for you? no problems - just questions: 1. is this module ever needed on a unix system? i.e., are there any unix systems still in use that lack gethostname? 2. are there any unix systems still in use that lack uname? Ben answered those questions. Even if the code may not be optimal for some platform we aren't aware of, until we know of a real problem with it I think we could leave the code as-is. Please name the platform(s) which can benefit from this module. Ben's answer suggests that the set is empty. Ben said MinGW doesn't have gethostname. I don't know what nsk-G06 is, is it a platform gnulib supports? Hm. I think MinGW actually does have a gethostname, but you need to link to -lws2_32 to see it. Yup. I actually have a local tree of gnulib that implements this test. Patch below, but it is ancient and there were likely some reason this was never installed. I'll see if I can revise it. /Simon diff --git a/ChangeLog b/ChangeLog index f52d4c2..070a15e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,22 @@ 2008-01-17 Simon Josefsson [EMAIL PROTECTED] +Bruno Haible [EMAIL PROTECTED] + + * lib/unistd.in.h: Include sys/socket.h conditionally. + * modules/unistd (Makefile.am): Substitute GNULIB_GETHOSTNAME. + * modules/gethostname (Depends-on): Add sys_socket and unistd. + (configure.ac): Invoke gl_MODULE_INDICATOR. + (Include): Document unistd.h. + * m4/unistd_h.m4: Default GNULIB_GETHOSTNAME to 0. + +2008-01-17 Simon Josefsson [EMAIL PROTECTED] + + * m4/gethostname.m4: Look for gethostname in winsock2.h and + -lws2_32 too, for mingw. + + * modules/gethostname (Depends-on): Add sys_socket, needed for + proper mingw checks. + +2008-01-17 Simon Josefsson [EMAIL PROTECTED] * modules/gethostname-tests: New file. diff --git a/lib/unistd.in.h b/lib/unistd.in.h index 5400c86..809c14a 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -1,5 +1,5 @@ /* Substitute for and wrapper around unistd.h. - Copyright (C) 2004-2007 Free Software Foundation, Inc. + Copyright (C) 2004-2008 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 @@ -33,6 +33,11 @@ /* mingw fails to declare _exit in unistd.h. */ #include stdlib.h +/* mingw fails to declare gethostname in unistd.h. */ +#if [EMAIL PROTECTED]@ ((defined _WIN32 || defined __WIN32__) ! defined __CYGWIN__) +# include sys/socket.h +#endif + /* The definition of GL_LINK_WARNING is copied here. */ diff --git a/m4/gethostname.m4 b/m4/gethostname.m4 index 1e9749d..359cdf1 100644 --- a/m4/gethostname.m4 +++ b/m4/gethostname.m4 @@ -1,13 +1,45 @@ -# gethostname.m4 serial 2 -dnl Copyright (C) 2002 Free Software Foundation, Inc. +# gethostname.m4 serial 3 +dnl Copyright (C) 2002, 2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. AC_DEFUN([gl_FUNC_GETHOSTNAME], [ - AC_REPLACE_FUNCS(gethostname) - if test $ac_cv_func_gethostname = no; then + AC_REQUIRE([gl_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H + + AC_CACHE_CHECK([for gethostname], [gl_cv_func_gethostname], [ +AC_TRY_LINK([ +#include sys/types.h +#ifdef HAVE_SYS_SOCKET_H +#include sys/socket.h +#endif +#include stddef.h +], [gethostname(, 0);], + [gl_cv_func_gethostname=yes], + [gl_cv_func_gethostname=no])]) + if test $gl_cv_func_gethostname = no; then +AC_CACHE_CHECK(for gethostname in winsock2.h and -lws2_32, + gl_cv_w32_gethostname, [ + gl_cv_w32_gethostname=no + am_save_LIBS=$LIBS + LIBS=$LIBS -lws2_32 + AC_TRY_LINK([ +#ifdef HAVE_WINSOCK2_H +#include winsock2
Re: gethostname
Ben Pfaff wrote: Sam Steingold [EMAIL PROTECTED] writes: Ben Pfaff wrote: Sam Steingold [EMAIL PROTECTED] writes: 1. is this module ever needed on a unix system? i.e., are there any unix systems still in use that lack gethostname? 2. are there any unix systems still in use that lack uname? Among the platforms represented in Bruno Haible's collection of library symbols, only mingw and nsk-G06 lack either one (and they lack both of them). Solaris has gethostname in libnsl, BeOS has it in libnet, and other platforms have it in libc. BeoS has uname in libnet, and other platforms have it in libc. but this makes the gethostname module completely useless, doesn't it? To me, it looks like it has limited value: on mingw and nsk-G06 it provides a gethostname function that is otherwise missing. I don't think it will ever fall back on uname on the systems that we know about, though, except possibly on Solaris (because the m4 code doesn't check for gethostname in libnsl). so the logical approach would be to either drop the C code altogether or implement it for nsk-G06 (whatever that might be). note: woe32 does have gethostname, so mingw is fully covered. Probably gethostname.m4 should check for gethostname in libnsl and in libnet? sounds good.
Re: gethostname
Simon Josefsson [EMAIL PROTECTED] writes: I don't know what nsk-G06 is, is it a platform gnulib supports? I think it's the Tandem (now HP) NonStop Kernel. I don't know whether gnulib supports it. -- Ben Pfaff http://benpfaff.org
Re: gethostname
Sam Steingold [EMAIL PROTECTED] writes: note: woe32 does have gethostname, so mingw is fully covered. Not quite, MinGW only has gethostname if you link to -lws2_32. The current m4 test doesn't check for this, so it fails to detect that gethostname exists. My patch attempts to test for gethostname in that library, a'la similar code for getaddrinfo etc, but it is rather messy. Possibly this link-to-ws2_32-if-it-makes-sense code should be re-factored out to a separate m4 test. /Simon
Re: gethostname
Ben Pfaff wrote: Solaris has gethostname in libnsl, Solaris = 2.5.1 also has it in libc. Since Solaris 2.4 is not worth supporting today any more, the current gethostname module is right in *not* testing for -lnsl. Bruno
Re: gethostname
Bruno Haible [EMAIL PROTECTED] writes: Ben Pfaff wrote: Solaris has gethostname in libnsl, Solaris = 2.5.1 also has it in libc. Since Solaris 2.4 is not worth supporting today any more, the current gethostname module is right in *not* testing for -lnsl. You are right, of course. I missed seeing that in the show-portability output, even though it was right there. I think that part of the reason was that the output is grouped by library. When I adjust the sorting to be by OS, it is easier for me to see that: libcaix-4.3.2 libcaix-5.1.0 libnet beos libccygwin libccygwin libcfreebsd-5.2.1 libcfreebsd-6.0 libcglibc-2.3.6 libchpux-11.00 libchpux-11.11 libcinterix-3.5 libcirix-6.5 libcmacosx-10.3 libcnetbsd-3.0 libcopenbsd-3.8 libcosf1-4.0d libcosf1-5.1a libcpips libcsolaris-2.10 libnsl solaris-2.10 libnsl solaris-2.4 libcsolaris-2.5.1 libnsl solaris-2.5.1 libcsolaris-2.6 libnsl solaris-2.6 libcsolaris-2.7 libnsl solaris-2.7 libcsolaris-2.8 libnsl solaris-2.8 libcsolaris-2.9 libnsl solaris-2.9 MISSING in mingw nsk-G06 --- tmp/various-symlists/show-portability 2008-01-13 12:51:54.0 -0800 +++ various-symlists/show-portability 2008-08-12 19:46:40.0 -0700 @@ -79,7 +79,7 @@ -e 's,^librootbeos$,libc beos,' grep '^'$symbol'$' $d/* /dev/null || echo $os 13 done \ -| sed -e 's,^\(\) *,\1,' | sort -k1,80 +| sed -e 's,^\(\) *,\1,' | sort -b -k 2,2 -k 1,1 } 3 .missing missing=`cat .missing` rm -f .missing -- I love deadlines. I love the whooshing noise they make as they go by. --Douglas Adams
gethostname
gnulib/lib/gethostname.c says: #ifdef HAVE_UNAME # include sys/utsname.h #endif 1. why aren't you testing for HAVE_SYS_UTSNAME_H instead? 2. are there really systems without uname sys/utsname.h, given that these are in posix? http://www.opengroup.org/onlinepubs/009695399/functions/uname.html
Re: gethostname
Sam Steingold [EMAIL PROTECTED] writes: gnulib/lib/gethostname.c says: #ifdef HAVE_UNAME # include sys/utsname.h #endif 1. why aren't you testing for HAVE_SYS_UTSNAME_H instead? 2. are there really systems without uname sys/utsname.h, given that these are in posix? http://www.opengroup.org/onlinepubs/009695399/functions/uname.html I think the right solution here should be to apply the patch below. If there are problems on any system caused by that, the solution to that problem should be to create a module for sys/utsname.h and uname. The gethostname module shouldn't depend on such modules, they need to be added manually by maintainers who want compatibility with non-POSIX systems. Thoughts? /Simon diff --git a/ChangeLog b/ChangeLog index 8d92122..b911473 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2008-08-11 Simon Josefsson [EMAIL PROTECTED] + + * lib/gethostname.c: Unconditionally include sys/utsname.h and + call uname. + + * m4/gethostname.m4: Assume uname exists. + 2008-08-07 Paolo Bonzini [EMAIL PROTECTED] * lib/lock.c (glthread_recursive_lock_init_multithreaded) diff --git a/lib/gethostname.c b/lib/gethostname.c index 169dd4e..c9624f0 100644 --- a/lib/gethostname.c +++ b/lib/gethostname.c @@ -19,9 +19,7 @@ #include config.h -#ifdef HAVE_UNAME -# include sys/utsname.h -#endif +#include sys/utsname.h #include string.h @@ -34,7 +32,6 @@ int gethostname (char *name, size_t len) { -#ifdef HAVE_UNAME struct utsname uts; if (uname (uts) == -1) @@ -46,8 +43,6 @@ gethostname (char *name, size_t len) len = sizeof (uts.nodename); } strncpy (name, uts.nodename, len); -#else - strcpy (name, ); /* Hardcode your system name if you want. */ -#endif + return 0; } diff --git a/m4/gethostname.m4 b/m4/gethostname.m4 index 1e9749d..616f9e4 100644 --- a/m4/gethostname.m4 +++ b/m4/gethostname.m4 @@ -1,5 +1,5 @@ -# gethostname.m4 serial 2 -dnl Copyright (C) 2002 Free Software Foundation, Inc. +# gethostname.m4 serial 3 +dnl Copyright (C) 2002, 2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. @@ -14,5 +14,5 @@ AC_DEFUN([gl_FUNC_GETHOSTNAME], # Prerequisites of lib/gethostname.c. AC_DEFUN([gl_PREREQ_GETHOSTNAME], [ - AC_CHECK_FUNCS(uname) + : ])
Re: gethostname
* Simon Josefsson [EMAIL PROTECTED] [2008-08-11 23:23:24 +0200]: Sam Steingold [EMAIL PROTECTED] writes: gnulib/lib/gethostname.c says: #ifdef HAVE_UNAME # include sys/utsname.h #endif 1. why aren't you testing for HAVE_SYS_UTSNAME_H instead? 2. are there really systems without uname sys/utsname.h, given that these are in posix? http://www.opengroup.org/onlinepubs/009695399/functions/uname.html I think the right solution here should be to apply the patch below. If there are problems on any system caused by that, the solution to that problem should be to create a module for sys/utsname.h and uname. The gethostname module shouldn't depend on such modules, they need to be added manually by maintainers who want compatibility with non-POSIX systems. Thoughts? note that gethostname is just as posix as uname, http://www.opengroup.org/onlinepubs/009695399/functions/gethostname.html so the gethostname module is only needed for non-posix systems. -- Sam Steingold (http://sds.podval.org/) on Fedora release 9 (Sulphur) http://israelunderattack.slide.com http://camera.org http://memri.org http://jihadwatch.org http://mideasttruth.com Are you smart enough to use Lisp?
Re: (x)gethostname
Bruno Haible [EMAIL PROTECTED] writes: And it is necessary to still override the function, in order to WSAStartup before? No, this doesn't seem to be necessary, at least under Wine. I don't think that you can judge the behaviour of Windows by testing on Wine. You know that Wine reimplements every DLL, so in particular Wine's winsock is a different implementation that Windows winsock. I checked real Windows behaviour here, and it does indeed require that WSAStartup is called before gethostname. Otherwise, gethostname will return -1 and doesn't populate the buffer. So the gethostname replacement module needs to call WSAStartup somehow. I'll post an updated patch that uses the sockets module shortly. /Simon
Re: (x)gethostname
Here is an updated patch to get gethostname working under MinGW. It uses the recent new gnulib module 'sockets'. The module works like this: If the gethostname function is not found on the system, it pulls in gethostname.c into the gnulib library, and defines gethostname to rpl_gethostname. rpl_gethostname calls the gl_socket_* functions to initialize and shutdown Windows sockets, calls the real Windows gethostname function. It then converts from the Windows error codes to errno, and then shut downs Windows sockets. What do people think about the approach chosen here? There are many choices which are open for discussion: 1) Should we convert from WSA* errors to errno codes? Alternatively, we could return WSA* errors in the errno variable, since the integer values do not seem to overlap, and enhance strerror to be able to print the Windows socket error strings. This may cause problems for code comparing errno values though, which is why I chose not to do this. 2) Should conversion of WSA* error codes happen inside the gethostname.c file, or should it be moved to (e.g.) sockets.{c,h}? It is not clear to me if there is a 1-1 mapping of all relevant error codes. Maybe some Windows function return Windows socket error X which is appropriate to map to errno value Y, while at the same time another Windows function return Windows socket error X which is appropriate (in that context) to map to errno value Z. 3) Is there any chance that the #define gethostname rpl_gethostname and replacement prototype of gethostname in gnulib's unistd.h will cause problems? Consider that gethostname in winsock2.h is declared with PASCAL calling conventions, so the prototype is not the same as the one that gnulib uses. That may cause problems in some weird situations, but I can't see them right now. The self test works fine. 4) Is calling WSAStartup/WSACleanup in each gnulib replacement module a good idea? I could easily argue that it is simpler for everyone if gnulib simply say that if you care about Windows portability, you need to call WSAStartup+WSACleanup yourself. The gnulib module 'sockets' could be used for this, and then the 'gethostname' module may not need to depend directly on the 'sockets' module. 5) Does this result in the right thing on cygwin? I suppose it should detect that gethostname is present and not do anything beyond that. Thanks, /Simon diff --git a/lib/gethostname.c b/lib/gethostname.c index 169dd4e..767afe6 100644 --- a/lib/gethostname.c +++ b/lib/gethostname.c @@ -15,14 +15,24 @@ You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. */ -/* David MacKenzie [EMAIL PROTECTED] */ +/* David MacKenzie [EMAIL PROTECTED], + MinGW port by Simon Josefsson [EMAIL PROTECTED] */ #include config.h +#undef gethostname #ifdef HAVE_UNAME # include sys/utsname.h #endif +#ifdef WINDOWS_SOCKETS +# include errno.h +# include sockets.h +# ifdef HAVE_WINSOCK2_H +# include winsock2.h +# endif +#endif + #include string.h /* Put up to LEN chars of the host name into NAME. @@ -32,7 +42,7 @@ #include stddef.h int -gethostname (char *name, size_t len) +rpl_gethostname (char *name, size_t len) { #ifdef HAVE_UNAME struct utsname uts; @@ -46,6 +56,37 @@ gethostname (char *name, size_t len) len = sizeof (uts.nodename); } strncpy (name, uts.nodename, len); +#elif WINDOWS_SOCKETS + int err; + + if (gl_sockets_startup (SOCKETS_1_1)) +{ + errno = ENOSYS; + return -1; +} + + err = gethostname (name, len); + if (err 0) +{ + switch (WSAGetLastError()) + { + case WSAEFAULT: + errno = EFAULT; + break; + + default: + errno = ENOSYS; + break; + } +} + + if (gl_sockets_cleanup ()) +{ + errno = ENOSYS; + return -1; +} + + return err; #else strcpy (name, ); /* Hardcode your system name if you want. */ #endif diff --git a/lib/unistd.in.h b/lib/unistd.in.h index 5400c86..b67e288 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -1,5 +1,5 @@ /* Substitute for and wrapper around unistd.h. - Copyright (C) 2004-2007 Free Software Foundation, Inc. + Copyright (C) 2004-2008 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 @@ -156,6 +156,18 @@ extern char * getcwd (char *buf, size_t size); getcwd (b, s)) #endif +#if @GNULIB_GETHOSTNAME@ +# if [EMAIL PROTECTED]@ +# include stddef.h + extern int gethostname (char *name, size_t size); +# endif +#elif defined GNULIB_POSIXCHECK +# undef gethostname +# define gethostname(n,s) \ + (GL_LINK_WARNING (gethostname is unportable - \ + use gnulib module gethostname for portability), \ + gethostname (n, s)) +#endif
Re: (x)gethostname
Brian Dessent [EMAIL PROTECTED] writes: Simon Josefsson wrote: 3) Is there any chance that the #define gethostname rpl_gethostname and replacement prototype of gethostname in gnulib's unistd.h will cause problems? Consider that gethostname in winsock2.h is declared with PASCAL calling conventions, so the prototype is not the same as the one that gnulib uses. That may cause problems in some weird situations, but I can't see them right now. The self test works fine. I was invisioning a situation where a package that is already Winsock-aware (i.e. does its own WSAStartup) erroniously adds the gethostname module. Then the replacement would be pulled in, resulting in a Startup+Cleanup when it's not desired, which would potentially trash any open socket operations the app might have at that point. However, the MSDN page does say that Startup/Cleanup are reference counted and the teardown is only done in the case where the count goes to 0. So as long as the replacement gethostname always does them in pairs this should be fine. Right, that was how I read the documentation as well. 4) Is calling WSAStartup/WSACleanup in each gnulib replacement module a good idea? I could easily argue that it is simpler for everyone if gnulib simply say that if you care about Windows portability, you need to call WSAStartup+WSACleanup yourself. The gnulib module 'sockets' could be used for this, and then the 'gethostname' module may not need to depend directly on the 'sockets' module. I think neglecting to WSAStartup is one of the most common mistakes when porting socket applications to Win32. Couldn't there be a gnulib module that perhaps wraps main() to first call WSAStartup() and atexit(WSACleanup()) so that portable sockets programs don't have to see any of it? This wouldn't work well for libraries. I think documenting this, and providing useful gnulib modules to make sure the WSAStartup gets called is sufficient. (I'm not even sure if the atexit is really necessary -- MSDN does say There must be a call to WSACleanup for each successful call to WSAStartup but that is in the context of explaining the reference count. I'm nearly certain that the required cleanup is also done when the process terminates normally.) Yeah, I think so too. 5) Does this result in the right thing on cygwin? I suppose it should detect that gethostname is present and not do anything beyond that. It looks fine here. WINDOWS_SOCKETS is not defined and the replacement is not used. Great. /Simon
Re: gethostname-test
Bruno Haible [EMAIL PROTECTED] writes: Hi Simon, 2008-01-17 Simon Josefsson [EMAIL PROTECTED] +* modules/gethostname-tests: New file. + +* tests/test-gethostname.c: New file. Thanks. A nice test. Just a fix to make it compile with gcc -Wall: Oops, thanks. /Simon 2008-01-17 Bruno Haible [EMAIL PROTECTED] * tests/test-gethostname.c (main): Fix printf statement. *** tests/test-gethostname.c.orig 2008-01-18 02:12:12.0 +0100 --- tests/test-gethostname.c2008-01-18 02:11:08.0 +0100 *** *** 36,42 if (rc != 0) { ! printf (gethostname failed, rc %d errno %d\n, errno); return 1; } --- 36,42 if (rc != 0) { ! printf (gethostname failed, rc %d errno %d\n, rc, errno); return 1; }
Re: (x)gethostname
Simon Josefsson wrote: For mingw, @GNULIB_GETHOSTNAME@ will be 0 since it has a gethostname (if sys/socket.h is pulled in). Huh? GNULIB_GETHOSTNAME is set to 1 by the gl_MODULE_INDICATOR([gethostname]) line, independently of platforms. It is 0 if the gethostname module is not used. Bruno
Re: (x)gethostname
Hi Simon, But this is still not complete: The doc/functions/gethostname.texi file says that the problem is that mingw lacks a gethostname function. This is not the case now, and I'm not sure it ever was. Where did that information come from? I wrote it: I saw that the .m4 macro only looked whether the function was missing and, based on the results of nm on various systems' libraries, found that mingw was such a system. Evidently I didn't consider ws2_32 as a system library. For reference, the Windows documentation for gethostname is: http://msdn2.microsoft.com/en-us/library/ms738527.aspx Thanks for the reference. So, to accomodate this function, we need a rpl_gethostname that calls gethostname and accomodates 1. for the need to call WSAStartup() before, This will require a new module 'sockets' in gnulib, because obviously we don't want to call WSAStartup() more than once. 2. for the conversion from a WSA* error code to an errno.h error code. This will require, in particular, an 'errno' module and a modification to the 'strerror' module. The MSDN documentation says it is in winsock2.h and ws2_32, so I think we should use those headers/libraries until we know a reason to prefer winsock.h and wsock32. Yes, I agree. Your sys_socket module already uses winsock2; there is no point in mixing two different implementations of socket libraries. And it is necessary to still override the function, in order to WSAStartup before? No, this doesn't seem to be necessary, at least under Wine. I don't think that you can judge the behaviour of Windows by testing on Wine. You know that Wine reimplements every DLL, so in particular Wine's winsock is a different implementation that Windows winsock. Bruno
Re: (x)gethostname
Simon, If we modify gethostname.m4 as I suggested, it will detect that mingw do have a gethostname function, and consequently, the gethostname module is not be needed. Actually, the replacement function is not needed. But the piece of configure.ac from modules/gethostname is expanded into configure in all cases. And it sets GNULIB_GETHOSTNAME=1 unconditionally. In that case, though, unistd.h needs to declare the function, since MinGW's unistd.h doesn't. So the test in unistd.h needs to be ... Then it needs to be (@GNULIB_GETHOSTNAME@ [EMAIL PROTECTED]@), and your gethostname.m4 file needs to set a HAVE_DECL_GETHOSTNAME variable. Anyway, it needs more prerequisite work regarding the modules 'sockets', 'errno', 'strerror'. Bruno
Re: (x)gethostname
Simon Josefsson [EMAIL PROTECTED] writes: For reference, the Windows documentation for gethostname is: http://msdn2.microsoft.com/en-us/library/ms738527.aspx Thanks for the reference. So, to accomodate this function, we need a rpl_gethostname that calls gethostname and accomodates 1. for the need to call WSAStartup() before, This will require a new module 'sockets' in gnulib, because obviously we don't want to call WSAStartup() more than once. 2. for the conversion from a WSA* error code to an errno.h error code. This will require, in particular, an 'errno' module and a modification to the 'strerror' module. I think you are right. I started to implement 1). The documentation for WSAStartup is at: http://msdn2.microsoft.com/en-us/library/ms742213.aspx Calling WSAStartup more than once is not a problem if we make sure to call WSACleanup the same number of times, which I think we have to do. A sample socket module would look like: winsocket.h: #ifndef WINSOCKET_H # define WINSOCKET_H 1 #define WINSOCK_1_0 0x100 #define WINSOCK_1_1 0x101 #define WINSOCK_2_0 0x200 #define WINSOCK_2_1 0x201 #define WINSOCK_2_2 0x202 int gl_wsastartup (int version); int gl_wsacleanup (void); #endif winsocket.c: #include config.h /* This includes winsock2.h on MinGW. */ #include sys/socket.h #include winsocket.h int gl_wsastartup (int version) { WSADATA data; int err; err = WSAStartup (version, data); if (err != 0) return 1; if (data.wVersion version) return 2; return 0; } int gl_wsacleanup (int version) { int err; err = WSACleanup (); if (err != 0) return 1; return 0; } Then the mingw rpl_gethostname will look like: #include winsocket.h int rpl_gethostname (char *name, size_t len) { int err; if (gl_wsastartup (WINSOCK_1_0)) return ENOTSUP; /* XXX what code to use? */ err = gethostname (name, len); /* WSAGetLastError-errno conversion.. */ gl_wsacleanup (); return err; } One alternative is to require that applications must call WSAStartup/WSACleanup themselves. This would simplify gnulib code a lot, and would probably improve efficiency as well (no need to re-load the winsocket stuff). /Simon
Re: (x)gethostname
Bruno Haible [EMAIL PROTECTED] writes: Hi Simon, But this is still not complete: The doc/functions/gethostname.texi file says that the problem is that mingw lacks a gethostname function. This is not the case now, and I'm not sure it ever was. Where did that information come from? I wrote it: I saw that the .m4 macro only looked whether the function was missing and, based on the results of nm on various systems' libraries, found that mingw was such a system. Evidently I didn't consider ws2_32 as a system library. It would be useful to collect nm from ws_32 (and winsock32) as well, for reference. As for it being a system library, I'm not sure. It appears to contain functions that POSIX considers to be system functions. If you know how to collect this data for Windows 95, 98 etc, it may be useful to understand which functions are available in which releases. For reference, the Windows documentation for gethostname is: http://msdn2.microsoft.com/en-us/library/ms738527.aspx Thanks for the reference. So, to accomodate this function, we need a rpl_gethostname that calls gethostname and accomodates 1. for the need to call WSAStartup() before, This will require a new module 'sockets' in gnulib, because obviously we don't want to call WSAStartup() more than once. 2. for the conversion from a WSA* error code to an errno.h error code. This will require, in particular, an 'errno' module and a modification to the 'strerror' module. I think you are right. The MSDN documentation says it is in winsock2.h and ws2_32, so I think we should use those headers/libraries until we know a reason to prefer winsock.h and wsock32. Yes, I agree. Your sys_socket module already uses winsock2; there is no point in mixing two different implementations of socket libraries. Actually, I think mixing them will break, but I'm not sure. Let's hope we never need to consider winsock2.h or wsock32.dll. My understanding is that they are only needed for old Windows releases (e.g., Windows 3.x, Windows 95 or Windows 98) without service packs, since they don't have the newer winsock libraries. And it is necessary to still override the function, in order to WSAStartup before? No, this doesn't seem to be necessary, at least under Wine. I don't think that you can judge the behaviour of Windows by testing on Wine. You know that Wine reimplements every DLL, so in particular Wine's winsock is a different implementation that Windows winsock. Let's trust the documentation. What surprised me was that I recall seeing failures under Wine because WSAStartup() wasn't called, so I guessed that possibly Wine follows Windows behaviour. /Simon
Re: (x)gethostname
Bruno Haible [EMAIL PROTECTED] writes: Simon Josefsson wrote: For mingw, @GNULIB_GETHOSTNAME@ will be 0 since it has a gethostname (if sys/socket.h is pulled in). Huh? GNULIB_GETHOSTNAME is set to 1 by the gl_MODULE_INDICATOR([gethostname]) line, independently of platforms. It is 0 if the gethostname module is not used. If we modify gethostname.m4 as I suggested, it will detect that mingw do have a gethostname function, and consequently, the gethostname module is not be needed. In that case, though, unistd.h needs to declare the function, since MinGW's unistd.h doesn't. So the test in unistd.h needs to be [EMAIL PROTECTED]@ to cover the mingw situation. /Simon
Re: (x)gethostname
Bruno Haible [EMAIL PROTECTED] writes: Simon, If we modify gethostname.m4 as I suggested, it will detect that mingw do have a gethostname function, and consequently, the gethostname module is not be needed. Actually, the replacement function is not needed. But the piece of configure.ac from modules/gethostname is expanded into configure in all cases. And it sets GNULIB_GETHOSTNAME=1 unconditionally. In that case, though, unistd.h needs to declare the function, since MinGW's unistd.h doesn't. So the test in unistd.h needs to be ... Then it needs to be (@GNULIB_GETHOSTNAME@ [EMAIL PROTECTED]@), and your gethostname.m4 file needs to set a HAVE_DECL_GETHOSTNAME variable. Ah, I see. Anyway, it needs more prerequisite work regarding the modules 'sockets', 'errno', 'strerror'. Right, I'm starting with that, see next message. /Simon
Re: missing strcpy prototype in gethostname module
Bruno Haible [EMAIL PROTECTED] writes: Simon Josefsson wrote: @@ -21,6 +21,8 @@ #ifdef HAVE_UNAME # include sys/utsname.h +#else +# include string.h #endif /* Put up to LEN chars of the host name into NAME. While at it, you could make the patch complete: The function strncpy also needs to be declared somewhere. Good point. Patch below applied. /Simon diff --git a/ChangeLog b/ChangeLog index 467fadb..776e8ff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2008-01-17 Simon Josefsson [EMAIL PROTECTED] + + * lib/gethostname.c: Include string.h unconditionally, strncpy is + used by the UNAME case. Reported by Bruno Haible + [EMAIL PROTECTED]. + 2008-01-17 Eric Blake [EMAIL PROTECTED] Convert c-strcasestr to be more efficient. diff --git a/lib/gethostname.c b/lib/gethostname.c index de3d9a5..169dd4e 100644 --- a/lib/gethostname.c +++ b/lib/gethostname.c @@ -21,10 +21,10 @@ #ifdef HAVE_UNAME # include sys/utsname.h -#else -# include string.h #endif +#include string.h + /* Put up to LEN chars of the host name into NAME. Null terminate it if the name is shorter than LEN. Return 0 if ok, -1 if error. */
Re: (x)gethostname
Simon Josefsson [EMAIL PROTECTED] writes: 1) Modify gethostname module to look for gethostname in winsock2.h and -lws2_32, similar to what's in getaddrinfo.m4. This will make gethostname depend on sys/socket.h, just like getaddrinfo does. 2) Modify unistd module to make sure it includes sys/socket.h when @GNULIB_GETHOSTNAME@ is enabled. I think your patch mostly takes care of 2), so I'll see if I can propose a better gethostname.m4 test that actually detects gethostname on MinGW. Below is the patch for gethostname.m4. On debian x86: checking for unistd.h... yes checking sys/socket.h usability... yes checking sys/socket.h presence... yes checking for sys/socket.h... yes checking whether sys/socket.h is self-contained... yes checking for gethostname... yes checking whether sys/socket.h is self-contained... (cached) yes The gethostname replacement isn't built, and the self-test works. On mingw32: checking for unistd.h... yes checking sys/socket.h usability... no checking sys/socket.h presence... no checking for sys/socket.h... no checking whether sys/socket.h is self-contained... no checking winsock2.h usability... yes checking winsock2.h presence... yes checking for winsock2.h... yes checking ws2tcpip.h usability... yes checking ws2tcpip.h presence... yes checking for ws2tcpip.h... yes checking for gethostname... no checking for gethostname in winsock2.h and -lws2_32... yes checking whether sys/socket.h is self-contained... (cached) no The gethostname replacement isn't built. The self-test fails on mingw because unistd.h doesn't pull in winsock2.h, but that's issue 2) above. If I manually add #include winsock2.h, it works. Comments? /Simon 2008-01-17 Simon Josefsson [EMAIL PROTECTED] * m4/gethostname.m4: Look for gethostname in winsock2.h and -lws2_32 too, for mingw. * modules/gethostname (Depends-on): Add sys_socket, needed for proper mingw checks. diff --git a/m4/gethostname.m4 b/m4/gethostname.m4 index 1e9749d..359cdf1 100644 --- a/m4/gethostname.m4 +++ b/m4/gethostname.m4 @@ -1,13 +1,45 @@ -# gethostname.m4 serial 2 -dnl Copyright (C) 2002 Free Software Foundation, Inc. +# gethostname.m4 serial 3 +dnl Copyright (C) 2002, 2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. AC_DEFUN([gl_FUNC_GETHOSTNAME], [ - AC_REPLACE_FUNCS(gethostname) - if test $ac_cv_func_gethostname = no; then + AC_REQUIRE([gl_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H + + AC_CACHE_CHECK([for gethostname], [gl_cv_func_gethostname], [ +AC_TRY_LINK([ +#include sys/types.h +#ifdef HAVE_SYS_SOCKET_H +#include sys/socket.h +#endif +#include stddef.h +], [gethostname(, 0);], + [gl_cv_func_gethostname=yes], + [gl_cv_func_gethostname=no])]) + if test $gl_cv_func_gethostname = no; then +AC_CACHE_CHECK(for gethostname in winsock2.h and -lws2_32, + gl_cv_w32_gethostname, [ + gl_cv_w32_gethostname=no + am_save_LIBS=$LIBS + LIBS=$LIBS -lws2_32 + AC_TRY_LINK([ +#ifdef HAVE_WINSOCK2_H +#include winsock2.h +#endif +#include stddef.h +], [gethostname(, 0);], gl_cv_w32_gethostname=yes) +LIBS=$am_save_LIBS]) +if test $gl_cv_w32_gethostname = yes; then + LIBS=$LIBS -lws2_32 +else + AC_LIBOBJ(gethostname) +fi + fi + + if test $ac_cv_func_gethostname = no + test $gl_cv_w32_gethostname = no; then gl_PREREQ_GETHOSTNAME fi ]) diff --git a/modules/gethostname b/modules/gethostname index 7c13807..ec367b7 100644 --- a/modules/gethostname +++ b/modules/gethostname @@ -6,6 +6,7 @@ lib/gethostname.c m4/gethostname.m4 Depends-on: +sys_socket configure.ac: gl_FUNC_GETHOSTNAME
gethostname-test
This will simplify gethostname module improvements. Applied. /Simon diff --git a/ChangeLog b/ChangeLog index 776e8ff..f52d4c2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2008-01-17 Simon Josefsson [EMAIL PROTECTED] + * modules/gethostname-tests: New file. + + * tests/test-gethostname.c: New file. + +2008-01-17 Simon Josefsson [EMAIL PROTECTED] + * lib/gethostname.c: Include string.h unconditionally, strncpy is used by the UNAME case. Reported by Bruno Haible [EMAIL PROTECTED]. diff --git a/modules/gethostname-tests b/modules/gethostname-tests new file mode 100644 index 000..236ca58 --- /dev/null +++ b/modules/gethostname-tests @@ -0,0 +1,10 @@ +Files: +tests/test-gethostname.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-gethostname +check_PROGRAMS += test-gethostname diff --git a/tests/test-gethostname.c b/tests/test-gethostname.c new file mode 100644 index 000..d298b7a --- /dev/null +++ b/tests/test-gethostname.c @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2008 Free Software Foundation + * Written by Simon Josefsson. + * + * 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 http://www.gnu.org/licenses/. */ + +#include config.h + +#include stdio.h +#include string.h +#include errno.h +#include unistd.h + +#define NOHOSTNAME magic-gnulib-test-string + +int +main (int argc, char *argv[]) +{ + char buf[2500]; + int rc; + + strcpy (buf, NOHOSTNAME); + + rc = gethostname (buf, sizeof (buf)); + + if (rc != 0) +{ + printf (gethostname failed, rc %d errno %d\n, errno); + return 1; +} + + if (strcmp (buf, NOHOSTNAME) == 0) +{ + printf (gethostname left buffer untouched.\n); + return 1; +} + + if (argc 1) +printf (hostname: %s\n, buf); + + return 0; +}
Re: (x)gethostname
Bruno Haible [EMAIL PROTECTED] writes: Simon Josefsson wrote: 2008-01-16 Simon Josefsson [EMAIL PROTECTED] * lib/unistd.in.h: Include sys/socket.h, to get to winsock2.h, which declares gethostname on MinGW. * modules/unistd (Depends-on): Add sys_socket. Half OK. This patch does too much, IMO: - It wants to fix a problem on mingw, and ends up including sys/socket.h on cygwin also, and you don't know if it will work. - It wants to fix a problem for packages that need gethostname, and ends up forcing a sys/socket.h file and winsock2 related autoconf test into every package. Especially sys/socket.h defines a lot of macros and types. For the sake of namespace cleanliness, it's good to avoid including when possible. Hence I'm in favour of limiting the effect through #ifdefs, like the appended patch. That approach would be fine with me. But this is still not complete: The doc/functions/gethostname.texi file says that the problem is that mingw lacks a gethostname function. This is not the case now, and I'm not sure it ever was. Where did that information come from? For reference, the Windows documentation for gethostname is: http://msdn2.microsoft.com/en-us/library/ms738527.aspx It seems to be a rather old function. But now you say that winsock2.h declares this function? Yes. So presumably this function exists, and can return more meaningful values than the empty string? To use this function, does a program need to link to non-default libraries? It works for me here, by linking to -lws2_32 or -lwsock32: [EMAIL PROTECTED]:~$ cat foo.c #include stdio.h #include winsock2.h int main () { char buf[1024]; int rc; rc = gethostname (buf, 1024); printf (rc %d\n, rc); printf (buf %s\n, buf); return 0; } [EMAIL PROTECTED]:~$ i586-mingw32msvc-gcc -o foo.exe foo.c /tmp/cccHKrQ2.o:foo.c:(.text+0x29): undefined reference to [EMAIL PROTECTED]' collect2: ld returned 1 exit status [EMAIL PROTECTED]:~$ i586-mingw32msvc-gcc -o foo.exe foo.c -lws2_32 [EMAIL PROTECTED]:~$ ./foo.exe rc 0 buf mocca Wine exited with a successful status [EMAIL PROTECTED]:~$ i586-mingw32msvc-gcc -o foo.exe foo.c -lwsock32 [EMAIL PROTECTED]:~$ ./foo.exe rc 0 buf mocca Wine exited with a successful status [EMAIL PROTECTED]:~$ The MSDN documentation says it is in winsock2.h and ws2_32, so I think we should use those headers/libraries until we know a reason to prefer winsock.h and wsock32. How does m4/gethostname.m4 need to be rewritten? I think it should do something of what getaddrinfo.m4 does, in particular: AC_REQUIRE([gl_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H AC_MSG_NOTICE([checking how to do getaddrinfo, freeaddrinfo and getnameinfo]) ... AC_CACHE_CHECK([for getaddrinfo], [gl_cv_func_getaddrinfo], [ AC_TRY_LINK([ #include sys/types.h #ifdef HAVE_SYS_SOCKET_H #include sys/socket.h #endif #ifdef HAVE_NETDB_H #include netdb.h #endif #include stddef.h ], [getaddrinfo(, , NULL, NULL);], [gl_cv_func_getaddrinfo=yes], [gl_cv_func_getaddrinfo=no])]) if test $gl_cv_func_getaddrinfo = no; then AC_CACHE_CHECK(for getaddrinfo in ws2tcpip.h and -lws2_32, gl_cv_w32_getaddrinfo, [ gl_cv_w32_getaddrinfo=no am_save_LIBS=$LIBS LIBS=$LIBS -lws2_32 AC_TRY_LINK([ #ifdef HAVE_WS2TCPIP_H #include ws2tcpip.h #endif #include stddef.h ], [getaddrinfo(NULL, NULL, NULL, NULL);], gl_cv_w32_getaddrinfo=yes) LIBS=$am_save_LIBS]) if test $gl_cv_w32_getaddrinfo = yes; then LIBS=$LIBS -lws2_32 else AC_LIBOBJ(getaddrinfo) fi fi And it is necessary to still override the function, in order to WSAStartup before? No, this doesn't seem to be necessary, at least under Wine. But the MSDN documentation says there is an error code WSANOTINITIALISED which is returned if WSAStartup hasn't been invoked. I'm not whether we should trust the MSDN documentation or Wine behaviour more. I think we have identified two work items: 1) Modify gethostname module to look for gethostname in winsock2.h and -lws2_32, similar to what's in getaddrinfo.m4. This will make gethostname depend on sys/socket.h, just like getaddrinfo does. 2) Modify unistd module to make sure it includes sys/socket.h when @GNULIB_GETHOSTNAME@ is enabled. I think your patch mostly takes care of 2), so I'll see if I can propose a better gethostname.m4 test that actually detects gethostname on MinGW. /Simon
Re: (x)gethostname
Bruno Haible [EMAIL PROTECTED] writes: + /* mingw fails to declare gethostname in unistd.h. */ + #if @GNULIB_GETHOSTNAME@ ((defined _WIN32 || defined __WIN32__) ! defined __CYGWIN__) + # include sys/socket.h + #endif + I tried your patch now, to take care of problem 1), but it doesn't work. For mingw, @GNULIB_GETHOSTNAME@ will be 0 since it has a gethostname (if sys/socket.h is pulled in). Also, the gethostname module needs the gnulib unistd.h to work around the problems on mingw. How about this? It is a complete patch against master, that works on debian-x86 and mingw. Thanks, /Simon diff --git a/ChangeLog b/ChangeLog index f52d4c2..070a15e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,22 @@ 2008-01-17 Simon Josefsson [EMAIL PROTECTED] +Bruno Haible [EMAIL PROTECTED] + + * lib/unistd.in.h: Include sys/socket.h conditionally. + * modules/unistd (Makefile.am): Substitute GNULIB_GETHOSTNAME. + * modules/gethostname (Depends-on): Add sys_socket and unistd. + (configure.ac): Invoke gl_MODULE_INDICATOR. + (Include): Document unistd.h. + * m4/unistd_h.m4: Default GNULIB_GETHOSTNAME to 0. + +2008-01-17 Simon Josefsson [EMAIL PROTECTED] + + * m4/gethostname.m4: Look for gethostname in winsock2.h and + -lws2_32 too, for mingw. + + * modules/gethostname (Depends-on): Add sys_socket, needed for + proper mingw checks. + +2008-01-17 Simon Josefsson [EMAIL PROTECTED] * modules/gethostname-tests: New file. diff --git a/lib/unistd.in.h b/lib/unistd.in.h index 5400c86..809c14a 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -1,5 +1,5 @@ /* Substitute for and wrapper around unistd.h. - Copyright (C) 2004-2007 Free Software Foundation, Inc. + Copyright (C) 2004-2008 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 @@ -33,6 +33,11 @@ /* mingw fails to declare _exit in unistd.h. */ #include stdlib.h +/* mingw fails to declare gethostname in unistd.h. */ +#if [EMAIL PROTECTED]@ ((defined _WIN32 || defined __WIN32__) ! defined __CYGWIN__) +# include sys/socket.h +#endif + /* The definition of GL_LINK_WARNING is copied here. */ diff --git a/m4/gethostname.m4 b/m4/gethostname.m4 index 1e9749d..359cdf1 100644 --- a/m4/gethostname.m4 +++ b/m4/gethostname.m4 @@ -1,13 +1,45 @@ -# gethostname.m4 serial 2 -dnl Copyright (C) 2002 Free Software Foundation, Inc. +# gethostname.m4 serial 3 +dnl Copyright (C) 2002, 2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. AC_DEFUN([gl_FUNC_GETHOSTNAME], [ - AC_REPLACE_FUNCS(gethostname) - if test $ac_cv_func_gethostname = no; then + AC_REQUIRE([gl_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H + + AC_CACHE_CHECK([for gethostname], [gl_cv_func_gethostname], [ +AC_TRY_LINK([ +#include sys/types.h +#ifdef HAVE_SYS_SOCKET_H +#include sys/socket.h +#endif +#include stddef.h +], [gethostname(, 0);], + [gl_cv_func_gethostname=yes], + [gl_cv_func_gethostname=no])]) + if test $gl_cv_func_gethostname = no; then +AC_CACHE_CHECK(for gethostname in winsock2.h and -lws2_32, + gl_cv_w32_gethostname, [ + gl_cv_w32_gethostname=no + am_save_LIBS=$LIBS + LIBS=$LIBS -lws2_32 + AC_TRY_LINK([ +#ifdef HAVE_WINSOCK2_H +#include winsock2.h +#endif +#include stddef.h +], [gethostname(, 0);], gl_cv_w32_gethostname=yes) +LIBS=$am_save_LIBS]) +if test $gl_cv_w32_gethostname = yes; then + LIBS=$LIBS -lws2_32 +else + AC_LIBOBJ(gethostname) +fi + fi + + if test $ac_cv_func_gethostname = no + test $gl_cv_w32_gethostname = no; then gl_PREREQ_GETHOSTNAME fi ]) diff --git a/m4/unistd_h.m4 b/m4/unistd_h.m4 index 4b8857c..1eca87c 100644 --- a/m4/unistd_h.m4 +++ b/m4/unistd_h.m4 @@ -1,5 +1,5 @@ # unistd_h.m4 serial 10 -dnl Copyright (C) 2006-2007 Free Software Foundation, Inc. +dnl Copyright (C) 2006-2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. @@ -38,6 +38,7 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS], GNULIB_FTRUNCATE=0; AC_SUBST([GNULIB_FTRUNCATE]) GNULIB_GETCWD=0;AC_SUBST([GNULIB_GETCWD]) GNULIB_GETLOGIN_R=0;AC_SUBST([GNULIB_GETLOGIN_R]) + GNULIB_GETHOSTNAME=0; AC_SUBST([GNULIB_GETHOSTNAME]) GNULIB_GETPAGESIZE=0; AC_SUBST([GNULIB_GETPAGESIZE]) GNULIB_LCHOWN=0;AC_SUBST([GNULIB_LCHOWN]) GNULIB_LSEEK=0; AC_SUBST([GNULIB_LSEEK]) diff --git a/modules/gethostname b/modules/gethostname index 7c13807..b169701 100644 --- a/modules/gethostname
Re: gethostname-test
Hi Simon, 2008-01-17 Simon Josefsson [EMAIL PROTECTED] + * modules/gethostname-tests: New file. + + * tests/test-gethostname.c: New file. Thanks. A nice test. Just a fix to make it compile with gcc -Wall: 2008-01-17 Bruno Haible [EMAIL PROTECTED] * tests/test-gethostname.c (main): Fix printf statement. *** tests/test-gethostname.c.orig 2008-01-18 02:12:12.0 +0100 --- tests/test-gethostname.c2008-01-18 02:11:08.0 +0100 *** *** 36,42 if (rc != 0) { ! printf (gethostname failed, rc %d errno %d\n, errno); return 1; } --- 36,42 if (rc != 0) { ! printf (gethostname failed, rc %d errno %d\n, rc, errno); return 1; }
(x)gethostname
Building shishi on mingw leads to: ../../../src/shishi-0.0.34/gl/xgethostname.c: In function 'xgethostname': ../../../src/shishi-0.0.34/gl/xgethostname.c:61: warning: implicit declaration of function 'gethostname' According to http://www.opengroup.org/onlinepubs/009695399/functions/gethostname.html gethostname is declared in unistd.h, but gnulib's unistd.h doesn't. MinGW declare gethostname in winsock.h/winsock2.h, so if gnulib's unistd.h would pull either of those in, I believe it would work. According to m4/sys_socket_h.m4, just checking for winsock2.h and including that file would break cygwin. It is claimed that cygwin never wants winsock2.h to be included, instead things should go via sys/socket.h. (Is this still relevant?) If I understand correctly, unistd-h could either duplicate the logic in sys_socket_h to make sure winsock2.h isn't pulled in on cygwin, or unistd could simply depend on sys_socket_h and #include sys/socket.h in unistd.in.h. The sys_socket module seems rather light-weight now. So how about the patch below? It probably needs to be revisited if the sys_socket module grows significantly. /Simon 2008-01-16 Simon Josefsson [EMAIL PROTECTED] * lib/unistd.in.h: Include sys/socket.h, to get to winsock2.h, which declares gethostname on MinGW. * modules/unistd (Depends-on): Add sys_socket. diff --git a/lib/unistd.in.h b/lib/unistd.in.h index 5400c86..7220908 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -1,5 +1,5 @@ /* Substitute for and wrapper around unistd.h. - Copyright (C) 2004-2007 Free Software Foundation, Inc. + Copyright (C) 2004-2008 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 @@ -33,6 +33,11 @@ /* mingw fails to declare _exit in unistd.h. */ #include stdlib.h +/* mingw fails to declare gethostname in unistd.h, but declare it in + winsock2.h. Cygwin should not include winsock2.h, instead + winsock2.h access needs to go via sys/socket.h, so we do that. */ +#include sys/socket.h + /* The definition of GL_LINK_WARNING is copied here. */ diff --git a/modules/unistd b/modules/unistd index 4d16cf3..b81ce90 100644 --- a/modules/unistd +++ b/modules/unistd @@ -8,6 +8,7 @@ lib/unistd.in.h Depends-on: include_next link-warning +sys_socket configure.ac: gl_UNISTD_H