public static string GetFullyQualifiedName(string computerName) { string result = null; AppAssert.AssertNotNull(computerName); try { IPAddress ipAddress = null; if (IPAddress.TryParse(computerName, out ipAddress)) { result = Dns.GetHostEntry(ipAddress).HostName; } else { result = Dns.GetHostEntry(computerName).HostName; } } catch (ArgumentException e) { throw new Exception("Compute name is invalid"); } catch (System.Net.Sockets.SocketException e) { throw new Exception("Cannot contact the machine with name " + computerName); } return(result); }
/// <summary> /// Deletes a registry value under specified key /// </summary> /// <param name="root"></param> /// <param name="registryKeyPath"></param> /// <param name="name"></param> /// <exception cref="BackEndErrorException"></exception> public static void DeleteRegistryValue(RegistryKey root, String registryKeyPath, String name) { AppAssert.AssertNotNull(root, "root"); AppAssert.AssertNotNull(registryKeyPath, "registryKeyPath"); AppAssert.AssertNotNull(name, "name"); try { using (RegistryKey regkey = root.OpenSubKey(registryKeyPath, true)) { if (regkey != null) { regkey.DeleteValue(name, false); } } } catch (System.Security.SecurityException se) { throw new Exception("Cannot access registry key" + registryKeyPath); } catch (UnauthorizedAccessException uae) { throw new Exception("Cannot access registry key" + registryKeyPath); } }
/// <summary> /// Starts a particular service /// The method times out if service does not reach "Running" status within the timeout period. /// </summary> /// <param name="serviceName">name of service to start</param> /// <param name="machineName">name of computer on which the service resides</param> /// <exception cref="ServiceConfigurationException">when service could not be started</exception> public void StartService(String serviceName, String machineName) { AppAssert.Assert(null != serviceName, "Null service name passed!"); SetupLogger.LogInfo("StartService: Start the service {0}", serviceName); //ConfigurationMessageEvent(this, new ConfigurationMessageEventArgs(String.Format(Resources.StartingService, serviceName))); try { AppAssert.AssertNotNull(serviceName, "serviceName"); ServiceController serviceController = null; try { SetupLogger.LogInfo("Starting service" + serviceName); if (machineName == null) { serviceController = new ServiceController(serviceName); // local host } else { serviceController = new ServiceController(serviceName, machineName); } SetupLogger.LogInfo("StartService: Service {0} status = {1}", serviceName, serviceController.Status); // // Query configurations (credentials and start mode) of service // ServiceConfig serviceConfig = GetServiceConfig(serviceName, machineName); // // Check if the service is disabled or manual and stopped // if (serviceConfig.StartMode == ServiceStartMode.Disabled || (serviceConfig.StartMode == ServiceStartMode.Manual && serviceController.Status == ServiceControllerStatus.Stopped)) { SetupLogger.LogInfo( "StartService : service is disabled or manual and not running"); throw new Exception(String.Format("Cannot start the service. Service name: {0}", serviceName)); } // Check if the service is stopped or paused if (serviceController.Status == ServiceControllerStatus.Running) { // Service is running, log and return SetupLogger.LogInfo("StartService: Service running, check if needs to be restarted"); return; } if (serviceController.Status == ServiceControllerStatus.Stopped) { // Service stopped, Start the service SetupLogger.LogInfo("StartService: Service stopped, Start the service"); serviceController.Start(); } else if (serviceController.Status == ServiceControllerStatus.Paused) { // Service paused, Resume the service SetupLogger.LogInfo("StartService: Service paused, Resume the service"); serviceController.Continue(); } SetupLogger.LogInfo("StartService: Wait for service to start (timeout = {0})", SetupConstants.ServiceStartTimeout); serviceController.WaitForStatus(ServiceControllerStatus.Running, SetupConstants.ServiceStartTimeout); } catch (System.ComponentModel.Win32Exception win32Exception) { // The native service API failed SetupLogger.LogError("StartService: Couldn't start service! Throwing exception: {0}", win32Exception.ToString()); throw new Exception(String.Format("Cannot start the service. Service name: {0}", serviceName)); } catch (InvalidOperationException invalidOperationException) { // The service can not be started SetupLogger.LogError("StartService: Couldn't start service! Throwing exception: {0}", invalidOperationException.ToString()); throw; } catch (System.ServiceProcess.TimeoutException timeoutException) { // There was a timeout SetupLogger.LogError("StartService: Couldn't start service! Throwing exception: {0}", timeoutException.ToString()); throw new Exception(String.Format("Cannot start the service. Service name: {0}", serviceName)); } finally { if (serviceController != null) { serviceController.Close(); } } } catch (Exception serviceNotFoundException) { SetupLogger.LogInfo("ServiceConfigurationHandler.StartService: Start the service, exception {0}", serviceNotFoundException); throw new Exception(String.Format("Cannot start the service. Service name: {0}", serviceName)); } }
/// <summary> /// Get the account name given a Sid /// </summary> /// <param name="id"></param> /// <returns></returns> public static string GetAccountNameFromSid(SecurityIdentifier id) { AppAssert.AssertNotNull(id); return(id.Translate(typeof(NTAccount)).Value); }
/// <summary> /// Create the engine service and start it /// </summary> public static void ConfigureCMPWorkerService(ServiceConfigurationHandler serviceConfigurationHandler) { AppAssert.AssertNotNull(serviceConfigurationHandler, "serviceConfigurationHandler"); string installPath = SetupConstants.GetServerInstallPath(); //attempt to remove services first. //this will ignore errors of service does not exist and service marked for deletion //If service is marked for deletion, then exception will be thrown at create time as //we will be unable to create the service. //reason: //1. Keeps the code path of Install and Repair same //2. Handles corner case of service already existing in Install mode //3. Repairs the configuration of the service if broken IntPtr hSCManager = NativeMethods.NullIntPtr; IntPtr password = NativeMethods.NullIntPtr; try { hSCManager = ServiceConfigurationHandler.GetSCMHandle(); //TODO: Handle rollback if exception is thrown ServiceConfigurationHandler.StopAndRemoveService(hSCManager, SetupConstants.EngineServiceName); //construct paths to service binaries string servicePathEngine = PathHelper.QuoteString(installPath + @"MSIT\CmpWorkerService\" + SetupConstants.EngineServiceBinary); SetupLogger.LogInfo("BackEnd.Configure: Engine Service path is : {0}", servicePathEngine); //Get account string userAccountName = null; bool runasLocalAccount = SetupInputs.Instance.FindItem(SetupInputTags.CmpServiceLocalAccountTag); if (!runasLocalAccount) { userAccountName = UserAccountHelper.GetVmmServiceDomainAccount(); password = Marshal.SecureStringToGlobalAllocUnicode(SetupInputs.Instance.FindItem(SetupInputTags.CmpServiceUserPasswordTag)); } //create engine service ServiceConfigurationHandler.CreateService( hSCManager, SetupConstants.EngineServiceName, Resources.EngineServiceDisplayName, Resources.EngineServiceDescription, servicePathEngine, null, // dependent services userAccountName, password: password, autoStart: true, interactive: false); //set failure actions for VMMService NativeMethods.SERVICE_FAILURE_ACTIONS sfa = new NativeMethods.SERVICE_FAILURE_ACTIONS(); NativeMethods.SC_ACTION[] sca = new NativeMethods.SC_ACTION[SetupConstants.ServiceActionsCount + 1]; for (int i = 0; i < SetupConstants.ServiceActionsCount; i++) { sca[i].Delay = SetupConstants.ServiceRestartDelay; sca[i].Type = NativeMethods.SC_ACTION_TYPE.SC_ACTION_RESTART; } sca[SetupConstants.ServiceActionsCount].Delay = 0; sca[SetupConstants.ServiceActionsCount].Type = NativeMethods.SC_ACTION_TYPE.SC_ACTION_NONE; IntPtr unmanagedStructArray = NativeMethods.NullIntPtr; try { unmanagedStructArray = GetUnmanagedStructArray(sca); sfa.sc_Action = unmanagedStructArray; sfa.cActions = SetupConstants.ServiceActionsCount + 1; sfa.dwResetPeriod = SetupConstants.ServiceResetPeriod; sfa.lpCommand = null; sfa.lpRebootMsg = null; //set service failure actions for engine service ServiceConfigurationHandler.SetFailureActions(SetupConstants.EngineServiceName, ref sfa); //ConfigurationProgressEvent(this, new EventArgs()); } finally { if (NativeMethods.NullIntPtr != unmanagedStructArray) { Marshal.FreeHGlobal(unmanagedStructArray); } } } finally { if (!NativeMethods.NullIntPtr.Equals(hSCManager)) { NativeMethods.CloseServiceHandle(hSCManager); } if (!NativeMethods.NullIntPtr.Equals(password)) { Marshal.ZeroFreeGlobalAllocUnicode(password); } } }