Ensure the forkit does not have lingering threads after preinit.

Change-Id: I216a03a0ada628189355ebfd4016bf6033430eef
This commit is contained in:
Michael Meeks 2018-04-16 20:03:01 +01:00
parent d05b60e611
commit 03bbe52665
3 changed files with 21 additions and 0 deletions

View file

@ -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()
{

View file

@ -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<std::string> &args,

View file

@ -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.