Skip to content

Commit 065a0bc

Browse files
committed
Add error checking for shm_unlink calls in shm_posix.cpp
Changes: 1. Added error checking for all three shm_unlink() calls 2. Save return value in temporary variable 'unlink_ret' 3. Log error with errno if shm_unlink() returns -1 4. Fixed remove(char const * name) to prepend '/' to name for consistency with acquire() function (POSIX requires /name format) This will help diagnose the second-run test failure on FreeBSD by showing if shm_unlink is failing and why.
1 parent 37f16ce commit 065a0bc

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/libipc/platform/posix/shm_posix.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ std::int32_t release(id_t id) noexcept {
173173
else if ((ret = acc_of(ii->mem_, ii->size_).fetch_sub(1, std::memory_order_acq_rel)) <= 1) {
174174
::munmap(ii->mem_, ii->size_);
175175
if (!ii->name_.empty()) {
176-
::shm_unlink(ii->name_.c_str());
176+
int unlink_ret = ::shm_unlink(ii->name_.c_str());
177+
if (unlink_ret == -1) {
178+
ipc::error("fail shm_unlink[%d]: %s\n", errno, ii->name_.c_str());
179+
}
177180
}
178181
}
179182
else ::munmap(ii->mem_, ii->size_);
@@ -190,7 +193,10 @@ void remove(id_t id) noexcept {
190193
auto name = std::move(ii->name_);
191194
release(id);
192195
if (!name.empty()) {
193-
::shm_unlink(name.c_str());
196+
int unlink_ret = ::shm_unlink(name.c_str());
197+
if (unlink_ret == -1) {
198+
ipc::error("fail shm_unlink[%d]: %s\n", errno, name.c_str());
199+
}
194200
}
195201
}
196202

@@ -199,7 +205,12 @@ void remove(char const * name) noexcept {
199205
ipc::error("fail remove: name is empty\n");
200206
return;
201207
}
202-
::shm_unlink(name);
208+
// For portable use, a shared memory object should be identified by name of the form /somename.
209+
ipc::string op_name = ipc::string{"/"} + name;
210+
int unlink_ret = ::shm_unlink(op_name.c_str());
211+
if (unlink_ret == -1) {
212+
ipc::error("fail shm_unlink[%d]: %s\n", errno, op_name.c_str());
213+
}
203214
}
204215

205216
} // namespace shm

0 commit comments

Comments
 (0)