From eb1b8a1488712b75562515f39c6ac44a086fec9c Mon Sep 17 00:00:00 2001 From: Martin Pecka Date: Wed, 2 Sep 2020 04:09:17 +0200 Subject: [PATCH] Use random name for manager semaphore Fixes #56. Signed-off-by: Martin Pecka --- src/Manager.cc | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Manager.cc b/src/Manager.cc index 36451f93..219f2b27 100644 --- a/src/Manager.cc +++ b/src/Manager.cc @@ -36,6 +36,7 @@ #include #include #include +#include #include #include "ignition/launch/config.hh" @@ -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 { @@ -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; @@ -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. @@ -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; } } }