mirror of
https://github.com/Atmosphere-NX/Atmosphere-libs.git
synced 2025-06-21 19:12:42 +02:00
libstrat: only hold sm sessions open when needed
This commit is contained in:
parent
4d6bf21f9c
commit
79bc9bf8d8
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
DoWithSmMitmSession([&]() {
|
||||||
strncpy(mitm_name, service_name, 8);
|
strncpy(mitm_name, service_name, 8);
|
||||||
mitm_name[8] = '\x00';
|
mitm_name[8] = '\x00';
|
||||||
if (R_FAILED((rc = smMitMInstall(&this->port_handle, &query_h, mitm_name)))) {
|
if (R_FAILED(smMitMInstall(&this->port_handle, &query_h, mitm_name))) {
|
||||||
fatalSimple(rc);
|
std::abort();
|
||||||
}
|
}
|
||||||
RegisterMitmServerQueryHandle(query_h, std::move(ServiceObjectHolder(std::move(std::make_shared<MitmQueryService<T>>()))));
|
});
|
||||||
|
|
||||||
smMitMExit();
|
RegisterMitmServerQueryHandle(query_h, std::move(ServiceObjectHolder(std::move(std::make_shared<MitmQueryService<T>>()))));
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~MitmServer() override {
|
virtual ~MitmServer() override {
|
||||||
if (this->port_handle) {
|
if (this->port_handle) {
|
||||||
|
DoWithSmMitmSession([&]() {
|
||||||
if (R_FAILED(smMitMUninstall(this->mitm_name))) {
|
if (R_FAILED(smMitMUninstall(this->mitm_name))) {
|
||||||
std::abort();
|
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;
|
||||||
|
|
||||||
|
DoWithSmMitmSession([&]() {
|
||||||
if (R_FAILED(smMitMAcknowledgeSession(forward_service.get(), &client_pid, mitm_name))) {
|
if (R_FAILED(smMitMAcknowledgeSession(forward_service.get(), &client_pid, mitm_name))) {
|
||||||
/* TODO: Panic. */
|
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;
|
||||||
|
@ -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) {
|
||||||
|
DoWithSmSession([&]() {
|
||||||
if (R_FAILED(smRegisterService(&this->port_handle, service_name, false, this->max_sessions))) {
|
if (R_FAILED(smRegisterService(&this->port_handle, service_name, false, this->max_sessions))) {
|
||||||
/* TODO: Panic. */
|
std::abort();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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
30
source/utilities.cpp
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user