Log and database functions moved from server core to libnxsrv
[public/netxms.git] / src / server / core / winsrv.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Copyright (C) 2003 Victor Kirhenshtein
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** $module: winsrv.cpp
20 **
21 **/
22
23 #include "nms_core.h"
24
25 #ifdef _WIN32
26
27 //
28 // Static data
29 //
30
31 static SERVICE_STATUS_HANDLE serviceHandle;
32
33
34 //
35 // Service control handler
36 //
37
38 static VOID WINAPI ServiceCtrlHandler(DWORD ctrlCode)
39 {
40 SERVICE_STATUS status;
41
42 status.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
43 status.dwCurrentState=SERVICE_RUNNING;
44 status.dwControlsAccepted=SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
45 status.dwWin32ExitCode=0;
46 status.dwServiceSpecificExitCode=0;
47 status.dwCheckPoint=0;
48 status.dwWaitHint=0;
49
50 switch(ctrlCode)
51 {
52 case SERVICE_CONTROL_STOP:
53 case SERVICE_CONTROL_SHUTDOWN:
54 status.dwCurrentState=SERVICE_STOP_PENDING;
55 status.dwWaitHint=4000;
56 SetServiceStatus(serviceHandle,&status);
57
58 Shutdown();
59
60 status.dwCurrentState=SERVICE_STOPPED;
61 status.dwWaitHint=0;
62 break;
63 default:
64 break;
65 }
66
67 SetServiceStatus(serviceHandle,&status);
68 }
69
70
71 //
72 // Service main
73 //
74
75 static VOID WINAPI CoreServiceMain(DWORD argc, LPTSTR *argv)
76 {
77 SERVICE_STATUS status;
78
79 serviceHandle=RegisterServiceCtrlHandler(CORE_SERVICE_NAME,ServiceCtrlHandler);
80
81 // Now we start service initialization
82 status.dwServiceType=SERVICE_WIN32_OWN_PROCESS;
83 status.dwCurrentState=SERVICE_START_PENDING;
84 status.dwControlsAccepted=SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
85 status.dwWin32ExitCode=0;
86 status.dwServiceSpecificExitCode=0;
87 status.dwCheckPoint=0;
88 status.dwWaitHint=2000;
89 SetServiceStatus(serviceHandle,&status);
90
91 // Actual initialization
92 if (!Initialize())
93 {
94 // Remove database lock
95 if (g_dwFlags & AF_DB_LOCKED)
96 {
97 DBQuery(g_hCoreDB, "UPDATE locks SET lock_status=-1,owner_info='' WHERE component_id=0");
98 DBDisconnect(g_hCoreDB);
99 }
100
101 // Now service is stopped
102 status.dwCurrentState=SERVICE_STOPPED;
103 status.dwWaitHint=0;
104 SetServiceStatus(serviceHandle,&status);
105 return;
106 }
107
108 // Now service is running
109 status.dwCurrentState=SERVICE_RUNNING;
110 status.dwWaitHint=0;
111 SetServiceStatus(serviceHandle,&status);
112
113 Main();
114 }
115
116
117 //
118 // Initialize service
119 //
120
121 void InitService(void)
122 {
123 static SERVICE_TABLE_ENTRY serviceTable[2] = { { CORE_SERVICE_NAME, CoreServiceMain }, { NULL, NULL } };
124
125 if (!StartServiceCtrlDispatcher(serviceTable))
126 printf("StartServiceCtrlDispatcher() failed: %s\n", GetSystemErrorText(GetLastError()));
127 }
128
129
130 //
131 // Create service
132 //
133
134 void InstallService(char *execName, char *dllName)
135 {
136 SC_HANDLE mgr,service;
137 char cmdLine[MAX_PATH*2];
138
139 mgr=OpenSCManager(NULL,NULL,GENERIC_WRITE);
140 if (mgr==NULL)
141 {
142 printf("ERROR: Cannot connect to Service Manager (%s)\n",GetSystemErrorText(GetLastError()));
143 return;
144 }
145
146 sprintf(cmdLine, "\"%s\" --config \"%s\"", execName, g_szConfigFile);
147 service = CreateService(mgr, CORE_SERVICE_NAME, "NetXMS Core", GENERIC_READ, SERVICE_WIN32_OWN_PROCESS,
148 SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, cmdLine, NULL, NULL, NULL, NULL, NULL);
149 if (service == NULL)
150 {
151 DWORD code = GetLastError();
152
153 if (code == ERROR_SERVICE_EXISTS)
154 printf("ERROR: Service named '" CORE_SERVICE_NAME "' already exist\n");
155 else
156 printf("ERROR: Cannot create service (%s)\n", GetSystemErrorText(code));
157 }
158 else
159 {
160 printf("NetXMS Core service created successfully\n");
161 CloseServiceHandle(service);
162 }
163
164 CloseServiceHandle(mgr);
165
166 InstallEventSource(dllName);
167 }
168
169
170 //
171 // Remove service
172 //
173
174 void RemoveService(void)
175 {
176 SC_HANDLE mgr, service;
177
178 mgr=OpenSCManager(NULL, NULL, GENERIC_WRITE);
179 if (mgr == NULL)
180 {
181 printf("ERROR: Cannot connect to Service Manager (%s)\n", GetSystemErrorText(GetLastError()));
182 return;
183 }
184
185 service = OpenService(mgr, CORE_SERVICE_NAME, DELETE);
186 if (service == NULL)
187 {
188 printf("ERROR: Cannot open service named '" CORE_SERVICE_NAME "' (%s)\n",
189 GetSystemErrorText(GetLastError()));
190 }
191 else
192 {
193 if (DeleteService(service))
194 printf("NetXMS Core service deleted successfully\n");
195 else
196 printf("ERROR: Cannot remove service named '" CORE_SERVICE_NAME "' (%s)\n",
197 GetSystemErrorText(GetLastError()));
198
199 CloseServiceHandle(service);
200 }
201
202 CloseServiceHandle(mgr);
203
204 RemoveEventSource();
205 }
206
207
208 //
209 // Start service
210 //
211
212 void StartCoreService(void)
213 {
214 SC_HANDLE mgr,service;
215
216 mgr = OpenSCManager(NULL,NULL,GENERIC_WRITE);
217 if (mgr == NULL)
218 {
219 printf("ERROR: Cannot connect to Service Manager (%s)\n", GetSystemErrorText(GetLastError()));
220 return;
221 }
222
223 service = OpenService(mgr, CORE_SERVICE_NAME, SERVICE_START);
224 if (service == NULL)
225 {
226 printf("ERROR: Cannot open service named '" CORE_SERVICE_NAME "' (%s)\n",
227 GetSystemErrorText(GetLastError()));
228 }
229 else
230 {
231 if (StartService(service, 0, NULL))
232 printf("NetXMS Core service started successfully\n");
233 else
234 printf("ERROR: Cannot start service named '" CORE_SERVICE_NAME "' (%s)\n",
235 GetSystemErrorText(GetLastError()));
236
237 CloseServiceHandle(service);
238 }
239
240 CloseServiceHandle(mgr);
241 }
242
243
244 //
245 // Stop service
246 //
247
248 void StopCoreService(void)
249 {
250 SC_HANDLE mgr,service;
251
252 mgr = OpenSCManager(NULL, NULL, GENERIC_WRITE);
253 if (mgr == NULL)
254 {
255 printf("ERROR: Cannot connect to Service Manager (%s)\n", GetSystemErrorText(GetLastError()));
256 return;
257 }
258
259 service = OpenService(mgr, CORE_SERVICE_NAME, SERVICE_STOP);
260 if (service == NULL)
261 {
262 printf("ERROR: Cannot open service named '" CORE_SERVICE_NAME "' (%s)\n",
263 GetSystemErrorText(GetLastError()));
264 }
265 else
266 {
267 SERVICE_STATUS status;
268
269 if (ControlService(service, SERVICE_CONTROL_STOP, &status))
270 printf("NetXMS Core service stopped successfully\n");
271 else
272 printf("ERROR: Cannot stop service named '" CORE_SERVICE_NAME "' (%s)\n",
273 GetSystemErrorText(GetLastError()));
274
275 CloseServiceHandle(service);
276 }
277
278 CloseServiceHandle(mgr);
279 }
280
281
282 //
283 // Install event source
284 //
285
286 void InstallEventSource(char *path)
287 {
288 HKEY hKey;
289 DWORD dwTypes = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
290
291 if (ERROR_SUCCESS != RegCreateKeyEx(HKEY_LOCAL_MACHINE,
292 "System\\CurrentControlSet\\Services\\EventLog\\System\\" CORE_EVENT_SOURCE,
293 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hKey, NULL))
294 {
295 printf("Unable to create registry key: %s\n", GetSystemErrorText(GetLastError()));
296 return;
297 }
298
299 RegSetValueEx(hKey, "TypesSupported", 0, REG_DWORD,(BYTE *)&dwTypes, sizeof(DWORD));
300 RegSetValueEx(hKey, "EventMessageFile", 0, REG_EXPAND_SZ,(BYTE *)path, strlen(path) + 1);
301
302 RegCloseKey(hKey);
303 printf("Event source \"" CORE_EVENT_SOURCE "\" installed successfully\n");
304 }
305
306
307 //
308 // Remove event source
309 //
310
311 void RemoveEventSource(void)
312 {
313 if (ERROR_SUCCESS == RegDeleteKey(HKEY_LOCAL_MACHINE,
314 "System\\CurrentControlSet\\Services\\EventLog\\System\\" CORE_EVENT_SOURCE))
315 {
316 printf("Event source \"" CORE_EVENT_SOURCE "\" uninstalled successfully\n");
317 }
318 else
319 {
320 printf("Unable to uninstall event source \"" CORE_EVENT_SOURCE "\": %s\n",
321 GetSystemErrorText(GetLastError()));
322 }
323 }
324
325 #endif /* _WIN32 */