|
| 1 | +// Copyright © 2017 - 2018 Chocolatey Software, Inc |
| 2 | +// Copyright © 2011 - 2017 RealDimensions Software, LLC |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +namespace chocolatey.infrastructure.app.services |
| 18 | +{ |
| 19 | + using configuration; |
| 20 | + using Microsoft.Win32; |
| 21 | + using platforms; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Service to check for System level pending reboot request |
| 25 | + /// </summary> |
| 26 | + /// <remarks>Based on code from https://github.com/puppetlabs/puppetlabs-reboot</remarks> |
| 27 | + public class PendingRebootService : IPendingRebootService |
| 28 | + { |
| 29 | + private readonly IRegistryService _registryService; |
| 30 | + |
| 31 | + public PendingRebootService(IRegistryService registryService) |
| 32 | + { |
| 33 | + _registryService = registryService; |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Test to see if there are any known situations that require |
| 38 | + /// a System reboot. |
| 39 | + /// </summary> |
| 40 | + /// <param name="config">The current Chocolatey Configuration</param> |
| 41 | + /// <returns><c>true</c> if reboot is required; otherwise <c>false</c>.</returns> |
| 42 | + public bool is_pending_reboot(ChocolateyConfiguration config) |
| 43 | + { |
| 44 | + if (config.Information.PlatformType != PlatformType.Windows) |
| 45 | + { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + this.Log().Debug("Performing reboot requirement checks..."); |
| 50 | + |
| 51 | + return is_pending_computer_rename() || |
| 52 | + is_pending_component_based_servicing() || |
| 53 | + is_pending_windows_auto_update() || |
| 54 | + is_pending_file_rename_operations() || |
| 55 | + is_pending_package_installer() || |
| 56 | + is_pending_package_installer_syswow64(); |
| 57 | + } |
| 58 | + |
| 59 | + private bool is_pending_computer_rename() |
| 60 | + { |
| 61 | + var path = "SYSTEM\\CurrentControlSet\\Control\\ComputerName\\{0}"; |
| 62 | + var activeName = get_registry_key_string_value(path.format_with("ActiveComputerName"), "ComputerName"); |
| 63 | + var pendingName = get_registry_key_string_value(path.format_with("ComputerName"), "ComputerName"); |
| 64 | + |
| 65 | + bool result = !string.IsNullOrWhiteSpace(activeName) && |
| 66 | + !string.IsNullOrWhiteSpace(pendingName) && |
| 67 | + activeName != pendingName; |
| 68 | + |
| 69 | + this.Log().Debug("PendingComputerRename: {0}".format_with(result)); |
| 70 | + |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + private bool is_pending_component_based_servicing() |
| 75 | + { |
| 76 | + if (!is_vista_sp1_or_later()) |
| 77 | + { |
| 78 | + this.Log().Debug("Not using Windows Vista SP1 or earlier, so no check for Component Based Servicing can be made."); |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
| 82 | + var path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Component Based Servicing\\RebootPending"; |
| 83 | + var key = _registryService.get_key(RegistryHive.LocalMachine, path); |
| 84 | + var result = key != null; |
| 85 | + |
| 86 | + this.Log().Debug("PendingComponentBasedServicing: {0}".format_with(result)); |
| 87 | + |
| 88 | + return result; |
| 89 | + } |
| 90 | + |
| 91 | + private bool is_pending_windows_auto_update() |
| 92 | + { |
| 93 | + var path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\WindowsUpdate\\Auto Update\\RebootRequired"; |
| 94 | + var key = _registryService.get_key(RegistryHive.LocalMachine, path); |
| 95 | + var result = key != null; |
| 96 | + |
| 97 | + this.Log().Debug("PendingWindowsAutoUpdate: {0}".format_with(result)); |
| 98 | + |
| 99 | + return result; |
| 100 | + } |
| 101 | + |
| 102 | + private bool is_pending_file_rename_operations() |
| 103 | + { |
| 104 | + var path = "SYSTEM\\CurrentControlSet\\Control\\Session Manager"; |
| 105 | + var value = get_registry_key_value(path, "PendingFileRenameOperations"); |
| 106 | + |
| 107 | + var result = false; |
| 108 | + |
| 109 | + if (value != null && value is string[]) |
| 110 | + { |
| 111 | + result = (value as string[]).Length != 0; |
| 112 | + } |
| 113 | + |
| 114 | + this.Log().Debug("PendingFileRenameOperations: {0}".format_with(result)); |
| 115 | + |
| 116 | + return result; |
| 117 | + } |
| 118 | + |
| 119 | + private bool is_pending_package_installer() |
| 120 | + { |
| 121 | + // http://support.microsoft.com/kb/832475 |
| 122 | + // 0x00000000 (0) No pending restart. |
| 123 | + var path = "SOFTWARE\\Microsoft\\Updates"; |
| 124 | + var value = get_registry_key_string_value(path, "UpdateExeVolatile"); |
| 125 | + |
| 126 | + var result = !string.IsNullOrWhiteSpace(value) && value != "0"; |
| 127 | + |
| 128 | + this.Log().Debug("PendingPackageInstaller: {0}".format_with(result)); |
| 129 | + |
| 130 | + return result; |
| 131 | + } |
| 132 | + |
| 133 | + private bool is_pending_package_installer_syswow64() |
| 134 | + { |
| 135 | + // http://support.microsoft.com/kb/832475 |
| 136 | + // 0x00000000 (0) No pending restart. |
| 137 | + var path = "SOFTWARE\\Wow6432Node\\Microsoft\\Updates"; |
| 138 | + var value = get_registry_key_string_value(path, "UpdateExeVolatile"); |
| 139 | + |
| 140 | + var result = !string.IsNullOrWhiteSpace(value) && value != "0"; |
| 141 | + |
| 142 | + this.Log().Debug("PendingPackageInstallerSysWow64: {0}".format_with(result)); |
| 143 | + |
| 144 | + return result; |
| 145 | + } |
| 146 | + |
| 147 | + private string get_registry_key_string_value(string path, string value) |
| 148 | + { |
| 149 | + var key = _registryService.get_key(RegistryHive.LocalMachine, path); |
| 150 | + |
| 151 | + if (key == null) |
| 152 | + { |
| 153 | + return string.Empty; |
| 154 | + } |
| 155 | + |
| 156 | + return key.GetValue(value, string.Empty).to_string(); |
| 157 | + } |
| 158 | + |
| 159 | + private object get_registry_key_value(string path, string value) |
| 160 | + { |
| 161 | + var key = _registryService.get_key(RegistryHive.LocalMachine, path); |
| 162 | + |
| 163 | + if (key == null) |
| 164 | + { |
| 165 | + return null; |
| 166 | + } |
| 167 | + |
| 168 | + return key.GetValue(value, null); |
| 169 | + } |
| 170 | + |
| 171 | + private bool is_vista_sp1_or_later() |
| 172 | + { |
| 173 | + var versionNumber = Platform.get_version(); |
| 174 | + |
| 175 | + this.Log().Debug("Operating System Version Number: {0}".format_with(versionNumber)); |
| 176 | + |
| 177 | + return versionNumber.Build >= 6001; |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments