libstrat: only hold sm sessions open when needed

This commit is contained in:
Michael Scire 2019-04-22 12:40:35 -07:00
parent 4d6bf21f9c
commit 79bc9bf8d8
4 changed files with 119 additions and 55 deletions

View File

@ -20,6 +20,7 @@
#include "mitm_query_service.hpp" #include "mitm_query_service.hpp"
#include "sm_mitm.h" #include "sm_mitm.h"
#include "mitm_session.hpp" #include "mitm_session.hpp"
#include "../utilities.hpp"
void RegisterMitmServerQueryHandle(Handle query_h, ServiceObjectHolder &&service); void RegisterMitmServerQueryHandle(Handle query_h, ServiceObjectHolder &&service);
@ -34,26 +35,25 @@ class MitmServer : public IWaitable {
public: public:
MitmServer(const char *service_name, unsigned int max_s) : port_handle(0), max_sessions(max_s) { MitmServer(const char *service_name, unsigned int max_s) : port_handle(0), max_sessions(max_s) {
Handle query_h = 0; Handle query_h = 0;
Result rc = smMitMInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
strncpy(mitm_name, service_name, 8); DoWithSmMitmSession([&]() {
mitm_name[8] = '\x00'; strncpy(mitm_name, service_name, 8);
if (R_FAILED((rc = smMitMInstall(&this->port_handle, &query_h, mitm_name)))) { mitm_name[8] = '\x00';
fatalSimple(rc); if (R_FAILED(smMitMInstall(&this->port_handle, &query_h, mitm_name))) {
} std::abort();
}
});
RegisterMitmServerQueryHandle(query_h, std::move(ServiceObjectHolder(std::move(std::make_shared<MitmQueryService<T>>())))); RegisterMitmServerQueryHandle(query_h, std::move(ServiceObjectHolder(std::move(std::make_shared<MitmQueryService<T>>()))));
smMitMExit();
} }
virtual ~MitmServer() override { virtual ~MitmServer() override {
if (this->port_handle) { if (this->port_handle) {
if (R_FAILED(smMitMUninstall(this->mitm_name))) { DoWithSmMitmSession([&]() {
std::abort(); if (R_FAILED(smMitMUninstall(this->mitm_name))) {
} std::abort();
}
});
svcCloseHandle(port_handle); svcCloseHandle(port_handle);
} }
} }
@ -82,18 +82,13 @@ class MitmServer : public IWaitable {
delete s; delete s;
}); });
rc = smMitMInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
u64 client_pid; u64 client_pid;
if (R_FAILED(smMitMAcknowledgeSession(forward_service.get(), &client_pid, mitm_name))) { DoWithSmMitmSession([&]() {
/* TODO: Panic. */ if (R_FAILED(smMitMAcknowledgeSession(forward_service.get(), &client_pid, mitm_name))) {
} std::abort();
}
smMitMExit(); });
this->GetSessionManager()->AddWaitable(new MitmSession(session_h, client_pid, forward_service, MakeShared(forward_service, client_pid))); this->GetSessionManager()->AddWaitable(new MitmSession(session_h, client_pid, forward_service, MakeShared(forward_service, client_pid)));
return ResultSuccess; return ResultSuccess;

View File

@ -19,6 +19,7 @@
#include "iwaitable.hpp" #include "iwaitable.hpp"
#include "ipc.hpp" #include "ipc.hpp"
#include "utilities.hpp"
template<typename T, auto MakeShared> template<typename T, auto MakeShared>
class IServer : public IWaitable { class IServer : public IWaitable {
@ -62,9 +63,11 @@ template <typename T, auto MakeShared = std::make_shared<T>>
class ServiceServer : public IServer<T, MakeShared> { class ServiceServer : public IServer<T, MakeShared> {
public: public:
ServiceServer(const char *service_name, unsigned int max_s) : IServer<T, MakeShared>(max_s) { ServiceServer(const char *service_name, unsigned int max_s) : IServer<T, MakeShared>(max_s) {
if (R_FAILED(smRegisterService(&this->port_handle, service_name, false, this->max_sessions))) { DoWithSmSession([&]() {
/* TODO: Panic. */ if (R_FAILED(smRegisterService(&this->port_handle, service_name, false, this->max_sessions))) {
} std::abort();
}
});
} }
}; };

View File

@ -18,6 +18,9 @@
#include <switch.h> #include <switch.h>
#include <cstdlib> #include <cstdlib>
#include "hossynch.hpp"
#include "mitm/sm_mitm.h"
static inline void RebootToRcm() { static inline void RebootToRcm() {
SecmonArgs args = {0}; SecmonArgs args = {0};
args.X[0] = 0xC3000401; /* smcSetConfig */ args.X[0] = 0xC3000401; /* smcSetConfig */
@ -97,3 +100,36 @@ static inline bool IsRcmBugPatched() {
} }
return rcm_bug_patched; return rcm_bug_patched;
} }
HosRecursiveMutex &GetSmSessionMutex();
HosRecursiveMutex &GetSmMitmSessionMutex();
template<typename F>
static void DoWithSmSession(F f) {
std::scoped_lock<HosRecursiveMutex &> lk(GetSmSessionMutex());
{
Result rc;
if (R_SUCCEEDED((rc = smInitialize()))) {
f();
} else {
/* TODO: fatalSimple(rc); ? */
std::abort();
}
smExit();
}
}
template<typename F>
static void DoWithSmMitmSession(F f) {
std::scoped_lock<HosRecursiveMutex &> lk(GetSmMitmSessionMutex());
{
Result rc;
if (R_SUCCEEDED((rc = smMitMInitialize()))) {
f();
} else {
/* TODO: fatalSimple(rc); ? */
std::abort();
}
smMitMExit();
}
}

30
source/utilities.cpp Normal file
View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2018-2019 Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <switch.h>
#include <stratosphere.hpp>
static HosRecursiveMutex g_sm_session_lock;
static HosRecursiveMutex g_sm_mitm_session_lock;
HosRecursiveMutex &GetSmSessionMutex() {
return g_sm_session_lock;
}
HosRecursiveMutex &GetSmMitmSessionMutex() {
return g_sm_mitm_session_lock;
}