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
This commit is contained in:
Carsten Haitzler 2016-08-05 18:11:42 +09:00
parent 8fd224b4d6
commit 4a40ff95de
1 changed files with 6 additions and 3 deletions

View File

@ -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
}