From 499fd07a44d35aabe7ff02275bd9907bab93893a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Gallet?= Date: Tue, 4 Aug 2020 18:41:03 +0200 Subject: [PATCH] Print forked processes and prevent closing the socket --- src/library/checkpoint/Checkpoint.cpp | 3 ++- src/library/global.cpp | 1 + src/library/global.h | 3 +++ src/library/logging.cpp | 12 ++++++++---- src/library/main.cpp | 8 +++++--- src/library/systemwrappers.cpp | 18 ++++++++++++++++++ src/library/systemwrappers.h | 7 ++++++- 7 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/library/checkpoint/Checkpoint.cpp b/src/library/checkpoint/Checkpoint.cpp index cdaaf13b..1b04e501 100644 --- a/src/library/checkpoint/Checkpoint.cpp +++ b/src/library/checkpoint/Checkpoint.cpp @@ -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; diff --git a/src/library/global.cpp b/src/library/global.cpp index 9e990520..05ea5109 100644 --- a/src/library/global.cpp +++ b/src/library/global.cpp @@ -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] = {}; diff --git a/src/library/global.h b/src/library/global.h index 1add8df7..8e920ac4 100644 --- a/src/library/global.h +++ b/src/library/global.h @@ -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; diff --git a/src/library/logging.cpp b/src/library/logging.cpp index 97089daa..9d4aa6be 100644 --- a/src/library/logging.cpp +++ b/src/library/logging.cpp @@ -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) { diff --git a/src/library/main.cpp b/src/library/main.cpp index 4038a174..759bd3a9 100644 --- a/src/library/main.cpp +++ b/src/library/main.cpp @@ -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(); } } diff --git a/src/library/systemwrappers.cpp b/src/library/systemwrappers.cpp index 6ed24bb7..d6ba5a80 100644 --- a/src/library/systemwrappers.cpp +++ b/src/library/systemwrappers.cpp @@ -27,6 +27,7 @@ namespace libtas { DEFINE_ORIG_POINTER(getpid); +DEFINE_ORIG_POINTER(fork); /* Override */ pid_t getpid (void) throw() { @@ -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; +} + } diff --git a/src/library/systemwrappers.h b/src/library/systemwrappers.h index 70983724..2313462e 100644 --- a/src/library/systemwrappers.h +++ b/src/library/systemwrappers.h @@ -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; }