fix(NamedEvent): Release semaphore ID in dtor when created with semget (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.
This commit is contained in:
Matej Kenda 2024-10-20 10:05:50 +02:00 committed by GitHub
parent 36c808230c
commit c735162abc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 112 additions and 36 deletions

View File

@ -17,12 +17,21 @@
#ifndef Foundation_NamedEvent_UNIX_INCLUDED #ifndef Foundation_NamedEvent_UNIX_INCLUDED
#define Foundation_NamedEvent_UNIX_INCLUDED #define Foundation_NamedEvent_UNIX_INCLUDED
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include <Poco/Platform.h>
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__) #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 #endif
#if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
#include <semaphore.h>
#endif
namespace Poco { namespace Poco {
@ -39,10 +48,11 @@ private:
std::string getFileName(); std::string getFileName();
std::string _name; std::string _name;
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__) #if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
sem_t* _sem; ::sem_t* _sem;
#else #else
int _semid; // semaphore id int _semid; // semaphore id
bool _createdId; // semaphore id was created with this instance
#endif #endif
}; };

View File

@ -11,20 +11,21 @@
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
// //
#include "Poco/NamedEvent_UNIX.h" #include "Poco/NamedEvent_UNIX.h"
#include "Poco/Format.h" #include "Poco/Format.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h> #include <errno.h>
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__) #if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
#include <semaphore.h> #include <semaphore.h>
#else #else
#include <unistd.h> // System V semaphores
#include <sys/types.h> #include <unistd.h>
#include <sys/ipc.h> #include <sys/types.h>
#include <sys/sem.h> #include <sys/ipc.h>
#include <sys/sem.h>
#endif #endif
@ -53,71 +54,83 @@ NamedEventImpl::NamedEventImpl(const std::string& name):
_name(name) _name(name)
{ {
std::string fileName = getFileName(); std::string fileName = getFileName();
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__) #if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
_sem = sem_open(fileName.c_str(), O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 0); _sem = ::sem_open(fileName.c_str(), O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 0);
if ((long) _sem == (long) SEM_FAILED) 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 #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) 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) if (fd != -1)
close(fd); ::close(fd);
else else
throw SystemException(Poco::format("cannot create named event %s (lockfile)", fileName), _name); 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) if (key == -1)
throw SystemException(Poco::format("cannot create named mutex %s (ftok() failed, errno=%d)", fileName, errno), _name); 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);
_semid = ::semget(key, 1, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | IPC_CREAT | IPC_EXCL);
if (_semid >= 0) if (_semid >= 0)
{ {
_createdId = true;
union semun arg; union semun arg;
arg.val = 0; arg.val = 0;
semctl(_semid, 0, SETVAL, arg); ::semctl(_semid, 0, SETVAL, arg);
} }
else if (errno == EEXIST) 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); else
#endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__) 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() NamedEventImpl::~NamedEventImpl()
{ {
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__) #if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
sem_close(_sem); ::sem_close(_sem);
::sem_unlink(_name.c_str());
#else
if (_createdId)
{
::semctl(_semid, 0, IPC_RMID);
}
#endif #endif
} }
void NamedEventImpl::setImpl() void NamedEventImpl::setImpl()
{ {
#if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) || defined(__GNU__) #if POCO_NAMED_EVENT_USE_POSIX_SEMAPHORES
if (sem_post(_sem) != 0) if (::sem_post(_sem) != 0)
throw SystemException("cannot set named event", _name); throw SystemException("cannot set named event", _name);
#else #else
struct sembuf op; struct sembuf op;
op.sem_num = 0; op.sem_num = 0;
op.sem_op = 1; op.sem_op = 1;
op.sem_flg = 0; op.sem_flg = 0;
if (semop(_semid, &op, 1) != 0) if (::semop(_semid, &op, 1) != 0)
throw SystemException("cannot set named event", _name); throw SystemException("cannot set named event", _name);
#endif #endif
} }
void NamedEventImpl::waitImpl() 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; int err;
do do
{ {
err = sem_wait(_sem); err = ::sem_wait(_sem);
} }
while (err && errno == EINTR); 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 #else
struct sembuf op; struct sembuf op;
op.sem_num = 0; op.sem_num = 0;
@ -126,10 +139,11 @@ void NamedEventImpl::waitImpl()
int err; int err;
do do
{ {
err = semop(_semid, &op, 1); err = ::semop(_semid, &op, 1);
} }
while (err && errno == EINTR); 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 #endif
} }

View File

@ -15,13 +15,20 @@
#include "Poco/Thread.h" #include "Poco/Thread.h"
#include "Poco/Runnable.h" #include "Poco/Runnable.h"
#include "Poco/Timestamp.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::NamedEvent;
using Poco::Thread; using Poco::Thread;
using Poco::Runnable; using Poco::Runnable;
using Poco::Timestamp; using Poco::Timestamp;
using namespace std::string_literals;
static NamedEvent testEvent("TestEvent"); static NamedEvent testEvent("TestEvent");
@ -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() void NamedEventTest::setUp()
{ {
} }
@ -116,6 +166,7 @@ CppUnit::Test* NamedEventTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NamedEventTest"); CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NamedEventTest");
CppUnit_addTest(pSuite, NamedEventTest, testNamedEvent); CppUnit_addTest(pSuite, NamedEventTest, testNamedEvent);
CppUnit_addTest(pSuite, NamedEventTest, testCreateManyNamedEvents);
return pSuite; return pSuite;
} }

View File

@ -25,6 +25,7 @@ public:
~NamedEventTest(); ~NamedEventTest();
void testNamedEvent(); void testNamedEvent();
void testCreateManyNamedEvents();
void setUp(); void setUp();
void tearDown(); void tearDown();