mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-13 15:01:49 +00:00
add platform::new_engine_instance subProcess parameter
This commit is contained in:
parent
8ade2ab5da
commit
513abd332c
3 changed files with 77 additions and 3 deletions
|
|
@ -365,7 +365,8 @@ static int l_start_debug_instance(lua::State* L) {
|
||||||
|
|
||||||
platform::new_engine_instance(
|
platform::new_engine_instance(
|
||||||
std::move(args),
|
std::move(args),
|
||||||
outputPath.empty() ? "" : io::resolve(std::string(outputPath))
|
outputPath.empty() ? "" : io::resolve(std::string(outputPath)),
|
||||||
|
true // debug instance may be detached
|
||||||
);
|
);
|
||||||
return lua::pushinteger(L, port);
|
return lua::pushinteger(L, port);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,11 @@
|
||||||
#pragma comment(lib, "winmm.lib")
|
#pragma comment(lib, "winmm.lib")
|
||||||
#else
|
#else
|
||||||
#include <sys/poll.h>
|
#include <sys/poll.h>
|
||||||
|
#include <sys/prctl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <signal.h>
|
||||||
|
#include <stdio.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace platform::internal {
|
namespace platform::internal {
|
||||||
|
|
@ -216,7 +220,9 @@ std::filesystem::path platform::get_executable_path() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void platform::new_engine_instance(
|
void platform::new_engine_instance(
|
||||||
const std::vector<std::string>& args, std::filesystem::path outputFile
|
const std::vector<std::string>& args,
|
||||||
|
std::filesystem::path outputFile,
|
||||||
|
bool subProcess
|
||||||
) {
|
) {
|
||||||
auto executable = get_executable_path();
|
auto executable = get_executable_path();
|
||||||
|
|
||||||
|
|
@ -285,6 +291,37 @@ void platform::new_engine_instance(
|
||||||
&si,
|
&si,
|
||||||
&pi
|
&pi
|
||||||
);
|
);
|
||||||
|
if (subProcess) {
|
||||||
|
HANDLE job = CreateJobObjectW(nullptr, nullptr);
|
||||||
|
|
||||||
|
JOBOBJECT_EXTENDED_LIMIT_INFORMATION info{};
|
||||||
|
info.BasicLimitInformation.LimitFlags =
|
||||||
|
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!SetInformationJobObject(
|
||||||
|
job,
|
||||||
|
JobObjectExtendedLimitInformation,
|
||||||
|
&info,
|
||||||
|
sizeof(info))) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"SetInformationJobObject failed with code: " +
|
||||||
|
std::to_string(GetLastError())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (!AssignProcessToJobObject(job, pi.hProcess)) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"SetInformationJobObject failed with code: " +
|
||||||
|
std::to_string(GetLastError())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} catch (const std::exception& err) {
|
||||||
|
CloseHandle(job);
|
||||||
|
CloseHandle(pi.hProcess);
|
||||||
|
CloseHandle(pi.hThread);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
if (success) {
|
if (success) {
|
||||||
CloseHandle(pi.hProcess);
|
CloseHandle(pi.hProcess);
|
||||||
CloseHandle(pi.hThread);
|
CloseHandle(pi.hThread);
|
||||||
|
|
@ -295,6 +332,40 @@ void platform::new_engine_instance(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
if (subProcess) {
|
||||||
|
pid_t pid = fork();
|
||||||
|
|
||||||
|
int fd = open(
|
||||||
|
outputFile.string().c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644
|
||||||
|
);
|
||||||
|
|
||||||
|
if (pid == 0) {
|
||||||
|
prctl(PR_SET_PDEATHSIG, SIGTERM);
|
||||||
|
|
||||||
|
if (getppid() == 1) {
|
||||||
|
_exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
dup2(fd, STDOUT_FILENO);
|
||||||
|
dup2(fd, STDERR_FILENO);
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
std::vector<const char*> argv(args.size() + 2);
|
||||||
|
argv[0] = const_cast<char*>(executable.c_str());
|
||||||
|
for (int i = 0; i < args.size(); i++) {
|
||||||
|
argv[i + 1] = args[i].c_str();
|
||||||
|
}
|
||||||
|
argv[args.size() + 1] = nullptr;
|
||||||
|
|
||||||
|
execvp(executable.c_str(), const_cast<char**>(argv.data()));
|
||||||
|
|
||||||
|
_exit(127);
|
||||||
|
} else {
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << executable;
|
ss << executable;
|
||||||
for (int i = 0; i < args.size(); i++) {
|
for (int i = 0; i < args.size(); i++) {
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,9 @@ namespace platform {
|
||||||
std::filesystem::path get_executable_path();
|
std::filesystem::path get_executable_path();
|
||||||
/// @brief Run a separate engine instance with specified arguments
|
/// @brief Run a separate engine instance with specified arguments
|
||||||
void new_engine_instance(
|
void new_engine_instance(
|
||||||
const std::vector<std::string>& args, std::filesystem::path outputFile
|
const std::vector<std::string>& args,
|
||||||
|
std::filesystem::path outputFile,
|
||||||
|
bool subProcess
|
||||||
);
|
);
|
||||||
/// @brief Open URL in web browser
|
/// @brief Open URL in web browser
|
||||||
bool open_url(const std::string& url);
|
bool open_url(const std::string& url);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue