commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r806301 - in /commons/sandbox/runtime/trunk/src/main/native/os/unix: pmutex.c psema.c
Date Thu, 20 Aug 2009 18:47:25 GMT
Author: mturk
Date: Thu Aug 20 18:47:25 2009
New Revision: 806301

URL: http://svn.apache.org/viewvc?rev=806301&view=rev
Log:
SysV semaphores cannot be named. Use posix semaphores

Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c?rev=806301&r1=806300&r2=806301&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/pmutex.c Thu Aug 20 18:47:25 2009
@@ -25,9 +25,11 @@
 #include "acr_pointer.h"
 #include "acr_procmutex.h"
 
-#include <sys/ipc.h>
-#include <sys/sem.h>
-#include <sys/file.h>
+#include <semaphore.h>
+
+#ifndef NAME_MAX
+#define NAME_MAX   (64)
+#endif
 
 J_DECLARE_CLAZZ = {
     NULL,
@@ -41,74 +43,79 @@
     "(L" ACR_CLASS_PATH "Descriptor;)V"
 };
 
-union semun {
-    int val;
-    struct semid_ds *buf;
-    unsigned short *array;
-};
+static volatile unsigned int _mtx_counter = 1;
 
 struct acr_pmutex_t {
-    int   filedes;
-    int   locked;
+    sem_t *sem;
+    int    locked;
+    char   name[NAME_MAX];
 };
 
 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->filedes > 0) {
-            union semun ick;
-            ick.val = 0;
-            semctl(m->filedes, 0, IPC_RMID, ick);
+        if (m->name[0])
+            sem_unlink(m->name);
+        if (m->sem != (sem_t *)SEM_FAILED) {
+            if (sem_close(m->sem) < 0)
+                rc = ACR_GET_OS_ERROR();
         }
         free(m);
-        return ACR_SUCCESS;
+        return rc;
     }
     return ACR_EBADF;
 }
 
-ACR_DECLARE(int) ACR_ProcMutexCreate(JNIEnv *_E, const acr_pchar_t *fname)
+ACR_DECLARE(int) ACR_ProcMutexCreate(JNIEnv *_E, const acr_pchar_t *name)
 {
-    union semun ick;
+    unsigned int ic = _mtx_counter;
     int rc = 0;
     acr_pmutex_t *m;
-    key_t mkey = IPC_PRIVATE;
-    int flags  = IPC_CREAT;
 
-    if (fname) {
-        mkey = ftok(fname, 1);
-        if (mkey == (key_t)-1) {
-            ACR_THROW_IO_ERRNO();
-            return -1;
-        }
-        flags |= IPC_EXCL;
-    }
     m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t));
     if (!m)
         return -1;
-    m->filedes = semget(mkey, 1, flags | 0600);
-
-    if (m->filedes < 0) {
-        if (fname && errno == EEXIST) {
-            /* XXX: Should we throw separate exception here?
-             */
-        }
-        rc =  ACR_GET_OS_ERROR();
-        goto finally;
+    if (!name)
+        sprintf(m->name, "/AcM.%06x%02x", ((unsigned int)getpid() & 0xFFFFFF),
+                                          _mtx_counter++);
+    else {
+        if (*name != '/')
+            m->name[0] = '/';
+        strlcat(m->name, name, NAME_MAX - 2);
     }
-    ick.val = 1;
-    if (semctl(m->filedes, 0, SETVAL, ick) < 0) {
-        rc =  ACR_GET_OS_ERROR();
-        goto finally;
+    do {
+        m->sem = sem_open(m->name, O_CREAT | O_EXCL, 0644, 1);
+        if (m->sem == (sem_t *)SEM_FAILED) {
+            if (rc)
+                goto finally;
+            rc = ACR_GET_OS_ERROR();
+            if (errno == ENAMETOOLONG) {
+                m->name[13] = '\0';
+                continue;
+            } else if (errno == EEXIST && !name) {
+                if ((_mtx_counter - ic) > 10)
+                    goto finally;
+                sprintf(m->name, "/AcM.%06x%02x",
+                        ((unsigned int)getpid() & 0xFFFFFF),
+                        _mtx_counter++);
+                rc = 0;
+            }
+        }
+    } while (rc != 0);
+
+    if (!name) {
+        /* 
+         * Unlink the semaphore immediately, so it can't be accessed externally.
+         * However this prevents calling attach.
+         */
+        sem_unlink(m->name);
+        m->name[0] = '\0';
     }
     m->locked = 0;
-
 finally:
     if (rc) {
-        if (m->filedes > 0) {
-            ick.val = 0;
-            semctl(m->filedes, 0, IPC_RMID, ick);
-        }
         free(m);
         ACR_THROW_IO_IF_ERR(rc);
         return -1;
@@ -119,47 +126,36 @@
     }
 }
 
-ACR_DECLARE(int) ACR_ProcMutexAttach(JNIEnv *_E, const acr_pchar_t *fname)
+ACR_DECLARE(int) ACR_ProcMutexAttach(JNIEnv *_E, const acr_pchar_t *name)
 {
-    union semun ick;
     int rc = 0;
-    acr_pmutex_t *m;
-    key_t mkey;
+    acr_pmutex_t *m = NULL;
 
-    if (!fname) {
-        /* Cannot attach to unnamed mutex */
-        ACR_THROW_IO_IF_ERR(ACR_EINVAL);
-        return -1;
-    }
-
-    mkey = ftok(fname, 1);
-    if (mkey == (key_t)-1) {
-        ACR_THROW_IO_ERRNO();
-        return -1;
+    if (!name) {
+        rc = ACR_EINVAL;
+        goto finally;
     }
     m = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_pmutex_t));
     if (!m)
         return -1;
-    m->filedes = semget(mkey, 1, 0);
-
-    if (m->filedes < 0) {
-        rc =  ACR_GET_OS_ERROR();
+    if (*name != '/')
+        m->name[0] = '/';
+    strlcat(m->name, name, NAME_MAX - 2);
+    m->sem = sem_open(m->name, O_RDWR);
+    if (m->sem == (sem_t *)SEM_FAILED) {
+        rc = ACR_GET_OS_ERROR();
         goto finally;
     }
-    m->locked = 0;
-
+    m->name[0] = '\0';
+    m->locked  = 0;
 finally:
     if (rc) {
-        if (m->filedes > 0) {
-            ick.val = 0;
-            semctl(m->filedes, 0, IPC_RMID, ick);
-        }
-        free(m);
+        x_free(m);
         ACR_THROW_IO_IF_ERR(rc);
         return -1;
     }
     else {
-        rc = acr_ioh_open(m, ACR_DT_MUTEX, 0, mutex_cleanup);
+        rc = acr_ioh_open(m, ACR_DT_MUTEX, 1, mutex_cleanup);
         return rc;
     }
 }
@@ -167,19 +163,14 @@
 ACR_DECLARE(int) ACR_ProcMutexLock(JNIEnv *_E, int mutex)
 {
     int rc;
-    struct sembuf op;
     acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH(mutex);
 
     if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) {
         ACR_SET_OS_ERROR(ACR_EINVAL);
         return -1;
     }
-    op.sem_num = 0;
-    op.sem_op  = -1;
-    op.sem_flg = SEM_UNDO;
-
     do {
-        rc = semop(m->filedes, &op, 1);
+        rc = sem_wait(m->sem);
     } while (rc < 0 && errno == EINTR);
 
     if (rc < 0)
@@ -192,29 +183,21 @@
 ACR_DECLARE(int) ACR_ProcMutexTryLock(JNIEnv *_E, int mutex)
 {
     int rc;
-    struct sembuf op;
     acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH(mutex);
 
     if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) {
         return ACR_EINVAL;
     }
-    op.sem_num = 0;
-    op.sem_op  = -1;
-    op.sem_flg = SEM_UNDO | IPC_NOWAIT;
-
     do {
-        rc = semop(m->filedes, &op, 1);
+        rc = sem_trywait(m->sem);
     } while (rc < 0 && errno == EINTR);
-
     if (rc < 0) {
-        if (errno == EAGAIN) {
+        if (errno == EAGAIN)
             return ACR_EBUSY;
-        }
         else
             return ACR_GET_OS_ERROR();
     }
-    else
-        m->locked = 1;
+    m->locked = 1;
 
     return ACR_SUCCESS;;
 }
@@ -222,19 +205,15 @@
 ACR_DECLARE(int) ACR_ProcMutexRelease(JNIEnv *_E, int mutex)
 {
     int rc;
-    struct sembuf op;
     acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH(mutex);
 
     if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) {
         return ACR_EINVAL;
     }
 
-    m->locked  = 0;
-    op.sem_num = 0;
-    op.sem_op  = 1;
-    op.sem_flg = SEM_UNDO;
+    m->locked = 0;
     do {
-        rc = semop(m->filedes, &op, 1);
+        rc = sem_post(m->sem);
     } while (rc < 0 && errno == EINTR);
 
     if (rc < 0)
@@ -246,23 +225,12 @@
 ACR_DECLARE(int) ACR_ProcMutexPermSet(JNIEnv *_E, int mutex, int perms,
                                       acr_uid_t uid, acr_uid_t gid)
 {
-    union  semun ick;
-    struct semid_ds buf;
     acr_pmutex_t *m = (acr_pmutex_t *)ACR_IOH(mutex);
 
     if (IS_INVALID_HANDLE(m) || ACR_IOH_TYPE(mutex) != ACR_DT_MUTEX) {
         return ACR_EINVAL;
     }
-    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_SET, ick) < 0) {
-        return ACR_GET_OS_ERROR();
-    }
-    else
-        return ACR_SUCCESS;
+    return ACR_ENOTIMPL;
 }
 
 ACR_DECLARE(int) ACR_ProcMutexClose(JNIEnv *_E, int mutex)

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c?rev=806301&r1=806300&r2=806301&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c Thu Aug 20 18:47:25 2009
@@ -145,7 +145,7 @@
         return -1;
     if (*name != '/')
         s->name[0] = '/';
-    strlcat(s->name, name, NAME_MAX -2);
+    strlcat(s->name, name, NAME_MAX - 2);
     s->sem = sem_open(s->name, O_RDWR);
     if (s->sem == (sem_t *)SEM_FAILED) {
         rc = ACR_GET_OS_ERROR();
@@ -189,24 +189,12 @@
 ACR_DECLARE(int) ACR_SemaphorePermSet(JNIEnv *_E, int sema, int perms,
                                       acr_uid_t uid, acr_uid_t gid)
 {
-    int rc = 0;
     acr_semaphore_t *s = (acr_semaphore_t *)ACR_IOH(sema);
 
     if (IS_INVALID_HANDLE(s) || ACR_IOH_TYPE(sema) != ACR_DT_SEMAPHORE) {
         return ACR_EINVAL;
     }
-    if (!s->name[0])
-        return ACR_EINVAL;
-
-    if (uid != (uid_t)-1 && gid != (gid_t)-1) {
-        if (chown(s->name, uid, gid) < 0)
-            rc = ACR_GET_OS_ERROR();
-    }
-    if (!rc) {
-        if (chmod(s->name, ACR_UnixPermsToMode(perms)) < 0)
-            rc = ACR_GET_OS_ERROR();
-    }
-    return rc;
+    return ACR_ENOTIMPL;
 }
 
 ACR_DECLARE(int) ACR_SemaphoreWait(JNIEnv *_E, int sema)



Mime
View raw message