diff --git a/init/init.cpp b/init/init.cpp index 0d5690b07..d3c9968bf 100644 --- a/init/init.cpp +++ b/init/init.cpp @@ -277,40 +277,29 @@ void HandleControlMessage(const std::string& msg, const std::string& name, pid_t const ControlMessageFunction& function = it->second; - if (function.target == ControlTarget::SERVICE) { - Service* svc = ServiceList::GetInstance().FindService(name); - if (svc == nullptr) { - LOG(ERROR) << "No such service '" << name << "' for ctl." << msg; - return; - } - if (auto result = function.action(svc); !result) { - LOG(ERROR) << "Could not ctl." << msg << " for service " << name << ": " - << result.error(); - } + Service* svc = nullptr; + switch (function.target) { + case ControlTarget::SERVICE: + svc = ServiceList::GetInstance().FindService(name); + break; + case ControlTarget::INTERFACE: + svc = ServiceList::GetInstance().FindInterface(name); + break; + default: + LOG(ERROR) << "Invalid function target from static map key '" << msg << "': " + << static_cast::type>(function.target); + return; + } + + if (svc == nullptr) { + LOG(ERROR) << "Could not find '" << name << "' for ctl." << msg; return; } - if (function.target == ControlTarget::INTERFACE) { - for (const auto& svc : ServiceList::GetInstance()) { - if (svc->interfaces().count(name) == 0) { - continue; - } - - if (auto result = function.action(svc.get()); !result) { - LOG(ERROR) << "Could not handle ctl." << msg << " for service " << svc->name() - << " with interface " << name << ": " << result.error(); - } - - return; - } - - LOG(ERROR) << "Could not find service hosting interface " << name; - return; + if (auto result = function.action(svc); !result) { + LOG(ERROR) << "Could not ctl." << msg << " for '" << name << "': " << result.error(); } - - LOG(ERROR) << "Invalid function target from static map key '" << msg - << "': " << static_cast::type>(function.target); } static Result wait_for_coldboot_done_action(const BuiltinArguments& args) { diff --git a/init/service.h b/init/service.h index cf38f69a1..9cb35b881 100644 --- a/init/service.h +++ b/init/service.h @@ -244,6 +244,16 @@ class ServiceList { return nullptr; } + Service* FindInterface(const std::string& interface_name) { + for (const auto& svc : services_) { + if (svc->interfaces().count(interface_name) > 0) { + return svc.get(); + } + } + + return nullptr; + } + void DumpState() const; auto begin() const { return services_.begin(); }