ReactOS  0.4.14-dev-52-g6116262
services.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Service Control Manager
3  * LICENSE: GPL - See COPYING in the top level directory
4  * FILE: base/system/services/services.c
5  * PURPOSE: Main SCM controller
6  * COPYRIGHT: Copyright 2001-2005 Eric Kohl
7  * Copyright 2007 Ged Murphy <gedmurphy@reactos.org>
8  *
9  */
10 
11 /* INCLUDES *****************************************************************/
12 
13 #include "services.h"
14 
15 #include <wincon.h>
16 
17 #define NDEBUG
18 #include <debug.h>
19 
21 
22 /* GLOBALS ******************************************************************/
23 
24 /* Defined in include/reactos/services/services.h */
25 // #define SCM_START_EVENT L"SvcctrlStartEvent_A3752DX"
26 #define SCM_AUTOSTARTCOMPLETE_EVENT L"SC_AutoStartComplete"
27 
32 
33 
34 /* FUNCTIONS *****************************************************************/
35 
36 VOID
38 {
39 #if DBG
40  CHAR buffer[512];
41  va_list ap;
42 
43  va_start(ap, fmt);
44  vsprintf(buffer, fmt, ap);
45  va_end(ap);
46 
48 #endif
49 }
50 
51 
52 DWORD
54 {
55  DWORD dwError;
56 
58  return ERROR_SUCCESS;
59 
60  /* Create or open the SECURITY_SERVICES_STARTED event */
62  TRUE,
63  FALSE,
64  L"SECURITY_SERVICES_STARTED");
66  {
67  dwError = GetLastError();
68  if (dwError != ERROR_ALREADY_EXISTS)
69  return dwError;
70 
72  FALSE,
73  L"SECURITY_SERVICES_STARTED");
75  return GetLastError();
76  }
77 
79 
80  return ERROR_SUCCESS;
81 }
82 
83 
84 VOID
85 ScmLogEvent(DWORD dwEventId,
86  WORD wType,
87  WORD wStrings,
88  LPCWSTR *lpStrings)
89 {
90  HANDLE hLog;
91 
93  L"Service Control Manager");
94  if (hLog == NULL)
95  {
96  DPRINT1("ScmLogEvent: RegisterEventSourceW failed %lu\n", GetLastError());
97  return;
98  }
99 
100  if (!ReportEventW(hLog,
101  wType,
102  0,
103  dwEventId,
104  NULL,
105  wStrings,
106  0,
107  lpStrings,
108  NULL))
109  {
110  DPRINT1("ScmLogEvent: ReportEventW failed %lu\n", GetLastError());
111  }
112 
114 }
115 
116 
117 VOID
119 {
120  HANDLE hEvent = CreateEventW(NULL, TRUE, FALSE, L"LSA_RPC_SERVER_ACTIVE");
121  if (hEvent == NULL)
122  {
123  DPRINT1("Failed to create or open the notification event (Error %lu)\n", GetLastError());
124  }
125  else
126  {
127  DPRINT("Wait for the LSA server!\n");
129  DPRINT("LSA server running!\n");
131  }
132 
133  DPRINT("ScmWaitForLsa() done\n");
134 }
135 
136 
137 BOOL WINAPI
139 {
140  DPRINT1("ShutdownHandlerRoutine() called\n");
141 
142  if (dwCtrlType & (CTRL_SHUTDOWN_EVENT | CTRL_LOGOFF_EVENT))
143  {
144  DPRINT1("Shutdown event received!\n");
145  ScmShutdown = TRUE;
146 
149 
150  /* Set the shutdown event */
152  }
153 
154  return TRUE;
155 }
156 
157 
158 int WINAPI
160  HINSTANCE hPrevInstance,
161  LPWSTR lpCmdLine,
162  int nShowCmd)
163 {
164  HANDLE hScmStartEvent = NULL;
165  HANDLE hScmAutoStartCompleteEvent = NULL;
167  BOOL bCanDeleteNamedPipeCriticalSection = FALSE;
168  DWORD dwError;
169 
170  DPRINT("SERVICES: Service Control Manager\n");
171 
172  /* Make us critical */
174 
175  /* We are initializing ourselves */
177 
178  /* Create the start event */
179  hScmStartEvent = CreateEventW(NULL, TRUE, FALSE, SCM_START_EVENT);
180  if (hScmStartEvent == NULL)
181  {
182  DPRINT1("SERVICES: Failed to create the start event\n");
183  goto done;
184  }
185  DPRINT("SERVICES: Created start event with handle %p.\n", hScmStartEvent);
186 
187  /* Create the auto-start complete event */
188  hScmAutoStartCompleteEvent = CreateEventW(NULL, TRUE, FALSE, SCM_AUTOSTARTCOMPLETE_EVENT);
189  if (hScmAutoStartCompleteEvent == NULL)
190  {
191  DPRINT1("SERVICES: Failed to create the auto-start complete event\n");
192  goto done;
193  }
194  DPRINT("SERVICES: created auto-start complete event with handle %p.\n", hScmAutoStartCompleteEvent);
195 
196  /* Create the shutdown event */
198  if (hScmShutdownEvent == NULL)
199  {
200  DPRINT1("SERVICES: Failed to create the shutdown event\n");
201  goto done;
202  }
203 
204  /* Initialize our communication named pipe's critical section */
206  bCanDeleteNamedPipeCriticalSection = TRUE;
207 
208 // ScmInitThreadManager();
209 
211 
212  /* FIXME: more initialization */
213 
214  /* Create the 'Last Known Good' control set */
216  if (dwError != ERROR_SUCCESS)
217  {
218  DPRINT1("SERVICES: Failed to create the 'Last Known Good' control set (Error %lu)\n", dwError);
219  goto done;
220  }
221 
222  /* Create the services database */
223  dwError = ScmCreateServiceDatabase();
224  if (dwError != ERROR_SUCCESS)
225  {
226  DPRINT1("SERVICES: Failed to create SCM database (Error %lu)\n", dwError);
227  goto done;
228  }
229 
230  /* Update the services database */
232 
233  /* Register the Service Control Manager process with the ReactOS Subsystem */
235  {
236  DPRINT1("SERVICES: Could not register SCM process\n");
237  goto done;
238  }
239 
240  /*
241  * Acquire the user service start lock until
242  * auto-start services have been started.
243  */
244  dwError = ScmAcquireServiceStartLock(TRUE, &Lock);
245  if (dwError != ERROR_SUCCESS)
246  {
247  DPRINT1("SERVICES: Failed to acquire the service start lock (Error %lu)\n", dwError);
248  goto done;
249  }
250 
251  /* Start the RPC server */
253 
254  /* Signal start event */
255  SetEvent(hScmStartEvent);
256 
257  DPRINT("SERVICES: Initialized.\n");
258 
259  /* Register event handler (used for system shutdown) */
261 
262  /*
263  * Set our shutdown parameters: we want to shutdown after the maintained
264  * services (that inherit the default shutdown level of 640).
265  */
267 
268  /* Start auto-start services */
270 
271  /* Signal auto-start complete event */
272  SetEvent(hScmAutoStartCompleteEvent);
273 
274  /* FIXME: more to do ? */
275 
276  /* Release the service start lock */
278 
279  /* Initialization finished */
281 
282  DPRINT("SERVICES: Running.\n");
283 
284  /* Wait until the shutdown event gets signaled */
286 
287 done:
289 
290  /* Delete our communication named pipe's critical section */
291  if (bCanDeleteNamedPipeCriticalSection != FALSE)
293 
296 
297  /* Close the shutdown event */
298  if (hScmShutdownEvent != NULL)
300 
301  /* Close the auto-start complete event */
302  if (hScmAutoStartCompleteEvent != NULL)
303  CloseHandle(hScmAutoStartCompleteEvent);
304 
305  /* Close the start event */
306  if (hScmStartEvent != NULL)
307  CloseHandle(hScmStartEvent);
308 
309  DPRINT("SERVICES: Finished.\n");
310 
311  ExitThread(0);
312  return 0;
313 }
314 
315 /* EOF */
VOID PrintString(LPCSTR fmt,...)
Definition: services.c:37
ULONG_PTR ServicesProcessId
Definition: register.c:20
VOID ScmAutoStartServices(VOID)
Definition: database.c:2171
#define TRUE
Definition: types.h:120
#define CloseHandle
Definition: compat.h:398
#define ERROR_SUCCESS
Definition: deptool.c:10
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
static HANDLE hScmShutdownEvent
Definition: services.c:30
BOOL ScmInitialize
Definition: services.c:28
VOID ScmAutoShutdownServices(VOID)
Definition: database.c:2382
char CHAR
Definition: xmlstorage.h:175
VOID ScmWaitForLsa(VOID)
Definition: services.c:118
DWORD ScmCreateLastKnownGoodControlSet(VOID)
Definition: controlset.c:613
BOOL WINAPI DECLSPEC_HOTPATCH SetEvent(IN HANDLE hEvent)
Definition: synch.c:733
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
DWORD SetSecurityServicesEvent(VOID)
Definition: services.c:53
GLuint buffer
Definition: glext.h:5915
DWORD ScmInitializeSecurity(VOID)
Definition: security.c:372
HANDLE WINAPI DECLSPEC_HOTPATCH CreateEventW(IN LPSECURITY_ATTRIBUTES lpEventAttributes OPTIONAL, IN BOOL bManualReset, IN BOOL bInitialState, IN LPCWSTR lpName OPTIONAL)
Definition: synch.c:651
HANDLE WINAPI DECLSPEC_HOTPATCH OpenEventW(IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN LPCWSTR lpName)
Definition: synch.c:682
static HANDLE hEvent
Definition: comm.c:54
BOOL WINAPI SetProcessShutdownParameters(IN DWORD dwLevel, IN DWORD dwFlags)
Definition: proc.c:949
VOID ScmGetBootAndSystemDriverState(VOID)
Definition: database.c:1285
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
HINSTANCE hInstance
Definition: charmap.c:20
#define va_end(ap)
Definition: acmsvcex.h:90
DWORD ScmAcquireServiceStartLock(IN BOOL IsServiceController, OUT LPSC_RPC_LOCK lpLock)
Definition: lock.c:31
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleCtrlHandler(PHANDLER_ROUTINE HandlerRoutine, BOOL Add)
Definition: console.c:2111
smooth NULL
Definition: ftsmooth.c:416
char * va_list
Definition: acmsvcex.h:78
void DPRINT(...)
Definition: polytest.cpp:61
VOID WINAPI ExitThread(IN DWORD uExitCode)
Definition: thread.c:364
const char * LPCSTR
Definition: xmlstorage.h:183
VOID ScmLogEvent(DWORD dwEventId, WORD wType, WORD wStrings, LPCWSTR *lpStrings)
Definition: services.c:85
BOOL ScmShutdown
Definition: services.c:29
#define WINAPI
Definition: msvc.h:8
static HANDLE hLog
Definition: misc.cpp:14
unsigned short WORD
Definition: ntddk_ex.h:93
unsigned long DWORD
Definition: ntddk_ex.h:95
VOID ScmStartRpcServer(VOID)
Definition: rpcserver.c:105
#define CTRL_LOGOFF_EVENT
Definition: wincon.h:69
DWORD ScmReleaseServiceStartLock(IN OUT LPSC_RPC_LOCK lpLock)
Definition: lock.c:82
static const WCHAR L[]
Definition: oid.c:1250
IN OUT PLONG IN OUT PLONG Addend IN OUT PLONG IN LONG IN OUT PLONG IN LONG Increment IN PNDIS_RW_LOCK Lock
Definition: CrNtStubs.h:75
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
Definition: services.c:159
BOOL WINAPI ReportEventW(IN HANDLE hEventLog, IN WORD wType, IN WORD wCategory, IN DWORD dwEventID, IN PSID lpUserSid, IN WORD wNumStrings, IN DWORD dwDataSize, IN LPCWSTR *lpStrings, IN LPVOID lpRawData)
Definition: eventlog.c:1516
VOID ScmInitNamedPipeCriticalSection(VOID)
Definition: database.c:2441
VOID ScmDeleteNamedPipeCriticalSection(VOID)
Definition: database.c:2469
static HANDLE hScmSecurityServicesEvent
Definition: services.c:31
#define CTRL_SHUTDOWN_EVENT
Definition: wincon.h:70
#define SHUTDOWN_NORETRY
Definition: winbase.h:429
BOOL WINAPI ShutdownHandlerRoutine(DWORD dwCtrlType)
Definition: services.c:138
int __cdecl vsprintf(char *_Dest, const char *_Format, va_list _Args)
Definition: sprintf.c:733
#define va_start(ap, A)
Definition: acmsvcex.h:91
DWORD ScmCreateServiceDatabase(VOID)
Definition: database.c:1088
BOOL WINAPI DeregisterEventSource(IN HANDLE hEventLog)
Definition: eventlog.c:473
#define DPRINT1
Definition: precomp.h:8
NTSYSAPI NTSTATUS __cdecl RtlSetProcessIsCritical(_In_ BOOLEAN NewValue, _Out_opt_ PBOOLEAN OldValue, _In_ BOOLEAN NeedBreaks)
void int int ULONGLONG int va_list * ap
Definition: winesup.h:32
#define EVENT_MODIFY_STATE
Definition: winbase.h:163
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define SCM_AUTOSTARTCOMPLETE_EVENT
Definition: services.c:26
#define INFINITE
Definition: serial.h:102
#define SCM_START_EVENT
Definition: services.h:26
HANDLE WINAPI RegisterEventSourceW(IN LPCWSTR lpUNCServerName, IN LPCWSTR lpSourceName)
Definition: eventlog.c:1295
VOID ScmShutdownServiceDatabase(VOID)
Definition: database.c:1175
Definition: dsound.c:943
void WINAPI SHIM_OBJ_NAME() OutputDebugStringA(LPCSTR lpOutputString)
Definition: ignoredbgout.c:18
VOID ScmShutdownSecurity(VOID)
Definition: security.c:397
int WINAPI RegisterServicesProcess(DWORD ServicesProcessId)
Definition: logon.c:18
DWORD WINAPI GetCurrentProcessId(VOID)
Definition: proc.c:1158