[*] Update UNIX process spawning

This commit is contained in:
Reece Wilson 2022-04-13 15:43:38 +01:00
parent 9eecdcb0a9
commit f6183e672c

View File

@ -16,7 +16,7 @@
#include <sys/types.h>
#include <sys/wait.h>
//#include <Source/Threading/Primitives/Semaphore.Unix.hpp>
#include <Source/Threading/Primitives/Semaphore.Unix.hpp>
namespace Aurora::Processes
{
@ -42,6 +42,8 @@ namespace Aurora::Processes
{
this->debug_.resize(this->debug_.size() - 1);
}
this->type_ = this->startup_.type;
}
ProcessImpl::~ProcessImpl()
@ -133,6 +135,7 @@ namespace Aurora::Processes
auto handle = stream == EStandardHandle::eStdError ? this->pipeStdErr_[0] : this->pipeStdOut_[0];
if (handle < 0)
{
SysPushErrorUninitialized();
return false;
}
@ -153,9 +156,19 @@ namespace Aurora::Processes
fcntl(handle, F_SETFL, control);
}
auto tmp = read(handle, destination.ptr, destination.length);
if (tmp < 0)
int tmp;
do
{
tmp = read(handle, destination.ptr, destination.length);
} while ((tmp == -1 && errno == EINTR));
if (tmp <= 0)
{
if (tmp != 0)
{
SysPushErrorMem("couldn't read error : %i\n", errno);
}
return false;
}
@ -178,24 +191,27 @@ namespace Aurora::Processes
{
if (this->startup_.fwdOut)
{
if (!pipe(this->pipeStdOut_))
if (pipe(this->pipeStdOut_))
{
SysPushErrorMem();
return false;
}
}
if (this->startup_.fwdErr)
{
if (!pipe(this->pipeStdErr_))
if (pipe(this->pipeStdErr_))
{
SysPushErrorMem();
return false;
}
}
if (this->startup_.fwdIn)
{
if (!pipe(this->pipeStdIn_))
if (pipe(this->pipeStdIn_))
{
SysPushErrorMem();
return false;
}
}
@ -217,27 +233,29 @@ namespace Aurora::Processes
pid_t pid;
{
//Threading::Primitives::Semaphore semaphore;
pid = fork();
if (pid == 0)
{
if (this->startup_.fwdIn)
{
dup2(pipeStdIn_[0], STDIN_FILENO);
close(AuExchange(pipeStdIn_[0], 0));
::close(pipeStdIn_[0]);
::close(pipeStdIn_[1]);
}
if (this->startup_.fwdErr)
{
dup2(pipeStdErr_[1], STDERR_FILENO);
close(AuExchange(pipeStdErr_[1], 0));
::close(pipeStdErr_[0]);
::close(pipeStdErr_[1]);
}
if (this->startup_.fwdOut)
{
dup2(pipeStdOut_[1], STDOUT_FILENO);
close(AuExchange(pipeStdOut_[1], 0));
::close(pipeStdOut_[0]);
::close(pipeStdOut_[1]);
}
if (this->type_ != ESpawnType::eSpawnChildProcessWorker)
@ -245,28 +263,40 @@ namespace Aurora::Processes
setsid();
}
{
AU_LOCK_GUARD(gRWLock->AsWritable());
AuTryInsert(gPidLookupMap, gettid(), this);
}
this->alive_ = true;
//semaphore.Unlock();
execv(this->startup_.process.c_str(), (char * const *)this->cargs_.data()); // https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
execvp(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, given {} ({})", this->startup_.process, this->debug_);
return false;
}
else if (pid < 0)
else if (pid > 0)
{
return false;
if (pipeStdOut_[1])
{
::close(AuExchange(pipeStdOut_[1], 0));
}
if (pipeStdErr_[1])
{
::close(AuExchange(pipeStdErr_[1], 0));
}
if (pipeStdIn_[0])
{
::close(AuExchange(pipeStdIn_[0], 0));
}
this->pidt_ = pid;
this->alive_ = true;
{
AU_LOCK_GUARD(gRWLock->AsWritable());
SysAssert(AuTryInsert(gPidLookupMap, pid, this));
}
return true;
}
else
{
this->pidt_ = pid;
//semaphore.Lock();
return false;
}
}
@ -279,11 +309,13 @@ namespace Aurora::Processes
if (!ret)
{
SysPushErrorMem();
return {};
}
if (!ret->Init())
{
SysPushErrorNested();
delete ret;
return {};
}
@ -301,7 +333,10 @@ namespace Aurora::Processes
AU_LOCK_GUARD(gRWLock->AsReadable());
auto handler = gPidLookupMap.find(pid);
if (handler == gPidLookupMap.end()) return;
if (handler == gPidLookupMap.end())
{
return;
}
{
auto process = *handler;