2018-05-04 05:58:25 +00:00
|
|
|
#include <switch.h>
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
#include <atomic>
|
|
|
|
|
|
|
|
#include "pm_registration.hpp"
|
2018-05-07 09:50:33 +00:00
|
|
|
#include "pm_resource_limits.hpp"
|
2018-05-04 05:58:25 +00:00
|
|
|
#include "pm_process_wait.hpp"
|
|
|
|
|
|
|
|
static ProcessList g_process_list;
|
2018-05-04 23:55:39 +00:00
|
|
|
static ProcessList g_dead_process_list;
|
2018-05-04 05:58:25 +00:00
|
|
|
|
2018-05-07 04:59:54 +00:00
|
|
|
static SystemEvent *g_process_launch_start_event = NULL;
|
2018-05-04 05:58:25 +00:00
|
|
|
static HosSemaphore g_sema_finish_launch;
|
|
|
|
|
|
|
|
static HosMutex g_process_launch_mutex;
|
|
|
|
static Registration::ProcessLaunchState g_process_launch_state;
|
|
|
|
|
|
|
|
static std::atomic_bool g_debug_next_application(false);
|
|
|
|
static std::atomic<u64> g_debug_on_launch_tid(0);
|
|
|
|
|
|
|
|
static SystemEvent *g_process_event = NULL;
|
|
|
|
static SystemEvent *g_debug_title_event = NULL;
|
|
|
|
static SystemEvent *g_debug_application_event = NULL;
|
|
|
|
|
2018-05-05 01:56:59 +00:00
|
|
|
Registration::AutoProcessListLock::AutoProcessListLock() {
|
|
|
|
g_process_list.Lock();
|
|
|
|
this->has_lock = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Registration::AutoProcessListLock::~AutoProcessListLock() {
|
|
|
|
if (this->has_lock) {
|
|
|
|
this->Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Registration::AutoProcessListLock::Unlock() {
|
|
|
|
if (this->has_lock) {
|
|
|
|
g_process_list.Unlock();
|
|
|
|
}
|
|
|
|
this->has_lock = false;
|
|
|
|
}
|
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
void Registration::InitializeSystemResources() {
|
|
|
|
g_process_event = new SystemEvent(&IEvent::PanicCallback);
|
|
|
|
g_debug_title_event = new SystemEvent(&IEvent::PanicCallback);
|
|
|
|
g_debug_application_event = new SystemEvent(&IEvent::PanicCallback);
|
2018-05-07 04:59:54 +00:00
|
|
|
g_process_launch_start_event = new SystemEvent(&Registration::ProcessLaunchStartCallback);
|
2018-05-04 05:58:25 +00:00
|
|
|
|
2018-05-07 09:50:33 +00:00
|
|
|
ResourceLimitUtils::InitializeLimits();
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 04:59:54 +00:00
|
|
|
Result Registration::ProcessLaunchStartCallback(Handle *handles, size_t num_handles, u64 timeout) {
|
|
|
|
svcClearEvent(handles[0]);
|
|
|
|
Registration::HandleProcessLaunch();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
IWaitable *Registration::GetProcessLaunchStartEvent() {
|
|
|
|
return g_process_launch_start_event;
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IWaitable *Registration::GetProcessList() {
|
|
|
|
return &g_process_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Registration::HandleProcessLaunch() {
|
|
|
|
LoaderProgramInfo program_info = {0};
|
|
|
|
Result rc;
|
|
|
|
u64 launch_flags = g_process_launch_state.launch_flags;
|
|
|
|
u64 *out_pid = g_process_launch_state.out_pid;
|
|
|
|
Process new_process = {0};
|
|
|
|
new_process.tid_sid = g_process_launch_state.tid_sid;
|
2018-05-06 08:23:47 +00:00
|
|
|
u8 *ac_buf = new u8[4 * sizeof(LoaderProgramInfo)];
|
|
|
|
std::fill(ac_buf, ac_buf + 4 * sizeof(LoaderProgramInfo), 0xCC);
|
|
|
|
u8 *acid_sac = ac_buf, *aci0_sac = acid_sac + sizeof(LoaderProgramInfo), *fac = aci0_sac + sizeof(LoaderProgramInfo), *fah = fac + sizeof(LoaderProgramInfo);
|
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Check that this is a real program. */
|
|
|
|
if (R_FAILED((rc = ldrPmGetProgramInfo(new_process.tid_sid.title_id, new_process.tid_sid.storage_id, &program_info)))) {
|
|
|
|
goto HANDLE_PROCESS_LAUNCH_END;
|
|
|
|
}
|
2018-05-06 08:23:47 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Get the resource limit handle, ensure that we can launch the program. */
|
2018-05-07 09:50:33 +00:00
|
|
|
if ((program_info.application_type & 3) == 1 && HasApplicationProcess(NULL)) {
|
|
|
|
rc = 0xA0F;
|
|
|
|
goto HANDLE_PROCESS_LAUNCH_END;
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to register the title for launch in loader... */
|
|
|
|
if (R_FAILED((rc = ldrPmRegisterTitle(new_process.tid_sid.title_id, new_process.tid_sid.storage_id, &new_process.ldr_queue_index)))) {
|
|
|
|
goto HANDLE_PROCESS_LAUNCH_END;
|
|
|
|
}
|
2018-05-06 08:23:47 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Make sure the previous application is cleaned up. */
|
|
|
|
if ((program_info.application_type & 3) == 1) {
|
2018-05-07 09:50:33 +00:00
|
|
|
ResourceLimitUtils::EnsureApplicationResourcesAvailable();
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to create the process... */
|
2018-05-07 09:50:33 +00:00
|
|
|
if (R_FAILED((rc = ldrPmCreateProcess(LAUNCHFLAGS_ARGLOW(launch_flags) | LAUNCHFLAGS_ARGHIGH(launch_flags), new_process.ldr_queue_index, ResourceLimitUtils::GetResourceLimitHandle(program_info.application_type), &new_process.handle)))) {
|
2018-05-04 05:58:25 +00:00
|
|
|
goto PROCESS_CREATION_FAILED;
|
|
|
|
}
|
2018-05-06 08:23:47 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Get the new process's id. */
|
|
|
|
svcGetProcessId(&new_process.pid, new_process.handle);
|
2018-05-06 08:23:47 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Register with FS. */
|
2018-05-06 08:23:47 +00:00
|
|
|
memcpy(fac, program_info.ac_buffer + program_info.acid_sac_size + program_info.aci0_sac_size, program_info.acid_fac_size);
|
|
|
|
memcpy(fah, program_info.ac_buffer + program_info.acid_sac_size + program_info.aci0_sac_size + program_info.acid_fac_size, program_info.aci0_fah_size);
|
|
|
|
if (R_FAILED((rc = fsprRegisterProgram(new_process.pid, new_process.tid_sid.title_id, new_process.tid_sid.storage_id, fah, program_info.aci0_fah_size, fac, program_info.acid_fac_size)))) {
|
2018-05-04 05:58:25 +00:00
|
|
|
goto FS_REGISTRATION_FAILED;
|
|
|
|
}
|
2018-05-06 08:23:47 +00:00
|
|
|
|
|
|
|
/* Register with SM. */
|
|
|
|
memcpy(acid_sac, program_info.ac_buffer, program_info.acid_sac_size);
|
|
|
|
memcpy(aci0_sac, program_info.ac_buffer + program_info.acid_sac_size, program_info.aci0_sac_size);
|
2018-05-04 05:58:25 +00:00
|
|
|
if (R_FAILED((rc = smManagerRegisterProcess(new_process.pid, acid_sac, program_info.acid_sac_size, aci0_sac, program_info.aci0_sac_size)))) {
|
|
|
|
goto SM_REGISTRATION_FAILED;
|
|
|
|
}
|
2018-05-06 08:23:47 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Setup process flags. */
|
|
|
|
if (program_info.application_type & 1) {
|
|
|
|
new_process.flags |= 0x40;
|
|
|
|
}
|
2018-05-04 07:01:53 +00:00
|
|
|
if (kernelAbove200() && LAUNCHFLAGS_NOTIYDEBUGSPECIAL(launch_flags) && (program_info.application_type & 4)) {
|
|
|
|
|
|
|
|
}
|
|
|
|
if (LAUNCHFLAGS_NOTIFYWHENEXITED(launch_flags)) {
|
2018-05-04 05:58:25 +00:00
|
|
|
new_process.flags |= 1;
|
|
|
|
}
|
2018-05-04 07:01:53 +00:00
|
|
|
if (LAUNCHFLAGS_NOTIFYDEBUGEVENTS(launch_flags) && (!kernelAbove200() || (program_info.application_type & 4))) {
|
2018-05-04 05:58:25 +00:00
|
|
|
new_process.flags |= 0x8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add process to the list. */
|
|
|
|
Registration::AddProcessToList(&new_process);
|
|
|
|
|
|
|
|
/* Signal, if relevant. */
|
|
|
|
if (new_process.tid_sid.title_id == g_debug_on_launch_tid.load()) {
|
|
|
|
g_debug_title_event->signal_event();
|
|
|
|
g_debug_on_launch_tid = 0;
|
|
|
|
rc = 0;
|
|
|
|
} else if ((new_process.flags & 0x40) && g_debug_next_application.load()) {
|
|
|
|
g_debug_application_event->signal_event();
|
|
|
|
g_debug_next_application = false;
|
|
|
|
rc = 0;
|
2018-05-04 07:01:53 +00:00
|
|
|
} else if (LAUNCHFLAGS_STARTSUSPENDED(launch_flags)) {
|
2018-05-04 05:58:25 +00:00
|
|
|
rc = 0;
|
|
|
|
} else {
|
|
|
|
rc = svcStartProcess(new_process.handle, program_info.main_thread_priority, program_info.default_cpu_id, program_info.main_thread_stack_size);
|
2018-05-06 08:23:47 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
SetProcessState(new_process.pid, ProcessState_DebugDetached);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
Registration::RemoveProcessFromList(new_process.pid);
|
|
|
|
smManagerUnregisterProcess(new_process.pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
SM_REGISTRATION_FAILED:
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
fsprUnregisterProgram(new_process.pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
FS_REGISTRATION_FAILED:
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
svcCloseHandle(new_process.handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_CREATION_FAILED:
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
ldrPmUnregisterTitle(new_process.ldr_queue_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE_PROCESS_LAUNCH_END:
|
|
|
|
g_process_launch_state.result = rc;
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
*out_pid = new_process.pid;
|
|
|
|
}
|
2018-05-06 08:23:47 +00:00
|
|
|
delete ac_buf;
|
2018-05-04 05:58:25 +00:00
|
|
|
g_sema_finish_launch.Signal();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-07 10:45:44 +00:00
|
|
|
Result Registration::LaunchDebugProcess(u64 pid) {
|
|
|
|
AutoProcessListLock auto_lock;
|
|
|
|
LoaderProgramInfo program_info = {0};
|
|
|
|
Result rc;
|
|
|
|
|
|
|
|
Process *proc = GetProcess(pid);
|
|
|
|
if (proc == NULL) {
|
|
|
|
return 0x20F;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (proc->state >= ProcessState_DebugDetached) {
|
|
|
|
return 0x40F;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that this is a real program. */
|
|
|
|
if (R_FAILED((rc = ldrPmGetProgramInfo(proc->tid_sid.title_id, proc->tid_sid.storage_id, &program_info)))) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (R_SUCCEEDED((rc = svcStartProcess(proc->handle, program_info.main_thread_priority, program_info.default_cpu_id, program_info.main_thread_stack_size)))) {
|
|
|
|
proc->state = ProcessState_DebugDetached;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
Result Registration::LaunchProcess(u64 title_id, FsStorageId storage_id, u64 launch_flags, u64 *out_pid) {
|
|
|
|
Result rc;
|
|
|
|
/* Only allow one mutex to exist. */
|
|
|
|
g_process_launch_mutex.Lock();
|
|
|
|
g_process_launch_state.tid_sid.title_id = title_id;
|
|
|
|
g_process_launch_state.tid_sid.storage_id = storage_id;
|
|
|
|
g_process_launch_state.launch_flags = launch_flags;
|
|
|
|
g_process_launch_state.out_pid = out_pid;
|
|
|
|
|
|
|
|
/* Start a launch, and wait for it to exit. */
|
2018-05-07 04:59:54 +00:00
|
|
|
g_process_launch_start_event->signal_event();
|
2018-05-04 05:58:25 +00:00
|
|
|
g_sema_finish_launch.Wait();
|
|
|
|
|
|
|
|
rc = g_process_launch_state.result;
|
|
|
|
|
|
|
|
g_process_launch_mutex.Unlock();
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Registration::LaunchProcessByTidSid(TidSid tid_sid, u64 launch_flags, u64 *out_pid) {
|
|
|
|
return LaunchProcess(tid_sid.title_id, tid_sid.storage_id, launch_flags, out_pid);
|
|
|
|
};
|
|
|
|
|
|
|
|
void Registration::HandleSignaledProcess(Process *process) {
|
|
|
|
u64 tmp;
|
2018-05-06 08:23:47 +00:00
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Reset the signal. */
|
|
|
|
svcResetSignal(process->handle);
|
|
|
|
|
|
|
|
ProcessState old_state;
|
|
|
|
old_state = process->state;
|
|
|
|
svcGetProcessInfo(&tmp, process->handle, ProcessInfoType_ProcessState);
|
|
|
|
process->state = (ProcessState)tmp;
|
|
|
|
|
|
|
|
if (old_state == ProcessState_Crashed && process->state != ProcessState_Crashed) {
|
|
|
|
process->flags &= ~0x4;
|
|
|
|
}
|
|
|
|
switch (process->state) {
|
|
|
|
case ProcessState_Created:
|
|
|
|
case ProcessState_DebugAttached:
|
|
|
|
case ProcessState_Exiting:
|
|
|
|
break;
|
|
|
|
case ProcessState_DebugDetached:
|
|
|
|
if (process->flags & 8) {
|
|
|
|
process->flags &= ~0x30;
|
|
|
|
process->flags |= 0x10;
|
|
|
|
g_process_event->signal_event();
|
|
|
|
}
|
2018-05-04 23:55:39 +00:00
|
|
|
if (kernelAbove200() && process->flags & 0x80) {
|
|
|
|
process->flags &= ~0x180;
|
|
|
|
process->flags |= 0x100;
|
|
|
|
}
|
2018-05-04 05:58:25 +00:00
|
|
|
break;
|
|
|
|
case ProcessState_Crashed:
|
|
|
|
process->flags |= 6;
|
|
|
|
g_process_event->signal_event();
|
|
|
|
break;
|
2018-05-04 23:55:39 +00:00
|
|
|
case ProcessState_Running:
|
|
|
|
if (process->flags & 8) {
|
|
|
|
process->flags &= ~0x30;
|
|
|
|
process->flags |= 0x10;
|
|
|
|
g_process_event->signal_event();
|
|
|
|
}
|
|
|
|
break;
|
2018-05-04 05:58:25 +00:00
|
|
|
case ProcessState_Exited:
|
2018-05-04 23:55:39 +00:00
|
|
|
if (process->flags & 1 && !kernelAbove500()) {
|
2018-05-04 05:58:25 +00:00
|
|
|
g_process_event->signal_event();
|
|
|
|
} else {
|
|
|
|
FinalizeExitedProcess(process);
|
|
|
|
}
|
2018-05-06 08:23:47 +00:00
|
|
|
//Reboot();
|
2018-05-04 05:58:25 +00:00
|
|
|
break;
|
|
|
|
case ProcessState_DebugSuspended:
|
|
|
|
if (process->flags & 8) {
|
|
|
|
process->flags |= 0x30;
|
|
|
|
g_process_event->signal_event();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Registration::FinalizeExitedProcess(Process *process) {
|
2018-05-05 01:56:59 +00:00
|
|
|
AutoProcessListLock auto_lock;
|
2018-05-04 23:55:39 +00:00
|
|
|
bool signal_debug_process_5x = kernelAbove500() && process->flags & 1;
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Unregister with FS. */
|
|
|
|
if (R_FAILED(fsprUnregisterProgram(process->pid))) {
|
|
|
|
/* TODO: Panic. */
|
|
|
|
}
|
|
|
|
/* Unregister with SM. */
|
|
|
|
if (R_FAILED(smManagerUnregisterProcess(process->pid))) {
|
|
|
|
/* TODO: Panic. */
|
|
|
|
}
|
|
|
|
/* Unregister with LDR. */
|
|
|
|
if (R_FAILED(ldrPmUnregisterTitle(process->ldr_queue_index))) {
|
|
|
|
/* TODO: Panic. */
|
|
|
|
}
|
|
|
|
|
2018-05-04 23:55:39 +00:00
|
|
|
/* Close the process's handle. */
|
|
|
|
svcCloseHandle(process->handle);
|
|
|
|
|
|
|
|
/* Insert into dead process list, if relevant. */
|
|
|
|
if (signal_debug_process_5x) {
|
|
|
|
g_dead_process_list.Lock();
|
|
|
|
g_dead_process_list.process_waiters.push_back(new ProcessWaiter(process));
|
|
|
|
g_dead_process_list.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove NOTE: This probably frees process. */
|
2018-05-04 05:58:25 +00:00
|
|
|
RemoveProcessFromList(process->pid);
|
|
|
|
|
2018-05-05 01:56:59 +00:00
|
|
|
auto_lock.Unlock();
|
2018-05-04 23:55:39 +00:00
|
|
|
if (signal_debug_process_5x) {
|
|
|
|
g_process_event->signal_event();
|
|
|
|
}
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Registration::AddProcessToList(Process *process) {
|
2018-05-05 01:56:59 +00:00
|
|
|
AutoProcessListLock auto_lock;
|
2018-05-04 05:58:25 +00:00
|
|
|
g_process_list.process_waiters.push_back(new ProcessWaiter(process));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Registration::RemoveProcessFromList(u64 pid) {
|
2018-05-05 01:56:59 +00:00
|
|
|
AutoProcessListLock auto_lock;
|
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Remove process from list. */
|
|
|
|
for (unsigned int i = 0; i < g_process_list.process_waiters.size(); i++) {
|
|
|
|
ProcessWaiter *pw = g_process_list.process_waiters[i];
|
|
|
|
Registration::Process *process = pw->get_process();
|
|
|
|
if (process->pid == pid) {
|
|
|
|
g_process_list.process_waiters.erase(g_process_list.process_waiters.begin() + i);
|
|
|
|
svcCloseHandle(process->handle);
|
|
|
|
delete pw;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Registration::SetProcessState(u64 pid, ProcessState new_state) {
|
2018-05-05 01:56:59 +00:00
|
|
|
AutoProcessListLock auto_lock;
|
|
|
|
|
2018-05-04 05:58:25 +00:00
|
|
|
/* Set process state. */
|
2018-05-05 01:56:59 +00:00
|
|
|
for (auto &pw : g_process_list.process_waiters) {
|
2018-05-04 05:58:25 +00:00
|
|
|
Registration::Process *process = pw->get_process();
|
|
|
|
if (process->pid == pid) {
|
|
|
|
process->state = new_state;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 01:56:59 +00:00
|
|
|
bool Registration::HasApplicationProcess(Process **out) {
|
|
|
|
AutoProcessListLock auto_lock;
|
2018-05-04 05:58:25 +00:00
|
|
|
|
|
|
|
for (auto &pw : g_process_list.process_waiters) {
|
2018-05-05 01:56:59 +00:00
|
|
|
Registration::Process *process = pw->get_process();
|
|
|
|
if (process->flags & 0x40) {
|
|
|
|
if (out != NULL) {
|
|
|
|
*out = process;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Registration::Process *Registration::GetProcess(u64 pid) {
|
|
|
|
AutoProcessListLock auto_lock;
|
|
|
|
|
|
|
|
for (auto &pw : g_process_list.process_waiters) {
|
|
|
|
Process *p = pw->get_process();
|
|
|
|
if (p->pid == pid) {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Registration::Process *Registration::GetProcessByTitleId(u64 tid) {
|
|
|
|
AutoProcessListLock auto_lock;
|
|
|
|
|
|
|
|
for (auto &pw : g_process_list.process_waiters) {
|
|
|
|
Process *p = pw->get_process();
|
|
|
|
if (p->tid_sid.title_id == tid) {
|
|
|
|
return p;
|
2018-05-04 05:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 01:56:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-05-07 10:45:44 +00:00
|
|
|
|
|
|
|
Result Registration::GetDebugProcessIds(u64 *out_pids, u32 max_out, u32 *num_out) {
|
|
|
|
AutoProcessListLock auto_lock;
|
|
|
|
u32 num = 0;
|
|
|
|
|
|
|
|
|
|
|
|
for (auto &pw : g_process_list.process_waiters) {
|
|
|
|
Process *p = pw->get_process();
|
|
|
|
if (p->flags & 4 && num < max_out) {
|
|
|
|
out_pids[num++] = p->pid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*num_out = num;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-05 01:56:59 +00:00
|
|
|
Handle Registration::GetProcessEventHandle() {
|
|
|
|
return g_process_event->get_handle();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Registration::GetProcessEventType(u64 *out_pid, u64 *out_type) {
|
|
|
|
AutoProcessListLock auto_lock;
|
|
|
|
|
|
|
|
for (auto &pw : g_process_list.process_waiters) {
|
|
|
|
Process *p = pw->get_process();
|
|
|
|
if (kernelAbove200() && p->state >= ProcessState_DebugDetached && p->flags & 0x100) {
|
|
|
|
p->flags &= ~0x100;
|
|
|
|
*out_pid = p->pid;
|
|
|
|
*out_type = kernelAbove500() ? 2 : 5;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (p->flags & 0x10) {
|
2018-05-06 08:23:47 +00:00
|
|
|
u64 old_flags = p->flags;
|
2018-05-05 01:56:59 +00:00
|
|
|
p->flags &= ~0x10;
|
|
|
|
*out_pid = p->pid;
|
2018-05-06 08:23:47 +00:00
|
|
|
*out_type = kernelAbove500() ? (((old_flags >> 5) & 1) | 4) : (((old_flags >> 5) & 1) + 3);
|
2018-05-05 01:56:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (p->flags & 2) {
|
|
|
|
*out_pid = p->pid;
|
|
|
|
*out_type = kernelAbove500() ? 3 : 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!kernelAbove500() && p->flags & 1 && p->state == ProcessState_Exited) {
|
|
|
|
*out_pid = p->pid;
|
|
|
|
*out_type = 2;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (kernelAbove500()) {
|
|
|
|
auto_lock.Unlock();
|
|
|
|
g_dead_process_list.Lock();
|
|
|
|
if (g_dead_process_list.process_waiters.size()) {
|
|
|
|
ProcessWaiter *pw = g_dead_process_list.process_waiters[0];
|
|
|
|
Registration::Process *process = pw->get_process();
|
|
|
|
g_dead_process_list.process_waiters.erase(g_dead_process_list.process_waiters.begin());
|
|
|
|
*out_pid = process->pid;
|
|
|
|
*out_type = 1;
|
|
|
|
delete pw;
|
|
|
|
g_dead_process_list.Unlock();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
g_dead_process_list.Unlock();
|
|
|
|
}
|
|
|
|
*out_pid = 0;
|
|
|
|
*out_type = 0;
|
|
|
|
}
|
|
|
|
|
2018-05-07 10:45:44 +00:00
|
|
|
|
|
|
|
Result Registration::EnableDebugForTitleId(u64 tid, Handle *out) {
|
|
|
|
u64 old = g_debug_on_launch_tid.exchange(tid);
|
|
|
|
if (old) {
|
|
|
|
g_debug_on_launch_tid = old;
|
|
|
|
return 0x80F;
|
|
|
|
}
|
|
|
|
*out = g_debug_title_event->get_handle();
|
|
|
|
return 0x0;
|
2018-05-05 01:56:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 10:45:44 +00:00
|
|
|
Result Registration::EnableDebugForApplication(Handle *out) {
|
|
|
|
g_debug_next_application = true;
|
|
|
|
*out = g_debug_application_event->get_handle();
|
|
|
|
return 0;
|
|
|
|
}
|