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 record */
176 InsertTailList(&ServiceListHead
,
177 &lpService
->ServiceListEntry
);
179 /* Initialize the service status */
180 lpService
->Status
.dwCurrentState
= SERVICE_STOPPED
;
181 lpService
->Status
.dwControlsAccepted
= 0;
182 lpService
->Status
.dwWin32ExitCode
= ERROR_SERVICE_NEVER_STARTED
;
183 lpService
->Status
.dwServiceSpecificExitCode
= 0;
184 lpService
->Status
.dwCheckPoint
= 0;
185 lpService
->Status
.dwWaitHint
= 2000; /* 2 seconds */
187 return ERROR_SUCCESS
;
192 ScmDeleteServiceRecord(PSERVICE lpService
)
194 DPRINT1("Deleting Service %S\n", lpService
->lpServiceName
);
196 /* Delete the display name */
197 if (lpService
->lpDisplayName
!= NULL
&&
198 lpService
->lpDisplayName
!= lpService
->lpServiceName
)
199 HeapFree(GetProcessHeap(), 0, lpService
->lpDisplayName
);
201 /* Decrement the image reference counter */
202 if (lpService
->lpImage
)
203 lpService
->lpImage
->dwServiceRefCount
--;
205 /* Decrement the group reference counter */
206 if (lpService
->lpGroup
)
207 lpService
->lpGroup
->dwRefCount
--;
209 /* FIXME: SecurityDescriptor */
211 /* Close the control pipe */
212 if (lpService
->ControlPipeHandle
!= INVALID_HANDLE_VALUE
)
213 CloseHandle(lpService
->ControlPipeHandle
);
215 /* Remove the Service from the List */
216 RemoveEntryList(&lpService
->ServiceListEntry
);
218 DPRINT1("Deleted Service %S\n", lpService
->lpServiceName
);
220 /* Delete the service record */
221 HeapFree(GetProcessHeap(), 0, lpService
);
228 CreateServiceListEntry(LPWSTR lpServiceName
,
231 PSERVICE lpService
= NULL
;
232 LPWSTR lpDisplayName
= NULL
;
233 LPWSTR lpGroup
= NULL
;
238 DWORD dwErrorControl
;
241 DPRINT("Service: '%S'\n", lpServiceName
);
242 if (*lpServiceName
== L
'{')
243 return ERROR_SUCCESS
;
245 dwSize
= sizeof(DWORD
);
246 dwError
= RegQueryValueExW(hServiceKey
,
250 (LPBYTE
)&dwServiceType
,
252 if (dwError
!= ERROR_SUCCESS
)
253 return ERROR_SUCCESS
;
255 if (((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_OWN_PROCESS
) &&
256 ((dwServiceType
& ~SERVICE_INTERACTIVE_PROCESS
) != SERVICE_WIN32_SHARE_PROCESS
) &&
257 (dwServiceType
!= SERVICE_KERNEL_DRIVER
) &&
258 (dwServiceType
!= SERVICE_FILE_SYSTEM_DRIVER
))
259 return ERROR_SUCCESS
;
261 DPRINT("Service type: %lx\n", dwServiceType
);
263 dwSize
= sizeof(DWORD
);
264 dwError
= RegQueryValueExW(hServiceKey
,
268 (LPBYTE
)&dwStartType
,
270 if (dwError
!= ERROR_SUCCESS
)
271 return ERROR_SUCCESS
;
273 DPRINT("Start type: %lx\n", dwStartType
);
275 dwSize
= sizeof(DWORD
);
276 dwError
= RegQueryValueExW(hServiceKey
,
280 (LPBYTE
)&dwErrorControl
,
282 if (dwError
!= ERROR_SUCCESS
)
283 return ERROR_SUCCESS
;
285 DPRINT("Error control: %lx\n", dwErrorControl
);
287 dwError
= RegQueryValueExW(hServiceKey
,
293 if (dwError
!= ERROR_SUCCESS
)
296 DPRINT("Tag: %lx\n", dwTagId
);
298 dwError
= ScmReadString(hServiceKey
,
301 if (dwError
!= ERROR_SUCCESS
)
304 DPRINT("Group: %S\n", lpGroup
);
306 dwError
= ScmReadString(hServiceKey
,
309 if (dwError
!= ERROR_SUCCESS
)
310 lpDisplayName
= NULL
;
312 DPRINT("Display name: %S\n", lpDisplayName
);
314 dwError
= ScmCreateNewServiceRecord(lpServiceName
,
316 if (dwError
!= ERROR_SUCCESS
)
319 lpService
->Status
.dwServiceType
= dwServiceType
;
320 lpService
->dwStartType
= dwStartType
;
321 lpService
->dwErrorControl
= dwErrorControl
;
322 lpService
->dwTag
= dwTagId
;
326 dwError
= ScmSetServiceGroup(lpService
, lpGroup
);
327 if (dwError
!= ERROR_SUCCESS
)
331 if (lpDisplayName
!= NULL
)
333 lpService
->lpDisplayName
= lpDisplayName
;
334 lpDisplayName
= NULL
;
337 DPRINT("ServiceName: '%S'\n", lpService
->lpServiceName
);
338 if (lpService
->lpGroup
!= NULL
)
340 DPRINT("Group: '%S'\n", lpService
->lpGroup
->lpGroupName
);
342 DPRINT("Start %lx Type %lx Tag %lx ErrorControl %lx\n",
343 lpService
->dwStartType
,
344 lpService
->Status
.dwServiceType
,
346 lpService
->dwErrorControl
);
348 if (ScmIsDeleteFlagSet(hServiceKey
))
349 lpService
->bDeleted
= TRUE
;
353 HeapFree(GetProcessHeap(), 0, lpGroup
);
355 if (lpDisplayName
!= NULL
)
356 HeapFree(GetProcessHeap(), 0, lpDisplayName
);
363 ScmDeleteRegKey(HKEY hKey
, LPCWSTR lpszSubKey
)
365 DWORD dwRet
, dwMaxSubkeyLen
= 0, dwSize
;
366 WCHAR szNameBuf
[MAX_PATH
], *lpszName
= szNameBuf
;
369 dwRet
= RegOpenKeyExW(hKey
, lpszSubKey
, 0, KEY_READ
, &hSubKey
);
372 /* Find the maximum subkey length so that we can allocate a buffer */
373 dwRet
= RegQueryInfoKeyW(hSubKey
, NULL
, NULL
, NULL
, NULL
,
374 &dwMaxSubkeyLen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
378 if (dwMaxSubkeyLen
> sizeof(szNameBuf
)/sizeof(WCHAR
))
379 /* Name too big: alloc a buffer for it */
380 lpszName
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubkeyLen
*sizeof(WCHAR
));
383 dwRet
= ERROR_NOT_ENOUGH_MEMORY
;
386 while (dwRet
== ERROR_SUCCESS
)
388 dwSize
= dwMaxSubkeyLen
;
389 dwRet
= RegEnumKeyExW(hSubKey
, 0, lpszName
, &dwSize
, NULL
, NULL
, NULL
, NULL
);
390 if (dwRet
== ERROR_SUCCESS
|| dwRet
== ERROR_MORE_DATA
)
391 dwRet
= ScmDeleteRegKey(hSubKey
, lpszName
);
393 if (dwRet
== ERROR_NO_MORE_ITEMS
)
394 dwRet
= ERROR_SUCCESS
;
396 if (lpszName
!= szNameBuf
)
397 HeapFree(GetProcessHeap(), 0, lpszName
); /* Free buffer if allocated */
401 RegCloseKey(hSubKey
);
403 dwRet
= RegDeleteKeyW(hKey
, lpszSubKey
);
410 ScmDeleteMarkedServices(VOID
)
412 PLIST_ENTRY ServiceEntry
;
413 PSERVICE CurrentService
;
417 ServiceEntry
= ServiceListHead
.Flink
;
418 while (ServiceEntry
!= &ServiceListHead
)
420 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
422 ServiceEntry
= ServiceEntry
->Flink
;
424 if (CurrentService
->bDeleted
== TRUE
)
426 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
427 L
"System\\CurrentControlSet\\Services",
431 if (dwError
== ERROR_SUCCESS
)
433 dwError
= ScmDeleteRegKey(hServicesKey
, CurrentService
->lpServiceName
);
434 RegCloseKey(hServicesKey
);
435 if (dwError
== ERROR_SUCCESS
)
437 RemoveEntryList(&CurrentService
->ServiceListEntry
);
438 HeapFree(GetProcessHeap(), 0, CurrentService
);
442 if (dwError
!= ERROR_SUCCESS
)
443 DPRINT1("Delete service failed: %S\n", CurrentService
->lpServiceName
);
450 ScmCreateServiceDatabase(VOID
)
452 WCHAR szSubKey
[MAX_PATH
];
456 DWORD dwSubKeyLength
;
457 FILETIME ftLastChanged
;
460 DPRINT("ScmCreateServiceDatabase() called\n");
462 dwError
= ScmCreateGroupList();
463 if (dwError
!= ERROR_SUCCESS
)
466 /* Initialize basic variables */
467 InitializeListHead(&ServiceListHead
);
469 /* Initialize the database lock */
470 RtlInitializeResource(&DatabaseLock
);
472 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
473 L
"System\\CurrentControlSet\\Services",
477 if (dwError
!= ERROR_SUCCESS
)
483 dwSubKeyLength
= MAX_PATH
;
484 dwError
= RegEnumKeyExW(hServicesKey
,
492 if (dwError
== ERROR_SUCCESS
&&
495 DPRINT("SubKeyName: '%S'\n", szSubKey
);
497 dwError
= RegOpenKeyExW(hServicesKey
,
502 if (dwError
== ERROR_SUCCESS
)
504 dwError
= CreateServiceListEntry(szSubKey
,
507 RegCloseKey(hServiceKey
);
511 if (dwError
!= ERROR_SUCCESS
)
517 RegCloseKey(hServicesKey
);
519 /* Delete services that are marked for delete */
520 ScmDeleteMarkedServices();
522 DPRINT("ScmCreateServiceDatabase() done\n");
524 return ERROR_SUCCESS
;
529 ScmShutdownServiceDatabase(VOID
)
531 DPRINT("ScmShutdownServiceDatabase() called\n");
533 ScmDeleteMarkedServices();
534 RtlDeleteResource(&DatabaseLock
);
536 DPRINT("ScmShutdownServiceDatabase() done\n");
541 ScmCheckDriver(PSERVICE Service
)
543 OBJECT_ATTRIBUTES ObjectAttributes
;
544 UNICODE_STRING DirName
;
547 POBJECT_DIRECTORY_INFORMATION DirInfo
;
552 DPRINT("ScmCheckDriver(%S) called\n", Service
->lpServiceName
);
554 if (Service
->Status
.dwServiceType
== SERVICE_KERNEL_DRIVER
)
556 RtlInitUnicodeString(&DirName
,
561 RtlInitUnicodeString(&DirName
,
565 InitializeObjectAttributes(&ObjectAttributes
,
571 Status
= NtOpenDirectoryObject(&DirHandle
,
572 DIRECTORY_QUERY
| DIRECTORY_TRAVERSE
,
574 if (!NT_SUCCESS(Status
))
579 BufferLength
= sizeof(OBJECT_DIRECTORY_INFORMATION
) +
580 2 * MAX_PATH
* sizeof(WCHAR
);
581 DirInfo
= (OBJECT_DIRECTORY_INFORMATION
*) HeapAlloc(GetProcessHeap(),
588 Status
= NtQueryDirectoryObject(DirHandle
,
595 if (Status
== STATUS_NO_MORE_ENTRIES
)
597 /* FIXME: Add current service to 'failed service' list */
598 DPRINT("Service '%S' failed\n", Service
->lpServiceName
);
602 if (!NT_SUCCESS(Status
))
605 DPRINT("Comparing: '%S' '%wZ'\n", Service
->lpServiceName
, &DirInfo
->Name
);
607 if (_wcsicmp(Service
->lpServiceName
, DirInfo
->Name
.Buffer
) == 0)
609 DPRINT("Found: '%S' '%wZ'\n",
610 Service
->lpServiceName
, &DirInfo
->Name
);
612 /* Mark service as 'running' */
613 Service
->Status
.dwCurrentState
= SERVICE_RUNNING
;
615 /* Mark the service group as 'running' */
616 if (Service
->lpGroup
!= NULL
)
618 Service
->lpGroup
->ServicesRunning
= TRUE
;
625 HeapFree(GetProcessHeap(),
630 return STATUS_SUCCESS
;
635 ScmGetBootAndSystemDriverState(VOID
)
637 PLIST_ENTRY ServiceEntry
;
638 PSERVICE CurrentService
;
640 DPRINT("ScmGetBootAndSystemDriverState() called\n");
642 ServiceEntry
= ServiceListHead
.Flink
;
643 while (ServiceEntry
!= &ServiceListHead
)
645 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
647 if (CurrentService
->dwStartType
== SERVICE_BOOT_START
||
648 CurrentService
->dwStartType
== SERVICE_SYSTEM_START
)
651 DPRINT(" Checking service: %S\n", CurrentService
->lpServiceName
);
653 ScmCheckDriver(CurrentService
);
656 ServiceEntry
= ServiceEntry
->Flink
;
659 DPRINT("ScmGetBootAndSystemDriverState() done\n");
664 ScmControlService(PSERVICE Service
,
666 LPSERVICE_STATUS lpServiceStatus
)
668 PSCM_CONTROL_PACKET ControlPacket
;
672 DPRINT("ScmControlService() called\n");
674 TotalLength
= wcslen(Service
->lpServiceName
) + 1;
676 ControlPacket
= (SCM_CONTROL_PACKET
*)HeapAlloc(GetProcessHeap(),
678 sizeof(SCM_CONTROL_PACKET
) + (TotalLength
* sizeof(WCHAR
)));
679 if (ControlPacket
== NULL
)
680 return ERROR_NOT_ENOUGH_MEMORY
;
682 ControlPacket
->dwControl
= dwControl
;
683 ControlPacket
->hClient
= Service
->hClient
;
684 ControlPacket
->dwSize
= TotalLength
;
685 wcscpy(&ControlPacket
->szArguments
[0], Service
->lpServiceName
);
687 /* Send the start command */
688 WriteFile(Service
->ControlPipeHandle
,
690 sizeof(SCM_CONTROL_PACKET
) + (TotalLength
* sizeof(WCHAR
)),
694 /* FIXME: Read the reply */
696 /* Release the contol packet */
697 HeapFree(GetProcessHeap(),
701 RtlCopyMemory(lpServiceStatus
,
703 sizeof(SERVICE_STATUS
));
705 DPRINT("ScmControlService) done\n");
707 return ERROR_SUCCESS
;
712 ScmSendStartCommand(PSERVICE Service
,
716 PSCM_CONTROL_PACKET ControlPacket
;
718 DWORD ArgsLength
= 0;
723 DPRINT("ScmSendStartCommand() called\n");
725 /* Calculate the total length of the start command line */
726 TotalLength
= wcslen(Service
->lpServiceName
) + 1;
729 for (Count
= 0; Count
< argc
; Count
++)
731 DPRINT("Arg: %S\n", argv
[Count
]);
732 Length
= wcslen(argv
[Count
]) + 1;
733 TotalLength
+= Length
;
734 ArgsLength
+= Length
;
738 DPRINT("ArgsLength: %ld TotalLength: %ld\n", ArgsLength
, TotalLength
);
740 /* Allocate a control packet */
741 ControlPacket
= (SCM_CONTROL_PACKET
*)HeapAlloc(GetProcessHeap(),
743 sizeof(SCM_CONTROL_PACKET
) + (TotalLength
- 1) * sizeof(WCHAR
));
744 if (ControlPacket
== NULL
)
745 return ERROR_NOT_ENOUGH_MEMORY
;
747 ControlPacket
->dwControl
= SERVICE_CONTROL_START
;
748 ControlPacket
->hClient
= Service
->hClient
;
749 ControlPacket
->dwSize
= TotalLength
;
750 Ptr
= &ControlPacket
->szArguments
[0];
751 wcscpy(Ptr
, Service
->lpServiceName
);
752 Ptr
+= (wcslen(Service
->lpServiceName
) + 1);
754 /* Copy argument list */
758 DPRINT1("Arguments sent to service ignored!\n");
760 memcpy(Ptr
, Arguments
, ArgsLength
);
765 /* Terminate the argument list */
768 /* Send the start command */
769 WriteFile(Service
->ControlPipeHandle
,
771 sizeof(SCM_CONTROL_PACKET
) + (TotalLength
- 1) * sizeof(WCHAR
),
775 /* FIXME: Read the reply */
777 /* Release the contol packet */
778 HeapFree(GetProcessHeap(),
782 DPRINT("ScmSendStartCommand() done\n");
784 return ERROR_SUCCESS
;
789 ScmStartUserModeService(PSERVICE Service
,
793 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
794 PROCESS_INFORMATION ProcessInformation
;
795 STARTUPINFOW StartupInfo
;
796 UNICODE_STRING ImagePath
;
798 DWORD ServiceCurrent
= 0;
801 DWORD dwError
= ERROR_SUCCESS
;
802 WCHAR NtControlPipeName
[MAX_PATH
+ 1];
803 HKEY hServiceCurrentKey
= INVALID_HANDLE_VALUE
;
804 DWORD KeyDisposition
;
806 RtlInitUnicodeString(&ImagePath
, NULL
);
808 /* Get service data */
809 RtlZeroMemory(&QueryTable
,
812 QueryTable
[0].Name
= L
"Type";
813 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_QUERY_REGISTRY_REQUIRED
;
814 QueryTable
[0].EntryContext
= &Type
;
816 QueryTable
[1].Name
= L
"ImagePath";
817 QueryTable
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_QUERY_REGISTRY_REQUIRED
;
818 QueryTable
[1].EntryContext
= &ImagePath
;
820 Status
= RtlQueryRegistryValues(RTL_REGISTRY_SERVICES
,
821 Service
->lpServiceName
,
825 if (!NT_SUCCESS(Status
))
827 DPRINT1("RtlQueryRegistryValues() failed (Status %lx)\n", Status
);
828 return RtlNtStatusToDosError(Status
);
830 DPRINT("ImagePath: '%S'\n", ImagePath
.Buffer
);
831 DPRINT("Type: %lx\n", Type
);
833 /* Get the service number */
834 /* TODO: Create registry entry with correct write access */
835 Status
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
836 L
"SYSTEM\\CurrentControlSet\\Control\\ServiceCurrent", 0, NULL
,
838 KEY_WRITE
| KEY_READ
,
843 if (ERROR_SUCCESS
!= Status
)
845 DPRINT1("RegCreateKeyEx() failed with status %u\n", Status
);
849 if (REG_OPENED_EXISTING_KEY
== KeyDisposition
)
851 DWORD KeySize
= sizeof(ServiceCurrent
);
852 Status
= RegQueryValueExW(hServiceCurrentKey
, L
"", 0, NULL
, (BYTE
*)&ServiceCurrent
, &KeySize
);
854 if (ERROR_SUCCESS
!= Status
)
856 RegCloseKey(hServiceCurrentKey
);
857 DPRINT1("RegQueryValueEx() failed with status %u\n", Status
);
864 Status
= RegSetValueExW(hServiceCurrentKey
, L
"", 0, REG_DWORD
, (BYTE
*)&ServiceCurrent
, sizeof(ServiceCurrent
));
866 RegCloseKey(hServiceCurrentKey
);
868 if (ERROR_SUCCESS
!= Status
)
870 DPRINT1("RegSetValueExW() failed (Status %lx)\n", Status
);
874 /* Create '\\.\pipe\net\NtControlPipeXXX' instance */
875 swprintf(NtControlPipeName
, L
"\\\\.\\pipe\\net\\NtControlPipe%u", ServiceCurrent
);
876 Service
->ControlPipeHandle
= CreateNamedPipeW(NtControlPipeName
,
878 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
884 DPRINT("CreateNamedPipeW(%S) done\n", NtControlPipeName
);
885 if (Service
->ControlPipeHandle
== INVALID_HANDLE_VALUE
)
887 DPRINT1("Failed to create control pipe!\n");
888 return GetLastError();
891 StartupInfo
.cb
= sizeof(StartupInfo
);
892 StartupInfo
.lpReserved
= NULL
;
893 StartupInfo
.lpDesktop
= NULL
;
894 StartupInfo
.lpTitle
= NULL
;
895 StartupInfo
.dwFlags
= 0;
896 StartupInfo
.cbReserved2
= 0;
897 StartupInfo
.lpReserved2
= 0;
899 Result
= CreateProcessW(NULL
,
904 DETACHED_PROCESS
| CREATE_SUSPENDED
,
908 &ProcessInformation
);
909 RtlFreeUnicodeString(&ImagePath
);
913 dwError
= GetLastError();
914 /* Close control pipe */
915 CloseHandle(Service
->ControlPipeHandle
);
916 Service
->ControlPipeHandle
= INVALID_HANDLE_VALUE
;
918 DPRINT1("Starting '%S' failed!\n", Service
->lpServiceName
);
922 DPRINT("Process Id: %lu Handle %lx\n",
923 ProcessInformation
.dwProcessId
,
924 ProcessInformation
.hProcess
);
925 DPRINT("Thread Id: %lu Handle %lx\n",
926 ProcessInformation
.dwThreadId
,
927 ProcessInformation
.hThread
);
929 /* Get process and thread ids */
930 Service
->ProcessId
= ProcessInformation
.dwProcessId
;
931 Service
->ThreadId
= ProcessInformation
.dwThreadId
;
934 ResumeThread(ProcessInformation
.hThread
);
936 /* Connect control pipe */
937 if (ConnectNamedPipe(Service
->ControlPipeHandle
, NULL
) ?
938 TRUE
: (dwError
= GetLastError()) == ERROR_PIPE_CONNECTED
)
942 DPRINT("Control pipe connected!\n");
944 /* Read SERVICE_STATUS_HANDLE from pipe */
945 if (!ReadFile(Service
->ControlPipeHandle
,
946 (LPVOID
)&Service
->hClient
,
951 dwError
= GetLastError();
952 DPRINT1("Reading the service control pipe failed (Error %lu)\n",
957 DPRINT("Received service status %lu\n", Service
->hClient
);
959 /* Send start command */
960 dwError
= ScmSendStartCommand(Service
, argc
, argv
);
965 DPRINT1("Connecting control pipe failed! (Error %lu)\n", dwError
);
967 /* Close control pipe */
968 CloseHandle(Service
->ControlPipeHandle
);
969 Service
->ControlPipeHandle
= INVALID_HANDLE_VALUE
;
970 Service
->ProcessId
= 0;
971 Service
->ThreadId
= 0;
974 /* Close process and thread handle */
975 CloseHandle(ProcessInformation
.hThread
);
976 CloseHandle(ProcessInformation
.hProcess
);
983 ScmStartService(PSERVICE Service
, DWORD argc
, LPWSTR
*argv
)
985 PSERVICE_GROUP Group
= Service
->lpGroup
;
986 DWORD dwError
= ERROR_SUCCESS
;
988 DPRINT("ScmStartService() called\n");
990 Service
->ControlPipeHandle
= INVALID_HANDLE_VALUE
;
991 DPRINT("Service->Type: %lu\n", Service
->Status
.dwServiceType
);
993 if (Service
->Status
.dwServiceType
& SERVICE_DRIVER
)
996 dwError
= ScmLoadDriver(Service
);
997 if (dwError
== ERROR_SUCCESS
)
998 Service
->Status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
;
1002 /* Start user-mode service */
1003 dwError
= ScmStartUserModeService(Service
, argc
, argv
);
1006 DPRINT("ScmStartService() done (Error %lu)\n", dwError
);
1008 if (dwError
== ERROR_SUCCESS
)
1012 Group
->ServicesRunning
= TRUE
;
1014 Service
->Status
.dwCurrentState
= SERVICE_RUNNING
;
1019 switch (Service
->ErrorControl
)
1021 case SERVICE_ERROR_NORMAL
:
1022 /* FIXME: Log error */
1025 case SERVICE_ERROR_SEVERE
:
1026 if (IsLastKnownGood
== FALSE
)
1028 /* FIXME: Boot last known good configuration */
1032 case SERVICE_ERROR_CRITICAL
:
1033 if (IsLastKnownGood
== FALSE
)
1035 /* FIXME: Boot last known good configuration */
1051 ScmAutoStartServices(VOID
)
1053 PLIST_ENTRY GroupEntry
;
1054 PLIST_ENTRY ServiceEntry
;
1055 PSERVICE_GROUP CurrentGroup
;
1056 PSERVICE CurrentService
;
1059 /* Clear 'ServiceVisited' flag */
1060 ServiceEntry
= ServiceListHead
.Flink
;
1061 while (ServiceEntry
!= &ServiceListHead
)
1063 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1064 CurrentService
->ServiceVisited
= FALSE
;
1065 ServiceEntry
= ServiceEntry
->Flink
;
1068 /* Start all services which are members of an existing group */
1069 GroupEntry
= GroupListHead
.Flink
;
1070 while (GroupEntry
!= &GroupListHead
)
1072 CurrentGroup
= CONTAINING_RECORD(GroupEntry
, SERVICE_GROUP
, GroupListEntry
);
1074 DPRINT("Group '%S'\n", CurrentGroup
->lpGroupName
);
1076 /* Start all services witch have a valid tag */
1077 for (i
= 0; i
< CurrentGroup
->TagCount
; i
++)
1079 ServiceEntry
= ServiceListHead
.Flink
;
1080 while (ServiceEntry
!= &ServiceListHead
)
1082 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1084 if ((CurrentService
->lpGroup
== CurrentGroup
) &&
1085 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1086 (CurrentService
->ServiceVisited
== FALSE
) &&
1087 (CurrentService
->dwTag
== CurrentGroup
->TagArray
[i
]))
1089 CurrentService
->ServiceVisited
= TRUE
;
1090 ScmStartService(CurrentService
, 0, NULL
);
1093 ServiceEntry
= ServiceEntry
->Flink
;
1097 /* Start all services which have an invalid tag or which do not have a tag */
1098 ServiceEntry
= ServiceListHead
.Flink
;
1099 while (ServiceEntry
!= &ServiceListHead
)
1101 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1103 if ((CurrentService
->lpGroup
== CurrentGroup
) &&
1104 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1105 (CurrentService
->ServiceVisited
== FALSE
))
1107 CurrentService
->ServiceVisited
= TRUE
;
1108 ScmStartService(CurrentService
, 0, NULL
);
1111 ServiceEntry
= ServiceEntry
->Flink
;
1114 GroupEntry
= GroupEntry
->Flink
;
1117 /* Start all services which are members of any non-existing group */
1118 ServiceEntry
= ServiceListHead
.Flink
;
1119 while (ServiceEntry
!= &ServiceListHead
)
1121 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1123 if ((CurrentService
->lpGroup
!= NULL
) &&
1124 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1125 (CurrentService
->ServiceVisited
== FALSE
))
1127 CurrentService
->ServiceVisited
= TRUE
;
1128 ScmStartService(CurrentService
, 0, NULL
);
1131 ServiceEntry
= ServiceEntry
->Flink
;
1134 /* Start all services which are not a member of any group */
1135 ServiceEntry
= ServiceListHead
.Flink
;
1136 while (ServiceEntry
!= &ServiceListHead
)
1138 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1140 if ((CurrentService
->lpGroup
== NULL
) &&
1141 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1142 (CurrentService
->ServiceVisited
== FALSE
))
1144 CurrentService
->ServiceVisited
= TRUE
;
1145 ScmStartService(CurrentService
, 0, NULL
);
1148 ServiceEntry
= ServiceEntry
->Flink
;
1151 /* Clear 'ServiceVisited' flag again */
1152 ServiceEntry
= ServiceListHead
.Flink
;
1153 while (ServiceEntry
!= &ServiceListHead
)
1155 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1156 CurrentService
->ServiceVisited
= FALSE
;
1157 ServiceEntry
= ServiceEntry
->Flink
;
1163 ScmAutoShutdownServices(VOID
)
1165 PLIST_ENTRY ServiceEntry
;
1166 PSERVICE CurrentService
;
1167 SERVICE_STATUS ServiceStatus
;
1169 DPRINT("ScmAutoShutdownServices() called\n");
1171 ServiceEntry
= ServiceListHead
.Flink
;
1172 while (ServiceEntry
!= &ServiceListHead
)
1174 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1176 if (CurrentService
->Status
.dwCurrentState
== SERVICE_RUNNING
||
1177 CurrentService
->Status
.dwCurrentState
== SERVICE_START_PENDING
)
1179 /* shutdown service */
1180 ScmControlService(CurrentService
, SERVICE_CONTROL_STOP
, &ServiceStatus
);
1183 ServiceEntry
= ServiceEntry
->Flink
;
1186 DPRINT("ScmGetBootAndSystemDriverState() done\n");