Blame SOURCES/qt-4.8-poll.patch

01fa84
--- a/src/corelib/io/qprocess_unix.cpp	
01fa84
+++ a/src/corelib/io/qprocess_unix.cpp	
01fa84
@@ -139,13 +139,6 @@ static void qt_sa_sigchld_handler(int signum)
01fa84
         oldAction(signum);
01fa84
 }
01fa84
 
01fa84
-static inline void add_fd(int &nfds, int fd, fd_set *fdset)
01fa84
-{
01fa84
-    FD_SET(fd, fdset);
01fa84
-    if ((fd) > nfds)
01fa84
-        nfds = fd;
01fa84
-}
01fa84
-
01fa84
 struct QProcessInfo {
01fa84
     QProcess *process;
01fa84
     int deathPipe;
01fa84
@@ -231,9 +224,9 @@ QProcessManager::~QProcessManager()
01fa84
 void QProcessManager::run()
01fa84
 {
01fa84
     forever {
01fa84
-        fd_set readset;
01fa84
-        FD_ZERO(&readset);
01fa84
-        FD_SET(qt_qprocess_deadChild_pipe[0], &readset);
01fa84
+	pollfd fd;
01fa84
+	fd.fd = qt_qprocess_deadChild_pipe[0];
01fa84
+	fd.events = POLLIN;
01fa84
 
01fa84
 #if defined (QPROCESS_DEBUG)
01fa84
         qDebug() << "QProcessManager::run() waiting for children to die";
01fa84
@@ -242,8 +235,8 @@ void QProcessManager::run()
01fa84
         // block forever, or until activity is detected on the dead child
01fa84
         // pipe. the only other peers are the SIGCHLD signal handler, and the
01fa84
         // QProcessManager destructor.
01fa84
-        int nselect = select(qt_qprocess_deadChild_pipe[0] + 1, &readset, 0, 0, 0);
01fa84
-        if (nselect < 0) {
01fa84
+        int ret = qt_safe_poll(&fd, 1, -1, /* retry_eintr */ false);
01fa84
+        if (ret < 0) {
01fa84
             if (errno == EINTR)
01fa84
                 continue;
01fa84
             break;
01fa84
@@ -992,17 +985,6 @@ void QProcessPrivate::killProcess()
01fa84
         ::kill(pid_t(pid), SIGKILL);
01fa84
 }
01fa84
 
01fa84
-static int select_msecs(int nfds, fd_set *fdread, fd_set *fdwrite, int timeout)
01fa84
-{
01fa84
-    if (timeout < 0)
01fa84
-        return qt_safe_select(nfds, fdread, fdwrite, 0, 0);
01fa84
-
01fa84
-    struct timeval tv;
01fa84
-    tv.tv_sec = timeout / 1000;
01fa84
-    tv.tv_usec = (timeout % 1000) * 1000;
01fa84
-    return qt_safe_select(nfds, fdread, fdwrite, 0, &tv;;
01fa84
-}
01fa84
-
01fa84
 /*
01fa84
    Returns the difference between msecs and elapsed. If msecs is -1,
01fa84
    however, -1 is returned.
01fa84
@@ -1025,10 +1007,10 @@ bool QProcessPrivate::waitForStarted(int msecs)
01fa84
 	   childStartedPipe[0]);
01fa84
 #endif
01fa84
 
01fa84
-    fd_set fds;
01fa84
-    FD_ZERO(&fds);
01fa84
-    FD_SET(childStartedPipe[0], &fds);
01fa84
-    if (select_msecs(childStartedPipe[0] + 1, &fds, 0, msecs) == 0) {
01fa84
+    pollfd fd;
01fa84
+    fd.fd = childStartedPipe[0];
01fa84
+    fd.events = POLLIN;
01fa84
+    if (qt_safe_poll(&fd, 1, msecs) == 0) {
01fa84
         processError = QProcess::Timedout;
01fa84
         q->setErrorString(QProcess::tr("Process operation timed out"));
01fa84
 #if defined (QPROCESS_DEBUG)
01fa84
@@ -1044,6 +1026,47 @@ bool QProcessPrivate::waitForStarted(int msecs)
01fa84
     return startedEmitted;
01fa84
 }
01fa84
 
01fa84
+class QProcessFDSet {
01fa84
+    pollfd fds[5];
01fa84
+
01fa84
+    static size_t size()
01fa84
+    {
01fa84
+	return sizeof(fds)/sizeof(fds[0]);
01fa84
+    }
01fa84
+
01fa84
+public:
01fa84
+    QProcessFDSet(QProcessPrivate &proc)
01fa84
+    {
01fa84
+	for (size_t i = 0; i < size(); ++i) {
01fa84
+	    fds[i].fd = -1;
01fa84
+	    fds[i].events = POLLIN;
01fa84
+	}
01fa84
+	death().fd = proc.deathPipe[0];
01fa84
+
01fa84
+        if (proc.processState == QProcess::Starting)
01fa84
+	    started().fd = proc.childStartedPipe[0];
01fa84
+
01fa84
+	stdout().fd = proc.stdoutChannel.pipe[0];
01fa84
+	stderr().fd = proc.stderrChannel.pipe[0];
01fa84
+
01fa84
+        if (!proc.writeBuffer.isEmpty()) {
01fa84
+	    stdin().fd = proc.stdinChannel.pipe[1];
01fa84
+	    stdin().events = POLLOUT;
01fa84
+	}
01fa84
+    }
01fa84
+
01fa84
+    int poll(int timeout)
01fa84
+    {
01fa84
+	return qt_safe_poll(fds, size(), timeout);
01fa84
+    }
01fa84
+
01fa84
+    pollfd &death() { return fds[0]; }
01fa84
+    pollfd &started() { return fds[1]; }
01fa84
+    pollfd &stdout() { return fds[2]; }
01fa84
+    pollfd &stderr() { return fds[3]; }
01fa84
+    pollfd &stdin() { return fds[4]; }
01fa84
+};
01fa84
+
01fa84
 bool QProcessPrivate::waitForReadyRead(int msecs)
01fa84
 {
01fa84
     Q_Q(QProcess);
01fa84
@@ -1055,28 +1078,9 @@ bool QProcessPrivate::waitForReadyRead(int msecs)
01fa84
     stopWatch.start();
01fa84
 
01fa84
     forever {
01fa84
-        fd_set fdread;
01fa84
-        fd_set fdwrite;
01fa84
-
01fa84
-        FD_ZERO(&fdread);
01fa84
-        FD_ZERO(&fdwrite);
01fa84
-
01fa84
-        int nfds = deathPipe[0];
01fa84
-        FD_SET(deathPipe[0], &fdread);
01fa84
-
01fa84
-        if (processState == QProcess::Starting)
01fa84
-            add_fd(nfds, childStartedPipe[0], &fdread);
01fa84
-
01fa84
-        if (stdoutChannel.pipe[0] != -1)
01fa84
-            add_fd(nfds, stdoutChannel.pipe[0], &fdread);
01fa84
-        if (stderrChannel.pipe[0] != -1)
01fa84
-            add_fd(nfds, stderrChannel.pipe[0], &fdread);
01fa84
-
01fa84
-        if (!writeBuffer.isEmpty() && stdinChannel.pipe[1] != -1)
01fa84
-            add_fd(nfds, stdinChannel.pipe[1], &fdwrite);
01fa84
-
01fa84
+	QProcessFDSet fdset(*this);
01fa84
         int timeout = qt_timeout_value(msecs, stopWatch.elapsed());
01fa84
-        int ret = select_msecs(nfds + 1, &fdread, &fdwrite, timeout);
01fa84
+        int ret = fdset.poll(timeout);
01fa84
         if (ret < 0) {
01fa84
             break;
01fa84
         }
01fa84
@@ -1086,18 +1090,18 @@ bool QProcessPrivate::waitForReadyRead(int msecs)
01fa84
 	    return false;
01fa84
 	}
01fa84
 
01fa84
-	if (childStartedPipe[0] != -1 && FD_ISSET(childStartedPipe[0], &fdread)) {
01fa84
+	if (qt_readable(fdset.started())) {
01fa84
             if (!_q_startupNotification())
01fa84
                 return false;
01fa84
 	}
01fa84
 
01fa84
         bool readyReadEmitted = false;
01fa84
-	if (stdoutChannel.pipe[0] != -1 && FD_ISSET(stdoutChannel.pipe[0], &fdread)) {
01fa84
+	if (qt_readable(fdset.stdout())) {
01fa84
 	    bool canRead = _q_canReadStandardOutput();
01fa84
             if (processChannel == QProcess::StandardOutput && canRead)
01fa84
                 readyReadEmitted = true;
01fa84
 	}
01fa84
-	if (stderrChannel.pipe[0] != -1 && FD_ISSET(stderrChannel.pipe[0], &fdread)) {
01fa84
+	if (qt_readable(fdset.stderr())) {
01fa84
 	    bool canRead = _q_canReadStandardError();
01fa84
             if (processChannel == QProcess::StandardError && canRead)
01fa84
                 readyReadEmitted = true;
01fa84
@@ -1105,13 +1109,13 @@ bool QProcessPrivate::waitForReadyRead(int msecs)
01fa84
         if (readyReadEmitted)
01fa84
             return true;
01fa84
 
01fa84
-	if (stdinChannel.pipe[1] != -1 && FD_ISSET(stdinChannel.pipe[1], &fdwrite))
01fa84
+	if (qt_writable(fdset.stdin()))
01fa84
 	    _q_canWrite();
01fa84
 
01fa84
-	if (deathPipe[0] == -1 || FD_ISSET(deathPipe[0], &fdread)) {
01fa84
+	if (qt_readable(fdset.death())) {
01fa84
             if (_q_processDied())
01fa84
                 return false;
01fa84
-        }
01fa84
+	}
01fa84
     }
01fa84
     return false;
01fa84
 }
01fa84
@@ -1127,29 +1131,9 @@ bool QProcessPrivate::waitForBytesWritten(int msecs)
01fa84
     stopWatch.start();
01fa84
 
01fa84
     while (!writeBuffer.isEmpty()) {
01fa84
-        fd_set fdread;
01fa84
-        fd_set fdwrite;
01fa84
-
01fa84
-        FD_ZERO(&fdread);
01fa84
-        FD_ZERO(&fdwrite);
01fa84
-
01fa84
-        int nfds = deathPipe[0];
01fa84
-        FD_SET(deathPipe[0], &fdread);
01fa84
-
01fa84
-        if (processState == QProcess::Starting)
01fa84
-            add_fd(nfds, childStartedPipe[0], &fdread);
01fa84
-
01fa84
-        if (stdoutChannel.pipe[0] != -1)
01fa84
-            add_fd(nfds, stdoutChannel.pipe[0], &fdread);
01fa84
-        if (stderrChannel.pipe[0] != -1)
01fa84
-            add_fd(nfds, stderrChannel.pipe[0], &fdread);
01fa84
-
01fa84
-
01fa84
-        if (!writeBuffer.isEmpty() && stdinChannel.pipe[1] != -1)
01fa84
-            add_fd(nfds, stdinChannel.pipe[1], &fdwrite);
01fa84
-
01fa84
+	QProcessFDSet fdset(*this);
01fa84
 	int timeout = qt_timeout_value(msecs, stopWatch.elapsed());
01fa84
-	int ret = select_msecs(nfds + 1, &fdread, &fdwrite, timeout);
01fa84
+	int ret = fdset.poll(timeout);
01fa84
         if (ret < 0) {
01fa84
             break;
01fa84
         }
01fa84
@@ -1160,24 +1144,24 @@ bool QProcessPrivate::waitForBytesWritten(int msecs)
01fa84
 	    return false;
01fa84
 	}
01fa84
 
01fa84
-	if (childStartedPipe[0] != -1 && FD_ISSET(childStartedPipe[0], &fdread)) {
01fa84
+	if (qt_readable(fdset.started())) {
01fa84
 	    if (!_q_startupNotification())
01fa84
 		return false;
01fa84
 	}
01fa84
 
01fa84
-	if (stdinChannel.pipe[1] != -1 && FD_ISSET(stdinChannel.pipe[1], &fdwrite))
01fa84
+	if (qt_writable(fdset.stdin()))
01fa84
 	    return _q_canWrite();
01fa84
 
01fa84
-	if (stdoutChannel.pipe[0] != -1 && FD_ISSET(stdoutChannel.pipe[0], &fdread))
01fa84
+	if (qt_readable(fdset.stdout()))
01fa84
 	    _q_canReadStandardOutput();
01fa84
 
01fa84
-	if (stderrChannel.pipe[0] != -1 && FD_ISSET(stderrChannel.pipe[0], &fdread))
01fa84
+	if (qt_readable(fdset.stderr()))
01fa84
 	    _q_canReadStandardError();
01fa84
 
01fa84
-	if (deathPipe[0] == -1 || FD_ISSET(deathPipe[0], &fdread)) {
01fa84
-            if (_q_processDied())
01fa84
-                return false;
01fa84
-        }
01fa84
+	if (qt_readable(fdset.death())) {
01fa84
+	    if (_q_processDied())
01fa84
+		return false;
01fa84
+	}
01fa84
     }
01fa84
 
01fa84
     return false;
01fa84
@@ -1194,29 +1178,9 @@ bool QProcessPrivate::waitForFinished(int msecs)
01fa84
     stopWatch.start();
01fa84
 
01fa84
     forever {
01fa84
-        fd_set fdread;
01fa84
-        fd_set fdwrite;
01fa84
-        int nfds = -1;
01fa84
-
01fa84
-        FD_ZERO(&fdread);
01fa84
-        FD_ZERO(&fdwrite);
01fa84
-
01fa84
-        if (processState == QProcess::Starting)
01fa84
-            add_fd(nfds, childStartedPipe[0], &fdread);
01fa84
-
01fa84
-        if (stdoutChannel.pipe[0] != -1)
01fa84
-            add_fd(nfds, stdoutChannel.pipe[0], &fdread);
01fa84
-        if (stderrChannel.pipe[0] != -1)
01fa84
-            add_fd(nfds, stderrChannel.pipe[0], &fdread);
01fa84
-
01fa84
-        if (processState == QProcess::Running)
01fa84
-            add_fd(nfds, deathPipe[0], &fdread);
01fa84
-
01fa84
-        if (!writeBuffer.isEmpty() && stdinChannel.pipe[1] != -1)
01fa84
-            add_fd(nfds, stdinChannel.pipe[1], &fdwrite);
01fa84
-
01fa84
+	QProcessFDSet fdset(*this);
01fa84
 	int timeout = qt_timeout_value(msecs, stopWatch.elapsed());
01fa84
-	int ret = select_msecs(nfds + 1, &fdread, &fdwrite, timeout);
01fa84
+	int ret = fdset.poll(timeout);
01fa84
         if (ret < 0) {
01fa84
             break;
01fa84
         }
01fa84
@@ -1226,20 +1190,20 @@ bool QProcessPrivate::waitForFinished(int msecs)
01fa84
 	    return false;
01fa84
 	}
01fa84
 
01fa84
-	if (childStartedPipe[0] != -1 && FD_ISSET(childStartedPipe[0], &fdread)) {
01fa84
+	if (qt_readable(fdset.started())) {
01fa84
 	    if (!_q_startupNotification())
01fa84
 		return false;
01fa84
 	}
01fa84
-	if (stdinChannel.pipe[1] != -1 && FD_ISSET(stdinChannel.pipe[1], &fdwrite))
01fa84
+	if (qt_writable(fdset.stdin()))
01fa84
 	    _q_canWrite();
01fa84
 
01fa84
-	if (stdoutChannel.pipe[0] != -1 && FD_ISSET(stdoutChannel.pipe[0], &fdread))
01fa84
+	if (qt_readable(fdset.stdout()))
01fa84
 	    _q_canReadStandardOutput();
01fa84
 
01fa84
-	if (stderrChannel.pipe[0] != -1 && FD_ISSET(stderrChannel.pipe[0], &fdread))
01fa84
+	if (qt_readable(fdset.stderr()))
01fa84
 	    _q_canReadStandardError();
01fa84
 
01fa84
-	if (deathPipe[0] == -1 || FD_ISSET(deathPipe[0], &fdread)) {
01fa84
+	if (qt_readable(fdset.death())) {
01fa84
             if (_q_processDied())
01fa84
                 return true;
01fa84
 	}
01fa84
@@ -1249,10 +1213,10 @@ bool QProcessPrivate::waitForFinished(int msecs)
01fa84
 
01fa84
 bool QProcessPrivate::waitForWrite(int msecs)
01fa84
 {
01fa84
-    fd_set fdwrite;
01fa84
-    FD_ZERO(&fdwrite);
01fa84
-    FD_SET(stdinChannel.pipe[1], &fdwrite);
01fa84
-    return select_msecs(stdinChannel.pipe[1] + 1, 0, &fdwrite, msecs < 0 ? 0 : msecs) == 1;
01fa84
+    pollfd fd;
01fa84
+    fd.fd = stdinChannel.pipe[1];
01fa84
+    fd.events = POLLIN;
01fa84
+    return qt_safe_poll(&fd, 1, msecs);
01fa84
 }
01fa84
 
01fa84
 void QProcessPrivate::findExitCode()
01fa84
--- a/src/corelib/kernel/qcore_unix.cpp	
01fa84
+++ a/src/corelib/kernel/qcore_unix.cpp	
01fa84
@@ -103,4 +103,165 @@ int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept,
01fa84
     }
01fa84
 }
01fa84
 
01fa84
+#ifndef Q_OS_VXWORKS
01fa84
+
01fa84
+int qt_safe_poll(struct pollfd *fds, int nfds, int timeout_ms, bool retry_eintr)
01fa84
+{
01fa84
+    if (nfds == 0)
01fa84
+	return 0;
01fa84
+    if (nfds < 0) {
01fa84
+	errno = EINVAL;
01fa84
+	return -1;
01fa84
+    }
01fa84
+
01fa84
+    // Retry on ret == 0 if the deadline has not yet passed because
01fa84
+    // Linux can return early from the syscall, without setting EINTR.
01fa84
+    if (timeout_ms < 0) {
01fa84
+	forever {
01fa84
+	    int ret = ::poll(fds, nfds, -1);
01fa84
+	    if (ret > 0)
01fa84
+		return ret;
01fa84
+	    if (retry_eintr) {
01fa84
+		if (ret == 0 || ret == -1 && errno == EINTR) {
01fa84
+		    continue;
01fa84
+		} else {
01fa84
+		    return -1;
01fa84
+		}
01fa84
+	    }
01fa84
+	    if (ret == 0) {
01fa84
+		errno = EINTR;
01fa84
+		return -1;
01fa84
+	    }
01fa84
+	    return ret;
01fa84
+	}
01fa84
+    }
01fa84
+
01fa84
+    timeval previous = qt_gettime();
01fa84
+    timeval deadline = previous;
01fa84
+    deadline.tv_sec += timeout_ms / 1000;
01fa84
+    deadline.tv_usec += (timeout_ms % 1000) * 1000;
01fa84
+    if (deadline.tv_usec >= 1000000) {
01fa84
+	++deadline.tv_sec;
01fa84
+	deadline.tv_usec -= 1000000;
01fa84
+    }
01fa84
+    int remaining = timeout_ms;
01fa84
+
01fa84
+    forever {
01fa84
+	int ret = ::poll(fds, nfds, remaining);
01fa84
+	if (ret > 0)
01fa84
+	    return ret;
01fa84
+	timeval now = qt_gettime();
01fa84
+	if ((now.tv_sec > deadline.tv_sec // past deadline
01fa84
+	     || (now.tv_sec == deadline.tv_sec
01fa84
+		 && now.tv_usec >= deadline.tv_usec))
01fa84
+	    || (now.tv_sec < previous.tv_sec // time warp
01fa84
+		|| (now.tv_sec == previous.tv_sec
01fa84
+		    && now.tv_usec < previous.tv_usec))
01fa84
+	    || (ret < 0 && (errno != EINTR || !retry_eintr))) // other error
01fa84
+	    return ret;
01fa84
+	if (ret == 0 && !retry_eintr) {
01fa84
+	    errno = EINTR;
01fa84
+	    return -1;
01fa84
+	}
01fa84
+        remaining = (deadline.tv_sec - now.tv_sec) * 1000
01fa84
+		     + (deadline.tv_usec - now.tv_usec) / 1000;
01fa84
+	previous = now;
01fa84
+    }
01fa84
+}
01fa84
+
01fa84
+#else
01fa84
+
01fa84
+// Poll emulation for VxWorks.
01fa84
+
01fa84
+static int mark_bad_descriptors(pollfd *fds, int nfds)
01fa84
+{
01fa84
+    fd_set r;
01fa84
+    FD_ZERO(&r);
01fa84
+    struct timeval tv;
01fa84
+    tv.tv_sec = 0;
01fa84
+    tv.tv_usec = 0;
01fa84
+    int ret = 0;
01fa84
+
01fa84
+    // Check each descriptor invidually for badness.
01fa84
+    for (int i = 0; i < nfds; ++i) {
01fa84
+        pollfd &fd(fds[i]);
01fa84
+        if (fd.fd >= 0) {
01fa84
+            FD_SET(fd.fd, &r);
01fa84
+            int ret = qt_safe_select(fd.fd + 1, &r, NULL, NULL, &tv;;
01fa84
+            FD_CLR(fd.fd, &r);
01fa84
+            if (ret < 0 && errno == EBADF) {
01fa84
+                fd.revents = POLLNVAL;
01fa84
+                ++ret;
01fa84
+            }
01fa84
+        }
01fa84
+    }
01fa84
+    Q_ASSERT(ret > 0);
01fa84
+    return ret;
01fa84
+}
01fa84
+
01fa84
+int qt_safe_poll(pollfd *fds, int nfds, int timeout, bool retry_eintr)
01fa84
+{
01fa84
+    fd_set r, w;
01fa84
+    FD_ZERO(&r);
01fa84
+    FD_ZERO(&w);
01fa84
+    int maxfd = -1;
01fa84
+
01fa84
+    // Extract the watched descriptors.
01fa84
+    for (int i = 0; i < nfds; ++i) {
01fa84
+        pollfd &fd(fds[i]);
01fa84
+        if (fd.fd >= 0 && fd.fd < FD_SETSIZE) {
01fa84
+            if (fd.events & POLLIN) {
01fa84
+                FD_SET(fd.fd, &r);
01fa84
+                if (fd.fd > maxfd)
01fa84
+                    maxfd = fd.fd;
01fa84
+            }
01fa84
+            if (fd.events & POLLOUT) {
01fa84
+                FD_SET(fd.fd, &w);
01fa84
+                if (fd.fd > maxfd)
01fa84
+                    maxfd = fd.fd;
01fa84
+            }
01fa84
+        }
01fa84
+    }
01fa84
+
01fa84
+    // If timeout is negative, wait indefinitely for activity.
01fa84
+    timeval tv;
01fa84
+    timeval *ptv;
01fa84
+    if (timeout >= 0) {
01fa84
+        tv.tv_sec = timeout / 1000;
01fa84
+        tv.tv_usec = (timeout % 1000) * 1000;
01fa84
+        ptv = &tv;
01fa84
+    } else
01fa84
+        ptv = NULL;
01fa84
+
01fa84
+    int ret;
01fa84
+    if (retry_eintr)
01fa84
+        ret = qt_safe_select(maxfd + 1, &r, &w, NULL, ptv);
01fa84
+    else
01fa84
+        ret = ::select(maxfd + 1, &r, &w, NULL, ptv);
01fa84
+    if (ret < 0 && errno == EBADF) {
01fa84
+        return mark_bad_descriptors(fds, nfds);
01fa84
+    }
01fa84
+    if (ret <= 0)
01fa84
+        return ret;
01fa84
+
01fa84
+    // Set the revents flags.
01fa84
+    ret = 0;
01fa84
+    for (int i = 0; i < nfds; ++i) {
01fa84
+        pollfd &fd(fds[i]);
01fa84
+        fd.revents = 0;
01fa84
+        if (fd.fd >= 0 && fd.fd < FD_SETSIZE) {
01fa84
+            if ((fd.events & POLLIN) && FD_ISSET(fd.fd, &r))
01fa84
+                fd.revents |= POLLIN;
01fa84
+            if ((fd.events & POLLOUT) && FD_ISSET(fd.fd, &w))
01fa84
+                fd.revents |= POLLOUT;
01fa84
+            if (fd.revents)
01fa84
+                ++ret;
01fa84
+        }
01fa84
+    }
01fa84
+    Q_ASSERT(ret > 0);
01fa84
+    return ret;
01fa84
+}
01fa84
+
01fa84
+#endif
01fa84
+
01fa84
 QT_END_NAMESPACE
01fa84
--- a/src/corelib/kernel/qcore_unix_p.h	
01fa84
+++ a/src/corelib/kernel/qcore_unix_p.h	
01fa84
@@ -316,9 +316,42 @@ static inline pid_t qt_safe_waitpid(pid_t pid, int *status, int options)
01fa84
 
01fa84
 timeval qt_gettime(); // in qelapsedtimer_mac.cpp or qtimestamp_unix.cpp
01fa84
 
01fa84
+// Deprecated due to FD_SETSIZE limitation, use qt_safe_poll instead.
01fa84
 Q_CORE_EXPORT int qt_safe_select(int nfds, fd_set *fdread, fd_set *fdwrite, fd_set *fdexcept,
01fa84
                                  const struct timeval *tv);
01fa84
 
01fa84
+#ifndef Q_OS_VXWORKS
01fa84
+#include <poll.h>
01fa84
+#else
01fa84
+
01fa84
+// Poll emulation for VxWorks.
01fa84
+
01fa84
+struct pollfd {
01fa84
+  int fd;
01fa84
+  short events;
01fa84
+  short revents;
01fa84
+};
01fa84
+
01fa84
+#define POLLIN 1
01fa84
+#define POLLOUT 2
01fa84
+#define POLLERR 4
01fa84
+#define POLLHUP 8
01fa84
+#define POLLNVAL 16
01fa84
+#endif
01fa84
+
01fa84
+inline bool qt_readable(const pollfd &fd)
01fa84
+{
01fa84
+  return fd.fd >= 0 && (fd.revents & (POLLIN | POLLHUP | POLLERR | POLLNVAL)) != 0;
01fa84
+}
01fa84
+
01fa84
+inline bool qt_writable(const pollfd &fd)
01fa84
+{
01fa84
+  return fd.fd >= 0 && (fd.revents & (POLLOUT | POLLHUP | POLLERR | POLLNVAL)) != 0;
01fa84
+}
01fa84
+
01fa84
+Q_CORE_EXPORT int qt_safe_poll(pollfd *fds, int nfds, int timeout,
01fa84
+                               bool retry_eintr = true);
01fa84
+
01fa84
 // according to X/OPEN we have to define semun ourselves
01fa84
 // we use prefix as on some systems sem.h will have it
01fa84
 struct semid_ds;
01fa84
--- a/src/network/socket/qlocalserver_unix.cpp	
01fa84
+++ a/src/network/socket/qlocalserver_unix.cpp	
01fa84
@@ -208,16 +208,11 @@ void QLocalServerPrivate::_q_onNewConnection()
01fa84
 
01fa84
 void QLocalServerPrivate::waitForNewConnection(int msec, bool *timedOut)
01fa84
 {
01fa84
-    fd_set readfds;
01fa84
-    FD_ZERO(&readfds);
01fa84
-    FD_SET(listenSocket, &readfds);
01fa84
+    struct pollfd fd;
01fa84
+    fd.fd = listenSocket;
01fa84
+    fd.events = POLLIN;
01fa84
 
01fa84
-    timeval timeout;
01fa84
-    timeout.tv_sec = msec / 1000;
01fa84
-    timeout.tv_usec = (msec % 1000) * 1000;
01fa84
-
01fa84
-    int result = -1;
01fa84
-    result = qt_safe_select(listenSocket + 1, &readfds, 0, 0, (msec == -1) ? 0 : &timeout);
01fa84
+    int result = qt_safe_poll(&fd, 1, msec);
01fa84
     if (-1 == result) {
01fa84
         setError(QLatin1String("QLocalServer::waitForNewConnection"));
01fa84
         closeServer();
01fa84
--- a/src/network/socket/qlocalsocket_unix.cpp	
01fa84
+++ a/src/network/socket/qlocalsocket_unix.cpp	
01fa84
@@ -56,10 +56,6 @@ 
01fa84
 #include <qdebug.h>
01fa84
 #include <qelapsedtimer.h>
01fa84
 
01fa84
-#ifdef Q_OS_VXWORKS
01fa84
-#  include <selectLib.h>
01fa84
-#endif
01fa84
-
01fa84
 #define QT_CONNECT_TIMEOUT 30000
01fa84
 
01fa84
 QT_BEGIN_NAMESPACE
01fa84
@@ -520,32 +516,17 @@ bool QLocalSocket::waitForConnected(int msec)
01fa84
     if (state() != ConnectingState)
01fa84
         return (state() == ConnectedState);
01fa84
 
01fa84
-    fd_set fds;
01fa84
-    FD_ZERO(&fds);
01fa84
-    FD_SET(d->connectingSocket, &fds);
01fa84
-
01fa84
-    timeval timeout;
01fa84
-    timeout.tv_sec = msec / 1000;
01fa84
-    timeout.tv_usec = (msec % 1000) * 1000;
01fa84
-
01fa84
-    // timeout can not be 0 or else select will return an error.
01fa84
-    if (0 == msec)
01fa84
-        timeout.tv_usec = 1000;
01fa84
+    pollfd fd;
01fa84
+    fd.fd = d->connectingSocket;
01fa84
+    fd.events = POLLIN | POLLOUT;
01fa84
 
01fa84
     int result = -1;
01fa84
     // on Linux timeout will be updated by select, but _not_ on other systems.
01fa84
     QElapsedTimer timer;
01fa84
+    int remaining = msec;
01fa84
     timer.start();
01fa84
-    while (state() == ConnectingState
01fa84
-           && (-1 == msec || timer.elapsed() < msec)) {
01fa84
-#ifdef Q_OS_SYMBIAN
01fa84
-        // On Symbian, ready-to-write is signaled when non-blocking socket
01fa84
-        // connect is finised. Is ready-to-read really used on other
01fa84
-        // UNIX paltforms when using non-blocking AF_UNIX socket?
01fa84
-        result = ::select(d->connectingSocket + 1, 0, &fds, 0, &timeout);
01fa84
-#else
01fa84
-        result = ::select(d->connectingSocket + 1, &fds, 0, 0, &timeout);
01fa84
-#endif
01fa84
+    while (state() == ConnectingState) {
01fa84
+        result = qt_safe_poll(&fd, 1, remaining, /* retry_eintr */ false);
01fa84
         if (-1 == result && errno != EINTR) {
01fa84
             d->errorOccurred( QLocalSocket::UnknownSocketError,
01fa84
                     QLatin1String("QLocalSocket::waitForConnected"));
01fa84
@@ -553,6 +534,11 @@ bool QLocalSocket::waitForConnected(int msec)
01fa84
         }
01fa84
         if (result > 0)
01fa84
             d->_q_connectToSocket();
01fa84
+        if (msec >= 0) {
01fa84
+            remaining = timer.elapsed() - msec;
01fa84
+            if (remaining < 0)
01fa84
+                break;
01fa84
+        }
01fa84
     }
01fa84
 
01fa84
     return (state() == ConnectedState);
01fa84
--- a/src/network/socket/qnativesocketengine_unix.cpp	
01fa84
+++ a/src/network/socket/qnativesocketengine_unix.cpp	
01fa84
@@ -1090,48 +1090,40 @@ qint64 QNativeSocketEnginePrivate::nativeRead(char *data, qint64 maxSize)
01fa84
 
01fa84
 int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) const
01fa84
 {
01fa84
-    fd_set fds;
01fa84
-    FD_ZERO(&fds);
01fa84
-    FD_SET(socketDescriptor, &fds);
01fa84
-
01fa84
-    struct timeval tv;
01fa84
-    tv.tv_sec = timeout / 1000;
01fa84
-    tv.tv_usec = (timeout % 1000) * 1000;
01fa84
-
01fa84
-    int retval;
01fa84
-    if (selectForRead)
01fa84
-        retval = qt_safe_select(socketDescriptor + 1, &fds, 0, 0, timeout < 0 ? 0 : &tv;;
01fa84
-    else
01fa84
-        retval = qt_safe_select(socketDescriptor + 1, 0, &fds, 0, timeout < 0 ? 0 : &tv;;
01fa84
-
01fa84
-    return retval;
01fa84
+    struct pollfd fd;
01fa84
+    fd.fd = socketDescriptor;
01fa84
+    if (selectForRead) {
01fa84
+	fd.events = POLLIN;
01fa84
+    } else {
01fa84
+	fd.events = POLLOUT;
01fa84
+    }
01fa84
+    return qt_safe_poll(&fd, 1, timeout);
01fa84
 }
01fa84
 
01fa84
 int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool checkWrite,
01fa84
                        bool *selectForRead, bool *selectForWrite) const
01fa84
 {
01fa84
-    fd_set fdread;
01fa84
-    FD_ZERO(&fdread);
01fa84
+    struct pollfd fd;
01fa84
+    fd.fd = socketDescriptor;
01fa84
     if (checkRead)
01fa84
-        FD_SET(socketDescriptor, &fdread);
01fa84
-
01fa84
-    fd_set fdwrite;
01fa84
-    FD_ZERO(&fdwrite);
01fa84
+	fd.events =  POLLIN;
01fa84
+    else
01fa84
+	fd.events = 0;
01fa84
     if (checkWrite)
01fa84
-        FD_SET(socketDescriptor, &fdwrite);
01fa84
-
01fa84
-    struct timeval tv;
01fa84
-    tv.tv_sec = timeout / 1000;
01fa84
-    tv.tv_usec = (timeout % 1000) * 1000;
01fa84
-
01fa84
-    int ret;
01fa84
-    ret = qt_safe_select(socketDescriptor + 1, &fdread, &fdwrite, 0, timeout < 0 ? 0 : &tv;;
01fa84
-
01fa84
+	fd.events |= POLLOUT;
01fa84
+    int ret = qt_safe_poll(&fd, 1, timeout);
01fa84
     if (ret <= 0)
01fa84
-        return ret;
01fa84
-    *selectForRead = FD_ISSET(socketDescriptor, &fdread);
01fa84
-    *selectForWrite = FD_ISSET(socketDescriptor, &fdwrite);
01fa84
-
01fa84
+	return ret;
01fa84
+    bool r = (fd.revents & (POLLIN | POLLHUP | POLLERR)) != 0;
01fa84
+    bool w = (fd.revents & (POLLOUT | POLLHUP | POLLERR)) != 0;
01fa84
+    // Emulate the return value from select(2).
01fa84
+    ret = 0;
01fa84
+    if (r)
01fa84
+	++ret;
01fa84
+    if (w)
01fa84
+	++ret;
01fa84
+    *selectForRead = r;
01fa84
+    *selectForWrite = w;
01fa84
     return ret;
01fa84
 }
01fa84
 
01fa84
--- a/src/qt3support/network/q3socketdevice_unix.cpp	
01fa84
+++ a/src/qt3support/network/q3socketdevice_unix.cpp	
01fa84
@@ -68,6 +68,7 @@ static inline int qt_socket_socket(int domain, int type, int protocol)
01fa84
 #endif
01fa84
 
01fa84
 #include "q3socketdevice.h"
01fa84
+#include "private/qcore_unix_p.h"
01fa84
 
01fa84
 #ifndef QT_NO_NETWORK
01fa84
 
01fa84
@@ -588,19 +589,10 @@ Q_LONG Q3SocketDevice::waitForMore( int msecs, bool *timeout ) const
01fa84
 {
01fa84
     if ( !isValid() )
01fa84
 	return -1;
01fa84
-    if ( fd >= FD_SETSIZE )
01fa84
-	return -1;
01fa84
-
01fa84
-    fd_set fds;
01fa84
-    struct timeval tv;
01fa84
-
01fa84
-    FD_ZERO( &fds );
01fa84
-    FD_SET( fd, &fds );
01fa84
-
01fa84
-    tv.tv_sec = msecs / 1000;
01fa84
-    tv.tv_usec = (msecs % 1000) * 1000;
01fa84
 
01fa84
-    int rv = select( fd+1, &fds, 0, 0, msecs < 0 ? 0 : &tv );
01fa84
+    pollfd pfd;
01fa84
+    pfd.fd = fd;
01fa84
+    int rv = qt_safe_poll(&pfd, 1, msecs, /* retry_eintr */ false);
01fa84
 
01fa84
     if ( rv < 0 )
01fa84
 	return -1;
01fa84
--- a/src/qt3support/other/q3process_unix.cpp	
01fa84
+++ a/src/qt3support/other/q3process_unix.cpp	
01fa84
@@ -981,13 +981,10 @@ bool Q3Process::isRunning() const
01fa84
 	// On heavy processing, the socket notifier for the sigchild might not
01fa84
 	// have found time to fire yet.
01fa84
 	if ( d->procManager && d->procManager->sigchldFd[1] < FD_SETSIZE ) {
01fa84
-	    fd_set fds;
01fa84
-	    struct timeval tv;
01fa84
-	    FD_ZERO( &fds );
01fa84
-	    FD_SET( d->procManager->sigchldFd[1], &fds );
01fa84
-	    tv.tv_sec = 0;
01fa84
-	    tv.tv_usec = 0;
01fa84
-	    if ( ::select( d->procManager->sigchldFd[1]+1, &fds, 0, 0, &tv ) > 0 )
01fa84
+	    pollfd fd;
01fa84
+	    fd.fd = d->procManager->sigchldFd[1];
01fa84
+	    fd.events = POLLIN;
01fa84
+	    if ( qt_safe_poll(&fd, 1, 0, /* retry_eintr */ false) > 0 )
01fa84
 		d->procManager->sigchldHnd( d->procManager->sigchldFd[1] );
01fa84
 	}
01fa84
 
01fa84
@@ -1124,29 +1121,21 @@ void Q3Process::socketRead( int fd )
01fa84
 	}
01fa84
     }
01fa84
 
01fa84
-    if ( fd < FD_SETSIZE ) {
01fa84
-	fd_set fds;
01fa84
-	struct timeval tv;
01fa84
-	FD_ZERO( &fds );
01fa84
-	FD_SET( fd, &fds );
01fa84
-	tv.tv_sec = 0;
01fa84
-	tv.tv_usec = 0;
01fa84
-	while ( ::select( fd+1, &fds, 0, 0, &tv ) > 0 ) {
01fa84
-	    // prepare for the next round
01fa84
-	    FD_ZERO( &fds );
01fa84
-	    FD_SET( fd, &fds );
01fa84
-	    // read data
01fa84
-	    ba = new QByteArray( basize );
01fa84
-	    n = ::read( fd, ba->data(), basize );
01fa84
-	    if ( n > 0 ) {
01fa84
-		ba->resize( n );
01fa84
-		buffer->append( ba );
01fa84
-		ba = 0;
01fa84
-	    } else {
01fa84
-		delete ba;
01fa84
-		ba = 0;
01fa84
-		break;
01fa84
-	    }
01fa84
+    pollfd pfd;
01fa84
+    pfd.fd = fd;
01fa84
+    pfd.events = POLLIN;
01fa84
+    while (qt_safe_poll(&pfd, 1, 0)) {
01fa84
+	// read data
01fa84
+	ba = new QByteArray( basize );
01fa84
+	n = ::read( fd, ba->data(), basize );
01fa84
+	if ( n > 0 ) {
01fa84
+	    ba->resize( n );
01fa84
+	    buffer->append( ba );
01fa84
+	    ba = 0;
01fa84
+	} else {
01fa84
+	    delete ba;
01fa84
+	    ba = 0;
01fa84
+	    break;
01fa84
 	}
01fa84
     }
01fa84