Home | History | Annotate | Download | only in init
      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
     79