init: don't use magic numbers for RLIMIT_ constants.

Also, why are we accepting the completely made-up "RLIM_AS" but not the
real "RLIMIT_AS" in .rc files?

Bug: http://b/293894041
Test: treehugger
Change-Id: I18b10b6dd77265a9a14b88bfdf1cc0b474800a94
This commit is contained in:
Elliott Hughes 2023-10-16 22:28:11 +00:00
parent a7dd77670e
commit 7a19bf8e0d
2 changed files with 30 additions and 4 deletions

View file

@ -30,10 +30,14 @@ 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},
{"cpu", RLIMIT_CPU}, {"fsize", RLIMIT_FSIZE},
{"data", RLIMIT_DATA}, {"stack", RLIMIT_STACK},
{"core", RLIMIT_CORE}, {"rss", RLIMIT_RSS},
{"nproc", RLIMIT_NPROC}, {"nofile", RLIMIT_NOFILE},
{"memlock", RLIMIT_MEMLOCK}, {"as", RLIMIT_AS},
{"locks", RLIMIT_LOCKS}, {"sigpending", RLIMIT_SIGPENDING},
{"msgqueue", RLIMIT_MSGQUEUE}, {"nice", RLIMIT_NICE},
{"rtprio", RLIMIT_RTPRIO}, {"rttime", RLIMIT_RTTIME},
};
int resource;
@ -49,6 +53,8 @@ Result<std::pair<int, rlimit>> ParseRlimit(const std::vector<std::string>& args)
std::string resource_string;
if (StartsWith(args[1], "RLIM_")) {
resource_string = args[1].substr(5);
} else if (StartsWith(args[1], "RLIMIT_")) {
resource_string = args[1].substr(7);
} else {
resource_string = args[1];
}

View file

@ -67,6 +67,7 @@ TEST(rlimit, RlimitSuccess) {
{{"rtprio", "10", "10"}, {14, {10, 10}}},
{{"rttime", "10", "10"}, {15, {10, 10}}},
// For some reason, we spelled these wrong.
{{"RLIM_CPU", "10", "10"}, {0, {10, 10}}},
{{"RLIM_FSIZE", "10", "10"}, {1, {10, 10}}},
{{"RLIM_DATA", "10", "10"}, {2, {10, 10}}},
@ -84,6 +85,24 @@ TEST(rlimit, RlimitSuccess) {
{{"RLIM_RTPRIO", "10", "10"}, {14, {10, 10}}},
{{"RLIM_RTTIME", "10", "10"}, {15, {10, 10}}},
// These are the correct spellings.
{{"RLIMIT_CPU", "10", "10"}, {0, {10, 10}}},
{{"RLIMIT_FSIZE", "10", "10"}, {1, {10, 10}}},
{{"RLIMIT_DATA", "10", "10"}, {2, {10, 10}}},
{{"RLIMIT_STACK", "10", "10"}, {3, {10, 10}}},
{{"RLIMIT_CORE", "10", "10"}, {4, {10, 10}}},
{{"RLIMIT_RSS", "10", "10"}, {5, {10, 10}}},
{{"RLIMIT_NPROC", "10", "10"}, {6, {10, 10}}},
{{"RLIMIT_NOFILE", "10", "10"}, {7, {10, 10}}},
{{"RLIMIT_MEMLOCK", "10", "10"}, {8, {10, 10}}},
{{"RLIMIT_AS", "10", "10"}, {9, {10, 10}}},
{{"RLIMIT_LOCKS", "10", "10"}, {10, {10, 10}}},
{{"RLIMIT_SIGPENDING", "10", "10"}, {11, {10, 10}}},
{{"RLIMIT_MSGQUEUE", "10", "10"}, {12, {10, 10}}},
{{"RLIMIT_NICE", "10", "10"}, {13, {10, 10}}},
{{"RLIMIT_RTPRIO", "10", "10"}, {14, {10, 10}}},
{{"RLIMIT_RTTIME", "10", "10"}, {15, {10, 10}}},
{{"0", "10", "10"}, {0, {10, 10}}},
{{"1", "10", "10"}, {1, {10, 10}}},
{{"2", "10", "10"}, {2, {10, 10}}},
@ -113,6 +132,7 @@ TEST(rlimit, RlimitFailure) {
{{"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_'"},
{{"RLIMIT_", "10", "10"}, "Could not parse resource 'RLIMIT_'"},
{{"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'"},