Replace select() system call with poll()

poll() is specified in POSIX.1-2001 as a simpler interface for
multiplexed file descriptors than select(). It also provides more
functionality.

This PR replaces the select() calls used in xrdp with poll()
equivalents.
This commit is contained in:
matt335672
2023-01-12 12:04:37 +00:00
parent b414cfcd5e
commit 78fa1c15b2
7 changed files with 204 additions and 235 deletions
+28 -13
View File
@@ -105,6 +105,7 @@
#include <fcntl.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <poll.h>
#define _PATH_DEVNULL "/dev/null"
@@ -275,14 +276,24 @@ handle_connection(int client_fd)
int client_going = 1;
while (client_going)
{
/* Wait for data from RDP or the client */
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(client_fd, &readfds);
FD_SET(rdp_fd, &readfds);
select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
struct pollfd pollfd[2];
enum
{
RDP_FD = 0,
CLIENT_FD
};
if (FD_ISSET(rdp_fd, &readfds))
/* Wait for data from RDP or the client */
pollfd[RDP_FD].fd = rdp_fd;
pollfd[RDP_FD].events = POLLIN;
pollfd[RDP_FD].revents = 0;
pollfd[CLIENT_FD].fd = client_fd;
pollfd[CLIENT_FD].events = POLLIN;
pollfd[CLIENT_FD].revents = 0;
poll(pollfd, 2, -1);
if ((pollfd[RDP_FD].revents & (POLLIN | POLLHUP)) != 0)
{
/* Read from RDP and write to the client */
char buffer[4096];
@@ -325,7 +336,7 @@ handle_connection(int client_fd)
}
}
if (FD_ISSET(client_fd, &readfds))
if ((pollfd[CLIENT_FD].revents & (POLLIN | POLLHUP)) != 0)
{
/* Read from the client and write to RDP */
char buffer[4096];
@@ -380,14 +391,18 @@ main(int argc, char **argv)
/* Wait for a client to connect to the socket */
while (is_going)
{
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(sa_uds_fd, &readfds);
select(FD_SETSIZE, &readfds, NULL, NULL, NULL);
struct pollfd pollfd;
pollfd.fd = sa_uds_fd;
pollfd.events = POLLIN;
pollfd.revents = 0;
poll(pollfd, 1, -1);
/* If something connected then get it...
* (You can test this using "socat - UNIX-CONNECT:<udspath>".) */
if (FD_ISSET(sa_uds_fd, &readfds))
if ((pollfd.revents & (POLLIN | POLLHUP)) != 0)
{
socklen_t addrsize = sizeof(addr);
int client_fd = accept(sa_uds_fd,
+25 -24
View File
@@ -32,6 +32,7 @@
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <poll.h>
#include "log.h"
#include "xrdp_sockets.h"
@@ -481,40 +482,40 @@ WTSFreeMemory(void *pMemory)
static int
can_send(int sck, int millis)
{
struct timeval time;
fd_set wfds;
int select_rv;
int rv = 0;
struct pollfd pollfd;
/* setup for a select call */
FD_ZERO(&wfds);
FD_SET(sck, &wfds);
time.tv_sec = millis / 1000;
time.tv_usec = (millis * 1000) % 1000000;
pollfd.fd = sck;
pollfd.events = POLLOUT;
pollfd.revents = 0;
if (poll(&pollfd, 1, millis) > 0)
{
if ((pollfd.revents & POLLOUT) != 0)
{
rv = 1;
}
}
/* check if it is ok to write to specified socket */
select_rv = select(sck + 1, 0, &wfds, 0, &time);
return (select_rv > 0) ? 1 : 0;
return rv;
}
/*****************************************************************************/
static int
can_recv(int sck, int millis)
{
struct timeval time;
fd_set rfds;
int select_rv;
int rv = 0;
struct pollfd pollfd;
FD_ZERO(&rfds);
FD_SET(sck, &rfds);
time.tv_sec = millis / 1000;
time.tv_usec = (millis * 1000) % 1000000;
select_rv = select(sck + 1, &rfds, 0, 0, &time);
if (select_rv > 0)
pollfd.fd = sck;
pollfd.events = POLLIN;
pollfd.revents = 0;
if (poll(&pollfd, 1, millis) > 0)
{
return 1;
if ((pollfd.revents & (POLLIN | POLLHUP)) != 0)
{
rv = 1;
}
}
return 0;
return rv;
}