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 /* GLOBALS *******************************************************************/
23 LIST_ENTRY ServiceListHead
;
25 static RTL_RESOURCE DatabaseLock
;
26 static DWORD dwResumeCount
= 1;
29 /* FUNCTIONS *****************************************************************/
33 ScmGetServiceEntryByName(LPWSTR lpServiceName
)
35 PLIST_ENTRY ServiceEntry
;
36 PSERVICE CurrentService
;
38 DPRINT("ScmGetServiceEntryByName() called\n");
40 ServiceEntry
= ServiceListHead
.Flink
;
41 while (ServiceEntry
!= &ServiceListHead
)
43 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
46 if (_wcsicmp(CurrentService
->lpServiceName
, lpServiceName
) == 0)
48 DPRINT("Found service: '%S'\n", CurrentService
->lpServiceName
);
49 return CurrentService
;
52 ServiceEntry
= ServiceEntry
->Flink
;
55 DPRINT("Couldn't find a matching service\n");
62 ScmGetServiceEntryByDisplayName(LPWSTR lpDisplayName
)
64 PLIST_ENTRY ServiceEntry
;
65 PSERVICE CurrentService
;
67 DPRINT("ScmGetServiceEntryByDisplayName() called\n");
69 ServiceEntry
= ServiceListHead
.Flink
;
70 while (ServiceEntry
!= &ServiceListHead
)
72 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
75 if (_wcsicmp(CurrentService
->lpDisplayName
, lpDisplayName
) == 0)
77 DPRINT("Found service: '%S'\n", CurrentService
->lpDisplayName
);
78 return CurrentService
;
81 ServiceEntry
= ServiceEntry
->Flink
;
84 DPRINT("Couldn't find a matching service\n");
91 ScmGetServiceEntryByResumeCount(DWORD dwResumeCount
)
93 PLIST_ENTRY ServiceEntry
;
94 PSERVICE CurrentService
;
96 DPRINT("ScmGetServiceEntryByResumeCount() called\n");
98 ServiceEntry
= ServiceListHead
.Flink
;
99 while (ServiceEntry
!= &ServiceListHead
)
101 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
104 if (CurrentService
->dwResumeCount
> dwResumeCount
)
106 DPRINT("Found service: '%S'\n", CurrentService
->lpDisplayName
);
107 return CurrentService
;
110 ServiceEntry
= ServiceEntry
->Flink
;
113 DPRINT("Couldn't find a matching service\n");
120 ScmGetServiceEntryByClientHandle(ULONG Handle
)
122 PLIST_ENTRY ServiceEntry
;
123 PSERVICE CurrentService
;
125 DPRINT("ScmGetServiceEntryByClientHandle() called\n");
126 DPRINT("looking for %lu\n", Handle
);
128 ServiceEntry
= ServiceListHead
.Flink
;
129 while (ServiceEntry
!= &ServiceListHead
)
131 CurrentService
= CONTAINING_RECORD(ServiceEntry
,
135 if (CurrentService
->hClient
== Handle
)
137 DPRINT("Found service: '%S'\n", CurrentService
->lpDisplayName
);
138 return CurrentService
;
141 ServiceEntry
= ServiceEntry
->Flink
;
144 DPRINT("Couldn't find a matching service\n");
151 ScmCreateNewServiceRecord(LPWSTR lpServiceName
,
152 PSERVICE
*lpServiceRecord
)
154 PSERVICE lpService
= NULL
;
156 DPRINT("Service: '%S'\n", lpServiceName
);
158 /* Allocate service entry */
159 lpService
= (SERVICE
*) HeapAlloc(GetProcessHeap(),
161 sizeof(SERVICE
) + ((wcslen(lpServiceName
) + 1) * sizeof(WCHAR
)));
162 if (lpService
== NULL
)
163 return ERROR_NOT_ENOUGH_MEMORY
;
165 *lpServiceRecord
= lpService
;
167 /* Copy service name */
168 wcscpy(lpService
->szServiceName
, lpServiceName
);
169 lpService
->lpServiceName
= lpService
->szServiceName
;
170 lpService
->lpDisplayName
= lpService
->lpServiceName
;
172 /* Set the resume count */
173 lpService
->dwResumeCount
= dwResumeCount
++;
175 /* Append service entry */
176 InsertTailList(&ServiceListHead
,
177 &lpService
->ServiceListEntry
);
179 lpService
->Status
.dwCurrentState
= SERVICE_STOPPED
;
180 lpService
->Status
.dwControlsAccepted
= 0;
181 lpService
->Status
.dwWin32ExitCode
= ERROR_SERVICE_NEVER_STARTED
;
182 lpService
->Status
.dwServiceSpecificExitCode
= 0;
183 lpService
->Status
.dwCheckPoint
= 0;
184 lpService
->Status
.dwWaitHint
= 2000; /* 2 seconds */
186 return ERROR_SUCCESS
;
191 CreateServiceListEntry(LPWSTR lpServiceName
,
194 PSERVICE lpService
= NULL
;
195 LPWSTR lpDisplayName
= NULL
;
196 LPWSTR lpGroup
= NULL
;
201 DWORD dwErrorControl
;
204 DPRINT("Service: '%S'\n", lpServiceName
);
205 if (*lpServiceName
== L
'{')
206 return ERROR_SUCCESS
;
208 dwSize
= sizeof(DWORD
);
209 dwError
= RegQueryValueExW(hServiceKey
,
213 (LPBYTE
)&dwServiceType
,
215 if (dwError
!= ERROR_SUCCESS
)
216 return ERROR_SUCCESS
;
218 if (((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_OWN_PROCESS
) &&
219 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_SHARE_PROCESS
) &&
220 (dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
221 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
))
222 return ERROR_SUCCESS
;
224 DPRINT("Service type: %lx\n", dwServiceType
);
226 dwSize
= sizeof(DWORD
);
227 dwError
= RegQueryValueExW(hServiceKey
,
231 (LPBYTE
)&dwStartType
,
233 if (dwError
!= ERROR_SUCCESS
)
234 return ERROR_SUCCESS
;
236 DPRINT("Start type: %lx\n", dwStartType
);
238 dwSize
= sizeof(DWORD
);
239 dwError
= RegQueryValueExW(hServiceKey
,
243 (LPBYTE
)&dwErrorControl
,
245 if (dwError
!= ERROR_SUCCESS
)
246 return ERROR_SUCCESS
;
248 DPRINT("Error control: %lx\n", dwErrorControl
);
250 dwError
= RegQueryValueExW(hServiceKey
,
256 if (dwError
!= ERROR_SUCCESS
)
259 DPRINT("Tag: %lx\n", dwTagId
);
261 dwError
= ScmReadString(hServiceKey
,
264 if (dwError
!= ERROR_SUCCESS
)
267 DPRINT("Group: %S\n", lpGroup
);
269 dwError
= ScmReadString(hServiceKey
,
272 if (dwError
!= ERROR_SUCCESS
)
273 lpDisplayName
= NULL
;
275 DPRINT("Display name: %S\n", lpDisplayName
);
277 dwError
= ScmCreateNewServiceRecord(lpServiceName
,
279 if (dwError
!= ERROR_SUCCESS
)
282 lpService
->Status
.dwServiceType
= dwServiceType
;
283 lpService
->dwStartType
= dwStartType
;
284 lpService
->dwErrorControl
= dwErrorControl
;
285 lpService
->dwTag
= dwTagId
;
289 dwError
= ScmSetServiceGroup(lpService
, lpGroup
);
290 if (dwError
!= ERROR_SUCCESS
)
294 if (lpDisplayName
!= NULL
)
296 lpService
->lpDisplayName
= lpDisplayName
;
297 lpDisplayName
= NULL
;
300 DPRINT("ServiceName: '%S'\n", lpService
->lpServiceName
);
301 if (lpService
->lpGroup
!= NULL
)
303 DPRINT("Group: '%S'\n", lpService
->lpGroup
->lpGroupName
);
305 DPRINT("Start %lx Type %lx Tag %lx ErrorControl %lx\n",
306 lpService
->dwStartType
,
307 lpService
->Status
.dwServiceType
,
309 lpService
->dwErrorControl
);
311 if (ScmIsDeleteFlagSet(hServiceKey
))
312 lpService
->bDeleted
= TRUE
;
316 HeapFree(GetProcessHeap(), 0, lpGroup
);
318 if (lpDisplayName
!= NULL
)
319 HeapFree(GetProcessHeap(), 0, lpDisplayName
);
326 ScmDeleteMarkedServices(VOID
)
328 PLIST_ENTRY ServiceEntry
;
329 PSERVICE CurrentService
;
331 ServiceEntry
= ServiceListHead
.Flink
;
332 while (ServiceEntry
!= &ServiceListHead
)
334 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
336 ServiceEntry
= ServiceEntry
->Flink
;
338 if (CurrentService
->bDeleted
== TRUE
)
340 DPRINT1("Delete service: %S\n", CurrentService
->lpServiceName
);
342 /* FIXME: Delete the registry keys */
344 /* FIXME: Delete the service record from the list */
352 ScmCreateServiceDatabase(VOID
)
354 WCHAR szSubKey
[MAX_PATH
];
358 DWORD dwSubKeyLength
;
359 FILETIME ftLastChanged
;
362 DPRINT("ScmCreateServiceDatabase() called\n");
364 dwError
= ScmCreateGroupList();
365 if (dwError
!= ERROR_SUCCESS
)
368 /* Initialize basic variables */
369 InitializeListHead(&ServiceListHead
);
371 /* Initialize the database lock */
372 RtlInitializeResource(&DatabaseLock
);
374 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
375 L
"System\\CurrentControlSet\\Services",
379 if (dwError
!= ERROR_SUCCESS
)
385 dwSubKeyLength
= MAX_PATH
;
386 dwError
= RegEnumKeyExW(hServicesKey
,
394 if (dwError
== ERROR_SUCCESS
&&
397 DPRINT("SubKeyName: '%S'\n", szSubKey
);
399 dwError
= RegOpenKeyExW(hServicesKey
,
404 if (dwError
== ERROR_SUCCESS
)
406 dwError
= CreateServiceListEntry(szSubKey
,
409 RegCloseKey(hServiceKey
);
413 if (dwError
!= ERROR_SUCCESS
)
419 RegCloseKey(hServicesKey
);
421 /* Delete services that are marked for delete */
422 ScmDeleteMarkedServices();
424 DPRINT("ScmCreateServiceDatabase() done\n");
426 return ERROR_SUCCESS
;
431 ScmShutdownServiceDatabase(VOID
)
433 DPRINT("ScmShutdownServiceDatabase() called\n");
435 ScmDeleteMarkedServices();
436 RtlDeleteResource(&DatabaseLock
);
438 DPRINT("ScmShutdownServiceDatabase() done\n");
443 ScmCheckDriver(PSERVICE Service
)
445 OBJECT_ATTRIBUTES ObjectAttributes
;
446 UNICODE_STRING DirName
;
449 POBJECT_DIRECTORY_INFORMATION DirInfo
;
454 DPRINT("ScmCheckDriver(%S) called\n", Service
->lpServiceName
);
456 if (Service
->Status
.dwServiceType
== SERVICE_KERNEL_DRIVER
)
458 RtlInitUnicodeString(&DirName
,
463 RtlInitUnicodeString(&DirName
,
467 InitializeObjectAttributes(&ObjectAttributes
,
473 Status
= NtOpenDirectoryObject(&DirHandle
,
474 DIRECTORY_QUERY
| DIRECTORY_TRAVERSE
,
476 if (!NT_SUCCESS(Status
))
481 BufferLength
= sizeof(OBJECT_DIRECTORY_INFORMATION
) +
482 2 * MAX_PATH
* sizeof(WCHAR
);
483 DirInfo
= (OBJECT_DIRECTORY_INFORMATION
*) HeapAlloc(GetProcessHeap(),
490 Status
= NtQueryDirectoryObject(DirHandle
,
497 if (Status
== STATUS_NO_MORE_ENTRIES
)
499 /* FIXME: Add current service to 'failed service' list */
500 DPRINT("Service '%S' failed\n", Service
->lpServiceName
);
504 if (!NT_SUCCESS(Status
))
507 DPRINT("Comparing: '%S' '%wZ'\n", Service
->lpServiceName
, &DirInfo
->Name
);
509 if (_wcsicmp(Service
->lpServiceName
, DirInfo
->Name
.Buffer
) == 0)
511 DPRINT("Found: '%S' '%wZ'\n",
512 Service
->lpServiceName
, &DirInfo
->Name
);
514 /* Mark service as 'running' */
515 Service
->Status
.dwCurrentState
= SERVICE_RUNNING
;
517 /* Mark the service group as 'running' */
518 if (Service
->lpGroup
!= NULL
)
520 Service
->lpGroup
->ServicesRunning
= TRUE
;
527 HeapFree(GetProcessHeap(),
532 return STATUS_SUCCESS
;
537 ScmGetBootAndSystemDriverState(VOID
)
539 PLIST_ENTRY ServiceEntry
;
540 PSERVICE CurrentService
;
542 DPRINT("ScmGetBootAndSystemDriverState() called\n");
544 ServiceEntry
= ServiceListHead
.Flink
;
545 while (ServiceEntry
!= &ServiceListHead
)
547 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
549 if (CurrentService
->dwStartType
== SERVICE_BOOT_START
||
550 CurrentService
->dwStartType
== SERVICE_SYSTEM_START
)
553 DPRINT(" Checking service: %S\n", CurrentService
->lpServiceName
);
555 ScmCheckDriver(CurrentService
);
558 ServiceEntry
= ServiceEntry
->Flink
;
561 DPRINT("ScmGetBootAndSystemDriverState() done\n");
566 ScmControlService(PSERVICE Service
,
568 LPSERVICE_STATUS lpServiceStatus
)
570 PSCM_CONTROL_PACKET ControlPacket
;
574 DPRINT("ScmControlService() called\n");
576 TotalLength
= wcslen(Service
->lpServiceName
) + 1;
578 ControlPacket
= (SCM_CONTROL_PACKET
*)HeapAlloc(GetProcessHeap(),
580 sizeof(SCM_CONTROL_PACKET
) + (TotalLength
* sizeof(WCHAR
)));
581 if (ControlPacket
== NULL
)
582 return ERROR_NOT_ENOUGH_MEMORY
;
584 ControlPacket
->dwControl
= dwControl
;
585 ControlPacket
->hClient
= Service
->hClient
;
586 ControlPacket
->dwSize
= TotalLength
;
587 wcscpy(&ControlPacket
->szArguments
[0], Service
->lpServiceName
);
589 /* Send the start command */
590 WriteFile(Service
->ControlPipeHandle
,
592 sizeof(SCM_CONTROL_PACKET
) + (TotalLength
* sizeof(WCHAR
)),
596 /* FIXME: Read the reply */
598 /* Release the contol packet */
599 HeapFree(GetProcessHeap(),
603 RtlCopyMemory(lpServiceStatus
,
605 sizeof(SERVICE_STATUS
));
607 DPRINT("ScmControlService) done\n");
609 return ERROR_SUCCESS
;
614 ScmSendStartCommand(PSERVICE Service
,
617 PSCM_CONTROL_PACKET ControlPacket
;
619 DWORD ArgsLength
= 0;
624 DPRINT("ScmSendStartCommand() called\n");
626 /* Calculate the total length of the start command line */
627 TotalLength
= wcslen(Service
->lpServiceName
) + 1;
628 if (Arguments
!= NULL
)
633 Length
= wcslen(Ptr
) + 1;
634 TotalLength
+= Length
;
635 ArgsLength
+= Length
;
637 DPRINT("Arg: %S\n", Ptr
);
641 DPRINT("ArgsLength: %ld TotalLength: %ld\n", ArgsLength
, TotalLength
);
643 /* Allocate a control packet */
644 ControlPacket
= (SCM_CONTROL_PACKET
*)HeapAlloc(GetProcessHeap(),
646 sizeof(SCM_CONTROL_PACKET
) + (TotalLength
- 1) * sizeof(WCHAR
));
647 if (ControlPacket
== NULL
)
648 return ERROR_NOT_ENOUGH_MEMORY
;
650 ControlPacket
->dwControl
= SERVICE_CONTROL_START
;
651 ControlPacket
->hClient
= Service
->hClient
;
652 ControlPacket
->dwSize
= TotalLength
;
653 Ptr
= &ControlPacket
->szArguments
[0];
654 wcscpy(Ptr
, Service
->lpServiceName
);
655 Ptr
+= (wcslen(Service
->lpServiceName
) + 1);
657 /* Copy argument list */
658 if (Arguments
!= NULL
)
660 memcpy(Ptr
, Arguments
, ArgsLength
);
664 /* Terminate the argument list */
667 /* Send the start command */
668 WriteFile(Service
->ControlPipeHandle
,
670 sizeof(SCM_CONTROL_PACKET
) + (TotalLength
- 1) * sizeof(WCHAR
),
674 /* FIXME: Read the reply */
676 /* Release the contol packet */
677 HeapFree(GetProcessHeap(),
681 DPRINT("ScmSendStartCommand() done\n");
683 return ERROR_SUCCESS
;
688 ScmStartUserModeService(PSERVICE Service
,
691 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
692 PROCESS_INFORMATION ProcessInformation
;
693 STARTUPINFOW StartupInfo
;
694 UNICODE_STRING ImagePath
;
696 DWORD ServiceCurrent
= 0;
699 DWORD dwError
= ERROR_SUCCESS
;
700 WCHAR NtControlPipeName
[MAX_PATH
+ 1];
702 RtlInitUnicodeString(&ImagePath
, NULL
);
704 /* Get service data */
705 RtlZeroMemory(&QueryTable
,
708 QueryTable
[0].Name
= L
"Type";
709 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_QUERY_REGISTRY_REQUIRED
;
710 QueryTable
[0].EntryContext
= &Type
;
712 QueryTable
[1].Name
= L
"ImagePath";
713 QueryTable
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_QUERY_REGISTRY_REQUIRED
;
714 QueryTable
[1].EntryContext
= &ImagePath
;
716 Status
= RtlQueryRegistryValues(RTL_REGISTRY_SERVICES
,
717 Service
->lpServiceName
,
721 if (!NT_SUCCESS(Status
))
723 DPRINT1("RtlQueryRegistryValues() failed (Status %lx)\n", Status
);
724 return RtlNtStatusToDosError(Status
);
726 DPRINT("ImagePath: '%S'\n", ImagePath
.Buffer
);
727 DPRINT("Type: %lx\n", Type
);
729 /* Get the service number */
730 RtlZeroMemory(&QueryTable
,
733 QueryTable
[0].Name
= L
"";
734 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_QUERY_REGISTRY_REQUIRED
;
735 QueryTable
[0].EntryContext
= &ServiceCurrent
;
737 Status
= RtlQueryRegistryValues(RTL_REGISTRY_CONTROL
,
743 if (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
745 /* TODO: Create registry entry with correct write access */
747 else if (!NT_SUCCESS(Status
))
749 DPRINT1("RtlQueryRegistryValues() failed (Status %lx)\n", Status
);
750 return RtlNtStatusToDosError(Status
);
757 Status
= RtlWriteRegistryValue(RTL_REGISTRY_CONTROL
, L
"ServiceCurrent", L
"", REG_DWORD
, &ServiceCurrent
, sizeof(ServiceCurrent
));
759 if (!NT_SUCCESS(Status
))
761 DPRINT1("RtlWriteRegistryValue() failed (Status %lx)\n", Status
);
762 return RtlNtStatusToDosError(Status
);
765 /* Create '\\.\pipe\net\NtControlPipeXXX' instance */
766 swprintf(NtControlPipeName
, L
"\\\\.\\pipe\\net\\NtControlPipe%u", ServiceCurrent
);
767 Service
->ControlPipeHandle
= CreateNamedPipeW(NtControlPipeName
,
769 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
775 DPRINT1("CreateNamedPipeW(%S) done\n", NtControlPipeName
);
776 if (Service
->ControlPipeHandle
== INVALID_HANDLE_VALUE
)
778 DPRINT1("Failed to create control pipe!\n");
779 return GetLastError();
782 StartupInfo
.cb
= sizeof(StartupInfo
);
783 StartupInfo
.lpReserved
= NULL
;
784 StartupInfo
.lpDesktop
= NULL
;
785 StartupInfo
.lpTitle
= NULL
;
786 StartupInfo
.dwFlags
= 0;
787 StartupInfo
.cbReserved2
= 0;
788 StartupInfo
.lpReserved2
= 0;
790 Result
= CreateProcessW(ImagePath
.Buffer
,
795 DETACHED_PROCESS
| CREATE_SUSPENDED
,
799 &ProcessInformation
);
800 RtlFreeUnicodeString(&ImagePath
);
804 dwError
= GetLastError();
805 /* Close control pipe */
806 CloseHandle(Service
->ControlPipeHandle
);
807 Service
->ControlPipeHandle
= INVALID_HANDLE_VALUE
;
809 DPRINT1("Starting '%S' failed!\n", Service
->lpServiceName
);
813 DPRINT("Process Id: %lu Handle %lx\n",
814 ProcessInformation
.dwProcessId
,
815 ProcessInformation
.hProcess
);
816 DPRINT("Thread Id: %lu Handle %lx\n",
817 ProcessInformation
.dwThreadId
,
818 ProcessInformation
.hThread
);
820 /* Get process and thread ids */
821 Service
->ProcessId
= ProcessInformation
.dwProcessId
;
822 Service
->ThreadId
= ProcessInformation
.dwThreadId
;
825 ResumeThread(ProcessInformation
.hThread
);
827 /* Connect control pipe */
828 if (ConnectNamedPipe(Service
->ControlPipeHandle
, NULL
) ?
829 TRUE
: (dwError
= GetLastError()) == ERROR_PIPE_CONNECTED
)
833 DPRINT("Control pipe connected!\n");
835 /* Read SERVICE_STATUS_HANDLE from pipe */
836 if (!ReadFile(Service
->ControlPipeHandle
,
837 (LPVOID
)&Service
->hClient
,
842 dwError
= GetLastError();
843 DPRINT1("Reading the service control pipe failed (Error %lu)\n",
848 DPRINT("Received service status %lu\n", Service
->hClient
);
850 /* Send start command */
851 dwError
= ScmSendStartCommand(Service
, lpArgs
);
856 DPRINT1("Connecting control pipe failed! (Error %lu)\n", dwError
);
858 /* Close control pipe */
859 CloseHandle(Service
->ControlPipeHandle
);
860 Service
->ControlPipeHandle
= INVALID_HANDLE_VALUE
;
861 Service
->ProcessId
= 0;
862 Service
->ThreadId
= 0;
865 /* Close process and thread handle */
866 CloseHandle(ProcessInformation
.hThread
);
867 CloseHandle(ProcessInformation
.hProcess
);
874 ScmStartService(PSERVICE Service
, LPWSTR lpArgs
)
876 PSERVICE_GROUP Group
= Service
->lpGroup
;
877 DWORD dwError
= ERROR_SUCCESS
;
879 DPRINT("ScmStartService() called\n");
881 Service
->ControlPipeHandle
= INVALID_HANDLE_VALUE
;
882 DPRINT("Service->Type: %lu\n", Service
->Status
.dwServiceType
);
884 if (Service
->Status
.dwServiceType
& SERVICE_DRIVER
)
887 dwError
= ScmLoadDriver(Service
);
888 if (dwError
== ERROR_SUCCESS
)
889 Service
->Status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
;
893 /* Start user-mode service */
894 dwError
= ScmStartUserModeService(Service
, lpArgs
);
897 DPRINT("ScmStartService() done (Error %lu)\n", dwError
);
899 if (dwError
== ERROR_SUCCESS
)
903 Group
->ServicesRunning
= TRUE
;
905 Service
->Status
.dwCurrentState
= SERVICE_RUNNING
;
910 switch (Service
->ErrorControl
)
912 case SERVICE_ERROR_NORMAL
:
913 /* FIXME: Log error */
916 case SERVICE_ERROR_SEVERE
:
917 if (IsLastKnownGood
== FALSE
)
919 /* FIXME: Boot last known good configuration */
923 case SERVICE_ERROR_CRITICAL
:
924 if (IsLastKnownGood
== FALSE
)
926 /* FIXME: Boot last known good configuration */
942 ScmAutoStartServices(VOID
)
944 PLIST_ENTRY GroupEntry
;
945 PLIST_ENTRY ServiceEntry
;
946 PSERVICE_GROUP CurrentGroup
;
947 PSERVICE CurrentService
;
950 /* Clear 'ServiceVisited' flag */
951 ServiceEntry
= ServiceListHead
.Flink
;
952 while (ServiceEntry
!= &ServiceListHead
)
954 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
955 CurrentService
->ServiceVisited
= FALSE
;
956 ServiceEntry
= ServiceEntry
->Flink
;
959 /* Start all services which are members of an existing group */
960 GroupEntry
= GroupListHead
.Flink
;
961 while (GroupEntry
!= &GroupListHead
)
963 CurrentGroup
= CONTAINING_RECORD(GroupEntry
, SERVICE_GROUP
, GroupListEntry
);
965 DPRINT("Group '%S'\n", CurrentGroup
->lpGroupName
);
967 /* Start all services witch have a valid tag */
968 for (i
= 0; i
< CurrentGroup
->TagCount
; i
++)
970 ServiceEntry
= ServiceListHead
.Flink
;
971 while (ServiceEntry
!= &ServiceListHead
)
973 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
975 if ((CurrentService
->lpGroup
== CurrentGroup
) &&
976 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
977 (CurrentService
->ServiceVisited
== FALSE
) &&
978 (CurrentService
->dwTag
== CurrentGroup
->TagArray
[i
]))
980 CurrentService
->ServiceVisited
= TRUE
;
981 ScmStartService(CurrentService
, NULL
);
984 ServiceEntry
= ServiceEntry
->Flink
;
988 /* Start all services which have an invalid tag or which do not have a tag */
989 ServiceEntry
= ServiceListHead
.Flink
;
990 while (ServiceEntry
!= &ServiceListHead
)
992 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
994 if ((CurrentService
->lpGroup
== CurrentGroup
) &&
995 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
996 (CurrentService
->ServiceVisited
== FALSE
))
998 CurrentService
->ServiceVisited
= TRUE
;
999 ScmStartService(CurrentService
, NULL
);
1002 ServiceEntry
= ServiceEntry
->Flink
;
1005 GroupEntry
= GroupEntry
->Flink
;
1008 /* Start all services which are members of any non-existing group */
1009 ServiceEntry
= ServiceListHead
.Flink
;
1010 while (ServiceEntry
!= &ServiceListHead
)
1012 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1014 if ((CurrentService
->lpGroup
!= NULL
) &&
1015 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1016 (CurrentService
->ServiceVisited
== FALSE
))
1018 CurrentService
->ServiceVisited
= TRUE
;
1019 ScmStartService(CurrentService
, NULL
);
1022 ServiceEntry
= ServiceEntry
->Flink
;
1025 /* Start all services which are not a member of any group */
1026 ServiceEntry
= ServiceListHead
.Flink
;
1027 while (ServiceEntry
!= &ServiceListHead
)
1029 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1031 if ((CurrentService
->lpGroup
== NULL
) &&
1032 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1033 (CurrentService
->ServiceVisited
== FALSE
))
1035 CurrentService
->ServiceVisited
= TRUE
;
1036 ScmStartService(CurrentService
, NULL
);
1039 ServiceEntry
= ServiceEntry
->Flink
;
1042 /* Clear 'ServiceVisited' flag again */
1043 ServiceEntry
= ServiceListHead
.Flink
;
1044 while (ServiceEntry
!= &ServiceListHead
)
1046 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1047 CurrentService
->ServiceVisited
= FALSE
;
1048 ServiceEntry
= ServiceEntry
->Flink
;
1054 ScmAutoShutdownServices(VOID
)
1056 PLIST_ENTRY ServiceEntry
;
1057 PSERVICE CurrentService
;
1058 SERVICE_STATUS ServiceStatus
;
1060 DPRINT("ScmAutoShutdownServices() called\n");
1062 ServiceEntry
= ServiceListHead
.Flink
;
1063 while (ServiceEntry
!= &ServiceListHead
)
1065 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1067 if (CurrentService
->Status
.dwCurrentState
== SERVICE_RUNNING
||
1068 CurrentService
->Status
.dwCurrentState
== SERVICE_START_PENDING
)
1070 /* shutdown service */
1071 ScmControlService(CurrentService
, SERVICE_CONTROL_STOP
, &ServiceStatus
);
1074 ServiceEntry
= ServiceEntry
->Flink
;
1077 DPRINT("ScmGetBootAndSystemDriverState() done\n");