blob: 84407aa7030fe3f00ed0d1628506c2fed97b0b61 [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>
Daniel Normand2533c32019-08-02 15:13:50 -070024
25using android::FqInstance;
26using android::FQName;
27using android::base::Error;
28
29namespace android {
30namespace init {
31
32namespace {
33
34std::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
Daniel Normand2533c32019-08-02 15:13:50 -070042Result<void> CheckInterfaceInheritanceHierarchy(const std::set<FQName>& interfaces,
43 const InterfaceInheritanceHierarchyMap& hierarchy) {
44 std::ostringstream error_stream;
45 for (const FQName& intf : interfaces) {
46 if (hierarchy.count(intf) == 0) {
47 error_stream << "\nInterface is not in the known set of hidl_interfaces: '"
48 << intf.string()
49 << "'. Please ensure the interface is spelled correctly and built "
50 << "by a hidl_interface target.";
51 continue;
52 }
53 const std::set<FQName>& required_interfaces = hierarchy.at(intf);
54 std::set<FQName> diff;
55 std::set_difference(required_interfaces.begin(), required_interfaces.end(),
56 interfaces.begin(), interfaces.end(),
57 std::inserter(diff, diff.begin()));
58 if (!diff.empty()) {
59 error_stream << "\nInterface '" << intf.string() << "' requires its full inheritance "
60 << "hierarchy to be listed in this init_rc file. Missing "
61 << "interfaces: [" << FQNamesToString(diff) << "]";
62 }
63 }
64 const std::string& errors = error_stream.str();
65 if (!errors.empty()) {
66 return Error() << errors;
67 }
68
69 return {};
70}
71
Jooyung Han95c42422024-08-21 13:57:44 +090072} // namespace
73
74Result<void> CheckInterfaceInheritanceHierarchy(const std::set<std::string>& instances,
75 const InterfaceInheritanceHierarchyMap& hierarchy) {
76 std::set<FQName> interface_fqnames;
77 for (const std::string& instance : instances) {
78 // There is insufficient build-time information on AIDL interfaces to check them here
79 // TODO(b/139307527): Rework how services store interfaces to avoid excess string parsing
80 if (base::Split(instance, "/")[0] == "aidl") {
81 continue;
82 }
83
84 FqInstance fqinstance;
85 if (!fqinstance.setTo(instance)) {
86 return Error() << "Unable to parse interface instance '" << instance << "'";
87 }
88 interface_fqnames.insert(fqinstance.getFqName());
89 }
90 return CheckInterfaceInheritanceHierarchy(interface_fqnames, hierarchy);
91}
92
Daniel Normand2533c32019-08-02 15:13:50 -070093std::optional<std::set<FQName>> known_interfaces;
94
95void 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
102Result<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
110Result<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