|
|
@ -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,
|
|
|
@ -107,33 +106,68 @@ SM::~SM() = default;
|
|
|
|
void SM::Initialize(Kernel::HLERequestContext& ctx) {
|
|
|
|
void SM::Initialize(Kernel::HLERequestContext& ctx) {
|
|
|
|
LOG_DEBUG(Service_SM, "called");
|
|
|
|
LOG_DEBUG(Service_SM, "called");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
is_initialized = true;
|
|
|
|
|
|
|
|
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
IPC::ResponseBuilder rb{ctx, 2};
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
rb.Push(RESULT_SUCCESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Kernel::KScopedResourceReservation session_reservation(
|
|
|
|
|
|
|
|
// kernel.CurrentProcess()->GetResourceLimit(), Kernel::LimitableResource::Sessions);
|
|
|
|
|
|
|
|
// R_UNLESS(session_reservation.Succeeded(), Kernel::ResultLimitReached);
|
|
|
|
|
|
|
|
|
|
|
|
auto* session = Kernel::KSession::Create(kernel);
|
|
|
|
auto* session = Kernel::KSession::Create(kernel);
|
|
|
|
session->Initialize(&port->GetClientPort(), std::move(name));
|
|
|
|
session->Initialize(&port->GetClientPort(), std::move(name));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Commit the session reservation.
|
|
|
|
|
|
|
|
// session_reservation.Commit();
|
|
|
|
|
|
|
|
|
|
|
|
if (port->GetServerPort().GetHLEHandler()) {
|
|
|
|
if (port->GetServerPort().GetHLEHandler()) {
|
|
|
|
port->GetServerPort().GetHLEHandler()->ClientConnected(&session->GetServerSession());
|
|
|
|
port->GetServerPort().GetHLEHandler()->ClientConnected(&session->GetServerSession());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -141,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>();
|
|
|
@ -160,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);
|
|
|
@ -178,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
|
|
|
|