NamedMutex_UNIX.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // NamedMutex_UNIX.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/NamedMutex_UNIX.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Processes
  8. // Module: NamedMutex
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/NamedMutex_UNIX.h"
  16. #include "Poco/Format.h"
  17. #include "Poco/Exception.h"
  18. #include <fcntl.h>
  19. #include <sys/stat.h>
  20. #include <errno.h>
  21. #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(_AIX)
  22. #include <semaphore.h>
  23. #else
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/ipc.h>
  27. #include <sys/sem.h>
  28. #endif
  29. namespace Poco {
  30. #if (POCO_OS == POCO_OS_LINUX) || (POCO_OS == POCO_OS_CYGWIN) || (POCO_OS == POCO_OS_FREE_BSD)
  31. union semun
  32. {
  33. int val;
  34. struct semid_ds* buf;
  35. unsigned short int* array;
  36. struct seminfo* __buf;
  37. };
  38. #elif (POCO_OS == POCO_OS_HPUX)
  39. union semun
  40. {
  41. int val;
  42. struct semid_ds* buf;
  43. ushort* array;
  44. };
  45. #endif
  46. NamedMutexImpl::NamedMutexImpl(const std::string& name):
  47. _name(name)
  48. {
  49. std::string fileName = getFileName();
  50. #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
  51. _sem = sem_open(fileName.c_str(), O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 1);
  52. if ((long) _sem == (long) SEM_FAILED)
  53. throw SystemException(Poco::format("cannot create named mutex %s (sem_open() failed, errno=%d)", fileName, errno), _name);
  54. #else
  55. int fd = open(fileName.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  56. if (fd != -1)
  57. close(fd);
  58. else
  59. throw SystemException(Poco::format("cannot create named mutex %s (lockfile)", fileName), _name);
  60. key_t key = ftok(fileName.c_str(), 0);
  61. if (key == -1)
  62. throw SystemException(Poco::format("cannot create named mutex %s (ftok() failed, errno=%d)", fileName, errno), _name);
  63. _semid = semget(key, 1, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | IPC_CREAT | IPC_EXCL);
  64. if (_semid >= 0)
  65. {
  66. union semun arg;
  67. arg.val = 1;
  68. semctl(_semid, 0, SETVAL, arg);
  69. }
  70. else if (errno == EEXIST)
  71. {
  72. _semid = semget(key, 1, 0);
  73. }
  74. else throw SystemException(Poco::format("cannot create named mutex %s (semget() failed, errno=%d)", fileName, errno), _name);
  75. #endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
  76. }
  77. NamedMutexImpl::~NamedMutexImpl()
  78. {
  79. #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
  80. sem_close(_sem);
  81. #endif
  82. }
  83. void NamedMutexImpl::lockImpl()
  84. {
  85. #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
  86. int err;
  87. do
  88. {
  89. err = sem_wait(_sem);
  90. }
  91. while (err && errno == EINTR);
  92. if (err) throw SystemException("cannot lock named mutex", _name);
  93. #else
  94. struct sembuf op;
  95. op.sem_num = 0;
  96. op.sem_op = -1;
  97. op.sem_flg = SEM_UNDO;
  98. int err;
  99. do
  100. {
  101. err = semop(_semid, &op, 1);
  102. }
  103. while (err && errno == EINTR);
  104. if (err) throw SystemException("cannot lock named mutex", _name);
  105. #endif
  106. }
  107. bool NamedMutexImpl::tryLockImpl()
  108. {
  109. #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
  110. return sem_trywait(_sem) == 0;
  111. #else
  112. struct sembuf op;
  113. op.sem_num = 0;
  114. op.sem_op = -1;
  115. op.sem_flg = SEM_UNDO | IPC_NOWAIT;
  116. return semop(_semid, &op, 1) == 0;
  117. #endif
  118. }
  119. void NamedMutexImpl::unlockImpl()
  120. {
  121. #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX)
  122. if (sem_post(_sem) != 0)
  123. throw SystemException("cannot unlock named mutex", _name);
  124. #else
  125. struct sembuf op;
  126. op.sem_num = 0;
  127. op.sem_op = 1;
  128. op.sem_flg = SEM_UNDO;
  129. if (semop(_semid, &op, 1) != 0)
  130. throw SystemException("cannot unlock named mutex", _name);
  131. #endif
  132. }
  133. std::string NamedMutexImpl::getFileName()
  134. {
  135. #if defined(sun) || defined(__APPLE__) || defined(__QNX__)
  136. std::string fn = "/";
  137. #else
  138. std::string fn = "/tmp/";
  139. #endif
  140. fn.append(_name);
  141. fn.append(".mutex");
  142. return fn;
  143. }
  144. } // namespace Poco