>From 850301bc8adf01b4e9008ffd38b0048b720bea99 Mon Sep 17 00:00:00 2001
From: KO Myung-Hun <komh78@gmail.com>
Date: Mon, 7 Sep 2015 15:56:39 +0900
Subject: [PATCH] os2: thread: set POLLNVAL instead of returning -1 if not a
 socket

---
 src/os2/thread.c | 35 ++++++++++++++++++++++++++++-------
 1 file changed, 28 insertions(+), 7 deletions(-)

diff --git a/src/os2/thread.c b/src/os2/thread.c
index e221632..8f8802a 100644
--- a/src/os2/thread.c
+++ b/src/os2/thread.c
@@ -785,6 +785,7 @@ int vlc_poll_os2( struct pollfd *fds, unsigned nfds, int timeout )
     fd_set rdset, wrset, exset;
 
     struct timeval tv = { 0, 0 };
+    struct timeval *ptv = NULL;
 
     int val = -1;
 
@@ -794,6 +795,19 @@ int vlc_poll_os2( struct pollfd *fds, unsigned nfds, int timeout )
     for( unsigned i = 0; i < nfds; i++ )
     {
         int fd = fds[ i ].fd;
+
+        fds[ i ].revents = 0;
+        if( getsockopt( fd, SOL_SOCKET, SO_TYPE,
+                        &( int ){ 0 }, &( int ){ sizeof( int )}) == -1 &&
+            ( errno == ENOTSOCK || errno == EBADF ))
+        {
+            fds[ i ].revents = POLLNVAL;
+            while( ++i < nfds )
+                fds[ i ].revents = 0;
+
+            return 1;
+        }
+
         if( val < fd )
             val = fd;
 
@@ -816,19 +830,26 @@ int vlc_poll_os2( struct pollfd *fds, unsigned nfds, int timeout )
         div_t d    = div( timeout, 1000 );
         tv.tv_sec  = d.quot;
         tv.tv_usec = d.rem * 1000;
+
+        ptv = &tv;
     }
 
-    val = vlc_select( val + 1, &rdset, &wrset, &exset,
-                      ( timeout >= 0 ) ? &tv : NULL );
-    if( val == -1 )
+    if( val != -1 && vlc_select( val + 1, &rdset, &wrset, &exset, ptv ) == -1 )
         return -1;
 
+    val = 0;
     for( unsigned i = 0; i < nfds; i++ )
     {
-        int fd = fds[ i ].fd;
-        fds[ i ].revents = ( FD_ISSET( fd, &rdset ) ? POLLIN  : 0 )
-                         | ( FD_ISSET( fd, &wrset ) ? POLLOUT : 0 )
-                         | ( FD_ISSET( fd, &exset ) ? POLLPRI : 0 );
+        if( fds[ i ].revents == 0 )
+        {
+            int fd = fds[ i ].fd;
+            fds[ i ].revents = ( FD_ISSET( fd, &rdset ) ? POLLIN  : 0 )
+                             | ( FD_ISSET( fd, &wrset ) ? POLLOUT : 0 )
+                             | ( FD_ISSET( fd, &exset ) ? POLLPRI : 0 );
+        }
+
+        if( fds[ i ].revents != 0 )
+            val++;
     }
 
     return val;
-- 
2.5.2

