2 * PROJECT: ReactOS Service Control Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/services/database.c
5 * PURPOSE: Database control interface
6 * COPYRIGHT: Copyright 2002-2006 Eric Kohl
7 * Copyright 2006 Hervé Poussineau <hpoussin@reactos.org>
8 * Copyright 2007 Ged Murphy <gedmurphy@reactos.org>
9 * Gregor Brunmar <gregor.brunmar@home.se>
13 /* INCLUDES *****************************************************************/
23 * Uncomment the line below to start services
24 * using the SERVICE_START_PENDING state.
26 #define USE_SERVICE_START_PENDING
29 * Uncomment the line below to use asynchronous IO operations
30 * on the service control pipes.
32 #define USE_ASYNCHRONOUS_IO
35 /* GLOBALS *******************************************************************/
37 LIST_ENTRY ImageListHead
;
38 LIST_ENTRY ServiceListHead
;
40 static RTL_RESOURCE DatabaseLock
;
41 static DWORD ResumeCount
= 1;
43 /* The critical section synchronizes service control requests */
44 static CRITICAL_SECTION ControlServiceCriticalSection
;
45 static DWORD PipeTimeout
= 30000; /* 30 Seconds */
48 /* FUNCTIONS *****************************************************************/
51 ScmCreateNewControlPipe(PSERVICE_IMAGE pServiceImage
)
53 WCHAR szControlPipeName
[MAX_PATH
+ 1];
54 HKEY hServiceCurrentKey
= INVALID_HANDLE_VALUE
;
55 DWORD ServiceCurrent
= 0;
60 /* Get the service number */
61 /* TODO: Create registry entry with correct write access */
62 dwError
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
63 L
"SYSTEM\\CurrentControlSet\\Control\\ServiceCurrent", 0, NULL
,
69 if (dwError
!= ERROR_SUCCESS
)
71 DPRINT1("RegCreateKeyEx() failed with error %lu\n", dwError
);
75 if (KeyDisposition
== REG_OPENED_EXISTING_KEY
)
77 dwKeySize
= sizeof(DWORD
);
78 dwError
= RegQueryValueExW(hServiceCurrentKey
,
79 L
"", 0, NULL
, (BYTE
*)&ServiceCurrent
, &dwKeySize
);
81 if (dwError
!= ERROR_SUCCESS
)
83 RegCloseKey(hServiceCurrentKey
);
84 DPRINT1("RegQueryValueEx() failed with error %lu\n", dwError
);
91 dwError
= RegSetValueExW(hServiceCurrentKey
, L
"", 0, REG_DWORD
, (BYTE
*)&ServiceCurrent
, sizeof(ServiceCurrent
));
93 RegCloseKey(hServiceCurrentKey
);
95 if (dwError
!= ERROR_SUCCESS
)
97 DPRINT1("RegSetValueExW() failed (Error %lu)\n", dwError
);
101 /* Create '\\.\pipe\net\NtControlPipeXXX' instance */
102 swprintf(szControlPipeName
, L
"\\\\.\\pipe\\net\\NtControlPipe%lu", ServiceCurrent
);
104 DPRINT("PipeName: %S\n", szControlPipeName
);
106 pServiceImage
->hControlPipe
= CreateNamedPipeW(szControlPipeName
,
107 #ifdef USE_ASYNCHRONOUS_IO
108 PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
112 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
118 DPRINT("CreateNamedPipeW(%S) done\n", szControlPipeName
);
119 if (pServiceImage
->hControlPipe
== INVALID_HANDLE_VALUE
)
121 DPRINT1("Failed to create control pipe!\n");
122 return GetLastError();
125 return ERROR_SUCCESS
;
129 static PSERVICE_IMAGE
130 ScmGetServiceImageByImagePath(LPWSTR lpImagePath
)
132 PLIST_ENTRY ImageEntry
;
133 PSERVICE_IMAGE CurrentImage
;
135 DPRINT("ScmGetServiceImageByImagePath(%S) called\n", lpImagePath
);
137 ImageEntry
= ImageListHead
.Flink
;
138 while (ImageEntry
!= &ImageListHead
)
140 CurrentImage
= CONTAINING_RECORD(ImageEntry
,
143 if (_wcsicmp(CurrentImage
->szImagePath
, lpImagePath
) == 0)
145 DPRINT("Found image: '%S'\n", CurrentImage
->szImagePath
);
149 ImageEntry
= ImageEntry
->Flink
;
152 DPRINT("Couldn't find a matching image\n");
160 ScmCreateOrReferenceServiceImage(PSERVICE pService
)
162 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
163 UNICODE_STRING ImagePath
;
164 PSERVICE_IMAGE pServiceImage
= NULL
;
166 DWORD dwError
= ERROR_SUCCESS
;
168 DPRINT("ScmCreateOrReferenceServiceImage(%p)\n", pService
);
170 RtlInitUnicodeString(&ImagePath
, NULL
);
172 /* Get service data */
173 RtlZeroMemory(&QueryTable
,
176 QueryTable
[0].Name
= L
"ImagePath";
177 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_QUERY_REGISTRY_REQUIRED
;
178 QueryTable
[0].EntryContext
= &ImagePath
;
180 Status
= RtlQueryRegistryValues(RTL_REGISTRY_SERVICES
,
181 pService
->lpServiceName
,
185 if (!NT_SUCCESS(Status
))
187 DPRINT1("RtlQueryRegistryValues() failed (Status %lx)\n", Status
);
188 return RtlNtStatusToDosError(Status
);
191 DPRINT("ImagePath: '%wZ'\n", &ImagePath
);
193 pServiceImage
= ScmGetServiceImageByImagePath(ImagePath
.Buffer
);
194 if (pServiceImage
== NULL
)
196 /* Create a new service image */
197 pServiceImage
= HeapAlloc(GetProcessHeap(),
199 FIELD_OFFSET(SERVICE_IMAGE
, szImagePath
[ImagePath
.Length
/ sizeof(WCHAR
) + 1]));
200 if (pServiceImage
== NULL
)
202 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
206 pServiceImage
->dwImageRunCount
= 1;
207 pServiceImage
->hControlPipe
= INVALID_HANDLE_VALUE
;
209 /* Set the image path */
210 wcscpy(pServiceImage
->szImagePath
,
213 RtlFreeUnicodeString(&ImagePath
);
215 /* Create the control pipe */
216 dwError
= ScmCreateNewControlPipe(pServiceImage
);
217 if (dwError
!= ERROR_SUCCESS
)
219 HeapFree(GetProcessHeap(), 0, pServiceImage
);
223 /* FIXME: Add more initialization code here */
226 /* Append service record */
227 InsertTailList(&ImageListHead
,
228 &pServiceImage
->ImageListEntry
);
232 /* Increment the run counter */
233 pServiceImage
->dwImageRunCount
++;
236 DPRINT("pServiceImage->dwImageRunCount: %lu\n", pServiceImage
->dwImageRunCount
);
238 /* Link the service image to the service */
239 pService
->lpImage
= pServiceImage
;
242 RtlFreeUnicodeString(&ImagePath
);
244 DPRINT("ScmCreateOrReferenceServiceImage() done (Error: %lu)\n", dwError
);
251 ScmDereferenceServiceImage(PSERVICE_IMAGE pServiceImage
)
253 DPRINT1("ScmDereferenceServiceImage() called\n");
255 pServiceImage
->dwImageRunCount
--;
257 if (pServiceImage
->dwImageRunCount
== 0)
259 DPRINT1("dwImageRunCount == 0\n");
261 /* FIXME: Terminate the process */
263 /* Remove the service image from the list */
264 RemoveEntryList(&pServiceImage
->ImageListEntry
);
266 /* Close the control pipe */
267 if (pServiceImage
->hControlPipe
!= INVALID_HANDLE_VALUE
)
268 CloseHandle(pServiceImage
->hControlPipe
);
270 /* Release the service image */
271 HeapFree(GetProcessHeap(), 0, pServiceImage
);
277 ScmGetServiceEntryByName(LPCWSTR lpServiceName
)
279 PLIST_ENTRY ServiceEntry
;
280 PSERVICE CurrentService
;
282 DPRINT("ScmGetServiceEntryByName() called\n");
284 ServiceEntry
= ServiceListHead
.Flink
;
285 while (ServiceEntry
!= &ServiceListHead
)
287 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
290 if (_wcsicmp(CurrentService
->lpServiceName
, lpServiceName
) == 0)
292 DPRINT("Found service: '%S'\n", CurrentService
->lpServiceName
);
293 return CurrentService
;
296 ServiceEntry
= ServiceEntry
->Flink
;
299 DPRINT("Couldn't find a matching service\n");
306 ScmGetServiceEntryByDisplayName(LPCWSTR lpDisplayName
)
308 PLIST_ENTRY ServiceEntry
;
309 PSERVICE CurrentService
;
311 DPRINT("ScmGetServiceEntryByDisplayName() called\n");
313 ServiceEntry
= ServiceListHead
.Flink
;
314 while (ServiceEntry
!= &ServiceListHead
)
316 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
319 if (_wcsicmp(CurrentService
->lpDisplayName
, lpDisplayName
) == 0)
321 DPRINT("Found service: '%S'\n", CurrentService
->lpDisplayName
);
322 return CurrentService
;
325 ServiceEntry
= ServiceEntry
->Flink
;
328 DPRINT("Couldn't find a matching service\n");
335 ScmGetServiceEntryByResumeCount(DWORD dwResumeCount
)
337 PLIST_ENTRY ServiceEntry
;
338 PSERVICE CurrentService
;
340 DPRINT("ScmGetServiceEntryByResumeCount() called\n");
342 ServiceEntry
= ServiceListHead
.Flink
;
343 while (ServiceEntry
!= &ServiceListHead
)
345 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
348 if (CurrentService
->dwResumeCount
> dwResumeCount
)
350 DPRINT("Found service: '%S'\n", CurrentService
->lpDisplayName
);
351 return CurrentService
;
354 ServiceEntry
= ServiceEntry
->Flink
;
357 DPRINT("Couldn't find a matching service\n");
364 ScmCreateNewServiceRecord(LPCWSTR lpServiceName
,
365 PSERVICE
* lpServiceRecord
)
367 PSERVICE lpService
= NULL
;
369 DPRINT("Service: '%S'\n", lpServiceName
);
371 /* Allocate service entry */
372 lpService
= HeapAlloc(GetProcessHeap(),
374 FIELD_OFFSET(SERVICE
, szServiceName
[wcslen(lpServiceName
) + 1]));
375 if (lpService
== NULL
)
376 return ERROR_NOT_ENOUGH_MEMORY
;
378 *lpServiceRecord
= lpService
;
380 /* Copy service name */
381 wcscpy(lpService
->szServiceName
, lpServiceName
);
382 lpService
->lpServiceName
= lpService
->szServiceName
;
383 lpService
->lpDisplayName
= lpService
->lpServiceName
;
385 /* Set the resume count */
386 lpService
->dwResumeCount
= ResumeCount
++;
388 /* Append service record */
389 InsertTailList(&ServiceListHead
,
390 &lpService
->ServiceListEntry
);
392 /* Initialize the service status */
393 lpService
->Status
.dwCurrentState
= SERVICE_STOPPED
;
394 lpService
->Status
.dwControlsAccepted
= 0;
395 lpService
->Status
.dwWin32ExitCode
= ERROR_SERVICE_NEVER_STARTED
;
396 lpService
->Status
.dwServiceSpecificExitCode
= 0;
397 lpService
->Status
.dwCheckPoint
= 0;
398 lpService
->Status
.dwWaitHint
= 2000; /* 2 seconds */
400 return ERROR_SUCCESS
;
405 ScmDeleteServiceRecord(PSERVICE lpService
)
407 DPRINT("Deleting Service %S\n", lpService
->lpServiceName
);
409 /* Delete the display name */
410 if (lpService
->lpDisplayName
!= NULL
&&
411 lpService
->lpDisplayName
!= lpService
->lpServiceName
)
412 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
414 /* Dereference the service image */
415 if (lpService
->lpImage
)
416 ScmDereferenceServiceImage(lpService
->lpImage
);
418 /* Decrement the group reference counter */
419 if (lpService
->lpGroup
)
420 lpService
->lpGroup
->dwRefCount
--;
422 /* FIXME: SecurityDescriptor */
425 /* Remove the Service from the List */
426 RemoveEntryList(&lpService
->ServiceListEntry
);
428 DPRINT("Deleted Service %S\n", lpService
->lpServiceName
);
430 /* Delete the service record */
431 HeapFree(GetProcessHeap(), 0, lpService
);
438 CreateServiceListEntry(LPCWSTR lpServiceName
,
441 PSERVICE lpService
= NULL
;
442 LPWSTR lpDisplayName
= NULL
;
443 LPWSTR lpGroup
= NULL
;
448 DWORD dwErrorControl
;
451 DPRINT("Service: '%S'\n", lpServiceName
);
452 if (*lpServiceName
== L
'{')
453 return ERROR_SUCCESS
;
455 dwSize
= sizeof(DWORD
);
456 dwError
= RegQueryValueExW(hServiceKey
,
460 (LPBYTE
)&dwServiceType
,
462 if (dwError
!= ERROR_SUCCESS
)
463 return ERROR_SUCCESS
;
465 if (((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_OWN_PROCESS
) &&
466 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_SHARE_PROCESS
) &&
467 (dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
468 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
))
469 return ERROR_SUCCESS
;
471 DPRINT("Service type: %lx\n", dwServiceType
);
473 dwSize
= sizeof(DWORD
);
474 dwError
= RegQueryValueExW(hServiceKey
,
478 (LPBYTE
)&dwStartType
,
480 if (dwError
!= ERROR_SUCCESS
)
481 return ERROR_SUCCESS
;
483 DPRINT("Start type: %lx\n", dwStartType
);
485 dwSize
= sizeof(DWORD
);
486 dwError
= RegQueryValueExW(hServiceKey
,
490 (LPBYTE
)&dwErrorControl
,
492 if (dwError
!= ERROR_SUCCESS
)
493 return ERROR_SUCCESS
;
495 DPRINT("Error control: %lx\n", dwErrorControl
);
497 dwError
= RegQueryValueExW(hServiceKey
,
503 if (dwError
!= ERROR_SUCCESS
)
506 DPRINT("Tag: %lx\n", dwTagId
);
508 dwError
= ScmReadString(hServiceKey
,
511 if (dwError
!= ERROR_SUCCESS
)
514 DPRINT("Group: %S\n", lpGroup
);
516 dwError
= ScmReadString(hServiceKey
,
519 if (dwError
!= ERROR_SUCCESS
)
520 lpDisplayName
= NULL
;
522 DPRINT("Display name: %S\n", lpDisplayName
);
524 dwError
= ScmCreateNewServiceRecord(lpServiceName
,
526 if (dwError
!= ERROR_SUCCESS
)
529 lpService
->Status
.dwServiceType
= dwServiceType
;
530 lpService
->dwStartType
= dwStartType
;
531 lpService
->dwErrorControl
= dwErrorControl
;
532 lpService
->dwTag
= dwTagId
;
536 dwError
= ScmSetServiceGroup(lpService
, lpGroup
);
537 if (dwError
!= ERROR_SUCCESS
)
541 if (lpDisplayName
!= NULL
)
543 lpService
->lpDisplayName
= lpDisplayName
;
544 lpDisplayName
= NULL
;
547 DPRINT("ServiceName: '%S'\n", lpService
->lpServiceName
);
548 if (lpService
->lpGroup
!= NULL
)
550 DPRINT("Group: '%S'\n", lpService
->lpGroup
->lpGroupName
);
552 DPRINT("Start %lx Type %lx Tag %lx ErrorControl %lx\n",
553 lpService
->dwStartType
,
554 lpService
->Status
.dwServiceType
,
556 lpService
->dwErrorControl
);
558 if (ScmIsDeleteFlagSet(hServiceKey
))
559 lpService
->bDeleted
= TRUE
;
563 HeapFree(GetProcessHeap(), 0, lpGroup
);
565 if (lpDisplayName
!= NULL
)
566 HeapFree(GetProcessHeap(), 0, lpDisplayName
);
568 if (lpService
!= NULL
)
570 if (lpService
->lpImage
!= NULL
)
571 ScmDereferenceServiceImage(lpService
->lpImage
);
579 ScmDeleteRegKey(HKEY hKey
, LPCWSTR lpszSubKey
)
581 DWORD dwRet
, dwMaxSubkeyLen
= 0, dwSize
;
582 WCHAR szNameBuf
[MAX_PATH
], *lpszName
= szNameBuf
;
585 dwRet
= RegOpenKeyExW(hKey
, lpszSubKey
, 0, KEY_READ
, &hSubKey
);
588 /* Find the maximum subkey length so that we can allocate a buffer */
589 dwRet
= RegQueryInfoKeyW(hSubKey
, NULL
, NULL
, NULL
, NULL
,
590 &dwMaxSubkeyLen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
594 if (dwMaxSubkeyLen
> sizeof(szNameBuf
) / sizeof(WCHAR
))
596 /* Name too big: alloc a buffer for it */
597 lpszName
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwMaxSubkeyLen
* sizeof(WCHAR
));
601 dwRet
= ERROR_NOT_ENOUGH_MEMORY
;
604 while (dwRet
== ERROR_SUCCESS
)
606 dwSize
= dwMaxSubkeyLen
;
607 dwRet
= RegEnumKeyExW(hSubKey
, 0, lpszName
, &dwSize
, NULL
, NULL
, NULL
, NULL
);
608 if (dwRet
== ERROR_SUCCESS
|| dwRet
== ERROR_MORE_DATA
)
609 dwRet
= ScmDeleteRegKey(hSubKey
, lpszName
);
611 if (dwRet
== ERROR_NO_MORE_ITEMS
)
612 dwRet
= ERROR_SUCCESS
;
614 if (lpszName
!= szNameBuf
)
615 HeapFree(GetProcessHeap(), 0, lpszName
); /* Free buffer if allocated */
619 RegCloseKey(hSubKey
);
621 dwRet
= RegDeleteKeyW(hKey
, lpszSubKey
);
628 ScmDeleteMarkedServices(VOID
)
630 PLIST_ENTRY ServiceEntry
;
631 PSERVICE CurrentService
;
635 ServiceEntry
= ServiceListHead
.Flink
;
636 while (ServiceEntry
!= &ServiceListHead
)
638 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
640 ServiceEntry
= ServiceEntry
->Flink
;
642 if (CurrentService
->bDeleted
== TRUE
)
644 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
645 L
"System\\CurrentControlSet\\Services",
649 if (dwError
== ERROR_SUCCESS
)
651 dwError
= ScmDeleteRegKey(hServicesKey
, CurrentService
->lpServiceName
);
652 RegCloseKey(hServicesKey
);
653 if (dwError
== ERROR_SUCCESS
)
655 RemoveEntryList(&CurrentService
->ServiceListEntry
);
656 HeapFree(GetProcessHeap(), 0, CurrentService
);
660 if (dwError
!= ERROR_SUCCESS
)
661 DPRINT1("Delete service failed: %S\n", CurrentService
->lpServiceName
);
668 ScmCreateServiceDatabase(VOID
)
670 WCHAR szSubKey
[MAX_PATH
];
674 DWORD dwSubKeyLength
;
675 FILETIME ftLastChanged
;
678 DPRINT("ScmCreateServiceDatabase() called\n");
680 dwError
= ScmCreateGroupList();
681 if (dwError
!= ERROR_SUCCESS
)
684 /* Initialize basic variables */
685 InitializeListHead(&ImageListHead
);
686 InitializeListHead(&ServiceListHead
);
688 /* Initialize the database lock */
689 RtlInitializeResource(&DatabaseLock
);
691 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
692 L
"System\\CurrentControlSet\\Services",
696 if (dwError
!= ERROR_SUCCESS
)
702 dwSubKeyLength
= MAX_PATH
;
703 dwError
= RegEnumKeyExW(hServicesKey
,
711 if (dwError
== ERROR_SUCCESS
&&
714 DPRINT("SubKeyName: '%S'\n", szSubKey
);
716 dwError
= RegOpenKeyExW(hServicesKey
,
721 if (dwError
== ERROR_SUCCESS
)
723 dwError
= CreateServiceListEntry(szSubKey
,
726 RegCloseKey(hServiceKey
);
730 if (dwError
!= ERROR_SUCCESS
)
736 RegCloseKey(hServicesKey
);
738 /* Wait for the LSA server */
741 /* Delete services that are marked for delete */
742 ScmDeleteMarkedServices();
744 DPRINT("ScmCreateServiceDatabase() done\n");
746 return ERROR_SUCCESS
;
751 ScmShutdownServiceDatabase(VOID
)
753 DPRINT("ScmShutdownServiceDatabase() called\n");
755 ScmDeleteMarkedServices();
756 RtlDeleteResource(&DatabaseLock
);
758 DPRINT("ScmShutdownServiceDatabase() done\n");
763 ScmCheckDriver(PSERVICE Service
)
765 OBJECT_ATTRIBUTES ObjectAttributes
;
766 UNICODE_STRING DirName
;
769 POBJECT_DIRECTORY_INFORMATION DirInfo
;
774 DPRINT("ScmCheckDriver(%S) called\n", Service
->lpServiceName
);
776 if (Service
->Status
.dwServiceType
== SERVICE_KERNEL_DRIVER
)
778 RtlInitUnicodeString(&DirName
, L
"\\Driver");
780 else // if (Service->Status.dwServiceType == SERVICE_FILE_SYSTEM_DRIVER)
782 ASSERT(Service
->Status
.dwServiceType
== SERVICE_FILE_SYSTEM_DRIVER
);
783 RtlInitUnicodeString(&DirName
, L
"\\FileSystem");
786 InitializeObjectAttributes(&ObjectAttributes
,
792 Status
= NtOpenDirectoryObject(&DirHandle
,
793 DIRECTORY_QUERY
| DIRECTORY_TRAVERSE
,
795 if (!NT_SUCCESS(Status
))
800 BufferLength
= sizeof(OBJECT_DIRECTORY_INFORMATION
) +
801 2 * MAX_PATH
* sizeof(WCHAR
);
802 DirInfo
= HeapAlloc(GetProcessHeap(),
809 Status
= NtQueryDirectoryObject(DirHandle
,
816 if (Status
== STATUS_NO_MORE_ENTRIES
)
818 /* FIXME: Add current service to 'failed service' list */
819 DPRINT("Service '%S' failed\n", Service
->lpServiceName
);
823 if (!NT_SUCCESS(Status
))
826 DPRINT("Comparing: '%S' '%wZ'\n", Service
->lpServiceName
, &DirInfo
->Name
);
828 if (_wcsicmp(Service
->lpServiceName
, DirInfo
->Name
.Buffer
) == 0)
830 DPRINT("Found: '%S' '%wZ'\n",
831 Service
->lpServiceName
, &DirInfo
->Name
);
833 /* Mark service as 'running' */
834 Service
->Status
.dwCurrentState
= SERVICE_RUNNING
;
836 /* Mark the service group as 'running' */
837 if (Service
->lpGroup
!= NULL
)
839 Service
->lpGroup
->ServicesRunning
= TRUE
;
846 HeapFree(GetProcessHeap(),
851 return STATUS_SUCCESS
;
856 ScmGetBootAndSystemDriverState(VOID
)
858 PLIST_ENTRY ServiceEntry
;
859 PSERVICE CurrentService
;
861 DPRINT("ScmGetBootAndSystemDriverState() called\n");
863 ServiceEntry
= ServiceListHead
.Flink
;
864 while (ServiceEntry
!= &ServiceListHead
)
866 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
868 if (CurrentService
->dwStartType
== SERVICE_BOOT_START
||
869 CurrentService
->dwStartType
== SERVICE_SYSTEM_START
)
872 DPRINT(" Checking service: %S\n", CurrentService
->lpServiceName
);
874 ScmCheckDriver(CurrentService
);
877 ServiceEntry
= ServiceEntry
->Flink
;
880 DPRINT("ScmGetBootAndSystemDriverState() done\n");
885 ScmControlService(PSERVICE Service
,
888 PSCM_CONTROL_PACKET ControlPacket
;
889 SCM_REPLY_PACKET ReplyPacket
;
891 DWORD dwWriteCount
= 0;
892 DWORD dwReadCount
= 0;
895 DWORD dwError
= ERROR_SUCCESS
;
897 #ifdef USE_ASYNCHRONOUS_IO
898 OVERLAPPED Overlapped
= {0};
901 DPRINT("ScmControlService() called\n");
903 /* Acquire the service control critical section, to synchronize requests */
904 EnterCriticalSection(&ControlServiceCriticalSection
);
906 /* Calculate the total length of the start command line */
907 PacketSize
= sizeof(SCM_CONTROL_PACKET
);
908 PacketSize
+= (DWORD
)((wcslen(Service
->lpServiceName
) + 1) * sizeof(WCHAR
));
910 ControlPacket
= HeapAlloc(GetProcessHeap(),
913 if (ControlPacket
== NULL
)
915 LeaveCriticalSection(&ControlServiceCriticalSection
);
916 return ERROR_NOT_ENOUGH_MEMORY
;
919 ControlPacket
->dwSize
= PacketSize
;
920 ControlPacket
->dwControl
= dwControl
;
921 ControlPacket
->hServiceStatus
= (SERVICE_STATUS_HANDLE
)Service
;
923 ControlPacket
->dwServiceNameOffset
= sizeof(SCM_CONTROL_PACKET
);
925 Ptr
= (PWSTR
)((PBYTE
)ControlPacket
+ ControlPacket
->dwServiceNameOffset
);
926 wcscpy(Ptr
, Service
->lpServiceName
);
928 ControlPacket
->dwArgumentsCount
= 0;
929 ControlPacket
->dwArgumentsOffset
= 0;
931 #ifdef USE_ASYNCHRONOUS_IO
932 bResult
= WriteFile(Service
->lpImage
->hControlPipe
,
937 if (bResult
== FALSE
)
939 DPRINT("WriteFile() returned FALSE\n");
941 dwError
= GetLastError();
942 if (dwError
== ERROR_IO_PENDING
)
944 DPRINT("dwError: ERROR_IO_PENDING\n");
946 dwError
= WaitForSingleObject(Service
->lpImage
->hControlPipe
,
948 DPRINT("WaitForSingleObject() returned %lu\n", dwError
);
950 if (dwError
== WAIT_TIMEOUT
)
952 bResult
= CancelIo(Service
->lpImage
->hControlPipe
);
953 if (bResult
== FALSE
)
955 DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
958 dwError
= ERROR_SERVICE_REQUEST_TIMEOUT
;
961 else if (dwError
== WAIT_OBJECT_0
)
963 bResult
= GetOverlappedResult(Service
->lpImage
->hControlPipe
,
967 if (bResult
== FALSE
)
969 dwError
= GetLastError();
970 DPRINT1("GetOverlappedResult() failed (Error %lu)\n", dwError
);
978 DPRINT1("WriteFile() failed (Error %lu)\n", dwError
);
984 Overlapped
.hEvent
= (HANDLE
) NULL
;
986 bResult
= ReadFile(Service
->lpImage
->hControlPipe
,
988 sizeof(SCM_REPLY_PACKET
),
991 if (bResult
== FALSE
)
993 DPRINT("ReadFile() returned FALSE\n");
995 dwError
= GetLastError();
996 if (dwError
== ERROR_IO_PENDING
)
998 DPRINT("dwError: ERROR_IO_PENDING\n");
1000 dwError
= WaitForSingleObject(Service
->lpImage
->hControlPipe
,
1002 DPRINT("WaitForSingleObject() returned %lu\n", dwError
);
1004 if (dwError
== WAIT_TIMEOUT
)
1006 bResult
= CancelIo(Service
->lpImage
->hControlPipe
);
1007 if (bResult
== FALSE
)
1009 DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
1012 dwError
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1015 else if (dwError
== WAIT_OBJECT_0
)
1017 bResult
= GetOverlappedResult(Service
->lpImage
->hControlPipe
,
1021 if (bResult
== FALSE
)
1023 dwError
= GetLastError();
1024 DPRINT1("GetOverlappedResult() failed (Error %lu)\n", dwError
);
1032 DPRINT1("ReadFile() failed (Error %lu)\n", dwError
);
1038 /* Send the control packet */
1039 bResult
= WriteFile(Service
->lpImage
->hControlPipe
,
1044 if (bResult
== FALSE
)
1046 dwError
= GetLastError();
1047 DPRINT("WriteFile() failed (Error %lu)\n", dwError
);
1049 if ((dwError
== ERROR_GEN_FAILURE
) &&
1050 (dwControl
== SERVICE_CONTROL_STOP
))
1052 /* Service is already terminated */
1053 Service
->Status
.dwCurrentState
= SERVICE_STOPPED
;
1054 Service
->Status
.dwControlsAccepted
= 0;
1055 Service
->Status
.dwWin32ExitCode
= ERROR_SERVICE_NOT_ACTIVE
;
1056 dwError
= ERROR_SUCCESS
;
1061 /* Read the reply */
1062 bResult
= ReadFile(Service
->lpImage
->hControlPipe
,
1064 sizeof(SCM_REPLY_PACKET
),
1067 if (bResult
== FALSE
)
1069 dwError
= GetLastError();
1070 DPRINT("ReadFile() failed (Error %lu)\n", dwError
);
1075 /* Release the contol packet */
1076 HeapFree(GetProcessHeap(),
1080 if (dwReadCount
== sizeof(SCM_REPLY_PACKET
))
1082 dwError
= ReplyPacket
.dwError
;
1085 if (dwError
== ERROR_SUCCESS
&&
1086 dwControl
== SERVICE_CONTROL_STOP
)
1088 ScmDereferenceServiceImage(Service
->lpImage
);
1091 LeaveCriticalSection(&ControlServiceCriticalSection
);
1093 DPRINT("ScmControlService() done\n");
1100 ScmSendStartCommand(PSERVICE Service
,
1104 PSCM_CONTROL_PACKET ControlPacket
;
1105 SCM_REPLY_PACKET ReplyPacket
;
1108 DWORD dwWriteCount
= 0;
1109 DWORD dwReadCount
= 0;
1110 DWORD dwError
= ERROR_SUCCESS
;
1115 #ifdef USE_ASYNCHRONOUS_IO
1116 OVERLAPPED Overlapped
= {0};
1119 DPRINT("ScmSendStartCommand() called\n");
1121 /* Calculate the total length of the start command line */
1122 PacketSize
= sizeof(SCM_CONTROL_PACKET
) +
1123 (DWORD
)((wcslen(Service
->lpServiceName
) + 1) * sizeof(WCHAR
));
1125 /* Calculate the required packet size for the start arguments */
1126 if (argc
> 0 && argv
!= NULL
)
1128 PacketSize
= ALIGN_UP(PacketSize
, LPWSTR
);
1130 DPRINT("Argc: %lu\n", argc
);
1131 for (i
= 0; i
< argc
; i
++)
1133 DPRINT("Argv[%lu]: %S\n", i
, argv
[i
]);
1134 PacketSize
+= (DWORD
)((wcslen(argv
[i
]) + 1) * sizeof(WCHAR
) + sizeof(PWSTR
));
1138 /* Allocate a control packet */
1139 ControlPacket
= HeapAlloc(GetProcessHeap(),
1142 if (ControlPacket
== NULL
)
1143 return ERROR_NOT_ENOUGH_MEMORY
;
1145 ControlPacket
->dwSize
= PacketSize
;
1146 ControlPacket
->dwControl
= (Service
->Status
.dwServiceType
& SERVICE_WIN32_OWN_PROCESS
)
1147 ? SERVICE_CONTROL_START_OWN
1148 : SERVICE_CONTROL_START_SHARE
;
1149 ControlPacket
->hServiceStatus
= (SERVICE_STATUS_HANDLE
)Service
;
1150 ControlPacket
->dwServiceNameOffset
= sizeof(SCM_CONTROL_PACKET
);
1152 Ptr
= (PWSTR
)((PBYTE
)ControlPacket
+ ControlPacket
->dwServiceNameOffset
);
1153 wcscpy(Ptr
, Service
->lpServiceName
);
1155 ControlPacket
->dwArgumentsCount
= 0;
1156 ControlPacket
->dwArgumentsOffset
= 0;
1158 /* Copy argument list */
1159 if (argc
> 0 && argv
!= NULL
)
1161 Ptr
+= wcslen(Service
->lpServiceName
) + 1;
1162 pOffPtr
= (PWSTR
*)ALIGN_UP_POINTER(Ptr
, PWSTR
);
1163 pArgPtr
= (PWSTR
)((ULONG_PTR
)pOffPtr
+ argc
* sizeof(PWSTR
));
1165 ControlPacket
->dwArgumentsCount
= argc
;
1166 ControlPacket
->dwArgumentsOffset
= (DWORD
)((ULONG_PTR
)pOffPtr
- (ULONG_PTR
)ControlPacket
);
1168 DPRINT("dwArgumentsCount: %lu\n", ControlPacket
->dwArgumentsCount
);
1169 DPRINT("dwArgumentsOffset: %lu\n", ControlPacket
->dwArgumentsOffset
);
1171 for (i
= 0; i
< argc
; i
++)
1173 wcscpy(pArgPtr
, argv
[i
]);
1174 *pOffPtr
= (PWSTR
)((ULONG_PTR
)pArgPtr
- (ULONG_PTR
)pOffPtr
);
1175 DPRINT("offset: %p\n", *pOffPtr
);
1177 pArgPtr
+= wcslen(argv
[i
]) + 1;
1182 #ifdef USE_ASYNCHRONOUS_IO
1183 bResult
= WriteFile(Service
->lpImage
->hControlPipe
,
1188 if (bResult
== FALSE
)
1190 DPRINT("WriteFile() returned FALSE\n");
1192 dwError
= GetLastError();
1193 if (dwError
== ERROR_IO_PENDING
)
1195 DPRINT("dwError: ERROR_IO_PENDING\n");
1197 dwError
= WaitForSingleObject(Service
->lpImage
->hControlPipe
,
1199 DPRINT("WaitForSingleObject() returned %lu\n", dwError
);
1201 if (dwError
== WAIT_TIMEOUT
)
1203 bResult
= CancelIo(Service
->lpImage
->hControlPipe
);
1204 if (bResult
== FALSE
)
1206 DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
1209 dwError
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1212 else if (dwError
== WAIT_OBJECT_0
)
1214 bResult
= GetOverlappedResult(Service
->lpImage
->hControlPipe
,
1218 if (bResult
== FALSE
)
1220 dwError
= GetLastError();
1221 DPRINT1("GetOverlappedResult() failed (Error %lu)\n", dwError
);
1229 DPRINT1("WriteFile() failed (Error %lu)\n", dwError
);
1234 /* Read the reply */
1235 Overlapped
.hEvent
= (HANDLE
) NULL
;
1237 bResult
= ReadFile(Service
->lpImage
->hControlPipe
,
1239 sizeof(SCM_REPLY_PACKET
),
1242 if (bResult
== FALSE
)
1244 DPRINT("ReadFile() returned FALSE\n");
1246 dwError
= GetLastError();
1247 if (dwError
== ERROR_IO_PENDING
)
1249 DPRINT("dwError: ERROR_IO_PENDING\n");
1251 dwError
= WaitForSingleObject(Service
->lpImage
->hControlPipe
,
1253 DPRINT("WaitForSingleObject() returned %lu\n", dwError
);
1255 if (dwError
== WAIT_TIMEOUT
)
1257 bResult
= CancelIo(Service
->lpImage
->hControlPipe
);
1258 if (bResult
== FALSE
)
1260 DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
1263 dwError
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1266 else if (dwError
== WAIT_OBJECT_0
)
1268 bResult
= GetOverlappedResult(Service
->lpImage
->hControlPipe
,
1272 if (bResult
== FALSE
)
1274 dwError
= GetLastError();
1275 DPRINT1("GetOverlappedResult() failed (Error %lu)\n", dwError
);
1283 DPRINT1("ReadFile() failed (Error %lu)\n", dwError
);
1289 /* Send the start command */
1290 bResult
= WriteFile(Service
->lpImage
->hControlPipe
,
1295 if (bResult
== FALSE
)
1297 dwError
= GetLastError();
1298 DPRINT("WriteFile() failed (Error %lu)\n", dwError
);
1302 /* Read the reply */
1303 bResult
= ReadFile(Service
->lpImage
->hControlPipe
,
1305 sizeof(SCM_REPLY_PACKET
),
1308 if (bResult
== FALSE
)
1310 dwError
= GetLastError();
1311 DPRINT("ReadFile() failed (Error %lu)\n", dwError
);
1316 /* Release the contol packet */
1317 HeapFree(GetProcessHeap(),
1321 if (dwReadCount
== sizeof(SCM_REPLY_PACKET
))
1323 dwError
= ReplyPacket
.dwError
;
1326 DPRINT("ScmSendStartCommand() done\n");
1333 ScmWaitForServiceConnect(PSERVICE Service
)
1336 DWORD dwProcessId
= 0;
1337 DWORD dwError
= ERROR_SUCCESS
;
1339 #ifdef USE_ASYNCHRONOUS_IO
1340 OVERLAPPED Overlapped
= {0};
1343 LPCWSTR lpErrorStrings
[3];
1344 WCHAR szBuffer1
[20];
1345 WCHAR szBuffer2
[20];
1348 DPRINT("ScmWaitForServiceConnect()\n");
1350 #ifdef USE_ASYNCHRONOUS_IO
1351 Overlapped
.hEvent
= (HANDLE
)NULL
;
1353 bResult
= ConnectNamedPipe(Service
->lpImage
->hControlPipe
,
1355 if (bResult
== FALSE
)
1357 DPRINT("ConnectNamedPipe() returned FALSE\n");
1359 dwError
= GetLastError();
1360 if (dwError
== ERROR_IO_PENDING
)
1362 DPRINT("dwError: ERROR_IO_PENDING\n");
1364 dwError
= WaitForSingleObject(Service
->lpImage
->hControlPipe
,
1366 DPRINT("WaitForSingleObject() returned %lu\n", dwError
);
1368 if (dwError
== WAIT_TIMEOUT
)
1370 DPRINT("WaitForSingleObject() returned WAIT_TIMEOUT\n");
1372 bResult
= CancelIo(Service
->lpImage
->hControlPipe
);
1373 if (bResult
== FALSE
)
1375 DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
1379 _ultow(PipeTimeout
, szBuffer1
, 10);
1380 lpErrorStrings
[0] = Service
->lpDisplayName
;
1381 lpErrorStrings
[1] = szBuffer1
;
1383 ScmLogEvent(EVENT_CONNECTION_TIMEOUT
,
1384 EVENTLOG_ERROR_TYPE
,
1388 DPRINT1("Log EVENT_CONNECTION_TIMEOUT by %S\n", Service
->lpDisplayName
);
1390 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1392 else if (dwError
== WAIT_OBJECT_0
)
1394 bResult
= GetOverlappedResult(Service
->lpImage
->hControlPipe
,
1398 if (bResult
== FALSE
)
1400 dwError
= GetLastError();
1401 DPRINT1("GetOverlappedResult failed (Error %lu)\n", dwError
);
1407 else if (dwError
!= ERROR_PIPE_CONNECTED
)
1409 DPRINT1("ConnectNamedPipe failed (Error %lu)\n", dwError
);
1414 DPRINT("Control pipe connected!\n");
1416 Overlapped
.hEvent
= (HANDLE
) NULL
;
1418 /* Read the process id from pipe */
1419 bResult
= ReadFile(Service
->lpImage
->hControlPipe
,
1420 (LPVOID
)&dwProcessId
,
1424 if (bResult
== FALSE
)
1426 DPRINT("ReadFile() returned FALSE\n");
1428 dwError
= GetLastError();
1429 if (dwError
== ERROR_IO_PENDING
)
1431 DPRINT("dwError: ERROR_IO_PENDING\n");
1433 dwError
= WaitForSingleObject(Service
->lpImage
->hControlPipe
,
1435 if (dwError
== WAIT_TIMEOUT
)
1437 DPRINT("WaitForSingleObject() returned WAIT_TIMEOUT\n");
1439 bResult
= CancelIo(Service
->lpImage
->hControlPipe
);
1440 if (bResult
== FALSE
)
1442 DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
1446 _ultow(PipeTimeout
, szBuffer1
, 10);
1447 lpErrorStrings
[0] = szBuffer1
;
1449 ScmLogEvent(EVENT_READFILE_TIMEOUT
,
1450 EVENTLOG_ERROR_TYPE
,
1454 DPRINT1("Log EVENT_READFILE_TIMEOUT by %S\n", Service
->lpDisplayName
);
1456 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1458 else if (dwError
== WAIT_OBJECT_0
)
1460 DPRINT("WaitForSingleObject() returned WAIT_OBJECT_0\n");
1462 DPRINT("Process Id: %lu\n", dwProcessId
);
1464 bResult
= GetOverlappedResult(Service
->lpImage
->hControlPipe
,
1468 if (bResult
== FALSE
)
1470 dwError
= GetLastError();
1471 DPRINT1("GetOverlappedResult() failed (Error %lu)\n", dwError
);
1478 DPRINT1("WaitForSingleObject() returned %lu\n", dwError
);
1483 DPRINT1("ReadFile() failed (Error %lu)\n", dwError
);
1488 if (dwProcessId
!= Service
->lpImage
->dwProcessId
)
1491 _ultow(Service
->lpImage
->dwProcessId
, szBuffer1
, 10);
1492 _ultow(dwProcessId
, szBuffer2
, 10);
1494 lpErrorStrings
[0] = Service
->lpDisplayName
;
1495 lpErrorStrings
[1] = szBuffer1
;
1496 lpErrorStrings
[2] = szBuffer2
;
1498 ScmLogEvent(EVENT_SERVICE_DIFFERENT_PID_CONNECTED
,
1499 EVENTLOG_WARNING_TYPE
,
1504 DPRINT1("Log EVENT_SERVICE_DIFFERENT_PID_CONNECTED by %S\n", Service
->lpDisplayName
);
1507 DPRINT("ScmWaitForServiceConnect() done\n");
1509 return ERROR_SUCCESS
;
1512 /* Connect control pipe */
1513 if (ConnectNamedPipe(Service
->lpImage
->hControlPipe
, NULL
) ?
1514 TRUE
: (dwError
= GetLastError()) == ERROR_PIPE_CONNECTED
)
1516 DPRINT("Control pipe connected!\n");
1518 /* Read SERVICE_STATUS_HANDLE from pipe */
1519 bResult
= ReadFile(Service
->lpImage
->hControlPipe
,
1520 (LPVOID
)&dwProcessId
,
1524 if (bResult
== FALSE
)
1526 dwError
= GetLastError();
1527 DPRINT1("Reading the service control pipe failed (Error %lu)\n",
1532 dwError
= ERROR_SUCCESS
;
1533 DPRINT("Read control pipe successfully\n");
1538 DPRINT1("Connecting control pipe failed! (Error %lu)\n", dwError
);
1547 ScmStartUserModeService(PSERVICE Service
,
1551 PROCESS_INFORMATION ProcessInformation
;
1552 STARTUPINFOW StartupInfo
;
1554 DWORD dwError
= ERROR_SUCCESS
;
1556 DPRINT("ScmStartUserModeService(%p)\n", Service
);
1558 /* If the image is already running ... */
1559 if (Service
->lpImage
->dwImageRunCount
> 1)
1561 /* ... just send a start command */
1562 return ScmSendStartCommand(Service
, argc
, argv
);
1565 /* Otherwise start its process */
1566 ZeroMemory(&StartupInfo
, sizeof(StartupInfo
));
1567 StartupInfo
.cb
= sizeof(StartupInfo
);
1568 ZeroMemory(&ProcessInformation
, sizeof(ProcessInformation
));
1570 Result
= CreateProcessW(NULL
,
1571 Service
->lpImage
->szImagePath
,
1575 DETACHED_PROCESS
| CREATE_SUSPENDED
,
1579 &ProcessInformation
);
1582 dwError
= GetLastError();
1583 DPRINT1("Starting '%S' failed!\n", Service
->lpServiceName
);
1587 DPRINT("Process Id: %lu Handle %p\n",
1588 ProcessInformation
.dwProcessId
,
1589 ProcessInformation
.hProcess
);
1590 DPRINT("Thread Id: %lu Handle %p\n",
1591 ProcessInformation
.dwThreadId
,
1592 ProcessInformation
.hThread
);
1594 /* Get process handle and id */
1595 Service
->lpImage
->dwProcessId
= ProcessInformation
.dwProcessId
;
1598 ResumeThread(ProcessInformation
.hThread
);
1600 /* Connect control pipe */
1601 dwError
= ScmWaitForServiceConnect(Service
);
1602 if (dwError
== ERROR_SUCCESS
)
1604 /* Send start command */
1605 dwError
= ScmSendStartCommand(Service
, argc
, argv
);
1609 DPRINT1("Connecting control pipe failed! (Error %lu)\n", dwError
);
1610 Service
->lpImage
->dwProcessId
= 0;
1613 /* Close thread and process handle */
1614 CloseHandle(ProcessInformation
.hThread
);
1615 CloseHandle(ProcessInformation
.hProcess
);
1622 ScmLoadService(PSERVICE Service
,
1626 PSERVICE_GROUP Group
= Service
->lpGroup
;
1627 DWORD dwError
= ERROR_SUCCESS
;
1628 LPCWSTR lpErrorStrings
[2];
1629 WCHAR szErrorBuffer
[32];
1631 DPRINT("ScmLoadService() called\n");
1632 DPRINT("Start Service %p (%S)\n", Service
, Service
->lpServiceName
);
1634 if (Service
->Status
.dwCurrentState
!= SERVICE_STOPPED
)
1636 DPRINT("Service %S is already running!\n", Service
->lpServiceName
);
1637 return ERROR_SERVICE_ALREADY_RUNNING
;
1640 DPRINT("Service->Type: %lu\n", Service
->Status
.dwServiceType
);
1642 if (Service
->Status
.dwServiceType
& SERVICE_DRIVER
)
1645 dwError
= ScmLoadDriver(Service
);
1646 if (dwError
== ERROR_SUCCESS
)
1648 Service
->Status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
;
1649 Service
->Status
.dwCurrentState
= SERVICE_RUNNING
;
1652 else // if (Service->Status.dwServiceType & (SERVICE_WIN32 | SERVICE_INTERACTIVE_PROCESS))
1654 /* Start user-mode service */
1655 dwError
= ScmCreateOrReferenceServiceImage(Service
);
1656 if (dwError
== ERROR_SUCCESS
)
1658 dwError
= ScmStartUserModeService(Service
, argc
, argv
);
1659 if (dwError
== ERROR_SUCCESS
)
1661 #ifdef USE_SERVICE_START_PENDING
1662 Service
->Status
.dwCurrentState
= SERVICE_START_PENDING
;
1664 Service
->Status
.dwCurrentState
= SERVICE_RUNNING
;
1669 ScmDereferenceServiceImage(Service
->lpImage
);
1670 Service
->lpImage
= NULL
;
1675 DPRINT("ScmLoadService() done (Error %lu)\n", dwError
);
1677 if (dwError
== ERROR_SUCCESS
)
1681 Group
->ServicesRunning
= TRUE
;
1684 /* Log a successful service start */
1685 lpErrorStrings
[0] = Service
->lpDisplayName
;
1686 lpErrorStrings
[1] = L
"start";
1687 ScmLogEvent(EVENT_SERVICE_CONTROL_SUCCESS
,
1688 EVENTLOG_INFORMATION_TYPE
,
1694 if (Service
->dwErrorControl
!= SERVICE_ERROR_IGNORE
)
1696 /* Log a failed service start */
1697 swprintf(szErrorBuffer
, L
"%lu", dwError
);
1698 lpErrorStrings
[0] = Service
->lpServiceName
;
1699 lpErrorStrings
[1] = szErrorBuffer
;
1700 ScmLogEvent(EVENT_SERVICE_START_FAILED
,
1701 EVENTLOG_ERROR_TYPE
,
1707 switch (Service
->dwErrorControl
)
1709 case SERVICE_ERROR_SEVERE
:
1710 if (IsLastKnownGood
== FALSE
)
1712 /* FIXME: Boot last known good configuration */
1716 case SERVICE_ERROR_CRITICAL
:
1717 if (IsLastKnownGood
== FALSE
)
1719 /* FIXME: Boot last known good configuration */
1735 ScmStartService(PSERVICE Service
,
1739 DWORD dwError
= ERROR_SUCCESS
;
1740 SC_RPC_LOCK Lock
= NULL
;
1742 DPRINT("ScmStartService() called\n");
1743 DPRINT("Start Service %p (%S)\n", Service
, Service
->lpServiceName
);
1745 /* Acquire the service control critical section, to synchronize starts */
1746 EnterCriticalSection(&ControlServiceCriticalSection
);
1749 * Acquire the user service start lock while the service is starting, if
1750 * needed (i.e. if we are not starting it during the initialization phase).
1751 * If we don't success, bail out.
1755 dwError
= ScmAcquireServiceStartLock(TRUE
, &Lock
);
1756 if (dwError
!= ERROR_SUCCESS
) goto done
;
1759 /* Really start the service */
1760 dwError
= ScmLoadService(Service
, argc
, argv
);
1762 /* Release the service start lock, if needed, and the critical section */
1763 if (Lock
) ScmReleaseServiceStartLock(&Lock
);
1766 LeaveCriticalSection(&ControlServiceCriticalSection
);
1768 DPRINT("ScmStartService() done (Error %lu)\n", dwError
);
1775 ScmAutoStartServices(VOID
)
1777 DWORD dwError
= ERROR_SUCCESS
;
1778 PLIST_ENTRY GroupEntry
;
1779 PLIST_ENTRY ServiceEntry
;
1780 PSERVICE_GROUP CurrentGroup
;
1781 PSERVICE CurrentService
;
1782 WCHAR szSafeBootServicePath
[MAX_PATH
];
1787 * This function MUST be called ONLY at initialization time.
1788 * Therefore, no need to acquire the user service start lock.
1790 ASSERT(ScmInitialize
);
1792 /* Acquire the service control critical section, to synchronize starts */
1793 EnterCriticalSection(&ControlServiceCriticalSection
);
1795 /* Clear 'ServiceVisited' flag (or set if not to start in Safe Mode) */
1796 ServiceEntry
= ServiceListHead
.Flink
;
1797 while (ServiceEntry
!= &ServiceListHead
)
1799 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1801 /* Build the safe boot path */
1802 wcscpy(szSafeBootServicePath
,
1803 L
"SYSTEM\\CurrentControlSet\\Control\\SafeBoot");
1805 switch (GetSystemMetrics(SM_CLEANBOOT
))
1807 /* NOTE: Assumes MINIMAL (1) and DSREPAIR (3) load same items */
1810 wcscat(szSafeBootServicePath
, L
"\\Minimal\\");
1814 wcscat(szSafeBootServicePath
, L
"\\Network\\");
1818 if (GetSystemMetrics(SM_CLEANBOOT
))
1820 /* If key does not exist then do not assume safe mode */
1821 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
1822 szSafeBootServicePath
,
1826 if (dwError
== ERROR_SUCCESS
)
1830 /* Finish Safe Boot path off */
1831 wcsncat(szSafeBootServicePath
,
1832 CurrentService
->lpServiceName
,
1833 MAX_PATH
- wcslen(szSafeBootServicePath
));
1835 /* Check that the key is in the Safe Boot path */
1836 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
1837 szSafeBootServicePath
,
1841 if (dwError
!= ERROR_SUCCESS
)
1843 /* Mark service as visited so it is not auto-started */
1844 CurrentService
->ServiceVisited
= TRUE
;
1848 /* Must be auto-started in safe mode - mark as unvisited */
1850 CurrentService
->ServiceVisited
= FALSE
;
1855 DPRINT1("WARNING: Could not open the associated Safe Boot key!");
1856 CurrentService
->ServiceVisited
= FALSE
;
1860 ServiceEntry
= ServiceEntry
->Flink
;
1863 /* Start all services which are members of an existing group */
1864 GroupEntry
= GroupListHead
.Flink
;
1865 while (GroupEntry
!= &GroupListHead
)
1867 CurrentGroup
= CONTAINING_RECORD(GroupEntry
, SERVICE_GROUP
, GroupListEntry
);
1869 DPRINT("Group '%S'\n", CurrentGroup
->lpGroupName
);
1871 /* Start all services witch have a valid tag */
1872 for (i
= 0; i
< CurrentGroup
->TagCount
; i
++)
1874 ServiceEntry
= ServiceListHead
.Flink
;
1875 while (ServiceEntry
!= &ServiceListHead
)
1877 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1879 if ((CurrentService
->lpGroup
== CurrentGroup
) &&
1880 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1881 (CurrentService
->ServiceVisited
== FALSE
) &&
1882 (CurrentService
->dwTag
== CurrentGroup
->TagArray
[i
]))
1884 CurrentService
->ServiceVisited
= TRUE
;
1885 ScmLoadService(CurrentService
, 0, NULL
);
1888 ServiceEntry
= ServiceEntry
->Flink
;
1892 /* Start all services which have an invalid tag or which do not have a tag */
1893 ServiceEntry
= ServiceListHead
.Flink
;
1894 while (ServiceEntry
!= &ServiceListHead
)
1896 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1898 if ((CurrentService
->lpGroup
== CurrentGroup
) &&
1899 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1900 (CurrentService
->ServiceVisited
== FALSE
))
1902 CurrentService
->ServiceVisited
= TRUE
;
1903 ScmLoadService(CurrentService
, 0, NULL
);
1906 ServiceEntry
= ServiceEntry
->Flink
;
1909 GroupEntry
= GroupEntry
->Flink
;
1912 /* Start all services which are members of any non-existing group */
1913 ServiceEntry
= ServiceListHead
.Flink
;
1914 while (ServiceEntry
!= &ServiceListHead
)
1916 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1918 if ((CurrentService
->lpGroup
!= NULL
) &&
1919 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1920 (CurrentService
->ServiceVisited
== FALSE
))
1922 CurrentService
->ServiceVisited
= TRUE
;
1923 ScmLoadService(CurrentService
, 0, NULL
);
1926 ServiceEntry
= ServiceEntry
->Flink
;
1929 /* Start all services which are not a member of any group */
1930 ServiceEntry
= ServiceListHead
.Flink
;
1931 while (ServiceEntry
!= &ServiceListHead
)
1933 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1935 if ((CurrentService
->lpGroup
== NULL
) &&
1936 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1937 (CurrentService
->ServiceVisited
== FALSE
))
1939 CurrentService
->ServiceVisited
= TRUE
;
1940 ScmLoadService(CurrentService
, 0, NULL
);
1943 ServiceEntry
= ServiceEntry
->Flink
;
1946 /* Clear 'ServiceVisited' flag again */
1947 ServiceEntry
= ServiceListHead
.Flink
;
1948 while (ServiceEntry
!= &ServiceListHead
)
1950 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1951 CurrentService
->ServiceVisited
= FALSE
;
1952 ServiceEntry
= ServiceEntry
->Flink
;
1955 /* Release the critical section */
1956 LeaveCriticalSection(&ControlServiceCriticalSection
);
1961 ScmAutoShutdownServices(VOID
)
1963 PLIST_ENTRY ServiceEntry
;
1964 PSERVICE CurrentService
;
1966 DPRINT("ScmAutoShutdownServices() called\n");
1968 /* Lock the service database exclusively */
1969 ScmLockDatabaseExclusive();
1971 ServiceEntry
= ServiceListHead
.Flink
;
1972 while (ServiceEntry
!= &ServiceListHead
)
1974 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1976 if (CurrentService
->Status
.dwCurrentState
== SERVICE_RUNNING
||
1977 CurrentService
->Status
.dwCurrentState
== SERVICE_START_PENDING
)
1979 /* shutdown service */
1980 DPRINT("Shutdown service: %S\n", CurrentService
->szServiceName
);
1981 ScmControlService(CurrentService
, SERVICE_CONTROL_SHUTDOWN
);
1984 ServiceEntry
= ServiceEntry
->Flink
;
1987 /* Unlock the service database */
1988 ScmUnlockDatabase();
1990 DPRINT("ScmAutoShutdownServices() done\n");
1995 ScmLockDatabaseExclusive(VOID
)
1997 return RtlAcquireResourceExclusive(&DatabaseLock
, TRUE
);
2002 ScmLockDatabaseShared(VOID
)
2004 return RtlAcquireResourceShared(&DatabaseLock
, TRUE
);
2009 ScmUnlockDatabase(VOID
)
2011 RtlReleaseResource(&DatabaseLock
);
2016 ScmInitNamedPipeCriticalSection(VOID
)
2022 InitializeCriticalSection(&ControlServiceCriticalSection
);
2024 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
2025 L
"SYSTEM\\CurrentControlSet\\Control",
2029 if (dwError
== ERROR_SUCCESS
)
2031 dwKeySize
= sizeof(DWORD
);
2032 RegQueryValueExW(hKey
,
2033 L
"ServicesPipeTimeout",
2036 (LPBYTE
)&PipeTimeout
,
2045 ScmDeleteNamedPipeCriticalSection(VOID
)
2047 DeleteCriticalSection(&ControlServiceCriticalSection
);