init: accept -1 or 'unlimited' for an infinite rlimit

Due to a bug with ParseUint(), init would defacto accept -1 for an
infinite rlimit, but only on 64bit devices.  That bug is now fixed,
such that -1 would be rejected by ParseUint() for all devices.

This change explicitly checks for -1 for all devices or 'unlimited' to
match ulimit's reporting and accepts either as an infinite rlimit.

Bug: 112668205
Test: new (and old) unit tests
Change-Id: Ie28ff622cdf375a65ceb5f32ffb14fb3d5d9f2ba
This commit is contained in:
Tom Cherry 2018-08-22 14:55:56 -07:00
parent 8e3f0b1b2a
commit ab434c70ce
3 changed files with 69 additions and 58 deletions

View file

@ -506,6 +506,7 @@ Commands
_resource_ is best specified using its text representation ('cpu', 'rtio', etc
or 'RLIM_CPU', 'RLIM_RTIO', etc). It also may be specified as the int value
that the resource enum corresponds to.
_cur_ and _max_ can be 'unlimited' or '-1' to indicate an infinite rlimit.
`start <service>`
> Start a service running if it is not already running.

View file

@ -65,12 +65,18 @@ Result<std::pair<int, rlimit>> ParseRlimit(const std::vector<std::string>& args)
}
rlimit limit;
if (!ParseUint(args[2], &limit.rlim_cur)) {
if (args[2] == "-1" || args[2] == "unlimited") {
limit.rlim_cur = RLIM_INFINITY;
} else if (!ParseUint(args[2], &limit.rlim_cur)) {
return Error() << "Could not parse soft limit '" << args[2] << "'";
}
if (!ParseUint(args[3], &limit.rlim_max)) {
if (args[3] == "-1" || args[3] == "unlimited") {
limit.rlim_max = RLIM_INFINITY;
} else if (!ParseUint(args[3], &limit.rlim_max)) {
return Error() << "Could not parse hard limit '" << args[3] << "'";
}
return {resource, limit};
}

View file

@ -49,58 +49,58 @@ void TestRlimitFailure(std::vector<std::string> input, const std::string& expect
TEST(rlimit, RlimitSuccess) {
const std::vector<std::pair<std::vector<std::string>, std::pair<int, rlimit>>>
inputs_and_results = {
{{"cpu", "10", "10"}, {0, {10, 10}}},
{{"fsize", "10", "10"}, {1, {10, 10}}},
{{"data", "10", "10"}, {2, {10, 10}}},
{{"stack", "10", "10"}, {3, {10, 10}}},
{{"core", "10", "10"}, {4, {10, 10}}},
{{"rss", "10", "10"}, {5, {10, 10}}},
{{"nproc", "10", "10"}, {6, {10, 10}}},
{{"nofile", "10", "10"}, {7, {10, 10}}},
{{"memlock", "10", "10"}, {8, {10, 10}}},
{{"as", "10", "10"}, {9, {10, 10}}},
{{"locks", "10", "10"}, {10, {10, 10}}},
{{"sigpending", "10", "10"}, {11, {10, 10}}},
{{"msgqueue", "10", "10"}, {12, {10, 10}}},
{{"nice", "10", "10"}, {13, {10, 10}}},
{{"rtprio", "10", "10"}, {14, {10, 10}}},
{{"rttime", "10", "10"}, {15, {10, 10}}},
inputs_and_results = {
{{"cpu", "10", "10"}, {0, {10, 10}}},
{{"fsize", "10", "10"}, {1, {10, 10}}},
{{"data", "10", "10"}, {2, {10, 10}}},
{{"stack", "10", "10"}, {3, {10, 10}}},
{{"core", "10", "10"}, {4, {10, 10}}},
{{"rss", "10", "10"}, {5, {10, 10}}},
{{"nproc", "10", "10"}, {6, {10, 10}}},
{{"nofile", "10", "10"}, {7, {10, 10}}},
{{"memlock", "10", "10"}, {8, {10, 10}}},
{{"as", "10", "10"}, {9, {10, 10}}},
{{"locks", "10", "10"}, {10, {10, 10}}},
{{"sigpending", "10", "10"}, {11, {10, 10}}},
{{"msgqueue", "10", "10"}, {12, {10, 10}}},
{{"nice", "10", "10"}, {13, {10, 10}}},
{{"rtprio", "10", "10"}, {14, {10, 10}}},
{{"rttime", "10", "10"}, {15, {10, 10}}},
{{"RLIM_CPU", "10", "10"}, {0, {10, 10}}},
{{"RLIM_FSIZE", "10", "10"}, {1, {10, 10}}},
{{"RLIM_DATA", "10", "10"}, {2, {10, 10}}},
{{"RLIM_STACK", "10", "10"}, {3, {10, 10}}},
{{"RLIM_CORE", "10", "10"}, {4, {10, 10}}},
{{"RLIM_RSS", "10", "10"}, {5, {10, 10}}},
{{"RLIM_NPROC", "10", "10"}, {6, {10, 10}}},
{{"RLIM_NOFILE", "10", "10"}, {7, {10, 10}}},
{{"RLIM_MEMLOCK", "10", "10"}, {8, {10, 10}}},
{{"RLIM_AS", "10", "10"}, {9, {10, 10}}},
{{"RLIM_LOCKS", "10", "10"}, {10, {10, 10}}},
{{"RLIM_SIGPENDING", "10", "10"}, {11, {10, 10}}},
{{"RLIM_MSGQUEUE", "10", "10"}, {12, {10, 10}}},
{{"RLIM_NICE", "10", "10"}, {13, {10, 10}}},
{{"RLIM_RTPRIO", "10", "10"}, {14, {10, 10}}},
{{"RLIM_RTTIME", "10", "10"}, {15, {10, 10}}},
{{"RLIM_CPU", "10", "10"}, {0, {10, 10}}},
{{"RLIM_FSIZE", "10", "10"}, {1, {10, 10}}},
{{"RLIM_DATA", "10", "10"}, {2, {10, 10}}},
{{"RLIM_STACK", "10", "10"}, {3, {10, 10}}},
{{"RLIM_CORE", "10", "10"}, {4, {10, 10}}},
{{"RLIM_RSS", "10", "10"}, {5, {10, 10}}},
{{"RLIM_NPROC", "10", "10"}, {6, {10, 10}}},
{{"RLIM_NOFILE", "10", "10"}, {7, {10, 10}}},
{{"RLIM_MEMLOCK", "10", "10"}, {8, {10, 10}}},
{{"RLIM_AS", "10", "10"}, {9, {10, 10}}},
{{"RLIM_LOCKS", "10", "10"}, {10, {10, 10}}},
{{"RLIM_SIGPENDING", "10", "10"}, {11, {10, 10}}},
{{"RLIM_MSGQUEUE", "10", "10"}, {12, {10, 10}}},
{{"RLIM_NICE", "10", "10"}, {13, {10, 10}}},
{{"RLIM_RTPRIO", "10", "10"}, {14, {10, 10}}},
{{"RLIM_RTTIME", "10", "10"}, {15, {10, 10}}},
{{"0", "10", "10"}, {0, {10, 10}}},
{{"1", "10", "10"}, {1, {10, 10}}},
{{"2", "10", "10"}, {2, {10, 10}}},
{{"3", "10", "10"}, {3, {10, 10}}},
{{"4", "10", "10"}, {4, {10, 10}}},
{{"5", "10", "10"}, {5, {10, 10}}},
{{"6", "10", "10"}, {6, {10, 10}}},
{{"7", "10", "10"}, {7, {10, 10}}},
{{"8", "10", "10"}, {8, {10, 10}}},
{{"9", "10", "10"}, {9, {10, 10}}},
{{"10", "10", "10"}, {10, {10, 10}}},
{{"11", "10", "10"}, {11, {10, 10}}},
{{"12", "10", "10"}, {12, {10, 10}}},
{{"13", "10", "10"}, {13, {10, 10}}},
{{"14", "10", "10"}, {14, {10, 10}}},
{{"15", "10", "10"}, {15, {10, 10}}},
};
{{"0", "10", "10"}, {0, {10, 10}}},
{{"1", "10", "10"}, {1, {10, 10}}},
{{"2", "10", "10"}, {2, {10, 10}}},
{{"3", "10", "10"}, {3, {10, 10}}},
{{"4", "10", "10"}, {4, {10, 10}}},
{{"5", "10", "10"}, {5, {10, 10}}},
{{"6", "10", "10"}, {6, {10, 10}}},
{{"7", "10", "10"}, {7, {10, 10}}},
{{"8", "10", "10"}, {8, {10, 10}}},
{{"9", "10", "10"}, {9, {10, 10}}},
{{"10", "10", "10"}, {10, {10, 10}}},
{{"11", "10", "10"}, {11, {10, 10}}},
{{"12", "unlimited", "10"}, {12, {RLIM_INFINITY, 10}}},
{{"13", "-1", "10"}, {13, {RLIM_INFINITY, 10}}},
{{"14", "10", "unlimited"}, {14, {10, RLIM_INFINITY}}},
{{"15", "10", "-1"}, {15, {10, RLIM_INFINITY}}},
};
for (const auto& [input, expected_result] : inputs_and_results) {
TestRlimitSuccess(input, expected_result);
@ -109,12 +109,16 @@ TEST(rlimit, RlimitSuccess) {
TEST(rlimit, RlimitFailure) {
const std::vector<std::pair<std::vector<std::string>, std::string>> inputs_and_results = {
{{"-4", "10", "10"}, "Resource '-4' below the minimum resource value '0'"},
{{"100", "10", "10"}, "Resource '100' over the maximum resource value '16'"},
{{"bad_string", "10", "10"}, "Could not parse resource 'bad_string'"},
{{"RLIM_", "10", "10"}, "Could not parse resource 'RLIM_'"},
{{"cpu", "abc", "10"}, "Could not parse soft limit 'abc'"},
{{"cpu", "10", "abc"}, "Could not parse hard limit 'abc'"},
{{"-4", "10", "10"}, "Resource '-4' below the minimum resource value '0'"},
{{"100", "10", "10"}, "Resource '100' over the maximum resource value '16'"},
{{"bad_string", "10", "10"}, "Could not parse resource 'bad_string'"},
{{"RLIM_", "10", "10"}, "Could not parse resource 'RLIM_'"},
{{"cpu", "abc", "10"}, "Could not parse soft limit 'abc'"},
{{"cpu", "10", "abc"}, "Could not parse hard limit 'abc'"},
{{"cpu", "unlimit", "10"}, "Could not parse soft limit 'unlimit'"},
{{"cpu", "10", "unlimit"}, "Could not parse hard limit 'unlimit'"},
{{"cpu", "-2", "10"}, "Could not parse soft limit '-2'"},
{{"cpu", "10", "-2"}, "Could not parse hard limit '-2'"},
};
for (const auto& [input, expected_result] : inputs_and_results) {