Clean up the start code

svn path=/trunk/; revision=44963
This commit is contained in:
Ged Murphy 2010-01-05 21:14:10 +00:00
parent d5bc8625fb
commit 5dfb2cc0c3

View file

@ -3,7 +3,7 @@
* LICENSE: GPL - See COPYING in the top level directory * LICENSE: GPL - See COPYING in the top level directory
* FILE: base/applications/mscutils/servman/start.c * FILE: base/applications/mscutils/servman/start.c
* PURPOSE: Start a service * PURPOSE: Start a service
* COPYRIGHT: Copyright 2005-2009 Ged Murphy <gedmurphy@reactos.org> * COPYRIGHT: Copyright 2005-2010 Ged Murphy <gedmurphy@reactos.org>
* *
*/ */
@ -11,7 +11,7 @@
static BOOL static BOOL
DoStartService(PMAIN_WND_INFO Info, DoStartService(PMAIN_WND_INFO Info,
HWND hProgDlg) HWND hProgress)
{ {
SC_HANDLE hSCManager; SC_HANDLE hSCManager;
SC_HANDLE hService; SC_HANDLE hService;
@ -25,86 +25,99 @@ DoStartService(PMAIN_WND_INFO Info,
hSCManager = OpenSCManager(NULL, hSCManager = OpenSCManager(NULL,
NULL, NULL,
SC_MANAGER_ALL_ACCESS); SC_MANAGER_CONNECT);
if (!hSCManager) if (hSCManager)
{ {
return FALSE;
}
hService = OpenService(hSCManager, hService = OpenService(hSCManager,
Info->pCurrentService->lpServiceName, Info->pCurrentService->lpServiceName,
SERVICE_START | SERVICE_QUERY_STATUS); SERVICE_START | SERVICE_QUERY_STATUS);
if (hService) if (hService)
{ {
if (hProgress)
{
/* Increment the progress bar */
IncrementProgressBar(hProgress, DEFAULT_STEP);
}
bRet = StartService(hService, bRet = StartService(hService,
0, 0,
NULL); NULL);
if (!bRet && GetLastError() == ERROR_SERVICE_ALREADY_RUNNING) if (!bRet && GetLastError() == ERROR_SERVICE_ALREADY_RUNNING)
{ {
/* If it's already running, just return TRUE */
bRet = TRUE; bRet = TRUE;
} }
else if (bRet) else if (bRet)
{ {
bRet = FALSE; bRet = FALSE;
/* Get the service status to check if it's running */
if (QueryServiceStatusEx(hService, if (QueryServiceStatusEx(hService,
SC_STATUS_PROCESS_INFO, SC_STATUS_PROCESS_INFO,
(LPBYTE)&ServiceStatus, (LPBYTE)&ServiceStatus,
sizeof(SERVICE_STATUS_PROCESS), sizeof(SERVICE_STATUS_PROCESS),
&BytesNeeded)) &BytesNeeded))
{ {
/* We don't want to wait for more than 30 seconds */
dwMaxWait = 30000;
dwStartTickCount = GetTickCount(); dwStartTickCount = GetTickCount();
dwOldCheckPoint = ServiceStatus.dwCheckPoint;
dwMaxWait = 30000; // 30 secs
/* Loop until it's running */
while (ServiceStatus.dwCurrentState != SERVICE_RUNNING) while (ServiceStatus.dwCurrentState != SERVICE_RUNNING)
{ {
dwOldCheckPoint = ServiceStatus.dwCheckPoint;
dwWaitTime = ServiceStatus.dwWaitHint / 10; dwWaitTime = ServiceStatus.dwWaitHint / 10;
/* Get the latest status info */
if (!QueryServiceStatusEx(hService, if (!QueryServiceStatusEx(hService,
SC_STATUS_PROCESS_INFO, SC_STATUS_PROCESS_INFO,
(LPBYTE)&ServiceStatus, (LPBYTE)&ServiceStatus,
sizeof(SERVICE_STATUS_PROCESS), sizeof(SERVICE_STATUS_PROCESS),
&BytesNeeded)) &BytesNeeded))
{ {
/* Something went wrong... */
break; break;
} }
/* Is the service making progress? */
if (ServiceStatus.dwCheckPoint > dwOldCheckPoint) if (ServiceStatus.dwCheckPoint > dwOldCheckPoint)
{ {
/* The service is making progress*/ /* It is, get the latest tickcount to reset the max wait time */
dwStartTickCount = GetTickCount(); dwStartTickCount = GetTickCount();
dwOldCheckPoint = ServiceStatus.dwCheckPoint; dwOldCheckPoint = ServiceStatus.dwCheckPoint;
} }
else else
{ {
/* It's not, make sure we haven't exceeded our wait time */
if (GetTickCount() >= dwStartTickCount + dwMaxWait) if (GetTickCount() >= dwStartTickCount + dwMaxWait)
{ {
/* We exceeded our max wait time, give up */ /* We have, give up */
break; break;
} }
} }
/* Adjust the wait hint times */
if (dwWaitTime < 200) if (dwWaitTime < 200)
dwWaitTime = 200; dwWaitTime = 200;
else if (dwWaitTime > 10000) else if (dwWaitTime > 10000)
dwWaitTime = 10000; dwWaitTime = 10000;
/* Wait before trying again */
Sleep(dwWaitTime); Sleep(dwWaitTime);
} }
}
if (ServiceStatus.dwCurrentState == SERVICE_RUNNING) if (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
{ {
bRet = TRUE; bRet = TRUE;
} }
} }
}
CloseServiceHandle(hService); CloseServiceHandle(hService);
} }
CloseServiceHandle(hSCManager); CloseServiceHandle(hSCManager);
}
return bRet; return bRet;
} }
@ -112,31 +125,22 @@ DoStartService(PMAIN_WND_INFO Info,
BOOL BOOL
DoStart(PMAIN_WND_INFO Info) DoStart(PMAIN_WND_INFO Info)
{ {
HWND hProgDlg; HWND hProgress;
BOOL bRet = FALSE; BOOL bRet = FALSE;
hProgDlg = CreateProgressDialog(Info->hMainWnd, /* Create a progress window to track the progress of the stopping service */
hProgress = CreateProgressDialog(Info->hMainWnd,
IDS_PROGRESS_INFO_START); IDS_PROGRESS_INFO_START);
if (hProgress)
if (hProgDlg)
{ {
InitializeProgressDialog(hProgDlg, Info->pCurrentService->lpServiceName); /* Set the service name and reset the progress bag */
InitializeProgressDialog(hProgress, Info->pCurrentService->lpServiceName);
bRet = DoStartService(Info, /* Start the requested service */
hProgDlg); bRet = DoStartService(Info, hProgress);
if (bRet) /* Complete and destroy the progress bar */
{ DestroyProgressDialog(hProgress, bRet);
CompleteProgressBar(hProgDlg);
Sleep(500);
bRet = TRUE;
}
else
{
GetError();
}
DestroyWindow(hProgDlg);
} }
return bRet; return bRet;