fssrv: skeleton more program registry logic

This commit is contained in:
Michael Scire
2021-12-07 02:04:39 -08:00
committed by SciresM
parent 33701bb387
commit e8d14eb77d
13 changed files with 448 additions and 3 deletions

View File

@@ -0,0 +1,69 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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/>.
*/
#include <stratosphere.hpp>
#include "fssrv_program_info.hpp"
namespace ams::fssrv::impl {
namespace {
constinit os::SdkMutex g_mutex;
constinit bool g_initialized = false;
constinit u64 g_initial_process_id_min = 0;
constinit u64 g_initial_process_id_max = 0;
constinit u64 g_current_process_id = 0;
ALWAYS_INLINE void InitializeInitialAndCurrentProcessId() {
if (AMS_UNLIKELY(!g_initialized)) {
std::scoped_lock lk(g_mutex);
if (AMS_LIKELY(!g_initialized)) {
/* Get initial process id range. */
R_ABORT_UNLESS(svc::GetSystemInfo(std::addressof(g_initial_process_id_min), svc::SystemInfoType_InitialProcessIdRange, svc::InvalidHandle, svc::InitialProcessIdRangeInfo_Minimum));
R_ABORT_UNLESS(svc::GetSystemInfo(std::addressof(g_initial_process_id_max), svc::SystemInfoType_InitialProcessIdRange, svc::InvalidHandle, svc::InitialProcessIdRangeInfo_Maximum));
AMS_ABORT_UNLESS(0 < g_initial_process_id_min);
AMS_ABORT_UNLESS(g_initial_process_id_min <= g_initial_process_id_max);
/* Get current procss id. */
R_ABORT_UNLESS(svc::GetProcessId(std::addressof(g_current_process_id), svc::PseudoHandle::CurrentProcess));
/* Set initialized. */
g_initialized = true;
}
}
}
}
bool IsInitialProgram(u64 process_id) {
/* Initialize/sanity check. */
InitializeInitialAndCurrentProcessId();
AMS_ABORT_UNLESS(g_initial_process_id_min > 0);
/* Check process id in range. */
return g_initial_process_id_min <= process_id && process_id <= g_initial_process_id_max;
}
bool IsCurrentProcess(u64 process_id) {
/* Initialize. */
InitializeInitialAndCurrentProcessId();
return process_id == g_current_process_id;
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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 <stratosphere.hpp>
namespace ams::fssrv::impl {
class ProgramInfo : public ::ams::fs::impl::Newable {
private:
u64 m_process_id;
ncm::ProgramId m_program_id;
ncm::StorageId m_storage_id;
/* TODO: AccessControl m_access_control; */
public:
ProgramInfo(u64 process_id, u64 program_id, u8 storage_id, const void *data, s64 data_size, const void *desc, s64 desc_size) : m_process_id(process_id) /* TODO: m_access_control */ {
m_program_id.value = program_id;
m_storage_id = static_cast<ncm::StorageId>(storage_id);
/* TODO */
AMS_UNUSED(data, data_size, desc, desc_size);
}
};
struct ProgramInfoNode : public util::IntrusiveListBaseNode<ProgramInfoNode>, public ::ams::fs::impl::Newable {
std::shared_ptr<ProgramInfo> program_info;
};
bool IsInitialProgram(u64 process_id);
bool IsCurrentProcess(u64 process_id);
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright (c) Atmosphère-NX
*
* 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 <stratosphere.hpp>
#include "fssrv_program_info.hpp"
namespace ams::fssrv::impl {
class ProgramRegistryManager {
NON_COPYABLE(ProgramRegistryManager);
NON_MOVEABLE(ProgramRegistryManager);
private:
using ProgramInfoList = util::IntrusiveListBaseTraits<ProgramInfoNode>::ListType;
private:
ProgramInfoList m_program_info_list{};
os::SdkMutex m_mutex{};
public:
constexpr ProgramRegistryManager() = default;
Result RegisterProgram(u64 process_id, u64 program_id, u8 storage_id, const void *data, s64 data_size, const void *desc, s64 desc_size);
Result UnregisterProgram(u64 process_id);
Result GetProgramInfo(std::shared_ptr<ProgramInfo> *out, u64 process_id);
Result GetProgramInfoByProgramId(std::shared_ptr<ProgramInfo> *out, u64 program_id);
};
}
AMS_FSSYSTEM_ENABLE_PIMPL(::ams::fssrv::impl::ProgramRegistryManager);