diff --git a/init/host_init_verifier.cpp b/init/host_init_verifier.cpp index 9323aa043..8aa350950 100644 --- a/init/host_init_verifier.cpp +++ b/init/host_init_verifier.cpp @@ -129,6 +129,23 @@ passwd* getpwnam(const char* login) { // NOLINT: implementing bad function. return nullptr; } +static std::optional> ReadKnownInterfaces( + const std::string& known_interfaces_file) { + if (known_interfaces_file.empty()) { + LOG(WARNING) << "Missing a known interfaces file."; + return {}; + } + + std::string known_interfaces; + if (!ReadFileToString(known_interfaces_file, &known_interfaces)) { + LOG(ERROR) << "Failed to read known interfaces file '" << known_interfaces_file << "'"; + return {}; + } + + auto interfaces = Split(known_interfaces, " "); + return std::set(interfaces.begin(), interfaces.end()); +} + namespace android { namespace init { @@ -139,11 +156,12 @@ static Result do_stub(const BuiltinArguments& args) { #include "generated_stub_builtin_function_map.h" void PrintUsage() { - std::cout << "usage: host_init_verifier [-p FILE] \n" + std::cout << "usage: host_init_verifier [-p FILE] -k FILE \n" "\n" "Tests an init script for correctness\n" "\n" "-p FILE\tSearch this passwd file for users and groups\n" + "-k FILE\tUse this file as a space-separated list of known interfaces\n" << std::endl; } @@ -151,13 +169,15 @@ int main(int argc, char** argv) { android::base::InitLogging(argv, &android::base::StdioLogger); android::base::SetMinimumLogSeverity(android::base::ERROR); + std::string known_interfaces_file; + while (true) { static const struct option long_options[] = { {"help", no_argument, nullptr, 'h'}, {nullptr, 0, nullptr, 0}, }; - int arg = getopt_long(argc, argv, "p:", long_options, nullptr); + int arg = getopt_long(argc, argv, "p:k:", long_options, nullptr); if (arg == -1) { break; @@ -170,6 +190,9 @@ int main(int argc, char** argv) { case 'p': passwd_files.emplace_back(optarg); break; + case 'k': + known_interfaces_file = optarg; + break; default: std::cerr << "getprop: getopt returned invalid result: " << arg << std::endl; return EXIT_FAILURE; @@ -189,7 +212,9 @@ int main(int argc, char** argv) { ActionManager& am = ActionManager::GetInstance(); ServiceList& sl = ServiceList::GetInstance(); Parser parser; - parser.AddSectionParser("service", std::make_unique(&sl, nullptr)); + parser.AddSectionParser( + "service", std::make_unique(&sl, nullptr, + ReadKnownInterfaces(known_interfaces_file))); parser.AddSectionParser("on", std::make_unique(&am, nullptr)); parser.AddSectionParser("import", std::make_unique()); diff --git a/init/init.cpp b/init/init.cpp index b6911e546..675f3e50a 100644 --- a/init/init.cpp +++ b/init/init.cpp @@ -113,7 +113,8 @@ void DumpState() { Parser CreateParser(ActionManager& action_manager, ServiceList& service_list) { Parser parser; - parser.AddSectionParser("service", std::make_unique(&service_list, subcontexts)); + parser.AddSectionParser( + "service", std::make_unique(&service_list, subcontexts, std::nullopt)); parser.AddSectionParser("on", std::make_unique(&action_manager, subcontexts)); parser.AddSectionParser("import", std::make_unique(&parser)); @@ -124,7 +125,8 @@ Parser CreateParser(ActionManager& action_manager, ServiceList& service_list) { Parser CreateServiceOnlyParser(ServiceList& service_list) { Parser parser; - parser.AddSectionParser("service", std::make_unique(&service_list, subcontexts)); + parser.AddSectionParser( + "service", std::make_unique(&service_list, subcontexts, std::nullopt)); return parser; } diff --git a/init/init_test.cpp b/init/init_test.cpp index 1bcc5eff2..a09db186e 100644 --- a/init/init_test.cpp +++ b/init/init_test.cpp @@ -44,7 +44,8 @@ void TestInit(const std::string& init_script_file, const TestFunctionMap& test_f Action::set_function_map(&test_function_map); Parser parser; - parser.AddSectionParser("service", std::make_unique(service_list, nullptr)); + parser.AddSectionParser("service", + std::make_unique(service_list, nullptr, std::nullopt)); parser.AddSectionParser("on", std::make_unique(&am, nullptr)); parser.AddSectionParser("import", std::make_unique(&parser)); diff --git a/init/service_parser.cpp b/init/service_parser.cpp index 33ed05097..ba351046a 100644 --- a/init/service_parser.cpp +++ b/init/service_parser.cpp @@ -152,6 +152,12 @@ Result ServiceParser::ParseInterface(std::vector&& args) { return Error() << "Interface name must not be a value name '" << interface_name << "'"; } + if (known_interfaces_ && known_interfaces_->count(interface_name) == 0) { + return Error() << "Interface is not in the known set of hidl_interfaces: '" + << interface_name << "'. Please ensure the interface is built " + << "by a hidl_interface target."; + } + const std::string fullname = interface_name + "/" + instance_name; for (const auto& svc : *service_list_) { diff --git a/init/service_parser.h b/init/service_parser.h index 0a5b29114..5a1676803 100644 --- a/init/service_parser.h +++ b/init/service_parser.h @@ -28,8 +28,12 @@ namespace init { class ServiceParser : public SectionParser { public: - ServiceParser(ServiceList* service_list, std::vector* subcontexts) - : service_list_(service_list), subcontexts_(subcontexts), service_(nullptr) {} + ServiceParser(ServiceList* service_list, std::vector* subcontexts, + const std::optional>& known_interfaces) + : service_list_(service_list), + subcontexts_(subcontexts), + known_interfaces_(known_interfaces), + service_(nullptr) {} Result ParseSection(std::vector&& args, const std::string& filename, int line) override; Result ParseLineSection(std::vector&& args, int line) override; @@ -81,6 +85,7 @@ class ServiceParser : public SectionParser { ServiceList* service_list_; std::vector* subcontexts_; + std::optional> known_interfaces_; std::unique_ptr service_; std::string filename_; };