OSDN Git Service

generalize qt_safe_poll() implementation and use it QProcessPrivate::waitForStarted()
authorIvailo Monev <xakepa10@gmail.com>
Wed, 22 Sep 2021 16:45:54 +0000 (19:45 +0300)
committerIvailo Monev <xakepa10@gmail.com>
Wed, 22 Sep 2021 16:45:54 +0000 (19:45 +0300)
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
src/core/io/qprocess_unix.cpp
src/core/kernel/qcore_unix_p.h
src/network/socket/qlocalserver_unix.cpp
src/network/socket/qlocalsocket_unix.cpp
src/network/socket/qnativesocketengine_unix.cpp

index 719e4fb..dcc1dc7 100644 (file)
@@ -793,10 +793,11 @@ bool QProcessPrivate::waitForStarted(int msecs)
            childStartedPipe[0]);
 #endif
 
-    fd_set fds;
-    FD_ZERO(&fds);
-    FD_SET(childStartedPipe[0], &fds);
-    if (select_msecs(childStartedPipe[0] + 1, &fds, 0, msecs) == 0) {
+    struct pollfd fds;
+    ::memset(&fds, 0, sizeof(struct pollfd));
+    fds.fd = childStartedPipe[0];
+    fds.events = POLLIN;
+    if (qt_safe_poll(&fds, 1, msecs) == 0) {
         processError = QProcess::Timedout;
         q->setErrorString(QProcess::tr("Process operation timed out"));
 #if defined (QPROCESS_DEBUG)
index 8c8ab10..ee67948 100644 (file)
@@ -258,19 +258,14 @@ static inline pid_t qt_safe_waitpid(pid_t pid, int *status, int options)
 }
 
 // don't call ::poll, call qt_safe_poll
-static inline int qt_safe_poll(int fd, int events, int timeout, int *revents)
+static inline int qt_safe_poll(struct pollfd *fds, nfds_t nfds, int timeout)
 {
-    struct pollfd pd;
-    ::memset(&pd, 0, sizeof(struct pollfd));
-    pd.fd = fd;
-    pd.events = events;
     int ret;
-    Q_EINTR_LOOP(ret, ::poll(&pd, 1, timeout));
-    if ((pd.revents & POLLERR) != 0 || (pd.revents & POLLHUP) != 0 || (pd.revents & POLLNVAL) != 0) {
+    Q_EINTR_LOOP(ret, ::poll(fds, nfds, timeout));
+    if ((fds->revents & POLLERR) != 0 || (fds->revents & POLLHUP) != 0 || (fds->revents & POLLNVAL) != 0) {
         // select() compat
         return -1;
     }
-    *revents = pd.revents;
     return ret;
 }
 
index 5c6d721..f837665 100644 (file)
@@ -162,8 +162,11 @@ void QLocalServerPrivate::_q_onNewConnection()
 
 void QLocalServerPrivate::waitForNewConnection(int msec, bool *timedOut)
 {
-    int revents = 0;
-    int result = qt_safe_poll(listenSocket, POLLIN, msec, &revents);
+    struct pollfd fds;
+    ::memset(&fds, 0, sizeof(struct pollfd));
+    fds.fd = listenSocket;
+    fds.events = POLLIN;
+    int result = qt_safe_poll(&fds, 1, msec);
     if (result == -1) {
         setError(QLatin1String("QLocalServer::waitForNewConnection"));
         closeServer();
index 8d0bc40..f0f0fe7 100644 (file)
@@ -505,8 +505,11 @@ bool QLocalSocket::waitForConnected(int msec)
         if (msec == 0)
             msec = 1000;
 
-        int revents = 0;
-        int result = qt_safe_poll(d->connectingSocket, POLLIN, msec, &revents);
+        struct pollfd fds;
+        ::memset(&fds, 0, sizeof(struct pollfd));
+        fds.fd = d->connectingSocket;
+        fds.events = POLLIN;
+        int result = qt_safe_poll(&fds, 1, msec);
         if (result == -1) {
             d->errorOccurred( QLocalSocket::UnknownSocketError,
                     QLatin1String("QLocalSocket::waitForConnected"));
index 53fc916..40722ae 100644 (file)
@@ -987,26 +987,31 @@ qint64 QNativeSocketEnginePrivate::nativeRead(char *data, qint64 maxSize)
 
 int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool selectForRead) const
 {
-    int revents = 0;
-    return qt_safe_poll(socketDescriptor, selectForRead ? POLLIN : POLLOUT, timeout, &revents);
+    struct pollfd fds;
+    ::memset(&fds, 0, sizeof(struct pollfd));
+    fds.fd = socketDescriptor;
+    fds.events = (selectForRead ? POLLIN : POLLOUT);
+    return qt_safe_poll(&fds, 1, timeout);
 }
 
 int QNativeSocketEnginePrivate::nativeSelect(int timeout, bool checkRead, bool checkWrite,
                        bool *selectForRead, bool *selectForWrite) const
 {
-    int events = 0;
+    struct pollfd fds;
+    ::memset(&fds, 0, sizeof(struct pollfd));
+    fds.fd = socketDescriptor;
+    fds.events = 0;
     if (checkRead) {
-        events |= POLLIN;
+        fds.events |= POLLIN;
     }
     if (checkWrite) {
-        events |= POLLOUT;
+        fds.events |= POLLOUT;
     }
-    int revents = 0;
 
-    int ret = qt_safe_poll(socketDescriptor, events, timeout, &revents);
+    int ret = qt_safe_poll(&fds, 1, timeout);
     if (ret > 0) {
-        *selectForRead = ((revents & POLLIN) != 0);
-        *selectForWrite = ((revents & POLLOUT) != 0);
+        *selectForRead = ((fds.revents & POLLIN) != 0);
+        *selectForWrite = ((fds.revents & POLLOUT) != 0);
     }
     return ret;
 }