You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/09/26 19:06:41 UTC
svn commit: r819176 - in /commons/sandbox/runtime/trunk/src: main/native/
main/native/include/arch/hpux/ main/native/os/darwin/ main/native/os/hpux/
main/native/os/linux/ main/native/os/unix/ main/native/test/
test/org/apache/commons/runtime/
Author: mturk
Date: Sat Sep 26 17:06:40 2009
New Revision: 819176
URL: http://svn.apache.org/viewvc?rev=819176&view=rev
Log:
Unify mutex/shm/semaphore code
Modified:
commons/sandbox/runtime/trunk/src/main/native/configure
commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h
commons/sandbox/runtime/trunk/src/main/native/os/darwin/mutex.c
commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c
commons/sandbox/runtime/trunk/src/main/native/os/linux/selinux.c
commons/sandbox/runtime/trunk/src/main/native/os/unix/mutex.c
commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c
commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
Modified: commons/sandbox/runtime/trunk/src/main/native/configure
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/configure?rev=819176&r1=819175&r2=819176&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/configure (original)
+++ commons/sandbox/runtime/trunk/src/main/native/configure Sat Sep 26 17:06:40 2009
@@ -94,6 +94,7 @@
has_maintainer_mode=no
has_native_threads=no
has_sysv_mutex=no
+has_posix_mutex=no
has_unicode=no
has_openssl=no
@@ -607,7 +608,13 @@
varadds cppopts -DNDEBUG
fi
if [ ".$has_sysv_mutex" = .yes ]; then
+ if [ ".$has_posix_mutex" = .yes ]; then
+ echo "[ERROR] Cannot have both Posix and System V mutexes"
+ exit 22
+ fi
varadds cppopts -DACR_USE_SYSV_MUTEX
+elif [ ".$has_posix_mutex" = .yes ]; then
+ varadds cppopts -DACR_USE_POSIX_MUTEX
fi
if [ ".$has_test" = .yes ]; then
varadds cppopts -DACR_ENABLE_TEST
Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h?rev=819176&r1=819175&r2=819176&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/hpux/acr_arch_private.h Sat Sep 26 17:06:40 2009
@@ -53,13 +53,6 @@
#define ACR_WANT_FNMATCH 1
/**
- * HP-UX requires sysv mutex interface
- */
-#if !define(ACR_USE_SYSV_MUTEX)
-#define ACR_USE_SYSV_MUTEX 1
-#endif
-
-/**
* Temporary APR flags
*/
#define APR_USE_SHMEM_SHMGET 1
Modified: commons/sandbox/runtime/trunk/src/main/native/os/darwin/mutex.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/darwin/mutex.c?rev=819176&r1=819175&r2=819176&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/darwin/mutex.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/darwin/mutex.c Sat Sep 26 17:06:40 2009
@@ -133,7 +133,7 @@
if (fname) {
size_t nbytes;
semblock_t hdr;
- int fs, fd = -1;
+ int fd = -1;
fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0660);
if (fd < 0) {
@@ -156,10 +156,7 @@
hdr.creator = getpid();
hdr.magic = ACR_MTX_MAGIC;
hdr.value = 1;
- do {
- fs = write(fd,(const void *)&hdr, nbytes);
- } while (fs == (acr_size_t)-1 && errno == EINTR);
- if (fs == -1) {
+ if (r_write(fd,(const void *)&hdr, nbytes) < 0) {
rc = ACR_GET_OS_ERROR();
close(fd);
ACR_THROW_IO_IF_ERR(rc);
@@ -168,7 +165,7 @@
close(fd);
flags |= IPC_EXCL;
}
- m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t));
+ m = ACR_CALLOC(acr_pmutex_t, 1);
if (!m)
return -1;
if (fname) {
@@ -241,7 +238,7 @@
return -1;
}
close(file);
- m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t));
+ m = ACR_CALLOC(acr_pmutex_t, 1);
if (!m)
return -1;
@@ -274,7 +271,7 @@
return ACR_EFTYPE;
}
if (IS_INVALID_HANDLE(m)) {
- return ACR_EINVAL;
+ return ACR_EBADF;
}
op.sem_num = 0;
op.sem_op = -1;
@@ -301,7 +298,7 @@
return ACR_EFTYPE;
}
if (IS_INVALID_HANDLE(m)) {
- return ACR_EINVAL;
+ return ACR_EBADF;
}
op.sem_num = 0;
op.sem_op = -1;
@@ -334,8 +331,9 @@
return ACR_EFTYPE;
}
if (IS_INVALID_HANDLE(m)) {
- return ACR_EINVAL;
+ return ACR_EBADF;
}
+
m->locked = 0;
op.sem_num = 0;
op.sem_op = 1;
@@ -361,7 +359,7 @@
return ACR_EFTYPE;
}
if (IS_INVALID_HANDLE(m)) {
- return ACR_EINVAL;
+ return ACR_EBADF;
}
buf.sem_perm.uid = uid;
buf.sem_perm.gid = gid;
Modified: commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c?rev=819176&r1=819175&r2=819176&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/hpux/shm.c Sat Sep 26 17:06:40 2009
@@ -99,8 +99,8 @@
rc = ACR_GET_OS_ERROR();
goto finally;
}
- if (access(m->filename, F_OK) == 0) {
- /* File exists. Remove it */
+ if (!access(m->filename, F_OK)) {
+ /* Remove existing file */
if (unlink(m->filename))
rc = ACR_GET_OS_ERROR();
}
@@ -208,13 +208,10 @@
if (reqsize > ACR_SIZE_T_MAX) {
/* Guard against insane sizes */
- if (IS_VALID_HANDLE(_E)) {
- ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0);
- }
- ACR_SET_OS_ERROR(ACR_EINVAL);
+ ACR_THROW_EX_IF_ERR(ACR_EX_EINVAL, ACR_EINVAL);
return -1;
}
- shm = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_shm_t));
+ shm = ACR_CALLOC(acr_shm_t, 1);
if (!shm)
return -1;
/* Check if they want anonymous or name-based shared memory */
@@ -314,10 +311,8 @@
hdr.magic = ACR_SHM_MAGIC;
hdr.size = shm->reqsize;
hdr.length = shm->realsize;
- do {
- rc = write(file,(const void *)&hdr, nbytes);
- } while (rc == (acr_size_t)-1 && errno == EINTR);
- if (rc == -1) {
+
+ if (r_write(file, (const void *)&hdr, nbytes) < 0) {
rc = ACR_GET_OS_ERROR();
goto finally;
}
@@ -349,16 +344,16 @@
acr_size_t nbytes;
if (!filename) {
- ACR_SET_OS_ERROR(ACR_EINVAL);
+ ACR_THROW_IO_IF_ERR(ACR_EINVAL);
return -1;
}
- shm = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_shm_t));
+ shm = ACR_CALLOC(acr_shm_t, 1);
if (!shm)
return -1;
shm->filename = ACR_StrdupA(_E, THROW_FMARK, filename);
if (!shm->filename) {
rc = ACR_GET_OS_ERROR();
- free(shm);
+ x_free(shm);
ACR_SET_OS_ERROR(rc);
return -1;
}
@@ -371,9 +366,7 @@
nbytes = sizeof(memblock_t);
- do {
- rc = read(file, (void *)&hdr, nbytes);
- } while (rc == -1 && errno == EINTR);
+ rc = r_read(file, (void *)&hdr, nbytes);
if (rc != nbytes) {
/* Wrong format */
rc = EBADF;
@@ -382,6 +375,7 @@
}
rc = close(file);
if (rc) {
+ rc = ACR_GET_OS_ERROR();
goto finally;
}
if (hdr.magic != ACR_SHM_MAGIC) {
@@ -438,6 +432,7 @@
m->filename = NULL;
rc = acr_ioh_close(shm);
+
finally:
ACR_THROW_IO_IF_ERR(rc);
return rc;
@@ -458,8 +453,12 @@
rc = ACR_EBADF;
goto finally;
}
- shmbuf.shm_perm.uid = uid == -1 ? getuid() : uid;
- shmbuf.shm_perm.gid = gid == -1 ? getgid() : gid;
+ if (shmctl(m->shmid, IPC_STAT, &shmbuf) == -1) {
+ rc = ACR_GET_OS_ERROR();
+ goto finally;
+ }
+ shmbuf.shm_perm.uid = uid < 0 ? shmbuf.shm_perm.uid : uid;
+ shmbuf.shm_perm.gid = gid < 0 ? shmbuf.shm_perm.gid : gid;
shmbuf.shm_perm.mode = ACR_UnixPermsToMode(perms);
if (shmctl(m->shmid, IPC_SET, &shmbuf) == -1) {
rc = ACR_GET_OS_ERROR();
@@ -551,7 +550,7 @@
return NULL;
}
/* Create Descriptor Object */
- shmd = ACR_DescriptorCreate(_E, ACR_DT_USER, ishm, NULL,
+ shmd = ACR_DescriptorCreate(_E, ACR_DT_SHM, ishm, NULL,
shm_descriptor_cleanup);
if (!shmd) {
@@ -574,7 +573,7 @@
return NULL;
}
/* Create Descriptor Object */
- shmd = ACR_DescriptorCreate(_E, ACR_DT_USER, ishm, NULL,
+ shmd = ACR_DescriptorCreate(_E, ACR_DT_SHM, ishm, NULL,
shm_descriptor_cleanup);
if (!shmd) {
Modified: commons/sandbox/runtime/trunk/src/main/native/os/linux/selinux.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/linux/selinux.c?rev=819176&r1=819175&r2=819176&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/linux/selinux.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/linux/selinux.c Sat Sep 26 17:06:40 2009
@@ -23,6 +23,9 @@
#if HAVE_SELINUX_H
#include <selinux/selinux.h>
+/* TODO: Use dlsym for selinux API so that we don't
+ * have reference to the libselinux
+ */
ACR_JNI_PLATFORM_DECLARE(jboolean, SELinux, enabled0)(ACR_JNISTDARGS)
{
int rc;
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/mutex.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/mutex.c?rev=819176&r1=819175&r2=819176&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/mutex.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/mutex.c Sat Sep 26 17:06:40 2009
@@ -71,16 +71,224 @@
struct acr_pmutex_t {
int locked;
-#if defined(ACR_USE_SYSV_MUTEX)
- int filedes;
- const char *filename; /* NULL if anonymous */
-#else
+#if defined(ACR_USE_POSIX_MUTEX)
sem_t *sem;
char filename[NAME_MAX];
+#else /* System V semaphore */
+ int filedes;
+ const char *filename; /* NULL if anonymous */
#endif
};
-#if defined(ACR_USE_SYSV_MUTEX)
+#if defined(ACR_USE_POSIX_MUTEX)
+
+static int mutex_cleanup(void *mutex, int type, unsigned int flags)
+{
+ if (type == ACR_DT_MUTEX) {
+ int rc = 0;
+ acr_pmutex_t *m = (acr_pmutex_t *)mutex;
+ if (m->filename[0])
+ sem_unlink(m->filename);
+ if (m->sem != (sem_t *)SEM_FAILED) {
+ if (sem_close(m->sem) < 0)
+ rc = ACR_GET_OS_ERROR();
+ }
+ else
+ rc = ACR_EBADF;
+ x_free(m);
+ return rc;
+ }
+ return ACR_EFTYPE;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexCreate(JNIEnv *_E, const acr_pchar_t *name)
+{
+ int rc = 0;
+ acr_pmutex_t *m;
+ char *p;
+
+ m = ACR_CALLOC(acr_pmutex_t, 1);
+ if (!m)
+ return -1;
+ if (!name)
+ sprintf(m->filename, "/AcM.%08x", _mtx_counter++);
+ else {
+ if (*name != '/')
+ m->filename[0] = '/';
+ strlcat(m->filename, name, NAME_MAX - 2);
+ }
+ for (p = &m->filename[1]; *p; p++) {
+ if (*p == '/')
+ *p = '_';
+ }
+ do {
+ m->sem = sem_open(m->filename, O_CREAT | O_EXCL, 0660, 1);
+ if (m->sem == (sem_t *)SEM_FAILED) {
+ if (rc)
+ goto finally;
+ rc = ACR_GET_OS_ERROR();
+ if (errno == ENAMETOOLONG) {
+ m->filename[13] = '\0';
+ continue;
+ } else if (errno == EEXIST && !name) {
+ sprintf(m->filename, "/AcM.%08x", _mtx_counter++);
+ rc = 0;
+ continue;
+ }
+ }
+ } while (rc != 0);
+
+ if (!name) {
+ /*
+ * Unlink the semaphore immediately, so it can't be accessed externally.
+ * However this prevents calling attach.
+ */
+ sem_unlink(m->filename);
+ m->filename[0] = '\0';
+ }
+ m->locked = 0;
+
+finally:
+ if (rc) {
+ x_free(m);
+ ACR_THROW_IO_IF_ERR(rc);
+ return -1;
+ }
+ else {
+ rc = acr_ioh_open(m, ACR_DT_MUTEX, 0, mutex_cleanup);
+ return rc;
+ }
+}
+
+ACR_DECLARE(int) ACR_ProcMutexAttach(JNIEnv *_E, const acr_pchar_t *name)
+{
+ int rc = 0;
+ acr_pmutex_t *m = NULL;
+ char *p;
+
+ if (!name) {
+ rc = ACR_EINVAL;
+ goto finally;
+ }
+ m = ACR_CALLOC(acr_pmutex_t, 1);
+ if (!m)
+ return -1;
+ if (*name != '/')
+ m->filename[0] = '/';
+ strlcat(m->filename, name, NAME_MAX - 2);
+ for (p = &m->filename[1]; *p; p++) {
+ if (*p == '/')
+ *p = '_';
+ }
+ m->sem = sem_open(m->filename, O_RDWR);
+ if (m->sem == (sem_t *)SEM_FAILED) {
+ rc = ACR_GET_OS_ERROR();
+ goto finally;
+ }
+ m->filename[0] = '\0';
+ m->locked = 0;
+finally:
+ if (rc) {
+ x_free(m);
+ ACR_THROW_IO_IF_ERR(rc);
+ return -1;
+ }
+ else {
+ rc = acr_ioh_open(m, ACR_DT_MUTEX, 1, mutex_cleanup);
+ return rc;
+ }
+}
+
+ACR_DECLARE(int) ACR_ProcMutexLock(JNIEnv *_E, int mutex)
+{
+ int rc;
+ acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
+
+ if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
+ return ACR_EFTYPE;
+ }
+ if (IS_INVALID_HANDLE(m)) {
+ return ACR_EBADF;
+ }
+ do {
+ rc = sem_wait(m->sem);
+ } while (rc < 0 && errno == EINTR);
+
+ if (rc < 0)
+ return ACR_GET_OS_ERROR();
+ m->locked = 1;
+
+ return ACR_SUCCESS;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexTryLock(JNIEnv *_E, int mutex)
+{
+ int rc;
+ acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
+
+ if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
+ return ACR_EFTYPE;
+ }
+ if (IS_INVALID_HANDLE(m)) {
+ return ACR_EBADF;
+ }
+ do {
+ rc = sem_trywait(m->sem);
+ } while (rc < 0 && errno == EINTR);
+ if (rc < 0) {
+ if (errno == EAGAIN)
+ return ACR_EBUSY;
+ else
+ return ACR_GET_OS_ERROR();
+ }
+ m->locked = 1;
+
+ return ACR_SUCCESS;;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexRelease(JNIEnv *_E, int mutex)
+{
+ int rc;
+ acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
+
+ if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
+ return ACR_EFTYPE;
+ }
+ if (IS_INVALID_HANDLE(m)) {
+ return ACR_EBADF;
+ }
+
+ m->locked = 0;
+ do {
+ rc = sem_post(m->sem);
+ } while (rc < 0 && errno == EINTR);
+
+ if (rc < 0)
+ return ACR_GET_OS_ERROR();
+ else
+ return ACR_SUCCESS;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexPermSet(JNIEnv *_E, int mutex, int perms,
+ acr_uid_t uid, acr_uid_t gid)
+{
+ acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
+
+ if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
+ return ACR_EFTYPE;
+ }
+ if (IS_INVALID_HANDLE(m)) {
+ return ACR_EBADF;
+ }
+ return ACR_ENOTIMPL;
+}
+
+ACR_DECLARE(int) ACR_ProcMutexRemove(JNIEnv *_E, const acr_pchar_t *filename)
+{
+ return ACR_ENOTIMPL;
+}
+
+#else /* ACR_USE_POSIX_MUTEX */
static int mutex_owner_cleanup(void *mutex, int type, unsigned int flags)
{
@@ -380,10 +588,13 @@
if (IS_INVALID_HANDLE(m)) {
return ACR_EBADF;
}
- buf.sem_perm.uid = uid;
- buf.sem_perm.gid = gid;
- buf.sem_perm.mode = ACR_UnixPermsToMode(perms);
ick.buf = &buf;
+ if (semctl(m->filedes, 0, IPC_STAT, ick) < 0) {
+ return ACR_GET_OS_ERROR();
+ }
+ buf.sem_perm.uid = uid < 0 ? buf.sem_perm.uid : uid;
+ buf.sem_perm.gid = gid < 0 ? buf.sem_perm.gid : gid;
+ buf.sem_perm.mode = ACR_UnixPermsToMode(perms);
if (semctl(m->filedes, 0, IPC_SET, ick) < 0) {
return ACR_GET_OS_ERROR();
@@ -436,215 +647,7 @@
return rc;
}
-#else /* ACR_USE_SYSV_MUTEX */
-
-static int mutex_cleanup(void *mutex, int type, unsigned int flags)
-{
- if (type == ACR_DT_MUTEX) {
- int rc = 0;
- acr_pmutex_t *m = (acr_pmutex_t *)mutex;
- if (m->filename[0])
- sem_unlink(m->filename);
- if (m->sem != (sem_t *)SEM_FAILED) {
- if (sem_close(m->sem) < 0)
- rc = ACR_GET_OS_ERROR();
- }
- else
- rc = ACR_EBADF;
- x_free(m);
- return rc;
- }
- return ACR_EFTYPE;
-}
-
-ACR_DECLARE(int) ACR_ProcMutexCreate(JNIEnv *_E, const acr_pchar_t *name)
-{
- int rc = 0;
- acr_pmutex_t *m;
- char *p;
-
- m = ACR_CALLOC(acr_pmutex_t, 1);
- if (!m)
- return -1;
- if (!name)
- sprintf(m->filename, "/AcM.%08x", _mtx_counter++);
- else {
- if (*name != '/')
- m->filename[0] = '/';
- strlcat(m->filename, name, NAME_MAX - 2);
- }
- for (p = &m->filename[1]; *p; p++) {
- if (*p == '/')
- *p = '_';
- }
- do {
- m->sem = sem_open(m->filename, O_CREAT | O_EXCL, 0660, 1);
- if (m->sem == (sem_t *)SEM_FAILED) {
- if (rc)
- goto finally;
- rc = ACR_GET_OS_ERROR();
- if (errno == ENAMETOOLONG) {
- m->filename[13] = '\0';
- continue;
- } else if (errno == EEXIST && !name) {
- sprintf(m->filename, "/AcM.%08x", _mtx_counter++);
- rc = 0;
- continue;
- }
- }
- } while (rc != 0);
-
- if (!name) {
- /*
- * Unlink the semaphore immediately, so it can't be accessed externally.
- * However this prevents calling attach.
- */
- sem_unlink(m->filename);
- m->filename[0] = '\0';
- }
- m->locked = 0;
-
-finally:
- if (rc) {
- x_free(m);
- ACR_THROW_IO_IF_ERR(rc);
- return -1;
- }
- else {
- rc = acr_ioh_open(m, ACR_DT_MUTEX, 0, mutex_cleanup);
- return rc;
- }
-}
-
-ACR_DECLARE(int) ACR_ProcMutexAttach(JNIEnv *_E, const acr_pchar_t *name)
-{
- int rc = 0;
- acr_pmutex_t *m = NULL;
- char *p;
-
- if (!name) {
- rc = ACR_EINVAL;
- goto finally;
- }
- m = ACR_CALLOC(acr_pmutex_t, 1);
- if (!m)
- return -1;
- if (*name != '/')
- m->filename[0] = '/';
- strlcat(m->filename, name, NAME_MAX - 2);
- for (p = &m->filename[1]; *p; p++) {
- if (*p == '/')
- *p = '_';
- }
- m->sem = sem_open(m->filename, O_RDWR);
- if (m->sem == (sem_t *)SEM_FAILED) {
- rc = ACR_GET_OS_ERROR();
- goto finally;
- }
- m->filename[0] = '\0';
- m->locked = 0;
-finally:
- if (rc) {
- x_free(m);
- ACR_THROW_IO_IF_ERR(rc);
- return -1;
- }
- else {
- rc = acr_ioh_open(m, ACR_DT_MUTEX, 1, mutex_cleanup);
- return rc;
- }
-}
-
-ACR_DECLARE(int) ACR_ProcMutexLock(JNIEnv *_E, int mutex)
-{
- int rc;
- acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
-
- if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
- return ACR_EFTYPE;
- }
- if (IS_INVALID_HANDLE(m)) {
- return ACR_EBADF;
- }
- do {
- rc = sem_wait(m->sem);
- } while (rc < 0 && errno == EINTR);
-
- if (rc < 0)
- return ACR_GET_OS_ERROR();
- m->locked = 1;
-
- return ACR_SUCCESS;
-}
-
-ACR_DECLARE(int) ACR_ProcMutexTryLock(JNIEnv *_E, int mutex)
-{
- int rc;
- acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
-
- if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
- return ACR_EFTYPE;
- }
- if (IS_INVALID_HANDLE(m)) {
- return ACR_EBADF;
- }
- do {
- rc = sem_trywait(m->sem);
- } while (rc < 0 && errno == EINTR);
- if (rc < 0) {
- if (errno == EAGAIN)
- return ACR_EBUSY;
- else
- return ACR_GET_OS_ERROR();
- }
- m->locked = 1;
-
- return ACR_SUCCESS;;
-}
-
-ACR_DECLARE(int) ACR_ProcMutexRelease(JNIEnv *_E, int mutex)
-{
- int rc;
- acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
-
- if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
- return ACR_EFTYPE;
- }
- if (IS_INVALID_HANDLE(m)) {
- return ACR_EBADF;
- }
-
- m->locked = 0;
- do {
- rc = sem_post(m->sem);
- } while (rc < 0 && errno == EINTR);
-
- if (rc < 0)
- return ACR_GET_OS_ERROR();
- else
- return ACR_SUCCESS;
-}
-
-ACR_DECLARE(int) ACR_ProcMutexPermSet(JNIEnv *_E, int mutex, int perms,
- acr_uid_t uid, acr_uid_t gid)
-{
- acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH_FDATA(mutex);
-
- if (ACR_IOH_FTYPE(mutex) != ACR_DT_MUTEX) {
- return ACR_EFTYPE;
- }
- if (IS_INVALID_HANDLE(m)) {
- return ACR_EBADF;
- }
- return ACR_ENOTIMPL;
-}
-
-ACR_DECLARE(int) ACR_ProcMutexRemove(JNIEnv *_E, const acr_pchar_t *filename)
-{
- return ACR_ENOTIMPL;
-}
-
-#endif /* ACR_USE_SYSV_MUTEX */
+#endif /* ACR_USE_POSIX_MUTEX */
/* Java API
*/
Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c?rev=819176&r1=819175&r2=819176&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/shm.c Sat Sep 26 17:06:40 2009
@@ -423,7 +423,6 @@
{
int rc = 0;
struct shmid_ds shmbuf;
- int shmid;
acr_shm_t *m = (acr_shm_t *)ACR_IOH_FDATA(shm);
if (ACR_IOH_FTYPE(shm) != ACR_DT_SHM) {
@@ -434,14 +433,14 @@
rc = ACR_EBADF;
goto finally;
}
- if ((shmid = shmget(m->shmkey, 0, SHM_R | SHM_W)) == -1) {
+ if (shmctl(m->shmid, IPC_STAT, &shmbuf) == -1) {
rc = ACR_GET_OS_ERROR();
goto finally;
}
- shmbuf.shm_perm.uid = uid;
- shmbuf.shm_perm.gid = gid;
+ shmbuf.shm_perm.uid = uid < 0 ? shmbuf.shm_perm.uid : uid;
+ shmbuf.shm_perm.gid = gid < 0 ? shmbuf.shm_perm.gid : gid;
shmbuf.shm_perm.mode = ACR_UnixPermsToMode(perms);
- if (shmctl(shmid, IPC_SET, &shmbuf) == -1) {
+ if (shmctl(m->shmid, IPC_SET, &shmbuf) == -1) {
rc = ACR_GET_OS_ERROR();
goto finally;
}
Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=819176&r1=819175&r2=819176&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Sat Sep 26 17:06:40 2009
@@ -843,6 +843,12 @@
return ACR_ProcMutexRelease(_E, m);
}
+ACR_JNI_EXPORT_DECLARE(jint, TestPrivate, test078)(ACR_JNISTDARGS, jint m,
+ jint p)
+{
+ return ACR_ProcMutexPermSet(_E, m, p, -1, -1);
+}
+
#if defined(WIN32)
static const wchar_t *sem_name = L"TestSemaphore";
#else
Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java?rev=819176&r1=819175&r2=819176&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestPrivate.java Sat Sep 26 17:06:40 2009
@@ -125,6 +125,7 @@
private static native int test075(int s);
private static native int test076(int s);
private static native int test077(int s);
+ private static native int test078(int s, int p);
protected void setUp()
throws Exception
@@ -1095,5 +1096,19 @@
rv = test073(sa);
assertEquals("Destroy Named Process Mutex", 0, rv);
}
+ public void testPmutexPerms()
+ throws Throwable
+ {
+ int rv;
+ int sa = test072(0);
+ assertTrue("Named Process Mutex", sa > 0);
+ int sc = test078(sa, 0666);
+ assertTrue("Named Process Mutex perms", sc == 0);
+
+ // Destory mutex
+ rv = test073(sa);
+ assertEquals("Destroy Named Process Mutex", 0, rv);
+ }
+
}