2019-06-29 09:20:36 +00:00
|
|
|
/*
|
2021-10-04 19:59:10 +00:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-06-29 09:20:36 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "pm_process_manager.hpp"
|
|
|
|
|
2019-10-24 09:30:10 +00:00
|
|
|
namespace ams::pm::impl {
|
2019-06-29 09:20:36 +00:00
|
|
|
|
2019-09-28 01:04:58 +00:00
|
|
|
class ProcessList;
|
|
|
|
|
2019-06-29 09:20:36 +00:00
|
|
|
class ProcessInfo {
|
2019-09-28 01:04:58 +00:00
|
|
|
friend class ProcessList;
|
2019-06-29 09:20:36 +00:00
|
|
|
NON_COPYABLE(ProcessInfo);
|
2019-09-28 01:04:58 +00:00
|
|
|
NON_MOVEABLE(ProcessInfo);
|
2019-06-29 09:20:36 +00:00
|
|
|
private:
|
|
|
|
enum Flag : u32 {
|
|
|
|
Flag_SignalOnExit = (1 << 0),
|
|
|
|
Flag_ExceptionOccurred = (1 << 1),
|
|
|
|
Flag_ExceptionWaitingAttach = (1 << 2),
|
|
|
|
Flag_SignalOnDebugEvent = (1 << 3),
|
|
|
|
Flag_SuspendedStateChanged = (1 << 4),
|
|
|
|
Flag_Suspended = (1 << 5),
|
|
|
|
Flag_Application = (1 << 6),
|
|
|
|
Flag_SignalOnStart = (1 << 7),
|
|
|
|
Flag_StartedStateChanged = (1 << 8),
|
2020-12-15 02:53:16 +00:00
|
|
|
Flag_UnhandledException = (1 << 9),
|
2019-06-29 09:20:36 +00:00
|
|
|
};
|
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
util::IntrusiveListNode m_list_node;
|
|
|
|
const os::ProcessId m_process_id;
|
|
|
|
const ldr::PinId m_pin_id;
|
|
|
|
const ncm::ProgramLocation m_loc;
|
|
|
|
const cfg::OverrideStatus m_status;
|
|
|
|
os::NativeHandle m_handle;
|
|
|
|
svc::ProcessState m_state;
|
|
|
|
u32 m_flags;
|
|
|
|
os::MultiWaitHolderType m_multi_wait_holder;
|
2019-06-29 09:20:36 +00:00
|
|
|
private:
|
|
|
|
void SetFlag(Flag flag) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_flags |= flag;
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClearFlag(Flag flag) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_flags &= ~flag;
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HasFlag(Flag flag) const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return (m_flags & flag);
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
public:
|
2021-10-04 21:54:13 +00:00
|
|
|
ProcessInfo(os::NativeHandle h, os::ProcessId pid, ldr::PinId pin, const ncm::ProgramLocation &l, const cfg::OverrideStatus &s);
|
2019-06-29 09:20:36 +00:00
|
|
|
~ProcessInfo();
|
|
|
|
void Cleanup();
|
|
|
|
|
2021-10-01 02:00:47 +00:00
|
|
|
void LinkToMultiWait(os::MultiWaitType &multi_wait) {
|
2021-10-10 07:14:06 +00:00
|
|
|
os::LinkMultiWaitHolder(std::addressof(multi_wait), std::addressof(m_multi_wait_holder));
|
2019-09-28 01:04:58 +00:00
|
|
|
}
|
|
|
|
|
2021-10-04 21:54:13 +00:00
|
|
|
os::NativeHandle GetHandle() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_handle;
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 05:49:06 +00:00
|
|
|
os::ProcessId GetProcessId() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_process_id;
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ldr::PinId GetPinId() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_pin_id;
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
const ncm::ProgramLocation &GetProgramLocation() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_loc;
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
2019-11-21 12:03:19 +00:00
|
|
|
|
|
|
|
const cfg::OverrideStatus &GetOverrideStatus() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_status;
|
2019-11-21 12:03:19 +00:00
|
|
|
}
|
2019-06-29 09:20:36 +00:00
|
|
|
|
2020-03-29 22:24:40 +00:00
|
|
|
svc::ProcessState GetState() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_state;
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 22:24:40 +00:00
|
|
|
void SetState(svc::ProcessState state) {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_state = state;
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HasStarted() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_state != svc::ProcessState_Created && m_state != svc::ProcessState_CreatedAttached;
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 22:24:40 +00:00
|
|
|
bool HasTerminated() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_state == svc::ProcessState_Terminated;
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 05:21:47 +00:00
|
|
|
#define DEFINE_FLAG_SET(flag) \
|
|
|
|
void Set##flag() { \
|
|
|
|
this->SetFlag(Flag_##flag); \
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 05:21:47 +00:00
|
|
|
#define DEFINE_FLAG_GET(get, flag) \
|
|
|
|
bool get##flag() const { \
|
|
|
|
return this->HasFlag(Flag_##flag); \
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 05:21:47 +00:00
|
|
|
#define DEFINE_FLAG_CLEAR(flag) \
|
|
|
|
void Clear##flag() { \
|
|
|
|
this->ClearFlag(Flag_##flag); \
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 05:21:47 +00:00
|
|
|
DEFINE_FLAG_SET(SignalOnExit)
|
|
|
|
DEFINE_FLAG_GET(Should, SignalOnExit)
|
2019-06-29 09:20:36 +00:00
|
|
|
|
2019-07-03 05:21:47 +00:00
|
|
|
/* This needs a manual setter, because it sets two flags. */
|
|
|
|
void SetExceptionOccurred() {
|
|
|
|
this->SetFlag(Flag_ExceptionOccurred);
|
2020-12-15 02:53:16 +00:00
|
|
|
this->SetFlag(Flag_UnhandledException);
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 05:21:47 +00:00
|
|
|
DEFINE_FLAG_GET(Has, ExceptionOccurred)
|
|
|
|
DEFINE_FLAG_GET(Has, ExceptionWaitingAttach)
|
2020-12-15 02:53:16 +00:00
|
|
|
DEFINE_FLAG_GET(Has, UnhandledException)
|
|
|
|
|
|
|
|
DEFINE_FLAG_SET(ExceptionWaitingAttach)
|
|
|
|
|
2019-07-03 05:21:47 +00:00
|
|
|
DEFINE_FLAG_CLEAR(ExceptionOccurred)
|
|
|
|
DEFINE_FLAG_CLEAR(ExceptionWaitingAttach)
|
2020-12-15 02:53:16 +00:00
|
|
|
DEFINE_FLAG_CLEAR(UnhandledException)
|
2019-06-29 09:20:36 +00:00
|
|
|
|
2019-07-03 05:21:47 +00:00
|
|
|
DEFINE_FLAG_SET(SignalOnDebugEvent)
|
|
|
|
DEFINE_FLAG_GET(Should, SignalOnDebugEvent)
|
2019-06-29 09:20:36 +00:00
|
|
|
|
2019-07-03 05:21:47 +00:00
|
|
|
DEFINE_FLAG_SET(SuspendedStateChanged)
|
|
|
|
DEFINE_FLAG_GET(Has, SuspendedStateChanged)
|
|
|
|
DEFINE_FLAG_CLEAR(SuspendedStateChanged)
|
2019-06-29 09:20:36 +00:00
|
|
|
|
2019-07-03 05:21:47 +00:00
|
|
|
DEFINE_FLAG_SET(Suspended)
|
|
|
|
DEFINE_FLAG_GET(Is, Suspended)
|
|
|
|
DEFINE_FLAG_CLEAR(Suspended)
|
|
|
|
|
|
|
|
DEFINE_FLAG_SET(Application)
|
|
|
|
DEFINE_FLAG_GET(Is, Application)
|
|
|
|
|
|
|
|
DEFINE_FLAG_SET(SignalOnStart)
|
|
|
|
DEFINE_FLAG_GET(Should, SignalOnStart)
|
|
|
|
DEFINE_FLAG_CLEAR(SignalOnStart)
|
|
|
|
|
|
|
|
DEFINE_FLAG_SET(StartedStateChanged)
|
|
|
|
DEFINE_FLAG_GET(Has, StartedStateChanged)
|
|
|
|
DEFINE_FLAG_CLEAR(StartedStateChanged)
|
|
|
|
|
|
|
|
#undef DEFINE_FLAG_SET
|
|
|
|
#undef DEFINE_FLAG_GET
|
|
|
|
#undef DEFINE_FLAG_CLEAR
|
2019-06-29 09:20:36 +00:00
|
|
|
};
|
|
|
|
|
2021-10-10 07:14:06 +00:00
|
|
|
class ProcessList final : public util::IntrusiveListMemberTraits<&ProcessInfo::m_list_node>::ListType {
|
2019-06-29 09:20:36 +00:00
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
os::SdkMutex m_lock;
|
2019-06-29 09:20:36 +00:00
|
|
|
public:
|
2021-10-10 07:14:06 +00:00
|
|
|
constexpr ProcessList() : m_lock() { /* ... */ }
|
2020-04-08 09:21:35 +00:00
|
|
|
|
2019-06-29 09:20:36 +00:00
|
|
|
void Lock() {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_lock.Lock();
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Unlock() {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_lock.Unlock();
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-09-28 01:04:58 +00:00
|
|
|
void Remove(ProcessInfo *process_info) {
|
|
|
|
this->erase(this->iterator_to(*process_info));
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-10-15 05:49:06 +00:00
|
|
|
ProcessInfo *Find(os::ProcessId process_id) {
|
2021-10-10 07:14:06 +00:00
|
|
|
for (auto &info : *this) {
|
|
|
|
if (info.GetProcessId() == process_id) {
|
|
|
|
return std::addressof(info);
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
ProcessInfo *Find(ncm::ProgramId program_id) {
|
2021-10-10 07:14:06 +00:00
|
|
|
for (auto &info : *this) {
|
|
|
|
if (info.GetProgramLocation().program_id == program_id) {
|
|
|
|
return std::addressof(info);
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProcessListAccessor final {
|
|
|
|
private:
|
2021-10-10 07:14:06 +00:00
|
|
|
ProcessList &m_list;
|
2019-06-29 09:20:36 +00:00
|
|
|
public:
|
2021-10-10 07:14:06 +00:00
|
|
|
explicit ProcessListAccessor(ProcessList &l) : m_list(l) {
|
|
|
|
m_list.Lock();
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~ProcessListAccessor() {
|
2021-10-10 07:14:06 +00:00
|
|
|
m_list.Unlock();
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ProcessList *operator->() {
|
2021-10-10 07:14:06 +00:00
|
|
|
return std::addressof(m_list);
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ProcessList *operator->() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return std::addressof(m_list);
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ProcessList &operator*() {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_list;
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ProcessList &operator*() const {
|
2021-10-10 07:14:06 +00:00
|
|
|
return m_list;
|
2019-06-29 09:20:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|