[CRYPTEX] Add French translation
[reactos.git] / base / services / nfsd / service.c
1 /*---------------------------------------------------------------------------
2 THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
3 ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED
4 TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
5 PARTICULAR PURPOSE.
6
7 Copyright (C) Microsoft Corporation. All rights reserved.
8
9 MODULE: service.c
10
11 PURPOSE: Implements functions required by all Windows NT services
12
13 FUNCTIONS:
14 main(int argc, char **argv);
15 service_ctrl(DWORD dwCtrlCode);
16 service_main(DWORD dwArgc, LPTSTR *lpszArgv);
17 CmdInstallService();
18 CmdRemoveService();
19 CmdDebugService(int argc, char **argv);
20 ControlHandler ( DWORD dwCtrlType );
21 GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize );
22
23 ---------------------------------------------------------------------------*/
24 #include <windows.h>
25 #ifndef STANDALONE_NFSD
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <process.h>
29 #include <tchar.h>
30
31 #include "service.h"
32
33 // internal variables
34 SERVICE_STATUS ssStatus; // current status of the service
35 SERVICE_STATUS_HANDLE sshStatusHandle;
36 DWORD dwErr = 0;
37 BOOL bDebug = FALSE;
38 TCHAR szErr[256];
39
40 // internal function prototypes
41 VOID WINAPI service_ctrl(DWORD dwCtrlCode);
42 VOID WINAPI service_main(DWORD dwArgc, LPTSTR *lpszArgv);
43 VOID CmdInstallService();
44 VOID CmdRemoveService();
45 VOID CmdDebugService(int argc, char **argv);
46 BOOL WINAPI ControlHandler ( DWORD dwCtrlType );
47 LPTSTR GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize );
48
49 //
50 // FUNCTION: main
51 //
52 // PURPOSE: entrypoint for service
53 //
54 // PARAMETERS:
55 // argc - number of command line arguments
56 // argv - array of command line arguments
57 //
58 // RETURN VALUE:
59 // none
60 //
61 // COMMENTS:
62 // main() either performs the command line task, or
63 // call StartServiceCtrlDispatcher to register the
64 // main service thread. When the this call returns,
65 // the service has stopped, so exit.
66 //
67 void __cdecl main(int argc, char **argv)
68 {
69 SERVICE_TABLE_ENTRY dispatchTable[] =
70 {
71 { TEXT(SZSERVICENAME), (LPSERVICE_MAIN_FUNCTION)service_main},
72 { NULL, NULL}
73 };
74
75 if ( (argc > 1) &&
76 ((*argv[1] == '-') || (*argv[1] == '/')) )
77 {
78 if ( _stricmp( "install", argv[1]+1 ) == 0 )
79 {
80 CmdInstallService();
81 }
82 else if ( _stricmp( "remove", argv[1]+1 ) == 0 )
83 {
84 CmdRemoveService();
85 }
86 else if ( _stricmp( "debug", argv[1]+1 ) == 0 )
87 {
88 bDebug = TRUE;
89 CmdDebugService(argc, argv);
90 }
91 else
92 {
93 goto dispatch;
94 }
95 exit(0);
96 }
97
98 // if it doesn't match any of the above parameters
99 // the service control manager may be starting the service
100 // so we must call StartServiceCtrlDispatcher
101 dispatch:
102 // this is just to be friendly
103 printf( "%s -install to install the service\n", SZAPPNAME );
104 printf( "%s -remove to remove the service\n", SZAPPNAME );
105 printf( "%s -debug <params> to run as a console app for debugging\n", SZAPPNAME );
106 printf( "\nStartServiceCtrlDispatcher being called.\n" );
107 printf( "This may take several seconds. Please wait.\n" );
108
109 if (!StartServiceCtrlDispatcher(dispatchTable))
110 AddToMessageLog(TEXT("StartServiceCtrlDispatcher failed."));
111 }
112
113
114
115 //
116 // FUNCTION: service_main
117 //
118 // PURPOSE: To perform actual initialization of the service
119 //
120 // PARAMETERS:
121 // dwArgc - number of command line arguments
122 // lpszArgv - array of command line arguments
123 //
124 // RETURN VALUE:
125 // none
126 //
127 // COMMENTS:
128 // This routine performs the service initialization and then calls
129 // the user defined ServiceStart() routine to perform majority
130 // of the work.
131 //
132 void WINAPI service_main(DWORD dwArgc, LPTSTR *lpszArgv)
133 {
134
135 // register our service control handler:
136 //
137 sshStatusHandle = RegisterServiceCtrlHandler( TEXT(SZSERVICENAME), service_ctrl);
138
139 if (!sshStatusHandle)
140 goto cleanup;
141
142 DbgPrint("Starting service\n");
143
144 // SERVICE_STATUS members that don't change in example
145 //
146 ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
147 ssStatus.dwServiceSpecificExitCode = 0;
148
149
150 // report the status to the service control manager.
151 //
152 if (!ReportStatusToSCMgr(
153 SERVICE_START_PENDING, // service state
154 NO_ERROR, // exit code
155 3000)) // wait hint
156 goto cleanup;
157
158 DbgPrint("Starting service 2\n");
159
160 SetConsoleCtrlHandler( ControlHandler, TRUE );
161
162 ServiceStart( dwArgc, lpszArgv );
163
164 DbgPrint("Done\n");
165
166 cleanup:
167
168 // try to report the stopped status to the service control manager.
169 //
170 if (sshStatusHandle)
171 (VOID)ReportStatusToSCMgr(
172 SERVICE_STOPPED,
173 dwErr,
174 0);
175
176 return;
177 }
178
179
180
181 //
182 // FUNCTION: service_ctrl
183 //
184 // PURPOSE: This function is called by the SCM whenever
185 // ControlService() is called on this service.
186 //
187 // PARAMETERS:
188 // dwCtrlCode - type of control requested
189 //
190 // RETURN VALUE:
191 // none
192 //
193 // COMMENTS:
194 //
195 VOID WINAPI service_ctrl(DWORD dwCtrlCode)
196 {
197 // Handle the requested control code.
198 //
199
200 DbgPrint("service_ctrl called\n");
201
202 switch (dwCtrlCode)
203 {
204 // Stop the service.
205 //
206 // SERVICE_STOP_PENDING should be reported before
207 // setting the Stop Event - hServerStopEvent - in
208 // ServiceStop(). This avoids a race condition
209 // which may result in a 1053 - The Service did not respond...
210 // error.
211 #ifdef __REACTOS__
212 case SERVICE_CONTROL_SHUTDOWN:
213 #endif
214 case SERVICE_CONTROL_STOP:
215 DbgPrint("for stop\n");
216 ReportStatusToSCMgr(SERVICE_STOP_PENDING, NO_ERROR, 0);
217 ServiceStop();
218 DbgPrint("Done\n");
219 return;
220
221 // Update the service status.
222 //
223 case SERVICE_CONTROL_INTERROGATE:
224 break;
225
226 // invalid control code
227 //
228 default:
229 break;
230
231 }
232
233 ReportStatusToSCMgr(ssStatus.dwCurrentState, NO_ERROR, 0);
234 }
235
236
237
238 //
239 // FUNCTION: ReportStatusToSCMgr()
240 //
241 // PURPOSE: Sets the current status of the service and
242 // reports it to the Service Control Manager
243 //
244 // PARAMETERS:
245 // dwCurrentState - the state of the service
246 // dwWin32ExitCode - error code to report
247 // dwWaitHint - worst case estimate to next checkpoint
248 //
249 // RETURN VALUE:
250 // TRUE - success
251 // FALSE - failure
252 //
253 // COMMENTS:
254 //
255 BOOL ReportStatusToSCMgr(DWORD dwCurrentState,
256 DWORD dwWin32ExitCode,
257 DWORD dwWaitHint)
258 {
259 static DWORD dwCheckPoint = 1;
260 BOOL fResult = TRUE;
261
262
263 if ( !bDebug ) // when debugging we don't report to the SCM
264 {
265 if (dwCurrentState == SERVICE_START_PENDING)
266 ssStatus.dwControlsAccepted = 0;
267 else
268 #ifndef __REACTOS__
269 ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
270 #else
271 ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
272 #endif
273
274 ssStatus.dwCurrentState = dwCurrentState;
275 ssStatus.dwWin32ExitCode = dwWin32ExitCode;
276 ssStatus.dwWaitHint = dwWaitHint;
277
278 if ( ( dwCurrentState == SERVICE_RUNNING ) ||
279 ( dwCurrentState == SERVICE_STOPPED ) )
280 ssStatus.dwCheckPoint = 0;
281 else
282 ssStatus.dwCheckPoint = dwCheckPoint++;
283
284
285 // Report the status of the service to the service control manager.
286 fResult = SetServiceStatus(sshStatusHandle, &ssStatus);
287 if (!fResult)
288 AddToMessageLog(TEXT("SetServiceStatus"));
289 }
290 return fResult;
291 }
292
293
294
295 //
296 // FUNCTION: AddToMessageLog(LPTSTR lpszMsg)
297 //
298 // PURPOSE: Allows any thread to log an error message
299 //
300 // PARAMETERS:
301 // lpszMsg - text for message
302 //
303 // RETURN VALUE:
304 // none
305 //
306 // COMMENTS:
307 //
308 VOID AddToMessageLog(LPTSTR lpszMsg)
309 {
310 TCHAR szMsg [(sizeof(SZSERVICENAME) / sizeof(TCHAR)) + 100 ];
311 HANDLE hEventSource;
312 LPTSTR lpszStrings[2];
313
314 if ( !bDebug )
315 {
316 dwErr = GetLastError();
317
318 // Use event logging to log the error.
319 //
320 hEventSource = RegisterEventSource(NULL, TEXT(SZSERVICENAME));
321
322 #ifndef __REACTOS__
323 _stprintf_s(szMsg,(sizeof(SZSERVICENAME) / sizeof(TCHAR)) + 100, TEXT("%s error: %d"), TEXT(SZSERVICENAME), dwErr);
324 #else
325 _sntprintf(szMsg,(sizeof(SZSERVICENAME) / sizeof(TCHAR)) + 100, TEXT("%s error: %d"), TEXT(SZSERVICENAME), dwErr);
326 #endif
327 lpszStrings[0] = szMsg;
328 lpszStrings[1] = lpszMsg;
329
330 if (hEventSource != NULL)
331 {
332 ReportEvent(hEventSource, // handle of event source
333 EVENTLOG_ERROR_TYPE, // event type
334 0, // event category
335 0, // event ID
336 NULL, // current user's SID
337 2, // strings in lpszStrings
338 0, // no bytes of raw data
339 lpszStrings, // array of error strings
340 NULL); // no raw data
341
342 (VOID) DeregisterEventSource(hEventSource);
343 }
344 }
345 }
346
347
348
349
350 ///////////////////////////////////////////////////////////////////
351 //
352 // The following code handles service installation and removal
353 //
354
355
356 //
357 // FUNCTION: CmdInstallService()
358 //
359 // PURPOSE: Installs the service
360 //
361 // PARAMETERS:
362 // none
363 //
364 // RETURN VALUE:
365 // none
366 //
367 // COMMENTS:
368 //
369 void CmdInstallService()
370 {
371 SC_HANDLE schService;
372 SC_HANDLE schSCManager;
373
374 TCHAR szPath[512];
375
376 if ( GetModuleFileName( NULL, szPath, 512 ) == 0 )
377 {
378 _tprintf(TEXT("Unable to install %s - %s\n"), TEXT(SZSERVICEDISPLAYNAME), GetLastErrorText(szErr, 256));
379 return;
380 }
381
382 schSCManager = OpenSCManager(
383 NULL, // machine (NULL == local)
384 NULL, // database (NULL == default)
385 SC_MANAGER_CONNECT | SC_MANAGER_CREATE_SERVICE // access required
386 );
387 if ( schSCManager )
388 {
389 schService = CreateService(
390 schSCManager, // SCManager database
391 TEXT(SZSERVICENAME), // name of service
392 TEXT(SZSERVICEDISPLAYNAME), // name to display
393 SERVICE_QUERY_STATUS, // desired access
394 SERVICE_WIN32_OWN_PROCESS, // service type
395 SERVICE_AUTO_START, // start type
396 SERVICE_ERROR_NORMAL, // error control type
397 szPath, // service's binary
398 NULL, // no load ordering group
399 NULL, // no tag identifier
400 TEXT(SZDEPENDENCIES), // dependencies
401 NULL, // LocalSystem account
402 NULL); // no password
403
404 if ( schService )
405 {
406 _tprintf(TEXT("%s installed.\n"), TEXT(SZSERVICEDISPLAYNAME) );
407 CloseServiceHandle(schService);
408 }
409 else
410 {
411 _tprintf(TEXT("CreateService failed - %s\n"), GetLastErrorText(szErr, 256));
412 }
413
414 CloseServiceHandle(schSCManager);
415 }
416 else
417 _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
418 }
419
420
421
422 //
423 // FUNCTION: CmdRemoveService()
424 //
425 // PURPOSE: Stops and removes the service
426 //
427 // PARAMETERS:
428 // none
429 //
430 // RETURN VALUE:
431 // none
432 //
433 // COMMENTS:
434 //
435 void CmdRemoveService()
436 {
437 SC_HANDLE schService;
438 SC_HANDLE schSCManager;
439
440 schSCManager = OpenSCManager(
441 NULL, // machine (NULL == local)
442 NULL, // database (NULL == default)
443 SC_MANAGER_CONNECT // access required
444 );
445 if ( schSCManager )
446 {
447 schService = OpenService(schSCManager, TEXT(SZSERVICENAME), DELETE | SERVICE_STOP | SERVICE_QUERY_STATUS);
448
449 if (schService)
450 {
451 // try to stop the service
452 if ( ControlService( schService, SERVICE_CONTROL_STOP, &ssStatus ) )
453 {
454 _tprintf(TEXT("Stopping %s."), TEXT(SZSERVICEDISPLAYNAME));
455 Sleep( 1000 );
456
457 while ( QueryServiceStatus( schService, &ssStatus ) )
458 {
459 if ( ssStatus.dwCurrentState == SERVICE_STOP_PENDING )
460 {
461 _tprintf(TEXT("."));
462 Sleep( 1000 );
463 }
464 else
465 break;
466 }
467
468 if ( ssStatus.dwCurrentState == SERVICE_STOPPED )
469 _tprintf(TEXT("\n%s stopped.\n"), TEXT(SZSERVICEDISPLAYNAME) );
470 else
471 _tprintf(TEXT("\n%s failed to stop.\n"), TEXT(SZSERVICEDISPLAYNAME) );
472
473 }
474
475 // now remove the service
476 if ( DeleteService(schService) )
477 _tprintf(TEXT("%s removed.\n"), TEXT(SZSERVICEDISPLAYNAME) );
478 else
479 _tprintf(TEXT("DeleteService failed - %s\n"), GetLastErrorText(szErr,256));
480
481
482 CloseServiceHandle(schService);
483 }
484 else
485 _tprintf(TEXT("OpenService failed - %s\n"), GetLastErrorText(szErr,256));
486
487 CloseServiceHandle(schSCManager);
488 }
489 else
490 _tprintf(TEXT("OpenSCManager failed - %s\n"), GetLastErrorText(szErr,256));
491 }
492
493
494
495
496 ///////////////////////////////////////////////////////////////////
497 //
498 // The following code is for running the service as a console app
499 //
500
501
502 //
503 // FUNCTION: CmdDebugService(int argc, char ** argv)
504 //
505 // PURPOSE: Runs the service as a console application
506 //
507 // PARAMETERS:
508 // argc - number of command line arguments
509 // argv - array of command line arguments
510 //
511 // RETURN VALUE:
512 // none
513 //
514 // COMMENTS:
515 //
516 void CmdDebugService(int argc, char ** argv)
517 {
518 DWORD dwArgc;
519 LPTSTR *lpszArgv;
520
521 #ifdef UNICODE
522 lpszArgv = CommandLineToArgvW(GetCommandLineW(), &(dwArgc) );
523 if (NULL == lpszArgv)
524 {
525 // CommandLineToArvW failed!!
526 _tprintf(TEXT("CmdDebugService CommandLineToArgvW returned NULL\n"));
527 return;
528 }
529 #else
530 dwArgc = (DWORD) argc;
531 lpszArgv = argv;
532 #endif
533
534 _tprintf(TEXT("Debugging %s.\n"), TEXT(SZSERVICEDISPLAYNAME));
535
536 SetConsoleCtrlHandler( ControlHandler, TRUE );
537
538 ServiceStart( dwArgc, lpszArgv );
539
540 #ifdef UNICODE
541 // Must free memory allocated for arguments
542
543 GlobalFree(lpszArgv);
544 #endif // UNICODE
545
546 }
547
548
549 //
550 // FUNCTION: ControlHandler ( DWORD dwCtrlType )
551 //
552 // PURPOSE: Handled console control events
553 //
554 // PARAMETERS:
555 // dwCtrlType - type of control event
556 //
557 // RETURN VALUE:
558 // True - handled
559 // False - unhandled
560 //
561 // COMMENTS:
562 //
563 BOOL WINAPI ControlHandler ( DWORD dwCtrlType )
564 {
565 switch ( dwCtrlType )
566 {
567 case CTRL_BREAK_EVENT: // use Ctrl+C or Ctrl+Break to simulate
568 case CTRL_C_EVENT: // SERVICE_CONTROL_STOP in debug mode
569 _tprintf(TEXT("Stopping %s.\n"), TEXT(SZSERVICEDISPLAYNAME));
570 ServiceStop();
571 return TRUE;
572 break;
573
574 }
575 return FALSE;
576 }
577
578 //
579 // FUNCTION: GetLastErrorText
580 //
581 // PURPOSE: copies error message text to string
582 //
583 // PARAMETERS:
584 // lpszBuf - destination buffer
585 // dwSize - size of buffer
586 //
587 // RETURN VALUE:
588 // destination buffer
589 //
590 // COMMENTS:
591 //
592 LPTSTR GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize )
593 {
594 DWORD dwRet;
595 LPTSTR lpszTemp = NULL;
596
597 dwRet = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
598 NULL,
599 GetLastError(),
600 LANG_NEUTRAL,
601 (LPTSTR)&lpszTemp,
602 0,
603 NULL );
604
605 // supplied buffer is not long enough
606 if ( !dwRet || ( (long)dwSize < (long)dwRet+14 ) )
607 lpszBuf[0] = TEXT('\0');
608 else
609 {
610 if (NULL != lpszTemp)
611 {
612 lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0'); //remove cr and newline character
613 #ifndef __REACTOS__
614 _stprintf_s( lpszBuf, dwSize, TEXT("%s (0x%x)"), lpszTemp, GetLastError() );
615 #else
616 _sntprintf( lpszBuf, dwSize, TEXT("%s (0x%x)"), lpszTemp, GetLastError() );
617 #endif
618 }
619 }
620
621 if ( NULL != lpszTemp )
622 LocalFree((HLOCAL) lpszTemp );
623
624 return lpszBuf;
625 }
626 #endif