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 *****************************************************************/
21 * Uncomment the line below to start services
22 * using the SERVICE_START_PENDING state.
24 #define USE_SERVICE_START_PENDING
27 * Uncomment the line below to use asynchronous IO operations
28 * on the service control pipes.
30 // #define USE_ASYNCHRONOUS_IO
33 /* GLOBALS *******************************************************************/
35 LIST_ENTRY ImageListHead
;
36 LIST_ENTRY ServiceListHead
;
38 static RTL_RESOURCE DatabaseLock
;
39 static DWORD ResumeCount
= 1;
41 /* The critical section synchronizes service control requests */
42 static CRITICAL_SECTION ControlServiceCriticalSection
;
43 static DWORD PipeTimeout
= 30000; /* 30 Seconds */
46 /* FUNCTIONS *****************************************************************/
49 ScmCreateNewControlPipe(PSERVICE_IMAGE pServiceImage
)
51 WCHAR szControlPipeName
[MAX_PATH
+ 1];
52 HKEY hServiceCurrentKey
= INVALID_HANDLE_VALUE
;
53 DWORD ServiceCurrent
= 0;
58 /* Get the service number */
59 /* TODO: Create registry entry with correct write access */
60 dwError
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
61 L
"SYSTEM\\CurrentControlSet\\Control\\ServiceCurrent", 0, NULL
,
67 if (dwError
!= ERROR_SUCCESS
)
69 DPRINT1("RegCreateKeyEx() failed with error %lu\n", dwError
);
73 if (KeyDisposition
== REG_OPENED_EXISTING_KEY
)
75 dwKeySize
= sizeof(DWORD
);
76 dwError
= RegQueryValueExW(hServiceCurrentKey
,
77 L
"", 0, NULL
, (BYTE
*)&ServiceCurrent
, &dwKeySize
);
79 if (dwError
!= ERROR_SUCCESS
)
81 RegCloseKey(hServiceCurrentKey
);
82 DPRINT1("RegQueryValueEx() failed with error %lu\n", dwError
);
89 dwError
= RegSetValueExW(hServiceCurrentKey
, L
"", 0, REG_DWORD
, (BYTE
*)&ServiceCurrent
, sizeof(ServiceCurrent
));
91 RegCloseKey(hServiceCurrentKey
);
93 if (dwError
!= ERROR_SUCCESS
)
95 DPRINT1("RegSetValueExW() failed (Error %lu)\n", dwError
);
99 /* Create '\\.\pipe\net\NtControlPipeXXX' instance */
100 swprintf(szControlPipeName
, L
"\\\\.\\pipe\\net\\NtControlPipe%u", ServiceCurrent
);
102 DPRINT("PipeName: %S\n", szControlPipeName
);
104 pServiceImage
->hControlPipe
= CreateNamedPipeW(szControlPipeName
,
105 #ifdef USE_ASYNCHRONOUS_IO
106 PIPE_ACCESS_DUPLEX
| FILE_FLAG_OVERLAPPED
,
110 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
116 DPRINT("CreateNamedPipeW(%S) done\n", szControlPipeName
);
117 if (pServiceImage
->hControlPipe
== INVALID_HANDLE_VALUE
)
119 DPRINT1("Failed to create control pipe!\n");
120 return GetLastError();
123 return ERROR_SUCCESS
;
127 static PSERVICE_IMAGE
128 ScmGetServiceImageByImagePath(LPWSTR lpImagePath
)
130 PLIST_ENTRY ImageEntry
;
131 PSERVICE_IMAGE CurrentImage
;
133 DPRINT("ScmGetServiceImageByImagePath(%S) called\n", lpImagePath
);
135 ImageEntry
= ImageListHead
.Flink
;
136 while (ImageEntry
!= &ImageListHead
)
138 CurrentImage
= CONTAINING_RECORD(ImageEntry
,
141 if (_wcsicmp(CurrentImage
->szImagePath
, lpImagePath
) == 0)
143 DPRINT("Found image: '%S'\n", CurrentImage
->szImagePath
);
147 ImageEntry
= ImageEntry
->Flink
;
150 DPRINT("Couldn't find a matching image\n");
158 ScmCreateOrReferenceServiceImage(PSERVICE pService
)
160 RTL_QUERY_REGISTRY_TABLE QueryTable
[2];
161 UNICODE_STRING ImagePath
;
162 PSERVICE_IMAGE pServiceImage
= NULL
;
164 DWORD dwError
= ERROR_SUCCESS
;
166 DPRINT("ScmCreateOrReferenceServiceImage(%p)\n", pService
);
168 RtlInitUnicodeString(&ImagePath
, NULL
);
170 /* Get service data */
171 RtlZeroMemory(&QueryTable
,
174 QueryTable
[0].Name
= L
"ImagePath";
175 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_QUERY_REGISTRY_REQUIRED
;
176 QueryTable
[0].EntryContext
= &ImagePath
;
178 Status
= RtlQueryRegistryValues(RTL_REGISTRY_SERVICES
,
179 pService
->lpServiceName
,
183 if (!NT_SUCCESS(Status
))
185 DPRINT1("RtlQueryRegistryValues() failed (Status %lx)\n", Status
);
186 return RtlNtStatusToDosError(Status
);
189 DPRINT("ImagePath: '%wZ'\n", &ImagePath
);
191 pServiceImage
= ScmGetServiceImageByImagePath(ImagePath
.Buffer
);
192 if (pServiceImage
== NULL
)
194 /* Create a new service image */
195 pServiceImage
= HeapAlloc(GetProcessHeap(),
197 FIELD_OFFSET(SERVICE_IMAGE
, szImagePath
[ImagePath
.Length
/ sizeof(WCHAR
) + 1]));
198 if (pServiceImage
== NULL
)
200 dwError
= ERROR_NOT_ENOUGH_MEMORY
;
204 pServiceImage
->dwImageRunCount
= 1;
205 pServiceImage
->hControlPipe
= INVALID_HANDLE_VALUE
;
207 /* Set the image path */
208 wcscpy(pServiceImage
->szImagePath
,
211 RtlFreeUnicodeString(&ImagePath
);
213 /* Create the control pipe */
214 dwError
= ScmCreateNewControlPipe(pServiceImage
);
215 if (dwError
!= ERROR_SUCCESS
)
217 HeapFree(GetProcessHeap(), 0, pServiceImage
);
221 /* FIXME: Add more initialization code here */
224 /* Append service record */
225 InsertTailList(&ImageListHead
,
226 &pServiceImage
->ImageListEntry
);
230 /* Increment the run counter */
231 pServiceImage
->dwImageRunCount
++;
234 DPRINT("pServiceImage->dwImageRunCount: %lu\n", pServiceImage
->dwImageRunCount
);
236 /* Link the service image to the service */
237 pService
->lpImage
= pServiceImage
;
240 RtlFreeUnicodeString(&ImagePath
);
242 DPRINT("ScmCreateOrReferenceServiceImage() done (Error: %lu)\n", dwError
);
249 ScmDereferenceServiceImage(PSERVICE_IMAGE pServiceImage
)
251 DPRINT1("ScmDereferenceServiceImage() called\n");
253 pServiceImage
->dwImageRunCount
--;
255 if (pServiceImage
->dwImageRunCount
== 0)
257 DPRINT1("dwImageRunCount == 0\n");
259 /* FIXME: Terminate the process */
261 /* Remove the service image from the list */
262 RemoveEntryList(&pServiceImage
->ImageListEntry
);
264 /* Close the control pipe */
265 if (pServiceImage
->hControlPipe
!= INVALID_HANDLE_VALUE
)
266 CloseHandle(pServiceImage
->hControlPipe
);
268 /* Release the service image */
269 HeapFree(GetProcessHeap(), 0, pServiceImage
);
275 ScmGetServiceEntryByName(LPCWSTR lpServiceName
)
277 PLIST_ENTRY ServiceEntry
;
278 PSERVICE CurrentService
;
280 DPRINT("ScmGetServiceEntryByName() called\n");
282 ServiceEntry
= ServiceListHead
.Flink
;
283 while (ServiceEntry
!= &ServiceListHead
)
285 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
288 if (_wcsicmp(CurrentService
->lpServiceName
, lpServiceName
) == 0)
290 DPRINT("Found service: '%S'\n", CurrentService
->lpServiceName
);
291 return CurrentService
;
294 ServiceEntry
= ServiceEntry
->Flink
;
297 DPRINT("Couldn't find a matching service\n");
304 ScmGetServiceEntryByDisplayName(LPCWSTR lpDisplayName
)
306 PLIST_ENTRY ServiceEntry
;
307 PSERVICE CurrentService
;
309 DPRINT("ScmGetServiceEntryByDisplayName() called\n");
311 ServiceEntry
= ServiceListHead
.Flink
;
312 while (ServiceEntry
!= &ServiceListHead
)
314 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
317 if (_wcsicmp(CurrentService
->lpDisplayName
, lpDisplayName
) == 0)
319 DPRINT("Found service: '%S'\n", CurrentService
->lpDisplayName
);
320 return CurrentService
;
323 ServiceEntry
= ServiceEntry
->Flink
;
326 DPRINT("Couldn't find a matching service\n");
333 ScmGetServiceEntryByResumeCount(DWORD dwResumeCount
)
335 PLIST_ENTRY ServiceEntry
;
336 PSERVICE CurrentService
;
338 DPRINT("ScmGetServiceEntryByResumeCount() called\n");
340 ServiceEntry
= ServiceListHead
.Flink
;
341 while (ServiceEntry
!= &ServiceListHead
)
343 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
346 if (CurrentService
->dwResumeCount
> dwResumeCount
)
348 DPRINT("Found service: '%S'\n", CurrentService
->lpDisplayName
);
349 return CurrentService
;
352 ServiceEntry
= ServiceEntry
->Flink
;
355 DPRINT("Couldn't find a matching service\n");
362 ScmCreateNewServiceRecord(LPCWSTR lpServiceName
,
363 PSERVICE
* lpServiceRecord
)
365 PSERVICE lpService
= NULL
;
367 DPRINT("Service: '%S'\n", lpServiceName
);
369 /* Allocate service entry */
370 lpService
= HeapAlloc(GetProcessHeap(),
372 FIELD_OFFSET(SERVICE
, szServiceName
[wcslen(lpServiceName
) + 1]));
373 if (lpService
== NULL
)
374 return ERROR_NOT_ENOUGH_MEMORY
;
376 *lpServiceRecord
= lpService
;
378 /* Copy service name */
379 wcscpy(lpService
->szServiceName
, lpServiceName
);
380 lpService
->lpServiceName
= lpService
->szServiceName
;
381 lpService
->lpDisplayName
= lpService
->lpServiceName
;
383 /* Set the resume count */
384 lpService
->dwResumeCount
= ResumeCount
++;
386 /* Append service record */
387 InsertTailList(&ServiceListHead
,
388 &lpService
->ServiceListEntry
);
390 /* Initialize the service status */
391 lpService
->Status
.dwCurrentState
= SERVICE_STOPPED
;
392 lpService
->Status
.dwControlsAccepted
= 0;
393 lpService
->Status
.dwWin32ExitCode
= ERROR_SERVICE_NEVER_STARTED
;
394 lpService
->Status
.dwServiceSpecificExitCode
= 0;
395 lpService
->Status
.dwCheckPoint
= 0;
396 lpService
->Status
.dwWaitHint
= 2000; /* 2 seconds */
398 return ERROR_SUCCESS
;
403 ScmDeleteServiceRecord(PSERVICE lpService
)
405 DPRINT("Deleting Service %S\n", lpService
->lpServiceName
);
407 /* Delete the display name */
408 if (lpService
->lpDisplayName
!= NULL
&&
409 lpService
->lpDisplayName
!= lpService
->lpServiceName
)
410 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
412 /* Dereference the service image */
413 if (lpService
->lpImage
)
414 ScmDereferenceServiceImage(lpService
->lpImage
);
416 /* Decrement the group reference counter */
417 if (lpService
->lpGroup
)
418 lpService
->lpGroup
->dwRefCount
--;
420 /* FIXME: SecurityDescriptor */
423 /* Remove the Service from the List */
424 RemoveEntryList(&lpService
->ServiceListEntry
);
426 DPRINT("Deleted Service %S\n", lpService
->lpServiceName
);
428 /* Delete the service record */
429 HeapFree(GetProcessHeap(), 0, lpService
);
436 CreateServiceListEntry(LPCWSTR lpServiceName
,
439 PSERVICE lpService
= NULL
;
440 LPWSTR lpDisplayName
= NULL
;
441 LPWSTR lpGroup
= NULL
;
446 DWORD dwErrorControl
;
449 DPRINT("Service: '%S'\n", lpServiceName
);
450 if (*lpServiceName
== L
'{')
451 return ERROR_SUCCESS
;
453 dwSize
= sizeof(DWORD
);
454 dwError
= RegQueryValueExW(hServiceKey
,
458 (LPBYTE
)&dwServiceType
,
460 if (dwError
!= ERROR_SUCCESS
)
461 return ERROR_SUCCESS
;
463 if (((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_OWN_PROCESS
) &&
464 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_SHARE_PROCESS
) &&
465 (dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
466 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
))
467 return ERROR_SUCCESS
;
469 DPRINT("Service type: %lx\n", dwServiceType
);
471 dwSize
= sizeof(DWORD
);
472 dwError
= RegQueryValueExW(hServiceKey
,
476 (LPBYTE
)&dwStartType
,
478 if (dwError
!= ERROR_SUCCESS
)
479 return ERROR_SUCCESS
;
481 DPRINT("Start type: %lx\n", dwStartType
);
483 dwSize
= sizeof(DWORD
);
484 dwError
= RegQueryValueExW(hServiceKey
,
488 (LPBYTE
)&dwErrorControl
,
490 if (dwError
!= ERROR_SUCCESS
)
491 return ERROR_SUCCESS
;
493 DPRINT("Error control: %lx\n", dwErrorControl
);
495 dwError
= RegQueryValueExW(hServiceKey
,
501 if (dwError
!= ERROR_SUCCESS
)
504 DPRINT("Tag: %lx\n", dwTagId
);
506 dwError
= ScmReadString(hServiceKey
,
509 if (dwError
!= ERROR_SUCCESS
)
512 DPRINT("Group: %S\n", lpGroup
);
514 dwError
= ScmReadString(hServiceKey
,
517 if (dwError
!= ERROR_SUCCESS
)
518 lpDisplayName
= NULL
;
520 DPRINT("Display name: %S\n", lpDisplayName
);
522 dwError
= ScmCreateNewServiceRecord(lpServiceName
,
524 if (dwError
!= ERROR_SUCCESS
)
527 lpService
->Status
.dwServiceType
= dwServiceType
;
528 lpService
->dwStartType
= dwStartType
;
529 lpService
->dwErrorControl
= dwErrorControl
;
530 lpService
->dwTag
= dwTagId
;
534 dwError
= ScmSetServiceGroup(lpService
, lpGroup
);
535 if (dwError
!= ERROR_SUCCESS
)
539 if (lpDisplayName
!= NULL
)
541 lpService
->lpDisplayName
= lpDisplayName
;
542 lpDisplayName
= NULL
;
545 DPRINT("ServiceName: '%S'\n", lpService
->lpServiceName
);
546 if (lpService
->lpGroup
!= NULL
)
548 DPRINT("Group: '%S'\n", lpService
->lpGroup
->lpGroupName
);
550 DPRINT("Start %lx Type %lx Tag %lx ErrorControl %lx\n",
551 lpService
->dwStartType
,
552 lpService
->Status
.dwServiceType
,
554 lpService
->dwErrorControl
);
556 if (ScmIsDeleteFlagSet(hServiceKey
))
557 lpService
->bDeleted
= TRUE
;
561 HeapFree(GetProcessHeap(), 0, lpGroup
);
563 if (lpDisplayName
!= NULL
)
564 HeapFree(GetProcessHeap(), 0, lpDisplayName
);
566 if (lpService
!= NULL
)
568 if (lpService
->lpImage
!= NULL
)
569 ScmDereferenceServiceImage(lpService
->lpImage
);
577 ScmDeleteRegKey(HKEY hKey
, LPCWSTR lpszSubKey
)
579 DWORD dwRet
, dwMaxSubkeyLen
= 0, dwSize
;
580 WCHAR szNameBuf
[MAX_PATH
], *lpszName
= szNameBuf
;
583 dwRet
= RegOpenKeyExW(hKey
, lpszSubKey
, 0, KEY_READ
, &hSubKey
);
586 /* Find the maximum subkey length so that we can allocate a buffer */
587 dwRet
= RegQueryInfoKeyW(hSubKey
, NULL
, NULL
, NULL
, NULL
,
588 &dwMaxSubkeyLen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
592 if (dwMaxSubkeyLen
> sizeof(szNameBuf
) / sizeof(WCHAR
))
594 /* Name too big: alloc a buffer for it */
595 lpszName
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, dwMaxSubkeyLen
* sizeof(WCHAR
));
599 dwRet
= ERROR_NOT_ENOUGH_MEMORY
;
602 while (dwRet
== ERROR_SUCCESS
)
604 dwSize
= dwMaxSubkeyLen
;
605 dwRet
= RegEnumKeyExW(hSubKey
, 0, lpszName
, &dwSize
, NULL
, NULL
, NULL
, NULL
);
606 if (dwRet
== ERROR_SUCCESS
|| dwRet
== ERROR_MORE_DATA
)
607 dwRet
= ScmDeleteRegKey(hSubKey
, lpszName
);
609 if (dwRet
== ERROR_NO_MORE_ITEMS
)
610 dwRet
= ERROR_SUCCESS
;
612 if (lpszName
!= szNameBuf
)
613 HeapFree(GetProcessHeap(), 0, lpszName
); /* Free buffer if allocated */
617 RegCloseKey(hSubKey
);
619 dwRet
= RegDeleteKeyW(hKey
, lpszSubKey
);
626 ScmDeleteMarkedServices(VOID
)
628 PLIST_ENTRY ServiceEntry
;
629 PSERVICE CurrentService
;
633 ServiceEntry
= ServiceListHead
.Flink
;
634 while (ServiceEntry
!= &ServiceListHead
)
636 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
638 ServiceEntry
= ServiceEntry
->Flink
;
640 if (CurrentService
->bDeleted
== TRUE
)
642 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
643 L
"System\\CurrentControlSet\\Services",
647 if (dwError
== ERROR_SUCCESS
)
649 dwError
= ScmDeleteRegKey(hServicesKey
, CurrentService
->lpServiceName
);
650 RegCloseKey(hServicesKey
);
651 if (dwError
== ERROR_SUCCESS
)
653 RemoveEntryList(&CurrentService
->ServiceListEntry
);
654 HeapFree(GetProcessHeap(), 0, CurrentService
);
658 if (dwError
!= ERROR_SUCCESS
)
659 DPRINT1("Delete service failed: %S\n", CurrentService
->lpServiceName
);
666 ScmCreateServiceDatabase(VOID
)
668 WCHAR szSubKey
[MAX_PATH
];
672 DWORD dwSubKeyLength
;
673 FILETIME ftLastChanged
;
676 DPRINT("ScmCreateServiceDatabase() called\n");
678 dwError
= ScmCreateGroupList();
679 if (dwError
!= ERROR_SUCCESS
)
682 /* Initialize basic variables */
683 InitializeListHead(&ImageListHead
);
684 InitializeListHead(&ServiceListHead
);
686 /* Initialize the database lock */
687 RtlInitializeResource(&DatabaseLock
);
689 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
690 L
"System\\CurrentControlSet\\Services",
694 if (dwError
!= ERROR_SUCCESS
)
700 dwSubKeyLength
= MAX_PATH
;
701 dwError
= RegEnumKeyExW(hServicesKey
,
709 if (dwError
== ERROR_SUCCESS
&&
712 DPRINT("SubKeyName: '%S'\n", szSubKey
);
714 dwError
= RegOpenKeyExW(hServicesKey
,
719 if (dwError
== ERROR_SUCCESS
)
721 dwError
= CreateServiceListEntry(szSubKey
,
724 RegCloseKey(hServiceKey
);
728 if (dwError
!= ERROR_SUCCESS
)
734 RegCloseKey(hServicesKey
);
736 /* Wait for the LSA server */
739 /* Delete services that are marked for delete */
740 ScmDeleteMarkedServices();
742 DPRINT("ScmCreateServiceDatabase() done\n");
744 return ERROR_SUCCESS
;
749 ScmShutdownServiceDatabase(VOID
)
751 DPRINT("ScmShutdownServiceDatabase() called\n");
753 ScmDeleteMarkedServices();
754 RtlDeleteResource(&DatabaseLock
);
756 DPRINT("ScmShutdownServiceDatabase() done\n");
761 ScmCheckDriver(PSERVICE Service
)
763 OBJECT_ATTRIBUTES ObjectAttributes
;
764 UNICODE_STRING DirName
;
767 POBJECT_DIRECTORY_INFORMATION DirInfo
;
772 DPRINT("ScmCheckDriver(%S) called\n", Service
->lpServiceName
);
774 if (Service
->Status
.dwServiceType
== SERVICE_KERNEL_DRIVER
)
776 RtlInitUnicodeString(&DirName
, L
"\\Driver");
778 else // if (Service->Status.dwServiceType == SERVICE_FILE_SYSTEM_DRIVER)
780 ASSERT(Service
->Status
.dwServiceType
== SERVICE_FILE_SYSTEM_DRIVER
);
781 RtlInitUnicodeString(&DirName
, L
"\\FileSystem");
784 InitializeObjectAttributes(&ObjectAttributes
,
790 Status
= NtOpenDirectoryObject(&DirHandle
,
791 DIRECTORY_QUERY
| DIRECTORY_TRAVERSE
,
793 if (!NT_SUCCESS(Status
))
798 BufferLength
= sizeof(OBJECT_DIRECTORY_INFORMATION
) +
799 2 * MAX_PATH
* sizeof(WCHAR
);
800 DirInfo
= HeapAlloc(GetProcessHeap(),
807 Status
= NtQueryDirectoryObject(DirHandle
,
814 if (Status
== STATUS_NO_MORE_ENTRIES
)
816 /* FIXME: Add current service to 'failed service' list */
817 DPRINT("Service '%S' failed\n", Service
->lpServiceName
);
821 if (!NT_SUCCESS(Status
))
824 DPRINT("Comparing: '%S' '%wZ'\n", Service
->lpServiceName
, &DirInfo
->Name
);
826 if (_wcsicmp(Service
->lpServiceName
, DirInfo
->Name
.Buffer
) == 0)
828 DPRINT("Found: '%S' '%wZ'\n",
829 Service
->lpServiceName
, &DirInfo
->Name
);
831 /* Mark service as 'running' */
832 Service
->Status
.dwCurrentState
= SERVICE_RUNNING
;
834 /* Mark the service group as 'running' */
835 if (Service
->lpGroup
!= NULL
)
837 Service
->lpGroup
->ServicesRunning
= TRUE
;
844 HeapFree(GetProcessHeap(),
849 return STATUS_SUCCESS
;
854 ScmGetBootAndSystemDriverState(VOID
)
856 PLIST_ENTRY ServiceEntry
;
857 PSERVICE CurrentService
;
859 DPRINT("ScmGetBootAndSystemDriverState() called\n");
861 ServiceEntry
= ServiceListHead
.Flink
;
862 while (ServiceEntry
!= &ServiceListHead
)
864 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
866 if (CurrentService
->dwStartType
== SERVICE_BOOT_START
||
867 CurrentService
->dwStartType
== SERVICE_SYSTEM_START
)
870 DPRINT(" Checking service: %S\n", CurrentService
->lpServiceName
);
872 ScmCheckDriver(CurrentService
);
875 ServiceEntry
= ServiceEntry
->Flink
;
878 DPRINT("ScmGetBootAndSystemDriverState() done\n");
883 ScmControlService(PSERVICE Service
,
886 PSCM_CONTROL_PACKET ControlPacket
;
887 SCM_REPLY_PACKET ReplyPacket
;
889 DWORD dwWriteCount
= 0;
890 DWORD dwReadCount
= 0;
893 DWORD dwError
= ERROR_SUCCESS
;
895 #ifdef USE_ASYNCHRONOUS_IO
896 OVERLAPPED Overlapped
= {0, 0, 0, 0, 0};
899 DPRINT("ScmControlService() called\n");
901 /* Acquire the service control critical section, to synchronize requests */
902 EnterCriticalSection(&ControlServiceCriticalSection
);
904 /* Calculate the total length of the start command line */
905 PacketSize
= sizeof(SCM_CONTROL_PACKET
);
906 PacketSize
+= (DWORD
)((wcslen(Service
->lpServiceName
) + 1) * sizeof(WCHAR
));
908 ControlPacket
= HeapAlloc(GetProcessHeap(),
911 if (ControlPacket
== NULL
)
913 LeaveCriticalSection(&ControlServiceCriticalSection
);
914 return ERROR_NOT_ENOUGH_MEMORY
;
917 ControlPacket
->dwSize
= PacketSize
;
918 ControlPacket
->dwControl
= dwControl
;
919 ControlPacket
->hServiceStatus
= (SERVICE_STATUS_HANDLE
)Service
;
921 ControlPacket
->dwServiceNameOffset
= sizeof(SCM_CONTROL_PACKET
);
923 Ptr
= (PWSTR
)((PBYTE
)ControlPacket
+ ControlPacket
->dwServiceNameOffset
);
924 wcscpy(Ptr
, Service
->lpServiceName
);
926 ControlPacket
->dwArgumentsCount
= 0;
927 ControlPacket
->dwArgumentsOffset
= 0;
929 #ifdef USE_ASYNCHRONOUS_IO
930 bResult
= WriteFile(Service
->lpImage
->hControlPipe
,
935 if (bResult
== FALSE
)
937 DPRINT1("WriteFile() returned FALSE\n");
939 dwError
= GetLastError();
940 if (dwError
== ERROR_IO_PENDING
)
942 DPRINT1("dwError: ERROR_IO_PENDING\n");
944 dwError
= WaitForSingleObject(Service
->lpImage
->hControlPipe
,
946 DPRINT1("WaitForSingleObject() returned %lu\n", dwError
);
948 if (dwError
== WAIT_TIMEOUT
)
950 bResult
= CancelIo(Service
->lpImage
->hControlPipe
);
951 if (bResult
== FALSE
)
953 DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
956 dwError
= ERROR_SERVICE_REQUEST_TIMEOUT
;
959 else if (dwError
== WAIT_OBJECT_0
)
961 bResult
= GetOverlappedResult(Service
->lpImage
->hControlPipe
,
965 if (bResult
== FALSE
)
967 dwError
= GetLastError();
968 DPRINT1("GetOverlappedResult() failed (Error %lu)\n", dwError
);
976 DPRINT1("WriteFile() failed (Error %lu)\n", dwError
);
982 Overlapped
.hEvent
= (HANDLE
) NULL
;
984 bResult
= ReadFile(Service
->lpImage
->hControlPipe
,
986 sizeof(SCM_REPLY_PACKET
),
989 if (bResult
== FALSE
)
991 DPRINT1("ReadFile() returned FALSE\n");
993 dwError
= GetLastError();
994 if (dwError
== ERROR_IO_PENDING
)
996 DPRINT1("dwError: ERROR_IO_PENDING\n");
998 dwError
= WaitForSingleObject(Service
->lpImage
->hControlPipe
,
1000 DPRINT1("WaitForSingleObject() returned %lu\n", dwError
);
1002 if (dwError
== WAIT_TIMEOUT
)
1004 bResult
= CancelIo(Service
->lpImage
->hControlPipe
);
1005 if (bResult
== FALSE
)
1007 DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
1010 dwError
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1013 else if (dwError
== WAIT_OBJECT_0
)
1015 bResult
= GetOverlappedResult(Service
->lpImage
->hControlPipe
,
1019 if (bResult
== FALSE
)
1021 dwError
= GetLastError();
1022 DPRINT1("GetOverlappedResult() failed (Error %lu)\n", dwError
);
1030 DPRINT1("ReadFile() failed (Error %lu)\n", dwError
);
1036 /* Send the control packet */
1037 bResult
= WriteFile(Service
->lpImage
->hControlPipe
,
1042 if (bResult
== FALSE
)
1044 dwError
= GetLastError();
1045 DPRINT("WriteFile() failed (Error %lu)\n", dwError
);
1047 if ((dwError
== ERROR_GEN_FAILURE
) &&
1048 (dwControl
== SERVICE_CONTROL_STOP
))
1050 /* Service is already terminated */
1051 Service
->Status
.dwCurrentState
= SERVICE_STOPPED
;
1052 Service
->Status
.dwControlsAccepted
= 0;
1053 Service
->Status
.dwWin32ExitCode
= ERROR_SERVICE_NOT_ACTIVE
;
1054 dwError
= ERROR_SUCCESS
;
1059 /* Read the reply */
1060 bResult
= ReadFile(Service
->lpImage
->hControlPipe
,
1062 sizeof(SCM_REPLY_PACKET
),
1065 if (bResult
== FALSE
)
1067 dwError
= GetLastError();
1068 DPRINT("ReadFile() failed (Error %lu)\n", dwError
);
1073 /* Release the contol packet */
1074 HeapFree(GetProcessHeap(),
1078 if (dwReadCount
== sizeof(SCM_REPLY_PACKET
))
1080 dwError
= ReplyPacket
.dwError
;
1083 if (dwError
== ERROR_SUCCESS
&&
1084 dwControl
== SERVICE_CONTROL_STOP
)
1086 ScmDereferenceServiceImage(Service
->lpImage
);
1089 LeaveCriticalSection(&ControlServiceCriticalSection
);
1091 DPRINT("ScmControlService() done\n");
1098 ScmSendStartCommand(PSERVICE Service
,
1102 PSCM_CONTROL_PACKET ControlPacket
;
1103 SCM_REPLY_PACKET ReplyPacket
;
1106 DWORD dwWriteCount
= 0;
1107 DWORD dwReadCount
= 0;
1108 DWORD dwError
= ERROR_SUCCESS
;
1113 #ifdef USE_ASYNCHRONOUS_IO
1114 OVERLAPPED Overlapped
= {0, 0, 0, 0, 0};
1117 DPRINT("ScmSendStartCommand() called\n");
1119 /* Calculate the total length of the start command line */
1120 PacketSize
= sizeof(SCM_CONTROL_PACKET
) +
1121 (DWORD
)((wcslen(Service
->lpServiceName
) + 1) * sizeof(WCHAR
));
1123 /* Calculate the required packet size for the start arguments */
1124 if (argc
> 0 && argv
!= NULL
)
1126 PacketSize
= ALIGN_UP(PacketSize
, LPWSTR
);
1128 DPRINT("Argc: %lu\n", argc
);
1129 for (i
= 0; i
< argc
; i
++)
1131 DPRINT("Argv[%lu]: %S\n", i
, argv
[i
]);
1132 PacketSize
+= (DWORD
)((wcslen(argv
[i
]) + 1) * sizeof(WCHAR
) + sizeof(PWSTR
));
1136 /* Allocate a control packet */
1137 ControlPacket
= HeapAlloc(GetProcessHeap(),
1140 if (ControlPacket
== NULL
)
1141 return ERROR_NOT_ENOUGH_MEMORY
;
1143 ControlPacket
->dwSize
= PacketSize
;
1144 ControlPacket
->dwControl
= (Service
->Status
.dwServiceType
& SERVICE_WIN32_OWN_PROCESS
)
1145 ? SERVICE_CONTROL_START_OWN
1146 : SERVICE_CONTROL_START_SHARE
;
1147 ControlPacket
->hServiceStatus
= (SERVICE_STATUS_HANDLE
)Service
;
1148 ControlPacket
->dwServiceNameOffset
= sizeof(SCM_CONTROL_PACKET
);
1150 Ptr
= (PWSTR
)((PBYTE
)ControlPacket
+ ControlPacket
->dwServiceNameOffset
);
1151 wcscpy(Ptr
, Service
->lpServiceName
);
1153 ControlPacket
->dwArgumentsCount
= 0;
1154 ControlPacket
->dwArgumentsOffset
= 0;
1156 /* Copy argument list */
1157 if (argc
> 0 && argv
!= NULL
)
1159 Ptr
+= wcslen(Service
->lpServiceName
) + 1;
1160 pOffPtr
= (PWSTR
*)ALIGN_UP_POINTER(Ptr
, PWSTR
);
1161 pArgPtr
= (PWSTR
)((ULONG_PTR
)pOffPtr
+ argc
* sizeof(PWSTR
));
1163 ControlPacket
->dwArgumentsCount
= argc
;
1164 ControlPacket
->dwArgumentsOffset
= (DWORD
)((ULONG_PTR
)pOffPtr
- (ULONG_PTR
)ControlPacket
);
1166 DPRINT("dwArgumentsCount: %lu\n", ControlPacket
->dwArgumentsCount
);
1167 DPRINT("dwArgumentsOffset: %lu\n", ControlPacket
->dwArgumentsOffset
);
1169 for (i
= 0; i
< argc
; i
++)
1171 wcscpy(pArgPtr
, argv
[i
]);
1172 *pOffPtr
= (PWSTR
)((ULONG_PTR
)pArgPtr
- (ULONG_PTR
)pOffPtr
);
1173 DPRINT("offset: %p\n", *pOffPtr
);
1175 pArgPtr
+= wcslen(argv
[i
]) + 1;
1180 #ifdef USE_ASYNCHRONOUS_IO
1181 bResult
= WriteFile(Service
->lpImage
->hControlPipe
,
1186 if (bResult
== FALSE
)
1188 DPRINT1("WriteFile() returned FALSE\n");
1190 dwError
= GetLastError();
1191 if (dwError
== ERROR_IO_PENDING
)
1193 DPRINT1("dwError: ERROR_IO_PENDING\n");
1195 dwError
= WaitForSingleObject(Service
->lpImage
->hControlPipe
,
1197 DPRINT1("WaitForSingleObject() returned %lu\n", dwError
);
1199 if (dwError
== WAIT_TIMEOUT
)
1201 bResult
= CancelIo(Service
->lpImage
->hControlPipe
);
1202 if (bResult
== FALSE
)
1204 DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
1207 dwError
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1210 else if (dwError
== WAIT_OBJECT_0
)
1212 bResult
= GetOverlappedResult(Service
->lpImage
->hControlPipe
,
1216 if (bResult
== FALSE
)
1218 dwError
= GetLastError();
1219 DPRINT1("GetOverlappedResult() failed (Error %lu)\n", dwError
);
1227 DPRINT1("WriteFile() failed (Error %lu)\n", dwError
);
1232 /* Read the reply */
1233 Overlapped
.hEvent
= (HANDLE
) NULL
;
1235 bResult
= ReadFile(Service
->lpImage
->hControlPipe
,
1237 sizeof(SCM_REPLY_PACKET
),
1240 if (bResult
== FALSE
)
1242 DPRINT1("ReadFile() returned FALSE\n");
1244 dwError
= GetLastError();
1245 if (dwError
== ERROR_IO_PENDING
)
1247 DPRINT1("dwError: ERROR_IO_PENDING\n");
1249 dwError
= WaitForSingleObject(Service
->lpImage
->hControlPipe
,
1251 DPRINT1("WaitForSingleObject() returned %lu\n", dwError
);
1253 if (dwError
== WAIT_TIMEOUT
)
1255 bResult
= CancelIo(Service
->lpImage
->hControlPipe
);
1256 if (bResult
== FALSE
)
1258 DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
1261 dwError
= ERROR_SERVICE_REQUEST_TIMEOUT
;
1264 else if (dwError
== WAIT_OBJECT_0
)
1266 bResult
= GetOverlappedResult(Service
->lpImage
->hControlPipe
,
1270 if (bResult
== FALSE
)
1272 dwError
= GetLastError();
1273 DPRINT1("GetOverlappedResult() failed (Error %lu)\n", dwError
);
1281 DPRINT1("ReadFile() failed (Error %lu)\n", dwError
);
1287 /* Send the start command */
1288 bResult
= WriteFile(Service
->lpImage
->hControlPipe
,
1293 if (bResult
== FALSE
)
1295 dwError
= GetLastError();
1296 DPRINT("WriteFile() failed (Error %lu)\n", dwError
);
1300 /* Read the reply */
1301 bResult
= ReadFile(Service
->lpImage
->hControlPipe
,
1303 sizeof(SCM_REPLY_PACKET
),
1306 if (bResult
== FALSE
)
1308 dwError
= GetLastError();
1309 DPRINT("ReadFile() failed (Error %lu)\n", dwError
);
1314 /* Release the contol packet */
1315 HeapFree(GetProcessHeap(),
1319 if (dwReadCount
== sizeof(SCM_REPLY_PACKET
))
1321 dwError
= ReplyPacket
.dwError
;
1324 DPRINT("ScmSendStartCommand() done\n");
1331 ScmWaitForServiceConnect(PSERVICE Service
)
1334 DWORD dwProcessId
= 0;
1335 DWORD dwError
= ERROR_SUCCESS
;
1337 #ifdef USE_ASYNCHRONOUS_IO
1338 OVERLAPPED Overlapped
= {0, 0, 0, 0, 0};
1341 DPRINT("ScmWaitForServiceConnect()\n");
1343 #ifdef USE_ASYNCHRONOUS_IO
1344 Overlapped
.hEvent
= (HANDLE
)NULL
;
1346 bResult
= ConnectNamedPipe(Service
->lpImage
->hControlPipe
,
1348 if (bResult
== FALSE
)
1350 DPRINT("ConnectNamedPipe() returned FALSE\n");
1352 dwError
= GetLastError();
1353 if (dwError
== ERROR_IO_PENDING
)
1355 DPRINT("dwError: ERROR_IO_PENDING\n");
1357 dwError
= WaitForSingleObject(Service
->lpImage
->hControlPipe
,
1359 DPRINT("WaitForSingleObject() returned %lu\n", dwError
);
1361 if (dwError
== WAIT_TIMEOUT
)
1363 DPRINT("WaitForSingleObject() returned WAIT_TIMEOUT\n");
1365 bResult
= CancelIo(Service
->lpImage
->hControlPipe
);
1366 if (bResult
== FALSE
)
1368 DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
1371 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1373 else if (dwError
== WAIT_OBJECT_0
)
1375 bResult
= GetOverlappedResult(Service
->lpImage
->hControlPipe
,
1379 if (bResult
== FALSE
)
1381 dwError
= GetLastError();
1382 DPRINT1("GetOverlappedResult failed (Error %lu)\n", dwError
);
1388 else if (dwError
!= ERROR_PIPE_CONNECTED
)
1390 DPRINT1("ConnectNamedPipe failed (Error %lu)\n", dwError
);
1395 DPRINT("Control pipe connected!\n");
1397 Overlapped
.hEvent
= (HANDLE
) NULL
;
1399 /* Read the process id from pipe */
1400 bResult
= ReadFile(Service
->lpImage
->hControlPipe
,
1401 (LPVOID
)&dwProcessId
,
1405 if (bResult
== FALSE
)
1407 DPRINT("ReadFile() returned FALSE\n");
1409 dwError
= GetLastError();
1410 if (dwError
== ERROR_IO_PENDING
)
1412 DPRINT("dwError: ERROR_IO_PENDING\n");
1414 dwError
= WaitForSingleObject(Service
->lpImage
->hControlPipe
,
1416 if (dwError
== WAIT_TIMEOUT
)
1418 DPRINT("WaitForSingleObject() returned WAIT_TIMEOUT\n");
1420 bResult
= CancelIo(Service
->lpImage
->hControlPipe
);
1421 if (bResult
== FALSE
)
1423 DPRINT1("CancelIo() failed (Error: %lu)\n", GetLastError());
1426 return ERROR_SERVICE_REQUEST_TIMEOUT
;
1428 else if (dwError
== WAIT_OBJECT_0
)
1430 DPRINT("WaitForSingleObject() returned WAIT_OBJECT_0\n");
1432 DPRINT("Process Id: %lu\n", dwProcessId
);
1434 bResult
= GetOverlappedResult(Service
->lpImage
->hControlPipe
,
1438 if (bResult
== FALSE
)
1440 dwError
= GetLastError();
1441 DPRINT1("GetOverlappedResult() failed (Error %lu)\n", dwError
);
1448 DPRINT1("WaitForSingleObject() returned %lu\n", dwError
);
1453 DPRINT1("ReadFile() failed (Error %lu)\n", dwError
);
1458 DPRINT1("ScmWaitForServiceConnect() done\n");
1460 return ERROR_SUCCESS
;
1463 /* Connect control pipe */
1464 if (ConnectNamedPipe(Service
->lpImage
->hControlPipe
, NULL
) ?
1465 TRUE
: (dwError
= GetLastError()) == ERROR_PIPE_CONNECTED
)
1467 DPRINT("Control pipe connected!\n");
1469 /* Read SERVICE_STATUS_HANDLE from pipe */
1470 bResult
= ReadFile(Service
->lpImage
->hControlPipe
,
1471 (LPVOID
)&dwProcessId
,
1475 if (bResult
== FALSE
)
1477 dwError
= GetLastError();
1478 DPRINT1("Reading the service control pipe failed (Error %lu)\n",
1483 dwError
= ERROR_SUCCESS
;
1484 DPRINT("Read control pipe successfully\n");
1489 DPRINT1("Connecting control pipe failed! (Error %lu)\n", dwError
);
1498 ScmStartUserModeService(PSERVICE Service
,
1502 PROCESS_INFORMATION ProcessInformation
;
1503 STARTUPINFOW StartupInfo
;
1505 DWORD dwError
= ERROR_SUCCESS
;
1507 DPRINT("ScmStartUserModeService(%p)\n", Service
);
1509 /* If the image is already running ... */
1510 if (Service
->lpImage
->dwImageRunCount
> 1)
1512 /* ... just send a start command */
1513 return ScmSendStartCommand(Service
, argc
, argv
);
1516 /* Otherwise start its process */
1517 ZeroMemory(&StartupInfo
, sizeof(StartupInfo
));
1518 StartupInfo
.cb
= sizeof(StartupInfo
);
1519 ZeroMemory(&ProcessInformation
, sizeof(ProcessInformation
));
1521 Result
= CreateProcessW(NULL
,
1522 Service
->lpImage
->szImagePath
,
1526 DETACHED_PROCESS
| CREATE_SUSPENDED
,
1530 &ProcessInformation
);
1533 dwError
= GetLastError();
1534 DPRINT1("Starting '%S' failed!\n", Service
->lpServiceName
);
1538 DPRINT("Process Id: %lu Handle %p\n",
1539 ProcessInformation
.dwProcessId
,
1540 ProcessInformation
.hProcess
);
1541 DPRINT("Thread Id: %lu Handle %p\n",
1542 ProcessInformation
.dwThreadId
,
1543 ProcessInformation
.hThread
);
1545 /* Get process handle and id */
1546 Service
->lpImage
->dwProcessId
= ProcessInformation
.dwProcessId
;
1549 ResumeThread(ProcessInformation
.hThread
);
1551 /* Connect control pipe */
1552 dwError
= ScmWaitForServiceConnect(Service
);
1553 if (dwError
== ERROR_SUCCESS
)
1555 /* Send start command */
1556 dwError
= ScmSendStartCommand(Service
, argc
, argv
);
1560 DPRINT1("Connecting control pipe failed! (Error %lu)\n", dwError
);
1561 Service
->lpImage
->dwProcessId
= 0;
1564 /* Close thread and process handle */
1565 CloseHandle(ProcessInformation
.hThread
);
1566 CloseHandle(ProcessInformation
.hProcess
);
1573 ScmLoadService(PSERVICE Service
,
1577 PSERVICE_GROUP Group
= Service
->lpGroup
;
1578 DWORD dwError
= ERROR_SUCCESS
;
1579 LPCWSTR ErrorLogStrings
[2];
1580 WCHAR szErrorBuffer
[32];
1582 DPRINT("ScmLoadService() called\n");
1583 DPRINT("Start Service %p (%S)\n", Service
, Service
->lpServiceName
);
1585 if (Service
->Status
.dwCurrentState
!= SERVICE_STOPPED
)
1587 DPRINT("Service %S is already running!\n", Service
->lpServiceName
);
1588 return ERROR_SERVICE_ALREADY_RUNNING
;
1591 DPRINT("Service->Type: %lu\n", Service
->Status
.dwServiceType
);
1593 if (Service
->Status
.dwServiceType
& SERVICE_DRIVER
)
1596 dwError
= ScmLoadDriver(Service
);
1597 if (dwError
== ERROR_SUCCESS
)
1599 Service
->Status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
;
1600 Service
->Status
.dwCurrentState
= SERVICE_RUNNING
;
1603 else // if (Service->Status.dwServiceType & (SERVICE_WIN32 | SERVICE_INTERACTIVE_PROCESS))
1605 /* Start user-mode service */
1606 dwError
= ScmCreateOrReferenceServiceImage(Service
);
1607 if (dwError
== ERROR_SUCCESS
)
1609 dwError
= ScmStartUserModeService(Service
, argc
, argv
);
1610 if (dwError
== ERROR_SUCCESS
)
1612 #ifdef USE_SERVICE_START_PENDING
1613 Service
->Status
.dwCurrentState
= SERVICE_START_PENDING
;
1615 Service
->Status
.dwCurrentState
= SERVICE_RUNNING
;
1620 ScmDereferenceServiceImage(Service
->lpImage
);
1621 Service
->lpImage
= NULL
;
1626 DPRINT("ScmLoadService() done (Error %lu)\n", dwError
);
1628 if (dwError
== ERROR_SUCCESS
)
1632 Group
->ServicesRunning
= TRUE
;
1637 if (Service
->dwErrorControl
!= SERVICE_ERROR_IGNORE
)
1639 /* Log a failed service start */
1640 swprintf(szErrorBuffer
, L
"%lu", dwError
);
1641 ErrorLogStrings
[0] = Service
->lpServiceName
;
1642 ErrorLogStrings
[1] = szErrorBuffer
;
1643 ScmLogError(EVENT_SERVICE_START_FAILED
,
1649 switch (Service
->dwErrorControl
)
1651 case SERVICE_ERROR_SEVERE
:
1652 if (IsLastKnownGood
== FALSE
)
1654 /* FIXME: Boot last known good configuration */
1658 case SERVICE_ERROR_CRITICAL
:
1659 if (IsLastKnownGood
== FALSE
)
1661 /* FIXME: Boot last known good configuration */
1677 ScmStartService(PSERVICE Service
,
1681 DWORD dwError
= ERROR_SUCCESS
;
1682 SC_RPC_LOCK Lock
= NULL
;
1684 DPRINT("ScmStartService() called\n");
1685 DPRINT("Start Service %p (%S)\n", Service
, Service
->lpServiceName
);
1687 /* Acquire the service control critical section, to synchronize starts */
1688 EnterCriticalSection(&ControlServiceCriticalSection
);
1691 * Acquire the user service start lock while the service is starting, if
1692 * needed (i.e. if we are not starting it during the initialization phase).
1693 * If we don't success, bail out.
1697 dwError
= ScmAcquireServiceStartLock(TRUE
, &Lock
);
1698 if (dwError
!= ERROR_SUCCESS
) goto done
;
1701 /* Really start the service */
1702 dwError
= ScmLoadService(Service
, argc
, argv
);
1704 /* Release the service start lock, if needed, and the critical section */
1705 if (Lock
) ScmReleaseServiceStartLock(&Lock
);
1708 LeaveCriticalSection(&ControlServiceCriticalSection
);
1710 DPRINT("ScmStartService() done (Error %lu)\n", dwError
);
1717 ScmAutoStartServices(VOID
)
1719 DWORD dwError
= ERROR_SUCCESS
;
1720 PLIST_ENTRY GroupEntry
;
1721 PLIST_ENTRY ServiceEntry
;
1722 PSERVICE_GROUP CurrentGroup
;
1723 PSERVICE CurrentService
;
1724 WCHAR szSafeBootServicePath
[MAX_PATH
];
1729 * This function MUST be called ONLY at initialization time.
1730 * Therefore, no need to acquire the user service start lock.
1732 ASSERT(ScmInitialize
);
1734 /* Acquire the service control critical section, to synchronize starts */
1735 EnterCriticalSection(&ControlServiceCriticalSection
);
1737 /* Clear 'ServiceVisited' flag (or set if not to start in Safe Mode) */
1738 ServiceEntry
= ServiceListHead
.Flink
;
1739 while (ServiceEntry
!= &ServiceListHead
)
1741 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1743 /* Build the safe boot path */
1744 wcscpy(szSafeBootServicePath
,
1745 L
"SYSTEM\\CurrentControlSet\\Control\\SafeBoot");
1747 switch (GetSystemMetrics(SM_CLEANBOOT
))
1749 /* NOTE: Assumes MINIMAL (1) and DSREPAIR (3) load same items */
1752 wcscat(szSafeBootServicePath
, L
"\\Minimal\\");
1756 wcscat(szSafeBootServicePath
, L
"\\Network\\");
1760 if (GetSystemMetrics(SM_CLEANBOOT
))
1762 /* If key does not exist then do not assume safe mode */
1763 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
1764 szSafeBootServicePath
,
1768 if (dwError
== ERROR_SUCCESS
)
1772 /* Finish Safe Boot path off */
1773 wcsncat(szSafeBootServicePath
,
1774 CurrentService
->lpServiceName
,
1775 MAX_PATH
- wcslen(szSafeBootServicePath
));
1777 /* Check that the key is in the Safe Boot path */
1778 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
1779 szSafeBootServicePath
,
1783 if (dwError
!= ERROR_SUCCESS
)
1785 /* Mark service as visited so it is not auto-started */
1786 CurrentService
->ServiceVisited
= TRUE
;
1790 /* Must be auto-started in safe mode - mark as unvisited */
1792 CurrentService
->ServiceVisited
= FALSE
;
1797 DPRINT1("WARNING: Could not open the associated Safe Boot key!");
1798 CurrentService
->ServiceVisited
= FALSE
;
1802 ServiceEntry
= ServiceEntry
->Flink
;
1805 /* Start all services which are members of an existing group */
1806 GroupEntry
= GroupListHead
.Flink
;
1807 while (GroupEntry
!= &GroupListHead
)
1809 CurrentGroup
= CONTAINING_RECORD(GroupEntry
, SERVICE_GROUP
, GroupListEntry
);
1811 DPRINT("Group '%S'\n", CurrentGroup
->lpGroupName
);
1813 /* Start all services witch have a valid tag */
1814 for (i
= 0; i
< CurrentGroup
->TagCount
; i
++)
1816 ServiceEntry
= ServiceListHead
.Flink
;
1817 while (ServiceEntry
!= &ServiceListHead
)
1819 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1821 if ((CurrentService
->lpGroup
== CurrentGroup
) &&
1822 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1823 (CurrentService
->ServiceVisited
== FALSE
) &&
1824 (CurrentService
->dwTag
== CurrentGroup
->TagArray
[i
]))
1826 CurrentService
->ServiceVisited
= TRUE
;
1827 ScmLoadService(CurrentService
, 0, NULL
);
1830 ServiceEntry
= ServiceEntry
->Flink
;
1834 /* Start all services which have an invalid tag or which do not have a tag */
1835 ServiceEntry
= ServiceListHead
.Flink
;
1836 while (ServiceEntry
!= &ServiceListHead
)
1838 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1840 if ((CurrentService
->lpGroup
== CurrentGroup
) &&
1841 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1842 (CurrentService
->ServiceVisited
== FALSE
))
1844 CurrentService
->ServiceVisited
= TRUE
;
1845 ScmLoadService(CurrentService
, 0, NULL
);
1848 ServiceEntry
= ServiceEntry
->Flink
;
1851 GroupEntry
= GroupEntry
->Flink
;
1854 /* Start all services which are members of any non-existing group */
1855 ServiceEntry
= ServiceListHead
.Flink
;
1856 while (ServiceEntry
!= &ServiceListHead
)
1858 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1860 if ((CurrentService
->lpGroup
!= NULL
) &&
1861 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1862 (CurrentService
->ServiceVisited
== FALSE
))
1864 CurrentService
->ServiceVisited
= TRUE
;
1865 ScmLoadService(CurrentService
, 0, NULL
);
1868 ServiceEntry
= ServiceEntry
->Flink
;
1871 /* Start all services which are not a member of any group */
1872 ServiceEntry
= ServiceListHead
.Flink
;
1873 while (ServiceEntry
!= &ServiceListHead
)
1875 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1877 if ((CurrentService
->lpGroup
== NULL
) &&
1878 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1879 (CurrentService
->ServiceVisited
== FALSE
))
1881 CurrentService
->ServiceVisited
= TRUE
;
1882 ScmLoadService(CurrentService
, 0, NULL
);
1885 ServiceEntry
= ServiceEntry
->Flink
;
1888 /* Clear 'ServiceVisited' flag again */
1889 ServiceEntry
= ServiceListHead
.Flink
;
1890 while (ServiceEntry
!= &ServiceListHead
)
1892 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1893 CurrentService
->ServiceVisited
= FALSE
;
1894 ServiceEntry
= ServiceEntry
->Flink
;
1897 /* Release the critical section */
1898 LeaveCriticalSection(&ControlServiceCriticalSection
);
1903 ScmAutoShutdownServices(VOID
)
1905 PLIST_ENTRY ServiceEntry
;
1906 PSERVICE CurrentService
;
1908 DPRINT("ScmAutoShutdownServices() called\n");
1910 /* Lock the service database exclusively */
1911 ScmLockDatabaseExclusive();
1913 ServiceEntry
= ServiceListHead
.Flink
;
1914 while (ServiceEntry
!= &ServiceListHead
)
1916 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1918 if (CurrentService
->Status
.dwCurrentState
== SERVICE_RUNNING
||
1919 CurrentService
->Status
.dwCurrentState
== SERVICE_START_PENDING
)
1921 /* shutdown service */
1922 DPRINT("Shutdown service: %S\n", CurrentService
->szServiceName
);
1923 ScmControlService(CurrentService
, SERVICE_CONTROL_SHUTDOWN
);
1926 ServiceEntry
= ServiceEntry
->Flink
;
1929 /* Unlock the service database */
1930 ScmUnlockDatabase();
1932 DPRINT("ScmAutoShutdownServices() done\n");
1937 ScmLockDatabaseExclusive(VOID
)
1939 return RtlAcquireResourceExclusive(&DatabaseLock
, TRUE
);
1944 ScmLockDatabaseShared(VOID
)
1946 return RtlAcquireResourceShared(&DatabaseLock
, TRUE
);
1951 ScmUnlockDatabase(VOID
)
1953 RtlReleaseResource(&DatabaseLock
);
1958 ScmInitNamedPipeCriticalSection(VOID
)
1964 InitializeCriticalSection(&ControlServiceCriticalSection
);
1966 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
1967 L
"SYSTEM\\CurrentControlSet\\Control",
1971 if (dwError
== ERROR_SUCCESS
)
1973 dwKeySize
= sizeof(DWORD
);
1974 RegQueryValueExW(hKey
,
1975 L
"ServicesPipeTimeout",
1978 (LPBYTE
)&PipeTimeout
,
1987 ScmDeleteNamedPipeCriticalSection(VOID
)
1989 DeleteCriticalSection(&ControlServiceCriticalSection
);