AuroraRuntime/Source/Processes/AuOpen.Unix.cpp

125 lines
3.7 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
2022-11-17 07:46:07 +00:00
File: AuOpen.Unix.cpp
2021-06-27 21:25:29 +00:00
Date: 2021-6-12
Author: Reece
***/
#include <RuntimeInternal.hpp>
2022-11-17 07:46:07 +00:00
#include "AuProcesses.hpp"
#include "AuOpen.Unix.hpp"
2021-06-27 21:25:29 +00:00
#include <unistd.h>
#include <Source/IO/FS/FS.hpp>
2021-06-27 21:25:29 +00:00
namespace Aurora::Processes
{
2024-01-20 20:27:25 +00:00
static void UnixOpenAsyncThread(AuString uri, bool bType)
2021-06-27 21:25:29 +00:00
{
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;
}
}
2024-01-21 18:30:14 +00:00
if constexpr (AuBuild::kIsXnuDerived)
2021-06-27 21:25:29 +00:00
{
2024-01-21 18:30:14 +00:00
// TODO: MacOS/iOS support
}
else
{
if (fork() == 0)
{
setsid();
PosixDoForkHooks();
PosixShutup();
2024-04-14 18:48:40 +00:00
PosixFDYeetus();
2024-01-21 18:30:14 +00:00
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);
}
2021-06-27 21:25:29 +00:00
}
}
static void UnixOpenAsync(const AuString &uri, bool bType)
2021-06-27 21:25:29 +00:00
{
if (uri.empty())
2023-09-09 22:57:01 +00:00
{
return;
}
2024-01-20 20:27:25 +00:00
AuThreads::Spawn(std::bind(&UnixOpenAsyncThread, AuString(uri), bType), true);
}
2023-09-09 22:57:01 +00:00
AUKN_SYM void OpenUri(const AuString &uri)
{
UnixOpenAsync(uri, true);
}
AUKN_SYM void OpenFile(const AuString &file)
{
UnixOpenAsync(file, false);
2021-06-27 21:25:29 +00:00
}
}