|
29 | 29 | #include <stdlib.h> |
30 | 30 | #include <string.h> |
31 | 31 | #include <sys/ioctl.h> |
| 32 | +#include <sys/resource.h> |
32 | 33 | #include <sys/time.h> |
33 | 34 | #include <termios.h> |
34 | 35 | #include <unistd.h> |
@@ -119,6 +120,29 @@ static bool setTimer(nsj_t* nsj) { |
119 | 120 | return true; |
120 | 121 | } |
121 | 122 |
|
| 123 | +static bool setFDLimit() { |
| 124 | + constexpr uint64_t kRLimitNoFileDesired = 8192ULL; |
| 125 | + |
| 126 | + struct rlimit64 rl; |
| 127 | + if (util::getrlimit(RLIMIT_NOFILE, &rl) == -1) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + if (rl.rlim_cur >= kRLimitNoFileDesired) { |
| 131 | + return true; |
| 132 | + } |
| 133 | + uint64_t target = std::min((uint64_t)kRLimitNoFileDesired, (uint64_t)rl.rlim_max); |
| 134 | + if (target <= rl.rlim_cur) { |
| 135 | + return true; |
| 136 | + } |
| 137 | + rl.rlim_cur = target; |
| 138 | + if (util::setrlimit(RLIMIT_NOFILE, rl) == -1) { |
| 139 | + PLOG_W("util::setrlimit(RLIMIT_NOFILE, %" PRIu64 ") failed", (uint64_t)rl.rlim_cur); |
| 140 | + return false; |
| 141 | + } |
| 142 | + LOG_D("Increased RLIMIT_NOFILE to %" PRIu64, (uint64_t)rl.rlim_cur); |
| 143 | + return true; |
| 144 | +} |
| 145 | + |
122 | 146 | static bool pipeTraffic(nsj_t* nsj, int listenfd) { |
123 | 147 | std::vector<struct pollfd> fds; |
124 | 148 | fds.reserve(nsj->pipes.size() * 3 + 1); |
@@ -339,10 +363,12 @@ int main(int argc, char* argv[]) { |
339 | 363 | if (!nsjail::setSigHandlers()) { |
340 | 364 | LOG_F("nsjail::setSigHandlers() failed"); |
341 | 365 | } |
| 366 | + if (!nsjail::setFDLimit()) { |
| 367 | + LOG_E("nsjail::setFDLimit() failed"); |
| 368 | + } |
342 | 369 | if (!nsjail::setTimer(nsj.get())) { |
343 | 370 | LOG_F("nsjail::setTimer() failed"); |
344 | 371 | } |
345 | | - |
346 | 372 | if (nsj->njc.detect_cgroupv2()) { |
347 | 373 | cgroup2::detectCgroupv2(nsj.get()); |
348 | 374 | LOG_I("Detected cgroups version: %d", nsj->njc.use_cgroupv2() ? 2 : 1); |
|
0 commit comments