-
Notifications
You must be signed in to change notification settings - Fork 793
/
Copy pathRobustSharedLock.hpp
332 lines (272 loc) · 8.2 KB
/
RobustSharedLock.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Copyright 2020 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef _FASTDDS_ROBUST_SHARED_LOCK_H_
#define _FASTDDS_ROBUST_SHARED_LOCK_H_
#ifdef _MSC_VER
#include <io.h>
#else
#include <sys/file.h>
#endif // ifdef _MSC_VER
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "SharedDir.hpp"
namespace eprosima {
namespace fastdds {
namespace rtps {
/**
* This class implement an interprocess named resource that holds a shared exclusion lock until
* destroyed, or until the creator process dies
*/
class RobustSharedLock
{
public:
/**
* Open or create and acquire the interprocess lock.
* @param in name Is the object's interprocess global name, visible for all processes in the same machine.
* @param out was_lock_created If the lock succeeded, this parameter return whether the lock has been created
* or it already exist.
* @throw std::exception if lock coulnd't be acquired
*/
RobustSharedLock(
const std::string& name,
bool* was_lock_created,
bool* was_lock_released)
{
auto file_path = SharedDir::get_lock_path(name);
fd_ = open_and_lock_file(file_path, was_lock_created, was_lock_released);
name_ = name;
}
/**
* Open or create and acquire the interprocess lock.
* @param in name Is the object's interprocess global name, visible for all processes in the same machine.
* @throw std::exception if lock coulnd't be acquired
*/
RobustSharedLock(
const std::string& name)
{
bool was_lock_created;
auto file_path = SharedDir::get_lock_path(name);
fd_ = open_and_lock_file(file_path, &was_lock_created, nullptr);
name_ = name;
}
/**
* Release the lock
*/
~RobustSharedLock()
{
unlock_and_close();
}
/**
* Check if there is some process holding the lock
* @param in name Is the object's interprocess global name.
* @return true is the resource is locked, false if not locked by anyone
*/
static bool is_locked(
const std::string& name)
{
return test_lock(SharedDir::get_lock_path(name)) == LockStatus::LOCKED;
}
/**
* Remove the object
* @param in name Is the object's interprocess global name.
* @return true when success, false otherwise.
*/
static bool remove(
const std::string& name)
{
return 0 == std::remove(SharedDir::get_lock_path(name).c_str());
}
private:
std::string name_;
int fd_;
enum class LockStatus
{
NOT_LOCKED,
OPEN_FAILED,
LOCKED
};
#ifdef _MSC_VER
int open_and_lock_file(
const std::string& file_path,
bool* was_lock_created,
bool* was_lock_released)
{
int test_exist;
// Try open exclusive
auto ret = _sopen_s(&test_exist, file_path.c_str(), _O_WRONLY, _SH_DENYWR, _S_IREAD | _S_IWRITE);
if (ret == 0)
{
*was_lock_created = false;
if (was_lock_released)
{
*was_lock_released = true;
}
_close(test_exist);
}
else
{
// Try open shared
ret = _sopen_s(&test_exist, file_path.c_str(), _O_RDONLY, _SH_DENYWR, _S_IREAD | _S_IWRITE);
if (ret == 0)
{
if (was_lock_released)
{
*was_lock_released = false;
}
*was_lock_created = false;
return test_exist;
}
else
{
if (was_lock_released)
{
*was_lock_released = true;
}
*was_lock_created = true;
}
}
int fd;
// Open or create shared
ret = _sopen_s(&fd, file_path.c_str(), O_CREAT | _O_RDONLY, _SH_DENYWR, _S_IREAD | _S_IWRITE);
if (ret != 0)
{
char errmsg[1024];
strerror_s(errmsg, sizeof(errmsg), errno);
throw std::runtime_error("failed to open/create " + file_path + " " + std::string(errmsg));
}
return fd;
}
void unlock_and_close()
{
_close(fd_);
test_lock(SharedDir::get_lock_path(name_), true);
}
static LockStatus test_lock(
const std::string& file_path,
bool remove_if_unlocked = false)
{
LockStatus lock_status;
int fd;
auto ret = _sopen_s(&fd, file_path.c_str(), _O_RDONLY, _SH_DENYNO, _S_IREAD | _S_IWRITE);
if (ret == 0)
{
lock_status = LockStatus::NOT_LOCKED;
_close(fd);
// Lock exclusive
ret = _sopen_s(&fd, file_path.c_str(), _O_WRONLY, _SH_DENYWR, _S_IREAD | _S_IWRITE);
if (ret != 0)
{
lock_status = LockStatus::LOCKED;
}
else
{
_close(fd);
}
}
else
{
lock_status = LockStatus::OPEN_FAILED;
}
if (lock_status == LockStatus::NOT_LOCKED && remove_if_unlocked)
{
if (0 != std::remove(file_path.c_str()))
{
EPROSIMA_LOG_WARNING(RTPS_TRANSPORT_SHM, "Failed to remove " << file_path);
}
}
return lock_status;
}
#else
int open_and_lock_file(
const std::string& file_path,
bool* was_lock_created,
bool* was_lock_released)
{
auto fd = open(file_path.c_str(), O_RDONLY, 0);
if (fd != -1)
{
*was_lock_created = false;
}
else
{
*was_lock_created = true;
fd = open(file_path.c_str(), O_CREAT | O_RDONLY, 0666);
}
if (was_lock_released != nullptr)
{
// Lock exclusive
if (0 == flock(fd, LOCK_EX | LOCK_NB))
{
// Exclusive => shared
flock(fd, LOCK_SH | LOCK_NB);
*was_lock_released = true;
return fd;
}
else
{
*was_lock_released = false;
}
}
// Lock shared
if (0 != flock(fd, LOCK_SH | LOCK_NB))
{
close(fd);
throw std::runtime_error(("failed to lock " + file_path).c_str());
}
return fd;
}
void unlock_and_close()
{
flock(fd_, LOCK_UN | LOCK_NB);
close(fd_);
test_lock(SharedDir::get_lock_path(name_), true);
}
static LockStatus test_lock(
const std::string& file_path,
bool remove_if_unlocked = false)
{
LockStatus lock_status;
auto fd = open(file_path.c_str(), O_RDONLY, 0);
if (fd != -1)
{
lock_status = LockStatus::NOT_LOCKED;
// Try lock exclusive
if (0 != flock(fd, LOCK_EX | LOCK_NB))
{
// Failed so the file is locked shared
flock(fd, LOCK_UN | LOCK_NB);
lock_status = LockStatus::LOCKED;
}
close(fd);
}
else
{
lock_status = LockStatus::OPEN_FAILED;
}
if (lock_status == LockStatus::NOT_LOCKED && remove_if_unlocked)
{
if (0 != std::remove(file_path.c_str()))
{
EPROSIMA_LOG_WARNING(RTPS_TRANSPORT_SHM, "Failed to remove " << file_path);
}
}
return lock_status;
}
#endif // ifdef _MSC_VER
};
} // namespace rtps
} // namespace fastdds
} // namespace eprosima
#endif // _FASTDDS_ROBUST_SHARED_LOCK_H_