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 2011/04/19 09:35:35 UTC

svn commit: r1094908 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/platform/windows/ native/os/win32/

Author: mturk
Date: Tue Apr 19 07:35:35 2011
New Revision: 1094908

URL: http://svn.apache.org/viewvc?rev=1094908&view=rev
Log:
Move more API from native to Java

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Security.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java
    commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Security.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Security.java?rev=1094908&r1=1094907&r2=1094908&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Security.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Security.java Tue Apr 19 07:35:35 2011
@@ -34,24 +34,10 @@ final class Security
     {
         // No instance.
     }
-
     private static ConcurrentHashMap<String,Long> sdcache = new ConcurrentHashMap<String,Long>();
 
-    public static final int         PROT_SCOPE_WORLD        = 0;
-    public static final int         PROT_SCOPE_GROUP        = 4;
-    public static final int         PROT_SCOPE_USER         = 8;
-
-    /** Object types */
-    public static final int         FILE                    = 0;
-    public static final int         MUTEX                   = 1;
-    public static final int         SHMEM                   = 3;
-    public static final int         SEMAPHORE               = 4;
-
-    /* Win32 API defines */
     public static final int         SDDL_REVISION_1         = 1;
 
-
-    public static native int        GetAccessMask(int objectType, int scope, int prot);
     public static native long       AllocateWellKnownSid(int sidType)
         throws SystemException;
     public static native long       ConvertStringSecurityDescriptorToSecurityDescriptor(String securityDescriptor, int revision)
@@ -64,16 +50,23 @@ final class Security
         throws SystemException;
 
 
-    public static long getStdSecurityDescriptor(int objectType, int mode)
+    public static long stdSecurityDescriptor(int adminAccess, int groupAccess, int otherAccess)
         throws SystemException
     {
-        int uacc = GetAccessMask(objectType, PROT_SCOPE_USER,  mode);
-        int gacc = GetAccessMask(objectType, PROT_SCOPE_GROUP, mode);
-        int wacc = GetAccessMask(objectType, PROT_SCOPE_WORLD, mode);
+        String aas = Utils.hex(adminAccess);
+        String gas = Utils.hex(groupAccess);
+        String oas = Utils.hex(otherAccess);
+
+        StringBuffer key = new StringBuffer(aas);
+        key.append(gas);
+        key.append(oas);
+        Long sd = sdcache.get(key.toString());
+        if (sd != null)
+            return sd.longValue();
 
         StringBuffer sdd = new StringBuffer("D:(D;OICI;GA;;;BG)(D;OICI;GA;;;AN)");
         StringBuffer saa = new StringBuffer("(A;OICI;0x");
-        saa.append(Utils.hex(uacc));
+        saa.append(aas);
         sdd.append(saa);
         sdd.append(";;;BA)");
         sdd.append(saa);
@@ -84,29 +77,29 @@ final class Security
         sdd.append(";;;SY)");
         sdd.append(saa);
         sdd.append(";;;SU)");
-        if (gacc != 0) {
+        if (groupAccess != 0) {
             sdd.append("(A;OICI;0x");
-            sdd.append(Utils.hex(gacc));
+            sdd.append(gas);
             sdd.append(";;;CG)");
         }
-        if (wacc != 0) {
+        if (otherAccess != 0) {
             sdd.append("(A;OICI;0x");
-            sdd.append(Utils.hex(wacc));
+            sdd.append(oas);
             sdd.append(";;;AU)");
         }
-        String desc = sdd.toString();
-        Long sd = sdcache.get(desc);
-        if (sd == null) {
-            long sh = ConvertStringSecurityDescriptorToSecurityDescriptor(desc, SDDL_REVISION_1);
-            if (sh == 0L) {
-                // Throw SystemException
-                throw new SystemException(Status.msg());
-            }
-            sd = new Long(sh);
-            sdcache.put(desc, sd);
+        long nd = ConvertStringSecurityDescriptorToSecurityDescriptor(sdd.toString(), SDDL_REVISION_1);
+        if (nd == 0L) {
+            // Throw SystemException
+            throw new SystemException(Status.msg());
         }
-        return sd.longValue();
+        sd = sdcache.put(key.toString(), new Long(nd));
+        if (sd != null) {
+            // Someone already put the value while we were calculating the
+            // security descriptor string.
+            Win32.LocalFree(nd);
+            nd = sd.longValue();
+        }
+        return nd;
     }
 
-
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java?rev=1094908&r1=1094907&r2=1094908&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/Win32.java Tue Apr 19 07:35:35 2011
@@ -73,6 +73,24 @@ final class Win32
     public static final int         SYNCHRONIZE             = 0x00100000;
     public static final int         WRITE_DAC               = 0x00040000;
     public static final int         WRITE_OWNER             = 0x00080000;
+    public static final int         MAXIMUM_ALLOWED         = 0x02000000;
+    public static final int         STANDARD_RIGHTS_REQUIRED= 0x000F0000;
+    public static final int         STANDARD_RIGHTS_READ    = READ_CONTROL;
+    public static final int         STANDARD_RIGHTS_WRITE   = READ_CONTROL;
+    public static final int         STANDARD_RIGHTS_EXECUTE = READ_CONTROL;
+    public static final int         STANDARD_RIGHTS_ALL     = 0x001F0000;
+    public static final int         SPECIFIC_RIGHTS_ALL     = 0x0000FFFF;
+
+    /* Generic rights           */
+    public static final int         GENERIC_READ            = 0x80000000;
+    public static final int         GENERIC_WRITE           = 0x40000000;
+    public static final int         GENERIC_EXECUTE         = 0x20000000;
+    public static final int         GENERIC_ALL             = 0x10000000;
+    
+    /* Generic cumulative rights */
+    public static final int         GENERIC_RWX             = 0xe0000000;
+    public static final int         GENERIC_RWR             = 0xc0000000;
+
     /* Mutex access rights      */
     public static final int         MUTEX_ALL_ACCESS        = 0x001F0001;
     public static final int         MUTEX_MODIFY_STATE      = 0x00000001;
@@ -80,6 +98,36 @@ final class Win32
     public static final int         SEMAPHORE_ALL_ACCESS    = 0x001F0003;
     public static final int         SEMAPHORE_MODIFY_STATE  = 0x00000002;
 
+    public static final int         SECTION_QUERY               = 0x0001;
+    public static final int         SECTION_MAP_WRITE           = 0x0002;
+    public static final int         SECTION_MAP_READ            = 0x0004;
+    public static final int         SECTION_MAP_EXECUTE         = 0x0008;
+    public static final int         SECTION_EXTEND_SIZE         = 0x0010;
+    /* not included in SECTION_ALL_ACCESS */
+    public static final int         SECTION_MAP_EXECUTE_EXPLICIT= 0x0020;
+    public static final int         SECTION_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED|
+                                                         SECTION_QUERY |
+                                                         SECTION_MAP_WRITE |
+                                                         SECTION_MAP_READ |
+                                                         SECTION_MAP_EXECUTE |
+                                                         SECTION_EXTEND_SIZE;
+    /* Some of the file access rights */         
+    public static final int         FILE_EXECUTE            = 0x00000020;
+    public static final int         FILE_READ_DATA          = 0x00000001;
+    public static final int         FILE_WRITE_DATA         = 0x00000002;
+    public static final int         FILE_READ_EA            = 0x00000008;
+    public static final int         FILE_WRITE_EA           = 0x00000010;
+    public static final int         FILE_READ_ATTRIBUTES    = 0x00000080;
+    public static final int         FILE_WRITE_ATTRIBUTES   = 0x00000100;
+
+    
+    public static final int         FILE_MAP_COPY           = SECTION_QUERY;
+    public static final int         FILE_MAP_WRITE          = SECTION_MAP_WRITE;
+    public static final int         FILE_MAP_READ           = SECTION_MAP_READ;
+    public static final int         FILE_MAP_ALL_ACCESS     = SECTION_ALL_ACCESS;
+    /* not included in FILE_MAP_ALL_ACCESS */
+    public static final int         FILE_MAP_EXECUTE        = SECTION_MAP_EXECUTE_EXPLICIT;
+
     /* Memory allocation flags  */
     public static final int         MEM_COMMIT              = 0x00001000;
     public static final int         MEM_RESERVE             = 0x00002000;

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java?rev=1094908&r1=1094907&r2=1094908&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/platform/windows/WindowsSemaphore.java Tue Apr 19 07:35:35 2011
@@ -37,8 +37,8 @@ final class WindowsSemaphore extends Sem
         // No Instance
     }
 
-    // OS semaphore handle
-    private long    sd;
+    // Semaphore HANDLE
+    private long    handle;
 
     private static native long create0(String name, int value, long sd)
         throws IllegalAccessException,
@@ -60,11 +60,10 @@ final class WindowsSemaphore extends Sem
         if (name == null)
             throw new NullPointerException();
         this.name = "Global\\" + name.replace('\\', '_');
-        long sa = Security.getStdSecurityDescriptor(Security.SEMAPHORE,
-                                                    Win32.FPROT_URWX |
-                                                    Win32.FPROT_GRWX |
-                                                    Win32.FPROT_WRW);
-        sd = create0(this.name, value, sa);
+        long sa = Security.stdSecurityDescriptor(Win32.SEMAPHORE_ALL_ACCESS   | Win32.GENERIC_RWX,
+                                                 Win32.SEMAPHORE_ALL_ACCESS   | Win32.GENERIC_RWX,
+                                                 Win32.SEMAPHORE_MODIFY_STATE | Win32.GENERIC_RWR);
+        handle = create0(this.name, value, sa);
         owner = true;
     }
 
@@ -77,16 +76,16 @@ final class WindowsSemaphore extends Sem
         if (name == null)
             throw new NullPointerException();
         this.name = "Global\\" + name.replace('\\', '_');
-        sd = open0(this.name);
+        handle = open0(this.name);
         owner = false;
     }
 
     public void acquire()
         throws SystemException
     {
-        if (sd == 0L)
+        if (handle == 0L)
             throw new ClosedDescriptorException();
-        int rc = Win32.WaitForSingleObject(sd, Win32.INFINITE);
+        int rc = Win32.WaitForSingleObject(handle, Win32.INFINITE);
         if (rc != 0)
             throw new SystemException(Status.describe(rc));
     }
@@ -94,9 +93,9 @@ final class WindowsSemaphore extends Sem
     public boolean tryAcquire()
         throws SystemException
     {
-        if (sd == 0L)
+        if (handle == 0L)
             throw new ClosedDescriptorException();
-        int rc = try0(sd);
+        int rc = try0(handle);
         if (rc == 0)
             return true;
         if (Status.IS_EBUSY(rc))
@@ -107,9 +106,9 @@ final class WindowsSemaphore extends Sem
     public void release()
         throws SystemException
     {
-        if (sd == 0L)
+        if (handle == 0L)
             throw new ClosedDescriptorException();
-        int rc = release0(sd);
+        int rc = release0(handle);
         if (rc != 0)
             throw new SystemException(Status.describe(rc));
     }
@@ -119,10 +118,10 @@ final class WindowsSemaphore extends Sem
     {
         boolean once = false;
 
-        if (sd == 0L)
+        if (handle == 0L)
             throw new ClosedDescriptorException();
         while (true) {
-            int rc = try0(sd);
+            int rc = try0(handle);
             if (rc == 0) {
                 once = true;
             }
@@ -142,12 +141,12 @@ final class WindowsSemaphore extends Sem
     public void close()
         throws SystemException
     {
-        if (sd == 0L)
+        if (handle == 0L)
             throw new ClosedDescriptorException();
-        int rc = Win32.CloseHandle(sd);
+        int rc = Win32.CloseHandle(handle);
         if (rc != 0)
             throw new SystemException(Status.describe(rc));
-        sd = 0L;
+        handle = 0L;
     }
 
     /**
@@ -160,8 +159,8 @@ final class WindowsSemaphore extends Sem
     protected final void finalize()
         throws Throwable
     {
-        if (sd != 0L)
-            Win32.CloseHandle(sd);
+        if (handle != 0L)
+            Win32.CloseHandle(handle);
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c?rev=1094908&r1=1094907&r2=1094908&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/security.c Tue Apr 19 07:35:35 2011
@@ -29,147 +29,6 @@
 static PSECURITY_ATTRIBUTES _null_SA[2] = { 0, 0 };
 static SECURITY_ATTRIBUTES  _zero_SA[2];
 
-static struct {
-    SE_OBJECT_TYPE krnl;
-    DWORD          a;
-    DWORD          x;
-    DWORD          w;
-    DWORD          r;
-} prot_types [] = {
-    { /* File */
-      SE_FILE_OBJECT,
-      FILE_ALL_ACCESS,
-      FILE_GENERIC_EXECUTE,
-      FILE_GENERIC_WRITE,
-      FILE_GENERIC_READ
-    },
-    { /* Mutex */
-      SE_KERNEL_OBJECT,
-      MUTEX_ALL_ACCESS,
-      MUTEX_ALL_ACCESS,
-      MUTEX_ALL_ACCESS,
-      MUTEX_MODIFY_STATE
-    },
-    { /* Shmem */
-      SE_KERNEL_OBJECT,
-      FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE,
-      FILE_MAP_EXECUTE,
-      FILE_MAP_WRITE | FILE_MAP_COPY,
-      FILE_MAP_READ  | FILE_MAP_COPY
-    },
-    { /* Semaphore */
-      SE_KERNEL_OBJECT,
-      SEMAPHORE_ALL_ACCESS,
-      SEMAPHORE_ALL_ACCESS,
-      SEMAPHORE_ALL_ACCESS,
-      SEMAPHORE_MODIFY_STATE
-    },
-    {
-      0,
-      0,
-      0,
-      0,
-      0
-    }
-};
-
-static struct {
-    SE_OBJECT_TYPE krnl;
-    DWORD          x;
-    DWORD          w;
-    DWORD          r;
-} accm_types [] = {
-    { /* File */
-      SE_FILE_OBJECT,
-      FILE_EXECUTE    | GENERIC_EXECUTE,
-      FILE_WRITE_DATA | GENERIC_WRITE,
-      FILE_READ_DATA  | GENERIC_READ
-    },
-    { /* Mutex */
-      SE_KERNEL_OBJECT,
-      MUTEX_ALL_ACCESS   | GENERIC_EXECUTE,
-      MUTEX_ALL_ACCESS   | GENERIC_WRITE,
-      MUTEX_MODIFY_STATE | GENERIC_READ
-    },
-    { /* Shmem */
-      SE_KERNEL_OBJECT,
-      FILE_MAP_EXECUTE | GENERIC_EXECUTE,
-      FILE_MAP_WRITE | FILE_MAP_COPY | GENERIC_WRITE,
-      FILE_MAP_READ  | FILE_MAP_COPY | GENERIC_READ
-    },
-    { /* Semaphore */
-      SE_KERNEL_OBJECT,
-      SEMAPHORE_ALL_ACCESS   | GENERIC_EXECUTE,
-      SEMAPHORE_ALL_ACCESS   | GENERIC_WRITE,
-      SEMAPHORE_MODIFY_STATE | GENERIC_READ
-    },
-    {
-      0,
-      0,
-      0,
-      0
-    }
-};
-
-#define PROT_SCOPE_WORLD    0
-#define PROT_SCOPE_GROUP    4
-#define PROT_SCOPE_USER     8
-
-static ACCESS_MASK convert_acc(int prot, int type, int scope)
-{
-    int i = type;
-    /* These choices are based on the single filesystem bit that controls
-     * the given behavior.  They are -not- recommended for any set protection
-     * function, such a function should -set- use GENERIC_READ/WRITE/EXECUTE
-     */
-    ACCESS_MASK acc = 0;
-    prot = (prot >> scope) & 0x0F;
-
-    if (ACR_HAS(prot, ACR_FPROT_WEXECUTE))
-        acc |= GENERIC_EXECUTE;
-    if (ACR_HAS(prot, ACR_FPROT_WWRITE))
-        acc |= GENERIC_WRITE;
-    if (ACR_HAS(prot, ACR_FPROT_WREAD))
-        acc |= GENERIC_READ;
-
-    if ((prot & 0x07) == (ACR_FPROT_WEXECUTE | ACR_FPROT_WWRITE | ACR_FPROT_WREAD)) {
-        acc |= prot_types[i].a;
-    }
-    else {
-        if (ACR_HAS(prot, ACR_FPROT_WEXECUTE))
-            acc |= prot_types[i].x;
-        if (ACR_HAS(prot, ACR_FPROT_WWRITE))
-            acc |= prot_types[i].w;
-        if (ACR_HAS(prot, ACR_FPROT_WREAD))
-            acc |= prot_types[i].r;
-    }
-    return acc;
-}
-
-static int convert_prot(ACCESS_MASK acc, int type, int scope)
-{
-    int i    = type;
-    int prot = 0;
-
-    /* These choices are based on the single filesystem bit that controls
-     * the given behavior.  They are -not- recommended for any set protection
-     * function, such a function should -set- use GENERIC_READ/WRITE/EXECUTE
-     */
-    if (ACR_HAS(acc, accm_types[i].x))
-        prot |= ACR_FPROT_WEXECUTE;
-    if (ACR_HAS(acc, accm_types[i].w))
-        prot |= ACR_FPROT_WWRITE;
-    if (ACR_HAS(acc, accm_types[i].r))
-        prot |= ACR_FPROT_WREAD;
-
-    return (prot << scope);
-}
-
-ACR_WIN_EXPORT(jint, Security, GetAccessMask)(JNI_STDARGS, jint type, jint scope, jint prot)
-{
-    return convert_acc(prot, type, scope);
-}
-
 ACR_WIN_EXPORT(jlong, Security, AllocateWellKnownSid)(JNI_STDARGS, jint type)
 {
     DWORD ss = SECURITY_MAX_SID_SIZE;