diff --git a/libc/runtime/printargs.greg.c b/libc/runtime/printargs.greg.c index 807a8297c..4f3fb22d1 100644 --- a/libc/runtime/printargs.greg.c +++ b/libc/runtime/printargs.greg.c @@ -18,6 +18,7 @@ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/calls/calls.h" #include "libc/calls/strace.internal.h" +#include "libc/calls/struct/rlimit.h" #include "libc/calls/struct/sigset.h" #include "libc/calls/struct/termios.h" #include "libc/calls/struct/utsname.h" @@ -49,6 +50,7 @@ #include "libc/sysv/consts/auxv.h" #include "libc/sysv/consts/f.h" #include "libc/sysv/consts/poll.h" +#include "libc/sysv/consts/rlim.h" #include "libc/sysv/consts/sig.h" #include "libc/sysv/consts/termios.h" #include "tool/decode/lib/idname.h" @@ -140,6 +142,7 @@ textstartup void __printargs(const char *prologue) { unsigned i, n; int e, x, flags; uintptr_t *auxp; + struct rlimit rlim; struct utsname uts; struct termios termios; struct AuxiliaryValue *auxinfo; @@ -276,6 +279,17 @@ textstartup void __printargs(const char *prologue) { 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("ARGUMENTS (%p)", __argv); if (*__argv) { diff --git a/libc/testlib/testmain.c b/libc/testlib/testmain.c index 1e5b8f65e..ecfb75277 100644 --- a/libc/testlib/testmain.c +++ b/libc/testlib/testmain.c @@ -103,8 +103,9 @@ static void EmptySignalMask(void) { } static void FixIrregularFds(void) { - int i, fd; - struct pollfd pfds[64]; + int i, fd, maxfds; + struct rlimit rlim; + struct pollfd *pfds; for (i = 0; i < 3; ++i) { if (fcntl(i, F_GETFL) == -1) { 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].events = POLLIN; } @@ -123,12 +133,13 @@ static void FixIrregularFds(void) { // TODO(jart): Fix Blinkenlights poll() / close() return; } - if (poll(pfds, ARRAYLEN(pfds), 0) != -1) { - for (i = 0; i < ARRAYLEN(pfds); ++i) { + if (poll(pfds, maxfds, 0) != -1) { + for (i = 0; i < maxfds; ++i) { if (pfds[i].revents & POLLNVAL) continue; CHECK_EQ(0, close(pfds[i].fd)); } } + free(pfds); } static void SetLimit(int resource, uint64_t soft, uint64_t hard) {