bug-gnulib
[Top][All Lists]
Advanced

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

Re: [libvirt] [PATCH libvirt 6/6] Cast timeval.tv_sec long to localtime


From: Bruno Haible
Subject: Re: [libvirt] [PATCH libvirt 6/6] Cast timeval.tv_sec long to localtime expected type time_t
Date: Sat, 28 Jan 2012 18:14:01 +0100
User-agent: KMail/4.7.4 (Linux/3.1.0-1.2-desktop; KDE/4.7.4; x86_64; ; )

> > Should gnulib redefine this struct similarly to other incompatible types?
> 
> Yes, this will make sense. As far as I can see, it's a manageable task:
> Only the replacements of <sys/time.h>, <sys/select.h>, select(),
> gettimeofday(), getsockopt(), setsockopt() will need work.

This patch implements it. Tested on mingw, mingw64, MSVC 9 (all in 32-bit
mode). Please give it some testing in 64-bit mode.


2012-01-28  Bruno Haible  <address@hidden>

        sys_time: Override 'struct timeval' on some native Windows platforms.
        * m4/sys_time_h.m4 (gl_HEADER_SYS_TIME_H_BODY): Test whether tv_sec
        has the right type. Set REPLACE_STRUCT_TIMEVAL if not.
        (gl_HEADER_SYS_TIME_H_DEFAULTS): Initialize REPLACE_STRUCT_TIMEVAL.
        * lib/sys_time.in.h: Include <winsock2.h> also when 'struct timeval'
        needs to be overridden.
        (timeval): Override if REPLACE_STRUCT_TIMEVAL is set.
        * modules/sys_time (Makefile.am): Substitute REPLACE_STRUCT_TIMEVAL.
        * tests/test-sys_select.c: Check that the tv_sec member has the same
        size as a 'time_t'.
        * tests/test-sys_time.c: Likewise.
        * m4/gettimeofday.m4 (gl_FUNC_GETTIMEOFDAY): If REPLACE_STRUCT_TIMEVAL
        is set, set also REPLACE_GETTIMEOFDAY.
        * lib/gettimeofday.c (gettimeofday): If 'struct timeval' is overridden,
        convert the resulting 'struct timeval' before returning.
        * lib/select.c: Include <sys/time.h>.
        (select, timeval): Undefine at the right place.
        * modules/select (Depends-on): Add sys_time.
        * doc/posix-headers/sys_time.texi: Mention the problem with tv_sec on
        some Windows platforms.
        Reported by Marc-André Lureau <address@hidden>.

--- doc/posix-headers/sys_time.texi.orig        Sat Jan 28 18:01:44 2012
+++ doc/posix-headers/sys_time.texi     Sat Jan 28 16:47:32 2012
@@ -12,6 +12,13 @@
 MSVC 9.
 @item
 @samp{struct timeval} is not defined on some platforms.
address@hidden
address@hidden timeval} is defined with an incompatible type for @code{tv_sec}
+on some native Windows platforms:
+mingw64 in 64-bit mode,
+mingw64 in 32-bit mode when @code{__MINGW_USE_VC2005_COMPAT} is defined,
+MSVC 9 in 64-bit mode,
+MSVC 9 in 32-bit mode when @code{_USE_32BIT_TIME_T} is not defined.
 @end itemize
 
 Portability problems not fixed by Gnulib:
--- lib/gettimeofday.c.orig     Sat Jan 28 18:01:45 2012
+++ lib/gettimeofday.c  Sat Jan 28 16:44:00 2012
@@ -110,7 +110,18 @@
   struct tm save = *localtime_buffer_addr;
 # endif
 
+# if defined timeval /* 'struct timeval' overridden by gnulib?  */
+#  undef timeval
+  struct timeval otv;
+  int result = gettimeofday (&otv, (struct timezone *) tz);
+  if (result == 0)
+    {
+      tv->tv_sec = otv.tv_sec;
+      tv->tv_usec = otv.tv_usec;
+    }
+# else
   int result = gettimeofday (tv, (struct timezone *) tz);
+# endif
 
 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
   *localtime_buffer_addr = save;
--- lib/select.c.orig   Sat Jan 28 18:01:45 2012
+++ lib/select.c        Sat Jan 28 17:58:55 2012
@@ -37,8 +37,13 @@
 #include <conio.h>
 #include <time.h>
 
+/* Get the overridden 'struct timeval'.  */
+#include <sys/time.h>
+
 #include "msvc-nothrow.h"
 
+#undef select
+
 struct bitset {
   unsigned char in[FD_SETSIZE / CHAR_BIT];
   unsigned char out[FD_SETSIZE / CHAR_BIT];
@@ -237,6 +242,7 @@
 int
 rpl_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds,
             struct timeval *timeout)
+#undef timeval
 {
   static struct timeval tv0;
   static HANDLE hEvent;
--- lib/sys_time.in.h.orig      Sat Jan 28 18:01:45 2012
+++ lib/sys_time.in.h   Sat Jan 28 16:48:53 2012
@@ -40,9 +40,11 @@
 #  include <time.h>
 # endif
 
-/* On native Windows with MSVC:
-   Get the 'struct timeval' type.  */
-# if defined _MSC_VER && @HAVE_WINSOCK2_H@ && !defined _GL_INCLUDING_WINSOCK2_H
+/* On native Windows with MSVC, get the 'struct timeval' type.
+   Also, on native Windows with a 64-bit time_t, where we are overriding the
+   'struct timeval' type, get all declarations of system functions whose
+   signature contains 'struct timeval'.  */
+# if (defined _MSC_VER || @REPLACE_STRUCT_TIMEVAL@) && @HAVE_WINSOCK2_H@ && 
!defined _GL_INCLUDING_WINSOCK2_H
 #  define _GL_INCLUDING_WINSOCK2_H
 #  include <winsock2.h>
 #  undef _GL_INCLUDING_WINSOCK2_H
@@ -58,7 +60,11 @@
 extern "C" {
 # endif
 
-# if ! @HAVE_STRUCT_TIMEVAL@
+# if address@hidden@ || @REPLACE_STRUCT_TIMEVAL@
+
+#  if @REPLACE_STRUCT_TIMEVAL@
+#   define timeval rpl_timeval
+#  endif
 
 #  if !GNULIB_defined_struct_timeval
 struct timeval
--- m4/gettimeofday.m4.orig     Sat Jan 28 18:01:45 2012
+++ m4/gettimeofday.m4  Sat Jan 28 17:19:51 2012
@@ -1,4 +1,4 @@
-# serial 17
+# serial 18
 
 # Copyright (C) 2001-2003, 2005, 2007, 2009-2012 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
@@ -50,6 +50,10 @@
     elif test $gl_cv_func_gettimeofday_posix_signature != yes; then
       REPLACE_GETTIMEOFDAY=1
     fi
+    dnl If we override 'struct timeval', we also have to override gettimeofday.
+    if test $REPLACE_STRUCT_TIMEVAL = 1; then
+      REPLACE_GETTIMEOFDAY=1
+    fi
     m4_ifdef([gl_FUNC_TZSET_CLOBBER], [
       gl_FUNC_TZSET_CLOBBER
       if test $gl_cv_func_tzset_clobber = yes; then
--- m4/sys_time_h.m4.orig       Sat Jan 28 18:01:45 2012
+++ m4/sys_time_h.m4    Sat Jan 28 17:12:24 2012
@@ -1,5 +1,5 @@
 # Configure a replacement for <sys/time.h>.
-# serial 7
+# serial 8
 
 # Copyright (C) 2007, 2009-2012 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
@@ -43,9 +43,36 @@
           ]],
           [[static struct timeval x; x.tv_sec = x.tv_usec;]])],
        [gl_cv_sys_struct_timeval=yes],
-       [gl_cv_sys_struct_timeval=no])])
+       [gl_cv_sys_struct_timeval=no])
+    ])
   if test $gl_cv_sys_struct_timeval != yes; then
     HAVE_STRUCT_TIMEVAL=0
+  else
+    dnl On native Windows with a 64-bit 'time_t', 'struct timeval' is defined
+    dnl (in <sys/time.h> and <winsock2.h> for mingw64, in <winsock2.h> only
+    dnl for MSVC) with a tv_sec field of type 'long' (32-bit!), which is
+    dnl smaller than the 'time_t' type mandated by POSIX.
+    AC_CACHE_CHECK([for correct struct timeval.tv_sec member],
+      [gl_cv_sys_struct_timeval_tv_sec],
+      [AC_COMPILE_IFELSE(
+         [AC_LANG_PROGRAM(
+            [[#if HAVE_SYS_TIME_H
+               #include <sys/time.h>
+              #endif
+              #include <time.h>
+              #if HAVE_WINSOCK2_H
+              # include <winsock2.h>
+              #endif
+            ]],
+            [[static struct timeval x;
+              typedef int verify_tv_sec_type[sizeof (x.tv_sec) == sizeof 
(time_t) ? 1 : -1];
+            ]])],
+         [gl_cv_sys_struct_timeval_tv_sec=yes],
+         [gl_cv_sys_struct_timeval_tv_sec=no])
+      ])
+    if test $gl_cv_sys_struct_timeval_tv_sec != yes; then
+      REPLACE_STRUCT_TIMEVAL=1
+    fi
   fi
 
   dnl Check for declarations of anything we want to poison if the
@@ -75,4 +102,5 @@
   HAVE_STRUCT_TIMEVAL=1;     AC_SUBST([HAVE_STRUCT_TIMEVAL])
   HAVE_SYS_TIME_H=1;         AC_SUBST([HAVE_SYS_TIME_H])
   REPLACE_GETTIMEOFDAY=0;    AC_SUBST([REPLACE_GETTIMEOFDAY])
+  REPLACE_STRUCT_TIMEVAL=0;  AC_SUBST([REPLACE_STRUCT_TIMEVAL])
 ])
--- modules/select.orig Sat Jan 28 18:01:45 2012
+++ modules/select      Sat Jan 28 16:44:00 2012
@@ -9,6 +9,7 @@
 sys_select
 alloca          [test $REPLACE_SELECT = 1]
 sockets         [test $REPLACE_SELECT = 1]
+sys_time        [test $REPLACE_SELECT = 1]
 msvc-nothrow    [test $REPLACE_SELECT = 1]
 
 configure.ac:
--- modules/sys_time.orig       Sat Jan 28 18:01:45 2012
+++ modules/sys_time    Sat Jan 28 16:44:00 2012
@@ -36,6 +36,7 @@
              -e 's/@''HAVE_GETTIMEOFDAY''@/$(HAVE_GETTIMEOFDAY)/g' \
              -e 's/@''HAVE_STRUCT_TIMEVAL''@/$(HAVE_STRUCT_TIMEVAL)/g' \
              -e 's/@''REPLACE_GETTIMEOFDAY''@/$(REPLACE_GETTIMEOFDAY)/g' \
+             -e 's/@''REPLACE_STRUCT_TIMEVAL''@/$(REPLACE_STRUCT_TIMEVAL)/g' \
              -e '/definitions of _GL_FUNCDECL_RPL/r $(CXXDEFS_H)' \
              -e '/definition of _GL_ARG_NONNULL/r $(ARG_NONNULL_H)' \
              -e '/definition of _GL_WARN_ON_USE/r $(WARN_ON_USE_H)' \
--- tests/test-sys_select.c.orig        Sat Jan 28 18:01:45 2012
+++ tests/test-sys_select.c     Sat Jan 28 14:51:07 2012
@@ -38,7 +38,10 @@
 #endif
 
 /* Check that the 'struct timeval' type is defined.  */
-struct timeval t1;
+struct timeval a;
+
+/* Check that &a.tv_sec is a 'time_t *', ignoring signedness issues.  */
+typedef int verify_tv_sec_type[sizeof (a.tv_sec) == sizeof (time_t) ? 1 : -1];
 
 /* Check that sigset_t is defined.  */
 sigset_t t2;
--- tests/test-sys_time.c.orig  Sat Jan 28 18:01:45 2012
+++ tests/test-sys_time.c       Sat Jan 28 14:51:12 2012
@@ -20,8 +20,12 @@
 
 #include <sys/time.h>
 
+/* Check that the 'struct timeval' type is defined.  */
 struct timeval a;
 
+/* Check that &a.tv_sec is a 'time_t *', ignoring signedness issues.  */
+typedef int verify_tv_sec_type[sizeof (a.tv_sec) == sizeof (time_t) ? 1 : -1];
+
 int
 main (void)
 {




reply via email to

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