From 4a40ff95defa5fa7e6164459c50e674b53cddaf4 Mon Sep 17 00:00:00 2001 From: "Carsten Haitzler (Rasterman)" Date: Fri, 5 Aug 2016 18:11:42 +0900 Subject: [PATCH] eina lock semaphores - do not use shared semaphores and fix osx names the way eina does sempahores, they can NEVER be sensibly shared cross-process portably. so enabling sharing is a pointless idea. in fact some os's like openbsd check if the sempahore addres is in a sharable mem region and deny init if it is not. on osx you dont use shared memory but a sempahore name you share instead... and this is not exposed thus it can't be shared either. if we did process sharable semaphores we'd make shm segments and/or name them in a sharable way were you can share the idenitifer of the shm segment and/or the offset address or name from osx. but we don't, so making them process-private is the right thing. sharable sempahores will need a whole new api. this also fixes osx naming to make the name pretty unguessable/private and opened exclusive (or it fails) by using pid, sem counter, and 4 random numbers. it's not a security mechanism as the create will fail if there is a clash. chances are low. we unlink before anyway. good enough for osx for now. @fix --- src/lib/eina/eina_inline_lock_posix.x | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib/eina/eina_inline_lock_posix.x b/src/lib/eina/eina_inline_lock_posix.x index c28a2d0..55aad9d 100644 --- a/src/lib/eina/eina_inline_lock_posix.x +++ b/src/lib/eina/eina_inline_lock_posix.x @@ -841,12 +841,15 @@ eina_semaphore_new(Eina_Semaphore *sem, int count_init) ++_sem_ctr; eina_spinlock_release(&_sem_ctr_lock); - snprintf(sem->name, sizeof(sem->name), "/eina_sem_%u", _sem_ctr); + snprintf(sem->name, sizeof(sem->name), "/eina_sem_%x-%x_%x_%x_%x_%x", + (unsigned int)getpid(), _sem_ctr, + (unsigned int)rand(), (unsigned int)rand(), + (unsigned int)rand(), (unsigned int)rand()); sem_unlink(sem->name); - sem->sema = sem_open(sem->name, O_CREAT, 0644, count_init); + sem->sema = sem_open(sem->name, O_CREAT | O_EXCL, 0600, count_init); return (sem->sema == SEM_FAILED) ? EINA_FALSE : EINA_TRUE; #else - return (sem_init(sem, 1, count_init) == 0) ? EINA_TRUE : EINA_FALSE; + return (sem_init(sem, 0, count_init) == 0) ? EINA_TRUE : EINA_FALSE; #endif } -- 2.7.4