Author: ekohl
Date: Sat Mar 5 18:37:39 2011
New Revision: 50977
URL:
http://svn.reactos.org/svn/reactos?rev=50977&view=rev
Log:
[SERVICES]
- Use one instead of two critical sections to synchronize service start and control
actions.
- Fail, if an already running service is started again.
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/datab…
==============================================================================
--- trunk/reactos/base/system/services/database.c [iso-8859-1] (original)
+++ trunk/reactos/base/system/services/database.c [iso-8859-1] Sat Mar 5 18:37:39 2011
@@ -30,8 +30,7 @@
static RTL_RESOURCE DatabaseLock;
static DWORD dwResumeCount = 1;
-static CRITICAL_SECTION NamedPipeCriticalSection;
-static CRITICAL_SECTION StartServiceCriticalSection;
+static CRITICAL_SECTION ControlServiceCriticalSection;
/* FUNCTIONS *****************************************************************/
@@ -693,6 +692,8 @@
DPRINT("ScmControlService() called\n");
+ EnterCriticalSection(&ControlServiceCriticalSection);
+
TotalLength = wcslen(Service->lpServiceName) + 1;
ControlPacket = (SCM_CONTROL_PACKET*)HeapAlloc(GetProcessHeap(),
@@ -705,8 +706,6 @@
ControlPacket->dwSize = TotalLength;
ControlPacket->hServiceStatus = (SERVICE_STATUS_HANDLE)Service;
wcscpy(&ControlPacket->szArguments[0], Service->lpServiceName);
-
- EnterCriticalSection(&NamedPipeCriticalSection);
/* Send the control packet */
WriteFile(Service->ControlPipeHandle,
@@ -722,8 +721,6 @@
&dwReadCount,
NULL);
- LeaveCriticalSection(&NamedPipeCriticalSection);
-
/* Release the contol packet */
HeapFree(GetProcessHeap(),
0,
@@ -733,6 +730,8 @@
{
dwError = ReplyPacket.dwError;
}
+
+ LeaveCriticalSection(&ControlServiceCriticalSection);
DPRINT("ScmControlService() done\n");
@@ -801,8 +800,6 @@
/* Terminate the argument list */
*Ptr = 0;
- EnterCriticalSection(&NamedPipeCriticalSection);
-
/* Send the start command */
WriteFile(Service->ControlPipeHandle,
ControlPacket,
@@ -816,8 +813,6 @@
sizeof(SCM_REPLY_PACKET),
&dwReadCount,
NULL);
-
- LeaveCriticalSection(&NamedPipeCriticalSection);
/* Release the contol packet */
HeapFree(GetProcessHeap(),
@@ -924,6 +919,9 @@
/* Create '\\.\pipe\net\NtControlPipeXXX' instance */
swprintf(NtControlPipeName, L"\\\\.\\pipe\\net\\NtControlPipe%u",
ServiceCurrent);
+
+ DPRINT1("Service: %p ImagePath: %wZ PipeName: %S\n", Service,
&ImagePath, NtControlPipeName);
+
Service->ControlPipeHandle = CreateNamedPipeW(NtControlPipeName,
PIPE_ACCESS_DUPLEX,
PIPE_TYPE_MESSAGE |
PIPE_READMODE_MESSAGE | PIPE_WAIT,
@@ -1036,9 +1034,18 @@
PSERVICE_GROUP Group = Service->lpGroup;
DWORD dwError = ERROR_SUCCESS;
- EnterCriticalSection(&StartServiceCriticalSection);
-
DPRINT("ScmStartService() called\n");
+
+ DPRINT1("Start Service %p (%S)\n", Service, Service->lpServiceName);
+
+ EnterCriticalSection(&ControlServiceCriticalSection);
+
+ if (Service->Status.dwCurrentState != SERVICE_STOPPED)
+ {
+ DPRINT1("Service %S is already running!\n",
Service->lpServiceName);
+ LeaveCriticalSection(&ControlServiceCriticalSection);
+ return ERROR_SERVICE_ALREADY_RUNNING;
+ }
Service->ControlPipeHandle = INVALID_HANDLE_VALUE;
DPRINT("Service->Type: %lu\n", Service->Status.dwServiceType);
@@ -1067,8 +1074,9 @@
}
}
+ LeaveCriticalSection(&ControlServiceCriticalSection);
+
DPRINT("ScmStartService() done (Error %lu)\n", dwError);
- LeaveCriticalSection(&StartServiceCriticalSection);
if (dwError == ERROR_SUCCESS)
{
@@ -1274,16 +1282,14 @@
VOID
ScmInitNamedPipeCriticalSection(VOID)
{
- InitializeCriticalSection(&NamedPipeCriticalSection);
- InitializeCriticalSection(&StartServiceCriticalSection);
+ InitializeCriticalSection(&ControlServiceCriticalSection);
}
VOID
ScmDeleteNamedPipeCriticalSection(VOID)
{
- DeleteCriticalSection(&StartServiceCriticalSection);
- DeleteCriticalSection(&NamedPipeCriticalSection);
+ DeleteCriticalSection(&ControlServiceCriticalSection);
}
/* EOF */