]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
httpAddrConnect2: Check for error if POLLHUP is in valid revents
authorZdenek Dohnal <zdohnal@redhat.com>
Fri, 15 Dec 2023 09:59:54 +0000 (10:59 +0100)
committerZdenek Dohnal <zdohnal@redhat.com>
Fri, 15 Dec 2023 09:59:54 +0000 (10:59 +0100)
Some Linux kernel versions put POLLOUT|POLLHUP into revents when client tries to connect with httpAddrConnect2(), which makes the connection fail.
Let's check the option SO_ERROR before scratching the attempt - if there is no error, remove POLLHUP from revents.

I've re-purposed previously Solaris-only code to be used everywhere if the conditions are met - this should prevent bigger delays than necessary.

Slightly different issue than #827, but with similar symptoms (kernel sending POLLOUT|POLLHUP).

CHANGES.md
cups/http-addrlist.c

index 15977239ed28cd33cc93c2fd260b1886b3cf2da8..788ada7d8cc8ff2ba3474c916a99bc0fe2bd93e5 100644 (file)
@@ -4,6 +4,8 @@ CHANGES - OpenPrinting CUPS 2.4.8 - TBA
 Changes in CUPS v2.4.8 (TBA)
 ----------------------------
 
+- Added additional check on socket if `revents` from `poll()` returns POLLHUP
+  together with POLLIN or POLLOUT in `httpAddrConnect2()` (Issue #839)
 - Added new value for 'lpstat' option '-W' - successfull - for getting
   successfully printed jobs (Issue #830)
 - Added warning if the device has to be asked for 'all,media-col-database' separately
index 198d073d06bec4c705db675dc2823c0744fa8a4f..cffcf184c80236a2ab93ff6c8fb21fc930eb8317 100644 (file)
@@ -318,21 +318,38 @@ httpAddrConnect2(
       {
 #  ifdef HAVE_POLL
        DEBUG_printf(("pfds[%d].revents=%x\n", i, pfds[i].revents));
-#    ifdef __sun
-       // Solaris connect runs asynchronously returning EINPROGRESS. Following
-       // poll() does not detect the socket is not connected and returns
-       // POLLIN|POLLOUT. Check the connection status and update error flag.
-       int            sres, serr;
-       socklen_t      slen = sizeof(serr);
-       sres = getsockopt(fds[i], SOL_SOCKET, SO_ERROR, &serr, &slen);
-       if (sres || serr)
+
+#    ifdef _WIN32
+       if (((WSAGetLastError() == WSAEINPROGRESS) && (pfds[i].revents & POLLIN) && (pfds[i].revents & POLLOUT)) ||
+           ((pfds[i].revents & POLLHUP) && (pfds[i].revents & (POLLIN|POLLOUT))))
+#    else
+       if (((errno == EINPROGRESS) && (pfds[i].revents & POLLIN) && (pfds[i].revents & POLLOUT)) ||
+           ((pfds[i].revents & POLLHUP) && (pfds[i].revents & (POLLIN|POLLOUT))))
+#    endif /* _WIN32 */
        {
-         pfds[i].revents |= POLLERR;
-#      ifdef DEBUG
-         DEBUG_printf(("1httpAddrConnect2: getsockopt returned: %d with error: %s", sres, strerror(serr)));
-#      endif
+         // Some systems generate POLLIN or POLLOUT together with POLLHUP when doing
+         // asynchronous connections. The solution seems to be to use getsockopt to
+         // check the SO_ERROR value and ignore the POLLHUP if there is no error or
+         // the error is EINPROGRESS.
+
+         int       sres,                /* Return value from getsockopt() - 0, or -1 if error */
+                   serr;                /* Option SO_ERROR value */
+         socklen_t slen = sizeof(serr); /* Option value size */
+
+         sres = getsockopt(fds[i], SOL_SOCKET, SO_ERROR, &serr, &slen);
+
+         if (sres || serr)
+         {
+           pfds[i].revents |= POLLERR;
+#    ifdef DEBUG
+           DEBUG_printf(("1httpAddrConnect2: getsockopt returned: %d with error: %s", sres, strerror(serr)));
+#    endif
+         }
+         else if (pfds[i].revents && (pfds[i].revents & POLLHUP) && (pfds[i].revents & (POLLIN | POLLOUT)))
+         {
+           pfds[i].revents &= ~POLLHUP;
+         }
        }
-#    endif // __sun
 
 
        if (pfds[i].revents && !(pfds[i].revents & (POLLERR | POLLHUP)))