bbennett-ks commented on code in PR #643:
URL: https://github.com/apache/guacamole-server/pull/643#discussion_r2981223657
##########
src/libguac/tcp.c:
##########
@@ -97,20 +97,33 @@ int guac_tcp_connect(const char* hostname, const char*
port, const int timeout)
continue;
}
- /* Structure that stores our timeout setting. */
+ /* Structure that stores our timeout setting. On Linux, select()
updates
+ timeout with the remaining time on EINTR, so no need to
reinitialize on
+ retry. */
struct timeval tv;
tv.tv_sec = timeout;
tv.tv_usec = 0;
/* Connect and wait for timeout */
if ((retval = connect(fd, current_address->ai_addr,
current_address->ai_addrlen)) < 0) {
- if (errno == EINPROGRESS) {
- /* Set up timeout. */
+ /* If connect() is in progress (EINPROGRESS) or interrupted by a
signal
+ * (EINTR), wait for the socket to become writable and check
SO_ERROR. */
+ if (errno == EINPROGRESS || errno == EINTR) {
+
+ /* Prevent overflowing fd_set. */
+ if (fd >= FD_SETSIZE) {
+ guac_error = GUAC_STATUS_INVALID_ARGUMENT;
+ guac_error_message = "File descriptor exceeds FD_SETSIZE.";
+ close(fd);
+ fd = -1;
+ continue;
+ }
+
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(fd, &fdset);
-
- retval = select(fd + 1, NULL, &fdset, NULL, &tv);
+
+ GUAC_RETRY_EINTR(retval, select(fd + 1, NULL, &fdset, NULL,
&tv));
Review Comment:
I'd seen many opinions on these 2 issues so I did a deep dive into the Linux
kernel and glibc source code to be sure:
- `fd_set` is not modified if `select` returns -1.
- `struct timeval` is modified if `select()` returns -1 and `errno = EINTR`
to reflect the elapsed time. Leaving `tv` unmodified on retry means the
original timeout value is observed, even on retry.
#2 is Linux specific behavior, not behavior as defined by POSIX.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]