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 2010/01/08 11:22:47 UTC

svn commit: r897157 - in /commons/sandbox/runtime/trunk/src/main/native: include/arch/windows/acr_arch_private.h os/win32/main.c os/win32/posix.c os/win32/wusec.c

Author: mturk
Date: Fri Jan  8 10:22:33 2010
New Revision: 897157

URL: http://svn.apache.org/viewvc?rev=897157&view=rev
Log:
Cache current user token

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
    commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/posix.c
    commons/sandbox/runtime/trunk/src/main/native/os/win32/wusec.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h?rev=897157&r1=897156&r2=897157&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/arch/windows/acr_arch_private.h Fri Jan  8 10:22:33 2010
@@ -143,6 +143,9 @@
     } else ACR_ThrowException(_E, THROW_FMARK, ACR_EX_ENULL, 0)
 
 
+void acr_thread_lock(void);
+void acr_thread_unlock(void);
+
 /**
  * Local functions from wusec.c
  */

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c?rev=897157&r1=897156&r2=897157&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/main.c Fri Jan  8 10:22:33 2010
@@ -36,7 +36,9 @@
 static WCHAR            dos_file_name[ACR_SBUFF_SIZ];
 static DWORD            dll_tls_index = TLS_OUT_OF_INDEXES;
 HANDLE                  dll_heap_handle = NULL;
+HANDLE                  dll_user_token  = NULL;
 
+static CRITICAL_SECTION dll_main_lock;
 static SYSTEM_INFO      osinf;
 static OSVERSIONINFOEXA osver;
 
@@ -83,7 +85,8 @@
     acr_SignalsCleanup();
     ACR_DestroySecurityDescriptorTable();
     HeapDestroy(dll_heap_handle);
-
+    if (dll_user_token)
+        CloseHandle(dll_user_token);
 }
 
 BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
@@ -97,6 +100,8 @@
          */
         case DLL_PROCESS_ATTACH:
             dll_instance = instance;
+            if (!InitializeCriticalSectionAndSpinCount(&dll_main_lock, ACR_SPINCOUNT))
+                return FALSE;
             GetModuleFileNameW(instance, mod_file_name, ACR_HBUFF_LEN);
             GetLongPathNameW(mod_file_name, dll_file_name, ACR_SBUFF_LEN);
             GetShortPathNameW(dll_file_name, dos_file_name, ACR_SBUFF_LEN);
@@ -160,6 +165,7 @@
                  */
             }
             dll_instance = NULL;
+            DeleteCriticalSection(&dll_main_lock);
         break;
         default:
         break;
@@ -516,3 +522,14 @@
     return tlsd->env;
 }
 
+void acr_thread_lock()
+{
+    if (dll_instance)
+        EnterCriticalSection(&dll_main_lock);
+}
+
+void acr_thread_unlock()
+{
+    if (dll_instance)
+        LeaveCriticalSection(&dll_main_lock);
+}

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/posix.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/posix.c?rev=897157&r1=897156&r2=897157&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/posix.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/posix.c Fri Jan  8 10:22:33 2010
@@ -42,14 +42,18 @@
          */
         return ppid;
     }
+    acr_thread_lock();
+    if (ppid >= 0)
+        goto finally;
+
     snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
     if (IS_INVALID_HANDLE(snap))
-        return -1;
+        goto finally;
 
     e.dwSize = (DWORD)sizeof(PROCESSENTRY32W);
     if (!Process32FirstW(snap, &e)) {
         CloseHandle(snap);
-        return -1;
+        goto finally;
     }
     do {
         if (e.th32ProcessID == GetCurrentProcessId()) {
@@ -62,9 +66,17 @@
     } while (Process32NextW(snap, &e));
     CloseHandle(snap);
 
+finally:
+    acr_thread_unlock();
     return ppid;
 }
 
+/* Cached current process token.
+ * Once obtained it will never get destroyed
+ * for the DLL lifetime
+ */
+extern HANDLE dll_user_token;
+
 /* Get token of the currently logged on user
  * If pid is 0 the current user token is
  * duplicated.
@@ -130,11 +142,17 @@
         CloseHandle(snap);
     }
     if (psid != -1) {
-        HANDLE process;
+        HANDLE process = NULL;
         if (psid)
             process = OpenProcess(MAXIMUM_ALLOWED, FALSE, psid);
-        else
-            process = GetCurrentProcess();
+        else {
+            acr_thread_lock();
+            if (dll_user_token == NULL)
+                process = GetCurrentProcess();
+            else
+                ctok = dll_user_token;
+            acr_thread_unlock();
+        }
         if (process != NULL) {
             if (!OpenProcessToken(process,
                                   TOKEN_ADJUST_PRIVILEGES |
@@ -146,11 +164,19 @@
                                   &ctok)) {
                 /* Unable to open the process token
                  */
-                CloseHandle(process);
+                if (psid)
+                    CloseHandle(process);
                 return INVALID_HANDLE_VALUE;
             }
-            CloseHandle(process);
-
+            if (psid) {
+               CloseHandle(process);
+            }
+            else {
+                acr_thread_lock();
+                if (dll_user_token == NULL)
+                    dll_user_token = ctok;
+                acr_thread_unlock();
+            }
         }
     }
     if (IS_VALID_HANDLE(ctok)) {
@@ -166,8 +192,10 @@
         }
         else
             ptok = INVALID_HANDLE_VALUE;
-        CloseHandle(ctok);
+        if (ctok != dll_user_token)
+            CloseHandle(ctok);
     }
+
     return ptok;
 }
 

Modified: commons/sandbox/runtime/trunk/src/main/native/os/win32/wusec.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/win32/wusec.c?rev=897157&r1=897156&r2=897157&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/win32/wusec.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/win32/wusec.c Fri Jan  8 10:22:33 2010
@@ -584,6 +584,11 @@
     if (_null_sa)
         return _null_sa;
 
+    EnterCriticalSection(&security_lock);
+    if (_null_sa) {
+        LeaveCriticalSection(&security_lock);
+        return _null_sa;
+    }
     _null_sa  = (PSECURITY_ATTRIBUTES)LocalAlloc(LPTR, sizeof(SECURITY_ATTRIBUTES));
     _null_sa->nLength = sizeof(SECURITY_ATTRIBUTES);
 
@@ -604,6 +609,7 @@
     _null_sa->lpSecurityDescriptor = pSD;
     _null_sa->bInheritHandle       = FALSE;
 
+    LeaveCriticalSection(&security_lock);
     SetLastError(0);
     return _null_sa;
 
@@ -619,6 +625,7 @@
     _null_sa->lpSecurityDescriptor = NULL;
     _null_sa->bInheritHandle       = FALSE;
 
+    LeaveCriticalSection(&security_lock);
     SetLastError(rc);
     return _null_sa;
 }