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 ScmDeleteRegKey(HKEY hKey
, LPCWSTR lpszSubKey
)
328 DWORD dwRet
, dwMaxSubkeyLen
= 0, dwSize
;
329 WCHAR szNameBuf
[MAX_PATH
], *lpszName
= szNameBuf
;
332 dwRet
= RegOpenKeyExW(hKey
, lpszSubKey
, 0, KEY_READ
, &hSubKey
);
335 /* Find the maximum subkey length so that we can allocate a buffer */
336 dwRet
= RegQueryInfoKeyW(hSubKey
, NULL
, NULL
, NULL
, NULL
,
337 &dwMaxSubkeyLen
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
);
341 if (dwMaxSubkeyLen
> sizeof(szNameBuf
)/sizeof(WCHAR
))
342 /* Name too big: alloc a buffer for it */
343 lpszName
= HeapAlloc(GetProcessHeap(), 0, dwMaxSubkeyLen
*sizeof(WCHAR
));
346 dwRet
= ERROR_NOT_ENOUGH_MEMORY
;
349 while (dwRet
== ERROR_SUCCESS
)
351 dwSize
= dwMaxSubkeyLen
;
352 dwRet
= RegEnumKeyExW(hSubKey
, 0, lpszName
, &dwSize
, NULL
, NULL
, NULL
, NULL
);
353 if (dwRet
== ERROR_SUCCESS
|| dwRet
== ERROR_MORE_DATA
)
354 dwRet
= ScmDeleteRegKey(hSubKey
, lpszName
);
356 if (dwRet
== ERROR_NO_MORE_ITEMS
)
357 dwRet
= ERROR_SUCCESS
;
359 if (lpszName
!= szNameBuf
)
360 HeapFree(GetProcessHeap(), 0, lpszName
); /* Free buffer if allocated */
364 RegCloseKey(hSubKey
);
366 dwRet
= RegDeleteKeyW(hKey
, lpszSubKey
);
373 ScmDeleteMarkedServices(VOID
)
375 PLIST_ENTRY ServiceEntry
;
376 PSERVICE CurrentService
;
380 ServiceEntry
= ServiceListHead
.Flink
;
381 while (ServiceEntry
!= &ServiceListHead
)
383 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
385 ServiceEntry
= ServiceEntry
->Flink
;
387 if (CurrentService
->bDeleted
== TRUE
)
389 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
390 L
"System\\CurrentControlSet\\Services",
394 if (dwError
== ERROR_SUCCESS
)
396 dwError
= ScmDeleteRegKey(hServicesKey
, CurrentService
->lpServiceName
);
397 RegCloseKey(hServicesKey
);
398 if (dwError
== ERROR_SUCCESS
)
400 RemoveEntryList(&CurrentService
->ServiceListEntry
);
401 HeapFree(GetProcessHeap(), 0, CurrentService
);
405 if (dwError
!= ERROR_SUCCESS
)
406 DPRINT1("Delete service failed: %S\n", CurrentService
->lpServiceName
);
413 ScmCreateServiceDatabase(VOID
)
415 WCHAR szSubKey
[MAX_PATH
];
419 DWORD dwSubKeyLength
;
420 FILETIME ftLastChanged
;
423 DPRINT("ScmCreateServiceDatabase() called\n");
425 dwError
= ScmCreateGroupList();
426 if (dwError
!= ERROR_SUCCESS
)
429 /* Initialize basic variables */
430 InitializeListHead(&ServiceListHead
);
432 /* Initialize the database lock */
433 RtlInitializeResource(&DatabaseLock
);
435 dwError
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
,
436 L
"System\\CurrentControlSet\\Services",
440 if (dwError
!= ERROR_SUCCESS
)
446 dwSubKeyLength
= MAX_PATH
;
447 dwError
= RegEnumKeyExW(hServicesKey
,
455 if (dwError
== ERROR_SUCCESS
&&
458 DPRINT("SubKeyName: '%S'\n", szSubKey
);
460 dwError
= RegOpenKeyExW(hServicesKey
,
465 if (dwError
== ERROR_SUCCESS
)
467 dwError
= CreateServiceListEntry(szSubKey
,
470 RegCloseKey(hServiceKey
);
474 if (dwError
!= ERROR_SUCCESS
)
480 RegCloseKey(hServicesKey
);
482 /* Delete services that are marked for delete */
483 ScmDeleteMarkedServices();
485 DPRINT("ScmCreateServiceDatabase() done\n");
487 return ERROR_SUCCESS
;
492 ScmShutdownServiceDatabase(VOID
)
494 DPRINT("ScmShutdownServiceDatabase() called\n");
496 ScmDeleteMarkedServices();
497 RtlDeleteResource(&DatabaseLock
);
499 DPRINT("ScmShutdownServiceDatabase() done\n");
504 ScmCheckDriver(PSERVICE Service
)
506 OBJECT_ATTRIBUTES ObjectAttributes
;
507 UNICODE_STRING DirName
;
510 POBJECT_DIRECTORY_INFORMATION DirInfo
;
515 DPRINT("ScmCheckDriver(%S) called\n", Service
->lpServiceName
);
517 if (Service
->Status
.dwServiceType
== SERVICE_KERNEL_DRIVER
)
519 RtlInitUnicodeString(&DirName
,
524 RtlInitUnicodeString(&DirName
,
528 InitializeObjectAttributes(&ObjectAttributes
,
534 Status
= NtOpenDirectoryObject(&DirHandle
,
535 DIRECTORY_QUERY
| DIRECTORY_TRAVERSE
,
537 if (!NT_SUCCESS(Status
))
542 BufferLength
= sizeof(OBJECT_DIRECTORY_INFORMATION
) +
543 2 * MAX_PATH
* sizeof(WCHAR
);
544 DirInfo
= (OBJECT_DIRECTORY_INFORMATION
*) HeapAlloc(GetProcessHeap(),
551 Status
= NtQueryDirectoryObject(DirHandle
,
558 if (Status
== STATUS_NO_MORE_ENTRIES
)
560 /* FIXME: Add current service to 'failed service' list */
561 DPRINT("Service '%S' failed\n", Service
->lpServiceName
);
565 if (!NT_SUCCESS(Status
))
568 DPRINT("Comparing: '%S' '%wZ'\n", Service
->lpServiceName
, &DirInfo
->Name
);
570 if (_wcsicmp(Service
->lpServiceName
, DirInfo
->Name
.Buffer
) == 0)
572 DPRINT("Found: '%S' '%wZ'\n",
573 Service
->lpServiceName
, &DirInfo
->Name
);
575 /* Mark service as 'running' */
576 Service
->Status
.dwCurrentState
= SERVICE_RUNNING
;
578 /* Mark the service group as 'running' */
579 if (Service
->lpGroup
!= NULL
)
581 Service
->lpGroup
->ServicesRunning
= TRUE
;
588 HeapFree(GetProcessHeap(),
593 return STATUS_SUCCESS
;
598 ScmGetBootAndSystemDriverState(VOID
)
600 PLIST_ENTRY ServiceEntry
;
601 PSERVICE CurrentService
;
603 DPRINT("ScmGetBootAndSystemDriverState() called\n");
605 ServiceEntry
= ServiceListHead
.Flink
;
606 while (ServiceEntry
!= &ServiceListHead
)
608 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
610 if (CurrentService
->dwStartType
== SERVICE_BOOT_START
||
611 CurrentService
->dwStartType
== SERVICE_SYSTEM_START
)
614 DPRINT(" Checking service: %S\n", CurrentService
->lpServiceName
);
616 ScmCheckDriver(CurrentService
);
619 ServiceEntry
= ServiceEntry
->Flink
;
622 DPRINT("ScmGetBootAndSystemDriverState() done\n");
627 ScmControlService(PSERVICE Service
,
629 LPSERVICE_STATUS lpServiceStatus
)
631 PSCM_CONTROL_PACKET ControlPacket
;
635 DPRINT("ScmControlService() called\n");
637 TotalLength
= wcslen(Service
->lpServiceName
) + 1;
639 ControlPacket
= (SCM_CONTROL_PACKET
*)HeapAlloc(GetProcessHeap(),
641 sizeof(SCM_CONTROL_PACKET
) + (TotalLength
* sizeof(WCHAR
)));
642 if (ControlPacket
== NULL
)
643 return ERROR_NOT_ENOUGH_MEMORY
;
645 ControlPacket
->dwControl
= dwControl
;
646 ControlPacket
->hClient
= Service
->hClient
;
647 ControlPacket
->dwSize
= TotalLength
;
648 wcscpy(&ControlPacket
->szArguments
[0], Service
->lpServiceName
);
650 /* Send the start command */
651 WriteFile(Service
->ControlPipeHandle
,
653 sizeof(SCM_CONTROL_PACKET
) + (TotalLength
* sizeof(WCHAR
)),
657 /* FIXME: Read the reply */
659 /* Release the contol packet */
660 HeapFree(GetProcessHeap(),
664 RtlCopyMemory(lpServiceStatus
,
666 sizeof(SERVICE_STATUS
));
668 DPRINT("ScmControlService) done\n");
670 return ERROR_SUCCESS
;
675 ScmSendStartCommand(PSERVICE Service
,
679 PSCM_CONTROL_PACKET ControlPacket
;
681 DWORD ArgsLength
= 0;
686 DPRINT("ScmSendStartCommand() called\n");
688 /* Calculate the total length of the start command line */
689 TotalLength
= wcslen(Service
->lpServiceName
) + 1;
692 for (Count
= 0; Count
< argc
; Count
++)
694 DPRINT("Arg: %S\n", argv
[Count
]);
695 Length
= wcslen(argv
[Count
]) + 1;
696 TotalLength
+= Length
;
697 ArgsLength
+= Length
;
701 DPRINT("ArgsLength: %ld TotalLength: %ld\n", ArgsLength
, TotalLength
);
703 /* Allocate a control packet */
704 ControlPacket
= (SCM_CONTROL_PACKET
*)HeapAlloc(GetProcessHeap(),
706 sizeof(SCM_CONTROL_PACKET
) + (TotalLength
- 1) * sizeof(WCHAR
));
707 if (ControlPacket
== NULL
)
708 return ERROR_NOT_ENOUGH_MEMORY
;
710 ControlPacket
->dwControl
= SERVICE_CONTROL_START
;
711 ControlPacket
->hClient
= Service
->hClient
;
712 ControlPacket
->dwSize
= TotalLength
;
713 Ptr
= &ControlPacket
->szArguments
[0];
714 wcscpy(Ptr
, Service
->lpServiceName
);
715 Ptr
+= (wcslen(Service
->lpServiceName
) + 1);
717 /* Copy argument list */
721 DPRINT1("Arguments sent to service ignored!\n");
723 memcpy(Ptr
, Arguments
, ArgsLength
);
728 /* Terminate the argument list */
731 /* Send the start command */
732 WriteFile(Service
->ControlPipeHandle
,
734 sizeof(SCM_CONTROL_PACKET
) + (TotalLength
- 1) * sizeof(WCHAR
),
738 /* FIXME: Read the reply */
740 /* Release the contol packet */
741 HeapFree(GetProcessHeap(),
745 DPRINT("ScmSendStartCommand() done\n");
747 return ERROR_SUCCESS
;
752 ScmStartUserModeService(PSERVICE Service
,
756 RTL_QUERY_REGISTRY_TABLE QueryTable
[3];
757 PROCESS_INFORMATION ProcessInformation
;
758 STARTUPINFOW StartupInfo
;
759 UNICODE_STRING ImagePath
;
761 DWORD ServiceCurrent
= 0;
764 DWORD dwError
= ERROR_SUCCESS
;
765 WCHAR NtControlPipeName
[MAX_PATH
+ 1];
766 HKEY hServiceCurrentKey
= INVALID_HANDLE_VALUE
;
767 DWORD KeyDisposition
;
769 RtlInitUnicodeString(&ImagePath
, NULL
);
771 /* Get service data */
772 RtlZeroMemory(&QueryTable
,
775 QueryTable
[0].Name
= L
"Type";
776 QueryTable
[0].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_QUERY_REGISTRY_REQUIRED
;
777 QueryTable
[0].EntryContext
= &Type
;
779 QueryTable
[1].Name
= L
"ImagePath";
780 QueryTable
[1].Flags
= RTL_QUERY_REGISTRY_DIRECT
| RTL_QUERY_REGISTRY_REQUIRED
;
781 QueryTable
[1].EntryContext
= &ImagePath
;
783 Status
= RtlQueryRegistryValues(RTL_REGISTRY_SERVICES
,
784 Service
->lpServiceName
,
788 if (!NT_SUCCESS(Status
))
790 DPRINT1("RtlQueryRegistryValues() failed (Status %lx)\n", Status
);
791 return RtlNtStatusToDosError(Status
);
793 DPRINT("ImagePath: '%S'\n", ImagePath
.Buffer
);
794 DPRINT("Type: %lx\n", Type
);
796 /* Get the service number */
797 /* TODO: Create registry entry with correct write access */
798 Status
= RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
799 L
"SYSTEM\\CurrentControlSet\\Control\\ServiceCurrent", 0, NULL
,
801 KEY_WRITE
| KEY_READ
,
806 if (ERROR_SUCCESS
!= Status
)
808 DPRINT1("RegCreateKeyEx() failed with status %u\n", Status
);
812 if (REG_OPENED_EXISTING_KEY
== KeyDisposition
)
814 DWORD KeySize
= sizeof(ServiceCurrent
);
815 Status
= RegQueryValueExW(hServiceCurrentKey
, L
"", 0, NULL
, (BYTE
*)&ServiceCurrent
, &KeySize
);
817 if (ERROR_SUCCESS
!= Status
)
819 RegCloseKey(hServiceCurrentKey
);
820 DPRINT1("RegQueryValueEx() failed with status %u\n", Status
);
827 Status
= RegSetValueExW(hServiceCurrentKey
, L
"", 0, REG_DWORD
, (BYTE
*)&ServiceCurrent
, sizeof(ServiceCurrent
));
829 RegCloseKey(hServiceCurrentKey
);
831 if (ERROR_SUCCESS
!= Status
)
833 DPRINT1("RegSetValueExW() failed (Status %lx)\n", Status
);
837 /* Create '\\.\pipe\net\NtControlPipeXXX' instance */
838 swprintf(NtControlPipeName
, L
"\\\\.\\pipe\\net\\NtControlPipe%u", ServiceCurrent
);
839 Service
->ControlPipeHandle
= CreateNamedPipeW(NtControlPipeName
,
841 PIPE_TYPE_MESSAGE
| PIPE_READMODE_MESSAGE
| PIPE_WAIT
,
847 DPRINT("CreateNamedPipeW(%S) done\n", NtControlPipeName
);
848 if (Service
->ControlPipeHandle
== INVALID_HANDLE_VALUE
)
850 DPRINT1("Failed to create control pipe!\n");
851 return GetLastError();
854 StartupInfo
.cb
= sizeof(StartupInfo
);
855 StartupInfo
.lpReserved
= NULL
;
856 StartupInfo
.lpDesktop
= NULL
;
857 StartupInfo
.lpTitle
= NULL
;
858 StartupInfo
.dwFlags
= 0;
859 StartupInfo
.cbReserved2
= 0;
860 StartupInfo
.lpReserved2
= 0;
862 Result
= CreateProcessW(ImagePath
.Buffer
,
867 DETACHED_PROCESS
| CREATE_SUSPENDED
,
871 &ProcessInformation
);
872 RtlFreeUnicodeString(&ImagePath
);
876 dwError
= GetLastError();
877 /* Close control pipe */
878 CloseHandle(Service
->ControlPipeHandle
);
879 Service
->ControlPipeHandle
= INVALID_HANDLE_VALUE
;
881 DPRINT1("Starting '%S' failed!\n", Service
->lpServiceName
);
885 DPRINT("Process Id: %lu Handle %lx\n",
886 ProcessInformation
.dwProcessId
,
887 ProcessInformation
.hProcess
);
888 DPRINT("Thread Id: %lu Handle %lx\n",
889 ProcessInformation
.dwThreadId
,
890 ProcessInformation
.hThread
);
892 /* Get process and thread ids */
893 Service
->ProcessId
= ProcessInformation
.dwProcessId
;
894 Service
->ThreadId
= ProcessInformation
.dwThreadId
;
897 ResumeThread(ProcessInformation
.hThread
);
899 /* Connect control pipe */
900 if (ConnectNamedPipe(Service
->ControlPipeHandle
, NULL
) ?
901 TRUE
: (dwError
= GetLastError()) == ERROR_PIPE_CONNECTED
)
905 DPRINT("Control pipe connected!\n");
907 /* Read SERVICE_STATUS_HANDLE from pipe */
908 if (!ReadFile(Service
->ControlPipeHandle
,
909 (LPVOID
)&Service
->hClient
,
914 dwError
= GetLastError();
915 DPRINT1("Reading the service control pipe failed (Error %lu)\n",
920 DPRINT("Received service status %lu\n", Service
->hClient
);
922 /* Send start command */
923 dwError
= ScmSendStartCommand(Service
, argc
, argv
);
928 DPRINT1("Connecting control pipe failed! (Error %lu)\n", dwError
);
930 /* Close control pipe */
931 CloseHandle(Service
->ControlPipeHandle
);
932 Service
->ControlPipeHandle
= INVALID_HANDLE_VALUE
;
933 Service
->ProcessId
= 0;
934 Service
->ThreadId
= 0;
937 /* Close process and thread handle */
938 CloseHandle(ProcessInformation
.hThread
);
939 CloseHandle(ProcessInformation
.hProcess
);
946 ScmStartService(PSERVICE Service
, DWORD argc
, LPWSTR
*argv
)
948 PSERVICE_GROUP Group
= Service
->lpGroup
;
949 DWORD dwError
= ERROR_SUCCESS
;
951 DPRINT("ScmStartService() called\n");
953 Service
->ControlPipeHandle
= INVALID_HANDLE_VALUE
;
954 DPRINT("Service->Type: %lu\n", Service
->Status
.dwServiceType
);
956 if (Service
->Status
.dwServiceType
& SERVICE_DRIVER
)
959 dwError
= ScmLoadDriver(Service
);
960 if (dwError
== ERROR_SUCCESS
)
961 Service
->Status
.dwControlsAccepted
= SERVICE_ACCEPT_STOP
;
965 /* Start user-mode service */
966 dwError
= ScmStartUserModeService(Service
, argc
, argv
);
969 DPRINT("ScmStartService() done (Error %lu)\n", dwError
);
971 if (dwError
== ERROR_SUCCESS
)
975 Group
->ServicesRunning
= TRUE
;
977 Service
->Status
.dwCurrentState
= SERVICE_RUNNING
;
982 switch (Service
->ErrorControl
)
984 case SERVICE_ERROR_NORMAL
:
985 /* FIXME: Log error */
988 case SERVICE_ERROR_SEVERE
:
989 if (IsLastKnownGood
== FALSE
)
991 /* FIXME: Boot last known good configuration */
995 case SERVICE_ERROR_CRITICAL
:
996 if (IsLastKnownGood
== FALSE
)
998 /* FIXME: Boot last known good configuration */
1014 ScmAutoStartServices(VOID
)
1016 PLIST_ENTRY GroupEntry
;
1017 PLIST_ENTRY ServiceEntry
;
1018 PSERVICE_GROUP CurrentGroup
;
1019 PSERVICE CurrentService
;
1022 /* Clear 'ServiceVisited' flag */
1023 ServiceEntry
= ServiceListHead
.Flink
;
1024 while (ServiceEntry
!= &ServiceListHead
)
1026 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1027 CurrentService
->ServiceVisited
= FALSE
;
1028 ServiceEntry
= ServiceEntry
->Flink
;
1031 /* Start all services which are members of an existing group */
1032 GroupEntry
= GroupListHead
.Flink
;
1033 while (GroupEntry
!= &GroupListHead
)
1035 CurrentGroup
= CONTAINING_RECORD(GroupEntry
, SERVICE_GROUP
, GroupListEntry
);
1037 DPRINT("Group '%S'\n", CurrentGroup
->lpGroupName
);
1039 /* Start all services witch have a valid tag */
1040 for (i
= 0; i
< CurrentGroup
->TagCount
; i
++)
1042 ServiceEntry
= ServiceListHead
.Flink
;
1043 while (ServiceEntry
!= &ServiceListHead
)
1045 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1047 if ((CurrentService
->lpGroup
== CurrentGroup
) &&
1048 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1049 (CurrentService
->ServiceVisited
== FALSE
) &&
1050 (CurrentService
->dwTag
== CurrentGroup
->TagArray
[i
]))
1052 CurrentService
->ServiceVisited
= TRUE
;
1053 ScmStartService(CurrentService
, 0, NULL
);
1056 ServiceEntry
= ServiceEntry
->Flink
;
1060 /* Start all services which have an invalid tag or which do not have a tag */
1061 ServiceEntry
= ServiceListHead
.Flink
;
1062 while (ServiceEntry
!= &ServiceListHead
)
1064 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1066 if ((CurrentService
->lpGroup
== CurrentGroup
) &&
1067 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1068 (CurrentService
->ServiceVisited
== FALSE
))
1070 CurrentService
->ServiceVisited
= TRUE
;
1071 ScmStartService(CurrentService
, 0, NULL
);
1074 ServiceEntry
= ServiceEntry
->Flink
;
1077 GroupEntry
= GroupEntry
->Flink
;
1080 /* Start all services which are members of any non-existing group */
1081 ServiceEntry
= ServiceListHead
.Flink
;
1082 while (ServiceEntry
!= &ServiceListHead
)
1084 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1086 if ((CurrentService
->lpGroup
!= NULL
) &&
1087 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1088 (CurrentService
->ServiceVisited
== FALSE
))
1090 CurrentService
->ServiceVisited
= TRUE
;
1091 ScmStartService(CurrentService
, 0, NULL
);
1094 ServiceEntry
= ServiceEntry
->Flink
;
1097 /* Start all services which are not a member of any group */
1098 ServiceEntry
= ServiceListHead
.Flink
;
1099 while (ServiceEntry
!= &ServiceListHead
)
1101 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1103 if ((CurrentService
->lpGroup
== NULL
) &&
1104 (CurrentService
->dwStartType
== SERVICE_AUTO_START
) &&
1105 (CurrentService
->ServiceVisited
== FALSE
))
1107 CurrentService
->ServiceVisited
= TRUE
;
1108 ScmStartService(CurrentService
, 0, NULL
);
1111 ServiceEntry
= ServiceEntry
->Flink
;
1114 /* Clear 'ServiceVisited' flag again */
1115 ServiceEntry
= ServiceListHead
.Flink
;
1116 while (ServiceEntry
!= &ServiceListHead
)
1118 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1119 CurrentService
->ServiceVisited
= FALSE
;
1120 ServiceEntry
= ServiceEntry
->Flink
;
1126 ScmAutoShutdownServices(VOID
)
1128 PLIST_ENTRY ServiceEntry
;
1129 PSERVICE CurrentService
;
1130 SERVICE_STATUS ServiceStatus
;
1132 DPRINT("ScmAutoShutdownServices() called\n");
1134 ServiceEntry
= ServiceListHead
.Flink
;
1135 while (ServiceEntry
!= &ServiceListHead
)
1137 CurrentService
= CONTAINING_RECORD(ServiceEntry
, SERVICE
, ServiceListEntry
);
1139 if (CurrentService
->Status
.dwCurrentState
== SERVICE_RUNNING
||
1140 CurrentService
->Status
.dwCurrentState
== SERVICE_START_PENDING
)
1142 /* shutdown service */
1143 ScmControlService(CurrentService
, SERVICE_CONTROL_STOP
, &ServiceStatus
);
1146 ServiceEntry
= ServiceEntry
->Flink
;
1149 DPRINT("ScmGetBootAndSystemDriverState() done\n");