20 #include "cgroupsparser.h" 21 #include "cgroupsgateway.h" 22 #include "jsonparser.h" 28 CGroupsParser::CGroupsParser()
33 std::string CGroupsParser::convertToBytes(
const std::string settingValue,
const int multiply)
35 long long limit_in_bytes = std::strtoll(settingValue.c_str(), NULL, 10);
36 if (limit_in_bytes == std::numeric_limits<long long>::max() ||
37 limit_in_bytes == std::numeric_limits<long long>::min()) {
38 if (errno == ERANGE) {
39 std::string errMessage = settingValue +
" is out of range";
40 log_error() << errMessage;
41 throw LimitRangeError(errMessage);
45 if (limit_in_bytes > llrint(std::numeric_limits<long long>::max() / multiply)) {
46 std::string errMessage = settingValue +
" * " + std::to_string(multiply) +
" is out of range";
47 log_error() << errMessage;
48 throw LimitRangeError(errMessage);
51 limit_in_bytes *= multiply;
52 return std::to_string(limit_in_bytes);
55 std::string CGroupsParser::suffixCorrection(
const std::string settingValue)
57 auto suffix = settingValue.back();
58 std::string removeUnit = settingValue;
59 removeUnit.pop_back();
61 if (std::isdigit(suffix)) {
64 }
else if (
'K' == suffix ||
'k' == suffix) {
67 return convertToBytes(removeUnit, KB);
68 }
else if (
'M' == suffix ||
'm' == suffix) {
71 return convertToBytes(removeUnit, MB);
72 }
else if (
'G' == suffix ||
'g' == suffix) {
74 int GB = 1024 * 1024 * 1024;
75 return convertToBytes(removeUnit, GB);
77 std::string errMessage =
"Bad suffix on setting value " + settingValue;
78 log_error() << errMessage;
79 throw BadSuffixError(errMessage);
82 void CGroupsParser::parseCGroupsGatewayConfiguration(
const json_t *element)
84 std::string settingKey;
85 std::string settingValue;
88 std::string errMessage =
"Key \"setting\" either not a string or not in json configuration";
89 log_error() << errMessage;
90 throw JSonError(errMessage);
94 std::string errMessage =
"Key \"value\" either not a string or not in json configuration";
95 log_error() << errMessage;
96 throw JSonError(errMessage);
99 if ((
"memory.limit_in_bytes" == settingKey) || (
"memory.memsw.limit_in_bytes" == settingKey)) {
100 settingValue = suffixCorrection(settingValue);
103 if (m_settings.count(settingKey) != 0) {
104 if (std::stoll(m_settings[settingKey]) >= std::stoll(settingValue)) {
109 }
else if ((
"cpu.shares" == settingKey)) {
112 newValue = std::stoi(settingValue);
113 }
catch (std::out_of_range &err) {
114 std::string errorMessage =
"The value for cpu.shares is too high";
115 log_error() << errorMessage;
116 throw InvalidInputError(errorMessage);
117 }
catch (std::invalid_argument &err) {
118 std::string errorMessage =
"The value for cpu.shares is not an integer";
119 log_error() << errorMessage;
120 throw InvalidInputError(errorMessage);
124 std::string errorMessage =
"Value of cpu.shares must be at least 2";
125 log_error() << errorMessage;
126 throw InvalidInputError(errorMessage);
129 if (m_settings.count(settingKey) != 0) {
130 if (std::stoi(m_settings[settingKey]) >= newValue) {
134 settingValue = std::to_string(newValue);
135 log_debug() <<
"Value for cpu.shares: " << settingValue;
137 }
else if (
"net_cls.classid" == settingKey) {
139 if (settingValue.find(
"0x") != 0
140 || settingValue.length() > 10
141 || settingValue.length() < 7)
143 std::string errorMessage =
"net_cls.classid should be of form 0xAAAABBBB";
144 log_error() << errorMessage;
145 throw InvalidInputError(errorMessage);
149 unsigned long int hexedValue = strtoul(settingValue.c_str(), &endPtr, 16);
150 if (0 == hexedValue || ULONG_MAX == hexedValue || ((endPtr != NULL) && *endPtr !=
'\0')) {
151 std::string errorMessage =
"Could not parse all of net_cls.classid value as hex string";
152 log_error() << errorMessage;
153 throw InvalidInputError(errorMessage);
157 log_warning() << settingKey <<
" is not supported by CGroups Gateway" ;
161 m_settings[settingKey] = settingValue;
164 const std::map<std::string, std::string> &CGroupsParser::getSettings()
Developers guide to adding a config item:
static bool read(const json_t *element, const char *key, std::string &result)
Reads a string from a JSON Object.