From 7cd42fc5189533aa1601aea690b78e3bba4c1f44 Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 15 Jun 2020 01:50:03 +0300 Subject: [PATCH] Add set_resource_limit. GitOrigin-RevId: 29cf122b31ff86ccc8f6c1fc3b71c28e89b8054f --- tdutils/CMakeLists.txt | 2 ++ tdutils/td/utils/port/rlimit.cpp | 57 ++++++++++++++++++++++++++++++++ tdutils/td/utils/port/rlimit.h | 18 ++++++++++ 3 files changed, 77 insertions(+) create mode 100644 tdutils/td/utils/port/rlimit.cpp create mode 100644 tdutils/td/utils/port/rlimit.h diff --git a/tdutils/CMakeLists.txt b/tdutils/CMakeLists.txt index bf55fc376..09f2fbddd 100644 --- a/tdutils/CMakeLists.txt +++ b/tdutils/CMakeLists.txt @@ -58,6 +58,7 @@ set(TDUTILS_SOURCE td/utils/port/MemoryMapping.cpp td/utils/port/path.cpp td/utils/port/PollFlags.cpp + td/utils/port/rlimit.cpp td/utils/port/ServerSocketFd.cpp td/utils/port/signals.cpp td/utils/port/sleep.cpp @@ -133,6 +134,7 @@ set(TDUTILS_SOURCE td/utils/port/Poll.h td/utils/port/PollBase.h td/utils/port/PollFlags.h + td/utils/port/rlimit.h td/utils/port/RwMutex.h td/utils/port/ServerSocketFd.h td/utils/port/signals.h diff --git a/tdutils/td/utils/port/rlimit.cpp b/tdutils/td/utils/port/rlimit.cpp new file mode 100644 index 000000000..03a0babf4 --- /dev/null +++ b/tdutils/td/utils/port/rlimit.cpp @@ -0,0 +1,57 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/utils/port/rlimit.h" + +#include "td/utils/port/config.h" + +#include "td/utils/misc.h" + +#if TD_PORT_POSIX +#include +#include +#include +#endif + +namespace td { + +#if TD_PORT_POSIX +static int get_resource(ResourceLimitType type) { + switch (type) { + case ResourceLimitType::NoFile: + return RLIMIT_NOFILE; + default: + UNREACHABLE(); + return -1; + } +} +#endif + +Status set_resource_limit(ResourceLimitType type, uint64 value) { +#if TD_PORT_POSIX + int resource = get_resource(type); + + rlimit rlim; + if (getrlimit(resource, &rlim) == -1) { + return OS_ERROR("Failed to get current resource limit"); + } + + TRY_RESULT(new_value, narrow_cast_safe(value)); + if (rlim.rlim_max < new_value) { + rlim.rlim_max = new_value; + } + rlim.rlim_cur = new_value; + + if (setrlimit(resource, &rlim) < 0) { + return OS_ERROR("Failed to set resource limit"); + } + return Status::OK(); +#elif TD_PORT_WINDOWS + return Status::OK(); // Windows has no limits +#endif +} + +} // namespace td diff --git a/tdutils/td/utils/port/rlimit.h b/tdutils/td/utils/port/rlimit.h new file mode 100644 index 000000000..5e858243d --- /dev/null +++ b/tdutils/td/utils/port/rlimit.h @@ -0,0 +1,18 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/utils/common.h" +#include "td/utils/Status.h" + +namespace td { + +enum class ResourceLimitType { NoFile }; + +Status set_resource_limit(ResourceLimitType type, uint64 value); + +} // namespace td