bug-gnulib
[Top][All Lists]
Advanced

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

gethostname, socket need startup code


From: Bruno Haible
Subject: gethostname, socket need startup code
Date: Sun, 2 Aug 2009 16:45:07 +0200
User-agent: KMail/1.9.9

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.


2009-08-02  Bruno Haible  <address@hidden>

        Allow multiple calls to gl_sockets_startup.
        * lib/sockets.c (initialized_sockets_version): New variable.
        (gl_sockets_startup): Do nothing if already called for this or a higher
        version.
        (gl_sockets_cleanup): Reset initialized_sockets_version.

*** lib/sockets.c.orig  2009-08-02 16:35:41.000000000 +0200
--- lib/sockets.c       2009-08-02 16:31:19.000000000 +0200
***************
*** 69,91 ****
  
  static struct close_hook close_sockets_hook;
  
  #endif
  
  int
  gl_sockets_startup (int version)
  {
  #if WINDOWS_SOCKETS
!   WSADATA data;
!   int err;
  
!   err = WSAStartup (version, &data);
!   if (err != 0)
!     return 1;
  
!   if (data.wVersion < version)
!     return 2;
  
!   register_close_hook (close_fd_maybe_socket, &close_sockets_hook);
  #endif
  
    return 0;
--- 69,99 ----
  
  static struct close_hook close_sockets_hook;
  
+ static int initialized_sockets_version /* = 0 */;
+ 
  #endif
  
  int
  gl_sockets_startup (int version)
  {
  #if WINDOWS_SOCKETS
!   if (version > initialized_sockets_version)
!     {
!       WSADATA data;
!       int err;
! 
!       err = WSAStartup (version, &data);
!       if (err != 0)
!       return 1;
  
!       if (data.wVersion < version)
!       return 2;
  
!       if (initialized_sockets_version == 0)
!       register_close_hook (close_fd_maybe_socket, &close_sockets_hook);
  
!       initialized_sockets_version = version;
!     }
  #endif
  
    return 0;
***************
*** 97,102 ****
--- 105,112 ----
  #if WINDOWS_SOCKETS
    int err;
  
+   initialized_sockets_version = 0;
+ 
    unregister_close_hook (&close_sockets_hook);
  
    err = WSACleanup ();


2009-08-02  Bruno Haible  <address@hidden>

        Implicitly initialize the sockets library.
        * lib/gethostname.c: Include sockets.h.
        (rpl_gethostname): Invoke gl_sockets_startup.
        * lib/socket.c: Include sockets.h.
        (rpl_socket): Invoke gl_sockets_startup.
        * modules/gethostname (Depends-on): Add sockets.
        * modules/socket (Depends-on): Likewise.
        * tests/test-poll.c: Don't include sockets.h.
        (main): Don't invoke gl_sockets_startup.
        * tests/test-select.c: Don't include sockets.h.
        (main): Don't invoke gl_sockets_startup.

--- lib/gethostname.c.orig      2009-08-02 16:43:05.000000000 +0200
+++ lib/gethostname.c   2009-08-02 16:10:57.000000000 +0200
@@ -79,6 +79,8 @@
 /* Get set_winsock_errno. */
 #include "w32sock.h"
 
+#include "sockets.h"
+
 #undef gethostname
 
 int
@@ -88,6 +90,7 @@
 
   if (len > INT_MAX)
     len = INT_MAX;
+  gl_sockets_startup (SOCKETS_1_1);
   r = gethostname (name, (int) len);
   if (r < 0)
     set_winsock_errno ();
--- lib/socket.c.orig   2009-08-02 16:43:05.000000000 +0200
+++ lib/socket.c        2009-08-02 16:23:24.000000000 +0200
@@ -1,6 +1,6 @@
 /* socket.c --- wrappers for Windows socket function
 
-   Copyright (C) 2008 Free Software Foundation, Inc.
+   Copyright (C) 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
@@ -26,12 +26,18 @@
 /* Get set_winsock_errno, FD_TO_SOCKET etc. */
 #include "w32sock.h"
 
+#include "sockets.h"
+
 int
 rpl_socket (int domain, int type, int protocol)
 {
+  SOCKET fh;
+
+  gl_sockets_startup (SOCKETS_1_1);
+
   /* We have to use WSASocket() to create non-overlapped IO sockets.
      Overlapped IO sockets cannot be used with read/write.  */
-  SOCKET fh = WSASocket (domain, type, protocol, NULL, 0, 0);
+  fh = WSASocket (domain, type, protocol, NULL, 0, 0);
 
   if (fh == INVALID_SOCKET)
     {
--- modules/gethostname.orig    2009-08-02 16:43:05.000000000 +0200
+++ modules/gethostname 2009-08-02 16:16:26.000000000 +0200
@@ -10,6 +10,7 @@
 unistd
 sys_socket
 errno
+sockets
 
 configure.ac:
 gl_FUNC_GETHOSTNAME
--- modules/socket.orig 2009-08-02 16:43:05.000000000 +0200
+++ modules/socket      2009-08-02 16:16:35.000000000 +0200
@@ -8,6 +8,7 @@
 Depends-on:
 sys_socket
 errno
+sockets
 
 configure.ac:
 AC_REQUIRE([gl_HEADER_SYS_SOCKET])
--- tests/test-poll.c.orig      2009-08-02 16:43:05.000000000 +0200
+++ tests/test-poll.c   2009-08-02 16:13:10.000000000 +0200
@@ -1,5 +1,5 @@
 /* Test of poll() function.
-   Copyright (C) 2008 Free Software Foundation, Inc.
+   Copyright (C) 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
@@ -30,7 +30,6 @@
 #include <stdbool.h>
 #include <sys/ioctl.h>
 #include <errno.h>
-#include "sockets.h"
 
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
 # define WIN32_NATIVE
@@ -360,8 +359,6 @@
 {
   int result;
 
-  gl_sockets_startup (SOCKETS_1_1);
-
 #ifdef INTERACTIVE
   printf ("Please press Enter\n");
   test (test_tty, "TTY");
--- tests/test-select.c.orig    2009-08-02 16:43:05.000000000 +0200
+++ tests/test-select.c 2009-08-02 16:13:26.000000000 +0200
@@ -1,5 +1,5 @@
 /* Test of select() substitute.
-   Copyright (C) 2008 Free Software Foundation, Inc.
+   Copyright (C) 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
@@ -29,7 +29,6 @@
 #include <stdbool.h>
 #include <sys/ioctl.h>
 #include <errno.h>
-#include "sockets.h"
 
 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
 
@@ -360,8 +359,6 @@
 {
   int result;
 
-  gl_sockets_startup (SOCKETS_1_1);
-
 #ifdef INTERACTIVE
   printf ("Please press Enter\n");
   test (test_tty, "TTY");




reply via email to

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