Skip to content

Commit

Permalink
Update for new Plutonium, huge code cleanup/rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
XorTroll committed Feb 6, 2022
1 parent 033afd2 commit 4d915bc
Show file tree
Hide file tree
Showing 75 changed files with 5,815 additions and 9,314 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
build/
.vscode/
Out/
lib/
SdOut/
*.elf
*.npdm
Expand Down
2 changes: 1 addition & 1 deletion Plutonium
Submodule Plutonium updated 556 files
24 changes: 12 additions & 12 deletions uDaemon/include/ecs/ecs_ExternalContent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
#pragma once
#include <stratosphere.hpp>
#include <am/am_Application.hpp>
#include <am/am_LibraryApplet.hpp>

namespace ecs {

Result RegisterExternalContent(u64 program_id, const std::string &exefs_path);
Result LaunchApplet(u64 program_id, u32 la_version, void *args, size_t args_size);
Result LaunchSystemProcess(u64 program_id, const std::string &argv_str);
Result RegisterExternalContent(const u64 program_id, const std::string &exefs_path);
Result LaunchSystemProcess(const u64 program_id, const std::string &argv_str);

inline Result RegisterLaunchAsApplet(u64 program_id, u32 la_version, const std::string &exefs_path, void *args, size_t args_size) {
R_TRY(RegisterExternalContent(program_id, exefs_path));
R_TRY(LaunchApplet(program_id, la_version, args, args_size));
inline Result RegisterLaunchAsApplet(const u64 program_id, const u32 la_version, const std::string &exefs_path, const void *args, const size_t args_size) {
UL_RC_TRY(RegisterExternalContent(program_id, exefs_path));
UL_RC_TRY(am::LibraryAppletStart(am::LibraryAppletGetAppletIdForProgramId(program_id), la_version, args, args_size));
return ResultSuccess;
}

inline Result RegisterLaunchAsApplication(u64 program_id, const std::string &exefs_path, void *args, size_t args_size, AccountUid uid) {
R_TRY(RegisterExternalContent(program_id, exefs_path));
R_TRY(am::ApplicationStart(program_id, false, uid, args, args_size));
inline Result RegisterLaunchAsApplication(const u64 program_id, const std::string &exefs_path, const void *args, const size_t args_size, const AccountUid uid) {
UL_RC_TRY(RegisterExternalContent(program_id, exefs_path));
UL_RC_TRY(am::ApplicationStart(program_id, false, uid, args, args_size));
return ResultSuccess;
}

inline Result RegisterLaunchAsSystemProcess(u64 program_id, const std::string &exefs_path, const std::string &argv_str) {
R_TRY(RegisterExternalContent(program_id, exefs_path));
R_TRY(LaunchSystemProcess(program_id, argv_str));
inline Result RegisterLaunchAsSystemProcess(const u64 program_id, const std::string &exefs_path, const std::string &argv_str) {
UL_RC_TRY(RegisterExternalContent(program_id, exefs_path));
UL_RC_TRY(LaunchSystemProcess(program_id, argv_str));
return ResultSuccess;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <stratosphere.hpp>
#include <dmi/dmi_DaemonMenuInteraction.hpp>
#include <functional>

namespace ipc {

Expand Down Expand Up @@ -41,12 +40,13 @@ namespace ipc {
};

Result Initialize();
ServerManager &GetGlobalManager();
Allocator &GetServerAllocator();
Allocator &GetManagerAllocator();

ams::Result RegisterSession(const ams::os::NativeHandle session_handle, ams::sf::cmif::ServiceObjectHolder &&obj);

template<typename Impl, typename T, typename ...Args>
inline auto MakeShared(Args ...args) {
return ObjectFactory::CreateSharedEmplaced<Impl, T>(std::addressof(GetServerAllocator()), args...);
return ObjectFactory::CreateSharedEmplaced<Impl, T>(std::addressof(GetManagerAllocator()), args...);
}

}
173 changes: 86 additions & 87 deletions uDaemon/source/Main.cpp

Large diffs are not rendered by default.

33 changes: 11 additions & 22 deletions uDaemon/source/ecs/ecs_ExternalContent.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#include <ecs/ecs_ExternalContent.hpp>
#include <ipc/ipc_GlobalManager.hpp>
#include <am/am_LibraryApplet.hpp>
#include <am/am_HomeMenu.hpp>
#include <ipc/ipc_Manager.hpp>
#include <stratosphere/fssrv/fssrv_interface_adapters.hpp>

namespace {

inline Result ldrShellAtmosphereRegisterExternalCode(u64 app_id, Handle *out_h) {
inline Result ldrShellAtmosphereRegisterExternalCode(const u64 app_id, Handle *out_h) {
return serviceDispatchIn(ldrShellGetServiceSession(), 65000, app_id,
.out_handle_attrs = { SfOutHandleAttr_HipcMove },
.out_handles = out_h,
Expand All @@ -17,38 +15,29 @@ namespace {

namespace ecs {

Result RegisterExternalContent(u64 app_id, const std::string &exefs_path) {
Result RegisterExternalContent(const u64 app_id, const std::string &exefs_path) {
auto move_h = INVALID_HANDLE;
R_TRY(ldrShellAtmosphereRegisterExternalCode(app_id, &move_h));
UL_RC_TRY(ldrShellAtmosphereRegisterExternalCode(app_id, &move_h));

FsFileSystem sd_fs;
R_TRY(fsOpenSdCardFileSystem(&sd_fs));
UL_RC_TRY(fsOpenSdCardFileSystem(&sd_fs));
std::unique_ptr<ams::fs::fsa::IFileSystem> remote_sd_fs = std::make_unique<ams::fs::RemoteFileSystem>(sd_fs);
auto subdir_fs = std::make_shared<ams::fssystem::SubDirectoryFileSystem>(std::move(remote_sd_fs), exefs_path.c_str());
auto sd_ifs_ipc = ipc::MakeShared<ams::fssrv::sf::IFileSystem, ams::fssrv::impl::FileSystemInterfaceAdapter>(std::move(subdir_fs), false);

R_TRY(ipc::GetGlobalManager().RegisterSession(move_h, ams::sf::cmif::ServiceObjectHolder(std::move(sd_ifs_ipc))).GetValue());

return ResultSuccess;
}

Result LaunchApplet(u64 program_id, u32 la_version, void *args, size_t args_size) {
auto appletid = am::LibraryAppletGetAppletIdForProgramId(program_id);
if(appletid == 0) {
return 0xDEAD;
}
R_TRY(am::LibraryAppletStart(appletid, la_version, args, args_size));
UL_RC_TRY(ipc::RegisterSession(move_h, ams::sf::cmif::ServiceObjectHolder(std::move(sd_ifs_ipc))));
return ResultSuccess;
}

Result LaunchSystemProcess(u64 program_id, const std::string &argv_str) {
R_TRY(ldrShellSetProgramArguments(program_id, argv_str.c_str(), argv_str.length()));
Result LaunchSystemProcess(const u64 program_id, const std::string &argv_str) {
UL_RC_TRY(ldrShellSetProgramArguments(program_id, argv_str.c_str(), argv_str.length()));
NcmProgramLocation loc = {
.program_id = program_id,
.storageID = NcmStorageId_BuiltInSystem,
.storageID = NcmStorageId_BuiltInSystem
};

u64 pid;
R_TRY(pmshellLaunchProgram(0, &loc, &pid));
UL_RC_TRY(pmshellLaunchProgram(0, &loc, &pid));
return ResultSuccess;
}

Expand Down
58 changes: 0 additions & 58 deletions uDaemon/source/ipc/ipc_GlobalManager.cpp

This file was deleted.

8 changes: 4 additions & 4 deletions uDaemon/source/ipc/ipc_IPrivateService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace ipc {
ams::Result PrivateService::Initialize(const ams::sf::ClientProcessId &client_pid) {
if(!this->initialized) {
u64 program_id = 0;
R_TRY(pminfoGetProgramId(&program_id, client_pid.process_id.value));
const auto last_menu_program_id = am::LibraryAppletGetProgramIdForAppletId(am::LibraryAppletGetMenuAppletId());
UL_RC_TRY(pminfoGetProgramId(&program_id, client_pid.process_id.value));

const auto last_menu_program_id = am::LibraryAppletGetMenuProgramId();
// If Menu hasn't been launched it's program ID will be 0 (invalid), thus a single (program_id != last_menu_program_id) check isn't enough
// If any of the IDs is invalid, something unexpected is happening...
if((last_menu_program_id == 0) || (program_id == 0) || (program_id != last_menu_program_id)) {
return ipc::ResultInvalidProcess;
}

this->initialized = true;
}

Expand Down
63 changes: 63 additions & 0 deletions uDaemon/source/ipc/ipc_Manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <ipc/ipc_Manager.hpp>
#include <ipc/ipc_IPrivateService.hpp>

namespace {

ipc::ServerManager g_Manager;

constexpr size_t IpcManagerThreadStackSize = 32_KB;
ams::os::ThreadType g_ManagerThread;
alignas(ams::os::ThreadStackAlignment) u8 g_ManagerThreadStack[IpcManagerThreadStackSize];

ams::os::Mutex g_ManagerAllocatorLock(false);

constexpr size_t ServerAllocatorHeapSize = 32_KB;
alignas(0x40) constinit u8 g_ManagerAllocatorHeap[ServerAllocatorHeapSize];
ams::lmem::HeapHandle g_ManagerAllocatorHeapHandle;
ipc::Allocator g_ManagerAllocator;

void IpcManagerThread(void*) {
ams::os::SetThreadNamePointer(ams::os::GetCurrentThread(), "ul.daemon.IpcManager");

UL_RC_ASSERT(g_Manager.RegisterServer(ipc::Port_PrivateService, ipc::PrivateServiceName, ipc::MaxPrivateSessions));
// UL_RC_ASSERT(g_Manager.RegisterServer<ipc::IPublicService>(PublicServiceName, MaxPublicSessions));

g_Manager.LoopProcess();
}

void InitializeHeap() {
g_ManagerAllocatorHeapHandle = ams::lmem::CreateExpHeap(g_ManagerAllocatorHeap, sizeof(g_ManagerAllocatorHeap), ams::lmem::CreateOption_None);
g_ManagerAllocator.Attach(g_ManagerAllocatorHeapHandle);
}

}

namespace ipc {

ams::Result ServerManager::OnNeedsToAccept(int port_index, Server *server) {
switch(port_index) {
case Port_PrivateService: {
return this->AcceptImpl(server, MakeShared<ams::sf::ul::IPrivateService, ipc::PrivateService>());
}
AMS_UNREACHABLE_DEFAULT_CASE();
}
}

Result Initialize() {
InitializeHeap();
UL_RC_TRY(ams::os::CreateThread(&g_ManagerThread, &IpcManagerThread, nullptr, g_ManagerThreadStack, sizeof(g_ManagerThreadStack), 10));
ams::os::StartThread(&g_ManagerThread);

return ResultSuccess;
}

Allocator &GetManagerAllocator() {
std::scoped_lock lk(g_ManagerAllocatorLock);
return g_ManagerAllocator;
}

ams::Result RegisterSession(const ams::os::NativeHandle session_handle, ams::sf::cmif::ServiceObjectHolder &&obj) {
return g_Manager.RegisterSession(session_handle, std::move(obj));
}

}
Loading

0 comments on commit 4d915bc

Please sign in to comment.