|
@@ -37,6 +37,11 @@
|
|
|
#include <unistd.h>
|
|
|
#include <getopt.h>
|
|
|
|
|
|
+#ifdef HAVE_SYS_RESOURCE_H
|
|
|
+#include <sys/resource.h>
|
|
|
+#endif // HAVE_SYS_RESOURCE_H
|
|
|
+
|
|
|
+
|
|
|
#include <numeric>
|
|
|
#include <vector>
|
|
|
#include <iostream>
|
|
@@ -180,6 +185,39 @@ Context::Context(bool standalone,
|
|
|
A2_LOG_INFO(usedLibs());
|
|
|
A2_LOG_INFO(MSG_LOGGING_STARTED);
|
|
|
|
|
|
+#if defined(HAVE_SYS_RESOURCE_H) && defined(RLIMIT_NOFILE)
|
|
|
+ rlimit r = { 0, 0 };
|
|
|
+ if (getrlimit(RLIMIT_NOFILE, &r) >= 0 && r.rlim_cur != RLIM_INFINITY) {
|
|
|
+ // Thanks portability, for making it easy :p
|
|
|
+ auto rlim_new = r.rlim_cur; // So we get the right type for free.
|
|
|
+ if (r.rlim_cur != RLIM_INFINITY) {
|
|
|
+ rlim_new = op->getAsInt(PREF_RLIMIT_NOFILE);
|
|
|
+ rlim_new = std::max(r.rlim_cur, rlim_new);
|
|
|
+ if (r.rlim_max != RLIM_INFINITY) {
|
|
|
+ rlim_new = std::min(r.rlim_max, rlim_new);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (rlim_new != r.rlim_cur) {
|
|
|
+ if (setrlimit(RLIMIT_NOFILE, &r) != 0) {
|
|
|
+ int errNum = errno;
|
|
|
+ A2_LOG_WARN(fmt("Failed to set rlimit NO_FILE from %" PRIu64 " to "
|
|
|
+ "%" PRIu64 ": %s",
|
|
|
+ (uint64_t)r.rlim_cur, (uint64_t)rlim_new,
|
|
|
+ util::safeStrerror(errNum).c_str()));
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ A2_LOG_DEBUG(fmt("Set rlimit NO_FILE from %" PRIu64 " to %" PRIu64,
|
|
|
+ (uint64_t)r.rlim_cur, (uint64_t)rlim_new));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ rlim_new = op->getAsInt(PREF_RLIMIT_NOFILE);
|
|
|
+ A2_LOG_DEBUG(fmt("Not setting rlimit NO_FILE: %" PRIu64 " >= %" PRIu64,
|
|
|
+ (uint64_t)r.rlim_cur, (uint64_t)rlim_new));
|
|
|
+ }
|
|
|
+ }
|
|
|
+#endif // defined(HAVE_SYS_RESOURCE_H) && defined(RLIMIT_NOFILE)
|
|
|
+
|
|
|
if(op->getAsBool(PREF_DISABLE_IPV6)) {
|
|
|
SocketCore::setProtocolFamily(AF_INET);
|
|
|
// Get rid of AI_ADDRCONFIG. It causes name resolution error
|