intermittent ctrl commit: fork() based spawn code which makes nng panic

This commit is contained in:
oxmox 2023-02-19 16:23:29 +01:00
parent fc302969f6
commit df9cf1c14a

View file

@ -15,6 +15,7 @@
#include <signal.h>
#include <spawn.h>
#include <sys/wait.h>
#include <unistd.h>
#include "dp_common.h"
#include "dp_util.hpp"
@ -56,7 +57,7 @@ struct ControllerActions
#define DOOM_EXECUTABLE "dp_doom"
void spawn_doom(ControllerContext &ctx)
void spawn_doom_posix_spawn(ControllerContext &ctx)
{
DoomState ds;
@ -64,7 +65,8 @@ void spawn_doom(ControllerContext &ctx)
// TODO: Close stdin and stdout? Leave them open for now to see the logging
// output.
// FIXME: this doesn't work with SDL :(
// FIXME: SDL is not able to init its sound system in the doomchild. Try to ask
// it if it's already initialized and skip if true.
if (auto err = posix_spawn(&ds.id, DOOM_EXECUTABLE, nullptr, nullptr,
const_cast<char *const *>(argv), nullptr))
{
@ -82,6 +84,45 @@ void spawn_doom(ControllerContext &ctx)
ctx.dooms.emplace_back(ds);
}
// nng does not like this at all and panics (for good reasons)
void spawn_doom_fork(ControllerContext &ctx)
{
auto pid = fork();
if (pid < 0)
{
log_error("Could not spawn doom: %s", strerror(errno));
return;
}
if (pid == 0) // doomchild
{
log_info("doomchild %d cleaning the room", getpid());
ImGui::DestroyContext();
log_info("doomchild %d cleanup done", getpid());
const char *const argv[] = { DOOM_EXECUTABLE, nullptr };
execvp(DOOM_EXECUTABLE, const_cast<char *const *>(argv));
}
else if (pid > 0) // doomparent
{
DoomState ds;
ds.id = pid;
ds.texture = SDL_CreateTexture(ctx.renderer, SDL_PIXELFORMAT_RGB888,
SDL_TEXTUREACCESS_STREAMING, DoomScreenWidth, DoomScreenHeight);
if (!ds.texture)
dp_sdl_fatal("SDL_CreateTexture");
log_info("Spawned new doom, pid=%d", ds.id);
ctx.dooms.emplace_back(ds);
}
}
inline void spawn_doom(ControllerContext &ctx)
{
spawn_doom_posix_spawn(ctx);
}
void end_all_dooms(ControllerContext &ctx)
{
nng_msg *msg = nullptr;