xref: /reactos/base/services/dnsrslvr/dnsrslvr.c (revision ac43fd2b)
1 /*
2  * COPYRIGHT:   See COPYING in the top level directory
3  * PROJECT:     ReactOS DNS Resolver
4  * FILE:        base/services/dnsrslvr/dnsrslvr.c
5  * PURPOSE:     DNS Resolver Service
6  * PROGRAMMER:  Christoph von Wittich
7  */
8 
9 /* INCLUDES *****************************************************************/
10 
11 #include "precomp.h"
12 
13 #define NDEBUG
14 #include <debug.h>
15 
16 /* GLOBALS ******************************************************************/
17 
18 HINSTANCE hDllInstance;
19 SERVICE_STATUS_HANDLE ServiceStatusHandle;
20 SERVICE_STATUS SvcStatus;
21 static WCHAR ServiceName[] = L"Dnscache";
22 
23 DWORD WINAPI RpcThreadRoutine(LPVOID lpParameter);
24 
25 /* FUNCTIONS *****************************************************************/
26 
27 static
28 VOID
29 UpdateServiceStatus(
30     HANDLE hServiceStatus,
31     DWORD NewStatus,
32     DWORD Increment)
33 {
34     if (Increment > 0)
35         SvcStatus.dwCheckPoint += Increment;
36     else
37         SvcStatus.dwCheckPoint = 0;
38 
39     SvcStatus.dwCurrentState = NewStatus;
40     SetServiceStatus(hServiceStatus, &SvcStatus);
41 }
42 
43 static
44 DWORD
45 WINAPI
46 ServiceControlHandler(
47     DWORD dwControl,
48     DWORD dwEventType,
49     LPVOID lpEventData,
50     LPVOID lpContext)
51 {
52     switch (dwControl)
53     {
54         case SERVICE_CONTROL_SHUTDOWN:
55         case SERVICE_CONTROL_STOP:
56             UpdateServiceStatus(ServiceStatusHandle, SERVICE_STOP_PENDING, 1);
57             RpcMgmtStopServerListening(NULL);
58             DnsIntCacheFree();
59             UpdateServiceStatus(ServiceStatusHandle, SERVICE_STOPPED, 0);
60             break;
61 
62         case SERVICE_CONTROL_INTERROGATE:
63             return NO_ERROR;
64 
65         default:
66             return ERROR_CALL_NOT_IMPLEMENTED;
67     }
68     return NO_ERROR;
69 }
70 
71 VOID
72 WINAPI
73 ServiceMain(
74     DWORD argc,
75     LPWSTR *argv)
76 {
77     HANDLE hThread;
78 
79     UNREFERENCED_PARAMETER(argc);
80     UNREFERENCED_PARAMETER(argv);
81 
82     DPRINT("ServiceMain() called\n");
83 
84     SvcStatus.dwServiceType             = SERVICE_WIN32_OWN_PROCESS;
85     SvcStatus.dwCurrentState            = SERVICE_START_PENDING;
86     SvcStatus.dwControlsAccepted        = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
87     SvcStatus.dwCheckPoint              = 0;
88     SvcStatus.dwWin32ExitCode           = NO_ERROR;
89     SvcStatus.dwServiceSpecificExitCode = 0;
90     SvcStatus.dwWaitHint                = 4000;
91 
92     ServiceStatusHandle = RegisterServiceCtrlHandlerExW(ServiceName,
93                                                         ServiceControlHandler,
94                                                         NULL);
95 
96     if (!ServiceStatusHandle)
97     {
98         DPRINT1("DNSRSLVR: Unable to register service control handler (%lx)\n", GetLastError());
99         return; // FALSE
100     }
101 
102     DnsIntCacheInitialize();
103 
104     hThread = CreateThread(NULL,
105                            0,
106                            (LPTHREAD_START_ROUTINE)
107                            RpcThreadRoutine,
108                            NULL,
109                            0,
110                            NULL);
111 
112     if (!hThread)
113     {
114         DnsIntCacheFree();
115         DPRINT("Can't create RpcThread\n");
116         UpdateServiceStatus(ServiceStatusHandle, SERVICE_STOPPED, 0);
117     }
118     else
119     {
120         CloseHandle(hThread);
121     }
122 
123     DPRINT("ServiceMain() done\n");
124     UpdateServiceStatus(ServiceStatusHandle, SERVICE_RUNNING, 0);
125 }
126 
127 BOOL
128 WINAPI
129 DllMain(
130     _In_ HINSTANCE hinstDLL,
131     _In_ DWORD fdwReason,
132     _In_ PVOID pvReserved)
133 {
134     UNREFERENCED_PARAMETER(pvReserved);
135 
136     switch (fdwReason)
137     {
138         case DLL_PROCESS_ATTACH:
139             DisableThreadLibraryCalls(hinstDLL);
140             hDllInstance = hinstDLL;
141             break;
142 
143         case DLL_PROCESS_DETACH:
144             break;
145     }
146 
147     return TRUE;
148 }
149 
150 /* EOF */
151