fixed a memory leak in EnterCriticalPolicySection()
[reactos.git] / reactos / lib / userenv / gpolicy.c
index 2d93964..26cd254 100644 (file)
@@ -45,8 +45,12 @@ typedef enum
 
 static const WCHAR szLocalGPApplied[] = L"userenv: User Group Policy has been applied";
 static const WCHAR szLocalGPMutex[] = L"userenv: user policy mutex";
+static const WCHAR szLocalGPRefreshEvent[] = L"userenv: user policy refresh event";
+static const WCHAR szLocalGPForceRefreshEvent[] = L"userenv: user policy force refresh event";
 static const WCHAR szMachineGPApplied[] = L"Global\\userenv: Machine Group Policy has been applied";
 static const WCHAR szMachineGPMutex[] = L"Global\\userenv: machine policy mutex";
+static const WCHAR szMachineGPRefreshEvent[] = L"Global\\userenv: machine policy refresh event";
+static const WCHAR szMachineGPForceRefreshEvent[] = L"Global\\userenv: machine policy force refresh event";
 
 static CRITICAL_SECTION GPNotifyLock;
 static PGP_NOTIFY NotificationList = NULL;
@@ -412,6 +416,58 @@ UnregisterGPNotification(IN HANDLE hEvent)
     return Ret;
 }
 
+BOOL WINAPI
+RefreshPolicy(IN BOOL bMachine)
+{
+    HANDLE hEvent;
+    BOOL Ret = TRUE;
+
+    hEvent = OpenEventW(EVENT_MODIFY_STATE,
+                        FALSE,
+                        (bMachine ? szMachineGPRefreshEvent : szLocalGPRefreshEvent));
+    if (hEvent != NULL)
+    {
+        Ret = SetEvent(hEvent);
+        CloseHandle(hEvent);
+    }
+
+    /* return TRUE even if the mutex doesn't exist! */
+    return Ret;
+}
+
+BOOL WINAPI
+RefreshPolicyEx(IN BOOL bMachine,
+                IN DWORD dwOptions)
+{
+    if (dwOptions & ~RP_FORCE)
+    {
+        SetLastError(ERROR_INVALID_PARAMETER);
+        return FALSE;
+    }
+
+    if (dwOptions & RP_FORCE)
+    {
+        HANDLE hEvent;
+        BOOL Ret = TRUE;
+
+        hEvent = OpenEventW(EVENT_MODIFY_STATE,
+                            FALSE,
+                            (bMachine ? szMachineGPForceRefreshEvent : szLocalGPForceRefreshEvent));
+        if (hEvent != NULL)
+        {
+            Ret = SetEvent(hEvent);
+            CloseHandle(hEvent);
+        }
+
+        /* return TRUE even if the mutex doesn't exist! */
+        return Ret;
+    }
+    else
+    {
+        return RefreshPolicy(bMachine);
+    }
+}
+
 HANDLE WINAPI
 EnterCriticalPolicySection(IN BOOL bMachine)
 {
@@ -421,29 +477,29 @@ EnterCriticalPolicySection(IN BOOL bMachine)
 
     /* create or open the mutex */
     lpSecurityDescriptor = CreateDefaultSecurityDescriptor();
-    if (lpSecurityDescriptor == NULL)
+    if (lpSecurityDescriptor != NULL)
     {
-        return NULL;
-    }
+        SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
+        SecurityAttributes.lpSecurityDescriptor = lpSecurityDescriptor;
+        SecurityAttributes.bInheritHandle = FALSE;
 
-    SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES);
-    SecurityAttributes.lpSecurityDescriptor = lpSecurityDescriptor;
-    SecurityAttributes.bInheritHandle = FALSE;
+        hSection = CreateMutexW(&SecurityAttributes,
+                                FALSE,
+                                (bMachine ? szMachineGPMutex : szLocalGPMutex));
 
-    hSection = CreateMutexW(&SecurityAttributes,
-                            FALSE,
-                            (bMachine ? szMachineGPMutex : szLocalGPMutex));
+        LocalFree((HLOCAL)lpSecurityDescriptor);
 
-    if (hSection != NULL)
-    {
-        /* wait up to 10 seconds */
-        if (WaitForSingleObject(hSection,
-                                60000) != WAIT_FAILED)
+        if (hSection != NULL)
         {
-            return hSection;
-        }
+            /* wait up to 10 seconds */
+            if (WaitForSingleObject(hSection,
+                                    60000) != WAIT_FAILED)
+            {
+                return hSection;
+            }
 
-        CloseHandle(hSection);
+            CloseHandle(hSection);
+        }
     }
 
     return NULL;