AuroraRuntime/Source/Processes/AuOpen.Unix.cpp

127 lines
3.7 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuOpen.Unix.cpp
Date: 2021-6-12
Author: Reece
***/
#include <RuntimeInternal.hpp>
#include "AuProcesses.hpp"
#include "AuOpen.Unix.hpp"
#include <unistd.h>
#include <Source/IO/FS/FS.hpp>
namespace Aurora::Process
{
void PosixForkResetLocks();
}
namespace Aurora::Processes
{
static void UnixOpenAsyncThread(AuString uri, bool bType)
{
bool bDirExists {};
bool bFileExists {};
bFileExists = AuIOFS::FileExists(uri);
if (!bType)
{
bDirExists = AuIOFS::DirExists(uri);
if (!bFileExists &&
!bDirExists)
{
SysPushErrorGeneric("Exploit attempt? Attempted to open non-existent file/directory. (request: {})", uri);
return;
}
if (bFileExists)
{
if (AuFS::IsFileBlocked(uri))
{
SysPushErrorGeneric("Exploit attempt? Attempted to open untrusted file/directory. (request: {})", uri);
return;
}
}
}
else
{
if (bFileExists)
{
SysPushErrorGeneric("Exploit attempt? Attempted to open existing file/directory via URI ({})", uri);
return;
}
}
if constexpr (AuBuild::kIsXnuDerived)
{
// TODO: MacOS/iOS support
}
else
{
if (fork() == 0)
{
setsid();
AuProcess::PosixForkResetLocks();
auto optStringA = AuProcess::EnvironmentGetOne("container");
auto optStringB = AuProcess::EnvironmentGetOne("AURORA_RUNTIME_USE_GDBUS_BIN_TO_PORTAL");
bool bIsFireJail = optStringA && optStringA.Value() == "firejail";
bool bIsForcedDBUS = optStringB && optStringB.Value() == "YES";
if (bIsFireJail || bIsForcedDBUS)
{
execlp("gdbus",
"gdbus",
"call",
"--session",
"--dest", "org.freedesktop.portal.Desktop",
"--object-path", "/org/freedesktop/portal/desktop",
"--method", bFileExists ? "org.freedesktop.portal.OpenURI.OpenFile" : "org.freedesktop.portal.OpenURI.OpenURI",
"",
uri.c_str(),
"{}",
(char *)nullptr);
}
else
{
if (AuFS::FileExists("/usr/bin/xdg-open"))
{
execl("/usr/bin/xdg-open", "xdg-open", uri.c_str(), (char *)nullptr);
}
else if (AuFS::FileExists("/usr/local/bin/xdg-open"))
{
execl("/usr/local/bin/xdg-open", "xdg-open", uri.c_str(), (char *)nullptr);
}
else
{
execlp("xdg-open", "xdg-open", uri.c_str(), (char *)nullptr);
}
}
exit(0);
}
}
}
static void UnixOpenAsync(const AuString &uri, bool bType)
{
if (uri.empty())
{
return;
}
AuThreads::Spawn(std::bind(&UnixOpenAsyncThread, AuString(uri), bType), true);
}
AUKN_SYM void OpenUri(const AuString &uri)
{
UnixOpenAsync(uri, true);
}
AUKN_SYM void OpenFile(const AuString &file)
{
UnixOpenAsync(file, false);
}
}