blob: ddbacd763cd61244dd95332ad36d7cea99091294 [file] [log] [blame]
Daniel Normand2533c32019-08-02 15:13:50 -07001/*
2 * Copyright (C) 2019 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 "interface_utils.h"
18
19#include <fstream>
20#include <sstream>
21
22#include <android-base/strings.h>
23#include <hidl-util/FqInstance.h>
24#include <json/json.h>
25
26using android::FqInstance;
27using android::FQName;
28using android::base::Error;
29
30namespace android {
31namespace init {
32
33namespace {
34
35std::string FQNamesToString(const std::set<FQName>& fqnames) {
36 std::set<std::string> fqname_strings;
37 for (const FQName& fqname : fqnames) {
38 fqname_strings.insert(fqname.string());
39 }
40 return android::base::Join(fqname_strings, " ");
41}
42
43} // namespace
44
45Result<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
76Result<void> CheckInterfaceInheritanceHierarchy(const std::set<std::string>& instances,
77 const InterfaceInheritanceHierarchyMap& hierarchy) {
78 std::set<FQName> interface_fqnames;
79 for (const std::string& instance : instances) {
Jon Spivack16fb3f92019-07-26 13:14:42 -070080 // There is insufficient build-time information on AIDL interfaces to check them here
81 // TODO(b/139307527): Rework how services store interfaces to avoid excess string parsing
82 if (base::Split(instance, "/")[0] == "aidl") {
83 continue;
84 }
85
Daniel Normand2533c32019-08-02 15:13:50 -070086 FqInstance fqinstance;
87 if (!fqinstance.setTo(instance)) {
88 return Error() << "Unable to parse interface instance '" << instance << "'";
89 }
90 interface_fqnames.insert(fqinstance.getFqName());
91 }
92 return CheckInterfaceInheritanceHierarchy(interface_fqnames, hierarchy);
93}
94
95Result<void> CheckInterfaceInheritanceHierarchy(const std::set<FQName>& interfaces,
96 const InterfaceInheritanceHierarchyMap& hierarchy) {
97 std::ostringstream error_stream;
98 for (const FQName& intf : interfaces) {
99 if (hierarchy.count(intf) == 0) {
100 error_stream << "\nInterface is not in the known set of hidl_interfaces: '"
101 << intf.string()
102 << "'. Please ensure the interface is spelled correctly and built "
103 << "by a hidl_interface target.";
104 continue;
105 }
106 const std::set<FQName>& required_interfaces = hierarchy.at(intf);
107 std::set<FQName> diff;
108 std::set_difference(required_interfaces.begin(), required_interfaces.end(),
109 interfaces.begin(), interfaces.end(),
110 std::inserter(diff, diff.begin()));
111 if (!diff.empty()) {
112 error_stream << "\nInterface '" << intf.string() << "' requires its full inheritance "
113 << "hierarchy to be listed in this init_rc file. Missing "
114 << "interfaces: [" << FQNamesToString(diff) << "]";
115 }
116 }
117 const std::string& errors = error_stream.str();
118 if (!errors.empty()) {
119 return Error() << errors;
120 }
121
122 return {};
123}
124
125std::optional<std::set<FQName>> known_interfaces;
126
127void SetKnownInterfaces(const InterfaceInheritanceHierarchyMap& hierarchy) {
128 known_interfaces = std::set<FQName>();
129 for (const auto& [intf, inherited_interfaces] : hierarchy) {
130 known_interfaces->insert(intf);
131 }
132}
133
134Result<void> IsKnownInterface(const std::string& instance) {
135 FqInstance fqinstance;
136 if (!fqinstance.setTo(instance)) {
137 return Error() << "Unable to parse interface instance '" << instance << "'";
138 }
139 return IsKnownInterface(fqinstance.getFqName());
140}
141
142Result<void> IsKnownInterface(const FQName& intf) {
143 if (!known_interfaces) {
144 return Error() << "No known interfaces have been loaded.";
145 }
146 if (known_interfaces->count(intf) == 0) {
147 return Error() << "Interface is not in the known set of hidl_interfaces: '" << intf.string()
148 << "'. Please ensure the interface is spelled correctly and built "
149 << "by a hidl_interface target.";
150 }
151 return {};
152}
153
154} // namespace init
155} // namespace android