Close more superfluous file descriptors

GitHub Actions leaks file descriptors with numbers greater than a
hundred. But our test runner only checked for FDs up to 64.
This commit is contained in:
Justine Tunney 2022-06-21 07:22:39 -07:00
parent b8e9f71d33
commit ff28e38ef1
2 changed files with 30 additions and 5 deletions

View file

@ -18,6 +18,7 @@
*/ */
#include "libc/calls/calls.h" #include "libc/calls/calls.h"
#include "libc/calls/strace.internal.h" #include "libc/calls/strace.internal.h"
#include "libc/calls/struct/rlimit.h"
#include "libc/calls/struct/sigset.h" #include "libc/calls/struct/sigset.h"
#include "libc/calls/struct/termios.h" #include "libc/calls/struct/termios.h"
#include "libc/calls/struct/utsname.h" #include "libc/calls/struct/utsname.h"
@ -49,6 +50,7 @@
#include "libc/sysv/consts/auxv.h" #include "libc/sysv/consts/auxv.h"
#include "libc/sysv/consts/f.h" #include "libc/sysv/consts/f.h"
#include "libc/sysv/consts/poll.h" #include "libc/sysv/consts/poll.h"
#include "libc/sysv/consts/rlim.h"
#include "libc/sysv/consts/sig.h" #include "libc/sysv/consts/sig.h"
#include "libc/sysv/consts/termios.h" #include "libc/sysv/consts/termios.h"
#include "tool/decode/lib/idname.h" #include "tool/decode/lib/idname.h"
@ -140,6 +142,7 @@ textstartup void __printargs(const char *prologue) {
unsigned i, n; unsigned i, n;
int e, x, flags; int e, x, flags;
uintptr_t *auxp; uintptr_t *auxp;
struct rlimit rlim;
struct utsname uts; struct utsname uts;
struct termios termios; struct termios termios;
struct AuxiliaryValue *auxinfo; struct AuxiliaryValue *auxinfo;
@ -276,6 +279,17 @@ textstartup void __printargs(const char *prologue) {
PRINT(" error: sigprocmask() failed %m"); PRINT(" error: sigprocmask() failed %m");
} }
PRINT("");
PRINT("RESOURCE LIMITS");
for (i = 0; i < RLIM_NLIMITS; ++i) {
if (!getrlimit(i, &rlim)) {
if (rlim.rlim_cur == RLIM_INFINITY) rlim.rlim_cur = -1;
if (rlim.rlim_max == RLIM_INFINITY) rlim.rlim_max = -1;
PRINT(" ☼ %-20s %,16ld %,16ld", DescribeRlimitName(i), rlim.rlim_cur,
rlim.rlim_max);
}
}
PRINT(""); PRINT("");
PRINT("ARGUMENTS (%p)", __argv); PRINT("ARGUMENTS (%p)", __argv);
if (*__argv) { if (*__argv) {

View file

@ -103,8 +103,9 @@ static void EmptySignalMask(void) {
} }
static void FixIrregularFds(void) { static void FixIrregularFds(void) {
int i, fd; int i, fd, maxfds;
struct pollfd pfds[64]; struct rlimit rlim;
struct pollfd *pfds;
for (i = 0; i < 3; ++i) { for (i = 0; i < 3; ++i) {
if (fcntl(i, F_GETFL) == -1) { if (fcntl(i, F_GETFL) == -1) {
errno = 0; errno = 0;
@ -115,7 +116,16 @@ static void FixIrregularFds(void) {
} }
} }
} }
for (i = 0; i < ARRAYLEN(pfds); ++i) { if (IsWindows()) {
maxfds = 64;
} else {
maxfds = 256;
if (!getrlimit(RLIMIT_NOFILE, &rlim)) {
maxfds = MIN(maxfds, (uint64_t)rlim.rlim_cur);
}
}
pfds = malloc(maxfds * sizeof(struct pollfd));
for (i = 0; i < maxfds; ++i) {
pfds[i].fd = i + 3; pfds[i].fd = i + 3;
pfds[i].events = POLLIN; pfds[i].events = POLLIN;
} }
@ -123,12 +133,13 @@ static void FixIrregularFds(void) {
// TODO(jart): Fix Blinkenlights poll() / close() // TODO(jart): Fix Blinkenlights poll() / close()
return; return;
} }
if (poll(pfds, ARRAYLEN(pfds), 0) != -1) { if (poll(pfds, maxfds, 0) != -1) {
for (i = 0; i < ARRAYLEN(pfds); ++i) { for (i = 0; i < maxfds; ++i) {
if (pfds[i].revents & POLLNVAL) continue; if (pfds[i].revents & POLLNVAL) continue;
CHECK_EQ(0, close(pfds[i].fd)); CHECK_EQ(0, close(pfds[i].fd));
} }
} }
free(pfds);
} }
static void SetLimit(int resource, uint64_t soft, uint64_t hard) { static void SetLimit(int resource, uint64_t soft, uint64_t hard) {