diff --git a/common/Util.cpp b/common/Util.cpp index 962b5b4c1..a03cf4c46 100644 --- a/common/Util.cpp +++ b/common/Util.cpp @@ -140,6 +140,21 @@ namespace Util return newTmp; } + int getProcessThreadCount() + { + DIR *fdDir = opendir("/proc/self/task"); + if (!fdDir) + { + LOG_ERR("No proc mounted"); + return -1; + } + int tasks = 0; + while (readdir(fdDir)) + tasks++; + closedir(fdDir); + return tasks; + } + // close what we have - far faster than going up to a 1m open_max eg. static bool closeFdsFromProc() { diff --git a/common/Util.hpp b/common/Util.hpp index db5217fbe..cd59f7d95 100644 --- a/common/Util.hpp +++ b/common/Util.hpp @@ -48,6 +48,9 @@ namespace Util /// Create randomized temporary directory std::string createRandomTmpDir(); + /// Get number of threads in this process or -1 on error + int getProcessThreadCount(); + /// Spawn a process if stdInput is non-NULL it contains a writable descriptor /// to send data to the child. int spawnProcess(const std::string &cmd, const std::vector &args, diff --git a/kit/ForKit.cpp b/kit/ForKit.cpp index 3dac8ac18..89f49e53d 100644 --- a/kit/ForKit.cpp +++ b/kit/ForKit.cpp @@ -527,6 +527,9 @@ int main(int argc, char** argv) if (!globalPreinit(loTemplate)) std::_Exit(Application::EXIT_SOFTWARE); + if (Util::getProcessThreadCount() != 1) + LOG_ERR("Error: forkit has more than a single thread after pre-init"); + LOG_INF("Preinit stage OK."); // We must have at least one child, more are created dynamically.