libandroid-posix-semaphore: fix deadlock and add parameter check

This commit is contained in:
Chongyun Lee 2022-03-24 23:10:34 +08:00 committed by xtkoba
parent d2a0f42088
commit 636b8ada89
2 changed files with 10 additions and 8 deletions

View File

@ -3,13 +3,12 @@ TERMUX_PKG_DESCRIPTION="Shared library for the posix semaphore system function"
TERMUX_PKG_LICENSE="MIT"
TERMUX_PKG_MAINTAINER="@termux"
TERMUX_PKG_VERSION=0.1
TERMUX_PKG_REVISION=1
TERMUX_PKG_REVISION=2
TERMUX_PKG_SKIP_SRC_EXTRACT=true
TERMUX_PKG_BUILD_IN_SRC=true
termux_step_make() {
$CC $CFLAGS $CPPFLAGS -DPREFIX="\"$TERMUX_PREFIX\"" \
-c $TERMUX_PKG_BUILDER_DIR/semaphore.c
$CC $CFLAGS $CPPFLAGS -c $TERMUX_PKG_BUILDER_DIR/semaphore.c
$CC $LDFLAGS -shared semaphore.o -o libandroid-posix-semaphore.so
$AR rcu libandroid-posix-semaphore.a semaphore.o
cp -f $TERMUX_PKG_BUILDER_DIR/LICENSE $TERMUX_PKG_SRCDIR/

View File

@ -38,15 +38,13 @@
#include <sys/stat.h> // fstat()
#include <stdlib.h> // calloc()
#include <pthread.h> // mutex
#include <paths.h> // _PATH_TMP
#ifndef SEM_NSEMS_MAX
#define SEM_NSEMS_MAX 256
#endif // !SEM_NSEMS_MAX
#ifndef PREFIX
#define PREFIX "/data/data/com.termux/files/usr"
#endif
#define SEM_PREFIX PREFIX"/tmp/sem."
#define SEM_PREFIX _PATH_TMP "sem."
static __inline__ char *__strchrnul(const char *s, int c)
{
@ -63,7 +61,7 @@ typedef struct {
} semtab_type;
static semtab_type *semtab;
static pthread_mutex_t lock;
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
#define LOCK(l) (pthread_mutex_lock(&l))
#define UNLOCK(l) (pthread_mutex_unlock(&l))
@ -223,10 +221,15 @@ fail:
int sem_close(sem_t *sem)
{
if (sem == NULL || semtab == NULL) {
errno = EINVAL;
return -1;
}
int i;
LOCK(lock);
for (i=0; i<SEM_NSEMS_MAX && semtab[i].sem != sem; i++);
if (i == SEM_NSEMS_MAX) {
UNLOCK(lock);
errno = EINVAL;
return -1;
}