Skip to content

Commit

Permalink
fix(NamedEvent): Release semaphore ID in dtor when created with semget (
Browse files Browse the repository at this point in the history
fixes #2971) (#4357)

* fix(NamedEvent): Release semaphore ID in dtor when created with semget (#2971)

* Temporary: trying to solve for non-Linux semaphores.

* chore(NamedEvent): Cleanup of Posix NamedEvent and fixes of unit test.
  • Loading branch information
matejk authored Oct 20, 2024
1 parent 36c8082 commit c735162
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 36 deletions.
18 changes: 14 additions & 4 deletions Foundation/include/Poco/NamedEvent_UNIX.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@
#ifndef Foundation_NamedEvent_UNIX_INCLUDED
#define Foundation_NamedEvent_UNIX_INCLUDED


#include "Poco/Foundation.h"

#include <Poco/Platform.h>

#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__)
#include <semaphore.h>
#define POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES 1
#define POCO_NAMED_EVENT_USE_SYS_V_SEMAPHORES 0
#else
#define POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES 0
#define POCO_NAMED_EVENT_USE_SYS_V_SEMAPHORES 1
#endif

#if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
#include <semaphore.h>
#endif

namespace Poco {

Expand All @@ -39,10 +48,11 @@ class Foundation_API NamedEventImpl
std::string getFileName();

std::string _name;
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__)
sem_t* _sem;
#if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
::sem_t* _sem;
#else
int _semid; // semaphore id
bool _createdId; // semaphore id was created with this instance
#endif
};

Expand Down
78 changes: 46 additions & 32 deletions Foundation/src/NamedEvent_UNIX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@
// SPDX-License-Identifier: BSL-1.0
//


#include "Poco/NamedEvent_UNIX.h"
#include "Poco/Format.h"
#include "Poco/Exception.h"

#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__)
#include <semaphore.h>
#if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
#include <semaphore.h>
#else
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
// System V semaphores
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#endif


Expand Down Expand Up @@ -53,71 +54,83 @@ NamedEventImpl::NamedEventImpl(const std::string& name):
_name(name)
{
std::string fileName = getFileName();
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__)
_sem = sem_open(fileName.c_str(), O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 0);
#if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
_sem = ::sem_open(fileName.c_str(), O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 0);
if ((long) _sem == (long) SEM_FAILED)
throw SystemException(Poco::format("cannot create named mutex %s (sem_open() failed, errno=%d)", fileName, errno), _name);
throw SystemException(Poco::format("cannot create named event %s (sem_open() failed, errno=%d)", fileName, errno), _name);
#else
int fd = open(fileName.c_str(), O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
_createdId = false;
int fd = ::open(fileName.c_str(), O_RDONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd == -1 && errno == ENOENT)
fd = open(fileName.c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
fd = ::open(fileName.c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd != -1)
close(fd);
::close(fd);
else
throw SystemException(Poco::format("cannot create named event %s (lockfile)", fileName), _name);
key_t key = ftok(fileName.c_str(), 'p');

key_t key = ::ftok(fileName.c_str(), 'p');
if (key == -1)
throw SystemException(Poco::format("cannot create named mutex %s (ftok() failed, errno=%d)", fileName, errno), _name);
_semid = semget(key, 1, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | IPC_CREAT | IPC_EXCL);
throw SystemException(Poco::format("cannot create named event %s (ftok() failed, errno=%d)", fileName, errno), _name);

_semid = ::semget(key, 1, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | IPC_CREAT | IPC_EXCL);
if (_semid >= 0)
{
_createdId = true;
union semun arg;
arg.val = 0;
semctl(_semid, 0, SETVAL, arg);
::semctl(_semid, 0, SETVAL, arg);
}
else if (errno == EEXIST)
{
_semid = semget(key, 1, 0);
_semid = ::semget(key, 1, 0);
}
else throw SystemException(Poco::format("cannot create named mutex %s (semget() failed, errno=%d)", fileName, errno), _name);
#endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__)
else
throw SystemException(Poco::format("cannot create named event %s (semget() failed, errno=%d)", fileName, errno), _name);
#endif // POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
}


NamedEventImpl::~NamedEventImpl()
{
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__)
sem_close(_sem);
#if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
::sem_close(_sem);
::sem_unlink(_name.c_str());
#else
if (_createdId)
{
::semctl(_semid, 0, IPC_RMID);
}
#endif
}


void NamedEventImpl::setImpl()
{
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__)
if (sem_post(_sem) != 0)
throw SystemException("cannot set named event", _name);
#if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
if (::sem_post(_sem) != 0)
throw SystemException("cannot set named event", _name);
#else
struct sembuf op;
op.sem_num = 0;
op.sem_op = 1;
op.sem_flg = 0;
if (semop(_semid, &op, 1) != 0)
throw SystemException("cannot set named event", _name);
if (::semop(_semid, &op, 1) != 0)
throw SystemException("cannot set named event", _name);
#endif
}


void NamedEventImpl::waitImpl()
{
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__)
#if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
int err;
do
{
err = sem_wait(_sem);
err = ::sem_wait(_sem);
}
while (err && errno == EINTR);
if (err) throw SystemException("cannot wait for named event", _name);
if (err)
throw SystemException("cannot wait for named event", _name);
#else
struct sembuf op;
op.sem_num = 0;
Expand All @@ -126,10 +139,11 @@ void NamedEventImpl::waitImpl()
int err;
do
{
err = semop(_semid, &op, 1);
err = ::semop(_semid, &op, 1);
}
while (err && errno == EINTR);
if (err) throw SystemException("cannot wait for named event", _name);
if (err)
throw SystemException("cannot wait for named event", _name);
#endif
}

Expand Down
51 changes: 51 additions & 0 deletions Foundation/testsuite/src/NamedEventTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@
#include "Poco/Thread.h"
#include "Poco/Runnable.h"
#include "Poco/Timestamp.h"
#include "Poco/Exception.h"

#include <iostream>

#if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
#include <semaphore.h>
#endif

using Poco::NamedEvent;
using Poco::Thread;
using Poco::Runnable;
using Poco::Timestamp;

using namespace std::string_literals;

static NamedEvent testEvent("TestEvent");

Expand Down Expand Up @@ -101,6 +108,49 @@ void NamedEventTest::testNamedEvent()
}


void NamedEventTest::testCreateManyNamedEvents()
{

std::string name;
#if (POCO_OS == POCO_OS_MAC_OS_X)
constexpr int testEventCount = 5000;
#else
constexpr int testEventCount = 20000;
#endif

#if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
// Attempt to remove existing semaphores from previous run of the test suite.
for (int i = 0; i < testEventCount; i++)
{
name = std::string("TestEvent ") + std::to_string(i);
if (::sem_unlink(name.c_str()) != 0)
{
if (errno != ENOENT)
{
std::cout << "NamedEvent: Cleanup sem_unlink failed: " << errno << std::endl;
}
}
}
#endif

try
{
int i = 0;
for (; i < testEventCount; i++)
{
name = std::string("TestEvent_") + std::to_string(i);
auto* ne = new NamedEvent(name);
delete ne;
}
assertEqual(i, testEventCount);
}
catch (const Poco::Exception& e)
{
fail ("Failed creating named event: "s + name + " " + e.displayText());
}
}


void NamedEventTest::setUp()
{
}
Expand All @@ -116,6 +166,7 @@ CppUnit::Test* NamedEventTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NamedEventTest");

CppUnit_addTest(pSuite, NamedEventTest, testNamedEvent);
CppUnit_addTest(pSuite, NamedEventTest, testCreateManyNamedEvents);

return pSuite;
}
1 change: 1 addition & 0 deletions Foundation/testsuite/src/NamedEventTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class NamedEventTest: public CppUnit::TestCase
~NamedEventTest();

void testNamedEvent();
void testCreateManyNamedEvents();

void setUp();
void tearDown();
Expand Down

0 comments on commit c735162

Please sign in to comment.