2006-11-08 11:47:44 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS Services
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
2007-08-27 19:31:09 +00:00
|
|
|
* FILE: base/applications/mscutils/servman/control.c
|
2010-01-05 21:43:00 +00:00
|
|
|
* PURPOSE: Pauses and resumes a service
|
|
|
|
* COPYRIGHT: Copyright 2006-2010 Ged Murphy <gedmurphy@reactos.org>
|
2006-11-08 11:47:44 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precomp.h"
|
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
static BOOL
|
|
|
|
DoControl(PMAIN_WND_INFO Info,
|
|
|
|
HWND hProgress,
|
|
|
|
DWORD Control)
|
2006-11-08 11:47:44 +00:00
|
|
|
{
|
|
|
|
SC_HANDLE hSCManager;
|
2010-01-05 21:43:00 +00:00
|
|
|
SC_HANDLE hService;
|
2009-04-29 10:18:41 +00:00
|
|
|
SERVICE_STATUS_PROCESS ServiceStatus = {0};
|
2006-11-08 11:47:44 +00:00
|
|
|
SERVICE_STATUS Status;
|
|
|
|
DWORD BytesNeeded = 0;
|
2010-01-05 21:43:00 +00:00
|
|
|
DWORD dwStartTickCount;
|
|
|
|
DWORD dwOldCheckPoint;
|
|
|
|
DWORD dwWaitTime;
|
|
|
|
DWORD dwMaxWait;
|
2010-01-06 13:10:09 +00:00
|
|
|
DWORD dwReqState;
|
2007-09-12 13:50:51 +00:00
|
|
|
BOOL bRet = FALSE;
|
2006-11-08 11:47:44 +00:00
|
|
|
|
2010-01-06 13:10:09 +00:00
|
|
|
/* Set the state we're interested in */
|
|
|
|
switch (Control)
|
|
|
|
{
|
|
|
|
case SERVICE_CONTROL_PAUSE:
|
|
|
|
dwReqState = SERVICE_PAUSED;
|
|
|
|
break;
|
|
|
|
case SERVICE_CONTROL_CONTINUE:
|
|
|
|
dwReqState = SERVICE_RUNNING;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Unhandled control code */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2006-11-08 11:47:44 +00:00
|
|
|
hSCManager = OpenSCManager(NULL,
|
|
|
|
NULL,
|
2010-01-05 21:43:00 +00:00
|
|
|
SC_MANAGER_CONNECT);
|
|
|
|
if (hSCManager)
|
2006-11-08 11:47:44 +00:00
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
hService = OpenService(hSCManager,
|
|
|
|
Info->pCurrentService->lpServiceName,
|
2010-01-06 13:10:09 +00:00
|
|
|
SERVICE_PAUSE_CONTINUE | SERVICE_INTERROGATE | SERVICE_QUERY_STATUS);
|
2010-01-05 21:43:00 +00:00
|
|
|
if (hService)
|
2006-11-08 11:47:44 +00:00
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
if (hProgress)
|
|
|
|
{
|
|
|
|
/* Increment the progress bar */
|
|
|
|
IncrementProgressBar(hProgress, DEFAULT_STEP);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the control message to the service */
|
|
|
|
if (ControlService(hService,
|
2007-09-12 13:50:51 +00:00
|
|
|
Control,
|
|
|
|
&Status))
|
2006-11-08 11:47:44 +00:00
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Get the service status */
|
|
|
|
if (QueryServiceStatusEx(hService,
|
2007-09-12 13:50:51 +00:00
|
|
|
SC_STATUS_PROCESS_INFO,
|
|
|
|
(LPBYTE)&ServiceStatus,
|
|
|
|
sizeof(SERVICE_STATUS_PROCESS),
|
|
|
|
&BytesNeeded))
|
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
/* We don't want to wait for more than 30 seconds */
|
|
|
|
dwMaxWait = 30000;
|
|
|
|
dwStartTickCount = GetTickCount();
|
2007-09-12 13:50:51 +00:00
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Loop until it's at the correct state */
|
2010-01-06 13:10:09 +00:00
|
|
|
while (ServiceStatus.dwCurrentState != dwReqState)
|
2007-09-12 13:50:51 +00:00
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
dwOldCheckPoint = ServiceStatus.dwCheckPoint;
|
|
|
|
dwWaitTime = ServiceStatus.dwWaitHint / 10;
|
2007-09-12 13:50:51 +00:00
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Get the latest status info */
|
|
|
|
if (!QueryServiceStatusEx(hService,
|
2007-09-12 13:50:51 +00:00
|
|
|
SC_STATUS_PROCESS_INFO,
|
|
|
|
(LPBYTE)&ServiceStatus,
|
|
|
|
sizeof(SERVICE_STATUS_PROCESS),
|
|
|
|
&BytesNeeded))
|
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Something went wrong... */
|
2007-09-12 13:50:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Is the service making progress? */
|
2007-09-12 13:50:51 +00:00
|
|
|
if (ServiceStatus.dwCheckPoint > dwOldCheckPoint)
|
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
/* It is, get the latest tickcount to reset the max wait time */
|
2007-09-12 13:50:51 +00:00
|
|
|
dwStartTickCount = GetTickCount();
|
|
|
|
dwOldCheckPoint = ServiceStatus.dwCheckPoint;
|
2010-01-05 21:43:00 +00:00
|
|
|
IncrementProgressBar(hProgress, DEFAULT_STEP);
|
2007-09-12 13:50:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
/* It's not, make sure we haven't exceeded our wait time */
|
2007-09-12 13:50:51 +00:00
|
|
|
if(GetTickCount() >= dwStartTickCount + dwMaxWait)
|
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
/* We have, give up */
|
2007-09-12 13:50:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Adjust the wait hint times */
|
|
|
|
if (dwWaitTime < 200)
|
2007-09-12 13:50:51 +00:00
|
|
|
dwWaitTime = 200;
|
|
|
|
else if (dwWaitTime > 10000)
|
|
|
|
dwWaitTime = 10000;
|
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Wait before trying again */
|
2007-09-12 13:50:51 +00:00
|
|
|
Sleep(dwWaitTime);
|
|
|
|
}
|
|
|
|
}
|
2010-01-05 21:43:00 +00:00
|
|
|
|
2010-01-06 13:10:09 +00:00
|
|
|
if (ServiceStatus.dwCurrentState == dwReqState)
|
2010-01-05 21:43:00 +00:00
|
|
|
{
|
|
|
|
bRet = TRUE;
|
|
|
|
}
|
2006-11-08 11:47:44 +00:00
|
|
|
}
|
2007-10-19 23:21:45 +00:00
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
CloseServiceHandle(hService);
|
2006-11-08 11:47:44 +00:00
|
|
|
}
|
|
|
|
|
2007-09-12 13:50:51 +00:00
|
|
|
CloseServiceHandle(hSCManager);
|
|
|
|
}
|
2006-11-08 11:47:44 +00:00
|
|
|
|
2007-09-12 13:50:51 +00:00
|
|
|
return bRet;
|
2006-11-08 11:47:44 +00:00
|
|
|
}
|
2007-08-25 14:51:36 +00:00
|
|
|
|
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
BOOL
|
|
|
|
DoPause(PMAIN_WND_INFO Info)
|
2007-08-25 14:51:36 +00:00
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
HWND hProgress;
|
|
|
|
BOOL bRet = FALSE;
|
2007-08-25 14:51:36 +00:00
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Create a progress window to track the progress of the pausing service */
|
|
|
|
hProgress = CreateProgressDialog(Info->hMainWnd,
|
|
|
|
IDS_PROGRESS_INFO_PAUSE);
|
|
|
|
if (hProgress)
|
2007-08-25 14:51:36 +00:00
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Set the service name and reset the progress bag */
|
|
|
|
InitializeProgressDialog(hProgress, Info->pCurrentService->lpServiceName);
|
2010-01-05 20:41:14 +00:00
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Resume the requested service */
|
|
|
|
bRet = DoControl(Info, hProgress, SERVICE_CONTROL_PAUSE);
|
2007-08-25 14:51:36 +00:00
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Complete and destroy the progress bar */
|
|
|
|
DestroyProgressDialog(hProgress, bRet);
|
2007-08-25 14:51:36 +00:00
|
|
|
}
|
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
return bRet;
|
2007-08-25 14:51:36 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 09:07:38 +00:00
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
BOOL
|
|
|
|
DoResume(PMAIN_WND_INFO Info)
|
2007-08-25 14:51:36 +00:00
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
HWND hProgress;
|
|
|
|
BOOL bRet = FALSE;
|
2007-08-25 14:51:36 +00:00
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Create a progress window to track the progress of the resuming service */
|
|
|
|
hProgress = CreateProgressDialog(Info->hMainWnd,
|
|
|
|
IDS_PROGRESS_INFO_RESUME);
|
|
|
|
if (hProgress)
|
2007-08-25 14:51:36 +00:00
|
|
|
{
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Set the service name and reset the progress bag */
|
|
|
|
InitializeProgressDialog(hProgress, Info->pCurrentService->lpServiceName);
|
2010-01-05 20:41:14 +00:00
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Resume the requested service */
|
|
|
|
bRet = DoControl(Info, hProgress, SERVICE_CONTROL_CONTINUE);
|
2007-08-25 14:51:36 +00:00
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
/* Complete and destroy the progress bar */
|
|
|
|
DestroyProgressDialog(hProgress, bRet);
|
2007-08-25 14:51:36 +00:00
|
|
|
}
|
|
|
|
|
2010-01-05 21:43:00 +00:00
|
|
|
return bRet;
|
2007-08-25 14:51:36 +00:00
|
|
|
}
|