2019-06-29 10:20:36 +01:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-06-29 10:20:36 +01: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 10:30:10 +01:00
|
|
|
namespace ams::pm::impl {
|
2019-06-29 10:20:36 +01:00
|
|
|
|
2019-09-28 02:04:58 +01:00
|
|
|
class ProcessList;
|
|
|
|
|
2019-06-29 10:20:36 +01:00
|
|
|
class ProcessInfo {
|
2019-09-28 02:04:58 +01:00
|
|
|
friend class ProcessList;
|
2019-06-29 10:20:36 +01:00
|
|
|
NON_COPYABLE(ProcessInfo);
|
2019-09-28 02:04:58 +01:00
|
|
|
NON_MOVEABLE(ProcessInfo);
|
2019-06-29 10:20:36 +01: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),
|
|
|
|
};
|
|
|
|
private:
|
2019-09-28 02:04:58 +01:00
|
|
|
util::IntrusiveListNode list_node;
|
2019-10-15 06:49:06 +01:00
|
|
|
const os::ProcessId process_id;
|
2019-06-29 10:20:36 +01:00
|
|
|
const ldr::PinId pin_id;
|
2019-10-28 04:43:01 +00:00
|
|
|
const ncm::ProgramLocation loc;
|
2019-11-21 12:03:19 +00:00
|
|
|
const cfg::OverrideStatus status;
|
2019-06-29 10:20:36 +01:00
|
|
|
Handle handle;
|
|
|
|
ProcessState state;
|
|
|
|
u32 flags;
|
2019-09-28 02:04:58 +01:00
|
|
|
os::WaitableHolder waitable_holder;
|
2019-06-29 10:20:36 +01:00
|
|
|
private:
|
|
|
|
void SetFlag(Flag flag) {
|
|
|
|
this->flags |= flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearFlag(Flag flag) {
|
|
|
|
this->flags &= ~flag;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasFlag(Flag flag) const {
|
|
|
|
return (this->flags & flag);
|
|
|
|
}
|
|
|
|
public:
|
2019-11-21 12:03:19 +00:00
|
|
|
ProcessInfo(Handle h, os::ProcessId pid, ldr::PinId pin, const ncm::ProgramLocation &l, const cfg::OverrideStatus &s);
|
2019-06-29 10:20:36 +01:00
|
|
|
~ProcessInfo();
|
|
|
|
void Cleanup();
|
|
|
|
|
2019-09-28 02:04:58 +01:00
|
|
|
void LinkToWaitableManager(os::WaitableManager &manager) {
|
|
|
|
manager.LinkWaitableHolder(&this->waitable_holder);
|
|
|
|
}
|
|
|
|
|
2019-06-29 10:20:36 +01:00
|
|
|
Handle GetHandle() const {
|
|
|
|
return this->handle;
|
|
|
|
}
|
|
|
|
|
2019-10-15 06:49:06 +01:00
|
|
|
os::ProcessId GetProcessId() const {
|
2019-06-29 10:20:36 +01:00
|
|
|
return this->process_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
ldr::PinId GetPinId() const {
|
|
|
|
return this->pin_id;
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
const ncm::ProgramLocation &GetProgramLocation() const {
|
2019-06-29 10:20:36 +01:00
|
|
|
return this->loc;
|
|
|
|
}
|
2019-11-21 12:03:19 +00:00
|
|
|
|
|
|
|
const cfg::OverrideStatus &GetOverrideStatus() const {
|
|
|
|
return this->status;
|
|
|
|
}
|
2019-06-29 10:20:36 +01:00
|
|
|
|
|
|
|
ProcessState GetState() const {
|
|
|
|
return this->state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetState(ProcessState state) {
|
|
|
|
this->state = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasStarted() const {
|
|
|
|
return this->state != ProcessState_Created && this->state != ProcessState_CreatedAttached;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HasExited() const {
|
|
|
|
return this->state == ProcessState_Exited;
|
|
|
|
}
|
|
|
|
|
2019-07-03 06:21:47 +01:00
|
|
|
#define DEFINE_FLAG_SET(flag) \
|
|
|
|
void Set##flag() { \
|
|
|
|
this->SetFlag(Flag_##flag); \
|
2019-06-29 10:20:36 +01:00
|
|
|
}
|
|
|
|
|
2019-07-03 06:21:47 +01:00
|
|
|
#define DEFINE_FLAG_GET(get, flag) \
|
|
|
|
bool get##flag() const { \
|
|
|
|
return this->HasFlag(Flag_##flag); \
|
2019-06-29 10:20:36 +01:00
|
|
|
}
|
|
|
|
|
2019-07-03 06:21:47 +01:00
|
|
|
#define DEFINE_FLAG_CLEAR(flag) \
|
|
|
|
void Clear##flag() { \
|
|
|
|
this->ClearFlag(Flag_##flag); \
|
2019-06-29 10:20:36 +01:00
|
|
|
}
|
|
|
|
|
2019-07-03 06:21:47 +01:00
|
|
|
DEFINE_FLAG_SET(SignalOnExit)
|
|
|
|
DEFINE_FLAG_GET(Should, SignalOnExit)
|
2019-06-29 10:20:36 +01:00
|
|
|
|
2019-07-03 06:21:47 +01:00
|
|
|
/* This needs a manual setter, because it sets two flags. */
|
|
|
|
void SetExceptionOccurred() {
|
|
|
|
this->SetFlag(Flag_ExceptionOccurred);
|
|
|
|
this->SetFlag(Flag_ExceptionWaitingAttach);
|
2019-06-29 10:20:36 +01:00
|
|
|
}
|
|
|
|
|
2019-07-03 06:21:47 +01:00
|
|
|
DEFINE_FLAG_GET(Has, ExceptionOccurred)
|
|
|
|
DEFINE_FLAG_GET(Has, ExceptionWaitingAttach)
|
|
|
|
DEFINE_FLAG_CLEAR(ExceptionOccurred)
|
|
|
|
DEFINE_FLAG_CLEAR(ExceptionWaitingAttach)
|
2019-06-29 10:20:36 +01:00
|
|
|
|
2019-07-03 06:21:47 +01:00
|
|
|
DEFINE_FLAG_SET(SignalOnDebugEvent)
|
|
|
|
DEFINE_FLAG_GET(Should, SignalOnDebugEvent)
|
2019-06-29 10:20:36 +01:00
|
|
|
|
2019-07-03 06:21:47 +01:00
|
|
|
DEFINE_FLAG_SET(SuspendedStateChanged)
|
|
|
|
DEFINE_FLAG_GET(Has, SuspendedStateChanged)
|
|
|
|
DEFINE_FLAG_CLEAR(SuspendedStateChanged)
|
2019-06-29 10:20:36 +01:00
|
|
|
|
2019-07-03 06:21:47 +01: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 10:20:36 +01:00
|
|
|
};
|
|
|
|
|
2019-09-28 02:04:58 +01:00
|
|
|
class ProcessList final : public util::IntrusiveListMemberTraits<&ProcessInfo::list_node>::ListType {
|
2019-06-29 10:20:36 +01:00
|
|
|
private:
|
2019-09-24 11:15:36 +01:00
|
|
|
os::Mutex lock;
|
2019-06-29 10:20:36 +01:00
|
|
|
public:
|
|
|
|
void Lock() {
|
|
|
|
this->lock.Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unlock() {
|
|
|
|
this->lock.Unlock();
|
|
|
|
}
|
|
|
|
|
2019-09-28 02:04:58 +01:00
|
|
|
void Remove(ProcessInfo *process_info) {
|
|
|
|
this->erase(this->iterator_to(*process_info));
|
2019-06-29 10:20:36 +01:00
|
|
|
}
|
|
|
|
|
2019-10-15 06:49:06 +01:00
|
|
|
ProcessInfo *Find(os::ProcessId process_id) {
|
2019-09-28 02:04:58 +01:00
|
|
|
for (auto it = this->begin(); it != this->end(); it++) {
|
|
|
|
if ((*it).GetProcessId() == process_id) {
|
|
|
|
return &*it;
|
2019-06-29 10:20:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
ProcessInfo *Find(ncm::ProgramId program_id) {
|
2019-09-28 02:04:58 +01:00
|
|
|
for (auto it = this->begin(); it != this->end(); it++) {
|
2019-10-28 04:43:01 +00:00
|
|
|
if ((*it).GetProgramLocation().program_id == program_id) {
|
2019-09-28 02:04:58 +01:00
|
|
|
return &*it;
|
2019-06-29 10:20:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProcessListAccessor final {
|
|
|
|
private:
|
|
|
|
ProcessList &list;
|
|
|
|
public:
|
|
|
|
explicit ProcessListAccessor(ProcessList &l) : list(l) {
|
|
|
|
this->list.Lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ProcessListAccessor() {
|
|
|
|
this->list.Unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessList *operator->() {
|
|
|
|
return &this->list;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ProcessList *operator->() const {
|
|
|
|
return &this->list;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessList &operator*() {
|
|
|
|
return this->list;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ProcessList &operator*() const {
|
|
|
|
return this->list;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|