Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygencontrol.c
Go to the documentation of this file.
00001 /* 00002 * PROJECT: ReactOS Services 00003 * LICENSE: GPL - See COPYING in the top level directory 00004 * FILE: base/applications/mscutils/servman/control.c 00005 * PURPOSE: Pauses and resumes a service 00006 * COPYRIGHT: Copyright 2006-2010 Ged Murphy <gedmurphy@reactos.org> 00007 * 00008 */ 00009 00010 #include "precomp.h" 00011 00012 static BOOL 00013 DoControl(PMAIN_WND_INFO Info, 00014 HWND hProgress, 00015 DWORD Control) 00016 { 00017 SC_HANDLE hSCManager; 00018 SC_HANDLE hService; 00019 SERVICE_STATUS_PROCESS ServiceStatus = {0}; 00020 SERVICE_STATUS Status; 00021 DWORD BytesNeeded = 0; 00022 DWORD dwStartTickCount; 00023 DWORD dwOldCheckPoint; 00024 DWORD dwWaitTime; 00025 DWORD dwMaxWait; 00026 DWORD dwReqState; 00027 BOOL bRet = FALSE; 00028 00029 /* Set the state we're interested in */ 00030 switch (Control) 00031 { 00032 case SERVICE_CONTROL_PAUSE: 00033 dwReqState = SERVICE_PAUSED; 00034 break; 00035 case SERVICE_CONTROL_CONTINUE: 00036 dwReqState = SERVICE_RUNNING; 00037 break; 00038 default: 00039 /* Unhandled control code */ 00040 return FALSE; 00041 } 00042 00043 hSCManager = OpenSCManager(NULL, 00044 NULL, 00045 SC_MANAGER_CONNECT); 00046 if (hSCManager) 00047 { 00048 hService = OpenService(hSCManager, 00049 Info->pCurrentService->lpServiceName, 00050 SERVICE_PAUSE_CONTINUE | SERVICE_INTERROGATE | SERVICE_QUERY_STATUS); 00051 if (hService) 00052 { 00053 if (hProgress) 00054 { 00055 /* Increment the progress bar */ 00056 IncrementProgressBar(hProgress, DEFAULT_STEP); 00057 } 00058 00059 /* Send the control message to the service */ 00060 if (ControlService(hService, 00061 Control, 00062 &Status)) 00063 { 00064 /* Get the service status */ 00065 if (QueryServiceStatusEx(hService, 00066 SC_STATUS_PROCESS_INFO, 00067 (LPBYTE)&ServiceStatus, 00068 sizeof(SERVICE_STATUS_PROCESS), 00069 &BytesNeeded)) 00070 { 00071 /* We don't want to wait for more than 30 seconds */ 00072 dwMaxWait = 30000; 00073 dwStartTickCount = GetTickCount(); 00074 00075 /* Loop until it's at the correct state */ 00076 while (ServiceStatus.dwCurrentState != dwReqState) 00077 { 00078 dwOldCheckPoint = ServiceStatus.dwCheckPoint; 00079 dwWaitTime = ServiceStatus.dwWaitHint / 10; 00080 00081 /* Get the latest status info */ 00082 if (!QueryServiceStatusEx(hService, 00083 SC_STATUS_PROCESS_INFO, 00084 (LPBYTE)&ServiceStatus, 00085 sizeof(SERVICE_STATUS_PROCESS), 00086 &BytesNeeded)) 00087 { 00088 /* Something went wrong... */ 00089 break; 00090 } 00091 00092 /* Is the service making progress? */ 00093 if (ServiceStatus.dwCheckPoint > dwOldCheckPoint) 00094 { 00095 /* It is, get the latest tickcount to reset the max wait time */ 00096 dwStartTickCount = GetTickCount(); 00097 dwOldCheckPoint = ServiceStatus.dwCheckPoint; 00098 IncrementProgressBar(hProgress, DEFAULT_STEP); 00099 } 00100 else 00101 { 00102 /* It's not, make sure we haven't exceeded our wait time */ 00103 if(GetTickCount() >= dwStartTickCount + dwMaxWait) 00104 { 00105 /* We have, give up */ 00106 break; 00107 } 00108 } 00109 00110 /* Adjust the wait hint times */ 00111 if (dwWaitTime < 200) 00112 dwWaitTime = 200; 00113 else if (dwWaitTime > 10000) 00114 dwWaitTime = 10000; 00115 00116 /* Wait before trying again */ 00117 Sleep(dwWaitTime); 00118 } 00119 } 00120 00121 if (ServiceStatus.dwCurrentState == dwReqState) 00122 { 00123 bRet = TRUE; 00124 } 00125 } 00126 00127 CloseServiceHandle(hService); 00128 } 00129 00130 CloseServiceHandle(hSCManager); 00131 } 00132 00133 return bRet; 00134 } 00135 00136 00137 BOOL 00138 DoPause(PMAIN_WND_INFO Info) 00139 { 00140 HWND hProgress; 00141 BOOL bRet = FALSE; 00142 00143 /* Create a progress window to track the progress of the pausing service */ 00144 hProgress = CreateProgressDialog(Info->hMainWnd, 00145 IDS_PROGRESS_INFO_PAUSE); 00146 if (hProgress) 00147 { 00148 /* Set the service name and reset the progress bag */ 00149 InitializeProgressDialog(hProgress, Info->pCurrentService->lpServiceName); 00150 00151 /* Resume the requested service */ 00152 bRet = DoControl(Info, hProgress, SERVICE_CONTROL_PAUSE); 00153 00154 /* Complete and destroy the progress bar */ 00155 DestroyProgressDialog(hProgress, bRet); 00156 } 00157 00158 return bRet; 00159 } 00160 00161 00162 BOOL 00163 DoResume(PMAIN_WND_INFO Info) 00164 { 00165 HWND hProgress; 00166 BOOL bRet = FALSE; 00167 00168 /* Create a progress window to track the progress of the resuming service */ 00169 hProgress = CreateProgressDialog(Info->hMainWnd, 00170 IDS_PROGRESS_INFO_RESUME); 00171 if (hProgress) 00172 { 00173 /* Set the service name and reset the progress bag */ 00174 InitializeProgressDialog(hProgress, Info->pCurrentService->lpServiceName); 00175 00176 /* Resume the requested service */ 00177 bRet = DoControl(Info, hProgress, SERVICE_CONTROL_CONTINUE); 00178 00179 /* Complete and destroy the progress bar */ 00180 DestroyProgressDialog(hProgress, bRet); 00181 } 00182 00183 return bRet; 00184 } Generated on Wed May 23 2012 04:14:55 for ReactOS by
1.7.6.1
|