[80] | 1 | // |
---|
| 2 | // NamedMutex_UNIX.cpp |
---|
| 3 | // |
---|
| 4 | // $Id: //poco/1.3/Foundation/src/NamedMutex_UNIX.cpp#2 $ |
---|
| 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 | // Permission is hereby granted, free of charge, to any person or organization |
---|
| 14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
| 15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
| 16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
| 17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
| 18 | // do so, all subject to the following: |
---|
| 19 | // |
---|
| 20 | // The copyright notices in the Software and this entire statement, including |
---|
| 21 | // the above license grant, this restriction and the following disclaimer, |
---|
| 22 | // must be included in all copies of the Software, in whole or in part, and |
---|
| 23 | // all derivative works of the Software, unless such copies or derivative |
---|
| 24 | // works are solely in the form of machine-executable object code generated by |
---|
| 25 | // a source language processor. |
---|
| 26 | // |
---|
| 27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
| 30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
| 31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
| 32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
| 33 | // DEALINGS IN THE SOFTWARE. |
---|
| 34 | // |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | #include <Poco/NamedMutex_UNIX.h> |
---|
| 38 | #include <Poco/Exception.h> |
---|
| 39 | #include <fcntl.h> |
---|
| 40 | #include <sys/stat.h> |
---|
| 41 | #include <errno.h> |
---|
| 42 | #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(_AIX) |
---|
| 43 | #include <semaphore.h> |
---|
| 44 | #else |
---|
| 45 | #include <unistd.h> |
---|
| 46 | #include <sys/types.h> |
---|
| 47 | #include <sys/ipc.h> |
---|
| 48 | #include <sys/sem.h> |
---|
| 49 | #endif |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | namespace Poco { |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | #if defined(linux) || defined(__CYGWIN__) |
---|
| 56 | union semun |
---|
| 57 | { |
---|
| 58 | int val; |
---|
| 59 | struct semid_ds* buf; |
---|
| 60 | unsigned short int* array; |
---|
| 61 | struct seminfo* __buf; |
---|
| 62 | }; |
---|
| 63 | #elif defined(__hpux) |
---|
| 64 | union semun |
---|
| 65 | { |
---|
| 66 | int val; |
---|
| 67 | struct semid_ds* buf; |
---|
| 68 | ushort* array; |
---|
| 69 | }; |
---|
| 70 | #endif |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | NamedMutexImpl::NamedMutexImpl(const std::string& name): |
---|
| 74 | _name(name) |
---|
| 75 | { |
---|
| 76 | std::string fileName = getFileName(); |
---|
| 77 | #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) |
---|
| 78 | _sem = sem_open(fileName.c_str(), O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO, 1); |
---|
| 79 | if ((long) _sem == (long) SEM_FAILED) |
---|
| 80 | throw SystemException("cannot create named mutex (sem_open() failed)", _name); |
---|
| 81 | #else |
---|
| 82 | int fd = open(fileName.c_str(), O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
---|
| 83 | if (fd != -1) |
---|
| 84 | close(fd); |
---|
| 85 | else |
---|
| 86 | throw SystemException("cannot create named mutex (lockfile)", _name); |
---|
| 87 | key_t key = ftok(fileName.c_str(), 0); |
---|
| 88 | if (key == -1) |
---|
| 89 | throw SystemException("cannot create named mutex (ftok() failed)", _name); |
---|
| 90 | _semid = semget(key, 1, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH | IPC_CREAT | IPC_EXCL); |
---|
| 91 | if (_semid >= 0) |
---|
| 92 | { |
---|
| 93 | union semun arg; |
---|
| 94 | arg.val = 1; |
---|
| 95 | semctl(_semid, 0, SETVAL, arg); |
---|
| 96 | } |
---|
| 97 | else if (errno == EEXIST) |
---|
| 98 | { |
---|
| 99 | _semid = semget(key, 1, 0); |
---|
| 100 | } |
---|
| 101 | else throw SystemException("cannot create named mutex (semget() failed)", _name); |
---|
| 102 | #endif // defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | NamedMutexImpl::~NamedMutexImpl() |
---|
| 107 | { |
---|
| 108 | #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) |
---|
| 109 | sem_close(_sem); |
---|
| 110 | #endif |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | void NamedMutexImpl::lockImpl() |
---|
| 115 | { |
---|
| 116 | #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) |
---|
| 117 | int err; |
---|
| 118 | do |
---|
| 119 | { |
---|
| 120 | err = sem_wait(_sem); |
---|
| 121 | } |
---|
| 122 | while (err && errno == EINTR); |
---|
| 123 | if (err) throw SystemException("cannot lock 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 | int err; |
---|
| 130 | do |
---|
| 131 | { |
---|
| 132 | err = semop(_semid, &op, 1); |
---|
| 133 | } |
---|
| 134 | while (err && errno == EINTR); |
---|
| 135 | if (err) throw SystemException("cannot lock named mutex", _name); |
---|
| 136 | #endif |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | bool NamedMutexImpl::tryLockImpl() |
---|
| 141 | { |
---|
| 142 | #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) |
---|
| 143 | return sem_trywait(_sem) == 0; |
---|
| 144 | #else |
---|
| 145 | struct sembuf op; |
---|
| 146 | op.sem_num = 0; |
---|
| 147 | op.sem_op = -1; |
---|
| 148 | op.sem_flg = SEM_UNDO | IPC_NOWAIT; |
---|
| 149 | return semop(_semid, &op, 1) == 0; |
---|
| 150 | #endif |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | void NamedMutexImpl::unlockImpl() |
---|
| 155 | { |
---|
| 156 | #if defined(sun) || defined(__APPLE__) || defined(__osf__) || defined(__QNX__) || defined(_AIX) |
---|
| 157 | if (sem_post(_sem) != 0) |
---|
| 158 | throw SystemException("cannot unlock named mutex", _name); |
---|
| 159 | #else |
---|
| 160 | struct sembuf op; |
---|
| 161 | op.sem_num = 0; |
---|
| 162 | op.sem_op = 1; |
---|
| 163 | op.sem_flg = SEM_UNDO; |
---|
| 164 | if (semop(_semid, &op, 1) != 0) |
---|
| 165 | throw SystemException("cannot unlock named mutex", _name); |
---|
| 166 | #endif |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | std::string NamedMutexImpl::getFileName() |
---|
| 171 | { |
---|
| 172 | #if defined(sun) || defined(__APPLE__) || defined(__QNX__) |
---|
| 173 | std::string fn = "/"; |
---|
| 174 | #else |
---|
| 175 | std::string fn = "/tmp/"; |
---|
| 176 | #endif |
---|
| 177 | fn.append(_name); |
---|
| 178 | fn.append(".mutex"); |
---|
| 179 | return fn; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | |
---|
| 183 | } // namespace Poco |
---|