On Sun, Feb 23 2020 14:48:36 +0200, Lauri Tirkkonen wrote: > So, diff below makes struct __sem non-opaque and removes the indirect > allocations, so that the application is required to provide storage and > can therefore control where it's stored (which could be eg. shm).
followup diff that makes sem_init() ignore pshared arg -- it doesn't need to care, if the application puts the semaphore in shm. I tested an unnamed shm_init() semaphore in shm_mkstemp() -created shm with this, passing the fd to a child process and mmap()ing in the child process; sharing a semaphore this way does seem to work. diff --git a/lib/librthread/rthread_sem.c b/lib/librthread/rthread_sem.c index 984e5fb0047..441843d87ca 100644 --- a/lib/librthread/rthread_sem.c +++ b/lib/librthread/rthread_sem.c @@ -105,41 +105,6 @@ sem_init(sem_t *sem, int pshared, unsigned int value) return (-1); } - if (pshared) { - errno = EPERM; - return (-1); -#ifdef notyet - char name[SEM_RANDOM_NAME_LEN]; - sem_t *sempshared; - int i; - - for (;;) { - for (i = 0; i < SEM_RANDOM_NAME_LEN - 1; i++) - name[i] = arc4random_uniform(255) + 1; - name[SEM_RANDOM_NAME_LEN - 1] = '\0'; - sempshared = sem_open(name, O_CREAT | O_EXCL, 0, value); - if (sempshared != SEM_FAILED) - break; - if (errno == EEXIST) - continue; - if (errno != EPERM) - errno = ENOSPC; - return (-1); - } - - /* unnamed semaphore should not be opened twice */ - if (sem_unlink(name) == -1) { - sem_close(sempshared); - errno = ENOSPC; - return (-1); - } - - *semp = *sempshared; - free(sempshared); - return (0); -#endif - } - bzero(sem, sizeof(*sem)); sem->value = value; diff --git a/lib/librthread/rthread_sem_compat.c b/lib/librthread/rthread_sem_compat.c index 91c888cd49b..8dc863a04d3 100644 --- a/lib/librthread/rthread_sem_compat.c +++ b/lib/librthread/rthread_sem_compat.c @@ -116,41 +116,6 @@ sem_init(sem_t *sem, int pshared, unsigned int value) return (-1); } - if (pshared) { - errno = EPERM; - return (-1); -#ifdef notyet - char name[SEM_RANDOM_NAME_LEN]; - sem_t *sempshared; - int i; - - for (;;) { - for (i = 0; i < SEM_RANDOM_NAME_LEN - 1; i++) - name[i] = arc4random_uniform(255) + 1; - name[SEM_RANDOM_NAME_LEN - 1] = '\0'; - sempshared = sem_open(name, O_CREAT | O_EXCL, 0, value); - if (sempshared != SEM_FAILED) - break; - if (errno == EEXIST) - continue; - if (errno != EPERM) - errno = ENOSPC; - return (-1); - } - - /* unnamed semaphore should not be opened twice */ - if (sem_unlink(name) == -1) { - sem_close(sempshared); - errno = ENOSPC; - return (-1); - } - - *semp = *sempshared; - free(sempshared); - return (0); -#endif - } - bzero(sem, sizeof(*sem)); sem->lock = _SPINLOCK_UNLOCKED; sem->value = value; -- Lauri Tirkkonen | lotheac @ IRCnet