Skip to content

Commit

Permalink
Use random name for manager semaphore
Browse files Browse the repository at this point in the history
Fixes #56.

Signed-off-by: Martin Pecka <[email protected]>
  • Loading branch information
peci1 committed Sep 2, 2020
1 parent 9f4a929 commit eb1b8a1
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/Manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <ignition/common/Console.hh>
#include <ignition/common/SignalHandler.hh>
#include <ignition/common/SystemPaths.hh>
#include <ignition/common/Uuid.hh>
#include <ignition/plugin/Loader.hh>

#include "ignition/launch/config.hh"
Expand All @@ -47,8 +48,6 @@
using namespace ignition::launch;
using namespace std::chrono_literals;

static constexpr const char* kSemaphoreName = "/child_semaphore";

/// \brief A class to encapsulate an executable (program) to run.
class Executable
{
Expand Down Expand Up @@ -177,6 +176,9 @@ 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;

/// \brief Thread containing the restart loop
private: std::thread restartThread;
Expand Down Expand Up @@ -295,9 +297,11 @@ ManagerPrivate::ManagerPrivate()
std::bind(&ManagerPrivate::OnSigIntTerm, this, std::placeholders::_1));

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

// Register a signal handler to capture child process death events.
Expand Down Expand Up @@ -329,12 +333,14 @@ ManagerPrivate::~ManagerPrivate()
{
if (sem_close(this->stoppedChildSem) == -1)
{
ignerr << "Failed to close semaphore: " << strerror(errno) << std::endl;
ignerr << "Failed to close semaphore " << this->stoppedChildSemName << ": " <<
strerror(errno) << std::endl;
}

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

0 comments on commit eb1b8a1

Please sign in to comment.