Skip to content

Commit

Permalink
Print forked processes and prevent closing the socket
Browse files Browse the repository at this point in the history
  • Loading branch information
clementgallet committed Aug 4, 2020
1 parent ae7bd23 commit 499fd07
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/library/checkpoint/Checkpoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,8 @@ static void readAnArea(SaveState &saved_state, int spmfd, SaveState &parent_stat
static void writeAllAreas(bool base)
{
if (shared_config.savestate_settings & SharedConfig::SS_FORK) {
pid_t pid = fork();
pid_t pid;
NATIVECALL(pid = fork());
if (pid != 0)
return;

Expand Down
1 change: 1 addition & 0 deletions src/library/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace libtas {
SharedConfig shared_config;
GameInfo game_info;
volatile bool is_exiting = false;
volatile bool is_fork = false;
bool skipping_draw = false;
Display* gameDisplays[GAMEDISPLAYNUM] = {};
xcb_connection_t* gameConnections[GAMEDISPLAYNUM] = {};
Expand Down
3 changes: 3 additions & 0 deletions src/library/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ namespace libtas {
/* Indicate if the game is exiting. It helps avoiding some invalid or blocking calls */
extern volatile bool is_exiting;

/* Indicate if this is a forked process. We should not use socket on forked processes */
extern volatile bool is_fork;

/* Do we skip all rendering functions for the current frame */
extern bool skipping_draw;

Expand Down
12 changes: 8 additions & 4 deletions src/library/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ void debuglogstdio(LogCategoryFlag lcf, const char* fmt, ...)
snprintf(s + size, maxsize-size-1, "[libTAS f:%" PRIu64 "] ", framecount);
size = strlen(s);

pid_t tid = ThreadManager::getThreadTid();
if (ThreadManager::isMainThread())
snprintf(s + size, maxsize-size-1, "Thread %d (main) ", tid);
pid_t tid;
if (is_fork)
/* For forked processes, the thread manager have wrong pid values (those of parent process) */
NATIVECALL(tid = getpid());
else
snprintf(s + size, maxsize-size-1, "Thread %d ", tid);
tid = ThreadManager::getThreadTid();

snprintf(s + size, maxsize-size-1, "Thread %d %s ", tid, is_fork?"(fork)":(ThreadManager::isMainThread()?"(main)":""));

size = strlen(s);

if (isTerm) {
Expand Down
8 changes: 5 additions & 3 deletions src/library/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,12 @@ void __attribute__((constructor)) init(void)
void __attribute__((destructor)) term(void)
{
if (is_inited) {
ThreadManager::deallocateThreads();
sendMessage(MSGB_QUIT);
closeSocket();
if (!is_fork) {
sendMessage(MSGB_QUIT);
closeSocket();
}
debuglog(LCF_SOCKET, "Exiting.");
ThreadManager::deallocateThreads();
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/library/systemwrappers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
namespace libtas {

DEFINE_ORIG_POINTER(getpid);
DEFINE_ORIG_POINTER(fork);

/* Override */ pid_t getpid (void) throw()
{
Expand Down Expand Up @@ -54,4 +55,21 @@ DEFINE_ORIG_POINTER(getpid);
return pid;
}

/* Override */ pid_t fork(void) __THROWNL
{
LINK_NAMESPACE_GLOBAL(fork);
pid_t pid = orig::fork();

if (GlobalState::isNative()) {
return pid;
}

DEBUGLOGCALL(LCF_SYSTEM);

if (pid == 0) {
is_fork = true;
}
return pid;
}

}
7 changes: 6 additions & 1 deletion src/library/systemwrappers.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
namespace libtas {

/* Get the process ID of the calling process. */
OVERRIDE pid_t getpid (void) throw();
OVERRIDE pid_t getpid (void) __THROWNL;

/* Clone the calling process, creating an exact copy.
Return -1 for errors, 0 to the new process,
and the process ID of the new process to the old process. */
OVERRIDE pid_t fork(void) __THROWNL;

}

Expand Down

0 comments on commit 499fd07

Please sign in to comment.