Longstanding __USE_W32_SOCKETS hiccup.

Dave Korn dave.korn.cygwin@googlemail.com
Thu Aug 6 12:46:00 GMT 2009

  Hi all, just wanted to check what folks here think about this before I go
about sending it upstream to newlib.

  When we define __USE_W32_SOCKETS, it's supposed to keep enough posix-y stuff
out of the way to allow use of the winsock API.  All of sys/types.h,
sys/unistd.h and sys/select.h check for this macro and avoid defining clashing
structures and functions if it's present.

  Some of them also check for the _WINSOCK_H header guard, but that's a
last-resort, as it only works if winsock2.h has been included earlier than the
sys/*.h header, and that's often not going to be the case.

  There is one exception: sys/time.h wraps the timeval struct definition only in
a check of _WINSOCK_H, and so unless you #include winsock2.h first, you get a
redefinition error regardless of __USE_W32_SOCKETS.  This has been the cause of
the occasional bug report down the years; trivial test case:

> $ cat tc.c
> #define __USE_W32_SOCKETS
> #include <sys/time.h>
> #include <winsock2.h>
> int main (int argc, const char **argv)
> {
>   return 0;
> }
> $ gcc-4 -g -O2 tc.c -o tc
> In file included from tc.c:5:
> /usr/lib/gcc/i686-pc-cygwin/4.3.2/../../../../i686-pc-cygwin/lib/../include/w32api/winsock2.h:109:
> error: redefinition of 'struct timeval'>
> $

  The attached patch is my suggestion to fix this problem.  There is one little
bit of fallout; since struct itimerval requires struct timeval, if we're using
the winsock version of timeval (as indicated by __USE_W32_SOCKETS), and we
haven't already included winsock2.h, we can't define struct itimerval.  I don't
see how there could be any code relying on those definitions which wouldn't
already fail to compile because of the redefinition of struct timeval, but I
thought I should point it out.

  Any comments?  It looks like this struct just got omitted during the initial
implementation of __USE_W32_SOCKETS.  Having this work right is needed to fix
libada in upstream GCC, where it's not always an option to "just #include
winsock first"(*).  If I don't hear any issues raised, I'll send this to newlib
in fairly short order.

(*) - Although it would be technically possible to scatter "#include
<winsock.h>" directives throughout libada just in front of anywhere that might
directly or indirectly #include <sys/time.h>, it would be hideously ugly
overkill compared to just fixing our headers to make this one little thing a bit
more idempotent.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: w32-sockets-timer-clash-fix.diff
Type: text/x-c
Size: 1478 bytes
Desc: not available
URL: <http://cygwin.com/pipermail/cygwin-developers/attachments/20090806/d37f2b55/attachment.bin>

More information about the Cygwin-developers mailing list