internal void PreMountCopy(Guid mdbGuid, bool isPublicMdb) { RegistryReplicator registryReplicator = null; RealRegistry realRegistry = null; ClusterRegistry clusterRegistry = null; AmConfig config = AmSystemManager.Instance.Config; if (config.IsPamOrSam) { string text = isPublicMdb ? RegistryMonitor.publicString : RegistryMonitor.privateString; string text2 = string.Format(RegistryMonitor.localRegistryPathFormat, Environment.MachineName, mdbGuid, text); string root = string.Format(RegistryMonitor.clusterRegistryPathFormat, mdbGuid, text); try { if (this.replicators.ContainsKey(mdbGuid)) { this.replicators[mdbGuid].DisableCopy(); } SafeRegistryHandle handle; DiagnosticsNativeMethods.ErrorCode errorCode = DiagnosticsNativeMethods.RegOpenKeyEx(SafeRegistryHandle.LocalMachine, text2, 0, 131097, out handle); if (DiagnosticsNativeMethods.ErrorCode.FileNotFound == errorCode) { RegistryKey registryKey = Registry.LocalMachine.CreateSubKey(text2); if (registryKey != null) { registryKey.Close(); errorCode = DiagnosticsNativeMethods.RegOpenKeyEx(SafeRegistryHandle.LocalMachine, text2, 0, 131097, out handle); } } if (errorCode != DiagnosticsNativeMethods.ErrorCode.Success) { throw new AmRegistryException("RegOpenKeyEx", new Win32Exception((int)errorCode)); } realRegistry = new RealRegistry(text2, handle); clusterRegistry = new ClusterRegistry(root, config.DagConfig.Cluster.Handle); registryReplicator = new RegistryReplicator(realRegistry, clusterRegistry, null); realRegistry = null; clusterRegistry = null; if (!registryReplicator.IsInitialReplication) { registryReplicator.InverseCopy(); } } catch (ClusterException innerException) { throw new AmDbMountNotAllowedDueToRegistryConfigurationException(innerException); } catch (AmRegistryException innerException2) { throw new AmDbMountNotAllowedDueToRegistryConfigurationException(innerException2); } finally { if (registryReplicator != null) { registryReplicator.Dispose(); } if (realRegistry != null) { realRegistry.Dispose(); } if (clusterRegistry != null) { clusterRegistry.Dispose(); } if (this.replicators.ContainsKey(mdbGuid)) { this.replicators[mdbGuid].EnableCopy(); } } } }
private void MonitorRegistry() { int num = 0; int num2 = RegistryParameters.RegistryMonitorPollingIntervalInSec / 5; AmConfig amConfig = null; Dictionary <Guid, RegistryReplicator> .KeyCollection.Enumerator enumerator = this.replicators.Keys.GetEnumerator(); try { bool flag = false; RegistryReplicator registryReplicator = null; RealRegistry realRegistry = null; ClusterRegistry clusterRegistry = null; List <Guid> list = new List <Guid>(); List <WaitHandle> list2 = new List <WaitHandle>(); while (!this.stopEvent.WaitOne(5000, false)) { amConfig = AmSystemManager.Instance.Config; while (!this.stopEvent.WaitOne(0, false) && amConfig.IsPamOrSam) { AmClusterHandle handle = amConfig.DagConfig.Cluster.Handle; if (flag) { enumerator.Dispose(); enumerator = this.replicators.Keys.GetEnumerator(); } int num3 = 0; list2.Clear(); list.Clear(); while (num3 < 64 && enumerator.MoveNext()) { if (this.replicators[enumerator.Current].IsValid) { list2.Add(this.replicators[enumerator.Current].KeyChanged); list.Add(enumerator.Current); num3++; } } flag = (num3 < 64); if (list2.Count <= 0) { goto IL_205; } int num4 = WaitHandle.WaitAny(list2.ToArray(), 5000, false); if (num4 >= 0 && num4 < list2.Count) { Guid key = list[num4]; if (this.replicators[key].IsCopyEnabled) { lock (this.workQueue) { this.workQueue.Enqueue(this.replicators[key]); this.replicators[key].KeyChanged.Reset(); } try { DiagnosticsNativeMethods.ErrorCode errorCode = DiagnosticsNativeMethods.RegNotifyChangeKeyValue((SafeRegistryHandle)this.replicators[key].Handle, true, DiagnosticsNativeMethods.RegistryNotifications.ChangeName | DiagnosticsNativeMethods.RegistryNotifications.ChangeAttributes | DiagnosticsNativeMethods.RegistryNotifications.LastSet, this.replicators[key].KeyChanged.SafeWaitHandle, true); if (errorCode != DiagnosticsNativeMethods.ErrorCode.Success) { throw new AmRegistryException("RegNotifyChangeKeyValue", new Win32Exception((int)errorCode)); } goto IL_21B; } catch (AmRegistryException ex) { AmTrace.Warning("Registering for registry key change notifications failed. Inner Exception: {0}", new object[] { ex }); goto IL_21B; } goto IL_205; } } IL_21B: if (num == 0 || this.dbMountedEvent.WaitOne(0, false)) { num2 = RegistryParameters.RegistryMonitorPollingIntervalInSec / 5; foreach (KeyValuePair <Guid, RegistryReplicator> keyValuePair in this.replicators) { keyValuePair.Value.SetMarkedForRemoval(); } IADDatabase[] databasesToMonitor = this.GetDatabasesToMonitor(); if (databasesToMonitor != null) { foreach (IADDatabase iaddatabase in databasesToMonitor) { if (this.replicators.ContainsKey(iaddatabase.Guid)) { this.replicators[iaddatabase.Guid].ResetMarkedForRemoval(); } else { Exception ex2 = null; try { if (!flag) { enumerator.Dispose(); flag = true; } string text = iaddatabase.IsPublicFolderDatabase ? RegistryMonitor.publicString : RegistryMonitor.privateString; string text2 = string.Format(RegistryMonitor.localRegistryPathFormat, Environment.MachineName, iaddatabase.Guid, text); string root = string.Format(RegistryMonitor.clusterRegistryPathFormat, iaddatabase.Guid, text); ManualResetEvent manualResetEvent = new ManualResetEvent(false); SafeRegistryHandle safeRegistryHandle; DiagnosticsNativeMethods.ErrorCode errorCode = DiagnosticsNativeMethods.RegOpenKeyEx(SafeRegistryHandle.LocalMachine, text2, 0, 131097, out safeRegistryHandle); if (DiagnosticsNativeMethods.ErrorCode.FileNotFound == errorCode) { RegistryKey registryKey = Registry.LocalMachine.CreateSubKey(text2); if (registryKey != null) { registryKey.Close(); errorCode = DiagnosticsNativeMethods.RegOpenKeyEx(SafeRegistryHandle.LocalMachine, text2, 0, 131097, out safeRegistryHandle); } } if (errorCode != DiagnosticsNativeMethods.ErrorCode.Success) { throw new AmRegistryException("RegOpenKeyEx", new Win32Exception((int)errorCode)); } realRegistry = new RealRegistry(text2, safeRegistryHandle); clusterRegistry = new ClusterRegistry(root, amConfig.DagConfig.Cluster.Handle); registryReplicator = new RegistryReplicator(realRegistry, clusterRegistry, manualResetEvent); realRegistry = null; clusterRegistry = null; if (registryReplicator.IsInitialReplication) { registryReplicator.Copy(handle); registryReplicator.SetInitialReplication(); } else { registryReplicator.InverseCopy(); } errorCode = DiagnosticsNativeMethods.RegNotifyChangeKeyValue(safeRegistryHandle, true, DiagnosticsNativeMethods.RegistryNotifications.ChangeName | DiagnosticsNativeMethods.RegistryNotifications.ChangeAttributes | DiagnosticsNativeMethods.RegistryNotifications.LastSet, manualResetEvent.SafeWaitHandle, true); if (errorCode != DiagnosticsNativeMethods.ErrorCode.Success) { throw new AmRegistryException("RegNotifyChangeKeyValue", new Win32Exception((int)errorCode)); } this.replicators.Add(iaddatabase.Guid, registryReplicator); registryReplicator = null; } catch (AmRegistryException ex3) { ex2 = ex3; AmTrace.Warning("Failed to add database to monitor: {0}. Exception: {1}", new object[] { iaddatabase.Name, ex3 }); } catch (ClusterException ex4) { ex2 = ex4; AmTrace.Warning("Failed to add database to monitor: {0}. Exception: {1}", new object[] { iaddatabase.Name, ex4 }); } finally { if (ex2 != null) { ReplayEventLogConstants.Tuple_RegistryReplicatorException.LogEvent(null, new object[] { ex2 }); } if (registryReplicator != null) { registryReplicator.Dispose(); registryReplicator = null; } if (realRegistry != null) { realRegistry.Dispose(); realRegistry = null; } if (clusterRegistry != null) { clusterRegistry.Dispose(); clusterRegistry = null; } } } } } list.Clear(); foreach (KeyValuePair <Guid, RegistryReplicator> keyValuePair2 in this.replicators) { if (keyValuePair2.Value.IsMarkedForRemoval) { list.Add(keyValuePair2.Key); } } lock (this.workQueue) { foreach (Guid key2 in list) { registryReplicator = this.replicators[key2]; registryReplicator.SetInvalid(); if (!registryReplicator.IsCopying) { if (!flag) { flag = true; } this.replicators.Remove(key2); registryReplicator.Dispose(); } } } registryReplicator = null; } num = (num + 1) % num2; amConfig = AmSystemManager.Instance.Config; continue; IL_205: if (!this.stopEvent.WaitOne(5000, false)) { goto IL_21B; } break; } } foreach (KeyValuePair <Guid, RegistryReplicator> keyValuePair3 in this.replicators) { if (!keyValuePair3.Value.IsCopying) { keyValuePair3.Value.SetInvalid(); } } } finally { enumerator.Dispose(); } }