Skip to content

Commit 7ac013d

Browse files
author
Tom Cherry
committed
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
1 parent df3e89b commit 7ac013d

8 files changed

+276
-14
lines changed

init/Android.bp

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ cc_library_static {
7676
"security.cpp",
7777
"selinux.cpp",
7878
"service.cpp",
79+
"rlimit_parser.cpp",
7980
"tokenizer.cpp",
8081
"uevent_listener.cpp",
8182
"ueventd_parser.cpp",
@@ -163,6 +164,7 @@ cc_test {
163164
"init_test.cpp",
164165
"property_service_test.cpp",
165166
"result_test.cpp",
167+
"rlimit_parser_test.cpp",
166168
"service_test.cpp",
167169
"ueventd_test.cpp",
168170
"util_test.cpp",

init/README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ runs the service.
216216
http://man7.org/linux/man-pages/man7/capabilities.7.html for a list of Linux
217217
capabilities.
218218

219+
`setrlimit <resource> <cur> <max>`
220+
> This applies the given rlimit to the service. rlimits are inherited by child
221+
processes, so this effectively applies the given rlimit to the process tree
222+
started by this service.
223+
It is parsed similarly to the setrlimit command specified below.
224+
219225
`seclabel <seclabel>`
220226
> Change to 'seclabel' before exec'ing this service.
221227
Primarily for use by services run from the rootfs, e.g. ueventd, adbd.
@@ -455,7 +461,11 @@ Commands
455461
within _value_.
456462

457463
`setrlimit <resource> <cur> <max>`
458-
> Set the rlimit for a resource.
464+
> Set the rlimit for a resource. This applies to all processes launched after
465+
the limit is set. It is intended to be set early in init and applied globally.
466+
_resource_ is best specified using its text representation ('cpu', 'rtio', etc
467+
or 'RLIM_CPU', 'RLIM_RTIO', etc). It also may be specified as the int value
468+
that the resource enum corresponds to.
459469

460470
`start <service>`
461471
> Start a service running if it is not already running.

init/builtins.cpp

+4-13
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include "parser.h"
6464
#include "property_service.h"
6565
#include "reboot.h"
66+
#include "rlimit_parser.h"
6667
#include "service.h"
6768
#include "signal_handler.h"
6869
#include "util.h"
@@ -563,20 +564,10 @@ static Result<Success> do_setprop(const std::vector<std::string>& args) {
563564
}
564565

565566
static Result<Success> do_setrlimit(const std::vector<std::string>& args) {
566-
int resource;
567-
if (!android::base::ParseInt(args[1], &resource)) {
568-
return Error() << "unable to parse resource, " << args[1];
569-
}
570-
571-
struct rlimit limit;
572-
if (!android::base::ParseUint(args[2], &limit.rlim_cur)) {
573-
return Error() << "unable to parse rlim_cur, " << args[2];
574-
}
575-
if (!android::base::ParseUint(args[3], &limit.rlim_max)) {
576-
return Error() << "unable to parse rlim_max, " << args[3];
577-
}
567+
auto rlimit = ParseRlimit(args);
568+
if (!rlimit) return rlimit.error();
578569

579-
if (setrlimit(resource, &limit) == -1) {
570+
if (setrlimit(rlimit->first, &rlimit->second) == -1) {
580571
return ErrnoError() << "setrlimit failed";
581572
}
582573
return Success();

init/rlimit_parser.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (C) 2017 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "rlimit_parser.h"
18+
19+
#include <android-base/parseint.h>
20+
#include <android-base/strings.h>
21+
22+
using android::base::EqualsIgnoreCase;
23+
using android::base::ParseInt;
24+
using android::base::ParseUint;
25+
using android::base::StartsWith;
26+
27+
namespace android {
28+
namespace init {
29+
30+
// Builtins and service definitions both have their arguments start at 1 and finish at 3.
31+
Result<std::pair<int, rlimit>> ParseRlimit(const std::vector<std::string>& args) {
32+
static const std::vector<std::pair<const char*, int>> text_to_resources = {
33+
{"cpu", 0}, {"fsize", 1}, {"data", 2}, {"stack", 3},
34+
{"core", 4}, {"rss", 5}, {"nproc", 6}, {"nofile", 7},
35+
{"memlock", 8}, {"as", 9}, {"locks", 10}, {"sigpending", 11},
36+
{"msgqueue", 12}, {"nice", 13}, {"rtprio", 14}, {"rttime", 15},
37+
};
38+
39+
int resource;
40+
41+
if (ParseInt(args[1], &resource)) {
42+
if (resource >= RLIM_NLIMITS) {
43+
return Error() << "Resource '" << args[1] << "' over the maximum resource value '"
44+
<< RLIM_NLIMITS << "'";
45+
} else if (resource < 0) {
46+
return Error() << "Resource '" << args[1] << "' below the minimum resource value '0'";
47+
}
48+
} else {
49+
std::string resource_string;
50+
if (StartsWith(args[1], "RLIM_")) {
51+
resource_string = args[1].substr(5);
52+
} else {
53+
resource_string = args[1];
54+
}
55+
56+
auto it = std::find_if(text_to_resources.begin(), text_to_resources.end(),
57+
[&resource_string](const auto& entry) {
58+
return EqualsIgnoreCase(resource_string, entry.first);
59+
});
60+
if (it == text_to_resources.end()) {
61+
return Error() << "Could not parse resource '" << args[1] << "'";
62+
}
63+
64+
resource = it->second;
65+
}
66+
67+
rlimit limit;
68+
if (!ParseUint(args[2], &limit.rlim_cur)) {
69+
return Error() << "Could not parse soft limit '" << args[2] << "'";
70+
}
71+
if (!ParseUint(args[3], &limit.rlim_max)) {
72+
return Error() << "Could not parse hard limit '" << args[3] << "'";
73+
}
74+
return {resource, limit};
75+
}
76+
77+
} // namespace init
78+
} // namespace android

init/rlimit_parser.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2017 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef _INIT_RLIMIT_PARSER_H
18+
#define _INIT_RLIMIT_PARSER_H
19+
20+
#include <sys/resource.h>
21+
22+
#include <string>
23+
#include <vector>
24+
25+
#include "result.h"
26+
27+
namespace android {
28+
namespace init {
29+
30+
Result<std::pair<int, rlimit>> ParseRlimit(const std::vector<std::string>& args);
31+
32+
} // namespace init
33+
} // namespace android
34+
35+
#endif

init/rlimit_parser_test.cpp

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright (C) 2017 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "rlimit_parser.h"
18+
19+
#include <iostream>
20+
21+
#include <gtest/gtest.h>
22+
23+
namespace android {
24+
namespace init {
25+
26+
void TestRlimitSuccess(std::vector<std::string> input,
27+
const std::pair<int, rlimit>& expected_result) {
28+
input.emplace(input.begin(), "");
29+
ASSERT_EQ(4U, input.size());
30+
auto result = ParseRlimit(input);
31+
32+
ASSERT_TRUE(result) << "input: " << input[1];
33+
const auto& [resource, rlimit] = *result;
34+
const auto& [expected_resource, expected_rlimit] = expected_result;
35+
EXPECT_EQ(expected_resource, resource);
36+
EXPECT_EQ(expected_rlimit.rlim_cur, rlimit.rlim_cur);
37+
EXPECT_EQ(expected_rlimit.rlim_max, rlimit.rlim_max);
38+
}
39+
40+
void TestRlimitFailure(std::vector<std::string> input, const std::string& expected_result) {
41+
input.emplace(input.begin(), "");
42+
ASSERT_EQ(4U, input.size());
43+
auto result = ParseRlimit(input);
44+
45+
ASSERT_FALSE(result) << "input: " << input[1];
46+
EXPECT_EQ(expected_result, result.error_string());
47+
EXPECT_EQ(0, result.error_errno());
48+
}
49+
50+
TEST(rlimit, RlimitSuccess) {
51+
const std::vector<std::pair<std::vector<std::string>, std::pair<int, rlimit>>>
52+
inputs_and_results = {
53+
{{"cpu", "10", "10"}, {0, {10, 10}}},
54+
{{"fsize", "10", "10"}, {1, {10, 10}}},
55+
{{"data", "10", "10"}, {2, {10, 10}}},
56+
{{"stack", "10", "10"}, {3, {10, 10}}},
57+
{{"core", "10", "10"}, {4, {10, 10}}},
58+
{{"rss", "10", "10"}, {5, {10, 10}}},
59+
{{"nproc", "10", "10"}, {6, {10, 10}}},
60+
{{"nofile", "10", "10"}, {7, {10, 10}}},
61+
{{"memlock", "10", "10"}, {8, {10, 10}}},
62+
{{"as", "10", "10"}, {9, {10, 10}}},
63+
{{"locks", "10", "10"}, {10, {10, 10}}},
64+
{{"sigpending", "10", "10"}, {11, {10, 10}}},
65+
{{"msgqueue", "10", "10"}, {12, {10, 10}}},
66+
{{"nice", "10", "10"}, {13, {10, 10}}},
67+
{{"rtprio", "10", "10"}, {14, {10, 10}}},
68+
{{"rttime", "10", "10"}, {15, {10, 10}}},
69+
70+
{{"RLIM_CPU", "10", "10"}, {0, {10, 10}}},
71+
{{"RLIM_FSIZE", "10", "10"}, {1, {10, 10}}},
72+
{{"RLIM_DATA", "10", "10"}, {2, {10, 10}}},
73+
{{"RLIM_STACK", "10", "10"}, {3, {10, 10}}},
74+
{{"RLIM_CORE", "10", "10"}, {4, {10, 10}}},
75+
{{"RLIM_RSS", "10", "10"}, {5, {10, 10}}},
76+
{{"RLIM_NPROC", "10", "10"}, {6, {10, 10}}},
77+
{{"RLIM_NOFILE", "10", "10"}, {7, {10, 10}}},
78+
{{"RLIM_MEMLOCK", "10", "10"}, {8, {10, 10}}},
79+
{{"RLIM_AS", "10", "10"}, {9, {10, 10}}},
80+
{{"RLIM_LOCKS", "10", "10"}, {10, {10, 10}}},
81+
{{"RLIM_SIGPENDING", "10", "10"}, {11, {10, 10}}},
82+
{{"RLIM_MSGQUEUE", "10", "10"}, {12, {10, 10}}},
83+
{{"RLIM_NICE", "10", "10"}, {13, {10, 10}}},
84+
{{"RLIM_RTPRIO", "10", "10"}, {14, {10, 10}}},
85+
{{"RLIM_RTTIME", "10", "10"}, {15, {10, 10}}},
86+
87+
{{"0", "10", "10"}, {0, {10, 10}}},
88+
{{"1", "10", "10"}, {1, {10, 10}}},
89+
{{"2", "10", "10"}, {2, {10, 10}}},
90+
{{"3", "10", "10"}, {3, {10, 10}}},
91+
{{"4", "10", "10"}, {4, {10, 10}}},
92+
{{"5", "10", "10"}, {5, {10, 10}}},
93+
{{"6", "10", "10"}, {6, {10, 10}}},
94+
{{"7", "10", "10"}, {7, {10, 10}}},
95+
{{"8", "10", "10"}, {8, {10, 10}}},
96+
{{"9", "10", "10"}, {9, {10, 10}}},
97+
{{"10", "10", "10"}, {10, {10, 10}}},
98+
{{"11", "10", "10"}, {11, {10, 10}}},
99+
{{"12", "10", "10"}, {12, {10, 10}}},
100+
{{"13", "10", "10"}, {13, {10, 10}}},
101+
{{"14", "10", "10"}, {14, {10, 10}}},
102+
{{"15", "10", "10"}, {15, {10, 10}}},
103+
};
104+
105+
for (const auto& [input, expected_result] : inputs_and_results) {
106+
TestRlimitSuccess(input, expected_result);
107+
}
108+
}
109+
110+
TEST(rlimit, RlimitFailure) {
111+
const std::vector<std::pair<std::vector<std::string>, std::string>> inputs_and_results = {
112+
{{"-4", "10", "10"}, "Resource '-4' below the minimum resource value '0'"},
113+
{{"100", "10", "10"}, "Resource '100' over the maximum resource value '16'"},
114+
{{"bad_string", "10", "10"}, "Could not parse resource 'bad_string'"},
115+
{{"RLIM_", "10", "10"}, "Could not parse resource 'RLIM_'"},
116+
{{"cpu", "abc", "10"}, "Could not parse soft limit 'abc'"},
117+
{{"cpu", "10", "abc"}, "Could not parse hard limit 'abc'"},
118+
};
119+
120+
for (const auto& [input, expected_result] : inputs_and_results) {
121+
TestRlimitFailure(input, expected_result);
122+
}
123+
}
124+
125+
} // namespace init
126+
} // namespace android

init/service.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
#include "init.h"
4545
#include "property_service.h"
46+
#include "rlimit_parser.h"
4647
#include "util.h"
4748

4849
using android::base::boot_clock;
@@ -216,6 +217,12 @@ void Service::KillProcessGroup(int signal) {
216217
}
217218

218219
void Service::SetProcessAttributes() {
220+
for (const auto& rlimit : rlimits_) {
221+
if (setrlimit(rlimit.first, &rlimit.second) == -1) {
222+
LOG(FATAL) << StringPrintf("setrlimit(%d, {rlim_cur=%ld, rlim_max=%ld}) failed",
223+
rlimit.first, rlimit.second.rlim_cur, rlimit.second.rlim_max);
224+
}
225+
}
219226
// Keep capabilites on uid change.
220227
if (capabilities_.any() && uid_) {
221228
// If Android is running in a container, some securebits might already
@@ -489,6 +496,14 @@ Result<Success> Service::ParseMemcgSoftLimitInBytes(const std::vector<std::strin
489496
return Success();
490497
}
491498

499+
Result<Success> Service::ParseProcessRlimit(const std::vector<std::string>& args) {
500+
auto rlimit = ParseRlimit(args);
501+
if (!rlimit) return rlimit.error();
502+
503+
rlimits_.emplace_back(*rlimit);
504+
return Success();
505+
}
506+
492507
Result<Success> Service::ParseSeclabel(const std::vector<std::string>& args) {
493508
seclabel_ = args[1];
494509
return Success();
@@ -609,6 +624,7 @@ const Service::OptionParserMap::Map& Service::OptionParserMap::map() const {
609624
{"memcg.limit_in_bytes",
610625
{1, 1, &Service::ParseMemcgLimitInBytes}},
611626
{"namespace", {1, 2, &Service::ParseNamespace}},
627+
{"rlimit", {3, 3, &Service::ParseProcessRlimit}},
612628
{"seclabel", {1, 1, &Service::ParseSeclabel}},
613629
{"setenv", {2, 2, &Service::ParseSetenv}},
614630
{"shutdown", {1, 1, &Service::ParseShutdown}},

init/service.h

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#ifndef _INIT_SERVICE_H
1818
#define _INIT_SERVICE_H
1919

20+
#include <sys/resource.h>
2021
#include <sys/types.h>
2122

2223
#include <memory>
@@ -138,6 +139,7 @@ class Service {
138139
Result<Success> ParseMemcgSoftLimitInBytes(const std::vector<std::string>& args);
139140
Result<Success> ParseMemcgSwappiness(const std::vector<std::string>& args);
140141
Result<Success> ParseNamespace(const std::vector<std::string>& args);
142+
Result<Success> ParseProcessRlimit(const std::vector<std::string>& args);
141143
Result<Success> ParseSeclabel(const std::vector<std::string>& args);
142144
Result<Success> ParseSetenv(const std::vector<std::string>& args);
143145
Result<Success> ParseShutdown(const std::vector<std::string>& args);
@@ -195,6 +197,8 @@ class Service {
195197

196198
unsigned long start_order_;
197199

200+
std::vector<std::pair<int, rlimit>> rlimits_;
201+
198202
std::vector<std::string> args_;
199203
};
200204

0 commit comments

Comments
 (0)