2018-05-05 01:25:26 +01:00
|
|
|
#include <switch.h>
|
2018-06-15 00:50:01 +01:00
|
|
|
#include <stratosphere.hpp>
|
2018-05-05 01:25:26 +01:00
|
|
|
#include "pm_registration.hpp"
|
2018-05-07 10:50:33 +01:00
|
|
|
#include "pm_resource_limits.hpp"
|
2018-05-05 01:25:26 +01:00
|
|
|
#include "pm_shell.hpp"
|
2018-07-27 10:23:53 +01:00
|
|
|
#include "pm_boot2.hpp"
|
2018-05-05 01:25:26 +01:00
|
|
|
|
|
|
|
static bool g_has_boot_finished = false;
|
|
|
|
|
|
|
|
Result ShellService::dispatch(IpcParsedCommand &r, IpcCommand &out_c, u64 cmd_id, u8 *pointer_buffer, size_t pointer_buffer_size) {
|
|
|
|
Result rc = 0xF601;
|
2018-05-07 11:45:44 +01:00
|
|
|
|
2018-05-05 01:25:26 +01:00
|
|
|
if (kernelAbove500()) {
|
|
|
|
switch ((ShellCmd_5X)cmd_id) {
|
|
|
|
case Shell_Cmd_5X_LaunchProcess:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::launch_process>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_5X_TerminateProcessId:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::terminate_process_id>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_5X_TerminateTitleId:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::terminate_title_id>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_5X_GetProcessWaitEvent:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::get_process_wait_event>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_5X_GetProcessEventType:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::get_process_event_type>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_5X_NotifyBootFinished:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::notify_boot_finished>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_5X_GetApplicationProcessId:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::get_application_process_id>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_5X_BoostSystemMemoryResourceLimit:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::boost_system_memory_resource_limit>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
2018-05-07 11:45:44 +01:00
|
|
|
default:
|
|
|
|
break;
|
2018-05-05 01:25:26 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch ((ShellCmd)cmd_id) {
|
|
|
|
case Shell_Cmd_LaunchProcess:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::launch_process>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_TerminateProcessId:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::terminate_process_id>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_TerminateTitleId:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::terminate_title_id>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_GetProcessWaitEvent:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::get_process_wait_event>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_GetProcessEventType:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::get_process_event_type>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
2018-05-05 02:56:59 +01:00
|
|
|
case Shell_Cmd_FinalizeExitedProcess:
|
2018-05-05 03:16:40 +01:00
|
|
|
rc = WrapIpcCommandImpl<&ShellService::finalize_exited_process>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
2018-05-05 01:25:26 +01:00
|
|
|
break;
|
|
|
|
case Shell_Cmd_ClearProcessNotificationFlag:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::clear_process_notification_flag>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_NotifyBootFinished:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::notify_boot_finished>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_GetApplicationProcessId:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::get_application_process_id>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
case Shell_Cmd_BoostSystemMemoryResourceLimit:
|
|
|
|
rc = WrapIpcCommandImpl<&ShellService::boost_system_memory_resource_limit>(this, r, out_c, pointer_buffer, pointer_buffer_size);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-05-07 11:45:44 +01:00
|
|
|
|
2018-05-05 01:25:26 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result ShellService::handle_deferred() {
|
|
|
|
/* This service is never deferrable. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-05 03:16:40 +01:00
|
|
|
std::tuple<Result, u64> ShellService::launch_process(u64 launch_flags, Registration::TidSid tid_sid) {
|
2018-05-05 02:56:59 +01:00
|
|
|
u64 pid = 0;
|
|
|
|
Result rc = Registration::LaunchProcessByTidSid(tid_sid, launch_flags, &pid);
|
|
|
|
return {rc, pid};
|
2018-05-05 01:25:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-05 03:16:40 +01:00
|
|
|
std::tuple<Result> ShellService::terminate_process_id(u64 pid) {
|
2018-07-02 15:10:57 +01:00
|
|
|
auto auto_lock = Registration::GetProcessListUniqueLock();
|
2018-05-05 02:56:59 +01:00
|
|
|
|
2018-06-15 07:47:07 +01:00
|
|
|
std::shared_ptr<Registration::Process> proc = Registration::GetProcess(pid);
|
2018-05-05 02:56:59 +01:00
|
|
|
if (proc != NULL) {
|
2018-05-06 09:23:47 +01:00
|
|
|
return {svcTerminateProcess(proc->handle)};
|
2018-05-05 02:56:59 +01:00
|
|
|
} else {
|
|
|
|
return {0x20F};
|
|
|
|
}
|
2018-05-05 01:25:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-05 03:16:40 +01:00
|
|
|
std::tuple<Result> ShellService::terminate_title_id(u64 tid) {
|
2018-07-02 15:10:57 +01:00
|
|
|
auto auto_lock = Registration::GetProcessListUniqueLock();
|
2018-05-05 02:56:59 +01:00
|
|
|
|
2018-06-15 07:47:07 +01:00
|
|
|
std::shared_ptr<Registration::Process> proc = Registration::GetProcessByTitleId(tid);
|
2018-05-05 02:56:59 +01:00
|
|
|
if (proc != NULL) {
|
2018-05-06 09:23:47 +01:00
|
|
|
return {svcTerminateProcess(proc->handle)};
|
2018-05-05 02:56:59 +01:00
|
|
|
} else {
|
|
|
|
return {0x20F};
|
|
|
|
}
|
2018-05-05 01:25:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-05 03:16:40 +01:00
|
|
|
std::tuple<Result, CopiedHandle> ShellService::get_process_wait_event() {
|
2018-05-05 02:56:59 +01:00
|
|
|
return {0x0, Registration::GetProcessEventHandle()};
|
2018-05-05 01:25:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-05 03:16:40 +01:00
|
|
|
std::tuple<Result, u64, u64> ShellService::get_process_event_type() {
|
2018-05-05 02:56:59 +01:00
|
|
|
u64 type, pid;
|
|
|
|
Registration::GetProcessEventType(&pid, &type);
|
|
|
|
return {0x0, type, pid};
|
2018-05-05 01:25:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-05 03:16:40 +01:00
|
|
|
std::tuple<Result> ShellService::finalize_exited_process(u64 pid) {
|
2018-07-02 15:10:57 +01:00
|
|
|
auto auto_lock = Registration::GetProcessListUniqueLock();
|
2018-05-05 02:56:59 +01:00
|
|
|
|
2018-06-15 07:47:07 +01:00
|
|
|
std::shared_ptr<Registration::Process> proc = Registration::GetProcess(pid);
|
2018-05-05 02:56:59 +01:00
|
|
|
if (proc == NULL) {
|
|
|
|
return {0x20F};
|
|
|
|
} else if (proc->state != ProcessState_Exited) {
|
|
|
|
return {0x60F};
|
|
|
|
} else {
|
|
|
|
Registration::FinalizeExitedProcess(proc);
|
|
|
|
return {0x0};
|
|
|
|
}
|
2018-05-05 01:25:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-05 03:16:40 +01:00
|
|
|
std::tuple<Result> ShellService::clear_process_notification_flag(u64 pid) {
|
2018-07-02 15:10:57 +01:00
|
|
|
auto auto_lock = Registration::GetProcessListUniqueLock();
|
2018-05-05 02:56:59 +01:00
|
|
|
|
2018-06-15 07:47:07 +01:00
|
|
|
std::shared_ptr<Registration::Process> proc = Registration::GetProcess(pid);
|
2018-05-05 02:56:59 +01:00
|
|
|
if (proc != NULL) {
|
2018-07-27 09:56:24 +01:00
|
|
|
proc->flags &= ~PROCESSFLAGS_CRASHED;
|
2018-05-05 02:56:59 +01:00
|
|
|
return {0x0};
|
|
|
|
} else {
|
|
|
|
return {0x20F};
|
|
|
|
}
|
2018-05-05 01:25:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-05 03:16:40 +01:00
|
|
|
std::tuple<Result> ShellService::notify_boot_finished() {
|
2018-05-05 01:25:26 +01:00
|
|
|
if (!g_has_boot_finished) {
|
|
|
|
g_has_boot_finished = true;
|
2018-07-27 10:23:53 +01:00
|
|
|
EmbeddedBoot2::Main();
|
2018-05-05 01:25:26 +01:00
|
|
|
}
|
|
|
|
return {0};
|
|
|
|
}
|
|
|
|
|
2018-05-05 03:16:40 +01:00
|
|
|
std::tuple<Result, u64> ShellService::get_application_process_id() {
|
2018-07-02 15:10:57 +01:00
|
|
|
auto auto_lock = Registration::GetProcessListUniqueLock();
|
2018-05-05 02:56:59 +01:00
|
|
|
|
2018-06-15 07:47:07 +01:00
|
|
|
std::shared_ptr<Registration::Process> app_proc;
|
2018-05-05 02:56:59 +01:00
|
|
|
if (Registration::HasApplicationProcess(&app_proc)) {
|
|
|
|
return {0, app_proc->pid};
|
|
|
|
}
|
|
|
|
return {0x20F, 0};
|
2018-05-05 01:25:26 +01:00
|
|
|
}
|
|
|
|
|
2018-05-07 10:50:33 +01:00
|
|
|
std::tuple<Result> ShellService::boost_system_memory_resource_limit(u64 sysmem_size) {
|
2018-05-05 02:56:59 +01:00
|
|
|
if (!kernelAbove400()) {
|
|
|
|
return {0xF601};
|
|
|
|
}
|
|
|
|
|
2018-05-05 01:25:26 +01:00
|
|
|
/* TODO */
|
2018-05-07 10:50:33 +01:00
|
|
|
return {ResourceLimitUtils::BoostSystemMemoryResourceLimit(sysmem_size)};
|
2018-05-05 01:25:26 +01:00
|
|
|
}
|