Fixed bug 2330 - Debian bug report: SDL2 X11 driver buffer overflow with large X11 file descriptor
authorSam Lantinga <slouken@libsdl.org>
Mon, 14 Aug 2017 20:22:19 -0700
changeset 1129644853f387017
parent 11295 f926311278e8
child 11297 905d34b4033c
Fixed bug 2330 - Debian bug report: SDL2 X11 driver buffer overflow with large X11 file descriptor

manuel.montezelo

Original bug report (note that it was against 2.0.0, it might have been fixed in between): http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=733015

--------------------------------------------------------
Package: libsdl2-2.0-0
Version: 2.0.0+dfsg1-3
Severity: normal
Tags: patch

I have occasional crashes here caused by the X11 backend of SDL2. It seems to
be caused by the X11_Pending function trying to add a high number (> 1024)
file descriptor to a fd_set before doing a select on it to avoid busy waiting
on X11 events. This causes a buffer overflow because the file descriptor is
larger (or equal) than the limit FD_SETSIZE.

Attached is a possible workaround patch.

Please also keep in mind that fd_set are also used in following files which
may have similar problems.

src/audio/bsd/SDL_bsdaudio.c
src/audio/paudio/SDL_paudio.c
src/audio/qsa/SDL_qsa_audio.c
src/audio/sun/SDL_sunaudio.c
src/joystick/linux/SDL_sysjoystick.c


--------------------------------------------------------

On Tuesday 24 December 2013 00:43:13 Sven Eckelmann wrote:
> I have occasional crashes here caused by the X11 backend of SDL2. It seems
> to be caused by the X11_Pending function trying to add a high number (>
> 1024) file descriptor to a fd_set before doing a select on it to avoid busy
> waiting on X11 events. This causes a buffer overflow because the file
> descriptor is larger (or equal) than the limit FD_SETSIZE.


I personally experienced this problem while hacking on the python bindings
package for SDL2 [1] (while doing make runtest). But it easier to reproduce in
a smaller, synthetic testcase.
CMakeLists.txt
configure
configure.in
include/SDL_config.h.cmake
include/SDL_config.h.in
src/audio/arts/SDL_artsaudio.h
src/audio/netbsd/SDL_netbsdaudio.c
src/audio/netbsd/SDL_netbsdaudio.h
src/audio/paudio/SDL_paudio.c
src/audio/paudio/SDL_paudio.h
src/audio/qsa/SDL_qsa_audio.c
src/audio/sun/SDL_sunaudio.c
src/core/linux/SDL_ime.h
src/core/linux/SDL_udev.c
src/core/linux/SDL_udev.h
src/video/wayland/SDL_waylanddatamanager.c
src/video/wayland/SDL_waylandevents.c
src/video/x11/SDL_x11events.c
     1.1 --- a/CMakeLists.txt	Mon Aug 14 20:07:30 2017 -0700
     1.2 +++ b/CMakeLists.txt	Mon Aug 14 20:22:19 2017 -0700
     1.3 @@ -662,7 +662,7 @@
     1.4              _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull
     1.5              atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp
     1.6              vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp
     1.7 -            nanosleep sysconf sysctlbyname getauxval
     1.8 +            nanosleep sysconf sysctlbyname getauxval poll
     1.9              )
    1.10        string(TOUPPER ${_FN} _UPPER)
    1.11        set(_HAVEVAR "HAVE_${_UPPER}")
     2.1 --- a/configure	Mon Aug 14 20:07:30 2017 -0700
     2.2 +++ b/configure	Mon Aug 14 20:22:19 2017 -0700
     2.3 @@ -16631,7 +16631,7 @@
     2.4  rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
     2.5  fi
     2.6  
     2.7 -    for ac_func in malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcscmp strlen strlcpy strlcat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval
     2.8 +    for ac_func in malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcscmp strlen strlcpy strlcat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval poll
     2.9  do :
    2.10    as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
    2.11  ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
    2.12 @@ -23771,6 +23771,8 @@
    2.13          if test x$use_input_events = xyes; then
    2.14              SOURCES="$SOURCES $srcdir/src/core/linux/SDL_evdev*.c"
    2.15          fi
    2.16 +        # Set up other core UNIX files
    2.17 +        SOURCES="$SOURCES $srcdir/src/core/unix/*.c"
    2.18          ;;
    2.19      *-*-cygwin* | *-*-mingw32*)
    2.20          ARCH=win32
     3.1 --- a/configure.in	Mon Aug 14 20:07:30 2017 -0700
     3.2 +++ b/configure.in	Mon Aug 14 20:22:19 2017 -0700
     3.3 @@ -268,7 +268,7 @@
     3.4          AC_DEFINE(HAVE_MPROTECT, 1, [ ])
     3.5          ]),
     3.6      )
     3.7 -    AC_CHECK_FUNCS(malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcscmp strlen strlcpy strlcat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval)
     3.8 +    AC_CHECK_FUNCS(malloc calloc realloc free getenv setenv putenv unsetenv qsort abs bcopy memset memcpy memmove wcslen wcslcpy wcslcat wcscmp strlen strlcpy strlcat strdup _strrev _strupr _strlwr strchr strrchr strstr itoa _ltoa _uitoa _ultoa strtol strtoul _i64toa _ui64toa strtoll strtoull atoi atof strcmp strncmp _stricmp strcasecmp _strnicmp strncasecmp vsscanf vsnprintf fopen64 fseeko fseeko64 sigaction setjmp nanosleep sysconf sysctlbyname getauxval poll)
     3.9  
    3.10      AC_CHECK_LIB(m, pow, [LIBS="$LIBS -lm"; EXTRA_LDFLAGS="$EXTRA_LDFLAGS -lm"])
    3.11      AC_CHECK_FUNCS(atan atan2 acos asin ceil copysign cos cosf fabs floor log pow scalbn sin sinf sqrt sqrtf tan tanf)
    3.12 @@ -3352,6 +3352,8 @@
    3.13          if test x$use_input_events = xyes; then
    3.14              SOURCES="$SOURCES $srcdir/src/core/linux/SDL_evdev*.c"
    3.15          fi       
    3.16 +        # Set up other core UNIX files
    3.17 +        SOURCES="$SOURCES $srcdir/src/core/unix/*.c"
    3.18          ;;
    3.19      *-*-cygwin* | *-*-mingw32*)
    3.20          ARCH=win32
     4.1 --- a/include/SDL_config.h.cmake	Mon Aug 14 20:07:30 2017 -0700
     4.2 +++ b/include/SDL_config.h.cmake	Mon Aug 14 20:22:19 2017 -0700
     4.3 @@ -181,6 +181,7 @@
     4.4  #cmakedefine HAVE_PTHREAD_SET_NAME_NP 1
     4.5  #cmakedefine HAVE_SEM_TIMEDWAIT 1
     4.6  #cmakedefine HAVE_GETAUXVAL 1
     4.7 +#cmakedefine HAVE_POLL 1
     4.8  
     4.9  #elif __WIN32__
    4.10  #cmakedefine HAVE_STDARG_H 1
     5.1 --- a/include/SDL_config.h.in	Mon Aug 14 20:07:30 2017 -0700
     5.2 +++ b/include/SDL_config.h.in	Mon Aug 14 20:22:19 2017 -0700
     5.3 @@ -183,6 +183,7 @@
     5.4  #undef HAVE_PTHREAD_SET_NAME_NP
     5.5  #undef HAVE_SEM_TIMEDWAIT
     5.6  #undef HAVE_GETAUXVAL
     5.7 +#undef HAVE_POLL
     5.8  
     5.9  #else
    5.10  #define HAVE_STDARG_H   1
     6.1 --- a/src/audio/arts/SDL_artsaudio.h	Mon Aug 14 20:07:30 2017 -0700
     6.2 +++ b/src/audio/arts/SDL_artsaudio.h	Mon Aug 14 20:22:19 2017 -0700
     6.3 @@ -42,7 +42,7 @@
     6.4      Uint8 *mixbuf;
     6.5      int mixlen;
     6.6  
     6.7 -    /* Support for audio timing using a timer, in addition to select() */
     6.8 +    /* Support for audio timing using a timer, in addition to SDL_IOReady() */
     6.9      float frame_ticks;
    6.10      float next_frame;
    6.11  };
     7.1 --- a/src/audio/netbsd/SDL_netbsdaudio.c	Mon Aug 14 20:07:30 2017 -0700
     7.2 +++ b/src/audio/netbsd/SDL_netbsdaudio.c	Mon Aug 14 20:22:19 2017 -0700
     7.3 @@ -38,6 +38,7 @@
     7.4  
     7.5  #include "SDL_timer.h"
     7.6  #include "SDL_audio.h"
     7.7 +#include "../../core/unix/SDL_poll.h"
     7.8  #include "../SDL_audio_c.h"
     7.9  #include "../SDL_audiodev_c.h"
    7.10  #include "SDL_netbsdaudio.h"
    7.11 @@ -134,18 +135,11 @@
    7.12              SDL_Delay(ticks);
    7.13          }
    7.14      } else {
    7.15 -        /* Use select() for audio synchronization */
    7.16 -        fd_set fdset;
    7.17 -        struct timeval timeout;
    7.18 -
    7.19 -        FD_ZERO(&fdset);
    7.20 -        FD_SET(this->hidden->audio_fd, &fdset);
    7.21 -        timeout.tv_sec = 10;
    7.22 -        timeout.tv_usec = 0;
    7.23 +        /* Use SDL_IOReady() for audio synchronization */
    7.24  #ifdef DEBUG_AUDIO
    7.25          fprintf(stderr, "Waiting for audio to get ready\n");
    7.26  #endif
    7.27 -        if (select(this->hidden->audio_fd + 1, NULL, &fdset, NULL, &timeout)
    7.28 +        if (SDL_IOReady(this->hidden->audio_fd, SDL_TRUE, 10 * 1000)
    7.29              <= 0) {
    7.30              const char *message =
    7.31                  "Audio timeout - buggy audio driver? (disabled)";
     8.1 --- a/src/audio/netbsd/SDL_netbsdaudio.h	Mon Aug 14 20:07:30 2017 -0700
     8.2 +++ b/src/audio/netbsd/SDL_netbsdaudio.h	Mon Aug 14 20:22:19 2017 -0700
     8.3 @@ -36,7 +36,7 @@
     8.4      Uint8 *mixbuf;
     8.5      int mixlen;
     8.6  
     8.7 -    /* Support for audio timing using a timer, in addition to select() */
     8.8 +    /* Support for audio timing using a timer, in addition to SDL_IOReady() */
     8.9      float frame_ticks;
    8.10      float next_frame;
    8.11  };
     9.1 --- a/src/audio/paudio/SDL_paudio.c	Mon Aug 14 20:07:30 2017 -0700
     9.2 +++ b/src/audio/paudio/SDL_paudio.c	Mon Aug 14 20:22:19 2017 -0700
     9.3 @@ -36,6 +36,7 @@
     9.4  #include "SDL_audio.h"
     9.5  #include "SDL_stdinc.h"
     9.6  #include "../SDL_audio_c.h"
     9.7 +#include "../../core/unix/SDL_poll.h"
     9.8  #include "SDL_paudio.h"
     9.9  
    9.10  /* #define DEBUG_AUDIO */
    9.11 @@ -137,44 +138,31 @@
    9.12              SDL_Delay(ticks);
    9.13          }
    9.14      } else {
    9.15 +        int timeoutMS;
    9.16          audio_buffer paud_bufinfo;
    9.17  
    9.18 -        /* Use select() for audio synchronization */
    9.19 -        struct timeval timeout;
    9.20 -        FD_ZERO(&fdset);
    9.21 -        FD_SET(this->hidden->audio_fd, &fdset);
    9.22 -
    9.23          if (ioctl(this->hidden->audio_fd, AUDIO_BUFFER, &paud_bufinfo) < 0) {
    9.24  #ifdef DEBUG_AUDIO
    9.25              fprintf(stderr, "Couldn't get audio buffer information\n");
    9.26  #endif
    9.27 -            timeout.tv_sec = 10;
    9.28 -            timeout.tv_usec = 0;
    9.29 +            timeoutMS = 10 * 1000;
    9.30          } else {
    9.31 -            long ms_in_buf = paud_bufinfo.write_buf_time;
    9.32 -            timeout.tv_sec = ms_in_buf / 1000;
    9.33 -            ms_in_buf = ms_in_buf - timeout.tv_sec * 1000;
    9.34 -            timeout.tv_usec = ms_in_buf * 1000;
    9.35 +            timeoutMS = paud_bufinfo.write_buf_time;
    9.36  #ifdef DEBUG_AUDIO
    9.37 -            fprintf(stderr,
    9.38 -                    "Waiting for write_buf_time=%ld,%ld\n",
    9.39 -                    timeout.tv_sec, timeout.tv_usec);
    9.40 +            fprintf(stderr, "Waiting for write_buf_time=%d ms\n", timeoutMS);
    9.41  #endif
    9.42          }
    9.43  
    9.44  #ifdef DEBUG_AUDIO
    9.45          fprintf(stderr, "Waiting for audio to get ready\n");
    9.46  #endif
    9.47 -        if (select(this->hidden->audio_fd + 1, NULL, &fdset, NULL, &timeout)
    9.48 -            <= 0) {
    9.49 -            const char *message =
    9.50 -                "Audio timeout - buggy audio driver? (disabled)";
    9.51 +        if (SDL_IOReady(this->hidden->audio_fd, SDL_TRUE, timeoutMS) <= 0) {
    9.52              /*
    9.53               * In general we should never print to the screen,
    9.54               * but in this case we have no other way of letting
    9.55               * the user know what happened.
    9.56               */
    9.57 -            fprintf(stderr, "SDL: %s - %s\n", strerror(errno), message);
    9.58 +            fprintf(stderr, "SDL: %s - Audio timeout - buggy audio driver? (disabled)\n", strerror(errno));
    9.59              SDL_OpenedAudioDeviceDisconnected(this);
    9.60              /* Don't try to close - may hang */
    9.61              this->hidden->audio_fd = -1;
    9.62 @@ -486,7 +474,7 @@
    9.63          return SDL_SetError("Can't start audio play");
    9.64      }
    9.65  
    9.66 -    /* Check to see if we need to use select() workaround */
    9.67 +    /* Check to see if we need to use SDL_IOReady() workaround */
    9.68      if (workaround != NULL) {
    9.69          this->hidden->frame_ticks = (float) (this->spec.samples * 1000) /
    9.70              this->spec.freq;
    10.1 --- a/src/audio/paudio/SDL_paudio.h	Mon Aug 14 20:07:30 2017 -0700
    10.2 +++ b/src/audio/paudio/SDL_paudio.h	Mon Aug 14 20:22:19 2017 -0700
    10.3 @@ -37,7 +37,7 @@
    10.4      Uint8 *mixbuf;
    10.5      int mixlen;
    10.6  
    10.7 -    /* Support for audio timing using a timer, in addition to select() */
    10.8 +    /* Support for audio timing using a timer, in addition to SDL_IOReady() */
    10.9      float frame_ticks;
   10.10      float next_frame;
   10.11  };
    11.1 --- a/src/audio/qsa/SDL_qsa_audio.c	Mon Aug 14 20:07:30 2017 -0700
    11.2 +++ b/src/audio/qsa/SDL_qsa_audio.c	Mon Aug 14 20:22:19 2017 -0700
    11.3 @@ -45,6 +45,7 @@
    11.4  
    11.5  #include "SDL_timer.h"
    11.6  #include "SDL_audio.h"
    11.7 +#include "../../core/unix/SDL_poll.h"
    11.8  #include "../SDL_audio_c.h"
    11.9  #include "SDL_qsa_audio.h"
   11.10  
   11.11 @@ -113,67 +114,25 @@
   11.12  static void
   11.13  QSA_WaitDevice(_THIS)
   11.14  {
   11.15 -    fd_set wfds;
   11.16 -    fd_set rfds;
   11.17 -    int selectret;
   11.18 -    struct timeval timeout;
   11.19 +    int result;
   11.20  
   11.21 -    if (!this->hidden->iscapture) {
   11.22 -        FD_ZERO(&wfds);
   11.23 -        FD_SET(this->hidden->audio_fd, &wfds);
   11.24 -    } else {
   11.25 -        FD_ZERO(&rfds);
   11.26 -        FD_SET(this->hidden->audio_fd, &rfds);
   11.27 +    /* Setup timeout for playing one fragment equal to 2 seconds          */
   11.28 +    /* If timeout occured than something wrong with hardware or driver    */
   11.29 +    /* For example, Vortex 8820 audio driver stucks on second DAC because */
   11.30 +    /* it doesn't exist !                                                 */
   11.31 +    result = SDL_IOReady(this->hidden->audio_fd, !this->hidden->iscapture, 2 * 1000);
   11.32 +    switch (result) {
   11.33 +    case -1:
   11.34 +        SDL_SetError("QSA: SDL_IOReady() failed: %s", strerror(errno));
   11.35 +        break;
   11.36 +    case 0:
   11.37 +        SDL_SetError("QSA: timeout on buffer waiting occured");
   11.38 +        this->hidden->timeout_on_wait = 1;
   11.39 +        break;
   11.40 +    default:
   11.41 +        this->hidden->timeout_on_wait = 0;
   11.42 +        break;
   11.43      }
   11.44 -
   11.45 -    do {
   11.46 -        /* Setup timeout for playing one fragment equal to 2 seconds          */
   11.47 -        /* If timeout occured than something wrong with hardware or driver    */
   11.48 -        /* For example, Vortex 8820 audio driver stucks on second DAC because */
   11.49 -        /* it doesn't exist !                                                 */
   11.50 -        timeout.tv_sec = 2;
   11.51 -        timeout.tv_usec = 0;
   11.52 -        this->hidden->timeout_on_wait = 0;
   11.53 -
   11.54 -        if (!this->hidden->iscapture) {
   11.55 -            selectret =
   11.56 -                select(this->hidden->audio_fd + 1, NULL, &wfds, NULL,
   11.57 -                       &timeout);
   11.58 -        } else {
   11.59 -            selectret =
   11.60 -                select(this->hidden->audio_fd + 1, &rfds, NULL, NULL,
   11.61 -                       &timeout);
   11.62 -        }
   11.63 -
   11.64 -        switch (selectret) {
   11.65 -        case -1:
   11.66 -            {
   11.67 -                SDL_SetError("QSA: select() failed: %s", strerror(errno));
   11.68 -                return;
   11.69 -            }
   11.70 -            break;
   11.71 -        case 0:
   11.72 -            {
   11.73 -                SDL_SetError("QSA: timeout on buffer waiting occured");
   11.74 -                this->hidden->timeout_on_wait = 1;
   11.75 -                return;
   11.76 -            }
   11.77 -            break;
   11.78 -        default:
   11.79 -            {
   11.80 -                if (!this->hidden->iscapture) {
   11.81 -                    if (FD_ISSET(this->hidden->audio_fd, &wfds)) {
   11.82 -                        return;
   11.83 -                    }
   11.84 -                } else {
   11.85 -                    if (FD_ISSET(this->hidden->audio_fd, &rfds)) {
   11.86 -                        return;
   11.87 -                    }
   11.88 -                }
   11.89 -            }
   11.90 -            break;
   11.91 -        }
   11.92 -    } while (1);
   11.93  }
   11.94  
   11.95  static void
    12.1 --- a/src/audio/sun/SDL_sunaudio.c	Mon Aug 14 20:07:30 2017 -0700
    12.2 +++ b/src/audio/sun/SDL_sunaudio.c	Mon Aug 14 20:22:19 2017 -0700
    12.3 @@ -40,6 +40,7 @@
    12.4  
    12.5  #include "SDL_timer.h"
    12.6  #include "SDL_audio.h"
    12.7 +#include "../../core/unix/SDL_poll.h"
    12.8  #include "../SDL_audio_c.h"
    12.9  #include "../SDL_audiodev_c.h"
   12.10  #include "SDL_sunaudio.h"
   12.11 @@ -97,11 +98,7 @@
   12.12          }
   12.13      }
   12.14  #else
   12.15 -    fd_set fdset;
   12.16 -
   12.17 -    FD_ZERO(&fdset);
   12.18 -    FD_SET(this->hidden->audio_fd, &fdset);
   12.19 -    select(this->hidden->audio_fd + 1, NULL, &fdset, NULL, NULL);
   12.20 +    SDL_IOReady(this->hidden->audio_fd, SDL_TRUE, -1);
   12.21  #endif
   12.22  }
   12.23  
    13.1 --- a/src/core/linux/SDL_ime.h	Mon Aug 14 20:07:30 2017 -0700
    13.2 +++ b/src/core/linux/SDL_ime.h	Mon Aug 14 20:22:19 2017 -0700
    13.3 @@ -36,3 +36,5 @@
    13.4  extern void SDL_IME_PumpEvents(void);
    13.5  
    13.6  #endif /* SDL_ime_h_ */
    13.7 +
    13.8 +/* vi: set ts=4 sw=4 expandtab: */
    14.1 --- a/src/core/linux/SDL_udev.c	Mon Aug 14 20:07:30 2017 -0700
    14.2 +++ b/src/core/linux/SDL_udev.c	Mon Aug 14 20:22:19 2017 -0700
    14.3 @@ -31,7 +31,10 @@
    14.4  
    14.5  #include <linux/input.h>
    14.6  
    14.7 -#include "SDL.h"
    14.8 +#include "SDL_assert.h"
    14.9 +#include "SDL_loadso.h"
   14.10 +#include "SDL_timer.h"
   14.11 +#include "../unix/SDL_poll.h"
   14.12  
   14.13  static const char *SDL_UDEV_LIBS[] = {
   14.14  #ifdef SDL_UDEV_DYNAMIC
   14.15 @@ -105,14 +108,7 @@
   14.16  {
   14.17      if (_this->udev_mon != NULL) {
   14.18          const int fd = _this->udev_monitor_get_fd(_this->udev_mon);
   14.19 -        fd_set fds;
   14.20 -        struct timeval tv;
   14.21 -
   14.22 -        FD_ZERO(&fds);
   14.23 -        FD_SET(fd, &fds);
   14.24 -        tv.tv_sec = 0;
   14.25 -        tv.tv_usec = 0;
   14.26 -        if ((select(fd+1, &fds, NULL, NULL, &tv) > 0) && (FD_ISSET(fd, &fds))) {
   14.27 +        if (SDL_IOReady(fd, SDL_FALSE, 0)) {
   14.28              return SDL_TRUE;
   14.29          }
   14.30      }
    15.1 --- a/src/core/linux/SDL_udev.h	Mon Aug 14 20:07:30 2017 -0700
    15.2 +++ b/src/core/linux/SDL_udev.h	Mon Aug 14 20:22:19 2017 -0700
    15.3 @@ -117,3 +117,5 @@
    15.4  #endif /* HAVE_LIBUDEV_H */
    15.5  
    15.6  #endif /* SDL_udev_h_ */
    15.7 +
    15.8 +/* vi: set ts=4 sw=4 expandtab: */
    16.1 --- a/src/video/wayland/SDL_waylanddatamanager.c	Mon Aug 14 20:07:30 2017 -0700
    16.2 +++ b/src/video/wayland/SDL_waylanddatamanager.c	Mon Aug 14 20:22:19 2017 -0700
    16.3 @@ -30,6 +30,7 @@
    16.4  
    16.5  #include "SDL_stdinc.h"
    16.6  #include "SDL_assert.h"
    16.7 +#include "../../core/unix/SDL_poll.h"
    16.8  
    16.9  #include "SDL_waylandvideo.h"
   16.10  #include "SDL_waylanddatamanager.h"
   16.11 @@ -46,16 +47,8 @@
   16.12      sigset_t sig_set;
   16.13      sigset_t old_sig_set;
   16.14      struct timespec zerotime = {0};
   16.15 -    fd_set set;
   16.16 -    struct timeval timeout;
   16.17  
   16.18 -    FD_ZERO(&set);
   16.19 -    FD_SET(fd, &set);
   16.20 -
   16.21 -    timeout.tv_sec = 1;
   16.22 -    timeout.tv_usec = 0;
   16.23 -
   16.24 -    ready = select(fd + 1, NULL, &set, NULL, &timeout);
   16.25 +    ready = SDL_IOReady(fd, SDL_TRUE, 1 * 1000);
   16.26  
   16.27      sigemptyset(&sig_set);
   16.28      sigaddset(&sig_set, SIGPIPE);  
   16.29 @@ -92,16 +85,7 @@
   16.30      ssize_t bytes_read = 0;
   16.31      size_t pos = 0;
   16.32  
   16.33 -    fd_set set;
   16.34 -    struct timeval timeout;
   16.35 -
   16.36 -    FD_ZERO(&set);
   16.37 -    FD_SET(fd, &set);
   16.38 -
   16.39 -    timeout.tv_sec = 1;
   16.40 -    timeout.tv_usec = 0;
   16.41 -
   16.42 -    ready = select(fd + 1, &set, NULL, NULL, &timeout);  
   16.43 +    ready = SDL_IOReady(fd, SDL_FALSE, 1 * 1000);
   16.44    
   16.45      if (ready == 0) {
   16.46          bytes_read = SDL_SetError("Pipe timeout");
    17.1 --- a/src/video/wayland/SDL_waylandevents.c	Mon Aug 14 20:07:30 2017 -0700
    17.2 +++ b/src/video/wayland/SDL_waylandevents.c	Mon Aug 14 20:22:19 2017 -0700
    17.3 @@ -27,6 +27,7 @@
    17.4  #include "SDL_assert.h"
    17.5  #include "SDL_log.h"
    17.6  
    17.7 +#include "../../core/unix/SDL_poll.h"
    17.8  #include "../../events/SDL_sysevents.h"
    17.9  #include "../../events/SDL_events_c.h"
   17.10  #include "../../events/scancodes_xfree86.h"
   17.11 @@ -74,16 +75,14 @@
   17.12  Wayland_PumpEvents(_THIS)
   17.13  {
   17.14      SDL_VideoData *d = _this->driverdata;
   17.15 -    struct pollfd pfd[1];
   17.16  
   17.17 -    pfd[0].fd = WAYLAND_wl_display_get_fd(d->display);
   17.18 -    pfd[0].events = POLLIN;
   17.19 -    poll(pfd, 1, 0);
   17.20 -
   17.21 -    if (pfd[0].revents & POLLIN)
   17.22 +    if (SDL_IOReady(WAYLAND_wl_display_get_fd(d->display), SDL_FALSE, 0)) {
   17.23          WAYLAND_wl_display_dispatch(d->display);
   17.24 +    }
   17.25      else
   17.26 +    {
   17.27          WAYLAND_wl_display_dispatch_pending(d->display);
   17.28 +    }
   17.29  }
   17.30  
   17.31  static void
    18.1 --- a/src/video/x11/SDL_x11events.c	Mon Aug 14 20:07:30 2017 -0700
    18.2 +++ b/src/video/x11/SDL_x11events.c	Mon Aug 14 20:22:19 2017 -0700
    18.3 @@ -31,6 +31,7 @@
    18.4  #include "SDL_x11video.h"
    18.5  #include "SDL_x11touch.h"
    18.6  #include "SDL_x11xinput2.h"
    18.7 +#include "../../core/unix/SDL_poll.h"
    18.8  #include "../../events/SDL_events_c.h"
    18.9  #include "../../events/SDL_mouse_c.h"
   18.10  #include "../../events/SDL_touch_c.h"
   18.11 @@ -1409,17 +1410,8 @@
   18.12      }
   18.13  
   18.14      /* More drastic measures are required -- see if X is ready to talk */
   18.15 -    {
   18.16 -        static struct timeval zero_time;        /* static == 0 */
   18.17 -        int x11_fd;
   18.18 -        fd_set fdset;
   18.19 -
   18.20 -        x11_fd = ConnectionNumber(display);
   18.21 -        FD_ZERO(&fdset);
   18.22 -        FD_SET(x11_fd, &fdset);
   18.23 -        if (select(x11_fd + 1, &fdset, NULL, NULL, &zero_time) == 1) {
   18.24 -            return (X11_XPending(display));
   18.25 -        }
   18.26 +    if (SDL_IOReady(ConnectionNumber(display), SDL_FALSE, 0)) {
   18.27 +        return (X11_XPending(display));
   18.28      }
   18.29  
   18.30      /* Oh well, nothing is ready .. */