AuroraRuntime/Source/Process/Paths.cpp
Reece e5e36bd887 Large Commit
[*] Fix deadlock in the async subsystem (NoLockShutdown vs Shutdown in exception handler)
[+] Added ProccessMap NT variant
[+] Added ToolHelp image profiling
[*] Improved exception awareness
[*] Delegated SpawnThread to isolated TU, ready for reuse for RunAs and XNU Open - now with horrible evil alloc that could fail
[+] Added header for future api 'UtilRun'
[*] Improve NT core detection
[*] Changed small affinity bitmap to AuUInt64 instead of AuUInt32
[+] Added data structure to hold cpuids/affinity masks
[+] Implemented logger sinks
[+] Implemented logger glue logic
[*] Began migrating older loggers to sink-based default devices
[*] Minor refactors
[*] Improved internal exception discarding, not yet nothrow capable
[*] Minor create directory fix
2022-01-24 18:43:53 +00:00

247 lines
5.5 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Paths.cpp
Date: 2021-7-14
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "Paths.hpp"
#if defined(AURORA_IS_POSIX_DERIVED)
#include <stdlib.h>
#include <unistd.h>
#define _USE_GET_CWD
#endif
namespace Aurora::Process
{
AUKN_SYM bool GetWorkingDirectory(AuString &path)
{
if (!AuTryResize(path, 0x4000))
{
return false;
}
#if defined(AURORA_IS_MODERNNT_DERIVED)
std::wstring eh;
eh.resize(path.size());
auto length = ::GetCurrentDirectoryW(eh.size(), eh.data());
if (length == 0)
{
return false;
}
path = Locale::ConvertFromWChar(eh.data(), length);
return true;
#elif defined(AURORA_PLATFORM_ANDROID)
// TODO: consider pulling internal storage package directory
return false;
#elif defined(_USE_GET_CWD)
if (getcwd(path.data(), path.size()) == nullptr)
{
return false;
}
path.resize(strlen(path.data()));
return true;
#endif
return false;
}
static bool GetModulePathSlow(AuString &path, char &splitter)
{
path.clear();
splitter = 0;
#if defined(AURORA_IS_MODERNNT_DERIVED)
wchar_t chungus[0x4000];
auto len = GetModuleFileNameW(
NULL,
chungus,
AuArraySize(chungus)
);
if (!len)
{
return false;
}
path = Aurora::Locale::ConvertFromWChar(chungus, len);
splitter = '\\';
return true;
#elif defined(AURORA_IS_XNU_DERIVED)
CFURLRef bundleURL = CFBundleCopyExecutableURL(CFBundleGetMainBundle());
CFStringRef pathRef = CFURLCopyFileSystemPath(bundleURL, kCFURLPOSIXPathStyle);
auto utf16length = CFStringGetLength(pathRef);
auto maxUtf8len = CFStringGetMaximumSizeForEncoding(utf16length, kCFStringEncodingUTF8);
path.resize(maxUtf8len);
if (!CFStringGetCString(pathRef, path.data(), maxUtf8len, kCFStringEncodingUTF8))
{
path.clear();
return false;
}
path.resize(std::strlen(path.c_str()));
splitter = '/';
#elif defined(AURORA_PLATFORM_ANDROID)
// SELinux gets in the way in some instances
// There is no reason we should be searching for the jre binary
// I'm also not convined pulling a cached apk path is the best thing to do here
// Why should we even bother pulling /system/bin/app_process?
return false;
#elif defined(AURORA_IS_POSIX_DERIVED)
static const AuList<AuString> procFsPaths = {
"/proc/self/exe",
"/proc/self/file",
"/proc/curproc/exe",
"/proc/curproc/file"
};
for (const auto &name : procFsPaths)
{
if (AuIOFS::ReadString(name, path))
{
splitter = '/';
return true;
}
}
return false;
#endif
}
static bool GetModulePath(AuString &module, AuString &partialPath, AuString &fullPath)
{
static AuString cachedModule, cachedPartialPath, cachedFullPath;
static bool init = false;
static AuThreadPrimitives::SpinLock spinlock;
char spitter;
AU_TRY_LOCK_GUARD_RET_DEF(spinlock);
if (AuExchange(init, true))
{
if (!cachedModule.size())
{
return false;
}
goto returnCached;
}
if (!GetModulePathSlow(cachedFullPath, spitter))
{
return false;
}
// this scope is required for c++20, bleh
{
auto indexA = cachedFullPath.find_last_of(spitter);
if (indexA == AuString::npos)
{
return false;
}
cachedModule = cachedFullPath.substr(indexA + 1);
cachedPartialPath = cachedFullPath.substr(0, indexA);
}
returnCached:
module = cachedModule;
fullPath = cachedFullPath;
partialPath = cachedPartialPath;
return true;
}
AUKN_SYM bool GetProcName(AuString &executable)
{
AuString module, partial, full;
try
{
if (!GetModulePath(module, partial, full))
{
return false;
}
executable = module;
}
catch (...)
{
return false;
}
return true;
}
AUKN_SYM bool GetProcPath(AuString &path)
{
AuString module, partial, full;
try
{
if (!GetModulePath(module, partial, full))
{
return false;
}
path = partial;
if (path.empty())
{
return GetWorkingDirectory(path);
}
}
catch (...)
{
return false;
}
return true;
}
AUKN_SYM bool GetProcFullPath(AuString &path)
{
AuString module, partial, full;
try
{
if (!GetModulePath(module, partial, full))
{
return false;
}
path = full;
}
catch (...)
{
return false;
}
return true;
}
}