Skip to content

Commit 422a758

Browse files
committed
host_init_verifier: use libhidlmetadata
Since it's available in C++, it's easier to build host_init_verifier with this than create a new way to export the json file. Bug: 141567104 Test: manually change hidl interface in a manifest host_init_verifier: system/core/rootdir/init.rc: 69: host_init_verifier: Interface is not in the known set of hidl_interfaces: '[email protected]::IFoo'. Please ensure the interface is spelled correctly and built by a hidl_interface target. Change-Id: Ic73dcb51855cb751734bc497d8e69f379966c511
1 parent f5bad50 commit 422a758

File tree

4 files changed

+27
-50
lines changed

4 files changed

+27
-50
lines changed

init/Android.bp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ cc_defaults {
8282
"libfscrypt",
8383
"libgsi",
8484
"libhidl-gen-utils",
85-
"libjsoncpp",
8685
"libkeyutils",
8786
"liblog",
8887
"liblogwrap",
@@ -286,13 +285,11 @@ cc_binary {
286285
shared_libs: [
287286
"libcutils",
288287
"libhidl-gen-utils",
288+
"libhidlmetadata",
289289
"liblog",
290290
"libprocessgroup",
291291
"libprotobuf-cpp-lite",
292292
],
293-
header_libs: [
294-
"libjsoncpp_headers",
295-
],
296293
srcs: [
297294
"action.cpp",
298295
"action_manager.cpp",

init/host_init_verifier.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <android-base/logging.h>
3131
#include <android-base/parseint.h>
3232
#include <android-base/strings.h>
33+
#include <hidl/metadata.h>
3334

3435
#include "action.h"
3536
#include "action_manager.h"
@@ -142,28 +143,46 @@ static Result<void> check_stub(const BuiltinArguments& args) {
142143
#include "generated_stub_builtin_function_map.h"
143144

144145
void PrintUsage() {
145-
std::cout << "usage: host_init_verifier [-p FILE] -i FILE <init rc file>\n"
146+
std::cout << "usage: host_init_verifier [-p FILE] <init rc file>\n"
146147
"\n"
147148
"Tests an init script for correctness\n"
148149
"\n"
149150
"-p FILE\tSearch this passwd file for users and groups\n"
150-
"-i FILE\tParse this JSON file for the HIDL interface inheritance hierarchy\n"
151151
<< std::endl;
152152
}
153153

154+
Result<InterfaceInheritanceHierarchyMap> ReadInterfaceInheritanceHierarchy() {
155+
InterfaceInheritanceHierarchyMap result;
156+
for (const HidlInterfaceMetadata& iface : HidlInterfaceMetadata::all()) {
157+
std::set<FQName> inherited_interfaces;
158+
for (const std::string& intf : iface.inherited) {
159+
FQName fqname;
160+
if (!fqname.setTo(intf)) {
161+
return Error() << "Unable to parse interface '" << intf << "'";
162+
}
163+
inherited_interfaces.insert(fqname);
164+
}
165+
FQName fqname;
166+
if (!fqname.setTo(iface.name)) {
167+
return Error() << "Unable to parse interface '" << iface.name << "'";
168+
}
169+
result[fqname] = inherited_interfaces;
170+
}
171+
172+
return result;
173+
}
174+
154175
int main(int argc, char** argv) {
155176
android::base::InitLogging(argv, &android::base::StdioLogger);
156177
android::base::SetMinimumLogSeverity(android::base::ERROR);
157178

158-
std::string interface_inheritance_hierarchy_file;
159-
160179
while (true) {
161180
static const struct option long_options[] = {
162181
{"help", no_argument, nullptr, 'h'},
163182
{nullptr, 0, nullptr, 0},
164183
};
165184

166-
int arg = getopt_long(argc, argv, "p:i:", long_options, nullptr);
185+
int arg = getopt_long(argc, argv, "p:", long_options, nullptr);
167186

168187
if (arg == -1) {
169188
break;
@@ -176,9 +195,6 @@ int main(int argc, char** argv) {
176195
case 'p':
177196
passwd_files.emplace_back(optarg);
178197
break;
179-
case 'i':
180-
interface_inheritance_hierarchy_file = optarg;
181-
break;
182198
default:
183199
std::cerr << "getprop: getopt returned invalid result: " << arg << std::endl;
184200
return EXIT_FAILURE;
@@ -188,13 +204,12 @@ int main(int argc, char** argv) {
188204
argc -= optind;
189205
argv += optind;
190206

191-
if (argc != 1 || interface_inheritance_hierarchy_file.empty()) {
207+
if (argc != 1) {
192208
PrintUsage();
193209
return EXIT_FAILURE;
194210
}
195211

196-
auto interface_inheritance_hierarchy_map =
197-
ReadInterfaceInheritanceHierarchy(interface_inheritance_hierarchy_file);
212+
auto interface_inheritance_hierarchy_map = ReadInterfaceInheritanceHierarchy();
198213
if (!interface_inheritance_hierarchy_map) {
199214
LOG(ERROR) << interface_inheritance_hierarchy_map.error();
200215
return EXIT_FAILURE;

init/interface_utils.cpp

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include <android-base/strings.h>
2323
#include <hidl-util/FqInstance.h>
24-
#include <json/json.h>
2524

2625
using android::FqInstance;
2726
using android::FQName;
@@ -42,37 +41,6 @@ std::string FQNamesToString(const std::set<FQName>& fqnames) {
4241

4342
} // namespace
4443

45-
Result<InterfaceInheritanceHierarchyMap> ReadInterfaceInheritanceHierarchy(
46-
const std::string& path) {
47-
Json::Value root;
48-
Json::Reader reader;
49-
std::ifstream stream(path);
50-
if (!reader.parse(stream, root)) {
51-
return Error() << "Failed to read interface inheritance hierarchy file: " << path << "\n"
52-
<< reader.getFormattedErrorMessages();
53-
}
54-
55-
InterfaceInheritanceHierarchyMap result;
56-
for (const Json::Value& entry : root) {
57-
std::set<FQName> inherited_interfaces;
58-
for (const Json::Value& intf : entry["inheritedInterfaces"]) {
59-
FQName fqname;
60-
if (!fqname.setTo(intf.asString())) {
61-
return Error() << "Unable to parse interface '" << intf.asString() << "'";
62-
}
63-
inherited_interfaces.insert(fqname);
64-
}
65-
std::string intf_string = entry["interface"].asString();
66-
FQName fqname;
67-
if (!fqname.setTo(intf_string)) {
68-
return Error() << "Unable to parse interface '" << intf_string << "'";
69-
}
70-
result[fqname] = inherited_interfaces;
71-
}
72-
73-
return result;
74-
}
75-
7644
Result<void> CheckInterfaceInheritanceHierarchy(const std::set<std::string>& instances,
7745
const InterfaceInheritanceHierarchyMap& hierarchy) {
7846
std::set<FQName> interface_fqnames;

init/interface_utils.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ namespace init {
2929

3030
using InterfaceInheritanceHierarchyMap = std::map<android::FQName, std::set<android::FQName>>;
3131

32-
// Reads the HIDL interface inheritance hierarchy JSON file at the given path.
33-
Result<InterfaceInheritanceHierarchyMap> ReadInterfaceInheritanceHierarchy(const std::string& path);
34-
3532
// For the given set of interfaces / interface instances, checks that each
3633
// interface's hierarchy of inherited interfaces is also included in the given
3734
// interface set. Uses the provided hierarchy data.

0 commit comments

Comments
 (0)