mitm-server: simplify logic, eliminate deadlock possibility

This commit is contained in:
Michael Scire 2019-01-26 06:22:18 -08:00
parent 9bf228de3e
commit 236190f8a3
2 changed files with 57 additions and 7 deletions

View File

@ -21,6 +21,8 @@
#include "sm_mitm.h" #include "sm_mitm.h"
#include "mitm_session.hpp" #include "mitm_session.hpp"
void RegisterMitmServerQueryHandle(Handle query_h, ServiceObjectHolder &&service);
template <typename T> template <typename T>
class MitmServer : public IWaitable { class MitmServer : public IWaitable {
static_assert(std::is_base_of<IMitmServiceObject, T>::value, "MitM Service Objects must derive from IMitmServiceObject"); static_assert(std::is_base_of<IMitmServiceObject, T>::value, "MitM Service Objects must derive from IMitmServiceObject");
@ -30,7 +32,8 @@ class MitmServer : public IWaitable {
char mitm_name[9]; char mitm_name[9];
public: public:
MitmServer(Handle *out_query_h, 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;
Result rc = smMitMInitialize(); Result rc = smMitMInitialize();
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
fatalSimple(rc); fatalSimple(rc);
@ -38,9 +41,10 @@ class MitmServer : public IWaitable {
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, out_query_h, mitm_name)))) { if (R_FAILED((rc = smMitMInstall(&this->port_handle, &query_h, mitm_name)))) {
fatalSimple(rc); fatalSimple(rc);
} }
RegisterMitmServerQueryHandle(query_h, std::move(ServiceObjectHolder(std::move(std::make_shared<MitmQueryService<T>>()))));
smMitMExit(); smMitMExit();
} }
@ -99,10 +103,6 @@ class MitmServer : public IWaitable {
template<typename T> template<typename T>
static void AddMitmServerToManager(SessionManagerBase *manager, const char *srv_name, unsigned int max_sessions) { static void AddMitmServerToManager(SessionManagerBase *manager, const char *srv_name, unsigned int max_sessions) {
Handle query_h; auto *srv = new MitmServer<T>(srv_name, max_sessions);
auto *srv = new MitmServer<T>(&query_h, srv_name, max_sessions);
manager->AddSession(query_h, std::move(ServiceObjectHolder(std::move(std::make_shared<MitmQueryService<T>>()))));
manager->AddWaitable(srv); manager->AddWaitable(srv);
} }

50
source/mitm_server.cpp Normal file
View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2018 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 <mutex>
#include <switch.h>
#include <stratosphere.hpp>
static HosMutex g_server_query_mutex;
static HosThread g_server_query_manager_thread;
static SessionManagerBase *g_server_query_manager = nullptr;
static void ServerQueryManagerThreadFunc(void *arg) {
g_server_query_manager->Process();
}
void RegisterMitmServerQueryHandle(Handle query_h, ServiceObjectHolder &&service) {
std::scoped_lock<HosMutex> lock(g_server_query_mutex);
const bool exists = g_server_query_manager != nullptr;
if (!exists) {
/* Create a new waitable manager if it doesn't exist already. */
g_server_query_manager = new WaitableManager(1);
}
/* Add session to the manager. */
g_server_query_manager->AddSession(query_h, std::move(service));
/* If this is our first time, launch thread. */
if (!exists) {
if (R_FAILED(g_server_query_manager_thread.Initialize(&ServerQueryManagerThreadFunc, nullptr, 0x4000, 27))) {
std::abort();
}
if (R_FAILED(g_server_query_manager_thread.Start())) {
std::abort();
}
}
}