Skip to content

Commit

Permalink
Fix codecheck (#67)
Browse files Browse the repository at this point in the history
Signed-off-by: Louise Poubel <louise@openrobotics.org>
  • Loading branch information
chapulina authored Oct 15, 2020
1 parent c2f5192 commit eb93a00
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions src/Manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class ignition::launch::ManagerPrivate

/// \brief Semaphore to prevent restartThread from being a spinlock
private: sem_t *stoppedChildSem;

/// \brief Name of the semaphore created by stoppedChildSem.
private: std::string stoppedChildSemName;

Expand Down Expand Up @@ -299,32 +299,39 @@ ManagerPrivate::ManagerPrivate()
std::bind(&ManagerPrivate::OnSigIntTerm, this, std::placeholders::_1));

{
// The semaphore we initialize in the next section needs a unique name, so we need
// to seed a random number generator with a quality random number. Especially time(0)
// itself is not a good seed as there may be multiple processes launched at the same time.
// The semaphore we initialize in the next section needs a unique name, so
// we need to seed a random number generator with a quality random number.
// Especially time(0) itself is not a good seed as there may be multiple
// processes launched at the same time.
const auto time_seed = static_cast<size_t>(std::time(nullptr));
const auto pid_seed = std::hash<std::thread::id>()(std::this_thread::get_id());
const auto pid_seed = std::hash<std::thread::id>()(
std::this_thread::get_id());
std::seed_seq seed_value{time_seed, pid_seed};
std::vector<size_t> seeds(1);
seed_value.generate(seeds.begin(), seeds.end());
math::Rand::Seed(seeds[0]);
}
const auto semRandomId = math::Rand::IntUniform(0, std::numeric_limits<int32_t>::max());
const auto semRandomId = math::Rand::IntUniform(0,
std::numeric_limits<int32_t>::max());

// Initialize semaphore
this->stoppedChildSemName = std::string("ign-launch-") + std::to_string(semRandomId);
this->stoppedChildSem = sem_open(this->stoppedChildSemName.c_str(), O_CREAT, 0644, 1);
if (this->stoppedChildSem == SEM_FAILED) {
ignerr << "Error initializing semaphore " << this->stoppedChildSemName << ": " <<
strerror(errno) << std::endl;
this->stoppedChildSemName = std::string("ign-launch-") +
std::to_string(semRandomId);
this->stoppedChildSem = sem_open(this->stoppedChildSemName.c_str(), O_CREAT,
0644, 1);
if (this->stoppedChildSem == SEM_FAILED)
{
ignerr << "Error initializing semaphore " << this->stoppedChildSemName
<< ": " << strerror(errno) << std::endl;
}

// Register a signal handler to capture child process death events.
if (signal(SIGCHLD, ManagerPrivate::OnSigChild) == SIG_ERR)
ignerr << "signal(2) failed while setting up for SIGCHLD" << std::endl;

// Register backward signal handler for other signals
std::vector<int> signals = {
std::vector<int> signals =
{
SIGABRT, // Abort signal from abort(3)
SIGBUS, // Bus error (bad memory access)
SIGFPE, // Floating point exception
Expand All @@ -348,14 +355,14 @@ ManagerPrivate::~ManagerPrivate()
{
if (sem_close(this->stoppedChildSem) == -1)
{
ignerr << "Failed to close semaphore " << this->stoppedChildSemName << ": " <<
strerror(errno) << std::endl;
ignerr << "Failed to close semaphore " << this->stoppedChildSemName
<< ": " << strerror(errno) << std::endl;
}

if (sem_unlink(this->stoppedChildSemName.c_str()) == -1)
{
ignerr << "Failed to unlink semaphore " << this->stoppedChildSemName << ": " <<
strerror(errno) << std::endl;
ignerr << "Failed to unlink semaphore " << this->stoppedChildSemName
<< ": " << strerror(errno) << std::endl;
}
}
}
Expand Down

0 comments on commit eb93a00

Please # to comment.