From afe7e41de8ef8a317dd88b5cd7ff4178020edf3c Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 22 Jul 2020 11:15:45 -0700 Subject: [PATCH] kern: SvcGetProcessInfo, SvcTerminateProcess --- .../include/mesosphere/kern_k_process.hpp | 2 + .../libmesosphere/source/kern_k_process.cpp | 4 ++ .../source/svc/kern_svc_process.cpp | 69 +++++++++++++++++-- 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_process.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_process.hpp index 15bc3275a..81cdb9b7e 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_process.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_process.hpp @@ -155,6 +155,8 @@ namespace ams::kern { return this->is_suspended; } + Result Terminate(); + constexpr bool IsTerminated() const { return this->state == State_Terminated; } diff --git a/libraries/libmesosphere/source/kern_k_process.cpp b/libraries/libmesosphere/source/kern_k_process.cpp index 4852e96bf..7c1c322f2 100644 --- a/libraries/libmesosphere/source/kern_k_process.cpp +++ b/libraries/libmesosphere/source/kern_k_process.cpp @@ -277,6 +277,10 @@ namespace ams::kern { MESOSPHERE_UNIMPLEMENTED(); } + Result KProcess::Terminate() { + MESOSPHERE_UNIMPLEMENTED(); + } + Result KProcess::AddSharedMemory(KSharedMemory *shmem, KProcessAddress address, size_t size) { /* Lock ourselves, to prevent concurrent access. */ KScopedLightLock lk(this->state_lock); diff --git a/libraries/libmesosphere/source/svc/kern_svc_process.cpp b/libraries/libmesosphere/source/svc/kern_svc_process.cpp index 791ffb689..38cbd7b9a 100644 --- a/libraries/libmesosphere/source/svc/kern_svc_process.cpp +++ b/libraries/libmesosphere/source/svc/kern_svc_process.cpp @@ -283,6 +283,67 @@ namespace ams::kern::svc { return ResultSuccess(); } + Result TerminateProcess(ams::svc::Handle process_handle) { + /* Get the target process. */ + KProcess *process = GetCurrentProcess().GetHandleTable().GetObject(process_handle).ReleasePointerUnsafe(); + R_UNLESS(process != nullptr, svc::ResultInvalidHandle()); + + if (process != GetCurrentProcessPointer()) { + /* We're terminating another process. Close our reference after terminating the process. */ + ON_SCOPE_EXIT { process->Close(); }; + + /* Terminate the process. */ + R_TRY(process->Terminate()); + } else { + /* We're terminating ourselves. Close our reference immediately. */ + process->Close(); + + /* Exit. */ + ExitProcess(); + } + + return ResultSuccess(); + } + + Result GetProcessInfo(int64_t *out, ams::svc::Handle process_handle, ams::svc::ProcessInfoType info_type) { + /* Get the target process. */ + KScopedAutoObject process = GetCurrentProcess().GetHandleTable().GetObject(process_handle); + R_UNLESS(process.IsNotNull(), svc::ResultInvalidHandle()); + + /* Get the info. */ + switch (info_type) { + case ams::svc::ProcessInfoType_ProcessState: + { + /* Get the process's state. */ + KProcess::State state; + { + KScopedLightLock proc_lk(process->GetStateLock()); + KScopedSchedulerLock sl; + + state = process->GetState(); + } + + /* Convert to svc state. */ + switch (state) { + case KProcess::State_Created: *out = ams::svc::ProcessState_Created; break; + case KProcess::State_CreatedAttached: *out = ams::svc::ProcessState_CreatedAttached; break; + case KProcess::State_Running: *out = ams::svc::ProcessState_Running; break; + case KProcess::State_Crashed: *out = ams::svc::ProcessState_Crashed; break; + case KProcess::State_RunningAttached: *out = ams::svc::ProcessState_RunningAttached; break; + case KProcess::State_Terminating: *out = ams::svc::ProcessState_Terminating; break; + case KProcess::State_Terminated: *out = ams::svc::ProcessState_Terminated; break; + case KProcess::State_DebugBreak: *out = ams::svc::ProcessState_DebugBreak; break; + MESOSPHERE_UNREACHABLE_DEFAULT_CASE(); + } + } + break; + default: + return svc::ResultInvalidEnumValue(); + } + + return ResultSuccess(); + } + } /* ============================= 64 ABI ============================= */ @@ -308,11 +369,11 @@ namespace ams::kern::svc { } Result TerminateProcess64(ams::svc::Handle process_handle) { - MESOSPHERE_PANIC("Stubbed SvcTerminateProcess64 was called."); + return TerminateProcess(process_handle); } Result GetProcessInfo64(int64_t *out_info, ams::svc::Handle process_handle, ams::svc::ProcessInfoType info_type) { - MESOSPHERE_PANIC("Stubbed SvcGetProcessInfo64 was called."); + return GetProcessInfo(out_info, process_handle, info_type); } /* ============================= 64From32 ABI ============================= */ @@ -338,11 +399,11 @@ namespace ams::kern::svc { } Result TerminateProcess64From32(ams::svc::Handle process_handle) { - MESOSPHERE_PANIC("Stubbed SvcTerminateProcess64From32 was called."); + return TerminateProcess(process_handle); } Result GetProcessInfo64From32(int64_t *out_info, ams::svc::Handle process_handle, ams::svc::ProcessInfoType info_type) { - MESOSPHERE_PANIC("Stubbed SvcGetProcessInfo64From32 was called."); + return GetProcessInfo(out_info, process_handle, info_type); } }