Core/Process: Improve Posix implementation

- Handle chdir errors
- Calls _exit if execve failed
- Better format the code
This commit is contained in:
SirLynix 2024-01-23 09:37:01 +01:00 committed by Jérôme Leclercq
parent ac1422c221
commit 8ee128b475
1 changed files with 48 additions and 27 deletions

View File

@ -38,6 +38,20 @@ namespace Nz::PlatformImpl
// Child process
::setsid();
if (!workingDirectory.empty())
{
if (::chdir(workingDirectory.c_str()) != 0)
{
PidOrErr err;
err.pid = grandChildPid;
pipe.Write(&err, sizeof(err));
// Early exit
std::exit(1);
}
}
pid_t grandChildPid = ::vfork();
if (grandChildPid == 0)
{
@ -53,17 +67,18 @@ namespace Nz::PlatformImpl
char* envs[] = { nullptr };
if (!workingDirectory.empty())
::chdir(workingDirectory.c_str());
::execve(program.c_str(), argv.data(), envs);
// If we get here, execve failed
// Remember we share the memory of our parent (vfork) so we need to exit using _exit() to avoid calling the parent exit handler
if (::execve(program.c_str(), argv.data(), envs) == -1)
{
PidOrErr err;
err.pid = -1;
err.err = errno;
pipe.Write(&err, sizeof(err));
}
_exit(1);
}
else if (grandChildPid != -1)
{
@ -71,6 +86,9 @@ namespace Nz::PlatformImpl
err.pid = grandChildPid;
pipe.Write(&err, sizeof(err));
// Exits the child process, at this point the grand-child should have started
std::exit(0);
}
else
{
@ -79,12 +97,14 @@ namespace Nz::PlatformImpl
err.err = errno;
pipe.Write(&err, sizeof(err));
std::exit(1);
}
// Exits the child process, at this point the grand-child should have started
std::exit(0);
__builtin_unreachable(); // TODO: NAZARA_UNREACHABLE()
}
else
{
// Parent process
// Wait for and reap the child
@ -103,4 +123,5 @@ namespace Nz::PlatformImpl
return SafeCast<Pid>(pidOrErr.pid);
}
}
}