Skip to content

Commit

Permalink
rosalina & pm: properly shutdown when debugger, input redir (but not …
Browse files Browse the repository at this point in the history
…both) and force connection are enabled
  • Loading branch information
TuxSH committed Apr 28, 2020
1 parent 8c54613 commit 44cd392
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 74 deletions.
5 changes: 5 additions & 0 deletions sysmodules/pm/source/termination.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ ProcessData *terminateAllProcesses(u32 callerPid, s64 timeout)

ProcessList_Lock(&g_manager.processList);

// Send custom notification to at least Rosalina to make it relinquish some non-KIP services handles, stop the debugger, etc.
if (numKips >= 6) {
notifySubscribers(0x1001);
}

// Send notification 0x100 to the currently running application
if (g_manager.runningApplicationData != NULL) {
g_manager.runningApplicationData->flags &= ~PROCESSFLAG_DEPENDENCIES_LOADED;
Expand Down
1 change: 1 addition & 0 deletions sysmodules/rosalina/include/input_redirection.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ extern int inputRedirectionStartResult;

MyThread *inputRedirectionCreateThread(void);
void inputRedirectionThreadMain(void);
Result InputRedirection_Disable(s64 timeout);
Result InputRedirection_DoOrUndoPatches(void);
2 changes: 2 additions & 0 deletions sysmodules/rosalina/include/menus/debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
extern Menu debuggerMenu;

void debuggerFetchAndSetNextApplicationDebugHandleTask(void *argdata);
Result debuggerDisable(s64 timeout);

void DebuggerMenu_EnableDebugger(void);
void DebuggerMenu_DisableDebugger(void);
void DebuggerMenu_DebugNextApplicationByForce(void);
7 changes: 2 additions & 5 deletions sysmodules/rosalina/include/minisoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
#include <errno.h>

#define SYNC_ERROR ENODEV

extern Handle SOCU_handle;
extern Handle socMemhandle;

extern bool miniSocEnabled;

Result miniSocInit();
Result miniSocInit(void);
Result miniSocExitDirect(void);
Result miniSocExit(void);

s32 _net_convert_error(s32 sock_retval);
Expand Down
18 changes: 17 additions & 1 deletion sysmodules/rosalina/source/input_redirection.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,30 @@ void inputRedirectionThreadMain(void)
linger.l_linger = 0;

socSetsockopt(sock, SOL_SOCKET, SO_LINGER, &linger, sizeof(struct linger));

socClose(sock);

miniSocExit();
}

void hidCodePatchFunc(void);
void irCodePatchFunc(void);

Result InputRedirection_Disable(s64 timeout)
{
if(!inputRedirectionEnabled)
return 0;

Result res = InputRedirection_DoOrUndoPatches();
if(R_FAILED(res))
return res;

inputRedirectionEnabled = false;
res = MyThread_Join(&inputRedirectionThread, timeout);
svcCloseHandle(inputRedirectionThreadStartedEvent);

return res;
}

Result InputRedirection_DoOrUndoPatches(void)
{
s64 startAddress, textTotalRoundedSize, rodataTotalRoundedSize, dataTotalRoundedSize;
Expand Down
25 changes: 25 additions & 0 deletions sysmodules/rosalina/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#include "menus/debugger.h"
#include "menus/screen_filters.h"
#include "menus/cheats.h"
#include "menus/sysconfig.h"
#include "input_redirection.h"
#include "minisoc.h"

#include "task_runner.h"

Expand Down Expand Up @@ -161,6 +164,26 @@ static void handleTermNotification(u32 notificationId)
svcSignalEvent(terminationRequestEvent);
}

static void relinquishConnectionSessions(u32 notificationId)
{
(void)notificationId;
// Might be subject to a race condition, but heh.

// Disable input redirection
InputRedirection_Disable(100 * 1000 * 1000LL);

// Ask the debugger to terminate in approx 2 * 100ms
debuggerDisable(100 * 1000 * 1000LL);

// Kill the ac session if needed
if(isConnectionForced)
{
acExit();
isConnectionForced = false;
SysConfigMenu_UpdateStatus(true);
}
}

static void handleNextApplicationDebuggedByForce(u32 notificationId)
{
(void)notificationId;
Expand All @@ -175,7 +198,9 @@ static const ServiceManagerServiceEntry services[] = {

static const ServiceManagerNotificationEntry notifications[] = {
{ 0x100 , handleTermNotification },
//{ 0x103 , relinquishConnectionSessions }, // Sleep mode entry <=== causes issues
{ 0x1000, handleNextApplicationDebuggedByForce },
{ 0x1001, relinquishConnectionSessions },
{ 0x000, NULL },
};

Expand Down
45 changes: 26 additions & 19 deletions sysmodules/rosalina/source/menus/debugger.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,30 @@ void debuggerFetchAndSetNextApplicationDebugHandleTask(void *argdata)
GDB_UnlockAllContexts(&gdbServer);
}

Result debuggerDisable(s64 timeout)
{
Result res = 0;
bool initialized = gdbServer.referenceCount != 0;
if(initialized)
{
svcSignalEvent(gdbServer.super.shall_terminate_event);
server_kill_connections(&gdbServer.super);

res = MyThread_Join(&debuggerDebugThread, timeout);
if(res == 0)
res = MyThread_Join(&debuggerSocketThread, timeout);

Handle dummy = 0;
PMDBG_RunQueuedProcess(&dummy);
svcCloseHandle(dummy);
PMDBG_DebugNextApplicationByForce(false);
nextApplicationGdbCtx = NULL;
svcKernelSetState(0x10000, 2);
}

return res;
}

void DebuggerMenu_EnableDebugger(void)
{
bool done = false, alreadyEnabled = gdbServer.super.running;
Expand Down Expand Up @@ -144,26 +168,9 @@ void DebuggerMenu_EnableDebugger(void)
void DebuggerMenu_DisableDebugger(void)
{
bool initialized = gdbServer.referenceCount != 0;
Result res = 0;
char buf[65];

if(initialized)
{
svcSignalEvent(gdbServer.super.shall_terminate_event);
server_kill_connections(&gdbServer.super);
//server_set_should_close_all(&gdbServer.super);

res = MyThread_Join(&debuggerDebugThread, 2 * 1000 * 1000 * 1000LL);
if(res == 0)
res = MyThread_Join(&debuggerSocketThread, 2 * 1000 * 1000 * 1000LL);

Handle dummy = 0;
PMDBG_RunQueuedProcess(&dummy);
svcCloseHandle(dummy);
PMDBG_DebugNextApplicationByForce(false);
nextApplicationGdbCtx = NULL;
svcKernelSetState(0x10000, 2);
}
Result res = initialized ? debuggerDisable(2 * 1000 * 1000 * 1000LL) : 0;
char buf[65];

if(res != 0)
sprintf(buf, "Failed to disable debugger (0x%08lx).", (u32)res);
Expand Down
9 changes: 2 additions & 7 deletions sysmodules/rosalina/source/menus/miscellaneous.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ void MiscellaneousMenu_SaveSettings(void)

void MiscellaneousMenu_InputRedirection(void)
{
static MyThread *inputRedirectionThread = NULL;
bool done = false;

Result res;
Expand All @@ -231,11 +230,7 @@ void MiscellaneousMenu_InputRedirection(void)

if(wasEnabled)
{
res = InputRedirection_DoOrUndoPatches();
inputRedirectionEnabled = false;
res = MyThread_Join(inputRedirectionThread, 5 * 1000 * 1000 * 1000LL);
svcCloseHandle(inputRedirectionThreadStartedEvent);

res = InputRedirection_Disable(5 * 1000 * 1000 * 1000LL);
if(res != 0)
sprintf(buf, "Failed to stop InputRedirection (0x%08lx).", (u32)res);
else
Expand Down Expand Up @@ -282,7 +277,7 @@ void MiscellaneousMenu_InputRedirection(void)
res = svcCreateEvent(&inputRedirectionThreadStartedEvent, RESET_STICKY);
if(R_SUCCEEDED(res))
{
inputRedirectionThread = inputRedirectionCreateThread();
inputRedirectionCreateThread();
res = svcWaitSynchronization(inputRedirectionThreadStartedEvent, 10 * 1000 * 1000 * 1000LL);
if(res == 0)
res = (Result)inputRedirectionStartResult;
Expand Down
Loading

0 comments on commit 44cd392

Please sign in to comment.