[TFTPD] Fix compilation, and use the #define MAX_SERVERS where needed instead of...
[reactos.git] / base / services / rpcss / service_main.c
1 /*
2 * PROJECT: ReactOS Remote Procedure Call service
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/services/rpcss/service_main.c
5 * PURPOSE: Service control code
6 * COPYRIGHT: Copyright 2008 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10 #include "rpcss.h"
11
12 #include <winsvc.h>
13
14 #define NDEBUG
15 #include <debug.h>
16
17 extern BOOL RPCSS_Initialize(void);
18 extern BOOL RPCSS_Shutdown(void);
19 extern HANDLE exit_event;
20
21 static VOID WINAPI ServiceMain(DWORD, LPWSTR *);
22 static WCHAR ServiceName[] = L"RpcSs";
23 SERVICE_TABLE_ENTRYW ServiceTable[] =
24 {
25 { ServiceName, ServiceMain },
26 { NULL, NULL }
27 };
28
29 static SERVICE_STATUS ServiceStatus;
30 static SERVICE_STATUS_HANDLE ServiceStatusHandle;
31
32 DWORD WINAPI
33 ServiceControlHandler(DWORD dwControl,
34 DWORD dwEventType,
35 LPVOID lpEventData,
36 LPVOID lpContext)
37 {
38 switch (dwControl)
39 {
40 case SERVICE_CONTROL_SHUTDOWN:
41 case SERVICE_CONTROL_STOP:
42 SetEvent(exit_event);
43 return NO_ERROR;
44
45 case SERVICE_CONTROL_INTERROGATE:
46 return NO_ERROR;
47
48 default:
49 return ERROR_CALL_NOT_IMPLEMENTED;
50 }
51 }
52
53 VOID WINAPI
54 ServiceMain(DWORD argc, LPWSTR argv[])
55 {
56 DWORD dwError;
57
58 ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
59 ServiceControlHandler,
60 NULL);
61 if (!ServiceStatusHandle)
62 {
63 dwError = GetLastError();
64 DPRINT1("RegisterServiceCtrlHandlerW() failed! (Error %lu)\n", dwError);
65 return;
66 }
67
68 ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
69 ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
70 ServiceStatus.dwControlsAccepted = 0;
71 ServiceStatus.dwWin32ExitCode = NO_ERROR;
72 ServiceStatus.dwServiceSpecificExitCode = 0;
73 ServiceStatus.dwCheckPoint = 0;
74 ServiceStatus.dwWaitHint = 1000;
75 SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
76
77 if (RPCSS_Initialize())
78 {
79 ServiceStatus.dwCurrentState = SERVICE_RUNNING;
80 ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
81 SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
82
83 WaitForSingleObject(exit_event, INFINITE);
84
85 RPCSS_Shutdown();
86
87 ServiceStatus.dwCurrentState = SERVICE_STOPPED;
88 SetServiceStatus(ServiceStatusHandle, &ServiceStatus);
89 }
90 }
91
92 int wmain(int argc, LPWSTR argv[])
93 {
94 if (!StartServiceCtrlDispatcherW(ServiceTable))
95 {
96 DPRINT1("StartServiceCtrlDispatcherW() failed\n");
97 return 1;
98 }
99
100 return 0;
101 }