Skip to content

Commit a64e060

Browse files
fix a bug of invalid parameter for shm_open on macOS26
1 parent b5faabd commit a64e060

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/tsharedmemory_unix.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,41 @@ bool TSharedMemory::create(size_t size)
4040

4141
struct stat st;
4242
header_t *header = nullptr;
43+
int flags = 0;
4344

4445
// Creates shared memory
45-
_fd = shm_open(qUtf8Printable(_name), O_CREAT | O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR);
46+
_fd = shm_open(qUtf8Printable(_name), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
4647
if (_fd < 0) {
4748
// error
49+
tSystemError("SharedMemory shm_open error. name:{} size:{} errno:{} [{}:{}]", _name, (qulonglong)size, errno, __FILE__, __LINE__);
50+
goto error;
51+
}
52+
53+
flags = fcntl(_fd, F_GETFD);
54+
if (flags < 0 || fcntl(_fd, F_SETFD, flags | FD_CLOEXEC) < 0) {
55+
tSystemError("SharedMemory fcntl error. name:{} size:{} errno:{} [{}:{}]", _name, (qulonglong)size, errno, __FILE__, __LINE__);
4856
goto error;
4957
}
5058

5159
if (fstat(_fd, &st) < 0) {
5260
// error
61+
tSystemError("SharedMemory fstat error. name:{} size:{} errno:{} [{}:{}]", _name, (qulonglong)size, errno, __FILE__, __LINE__);
5362
goto error;
5463
}
5564

5665
size = std::max(sizeof(header_t) * 10, size);
5766
if ((size_t)st.st_size < size) {
5867
if (ftruncate(_fd, size) < 0) {
5968
// error
69+
tSystemError("SharedMemory ftruncate error. name:{} size:{} errno:{} [{}:{}]", _name, (qulonglong)size, errno, __FILE__, __LINE__);
6070
goto error;
6171
}
6272
}
6373

6474
_ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, _fd, 0);
6575
if (!_ptr || _ptr == MAP_FAILED) {
6676
// error
77+
tSystemError("SharedMemory mmap error. name:{} size:{} errno:{} [{}:{}]", _name, (qulonglong)size, errno, __FILE__, __LINE__);
6778
goto error;
6879
}
6980

@@ -75,8 +86,6 @@ bool TSharedMemory::create(size_t size)
7586
return true;
7687

7788
error:
78-
tSystemError("SharedMemory create error. name:{} size:{} errno:{} [{}:{}]", _name, (qulonglong)size, errno, __FILE__, __LINE__);
79-
8089
if (_fd > 0) {
8190
tf_close(_fd);
8291
_fd = 0;

0 commit comments

Comments
 (0)