xref: /aosp_15_r20/system/core/init/rlimit_parser.cpp (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
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.
ParseRlimit(const std::vector<std::string> & args)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", RLIMIT_CPU},           {"fsize", RLIMIT_FSIZE},
34              {"data", RLIMIT_DATA},         {"stack", RLIMIT_STACK},
35              {"core", RLIMIT_CORE},         {"rss", RLIMIT_RSS},
36              {"nproc", RLIMIT_NPROC},       {"nofile", RLIMIT_NOFILE},
37              {"memlock", RLIMIT_MEMLOCK},   {"as", RLIMIT_AS},
38              {"locks", RLIMIT_LOCKS},       {"sigpending", RLIMIT_SIGPENDING},
39              {"msgqueue", RLIMIT_MSGQUEUE}, {"nice", RLIMIT_NICE},
40              {"rtprio", RLIMIT_RTPRIO},     {"rttime", RLIMIT_RTTIME},
41      };
42  
43      int resource;
44  
45      if (ParseInt(args[1], &resource)) {
46          if (resource >= RLIM_NLIMITS) {
47              return Error() << "Resource '" << args[1] << "' over the maximum resource value '"
48                             << RLIM_NLIMITS << "'";
49          } else if (resource < 0) {
50              return Error() << "Resource '" << args[1] << "' below the minimum resource value '0'";
51          }
52      } else {
53          std::string resource_string;
54          if (StartsWith(args[1], "RLIM_")) {
55              resource_string = args[1].substr(5);
56          } else if (StartsWith(args[1], "RLIMIT_")) {
57              resource_string = args[1].substr(7);
58          } else {
59              resource_string = args[1];
60          }
61  
62          auto it = std::find_if(text_to_resources.begin(), text_to_resources.end(),
63                                 [&resource_string](const auto& entry) {
64                                     return EqualsIgnoreCase(resource_string, entry.first);
65                                 });
66          if (it == text_to_resources.end()) {
67              return Error() << "Could not parse resource '" << args[1] << "'";
68          }
69  
70          resource = it->second;
71      }
72  
73      rlimit limit;
74      if (args[2] == "-1" || args[2] == "unlimited") {
75          limit.rlim_cur = RLIM_INFINITY;
76      } else if (!ParseUint(args[2], &limit.rlim_cur)) {
77          return Error() << "Could not parse soft limit '" << args[2] << "'";
78      }
79  
80      if (args[3] == "-1" || args[3] == "unlimited") {
81          limit.rlim_max = RLIM_INFINITY;
82      } else if (!ParseUint(args[3], &limit.rlim_max)) {
83          return Error() << "Could not parse hard limit '" << args[3] << "'";
84      }
85  
86      return std::pair{resource, limit};
87  }
88  
89  }  // namespace init
90  }  // namespace android
91