Author: ekohl Date: Sat Dec 3 14:33:07 2016 New Revision: 73420
URL: http://svn.reactos.org/svn/reactos?rev=73420&view=rev Log: [SERVICES] - Code cleanup. - ScmCreateOrReferenceServiceImage() and ScmDereferenceServiceImage(): Close the logon token and service process handles properly. - ScmStartUserModeService(): Store the service process handle in the SERVICE_IMAGE struct. Patch by Hermès BÉLUSCA - MAÏTO.
Modified: trunk/reactos/base/system/services/database.c
Modified: trunk/reactos/base/system/services/database.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/system/services/databa... ============================================================================== --- trunk/reactos/base/system/services/database.c [iso-8859-1] (original) +++ trunk/reactos/base/system/services/database.c [iso-8859-1] Sat Dec 3 14:33:07 2016 @@ -153,8 +153,8 @@ static BOOL ScmIsSameServiceAccount( - IN PWSTR pszAccountName1, - IN PWSTR pszAccountName2) + _In_ PCWSTR pszAccountName1, + _In_ PCWSTR pszAccountName2) { if (pszAccountName1 == NULL && pszAccountName2 == NULL) return TRUE; @@ -175,7 +175,7 @@ static BOOL ScmIsLocalSystemAccount( - IN PWSTR pszAccountName) + _In_ PCWSTR pszAccountName) { if (pszAccountName == NULL || wcscmp(pszAccountName, L"LocalSystem") == 0) @@ -306,6 +306,7 @@
pServiceImage->dwImageRunCount = 1; pServiceImage->hControlPipe = INVALID_HANDLE_VALUE; + pServiceImage->hProcess = INVALID_HANDLE_VALUE;
pString = (PWSTR)((INT_PTR)pServiceImage + sizeof(SERVICE_IMAGE));
@@ -329,6 +330,10 @@ if (dwError != ERROR_SUCCESS) { DPRINT1("ScmLogonService() failed (Error %lu)\n", dwError); + + /* Release the service image */ + HeapFree(GetProcessHeap(), 0, pServiceImage); + goto done; }
@@ -336,7 +341,15 @@ dwError = ScmCreateNewControlPipe(pServiceImage); if (dwError != ERROR_SUCCESS) { + DPRINT1("ScmCreateNewControlPipe() failed (Error %lu)\n", dwError); + + /* Close the logon token */ + if (pServiceImage->hToken != NULL) + CloseHandle(pServiceImage->hToken); + + /* Release the service image */ HeapFree(GetProcessHeap(), 0, pServiceImage); + goto done; }
@@ -395,13 +408,17 @@ /* Remove the service image from the list */ RemoveEntryList(&pServiceImage->ImageListEntry);
+ /* Close the process handle */ + if (pServiceImage->hProcess != INVALID_HANDLE_VALUE) + CloseHandle(pServiceImage->hProcess); + + /* Close the control pipe */ + if (pServiceImage->hControlPipe != INVALID_HANDLE_VALUE) + CloseHandle(pServiceImage->hControlPipe); + /* Close the logon token */ if (pServiceImage->hToken != NULL) CloseHandle(pServiceImage->hToken); - - /* Close the control pipe */ - if (pServiceImage->hControlPipe != INVALID_HANDLE_VALUE) - CloseHandle(pServiceImage->hControlPipe);
/* Release the service image */ HeapFree(GetProcessHeap(), 0, pServiceImage); @@ -1706,30 +1723,25 @@ ProcessInformation.dwThreadId, ProcessInformation.hThread);
- /* Get process handle and id */ + /* Get the process handle and ID */ + Service->lpImage->hProcess = ProcessInformation.hProcess; Service->lpImage->dwProcessId = ProcessInformation.dwProcessId;
- /* Resume Thread */ + /* Resume the main thread and close its handle */ ResumeThread(ProcessInformation.hThread); + CloseHandle(ProcessInformation.hThread);
/* Connect control pipe */ dwError = ScmWaitForServiceConnect(Service); - if (dwError == ERROR_SUCCESS) - { - /* Send start command */ - dwError = ScmSendStartCommand(Service, argc, argv); - } - else + if (dwError != ERROR_SUCCESS) { DPRINT1("Connecting control pipe failed! (Error %lu)\n", dwError); Service->lpImage->dwProcessId = 0; - } - - /* Close thread and process handle */ - CloseHandle(ProcessInformation.hThread); - CloseHandle(ProcessInformation.hProcess); - - return dwError; + return dwError; + } + + /* Send the start command */ + return ScmSendStartCommand(Service, argc, argv); }