implemented EnterCriticalPolicySection() and LeaveCriticalPolicySection()
[reactos.git] / reactos / lib / userenv / gpolicy.c
index 31b359c..2d93964 100644 (file)
@@ -44,7 +44,9 @@ typedef enum
 } GP_ACTION;
 
 static const WCHAR szLocalGPApplied[] = L"userenv: User Group Policy has been applied";
+static const WCHAR szLocalGPMutex[] = L"userenv: user policy mutex";
 static const WCHAR szMachineGPApplied[] = L"Global\\userenv: Machine Group Policy has been applied";
+static const WCHAR szMachineGPMutex[] = L"Global\\userenv: machine policy mutex";
 
 static CRITICAL_SECTION GPNotifyLock;
 static PGP_NOTIFY NotificationList = NULL;
@@ -249,7 +251,7 @@ GPNotificationThreadProc(IN LPVOID lpParameter)
 
 static HANDLE
 CreateGPEvent(IN BOOL bMachine,
-              IN PVOID lpSecurityDescriptor)
+              IN PSECURITY_DESCRIPTOR lpSecurityDescriptor)
 {
     HANDLE hEvent;
     SECURITY_ATTRIBUTES SecurityAttributes;
@@ -258,7 +260,7 @@ CreateGPEvent(IN BOOL bMachine,
     SecurityAttributes.lpSecurityDescriptor = lpSecurityDescriptor;
     SecurityAttributes.bInheritHandle = FALSE;
 
-    hEvent = CreateEventW((lpSecurityDescriptor != NULL ? &SecurityAttributes : NULL),
+    hEvent = CreateEventW(&SecurityAttributes,
                           TRUE,
                           FALSE,
                           (bMachine ? szMachineGPApplied : szLocalGPApplied));
@@ -271,7 +273,7 @@ RegisterGPNotification(IN HANDLE hEvent,
                        IN BOOL bMachine)
 {
     PGP_NOTIFY Notify;
-    PVOID lpSecurityDescriptor = NULL;
+    PSECURITY_DESCRIPTOR lpSecurityDescriptor = NULL;
     BOOL Ret = FALSE;
 
     EnterCriticalSection(&GPNotifyLock);
@@ -292,7 +294,11 @@ RegisterGPNotification(IN HANDLE hEvent,
     /* create or open the machine group policy event */
     if (hMachineGPAppliedEvent == NULL)
     {
-        lpSecurityDescriptor = CreateDefaultSD();
+        lpSecurityDescriptor = CreateDefaultSecurityDescriptor();
+        if (lpSecurityDescriptor == NULL)
+        {
+            goto Cleanup;
+        }
 
         hMachineGPAppliedEvent = CreateGPEvent(TRUE,
                                                lpSecurityDescriptor);
@@ -307,7 +313,11 @@ RegisterGPNotification(IN HANDLE hEvent,
     {
         if (lpSecurityDescriptor == NULL)
         {
-            lpSecurityDescriptor = CreateDefaultSD();
+            lpSecurityDescriptor = CreateDefaultSecurityDescriptor();
+            if (lpSecurityDescriptor == NULL)
+            {
+                goto Cleanup;
+            }
         }
 
         hLocalGPAppliedEvent = CreateGPEvent(FALSE,
@@ -401,3 +411,57 @@ UnregisterGPNotification(IN HANDLE hEvent)
 
     return Ret;
 }
+
+HANDLE WINAPI
+EnterCriticalPolicySection(IN BOOL bMachine)
+{
+    SECURITY_ATTRIBUTES SecurityAttributes;
+    PSECURITY_DESCRIPTOR lpSecurityDescriptor;
+    HANDLE hSection;
+
+    /* create or open the mutex */
+    lpSecurityDescriptor = CreateDefaultSecurityDescriptor();
+    if (lpSecurityDescriptor == NULL)
+    {
+        return NULL;
+    }
+
+    SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
+    SecurityAttributes.lpSecurityDescriptor = lpSecurityDescriptor;
+    SecurityAttributes.bInheritHandle = FALSE;
+
+    hSection = CreateMutexW(&SecurityAttributes,
+                            FALSE,
+                            (bMachine ? szMachineGPMutex : szLocalGPMutex));
+
+    if (hSection != NULL)
+    {
+        /* wait up to 10 seconds */
+        if (WaitForSingleObject(hSection,
+                                60000) != WAIT_FAILED)
+        {
+            return hSection;
+        }
+
+        CloseHandle(hSection);
+    }
+
+    return NULL;
+}
+
+BOOL WINAPI
+LeaveCriticalPolicySection(IN HANDLE hSection)
+{
+    BOOL Ret;
+
+    if (hSection == NULL)
+    {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
+
+    Ret = ReleaseMutex(hSection);
+    CloseHandle(hSection);
+
+    return Ret;
+}