Tue, Jan 28, 2020 at 01:11:00AM CET, jerome99(a)internet.lu wrote:
The select function cannot be used in application if the application
has
already more than 1024 open files. The select will crash if an file
descriptor greater or equal than 1023 is monitored.
Okay, how we can come close that?
>
>Signed-off-by: Jerome Freilinger <jerome99(a)internet.lu>
>---
> libteamdctl/cli_usock.c | 25 +++++++++----------------
> 1 file changed, 9 insertions(+), 16 deletions(-)
>
>diff --git a/libteamdctl/cli_usock.c b/libteamdctl/cli_usock.c
>index 0dc97ae..431b12d 100644
>--- a/libteamdctl/cli_usock.c
>+++ b/libteamdctl/cli_usock.c
>@@ -25,6 +25,7 @@
> #include <sys/socket.h>
> #include <unistd.h>
> #include <teamdctl.h>
>+#include <poll.h>
> #include "teamdctl_private.h"
> #include "../teamd/teamd_usock_common.h"
>
>@@ -79,26 +80,18 @@ static int cli_usock_send(int sock, char *msg)
> return 0;
> }
>
>-#define WAIT_SEC (TEAMDCTL_REPLY_TIMEOUT / 1000)
>-#define WAIT_USEC (TEAMDCTL_REPLY_TIMEOUT % 1000 * 1000)
>-
> static int cli_usock_wait_recv(int sock)
> {
>- fd_set rfds;
>- int fdmax;
>- int ret;
>- struct timeval tv;
>+ struct pollfd fds[1];
>+
>+ fds[0].fd = sock;
>+ fds[0].events = POLLIN;
>+ fds[0].revents = 0;
>+ int ret = poll(fds, 1, TEAMDCTL_REPLY_TIMEOUT);
>
>- tv.tv_sec = WAIT_SEC;
>- tv.tv_usec = WAIT_USEC;
>- FD_ZERO(&rfds);
>- FD_SET(sock, &rfds);
>- fdmax = sock + 1;
>- ret = select(fdmax, &rfds, NULL, NULL, &tv);
>- if (ret == -1)
>- return -errno;
>- if (!FD_ISSET(sock, &rfds))
>+ if (ret == 0)
> return -ETIMEDOUT;
>+ else if (ret < 0)
>+ return -errno;
> return 0;
> }
>
>--
>2.20.1
>