[+] chroot after fork. also, removed todos about clone (wont support). the old unix behaviour works fine

This commit is contained in:
Reece Wilson 2022-08-10 16:04:41 +01:00
parent eb144f7377
commit b987384d58
2 changed files with 59 additions and 34 deletions

View File

@ -17,11 +17,14 @@
#include <sys/wait.h>
#include <Source/Threading/Primitives/Semaphore.Unix.hpp>
#include <setjmp.h>
#if !defined(CLONE_CLEAR_SIGHAND)
#define CLONE_CLEAR_SIGHAND 0x100000000ULL
#endif
namespace Aurora::Processes
{
struct ProcessImpl;
static AuThreadPrimitives::RWLockUnique_t gRWLock;
static AuHashMap<pid_t, ProcessImpl *> gPidLookupMap;
@ -43,6 +46,12 @@ namespace Aurora::Processes
ProcessImpl::ProcessImpl(const StartupParmaters &params) : startup_(params)
{
AuIOFS::NormalizePath(this->startup_.process, this->startup_.process);
if (this->startup_.workingDirectory)
{
AuString a;
AuIOFS::NormalizePath(a, this->startup_.workingDirectory.value());
this->startup_.workingDirectory = a;
}
this->startup_.args.insert(startup_.args.begin(), startup_.process);
@ -344,6 +353,51 @@ namespace Aurora::Processes
return this->fsErrorStream_ ? this->fsErrorStream_->NewTransaction() : AuSPtr<AuIO::IAsyncTransaction> {};
}
void ProcessImpl::ForkMain()
{
if (this->startup_.fwdIn != EStreamForward::eCurrentProcess)
{
::dup2(pipeStdIn_[0], STDIN_FILENO);
::close(pipeStdIn_[0]);
::close(pipeStdIn_[1]);
}
if (this->startup_.fwdErr != EStreamForward::eCurrentProcess)
{
::dup2(pipeStdErr_[1], STDERR_FILENO);
::close(pipeStdIn_[0]);
::close(pipeStdIn_[1]);
}
if (this->startup_.fwdOut != EStreamForward::eCurrentProcess)
{
::dup2(pipeStdOut_[1], STDOUT_FILENO);
::close(pipeStdIn_[0]);
::close(pipeStdIn_[1]);
}
{
::setsid();
}
#if defined(AURORA_IS_XNU_DERIVED)
if (this->startup_.workingDirectory)
{
::pthread_chdir_np(this->startup_.workingDirectory.value().c_str());
}
#else
if (this->startup_.workingDirectory)
{
::chroot(this->startup_.workingDirectory.value().c_str());
}
#endif
::execv(this->startup_.process.c_str(), (char * const *)this->cargs_.data()); // https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
SysPushErrorGen("execv didn't overwrite the process map. Launch: {} ({})", this->startup_.process, this->debug_);
}
bool ProcessImpl::Start()
{
this->exitCode_ = 0x10110100;
@ -357,42 +411,11 @@ namespace Aurora::Processes
pid_t pid;
{
// TODO: clone without CLONE_FS and CLONE_THREAD (if non-sid path)
pid = ::fork();
if (pid == 0)
{
if (this->startup_.fwdIn != EStreamForward::eCurrentProcess)
{
::dup2(pipeStdIn_[0], STDIN_FILENO);
::close(pipeStdIn_[0]);
::close(pipeStdIn_[1]);
}
if (this->startup_.fwdErr != EStreamForward::eCurrentProcess)
{
::dup2(pipeStdErr_[1], STDERR_FILENO);
::close(pipeStdErr_[0]);
::close(pipeStdErr_[1]);
}
if (this->startup_.fwdOut != EStreamForward::eCurrentProcess)
{
::dup2(pipeStdOut_[1], STDOUT_FILENO);
::close(pipeStdOut_[0]);
::close(pipeStdOut_[1]);
}
if (this->type_ != ESpawnType::eSpawnChildProcessWorker)
{
::setsid();
}
// TODO: pthread_chdir_np on OSX
::execv(this->startup_.process.c_str(), (char * const *)this->cargs_.data()); // https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
SysPushErrorGen("execv didn't overwrite the process map. Launch: {} ({})", this->startup_.process, this->debug_);
this->ForkMain();
return false;
}
else if (pid > 0)

View File

@ -52,6 +52,8 @@ namespace Aurora::Processes
bool Init();
void ByeLol(AuSInt code);
void ForkMain();
private:
int pipeStdOut_[2]{};