protected override void InternalProcessRecord() { TaskLogger.LogEnter(); if (ServiceControllerUtils.IsInstalled(this.Name)) { using (ServiceController serviceController = new ServiceController(this.Name)) { foreach (ServiceController serviceController2 in serviceController.DependentServices) { base.WriteVerbose(Strings.ProcessingDependentService(serviceController2.ServiceName)); StringBuilder stringBuilder = new StringBuilder(); foreach (ServiceController serviceController3 in serviceController2.ServicesDependedOn) { if (!serviceController3.ServiceName.Equals(this.Name)) { stringBuilder.AppendFormat("{0}\0", serviceController3.ServiceName); } } stringBuilder.Append('\0'); base.WriteVerbose(Strings.ServiceDependencies(serviceController2.ServiceName, stringBuilder.ToString())); using (SafeHandle serviceHandle = serviceController2.ServiceHandle) { if (!NativeMethods.ChangeServiceConfig(serviceHandle.DangerousGetHandle(), 4294967295U, 4294967295U, 4294967295U, null, null, null, stringBuilder.ToString(), null, null, null)) { base.WriteVerbose(Strings.ChangeServiceConfigFailure); base.WriteError(new Win32Exception(Marshal.GetLastWin32Error()), ErrorCategory.InvalidOperation, null); } } } } } base.Uninstall(); TaskLogger.LogExit(); }
protected void Uninstall() { TaskLogger.LogEnter(); if (!ServiceControllerUtils.IsInstalled(this.Name)) { base.WriteVerbose(Strings.ServiceNotInstalled(this.Name)); return; } base.WriteVerbose(Strings.WillUninstallInstalledService(this.Name)); try { this.serviceProcessInstaller.Uninstall(null); } catch (Win32Exception ex) { if (ex.NativeErrorCode == 1060) { this.WriteWarning(Strings.ServiceAlreadyNotInstalled(this.Name)); } else { base.WriteError(new ServiceUninstallFailureException(this.Name, ex.Message, ex), ErrorCategory.InvalidOperation, null); } } catch (InstallException ex2) { base.WriteError(new ServiceUninstallFailureException(this.Name, ex2.Message, ex2), ErrorCategory.InvalidOperation, null); } if (this.serviceFirewallRules.Count > 0) { using (List <ExchangeFirewallRule> .Enumerator enumerator = this.serviceFirewallRules.GetEnumerator()) { while (enumerator.MoveNext()) { ExchangeFirewallRule exchangeFirewallRule = enumerator.Current; TaskLogger.Trace("Removing Windows Firewall Rule for Service {0}", new object[] { this.Name }); exchangeFirewallRule.Remove(); } return; } } string fullPath = this.serviceProcessInstaller.Context.Parameters["assemblypath"]; TaskLogger.Trace("Removing Service {0} from windows firewall exception", new object[] { this.Name }); ManageService.RemoveAssemblyFromFirewallExceptions(this.Name, fullPath, new Task.TaskErrorLoggingDelegate(base.WriteError)); TaskLogger.LogExit(); }
protected override void InternalProcessRecord() { TaskLogger.LogEnter(); if (ServiceControllerUtils.IsInstalled(this.Name)) { base.WriteVerbose(Strings.AdTopologyServiceWithOldNameInstalled(this.Name)); using (ServiceController serviceController = new ServiceController(this.Name)) { foreach (ServiceController serviceController2 in serviceController.DependentServices) { base.WriteVerbose(Strings.ProcessingDependentService(serviceController2.ServiceName)); StringBuilder stringBuilder = new StringBuilder(); foreach (ServiceController serviceController3 in serviceController2.ServicesDependedOn) { if (serviceController3.ServiceName == this.Name) { stringBuilder.AppendFormat("{0}\0", base.CurrentName); } else { stringBuilder.AppendFormat("{0}\0", serviceController3.ServiceName); } } stringBuilder.Append('\0'); base.WriteVerbose(Strings.ServiceDependencies(serviceController2.ServiceName, stringBuilder.ToString())); using (SafeHandle serviceHandle = serviceController2.ServiceHandle) { if (!UninstallOldADTopologyService.ChangeServiceConfig(serviceHandle, 4294967295U, 4294967295U, 4294967295U, null, null, IntPtr.Zero, stringBuilder.ToString(), null, null, null)) { base.WriteVerbose(Strings.ChangeServiceConfigFailure); base.WriteError(new Win32Exception(Marshal.GetLastWin32Error()), ErrorCategory.InvalidOperation, null); } } } } base.Uninstall(); } TaskLogger.LogExit(); }
protected void Install() { TaskLogger.LogEnter(new object[] { this.Name }); Hashtable hashtable = new Hashtable(); if (!ServiceControllerUtils.IsInstalled(this.Name)) { try { TaskLogger.Trace("Installing service", new object[0]); this.serviceProcessInstaller.Install(hashtable); } catch (Win32Exception ex) { if (1072 == ex.NativeErrorCode) { Thread.Sleep(10000); hashtable = new Hashtable(); this.serviceProcessInstaller.Install(hashtable); } else { base.WriteError(new TaskWin32Exception(ex), ErrorCategory.WriteError, null); } } base.ConfigureServiceSidType(); if (this.serviceFirewallRules.Count > 0) { foreach (ExchangeFirewallRule exchangeFirewallRule in this.serviceFirewallRules) { TaskLogger.Trace("Adding Windows Firewall Rule for Service {0}", new object[] { this.Name }); exchangeFirewallRule.Add(); } } this.serviceProcessInstaller.Commit(hashtable); } else { TaskLogger.Trace("Service is already installed.", new object[0]); } if (this.Description != null) { try { using (RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(ManageService.serviceRegPath + this.Name, true)) { registryKey.SetValue(ManageService.descriptionSubKeyName, this.Description); } goto IL_197; } catch (SecurityException inner) { base.WriteError(new SecurityException(Strings.ErrorOpenKeyDeniedForWrite(ManageService.serviceRegPath + this.Name), inner), ErrorCategory.WriteError, null); goto IL_197; } } TaskLogger.Trace("No service description", new object[0]); IL_197: if (this.EventMessageFile != null) { RegistryKey registryKey2 = null; try { try { registryKey2 = Registry.LocalMachine.OpenSubKey(ManageService.eventLogRegPath + this.Name, true); if (registryKey2 == null) { registryKey2 = Registry.LocalMachine.CreateSubKey(ManageService.eventLogRegPath + this.Name, RegistryKeyPermissionCheck.ReadWriteSubTree); } registryKey2.SetValue(ManageService.eventMessageFileSubKeyName, this.EventMessageFile); registryKey2.SetValue(ManageService.categoryMessageFileSubKeyName, this.EventMessageFile); registryKey2.SetValue(ManageService.categoryCountSubKeyName, this.CategoryCount); registryKey2.SetValue(ManageService.typesSupportedSubKeyName, 7); } catch (SecurityException inner2) { base.WriteError(new SecurityException(Strings.ErrorOpenKeyDeniedForWrite(ManageService.serviceRegPath + this.Name), inner2), ErrorCategory.WriteError, null); } goto IL_281; } finally { if (registryKey2 != null) { registryKey2.Close(); registryKey2 = null; } } } TaskLogger.Trace("No event message file", new object[0]); IL_281: if (base.FirstFailureActionType != ServiceActionType.None) { base.ConfigureFailureActions(); base.ConfigureFailureActionsFlag(); } else { TaskLogger.Trace("No failure actions", new object[0]); } TaskLogger.LogExit(); }
internal void StopService(ServiceController serviceController, bool ignoreServiceStopTimeout, bool failIfServiceNotInstalled, Unlimited <EnhancedTimeSpan> maximumWaitTime) { if (!ServiceControllerUtils.IsInstalled(serviceController)) { string text; try { text = serviceController.ServiceName; } catch (InvalidOperationException) { text = ""; } TaskLogger.Trace("Service {0} is not installed. Cannot stop the service", new object[] { text }); if (failIfServiceNotInstalled) { base.WriteError(new ServiceNotInstalledException(text), ErrorCategory.InvalidOperation, null); return; } this.WriteWarning(Strings.ServiceNotInstalled(text)); return; } else { string text = serviceController.ServiceName; bool flag = false; ServiceControllerStatus status = serviceController.Status; if (status == ServiceControllerStatus.Stopped) { TaskLogger.Trace("Service {0} is already stopped. ", new object[] { text }); TaskLogger.LogExit(); return; } foreach (ServiceController serviceController2 in serviceController.DependentServices) { this.StopService(serviceController2, ignoreServiceStopTimeout, failIfServiceNotInstalled, maximumWaitTime); } if (status == ServiceControllerStatus.StartPending || status == ServiceControllerStatus.ContinuePending) { TaskLogger.Trace("Service {0} is starting. Waiting for the service to start", new object[] { text }); if (this.WaitForServiceStatus(serviceController, ServiceControllerStatus.Running, maximumWaitTime, true, false)) { TaskLogger.Trace("Service {0} is running", new object[] { text }); } } else if (status == ServiceControllerStatus.PausePending) { TaskLogger.Trace("Service {0} is pausing. Waiting for the service to pause", new object[] { text }); if (this.WaitForServiceStatus(serviceController, ServiceControllerStatus.Paused, maximumWaitTime, true, false)) { TaskLogger.Trace("Service {0} is paused", new object[] { text }); } } else if (status == ServiceControllerStatus.StopPending) { TaskLogger.Trace("Service {0} is already stopping", new object[] { text }); flag = true; } if (!flag) { TaskLogger.Trace("Attempting to stop service {0}", new object[] { text }); try { serviceController.Stop(); } catch (InvalidOperationException ex) { Win32Exception ex2 = ex.InnerException as Win32Exception; if (ex2 != null && ex2.NativeErrorCode == 1058) { this.WriteWarning(Strings.ServiceDisabled(text)); } else if (ex2 != null && ex2.NativeErrorCode == 1053) { if (!ignoreServiceStopTimeout) { base.WriteError(new ServiceDidNotReachStatusException(text, ServiceControllerStatus.Stopped.ToString(), ex), ErrorCategory.InvalidOperation, null); } else { this.WriteWarning(Strings.ServiceDidNotReachStatus(text, ServiceControllerStatus.Stopped.ToString())); } } else { base.WriteError(new ServiceStopFailureException(text, ex.Message, ex), ErrorCategory.InvalidOperation, null); } } } if (this.WaitForServiceStatus(serviceController, ServiceControllerStatus.Stopped, maximumWaitTime, ignoreServiceStopTimeout, false)) { TaskLogger.Trace("Service {0} is stopped", new object[] { text }); } TaskLogger.LogExit(); return; } }
internal void StartService(ServiceController serviceController, bool ignoreServiceStartTimeout, bool failIfServiceNotInstalled, Unlimited <EnhancedTimeSpan> maximumWaitTime, string[] serviceParameters) { if (!ServiceControllerUtils.IsInstalled(serviceController)) { string text; try { text = serviceController.ServiceName; } catch (InvalidOperationException) { text = ""; } TaskLogger.Trace("Service {0} is not installed. Cannot start the service", new object[] { text }); if (failIfServiceNotInstalled) { base.WriteError(new ServiceNotInstalledException(text), ErrorCategory.InvalidOperation, null); return; } this.WriteWarning(Strings.ServiceNotInstalled(text)); return; } else { string text = serviceController.ServiceName; bool flag = false; ServiceControllerStatus status = serviceController.Status; if (status == ServiceControllerStatus.Running) { TaskLogger.Trace("Service {0} is already running. ", new object[] { text }); TaskLogger.LogExit(); return; } foreach (ServiceController serviceController2 in serviceController.ServicesDependedOn) { this.StartService(serviceController2, ignoreServiceStartTimeout, failIfServiceNotInstalled, maximumWaitTime, null); } if (status == ServiceControllerStatus.StopPending) { TaskLogger.Trace("Service {0} is stopping. Waiting for the service to stop", new object[] { text }); if (this.WaitForServiceStatus(serviceController, ServiceControllerStatus.Stopped, maximumWaitTime, true, false)) { TaskLogger.Trace("Service {0} is stopped", new object[] { text }); } } else if (status == ServiceControllerStatus.PausePending) { TaskLogger.Trace("Service {0} is pausing. Waiting for the service to pause", new object[] { text }); if (this.WaitForServiceStatus(serviceController, ServiceControllerStatus.Paused, maximumWaitTime, true, false)) { TaskLogger.Trace("Service {0} is paused", new object[] { text }); } } else if (status == ServiceControllerStatus.ContinuePending || status == ServiceControllerStatus.StartPending) { TaskLogger.Trace("Service {0} is already starting", new object[] { text }); flag = true; } if (!flag) { TaskLogger.Trace("Attempting to start service {0}", new object[] { text }); ManageSetupService.StartServiceWorkerDelegate startServiceWorkerDelegate = new ManageSetupService.StartServiceWorkerDelegate(this.StartServiceWorker); IAsyncResult asyncResult = startServiceWorkerDelegate.BeginInvoke(serviceController, serviceParameters, null, null); if (!asyncResult.AsyncWaitHandle.WaitOne(this.MaxWaitTimeForRunningState.Value, false)) { this.SendWatsonReportForHungService(text); asyncResult.AsyncWaitHandle.WaitOne(); } Exception ex = startServiceWorkerDelegate.EndInvoke(asyncResult); if (ex is InvalidOperationException) { InvalidOperationException ex2 = ex as InvalidOperationException; Win32Exception ex3 = ex2.InnerException as Win32Exception; if (ex3 != null && ex3.NativeErrorCode == 1058) { if (failIfServiceNotInstalled) { base.WriteError(new ServiceDisabledException(text, ex2), ErrorCategory.InvalidOperation, null); return; } this.WriteWarning(Strings.ServiceDisabled(text)); return; } else if (ex3 != null && ex3.NativeErrorCode == 1053) { if (!ignoreServiceStartTimeout) { base.WriteError(new ServiceDidNotReachStatusException(text, ServiceControllerStatus.Running.ToString(), ex2), ErrorCategory.InvalidOperation, null); } else { this.WriteWarning(Strings.ServiceDidNotReachStatus(text, ServiceControllerStatus.Running.ToString())); } } else { base.WriteError(new ServiceStartFailureException(text, ex2.Message, ex2), ErrorCategory.InvalidOperation, null); } } } if (this.WaitForServiceStatus(serviceController, ServiceControllerStatus.Running, maximumWaitTime, ignoreServiceStartTimeout, true)) { TaskLogger.Trace("Service {0} is running", new object[] { text }); } TaskLogger.LogExit(); return; } }
internal static bool IsAdTopologyServiceInstalled() { return(ServiceControllerUtils.IsInstalled("MSExchangeADTopology")); }