1 /*
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
25 using android::FqInstance;
26 using android::FQName;
27 using android::base::Error;
28
29 namespace android {
30 namespace init {
31
32 namespace {
33
FQNamesToString(const std::set<FQName> & fqnames)34 std::string FQNamesToString(const std::set<FQName>& fqnames) {
35 std::set<std::string> fqname_strings;
36 for (const FQName& fqname : fqnames) {
37 fqname_strings.insert(fqname.string());
38 }
39 return android::base::Join(fqname_strings, " ");
40 }
41
42 } // namespace
43
CheckInterfaceInheritanceHierarchy(const std::set<std::string> & instances,const InterfaceInheritanceHierarchyMap & hierarchy)44 Result<void> CheckInterfaceInheritanceHierarchy(const std::set<std::string>& instances,
45 const InterfaceInheritanceHierarchyMap& hierarchy) {
46 std::set<FQName> interface_fqnames;
47 for (const std::string& instance : instances) {
48 // There is insufficient build-time information on AIDL interfaces to check them here
49 // TODO(b/139307527): Rework how services store interfaces to avoid excess string parsing
50 if (base::Split(instance, "/")[0] == "aidl") {
51 continue;
52 }
53
54 FqInstance fqinstance;
55 if (!fqinstance.setTo(instance)) {
56 return Error() << "Unable to parse interface instance '" << instance << "'";
57 }
58 interface_fqnames.insert(fqinstance.getFqName());
59 }
60 return CheckInterfaceInheritanceHierarchy(interface_fqnames, hierarchy);
61 }
62
CheckInterfaceInheritanceHierarchy(const std::set<FQName> & interfaces,const InterfaceInheritanceHierarchyMap & hierarchy)63 Result<void> CheckInterfaceInheritanceHierarchy(const std::set<FQName>& interfaces,
64 const InterfaceInheritanceHierarchyMap& hierarchy) {
65 std::ostringstream error_stream;
66 for (const FQName& intf : interfaces) {
67 if (hierarchy.count(intf) == 0) {
68 error_stream << "\nInterface is not in the known set of hidl_interfaces: '"
69 << intf.string()
70 << "'. Please ensure the interface is spelled correctly and built "
71 << "by a hidl_interface target.";
72 continue;
73 }
74 const std::set<FQName>& required_interfaces = hierarchy.at(intf);
75 std::set<FQName> diff;
76 std::set_difference(required_interfaces.begin(), required_interfaces.end(),
77 interfaces.begin(), interfaces.end(),
78 std::inserter(diff, diff.begin()));
79 if (!diff.empty()) {
80 error_stream << "\nInterface '" << intf.string() << "' requires its full inheritance "
81 << "hierarchy to be listed in this init_rc file. Missing "
82 << "interfaces: [" << FQNamesToString(diff) << "]";
83 }
84 }
85 const std::string& errors = error_stream.str();
86 if (!errors.empty()) {
87 return Error() << errors;
88 }
89
90 return {};
91 }
92
93 std::optional<std::set<FQName>> known_interfaces;
94
SetKnownInterfaces(const InterfaceInheritanceHierarchyMap & hierarchy)95 void SetKnownInterfaces(const InterfaceInheritanceHierarchyMap& hierarchy) {
96 known_interfaces = std::set<FQName>();
97 for (const auto& [intf, inherited_interfaces] : hierarchy) {
98 known_interfaces->insert(intf);
99 }
100 }
101
IsKnownInterface(const std::string & instance)102 Result<void> IsKnownInterface(const std::string& instance) {
103 FqInstance fqinstance;
104 if (!fqinstance.setTo(instance)) {
105 return Error() << "Unable to parse interface instance '" << instance << "'";
106 }
107 return IsKnownInterface(fqinstance.getFqName());
108 }
109
IsKnownInterface(const FQName & intf)110 Result<void> IsKnownInterface(const FQName& intf) {
111 if (!known_interfaces) {
112 return Error() << "No known interfaces have been loaded.";
113 }
114 if (known_interfaces->count(intf) == 0) {
115 return Error() << "Interface is not in the known set of hidl_interfaces: '" << intf.string()
116 << "'. Please ensure the interface is spelled correctly and built "
117 << "by a hidl_interface target.";
118 }
119 return {};
120 }
121
122 } // namespace init
123 } // namespace android
124