blob: ec7d1206ad80ed4618cf6dd30c0cdba8c51bd42d [file] [log] [blame]
Inseob Kim5f8f32c2018-08-24 11:10:44 +09001/*
2 * Copyright (C) 2018 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 "Common.h"
18
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <unistd.h>
22
Inseob Kim14e51872018-10-25 14:27:33 +090023#include <algorithm>
Inseob Kim5f8f32c2018-08-24 11:10:44 +090024#include <cctype>
25#include <cerrno>
26#include <cmath>
27#include <cstdlib>
28#include <cstring>
29#include <initializer_list>
30#include <memory>
31#include <regex>
32#include <string>
33#include <unordered_set>
34#include <vector>
35
36#include <android-base/file.h>
Inseob Kim1ca03f32019-06-08 16:31:59 +090037#include <android-base/logging.h>
Inseob Kim5f8f32c2018-08-24 11:10:44 +090038#include <android-base/strings.h>
39#include <google/protobuf/text_format.h>
40
41#include "sysprop.pb.h"
42
Inseob Kim053b83d2019-06-26 13:41:51 +090043using android::base::Result;
44
Inseob Kim5f8f32c2018-08-24 11:10:44 +090045namespace {
46
Inseob Kim14e51872018-10-25 14:27:33 +090047std::string GenerateDefaultPropName(const sysprop::Properties& props,
48 const sysprop::Property& prop);
Inseob Kim5f8f32c2018-08-24 11:10:44 +090049bool IsCorrectIdentifier(const std::string& name);
Inseob Kim053b83d2019-06-26 13:41:51 +090050Result<void> ValidateProp(const sysprop::Properties& props,
51 const sysprop::Property& prop);
52Result<void> ValidateProps(const sysprop::Properties& props);
Inseob Kim5f8f32c2018-08-24 11:10:44 +090053
Inseob Kim14e51872018-10-25 14:27:33 +090054std::string GenerateDefaultPropName(const sysprop::Properties& props,
55 const sysprop::Property& prop) {
56 std::string ret;
57
58 if (prop.access() != sysprop::ReadWrite) ret = "ro.";
59
60 switch (props.owner()) {
61 case sysprop::Vendor:
62 ret += "vendor.";
63 break;
64 case sysprop::Odm:
65 ret += "odm.";
66 break;
67 default:
68 break;
69 }
70
71 ret += prop.api_name();
72
73 return ret;
74}
75
Inseob Kim5f8f32c2018-08-24 11:10:44 +090076bool IsCorrectIdentifier(const std::string& name) {
77 if (name.empty()) return false;
78 if (std::isalpha(name[0]) == 0 && name[0] != '_') return false;
79
Inseob Kim14e51872018-10-25 14:27:33 +090080 return std::all_of(name.begin() + 1, name.end(), [](char ch) {
81 return std::isalnum(ch) != 0 || ch == '_';
82 });
Inseob Kim5f8f32c2018-08-24 11:10:44 +090083}
84
Jiyong Park87d439d2019-11-27 19:24:07 +090085bool IsCorrectName(const std::string& name,
86 const std::unordered_set<char>& allowed_chars) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +090087 if (name.empty()) return false;
Jiyong Park87d439d2019-11-27 19:24:07 +090088 if (!std::isalpha(*name.begin())) return false;
Inseob Kim5f8f32c2018-08-24 11:10:44 +090089
Jiyong Park87d439d2019-11-27 19:24:07 +090090 return std::all_of(name.begin(), name.end(), [allowed_chars](char ch) {
91 return std::isalnum(ch) != 0 || allowed_chars.count(ch) != 0;
Inseob Kim14e51872018-10-25 14:27:33 +090092 });
Inseob Kim5f8f32c2018-08-24 11:10:44 +090093}
94
Jiyong Park87d439d2019-11-27 19:24:07 +090095bool IsCorrectPropertyName(const std::string& name) {
96 std::unordered_set<char> allowed{'_', '-', '.'};
97 if (android::base::StartsWith(name, "ctl.")) {
98 allowed.emplace('$');
99 }
100 return IsCorrectName(name, allowed);
101}
102
103bool IsCorrectApiName(const std::string& name) {
104 static std::unordered_set<char> allowed{'_', '-'};
105 return IsCorrectName(name, allowed);
106}
107
Inseob Kim053b83d2019-06-26 13:41:51 +0900108Result<void> ValidateProp(const sysprop::Properties& props,
109 const sysprop::Property& prop) {
Jiyong Park87d439d2019-11-27 19:24:07 +0900110 if (!IsCorrectApiName(prop.api_name())) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900111 return Errorf("Invalid API name \"{}\"", prop.api_name());
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900112 }
113
114 if (prop.type() == sysprop::Enum || prop.type() == sysprop::EnumList) {
115 std::vector<std::string> names =
116 android::base::Split(prop.enum_values(), "|");
117 if (names.empty()) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900118 return Errorf("Enum values are empty for API \"{}\"", prop.api_name());
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900119 }
120
121 for (const std::string& name : names) {
122 if (!IsCorrectIdentifier(name)) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900123 return Errorf("Invalid enum value \"{}\" for API \"{}\"", name,
124 prop.api_name());
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900125 }
126 }
127
128 std::unordered_set<std::string> name_set;
129 for (const std::string& name : names) {
Inseob Kimf346e502019-01-04 10:32:39 +0900130 if (!name_set.insert(ToUpper(name)).second) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900131 return Errorf("Duplicated enum value \"{}\" for API \"{}\"", name,
132 prop.api_name());
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900133 }
134 }
135 }
136
Inseob Kim14e51872018-10-25 14:27:33 +0900137 std::string prop_name = prop.prop_name();
138 if (prop_name.empty()) prop_name = GenerateDefaultPropName(props, prop);
139
Jiyong Park87d439d2019-11-27 19:24:07 +0900140 if (!IsCorrectPropertyName(prop_name)) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900141 return Errorf("Invalid prop name \"{}\"", prop.prop_name());
Inseob Kim14e51872018-10-25 14:27:33 +0900142 }
143
Inseob Kim3f452c02020-03-31 13:41:19 +0900144 std::string legacy_name = prop.legacy_prop_name();
Inseob Kim96959fb2020-04-22 20:13:49 +0900145 if (!legacy_name.empty()) {
146 if (!IsCorrectPropertyName(legacy_name)) {
147 return Errorf("Invalid legacy prop name \"{}\"", legacy_name);
148 }
149 if (prop.access() != sysprop::Readonly) {
150 return Errorf("Prop \"{}\" which has legacy_prop_name must be Readonly",
151 prop.prop_name());
152 }
Inseob Kim3f452c02020-03-31 13:41:19 +0900153 }
154
Inseob Kimfcb5db72019-03-15 14:33:01 +0900155 static const std::regex vendor_regex(
156 "(init\\.svc\\.|ro\\.|persist\\.)?vendor\\..+|ro\\.hardware\\..+");
157 static const std::regex odm_regex(
158 "(init\\.svc\\.|ro\\.|persist\\.)?odm\\..+|ro\\.hardware\\..+");
Inseob Kim14e51872018-10-25 14:27:33 +0900159
160 switch (props.owner()) {
161 case sysprop::Platform:
162 if (std::regex_match(prop_name, vendor_regex) ||
163 std::regex_match(prop_name, odm_regex)) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900164 return Errorf(
165 "Prop \"{}\" owned by platform cannot have vendor. or odm. "
166 "namespace",
167 prop_name);
Inseob Kim14e51872018-10-25 14:27:33 +0900168 }
169 break;
170 case sysprop::Vendor:
171 if (!std::regex_match(prop_name, vendor_regex)) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900172 return Errorf(
173 "Prop \"{}\" owned by vendor should have vendor. namespace",
174 prop_name);
Inseob Kim14e51872018-10-25 14:27:33 +0900175 }
176 break;
177 case sysprop::Odm:
178 if (!std::regex_match(prop_name, odm_regex)) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900179 return Errorf("Prop \"{}\" owned by odm should have odm. namespace",
180 prop_name);
Inseob Kim14e51872018-10-25 14:27:33 +0900181 }
182 break;
Inseob Kim8802c7d2018-11-02 15:01:47 +0900183 default:
184 break;
Inseob Kim14e51872018-10-25 14:27:33 +0900185 }
186
Inseob Kim1d166c22020-07-22 11:05:24 +0900187 if (prop.access() == sysprop::ReadWrite &&
188 android::base::StartsWith(prop_name, "ro.")) {
189 return Errorf("Prop \"{}\" is ReadWrite and also have prefix \"ro.\"",
190 prop_name);
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900191 }
192
Inseob Kim9c5147d2019-03-06 11:36:36 +0900193 if (prop.integer_as_bool() && !(prop.type() == sysprop::Boolean ||
194 prop.type() == sysprop::BooleanList)) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900195 return Errorf("Prop \"{}\" has integer_as_bool: true, but not a boolean",
196 prop_name);
Inseob Kim9c5147d2019-03-06 11:36:36 +0900197 }
198
Inseob Kim053b83d2019-06-26 13:41:51 +0900199 return {};
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900200}
201
Inseob Kim053b83d2019-06-26 13:41:51 +0900202Result<void> ValidateProps(const sysprop::Properties& props) {
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900203 std::vector<std::string> names = android::base::Split(props.module(), ".");
204 if (names.size() <= 1) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900205 return Errorf("Invalid module name \"{}\"", props.module());
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900206 }
207
208 for (const auto& name : names) {
209 if (!IsCorrectIdentifier(name)) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900210 return Errorf("Invalid name \"{}\" in module", name);
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900211 }
212 }
213
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900214 if (props.prop_size() == 0) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900215 return Errorf("There is no defined property");
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900216 }
217
218 for (int i = 0; i < props.prop_size(); ++i) {
219 const auto& prop = props.prop(i);
Bernie Innocenti7ac8da42020-02-06 23:30:12 +0900220 if (auto res = ValidateProp(props, prop); !res.ok()) return res;
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900221 }
222
223 std::unordered_set<std::string> prop_names;
Inseob Kim1d316fc2020-04-09 22:29:28 +0900224 std::unordered_map<std::string, sysprop::Type> prop_types;
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900225
226 for (int i = 0; i < props.prop_size(); ++i) {
227 const auto& prop = props.prop(i);
Inseob Kim14e51872018-10-25 14:27:33 +0900228 auto res = prop_names.insert(ApiNameToIdentifier(prop.api_name()));
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900229
230 if (!res.second) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900231 return Errorf("Duplicated API name \"{}\"", prop.api_name());
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900232 }
Inseob Kim1d316fc2020-04-09 22:29:28 +0900233
234 std::vector<std::string> prop_names{prop.prop_name()};
235 std::string legacy_name = prop.legacy_prop_name();
236 if (!legacy_name.empty()) prop_names.push_back(legacy_name);
237
238 sysprop::Type type = prop.type();
239
240 for (auto& name : prop_names) {
241 // get type if already exists. inserts mine if not.
242 sysprop::Type prev_type = prop_types.emplace(name, type).first->second;
243 if (prev_type != type) {
244 return Errorf("Type error on prop \"{}\": it's {} but was {}", name,
245 sysprop::Type_Name(type), sysprop::Type_Name(prev_type));
246 }
247 }
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900248 }
249
Inseob Kim053b83d2019-06-26 13:41:51 +0900250 return {};
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900251}
252
Inseob Kim900fbad2019-06-26 14:09:20 +0900253void SetDefaultValues(sysprop::Properties* props) {
254 for (int i = 0; i < props->prop_size(); ++i) {
255 // set each optional field to its default value
256 sysprop::Property& prop = *props->mutable_prop(i);
257 if (prop.prop_name().empty())
258 prop.set_prop_name(GenerateDefaultPropName(*props, prop));
259 if (prop.scope() == sysprop::Scope::System) {
260 LOG(WARNING) << "Sysprop API " << prop.api_name()
261 << ": System scope is deprecated."
262 << " Please use Public scope instead.";
263 prop.set_scope(sysprop::Scope::Public);
264 }
265 }
266}
267
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900268} // namespace
269
Inseob Kimb04d6192018-12-14 08:33:16 +0900270bool IsListProp(const sysprop::Property& prop) {
271 switch (prop.type()) {
272 case sysprop::BooleanList:
273 case sysprop::IntegerList:
274 case sysprop::LongList:
275 case sysprop::DoubleList:
276 case sysprop::StringList:
277 case sysprop::EnumList:
Inseob Kim22133ec2020-10-28 22:08:07 +0900278 case sysprop::UIntList:
279 case sysprop::ULongList:
Inseob Kimb04d6192018-12-14 08:33:16 +0900280 return true;
281 default:
282 return false;
283 }
284}
285
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900286std::string GetModuleName(const sysprop::Properties& props) {
287 const std::string& module = props.module();
288 return module.substr(module.rfind('.') + 1);
289}
290
Inseob Kim472fb632020-03-21 03:29:39 +0900291std::vector<std::string> ParseEnumValues(const std::string& enum_values) {
292 return android::base::Split(enum_values, "|");
293}
294
Inseob Kim053b83d2019-06-26 13:41:51 +0900295Result<sysprop::Properties> ParseProps(const std::string& input_file_path) {
296 sysprop::Properties ret;
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900297 std::string file_contents;
298
299 if (!android::base::ReadFileToString(input_file_path, &file_contents, true)) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900300 return ErrnoErrorf("Error reading file {}", input_file_path);
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900301 }
302
Inseob Kim053b83d2019-06-26 13:41:51 +0900303 if (!google::protobuf::TextFormat::ParseFromString(file_contents, &ret)) {
304 return Errorf("Error parsing file {}", input_file_path);
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900305 }
306
Inseob Kim1d316fc2020-04-09 22:29:28 +0900307 SetDefaultValues(&ret);
308
309 // validate after filling default values such as prop_name
Bernie Innocenti7ac8da42020-02-06 23:30:12 +0900310 if (auto res = ValidateProps(ret); !res.ok()) {
Inseob Kim053b83d2019-06-26 13:41:51 +0900311 return res.error();
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900312 }
313
Inseob Kim900fbad2019-06-26 14:09:20 +0900314 return ret;
315}
316
317Result<sysprop::SyspropLibraryApis> ParseApiFile(
318 const std::string& input_file_path) {
319 sysprop::SyspropLibraryApis ret;
320 std::string file_contents;
321
322 if (!android::base::ReadFileToString(input_file_path, &file_contents, true)) {
323 return ErrnoErrorf("Error reading file {}", input_file_path);
324 }
325
326 if (!google::protobuf::TextFormat::ParseFromString(file_contents, &ret)) {
327 return Errorf("Error parsing file {}", input_file_path);
328 }
329
330 std::unordered_set<std::string> modules;
331
332 for (int i = 0; i < ret.props_size(); ++i) {
333 sysprop::Properties* props = ret.mutable_props(i);
334
335 if (!modules.insert(props->module()).second) {
336 return Errorf("Error parsing file {}: duplicated module {}",
337 input_file_path, props->module());
Inseob Kim1ca03f32019-06-08 16:31:59 +0900338 }
Inseob Kim900fbad2019-06-26 14:09:20 +0900339
Inseob Kim1d316fc2020-04-09 22:29:28 +0900340 SetDefaultValues(props);
341
342 // validate after filling default values such as prop_name
Bernie Innocenti7ac8da42020-02-06 23:30:12 +0900343 if (auto res = ValidateProps(*props); !res.ok()) {
Inseob Kim900fbad2019-06-26 14:09:20 +0900344 return res.error();
345 }
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900346 }
347
Inseob Kim053b83d2019-06-26 13:41:51 +0900348 return ret;
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900349}
350
Inseob Kimf346e502019-01-04 10:32:39 +0900351std::string ToUpper(std::string str) {
352 for (char& ch : str) {
353 ch = toupper(ch);
354 }
355 return str;
356}
357
Inseob Kim14e51872018-10-25 14:27:33 +0900358std::string ApiNameToIdentifier(const std::string& name) {
359 static const std::regex kRegexAllowed{"-|\\."};
360 return (isdigit(name[0]) ? "_" : "") +
361 std::regex_replace(name, kRegexAllowed, "_");
Inseob Kim5f8f32c2018-08-24 11:10:44 +0900362}