Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions eloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ static void eloop_sock_table_remove_sock(struct eloop_sock_table *table,
}


static inline void warn_too_many_fds()
{
static bool warned = false;

if (!warned) {
fprintf(stderr, "unable to handle more than %d file descriptors\n", FD_SETSIZE);
fprintf(stderr, "too many interfaces being watched\n");
warned = true;
}
}

static void eloop_sock_table_set_fds(struct eloop_sock_table *table,
fd_set *fds)
{
Expand All @@ -190,8 +201,13 @@ static void eloop_sock_table_set_fds(struct eloop_sock_table *table,
if (table->table == NULL)
return;

for (i = 0; i < table->count; i++)
for (i = 0; i < table->count; i++) {
if (table->table[i].sock >= FD_SETSIZE) {
warn_too_many_fds();
continue;
}
FD_SET(table->table[i].sock, fds);
}
}


Expand All @@ -205,6 +221,9 @@ static void eloop_sock_table_dispatch(struct eloop_sock_table *table,

table->changed = 0;
for (i = 0; i < table->count; i++) {
if (table->table[i].sock >= FD_SETSIZE) {
return;
}
if (FD_ISSET(table->table[i].sock, fds)) {
table->table[i].handler(table->table[i].sock,
table->table[i].eloop_data,
Expand Down Expand Up @@ -497,7 +516,8 @@ void eloop_run(void)
eloop_sock_table_set_fds(&eloop.readers, rfds);
eloop_sock_table_set_fds(&eloop.writers, wfds);
eloop_sock_table_set_fds(&eloop.exceptions, efds);
res = select(eloop.max_sock + 1, rfds, wfds, efds,
res = select(eloop.max_sock < FD_SETSIZE ? eloop.max_sock + 1 : FD_SETSIZE,
rfds, wfds, efds,
eloop.timeout ? &_tv : NULL);
if (res < 0 && errno != EINTR && errno != 0) {
perror("select");
Expand Down Expand Up @@ -571,6 +591,11 @@ void eloop_wait_for_read_sock(int sock)
if (sock < 0)
return;

if (sock >= FD_SETSIZE) {
warn_too_many_fds();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with this being added, but a quick grep through the code doesn't seem to find any in-tree callers for eloop_wait_for_read_sock.

Maybe we should delete it instead?

Copy link
Contributor Author

@penguin359 penguin359 Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I only see eloop_register_read_sock() and eloop_unregister_read_sock() used, but not eloop_wait_for_read_sock(). Also, it looks like eloop_register_sock(), eloop_unregister_sock(), and os_get_time() can all be made static.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I found several things to clean-up in eloop.c/eloop.h including function prototypes in the header files that refer to functions that have never existed in the Git history. I'll save that clean-up for another PR, but I think I can drop that function for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are okay with it, I'll merge this patch and backport it to the 1.1 branch.

return;
}

FD_ZERO(&rfds);
FD_SET(sock, &rfds);
select(sock + 1, &rfds, NULL, NULL, NULL);
Expand Down