hle: service: sm: Add TIPC support.

- Fixes our error checking of names as well.
This commit is contained in:
bunnei 2021-05-10 16:18:30 -07:00
parent 913971417e
commit 2c1e119c4a
2 changed files with 68 additions and 43 deletions

View file

@ -9,6 +9,7 @@
#include "core/hle/kernel/k_client_port.h" #include "core/hle/kernel/k_client_port.h"
#include "core/hle/kernel/k_client_session.h" #include "core/hle/kernel/k_client_session.h"
#include "core/hle/kernel/k_port.h" #include "core/hle/kernel/k_port.h"
#include "core/hle/kernel/k_scoped_resource_reservation.h"
#include "core/hle/kernel/k_server_port.h" #include "core/hle/kernel/k_server_port.h"
#include "core/hle/kernel/k_server_session.h" #include "core/hle/kernel/k_server_session.h"
#include "core/hle/kernel/k_session.h" #include "core/hle/kernel/k_session.h"
@ -18,6 +19,7 @@
namespace Service::SM { namespace Service::SM {
constexpr ResultCode ERR_NOT_INITIALIZED(ErrorModule::SM, 2);
constexpr ResultCode ERR_ALREADY_REGISTERED(ErrorModule::SM, 4); constexpr ResultCode ERR_ALREADY_REGISTERED(ErrorModule::SM, 4);
constexpr ResultCode ERR_INVALID_NAME(ErrorModule::SM, 6); constexpr ResultCode ERR_INVALID_NAME(ErrorModule::SM, 6);
constexpr ResultCode ERR_SERVICE_NOT_REGISTERED(ErrorModule::SM, 7); constexpr ResultCode ERR_SERVICE_NOT_REGISTERED(ErrorModule::SM, 7);
@ -34,20 +36,17 @@ static ResultCode ValidateServiceName(const std::string& name) {
LOG_ERROR(Service_SM, "Invalid service name! service={}", name); LOG_ERROR(Service_SM, "Invalid service name! service={}", name);
return ERR_INVALID_NAME; return ERR_INVALID_NAME;
} }
if (name.rfind('\0') != std::string::npos) {
LOG_ERROR(Service_SM, "A non null terminated service was passed");
return ERR_INVALID_NAME;
}
return RESULT_SUCCESS; return RESULT_SUCCESS;
} }
void ServiceManager::InstallInterfaces(std::shared_ptr<ServiceManager> self, Core::System& system) { Kernel::KClientPort& ServiceManager::InterfaceFactory(ServiceManager& self, Core::System& system) {
ASSERT(self->sm_interface.expired()); ASSERT(self.sm_interface.expired());
auto sm = std::make_shared<SM>(self, system); auto sm = std::make_shared<SM>(self, system);
sm->InstallAsNamedPort(system.Kernel()); self.sm_interface = sm;
self->sm_interface = sm; self.controller_interface = std::make_unique<Controller>(system);
self->controller_interface = std::make_unique<Controller>(system);
return sm->CreatePort(system.Kernel());
} }
ResultVal<Kernel::KServerPort*> ServiceManager::RegisterService(std::string name, ResultVal<Kernel::KServerPort*> ServiceManager::RegisterService(std::string name,
@ -114,21 +113,47 @@ void SM::Initialize(Kernel::HLERequestContext& ctx) {
} }
void SM::GetService(Kernel::HLERequestContext& ctx) { void SM::GetService(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx}; auto result = GetServiceImpl(ctx);
auto name_buf = rp.PopRaw<std::array<char, 8>>(); if (result.Succeeded()) {
auto end = std::find(name_buf.begin(), name_buf.end(), '\0'); IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles};
rb.Push(result.Code());
std::string name(name_buf.begin(), end); rb.PushMoveObjects(result.Unwrap());
} else {
auto result = service_manager->GetServicePort(name);
if (result.Failed()) {
IPC::ResponseBuilder rb{ctx, 2}; IPC::ResponseBuilder rb{ctx, 2};
rb.Push(result.Code()); rb.Push(result.Code());
}
}
void SM::GetServiceTipc(Kernel::HLERequestContext& ctx) {
auto result = GetServiceImpl(ctx);
IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles};
rb.Push(result.Code());
rb.PushMoveObjects(result.Succeeded() ? result.Unwrap() : nullptr);
}
static std::string PopServiceName(IPC::RequestParser& rp) {
auto name_buf = rp.PopRaw<std::array<char, 8>>();
std::string result;
for (const auto& c : name_buf) {
if (c >= ' ' && c <= '~') {
result.push_back(c);
}
}
return result;
}
ResultVal<Kernel::KClientSession*> SM::GetServiceImpl(Kernel::HLERequestContext& ctx) {
if (!is_initialized) {
return ERR_NOT_INITIALIZED;
}
IPC::RequestParser rp{ctx};
std::string name(PopServiceName(rp));
auto result = service_manager.GetServicePort(name);
if (result.Failed()) {
LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, result.Code().raw); LOG_ERROR(Service_SM, "called service={} -> error 0x{:08X}", name, result.Code().raw);
if (name.length() == 0) return result.Code();
return; // LibNX Fix
UNIMPLEMENTED();
return;
} }
auto* port = result.Unwrap(); auto* port = result.Unwrap();
@ -150,18 +175,12 @@ void SM::GetService(Kernel::HLERequestContext& ctx) {
} }
LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetId()); LOG_DEBUG(Service_SM, "called service={} -> session={}", name, session->GetId());
IPC::ResponseBuilder rb{ctx, 2, 0, 1, IPC::ResponseBuilder::Flags::AlwaysMoveHandles}; return MakeResult(&session->GetClientSession());
rb.Push(RESULT_SUCCESS);
rb.PushMoveObjects(session->GetClientSession());
} }
void SM::RegisterService(Kernel::HLERequestContext& ctx) { void SM::RegisterService(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx}; IPC::RequestParser rp{ctx};
std::string name(PopServiceName(rp));
const auto name_buf = rp.PopRaw<std::array<char, 8>>();
const auto end = std::find(name_buf.begin(), name_buf.end(), '\0');
const std::string name(name_buf.begin(), end);
const auto is_light = static_cast<bool>(rp.PopRaw<u32>()); const auto is_light = static_cast<bool>(rp.PopRaw<u32>());
const auto max_session_count = rp.PopRaw<u32>(); const auto max_session_count = rp.PopRaw<u32>();
@ -169,7 +188,7 @@ void SM::RegisterService(Kernel::HLERequestContext& ctx) {
LOG_DEBUG(Service_SM, "called with name={}, max_session_count={}, is_light={}", name, LOG_DEBUG(Service_SM, "called with name={}, max_session_count={}, is_light={}", name,
max_session_count, is_light); max_session_count, is_light);
auto handle = service_manager->RegisterService(name, max_session_count); auto handle = service_manager.RegisterService(name, max_session_count);
if (handle.Failed()) { if (handle.Failed()) {
LOG_ERROR(Service_SM, "failed to register service with error_code={:08X}", LOG_ERROR(Service_SM, "failed to register service with error_code={:08X}",
handle.Code().raw); handle.Code().raw);
@ -187,28 +206,31 @@ void SM::RegisterService(Kernel::HLERequestContext& ctx) {
void SM::UnregisterService(Kernel::HLERequestContext& ctx) { void SM::UnregisterService(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx}; IPC::RequestParser rp{ctx};
std::string name(PopServiceName(rp));
const auto name_buf = rp.PopRaw<std::array<char, 8>>();
const auto end = std::find(name_buf.begin(), name_buf.end(), '\0');
const std::string name(name_buf.begin(), end);
LOG_DEBUG(Service_SM, "called with name={}", name); LOG_DEBUG(Service_SM, "called with name={}", name);
IPC::ResponseBuilder rb{ctx, 2}; IPC::ResponseBuilder rb{ctx, 2};
rb.Push(service_manager->UnregisterService(name)); rb.Push(service_manager.UnregisterService(name));
} }
SM::SM(std::shared_ptr<ServiceManager> service_manager_, Core::System& system_) SM::SM(ServiceManager& service_manager_, Core::System& system_)
: ServiceFramework{system_, "sm:", 4}, : ServiceFramework{system_, "sm:", 4},
service_manager{std::move(service_manager_)}, kernel{system_.Kernel()} { service_manager{service_manager_}, kernel{system_.Kernel()} {
static const FunctionInfo functions[] = { RegisterHandlers({
{0, &SM::Initialize, "Initialize"}, {0, &SM::Initialize, "Initialize"},
{1, &SM::GetService, "GetService"}, {1, &SM::GetService, "GetService"},
{2, &SM::RegisterService, "RegisterService"}, {2, &SM::RegisterService, "RegisterService"},
{3, &SM::UnregisterService, "UnregisterService"}, {3, &SM::UnregisterService, "UnregisterService"},
{4, nullptr, "DetachClient"}, {4, nullptr, "DetachClient"},
}; });
RegisterHandlers(functions); RegisterHandlersTipc({
{0, &SM::Initialize, "Initialize"},
{1, &SM::GetServiceTipc, "GetService"},
{2, &SM::RegisterService, "RegisterService"},
{3, &SM::UnregisterService, "UnregisterService"},
{4, nullptr, "DetachClient"},
});
} }
} // namespace Service::SM } // namespace Service::SM

View file

@ -34,23 +34,26 @@ class Controller;
/// Interface to "sm:" service /// Interface to "sm:" service
class SM final : public ServiceFramework<SM> { class SM final : public ServiceFramework<SM> {
public: public:
explicit SM(std::shared_ptr<ServiceManager> service_manager_, Core::System& system_); explicit SM(ServiceManager& service_manager_, Core::System& system_);
~SM() override; ~SM() override;
private: private:
void Initialize(Kernel::HLERequestContext& ctx); void Initialize(Kernel::HLERequestContext& ctx);
void GetService(Kernel::HLERequestContext& ctx); void GetService(Kernel::HLERequestContext& ctx);
void GetServiceTipc(Kernel::HLERequestContext& ctx);
void RegisterService(Kernel::HLERequestContext& ctx); void RegisterService(Kernel::HLERequestContext& ctx);
void UnregisterService(Kernel::HLERequestContext& ctx); void UnregisterService(Kernel::HLERequestContext& ctx);
std::shared_ptr<ServiceManager> service_manager; ResultVal<Kernel::KClientSession*> GetServiceImpl(Kernel::HLERequestContext& ctx);
ServiceManager& service_manager;
bool is_initialized{}; bool is_initialized{};
Kernel::KernelCore& kernel; Kernel::KernelCore& kernel;
}; };
class ServiceManager { class ServiceManager {
public: public:
static void InstallInterfaces(std::shared_ptr<ServiceManager> self, Core::System& system); static Kernel::KClientPort& InterfaceFactory(ServiceManager& self, Core::System& system);
explicit ServiceManager(Kernel::KernelCore& kernel_); explicit ServiceManager(Kernel::KernelCore& kernel_);
~ServiceManager(); ~ServiceManager();