Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3aee7196 authored by Elliott Hughes's avatar Elliott Hughes Committed by Gerrit Code Review
Browse files

Merge "init: don't use magic numbers for RLIMIT_ constants." into main

parents 57b34ae3 7a19bf8e
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -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];
        }
+20 −0
Original line number Diff line number Diff line
@@ -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'"},