#include "services.h"
+#include <winuser.h>
+
#define NDEBUG
#include <debug.h>
/*
* Uncomment the line below to start services
- * using the SERVICE_START_PENDING state
+ * using the SERVICE_START_PENDING state.
*/
-// #define USE_SERVICE_START_PENDING
+#define USE_SERVICE_START_PENDING
/*
* Uncomment the line below to use asynchronous IO operations
* on the service control pipes.
*/
-// #define USE_ASYNCHRONOUS_IO
+#define USE_ASYNCHRONOUS_IO
/* GLOBALS *******************************************************************/
LIST_ENTRY ServiceListHead;
static RTL_RESOURCE DatabaseLock;
-static DWORD dwResumeCount = 1;
+static DWORD ResumeCount = 1;
+/* The critical section synchronizes service control requests */
static CRITICAL_SECTION ControlServiceCriticalSection;
-static DWORD dwPipeTimeout = 30000; /* 30 Seconds */
+static DWORD PipeTimeout = 30000; /* 30 Seconds */
/* FUNCTIONS *****************************************************************/
}
/* Create '\\.\pipe\net\NtControlPipeXXX' instance */
- swprintf(szControlPipeName, L"\\\\.\\pipe\\net\\NtControlPipe%u", ServiceCurrent);
+ swprintf(szControlPipeName, L"\\\\.\\pipe\\net\\NtControlPipe%lu", ServiceCurrent);
DPRINT("PipeName: %S\n", szControlPipeName);
100,
8000,
4,
- dwPipeTimeout,
+ PipeTimeout,
NULL);
DPRINT("CreateNamedPipeW(%S) done\n", szControlPipeName);
if (pServiceImage->hControlPipe == INVALID_HANDLE_VALUE)
DWORD
ScmCreateNewServiceRecord(LPCWSTR lpServiceName,
- PSERVICE *lpServiceRecord)
+ PSERVICE* lpServiceRecord)
{
PSERVICE lpService = NULL;
lpService->lpDisplayName = lpService->lpServiceName;
/* Set the resume count */
- lpService->dwResumeCount = dwResumeCount++;
+ lpService->dwResumeCount = ResumeCount++;
/* Append service record */
InsertTailList(&ServiceListHead,
if (dwMaxSubkeyLen > sizeof(szNameBuf) / sizeof(WCHAR))
{
/* Name too big: alloc a buffer for it */
- lpszName = HeapAlloc(GetProcessHeap(), 0, dwMaxSubkeyLen * sizeof(WCHAR));
+ lpszName = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwMaxSubkeyLen * sizeof(WCHAR));
}
if (!lpszName)
if (Service->Status.dwServiceType == SERVICE_KERNEL_DRIVER)
{
- RtlInitUnicodeString(&DirName,
- L"\\Driver");
+ RtlInitUnicodeString(&DirName, L"\\Driver");
}
- else
+ else // if (Service->Status.dwServiceType == SERVICE_FILE_SYSTEM_DRIVER)
{
- RtlInitUnicodeString(&DirName,
- L"\\FileSystem");
+ ASSERT(Service->Status.dwServiceType == SERVICE_FILE_SYSTEM_DRIVER);
+ RtlInitUnicodeString(&DirName, L"\\FileSystem");
}
InitializeObjectAttributes(&ObjectAttributes,
}
BufferLength = sizeof(OBJECT_DIRECTORY_INFORMATION) +
- 2 * MAX_PATH * sizeof(WCHAR);
+ 2 * MAX_PATH * sizeof(WCHAR);
DirInfo = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
BufferLength);
DWORD dwError = ERROR_SUCCESS;
BOOL bResult;
#ifdef USE_ASYNCHRONOUS_IO
- OVERLAPPED Overlapped = {0, 0, 0, 0, 0};
+ OVERLAPPED Overlapped = {0};
#endif
DPRINT("ScmControlService() called\n");
+ /* Acquire the service control critical section, to synchronize requests */
EnterCriticalSection(&ControlServiceCriticalSection);
/* Calculate the total length of the start command line */
PacketSize = sizeof(SCM_CONTROL_PACKET);
- PacketSize += (wcslen(Service->lpServiceName) + 1) * sizeof(WCHAR);
+ PacketSize += (DWORD)((wcslen(Service->lpServiceName) + 1) * sizeof(WCHAR));
ControlPacket = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
&Overlapped);
if (bResult == FALSE)
{
- DPRINT1("WriteFile() returned FALSE\n");
+ DPRINT("WriteFile() returned FALSE\n");
dwError = GetLastError();
if (dwError == ERROR_IO_PENDING)
{
- DPRINT1("dwError: ERROR_IO_PENDING\n");
+ DPRINT("dwError: ERROR_IO_PENDING\n");
dwError = WaitForSingleObject(Service->lpImage->hControlPipe,
- dwPipeTimeout);
- DPRINT1("WaitForSingleObject() returned %lu\n", dwError);
+ PipeTimeout);
+ DPRINT("WaitForSingleObject() returned %lu\n", dwError);
if (dwError == WAIT_TIMEOUT)
{
dwError = ERROR_SERVICE_REQUEST_TIMEOUT;
goto Done;
}
- else if (dwError == ERROR_SUCCESS)
+ else if (dwError == WAIT_OBJECT_0)
{
bResult = GetOverlappedResult(Service->lpImage->hControlPipe,
&Overlapped,
&Overlapped);
if (bResult == FALSE)
{
- DPRINT1("ReadFile() returned FALSE\n");
+ DPRINT("ReadFile() returned FALSE\n");
dwError = GetLastError();
if (dwError == ERROR_IO_PENDING)
{
- DPRINT1("dwError: ERROR_IO_PENDING\n");
+ DPRINT("dwError: ERROR_IO_PENDING\n");
dwError = WaitForSingleObject(Service->lpImage->hControlPipe,
- dwPipeTimeout);
- DPRINT1("WaitForSingleObject() returned %lu\n", dwError);
+ PipeTimeout);
+ DPRINT("WaitForSingleObject() returned %lu\n", dwError);
if (dwError == WAIT_TIMEOUT)
{
dwError = ERROR_SERVICE_REQUEST_TIMEOUT;
goto Done;
}
- else if (dwError == ERROR_SUCCESS)
+ else if (dwError == WAIT_OBJECT_0)
{
bResult = GetOverlappedResult(Service->lpImage->hControlPipe,
&Overlapped,
static DWORD
ScmSendStartCommand(PSERVICE Service,
DWORD argc,
- LPWSTR *argv)
+ LPWSTR* argv)
{
PSCM_CONTROL_PACKET ControlPacket;
SCM_REPLY_PACKET ReplyPacket;
PWSTR pArgPtr;
BOOL bResult;
#ifdef USE_ASYNCHRONOUS_IO
- OVERLAPPED Overlapped = {0, 0, 0, 0, 0};
+ OVERLAPPED Overlapped = {0};
#endif
DPRINT("ScmSendStartCommand() called\n");
/* Calculate the total length of the start command line */
PacketSize = sizeof(SCM_CONTROL_PACKET) +
- (wcslen(Service->lpServiceName) + 1) * sizeof(WCHAR);
+ (DWORD)((wcslen(Service->lpServiceName) + 1) * sizeof(WCHAR));
/* Calculate the required packet size for the start arguments */
if (argc > 0 && argv != NULL)
for (i = 0; i < argc; i++)
{
DPRINT("Argv[%lu]: %S\n", i, argv[i]);
- PacketSize += (wcslen(argv[i]) + 1) * sizeof(WCHAR) + sizeof(PWSTR);
+ PacketSize += (DWORD)((wcslen(argv[i]) + 1) * sizeof(WCHAR) + sizeof(PWSTR));
}
}
&Overlapped);
if (bResult == FALSE)
{
- DPRINT1("WriteFile() returned FALSE\n");
+ DPRINT("WriteFile() returned FALSE\n");
dwError = GetLastError();
if (dwError == ERROR_IO_PENDING)
{
- DPRINT1("dwError: ERROR_IO_PENDING\n");
+ DPRINT("dwError: ERROR_IO_PENDING\n");
dwError = WaitForSingleObject(Service->lpImage->hControlPipe,
- dwPipeTimeout);
- DPRINT1("WaitForSingleObject() returned %lu\n", dwError);
+ PipeTimeout);
+ DPRINT("WaitForSingleObject() returned %lu\n", dwError);
if (dwError == WAIT_TIMEOUT)
{
dwError = ERROR_SERVICE_REQUEST_TIMEOUT;
goto Done;
}
- else if (dwError == ERROR_SUCCESS)
+ else if (dwError == WAIT_OBJECT_0)
{
bResult = GetOverlappedResult(Service->lpImage->hControlPipe,
&Overlapped,
&Overlapped);
if (bResult == FALSE)
{
- DPRINT1("ReadFile() returned FALSE\n");
+ DPRINT("ReadFile() returned FALSE\n");
dwError = GetLastError();
if (dwError == ERROR_IO_PENDING)
{
- DPRINT1("dwError: ERROR_IO_PENDING\n");
+ DPRINT("dwError: ERROR_IO_PENDING\n");
dwError = WaitForSingleObject(Service->lpImage->hControlPipe,
- dwPipeTimeout);
- DPRINT1("WaitForSingleObject() returned %lu\n", dwError);
+ PipeTimeout);
+ DPRINT("WaitForSingleObject() returned %lu\n", dwError);
if (dwError == WAIT_TIMEOUT)
{
dwError = ERROR_SERVICE_REQUEST_TIMEOUT;
goto Done;
}
- else if (dwError == ERROR_SUCCESS)
+ else if (dwError == WAIT_OBJECT_0)
{
bResult = GetOverlappedResult(Service->lpImage->hControlPipe,
&Overlapped,
DWORD dwError = ERROR_SUCCESS;
BOOL bResult;
#ifdef USE_ASYNCHRONOUS_IO
- OVERLAPPED Overlapped = {0, 0, 0, 0, 0};
+ OVERLAPPED Overlapped = {0};
#endif
+ LPCWSTR lpErrorStrings[3];
+ WCHAR szBuffer1[20];
+ WCHAR szBuffer2[20];
DPRINT("ScmWaitForServiceConnect()\n");
DPRINT("dwError: ERROR_IO_PENDING\n");
dwError = WaitForSingleObject(Service->lpImage->hControlPipe,
- dwPipeTimeout);
+ PipeTimeout);
DPRINT("WaitForSingleObject() returned %lu\n", dwError);
if (dwError == WAIT_TIMEOUT)
DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
}
+ _ultow(PipeTimeout, szBuffer1, 10);
+ lpErrorStrings[0] = Service->lpDisplayName;
+ lpErrorStrings[1] = szBuffer1;
+
+ ScmLogEvent(EVENT_CONNECTION_TIMEOUT,
+ EVENTLOG_ERROR_TYPE,
+ 2,
+ lpErrorStrings);
+
return ERROR_SERVICE_REQUEST_TIMEOUT;
}
else if (dwError == WAIT_OBJECT_0)
DPRINT("dwError: ERROR_IO_PENDING\n");
dwError = WaitForSingleObject(Service->lpImage->hControlPipe,
- dwPipeTimeout);
+ PipeTimeout);
if (dwError == WAIT_TIMEOUT)
{
DPRINT("WaitForSingleObject() returned WAIT_TIMEOUT\n");
DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
}
+ _ultow(PipeTimeout, szBuffer1, 10);
+ lpErrorStrings[0] = szBuffer1;
+
+ ScmLogEvent(EVENT_READFILE_TIMEOUT,
+ EVENTLOG_ERROR_TYPE,
+ 1,
+ lpErrorStrings);
+
return ERROR_SERVICE_REQUEST_TIMEOUT;
}
- else if (dwError == ERROR_SUCCESS)
+ else if (dwError == WAIT_OBJECT_0)
{
- DPRINT("WaitForSingleObject() returned ERROR_SUCCESS\n");
+ DPRINT("WaitForSingleObject() returned WAIT_OBJECT_0\n");
DPRINT("Process Id: %lu\n", dwProcessId);
}
}
- DPRINT1("ScmWaitForServiceConnect() done\n");
+ if (dwProcessId != Service->lpImage->dwProcessId)
+ {
+ _ultow(Service->lpImage->dwProcessId, szBuffer1, 10);
+ _ultow(dwProcessId, szBuffer2, 10);
+
+ lpErrorStrings[0] = Service->lpDisplayName;
+ lpErrorStrings[1] = szBuffer1;
+ lpErrorStrings[2] = szBuffer2;
+
+ ScmLogEvent(EVENT_SERVICE_DIFFERENT_PID_CONNECTED,
+ EVENTLOG_WARNING_TYPE,
+ 3,
+ lpErrorStrings);
+ }
+
+ DPRINT("ScmWaitForServiceConnect() done\n");
return ERROR_SUCCESS;
#else
static DWORD
ScmStartUserModeService(PSERVICE Service,
DWORD argc,
- LPWSTR *argv)
+ LPWSTR* argv)
{
PROCESS_INFORMATION ProcessInformation;
STARTUPINFOW StartupInfo;
return ScmSendStartCommand(Service, argc, argv);
}
+ /* Otherwise start its process */
+ ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof(StartupInfo);
- StartupInfo.lpReserved = NULL;
- StartupInfo.lpDesktop = NULL;
- StartupInfo.lpTitle = NULL;
- StartupInfo.dwFlags = 0;
- StartupInfo.cbReserved2 = 0;
- StartupInfo.lpReserved2 = 0;
+ ZeroMemory(&ProcessInformation, sizeof(ProcessInformation));
Result = CreateProcessW(NULL,
Service->lpImage->szImagePath,
if (dwError == ERROR_SUCCESS)
{
/* Send start command */
- dwError = ScmSendStartCommand(Service,
- argc,
- argv);
+ dwError = ScmSendStartCommand(Service, argc, argv);
}
else
{
}
-DWORD
-ScmStartService(PSERVICE Service, DWORD argc, LPWSTR *argv)
+static DWORD
+ScmLoadService(PSERVICE Service,
+ DWORD argc,
+ LPWSTR* argv)
{
PSERVICE_GROUP Group = Service->lpGroup;
DWORD dwError = ERROR_SUCCESS;
- LPCWSTR ErrorLogStrings[2];
+ LPCWSTR lpErrorStrings[2];
WCHAR szErrorBuffer[32];
- DPRINT("ScmStartService() called\n");
-
+ DPRINT("ScmLoadService() called\n");
DPRINT("Start Service %p (%S)\n", Service, Service->lpServiceName);
- EnterCriticalSection(&ControlServiceCriticalSection);
-
if (Service->Status.dwCurrentState != SERVICE_STOPPED)
{
DPRINT("Service %S is already running!\n", Service->lpServiceName);
- LeaveCriticalSection(&ControlServiceCriticalSection);
return ERROR_SERVICE_ALREADY_RUNNING;
}
Service->Status.dwCurrentState = SERVICE_RUNNING;
}
}
- else
+ else // if (Service->Status.dwServiceType & (SERVICE_WIN32 | SERVICE_INTERACTIVE_PROCESS))
{
/* Start user-mode service */
dwError = ScmCreateOrReferenceServiceImage(Service);
}
}
- LeaveCriticalSection(&ControlServiceCriticalSection);
-
- DPRINT("ScmStartService() done (Error %lu)\n", dwError);
+ DPRINT("ScmLoadService() done (Error %lu)\n", dwError);
if (dwError == ERROR_SUCCESS)
{
{
Group->ServicesRunning = TRUE;
}
+
+ /* Log a successful service start */
+ lpErrorStrings[0] = Service->lpDisplayName;
+ lpErrorStrings[1] = L"start";
+ ScmLogEvent(EVENT_SERVICE_CONTROL_SUCCESS,
+ EVENTLOG_INFORMATION_TYPE,
+ 2,
+ lpErrorStrings);
}
else
{
{
/* Log a failed service start */
swprintf(szErrorBuffer, L"%lu", dwError);
- ErrorLogStrings[0] = Service->lpServiceName;
- ErrorLogStrings[1] = szErrorBuffer;
- ScmLogError(EVENT_SERVICE_START_FAILED,
+ lpErrorStrings[0] = Service->lpServiceName;
+ lpErrorStrings[1] = szErrorBuffer;
+ ScmLogEvent(EVENT_SERVICE_START_FAILED,
+ EVENTLOG_ERROR_TYPE,
2,
- ErrorLogStrings);
+ lpErrorStrings);
}
#if 0
}
+DWORD
+ScmStartService(PSERVICE Service,
+ DWORD argc,
+ LPWSTR* argv)
+{
+ DWORD dwError = ERROR_SUCCESS;
+ SC_RPC_LOCK Lock = NULL;
+
+ DPRINT("ScmStartService() called\n");
+ DPRINT("Start Service %p (%S)\n", Service, Service->lpServiceName);
+
+ /* Acquire the service control critical section, to synchronize starts */
+ EnterCriticalSection(&ControlServiceCriticalSection);
+
+ /*
+ * Acquire the user service start lock while the service is starting, if
+ * needed (i.e. if we are not starting it during the initialization phase).
+ * If we don't success, bail out.
+ */
+ if (!ScmInitialize)
+ {
+ dwError = ScmAcquireServiceStartLock(TRUE, &Lock);
+ if (dwError != ERROR_SUCCESS) goto done;
+ }
+
+ /* Really start the service */
+ dwError = ScmLoadService(Service, argc, argv);
+
+ /* Release the service start lock, if needed, and the critical section */
+ if (Lock) ScmReleaseServiceStartLock(&Lock);
+
+done:
+ LeaveCriticalSection(&ControlServiceCriticalSection);
+
+ DPRINT("ScmStartService() done (Error %lu)\n", dwError);
+
+ return dwError;
+}
+
+
VOID
ScmAutoStartServices(VOID)
{
+ DWORD dwError = ERROR_SUCCESS;
PLIST_ENTRY GroupEntry;
PLIST_ENTRY ServiceEntry;
PSERVICE_GROUP CurrentGroup;
PSERVICE CurrentService;
WCHAR szSafeBootServicePath[MAX_PATH];
- DWORD dwError;
HKEY hKey;
ULONG i;
+ /*
+ * This function MUST be called ONLY at initialization time.
+ * Therefore, no need to acquire the user service start lock.
+ */
+ ASSERT(ScmInitialize);
+
+ /* Acquire the service control critical section, to synchronize starts */
+ EnterCriticalSection(&ControlServiceCriticalSection);
+
/* Clear 'ServiceVisited' flag (or set if not to start in Safe Mode) */
ServiceEntry = ServiceListHead.Flink;
while (ServiceEntry != &ServiceListHead)
(CurrentService->dwTag == CurrentGroup->TagArray[i]))
{
CurrentService->ServiceVisited = TRUE;
- ScmStartService(CurrentService, 0, NULL);
+ ScmLoadService(CurrentService, 0, NULL);
}
ServiceEntry = ServiceEntry->Flink;
(CurrentService->ServiceVisited == FALSE))
{
CurrentService->ServiceVisited = TRUE;
- ScmStartService(CurrentService, 0, NULL);
+ ScmLoadService(CurrentService, 0, NULL);
}
ServiceEntry = ServiceEntry->Flink;
(CurrentService->ServiceVisited == FALSE))
{
CurrentService->ServiceVisited = TRUE;
- ScmStartService(CurrentService, 0, NULL);
+ ScmLoadService(CurrentService, 0, NULL);
}
ServiceEntry = ServiceEntry->Flink;
(CurrentService->ServiceVisited == FALSE))
{
CurrentService->ServiceVisited = TRUE;
- ScmStartService(CurrentService, 0, NULL);
+ ScmLoadService(CurrentService, 0, NULL);
}
ServiceEntry = ServiceEntry->Flink;
CurrentService->ServiceVisited = FALSE;
ServiceEntry = ServiceEntry->Flink;
}
+
+ /* Release the critical section */
+ LeaveCriticalSection(&ControlServiceCriticalSection);
}
L"ServicesPipeTimeout",
0,
NULL,
- (LPBYTE)&dwPipeTimeout,
+ (LPBYTE)&PipeTimeout,
&dwKeySize);
RegCloseKey(hKey);