summaryrefslogtreecommitdiffstats
path: root/init/rlimit_parser.cpp
diff options
context:
space:
mode:
authorTom Cherry <tomcherry@google.com>2017-08-25 10:39:25 -0700
committerTom Cherry <tomcherry@google.com>2017-08-28 10:19:50 -0700
commit7ac013de7edc7ec6570ff6a5b6bd3bdce68b769c (patch)
treee4d5ab70ea43a674a4ccfdadec5d9849b6e240bd /init/rlimit_parser.cpp
parentdf3e89be9466cd29d3d35c0e5a7b687789d2be09 (diff)
downloadsystem_core-7ac013de7edc7ec6570ff6a5b6bd3bdce68b769c.tar.gz
system_core-7ac013de7edc7ec6570ff6a5b6bd3bdce68b769c.tar.bz2
system_core-7ac013de7edc7ec6570ff6a5b6bd3bdce68b769c.zip
init: support setting rlimits per service
Add a new service option, `rlimit` that allows a given rlimit to be set for a specific service instead of globally. Use the same parsing, now allowing text such as 'cpu' or 'rtprio' instead of relying on the enum value for the `setrlimit` builtin command as well. Bug: 63882119 Bug: 64894637 Test: boot bullhead, run a test app that attempts to set its rtprio to 95, see that the priority set fails normally but passes when `rlimit rtprio 99 99` is used as its service option. See that this fails when `rlimit rtprio 50 50` is used as well. Test: new unit tests Change-Id: I4a13ca20e8529937d8b4bc11718ffaaf77523a52
Diffstat (limited to 'init/rlimit_parser.cpp')
-rw-r--r--init/rlimit_parser.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/init/rlimit_parser.cpp b/init/rlimit_parser.cpp
new file mode 100644
index 000000000..fe1d6a724
--- /dev/null
+++ b/init/rlimit_parser.cpp
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "rlimit_parser.h"
+
+#include <android-base/parseint.h>
+#include <android-base/strings.h>
+
+using android::base::EqualsIgnoreCase;
+using android::base::ParseInt;
+using android::base::ParseUint;
+using android::base::StartsWith;
+
+namespace android {
+namespace init {
+
+// Builtins and service definitions both have their arguments start at 1 and finish at 3.
+Result<std::pair<int, rlimit>> ParseRlimit(const std::vector<std::string>& args) {
+ static const std::vector<std::pair<const char*, int>> text_to_resources = {
+ {"cpu", 0}, {"fsize", 1}, {"data", 2}, {"stack", 3},
+ {"core", 4}, {"rss", 5}, {"nproc", 6}, {"nofile", 7},
+ {"memlock", 8}, {"as", 9}, {"locks", 10}, {"sigpending", 11},
+ {"msgqueue", 12}, {"nice", 13}, {"rtprio", 14}, {"rttime", 15},
+ };
+
+ int resource;
+
+ if (ParseInt(args[1], &resource)) {
+ if (resource >= RLIM_NLIMITS) {
+ return Error() << "Resource '" << args[1] << "' over the maximum resource value '"
+ << RLIM_NLIMITS << "'";
+ } else if (resource < 0) {
+ return Error() << "Resource '" << args[1] << "' below the minimum resource value '0'";
+ }
+ } else {
+ std::string resource_string;
+ if (StartsWith(args[1], "RLIM_")) {
+ resource_string = args[1].substr(5);
+ } else {
+ resource_string = args[1];
+ }
+
+ auto it = std::find_if(text_to_resources.begin(), text_to_resources.end(),
+ [&resource_string](const auto& entry) {
+ return EqualsIgnoreCase(resource_string, entry.first);
+ });
+ if (it == text_to_resources.end()) {
+ return Error() << "Could not parse resource '" << args[1] << "'";
+ }
+
+ resource = it->second;
+ }
+
+ rlimit limit;
+ if (!ParseUint(args[2], &limit.rlim_cur)) {
+ return Error() << "Could not parse soft limit '" << args[2] << "'";
+ }
+ if (!ParseUint(args[3], &limit.rlim_max)) {
+ return Error() << "Could not parse hard limit '" << args[3] << "'";
+ }
+ return {resource, limit};
+}
+
+} // namespace init
+} // namespace android