LibCore: Ensure shared memory file names on macOS are unique

At least on my mac, clock_gettime only provides millisecond resolution.
So if many WebContent processes are opened at once, it is not unlikely
that they will all create their backing stores within the same ms. When
that happens, all but the first will fail (and crash).

To prevent this, generate the shared memory file name based on the PID
and a static counter.
This commit is contained in:
Timothy Flynn 2024-10-04 16:16:43 -04:00 committed by Andreas Kling
commit 5056bda043
Notes: github-actions[bot] 2024-10-06 05:49:03 +00:00

View file

@ -528,9 +528,9 @@ ErrorOr<int> anon_create([[maybe_unused]] size_t size, [[maybe_unused]] int opti
return Error::from_errno(saved_errno);
}
#elif defined(AK_OS_BSD_GENERIC) || defined(AK_OS_EMSCRIPTEN) || defined(AK_OS_HAIKU)
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
auto name = ByteString::formatted("/shm-{}{}", (unsigned long)time.tv_sec, (unsigned long)time.tv_nsec);
static size_t shared_memory_id = 0;
auto name = ByteString::formatted("/shm-{}-{}", getpid(), shared_memory_id++);
fd = shm_open(name.characters(), O_RDWR | O_CREAT | options, 0600);
if (shm_unlink(name.characters()) == -1) {