Exemple #1
0
        private void EnableDriverPatched(DriverModuleName moduleName)
        {
            if (moduleName == null)
            {
                throw new InvalidDriverModuleName("null");
            }

            // TODO: either get the real driver status and use it here,
            //       or get rid of this method and make related refactoring.

            if (true     // Because is better to say, enabled driver
                         // is waiting activation than is not, when it is
                         // already activated (how to get the real status?).
                || this.driverPropsGetter.RequiresRestartToWork(moduleName)
                )
            {
                HashSet <DriverModuleName> driversWaitingActivation
                    = this.pendingChanges.Activation;

                if (driversWaitingActivation.Contains(moduleName) == false)
                {
                    this.pendingChanges.OnEnableRequest(moduleName);
                    // TODO: implement driver enabler
                }
            }
        }
Exemple #2
0
        public void CanDisableAtGivenTime()
        {
            int deactivateAfterMs = 200,
                checkAfterMs      = 200;

            this.ExecuteTest((proxy, pendingChanges, driversSource) =>
            {
                var moduleName = new DriverModuleName("ACPI");

                var switcher = new DriversScheduledEnablementSwitcher(
                    proxy.Query,
                    proxy.Manage,
                    pendingChanges
                    );

                var schedule
                    = new DriverEnablementSchedule {
                        {
                            DateTime.Now.AddMilliseconds(
                                deactivateAfterMs),
                            DriverEnablementRequest.Deactivation
                        }
                    };

                switcher.Disable(moduleName, schedule);
                Assert.IsTrue(driversSource.GetStatus(moduleName)
                              .IsDeactivationPending == false);

                Thread.Sleep(deactivateAfterMs + checkAfterMs);

                Assert.IsTrue(driversSource.GetStatus(moduleName)
                              .IsDeactivationPending == true);
            });
        }
Exemple #3
0
        public string GetProp(DriverModuleName mn, DriverPropName pn)
        {
            this.propsLock.EnterReadLock();
            try {
                foreach (Dictionary <DriverPropName, string>
                         driverPropDict in this.allDriversPropDicts
                         )
                {
                    if (driverPropDict[DPNs.ModuleName] == mn.ToString())
                    {
                        if (driverPropDict.ContainsKey(pn) == false)
                        {
                            throw new InvalidDriverPropName(pn.ToString());
                        }

                        return(driverPropDict[pn]);
                    }
                }

                throw new InvalidDriverModuleName(mn.ToString());
            }
            finally {
                this.propsLock.ExitReadLock();
            }
        }
Exemple #4
0
        public void Disable(
            DriverModuleName moduleName,
            DriverEnablementSchedule schedule
            )
        {
            var enablementSwitch
                = new DefaultOneDriverEnablementSwitch(
                      moduleName,
                      this.driverStatusGetter,
                      this.enablementController);
            var switchInitiator
                = new DefaultScheduledEnablementSwitchInitiator(
                      schedule, enablementSwitch);

            this.switchInitiators.Add(switchInitiator);

            enablementSwitch.ActivationRequested
                += (sender, ea)
                   => this.ActivationRequested?.Invoke(sender, ea);

            enablementSwitch.DeactivationRequested
                += (sender, ea)
                   => this.DeactivationRequested?.Invoke(sender, ea);

            this.ActivationRequested
                += (sender, dModuleName)
                   => this.ConsiderRestartingComputer(dModuleName);

            this.DeactivationRequested
                += (sender, dModuleName)
                   => this.ConsiderRestartingComputer(dModuleName);

            switchInitiator.Start();
        }
Exemple #5
0
        public void SetProp(
            DriverModuleName mn,
            DriverPropName pn,
            string value
            )
        {
            this.propsLock.EnterWriteLock();
            try {
                foreach (Dictionary <DriverPropName, string>
                         driverPropDict in this.allDriversPropDicts
                         )
                {
                    if (driverPropDict[DPNs.ModuleName] == mn.ToString())
                    {
                        if (driverPropDict.ContainsKey(pn) == false)
                        {
                            throw new InvalidDriverPropName(pn.ToString());
                        }

                        driverPropDict[pn] = value;
                    }
                }

                throw new InvalidDriverModuleName(mn.ToString());
            }
            finally {
                this.propsLock.ExitWriteLock();
            }
        }
Exemple #6
0
        protected string GetPropString(
            DriverModuleName moduleName,
            DriverPropName propName
            )
        {
            if (moduleName == null)
            {
                throw new InvalidDriverModuleName("null");
            }
            if (propName == null)
            {
                throw new InvalidDriverPropName("null");
            }

            List <Dictionary <DriverPropName, string> > allDriversPropDicts
                = this.driversSource.GetPropDictionariesCopy();

            foreach (Dictionary <DriverPropName, string>
                     driverPropDict in allDriversPropDicts
                     )
            {
                if (driverPropDict[DPNs.ModuleName] == moduleName.ToString())
                {
                    return(driverPropDict[propName]);
                }
            }

            throw new InvalidOperationException(
                      string.Format("Could not get prop string for driver "
                                    + "'{0}' prop '{1}'", moduleName, propName));
        }
Exemple #7
0
        private void DisableDriverPatched(DriverModuleName moduleName)
        {
            if (moduleName == null)
            {
                throw new InvalidDriverModuleName("null");
            }

            if (this.driverPropsGetter.SupportsDisabling(moduleName) == false)
            {
                throw new CannotDisableDriver(moduleName.ToString());
            }

            // TODO: either get the real driver status and use it here,
            //       or get rid of this method and make related refactoring.

            // Assumption: if driver was loaded, it will never be unloaded
            //             by Windows kernel before computer restart.

            HashSet <DriverModuleName> driversWaitingDeactivation
                = this.pendingChanges.Deactivation;

            if (driversWaitingDeactivation.Contains(moduleName) == false)
            {
                this.pendingChanges.OnDisableRequest(moduleName);
                // TODO: implement driver disabler
            }
        }
        private void LaunchEnablementSwitcher()
        {
            DriverModuleName moduleName;

            {
                string mnStr = GCTaskFilesInterface.GetNameOfModuleToDisable();
                if (string.IsNullOrEmpty(mnStr))
                {
                    Console.WriteLine("Could not get module name of driver to disable.");
                    return;
                }

                moduleName = new DriverModuleName(mnStr);
                if (this.proxy.Query.HasDriver(moduleName) == false)
                {
                    Console.WriteLine(string.Format(
                                          "Driver {0} was not found.", mnStr));
                    return;
                }

                if (this.proxy.Query.SupportsDisabling(moduleName) == false)
                {
                    Console.WriteLine(string.Format(
                                          "Driver {0} cannot be disabled.", mnStr));
                    return;
                }
            }

            this.switcher = new DriversScheduledEnablementSwitcher(
                this.proxy.Query,
                this.proxy.Manage,
                this.pendingChanges
                );

            IEnumerable <DateTime> inputSchedule
                = GCTaskFilesInterface.GetDisablementSchedule();

            if (inputSchedule == null)
            {
                return;
            }

            this.switcher.DeactivationRequested
                += (sender, ea)
                   => Console.WriteLine(string.Format(
                                            "Deactivation requested for driver '{0}'.",
                                            moduleName));

            var switcherSchedule = new DriverEnablementSchedule();

            foreach (DateTime time in inputSchedule)
            {
                switcherSchedule[time] = DriverEnablementRequest.Deactivation;
            }

            this.switcher.Disable(new DriverModuleName(moduleName),
                                  switcherSchedule);
        }
Exemple #9
0
 private void ConsiderRestartingComputer(
     DriverModuleName changedStatusModuleName
     )
 {
     if (this.IsComputerRestartRequired && this.rebootSwitch != null)
     {
         this.rebootSwitch.Reboot();
     }
 }
Exemple #10
0
        protected static string GetPropString(
            DriverModuleName moduleName,
            string propName
            )
        {
            if (moduleName == null)
            {
                throw new InvalidDriverModuleName("null");
            }
            if (string.IsNullOrEmpty(propName))
            {
                throw new InvalidDriverPropName("null or empty");
            }

            string propString = string.Empty;

            var query = new SelectQuery(
                string.Format(
                    "SELECT {0} FROM Win32_SystemDriver "
                    + "WHERE Name = '{1}'"
                    , propName, moduleName.ToString())
                );

            using (var searcher = new ManagementObjectSearcher(query)) {
                try {
                    ManagementObjectCollection driverArray = searcher.Get();
                    if (driverArray.Count == 0)
                    {
                        throw new InvalidDriverModuleName(moduleName.ToString());
                    }

                    if (driverArray.Count == 1)
                    {
                        foreach (ManagementObject
                                 driverDictionary in driverArray
                                 )
                        {
                            if (driverDictionary[propName] != null)
                            {
                                propString = (driverDictionary[propName]).ToString();
                            }
                        }
                    }

                    if (driverArray.Count > 1)
                    {
                        throw new TwoDriversWithSameModuleName(moduleName.ToString());
                    }
                } catch (ManagementException e) {
                    throw new InvalidDriverPropName(
                              "Check prop name: " + propName, e);
                }
            }

            return(propString);
        }
 public DefaultOneDriverEnablementSwitch(
     DriverModuleName moduleName,
     IDriverStatus driverStatusGetter,
     IDriverEnablementController enablementController
     )
 {
     this.moduleName           = moduleName;
     this.driverStatusGetter   = driverStatusGetter;
     this.enablementController = enablementController;
 }
Exemple #12
0
 public void DisableDriver(DriverModuleName moduleName)
 {
     if (this.pendingChanges != null)
     {
         this.DisableDriverPatched(moduleName);
     }
     else
     {
         this.DisableDriverPure(moduleName);
     }
 }
Exemple #13
0
        public bool IsActivated(DriverModuleName moduleName)
        {
            // TODO: implement logic, which gives always correct value.

            bool isActivatedAccordingToWindows
                = bool.Parse(GetPropString(moduleName, "Started"));

            bool mayBeActivated
                = isActivatedAccordingToWindows ||
                  this.IsActivationPending(moduleName);

            return(mayBeActivated);
        }
Exemple #14
0
        public void Save(List <DriverInfo> driversInfoList)
        {
            if (driversInfoList == null)
            {
                throw new ArgumentNullException();
            }

            foreach (DriverInfo driverInfo in driversInfoList)
            {
                DriverModuleName moduleName = driverInfo.ModuleName;
                this.info[moduleName] = driverInfo;
            }
        }
Exemple #15
0
        public void SetStatus(DriverModuleName n, DriverStatus s)
        {
            this.statusesLock.EnterWriteLock();
            try {
                if (this.driversStatus.ContainsKey(n) == false)
                {
                    throw new InvalidDriverModuleName(n.ToString());
                }

                this.driversStatus[n] = s;
            }
            finally {
                this.statusesLock.ExitWriteLock();
            }
        }
Exemple #16
0
        public DriverStatus GetStatus(DriverModuleName n)
        {
            this.statusesLock.EnterReadLock();
            try {
                if (this.driversStatus.ContainsKey(n) == false)
                {
                    throw new InvalidDriverModuleName(n.ToString());
                }

                return(this.driversStatus[n]);
            }
            finally {
                this.statusesLock.ExitReadLock();
            }
        }
Exemple #17
0
 public void OnEnableRequest(DriverModuleName mn)
 {
     rwLock.EnterWriteLock();
     try {
         if (this.deactivation.Contains(mn))
         {
             this.deactivation.Remove(mn);
         }
         else
         {
             this.activation.Add(mn);
         }
     }
     finally {
         rwLock.ExitWriteLock();
     }
 }
Exemple #18
0
        private bool GetStatusFieldValue(
            DriverModuleName moduleName,
            Func <DriverStatus, bool> selectValue
            )
        {
            Dictionary <DriverModuleName, DriverStatus> statusesCopy
                = this.driversSource.GetStatusDictionaryCopy();

            if (statusesCopy.ContainsKey(moduleName) == false)
            {
                throw new InvalidDriverModuleName(moduleName.ToString());
            }

            bool isActivated = selectValue(statusesCopy[moduleName]);

            return(isActivated);
        }
Exemple #19
0
        public void EnableDriver(DriverModuleName moduleName)
        {
            if (moduleName == null)
            {
                throw new InvalidDriverModuleName("null");
            }

            if (this.driversSource.GetStatus(moduleName).IsActivated)
            {
                return;
            }

            if (this.driverPropsGetter.RequiresRestartToWork(moduleName))
            {
                HashSet <DriverModuleName> driversWaitingActivation
                    = this.pendingChanges.Activation;

                if (driversWaitingActivation.Contains(moduleName))
                {
                    return;
                }

                this.pendingChanges.OnEnableRequest(moduleName);

                var s = new DriverStatus {
                    IsActivated           = false,
                    IsActivationPending   = true,
                    IsDeactivationPending = false
                };

                this.driversSource.SetStatus(moduleName, s);
            }
            else
            {
                var s = new DriverStatus {
                    IsActivated           = true,
                    IsActivationPending   = false,
                    IsDeactivationPending = false
                };

                this.driversSource.SetStatus(moduleName, s);
            }
        }
Exemple #20
0
        public void CanGetTestDriverStatus()
        {
            var pendingChanges = new TestDefaultPendingDriverChangesRegister();
            var driversSource  = new TestDriversSource();

            try {
                var dp = new TestDriversProxy(pendingChanges, driversSource);

                {
                    var mn = new DriverModuleName("ACPI");
                    Assert.IsTrue(dp.Query.IsActivated(mn));
                    Assert.IsFalse(dp.Query.IsActivationPending(mn));
                    Assert.IsFalse(dp.Query.IsDeactivationPending(mn));
                }
            }
            finally {
                pendingChanges.Dispose();
                driversSource.Dispose();
            }
        }
Exemple #21
0
        public List <DriverInfo> GetAllDriversInfo()
        {
            var infoList = new List <DriverInfo>();

            var query = new SelectQuery(
                "SELECT " +
                "       Name, DisplayName, AcceptStop, Started " +
                "FROM " +
                "       Win32_SystemDriver"
                );

            using (var searcher = new ManagementObjectSearcher(query))
                foreach (ManagementObject driverQr in searcher.Get())
                {
                    var moduleName
                        = new DriverModuleName((string)driverQr["Name"]);
                    var props = new DriverProps {
                        DisplayName           = (string)driverQr["DisplayName"],
                        SupportsDisabling     = (bool)driverQr["AcceptStop"],
                        RequiresRestartToWork = true     // TODO
                    };
                    var status = new DriverStatus {
                        IsActivated = (bool)driverQr["Started"],
                        IsActivationPending
                            = this.pendingChanges.Activation
                              .Contains(moduleName),
                        IsDeactivationPending
                            = this.pendingChanges.Deactivation
                              .Contains(moduleName)
                    };
                    var driverInfo = new DriverInfo {
                        ModuleName = moduleName,
                        Props      = props,
                        Status     = status
                    };

                    infoList.Add(driverInfo);
                }

            return(infoList);
        }
Exemple #22
0
        public bool HasDriver(DriverModuleName moduleName)
        {
            if (moduleName == null)
            {
                throw new InvalidDriverModuleName("null");
            }

            List <Dictionary <DriverPropName, string> > allDriversPropDicts
                = this.driversSource.GetPropDictionariesCopy();

            foreach (Dictionary <DriverPropName, string>
                     driverPropDict in allDriversPropDicts
                     )
            {
                if (driverPropDict[DPNs.ModuleName] == moduleName.ToString())
                {
                    return(true);
                }
            }

            return(false);
        }
Exemple #23
0
        public bool HasDriver(DriverModuleName moduleName)
        {
            if (moduleName == null)
            {
                throw new InvalidDriverModuleName("null");
            }

            bool hasDriver = false;

            var query = new SelectQuery(
                "SELECT Name FROM Win32_SystemDriver "
                + string.Format("WHERE Name = '{0}'", moduleName)
                );

            using (var searcher = new ManagementObjectSearcher(query))
                if (searcher.Get().Count >= 1)
                {
                    hasDriver = true;
                }

            return(hasDriver);
        }
Exemple #24
0
        public void DisableDriver(DriverModuleName moduleName)
        {
            if (moduleName == null)
            {
                throw new InvalidDriverModuleName("null");
            }

            if (this.driversSource.GetStatus(moduleName).IsActivated == false)
            {
                return;
            }

            if (this.driverPropsGetter.SupportsDisabling(moduleName) == false)
            {
                throw new CannotDisableDriver(moduleName.ToString());
            }

            // Assumption: if driver was loaded, it will never be unloaded
            //             by Windows kernel before computer restart.

            HashSet <DriverModuleName> driversWaitingDeactivation
                = this.pendingChanges.Deactivation;

            if (driversWaitingDeactivation.Contains(moduleName))
            {
                return;
            }

            this.pendingChanges.OnDisableRequest(moduleName);

            var s = new DriverStatus {
                IsActivated           = true,
                IsActivationPending   = false,
                IsDeactivationPending = true
            };

            this.driversSource.SetStatus(moduleName, s);
        }
Exemple #25
0
 public bool RequiresRestartToWork(DriverModuleName n)
 => this.driverPropsGetter.RequiresRestartToWork(n);
Exemple #26
0
 public bool HasDriver(DriverModuleName n)
 => this.driversLister.HasDriver(n);
Exemple #27
0
 public string GetDisplayName(DriverModuleName mn)
 => this.driverPropsGetter.GetDisplayName(mn);
Exemple #28
0
 public bool SupportsDisabling(DriverModuleName n)
 => this.driverPropsGetter.SupportsDisabling(n);
Exemple #29
0
 public bool IsActivated(DriverModuleName n)
 => this.driverStatusGetter.IsActivated(n);
Exemple #30
0
 public bool IsActivationPending(DriverModuleName n)
 => this.driverStatusGetter.IsActivationPending(n);