Thu, Jan 30, 2020 at 08:29:22PM CET, jerome99(a)internet.lu wrote:
Hello,
A firewall application is using libteamdctl to read the configuration of the
bonding interfaces. This application has a lot of open file descriptors and
is therefore crashing in the libteadmdctl at this select function.
The select function is a very old function and is using the FD_(*) macros to
the set the bit of monitored file descriptors in a fixed size array. In the
glibc this array can only contain 1024 entries ( file descritor 0->1023).
The select function can therefore only monitor the first 1024 file
descriptors. If the used file descriptor used in the select function is
greater then 1023, the select can/will crash.
Fair enough, I'm applying this.
Thanks!
>
>
>
>Sincerely,
>Jérôme
>
>On 1/29/20 6:16 PM, Jiri Pirko wrote:
>> 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
>> >
>