Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix codecheck #67

Merged
merged 1 commit into from
Oct 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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