From 7a19bf8e0dffa084e0cc342500afa0632669248a Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 16 Oct 2023 22:28:11 +0000 Subject: [PATCH] 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 --- init/rlimit_parser.cpp | 14 ++++++++++---- init/rlimit_parser_test.cpp | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/init/rlimit_parser.cpp b/init/rlimit_parser.cpp index 476a46ab8..c2a3fa129 100644 --- a/init/rlimit_parser.cpp +++ b/init/rlimit_parser.cpp @@ -30,10 +30,14 @@ namespace init { // Builtins and service definitions both have their arguments start at 1 and finish at 3. Result> ParseRlimit(const std::vector& args) { static const std::vector> 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> ParseRlimit(const std::vector& 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]; } diff --git a/init/rlimit_parser_test.cpp b/init/rlimit_parser_test.cpp index 3c3f848b5..a6955f6b4 100644 --- a/init/rlimit_parser_test.cpp +++ b/init/rlimit_parser_test.cpp @@ -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'"},