libandroid-posix-semaphore: fix deadlock and add parameter check
This commit is contained in:
parent
d2a0f42088
commit
636b8ada89
@ -3,13 +3,12 @@ TERMUX_PKG_DESCRIPTION="Shared library for the posix semaphore system function"
|
|||||||
TERMUX_PKG_LICENSE="MIT"
|
TERMUX_PKG_LICENSE="MIT"
|
||||||
TERMUX_PKG_MAINTAINER="@termux"
|
TERMUX_PKG_MAINTAINER="@termux"
|
||||||
TERMUX_PKG_VERSION=0.1
|
TERMUX_PKG_VERSION=0.1
|
||||||
TERMUX_PKG_REVISION=1
|
TERMUX_PKG_REVISION=2
|
||||||
TERMUX_PKG_SKIP_SRC_EXTRACT=true
|
TERMUX_PKG_SKIP_SRC_EXTRACT=true
|
||||||
TERMUX_PKG_BUILD_IN_SRC=true
|
TERMUX_PKG_BUILD_IN_SRC=true
|
||||||
|
|
||||||
termux_step_make() {
|
termux_step_make() {
|
||||||
$CC $CFLAGS $CPPFLAGS -DPREFIX="\"$TERMUX_PREFIX\"" \
|
$CC $CFLAGS $CPPFLAGS -c $TERMUX_PKG_BUILDER_DIR/semaphore.c
|
||||||
-c $TERMUX_PKG_BUILDER_DIR/semaphore.c
|
|
||||||
$CC $LDFLAGS -shared semaphore.o -o libandroid-posix-semaphore.so
|
$CC $LDFLAGS -shared semaphore.o -o libandroid-posix-semaphore.so
|
||||||
$AR rcu libandroid-posix-semaphore.a semaphore.o
|
$AR rcu libandroid-posix-semaphore.a semaphore.o
|
||||||
cp -f $TERMUX_PKG_BUILDER_DIR/LICENSE $TERMUX_PKG_SRCDIR/
|
cp -f $TERMUX_PKG_BUILDER_DIR/LICENSE $TERMUX_PKG_SRCDIR/
|
||||||
|
@ -38,15 +38,13 @@
|
|||||||
#include <sys/stat.h> // fstat()
|
#include <sys/stat.h> // fstat()
|
||||||
#include <stdlib.h> // calloc()
|
#include <stdlib.h> // calloc()
|
||||||
#include <pthread.h> // mutex
|
#include <pthread.h> // mutex
|
||||||
|
#include <paths.h> // _PATH_TMP
|
||||||
|
|
||||||
#ifndef SEM_NSEMS_MAX
|
#ifndef SEM_NSEMS_MAX
|
||||||
#define SEM_NSEMS_MAX 256
|
#define SEM_NSEMS_MAX 256
|
||||||
#endif // !SEM_NSEMS_MAX
|
#endif // !SEM_NSEMS_MAX
|
||||||
|
|
||||||
#ifndef PREFIX
|
#define SEM_PREFIX _PATH_TMP "sem."
|
||||||
#define PREFIX "/data/data/com.termux/files/usr"
|
|
||||||
#endif
|
|
||||||
#define SEM_PREFIX PREFIX"/tmp/sem."
|
|
||||||
|
|
||||||
static __inline__ char *__strchrnul(const char *s, int c)
|
static __inline__ char *__strchrnul(const char *s, int c)
|
||||||
{
|
{
|
||||||
@ -63,7 +61,7 @@ typedef struct {
|
|||||||
} semtab_type;
|
} semtab_type;
|
||||||
|
|
||||||
static semtab_type *semtab;
|
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 LOCK(l) (pthread_mutex_lock(&l))
|
||||||
#define UNLOCK(l) (pthread_mutex_unlock(&l))
|
#define UNLOCK(l) (pthread_mutex_unlock(&l))
|
||||||
@ -223,10 +221,15 @@ fail:
|
|||||||
|
|
||||||
int sem_close(sem_t *sem)
|
int sem_close(sem_t *sem)
|
||||||
{
|
{
|
||||||
|
if (sem == NULL || semtab == NULL) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
int i;
|
int i;
|
||||||
LOCK(lock);
|
LOCK(lock);
|
||||||
for (i=0; i<SEM_NSEMS_MAX && semtab[i].sem != sem; i++);
|
for (i=0; i<SEM_NSEMS_MAX && semtab[i].sem != sem; i++);
|
||||||
if (i == SEM_NSEMS_MAX) {
|
if (i == SEM_NSEMS_MAX) {
|
||||||
|
UNLOCK(lock);
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user