private bool WaitForAsyncRead(int timeoutInMs)
        {
            bool flag = false;

            lock (this)
            {
                if (this.m_asyncReadCompleteEvent != null && !this.m_asyncReadCompleteEvent.WaitOne(0, false))
                {
                    flag = true;
                }
            }
            if (flag)
            {
                ExTraceGlobals.NetworkChannelTracer.TraceDebug <int, int>((long)this.GetHashCode(), "WaitForAsyncRead: waiting a max timeout={0}ms on thread {1}", timeoutInMs, DiagnosticsNativeMethods.GetCurrentThreadId());
                if (!this.m_asyncReadCompleteEvent.WaitOne(timeoutInMs, false))
                {
                    ExTraceGlobals.NetworkChannelTracer.TraceError <int>((long)this.GetHashCode(), "WaitForAsyncRead: timed out on thread {0}", DiagnosticsNativeMethods.GetCurrentThreadId());
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 2
0
 // Token: 0x0600032A RID: 810 RVA: 0x0000DBE8 File Offset: 0x0000BDE8
 internal static int GetThreadId()
 {
     return(DiagnosticsNativeMethods.GetCurrentThreadId());
 }
Exemplo n.º 3
0
 private static void TerminateSelf(int exitCode)
 {
     DiagnosticsNativeMethods.TerminateProcess(Process.GetCurrentProcess().Handle, exitCode);
 }
Exemplo n.º 4
0
        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();
                    }
                }
            }
        }
Exemplo n.º 5
0
        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();
            }
        }
Exemplo n.º 6
0
 public bool IsChanged(int timeout, WaitHandle cancelEvent)
 {
     if (this.registryKey.IsInvalid || this.registryKey.IsClosed)
     {
         SafeRegistryHandle safeRegistryHandle;
         DiagnosticsNativeMethods.ErrorCode errorCode = DiagnosticsNativeMethods.RegOpenKeyEx(SafeRegistryHandle.LocalMachine, this.keyName, 0, 131097, out safeRegistryHandle);
         if (DiagnosticsNativeMethods.ErrorCode.FileNotFound == errorCode)
         {
             return(false);
         }
         if (errorCode != DiagnosticsNativeMethods.ErrorCode.Success)
         {
             throw new Win32Exception((int)errorCode);
         }
         try
         {
             this.rwl.EnterWriteLock();
             if (this.registryKey.IsInvalid || this.registryKey.IsClosed)
             {
                 this.registryKey = safeRegistryHandle;
                 this.notifyEvent = new AutoResetEvent(false);
                 errorCode        = this.RegisterKeyChangeNotification(this.registryKey, this.watchSubtree, DiagnosticsNativeMethods.RegistryNotifications.ChangeName | DiagnosticsNativeMethods.RegistryNotifications.ChangeAttributes | DiagnosticsNativeMethods.RegistryNotifications.LastSet, this.notifyEvent, true);
                 if (errorCode != DiagnosticsNativeMethods.ErrorCode.Success)
                 {
                     this.notifyEvent.Dispose();
                     this.notifyEvent = null;
                     this.registryKey.Dispose();
                     throw new Win32Exception((int)errorCode);
                 }
                 return(true);
             }
             else
             {
                 safeRegistryHandle.Dispose();
             }
         }
         finally
         {
             this.rwl.ExitWriteLock();
         }
     }
     try
     {
         this.rwl.EnterReadLock();
         bool flag = false;
         if (cancelEvent != null)
         {
             if (WaitHandle.WaitAny(new WaitHandle[]
             {
                 this.notifyEvent,
                 cancelEvent
             }, timeout) == 0)
             {
                 flag = true;
             }
         }
         else if (this.notifyEvent != null)
         {
             flag = this.notifyEvent.WaitOne(timeout);
         }
         if (!flag)
         {
             return(false);
         }
     }
     finally
     {
         try
         {
             this.rwl.ExitReadLock();
         }
         catch (SynchronizationLockException)
         {
         }
     }
     try
     {
         this.rwl.EnterWriteLock();
         DiagnosticsNativeMethods.ErrorCode errorCode = this.RegisterKeyChangeNotification(this.registryKey, this.watchSubtree, DiagnosticsNativeMethods.RegistryNotifications.ChangeName | DiagnosticsNativeMethods.RegistryNotifications.ChangeAttributes | DiagnosticsNativeMethods.RegistryNotifications.LastSet, this.notifyEvent, true);
         if (errorCode != DiagnosticsNativeMethods.ErrorCode.Success)
         {
             this.notifyEvent.Dispose();
             this.notifyEvent = null;
             this.registryKey.Dispose();
             if (DiagnosticsNativeMethods.ErrorCode.KeyDeleted != errorCode)
             {
                 throw new Win32Exception((int)errorCode);
             }
             errorCode = DiagnosticsNativeMethods.RegOpenKeyEx(SafeRegistryHandle.LocalMachine, this.keyName, 0, 131097, out this.registryKey);
             if (DiagnosticsNativeMethods.ErrorCode.FileNotFound != errorCode && errorCode != DiagnosticsNativeMethods.ErrorCode.Success)
             {
                 throw new Win32Exception((int)errorCode);
             }
             if (!this.registryKey.IsInvalid && !this.registryKey.IsClosed)
             {
                 this.notifyEvent = new AutoResetEvent(false);
                 errorCode        = this.RegisterKeyChangeNotification(this.registryKey, this.watchSubtree, DiagnosticsNativeMethods.RegistryNotifications.ChangeName | DiagnosticsNativeMethods.RegistryNotifications.ChangeAttributes | DiagnosticsNativeMethods.RegistryNotifications.LastSet, this.notifyEvent, true);
                 if (errorCode != DiagnosticsNativeMethods.ErrorCode.Success)
                 {
                     this.notifyEvent.Dispose();
                     this.notifyEvent = null;
                     this.registryKey.Dispose();
                     throw new Win32Exception((int)errorCode);
                 }
             }
         }
     }
     finally
     {
         try
         {
             this.rwl.ExitWriteLock();
         }
         catch (SynchronizationLockException)
         {
         }
     }
     return(true);
 }
Exemplo n.º 7
0
 private DiagnosticsNativeMethods.ErrorCode RegisterKeyChangeNotification(SafeRegistryHandle key, bool watchSubtree, DiagnosticsNativeMethods.RegistryNotifications notifyFilter, AutoResetEvent notifyEvent, bool asynchronous)
 {
     return(DiagnosticsNativeMethods.RegNotifyChangeKeyValue(this.registryKey, watchSubtree, notifyFilter, notifyEvent.SafeWaitHandle, asynchronous));
 }