Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
4.6 KiB

  1. /******************************************************************************
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. dllmain.cpp
  5. Revision History:
  6. derekm 02/28/2001 created
  7. ******************************************************************************/
  8. #include "stdafx.h"
  9. //////////////////////////////////////////////////////////////////////////////
  10. // globals
  11. struct SServiceOps
  12. {
  13. SERVICE_STATUS_HANDLE hss;
  14. SERVICE_STATUS ss;
  15. HANDLE hev;
  16. HANDLE hwait;
  17. HANDLE hevStartDone;
  18. };
  19. CRITICAL_SECTION g_csReqs;
  20. HANDLE g_hevSvcStop = NULL;
  21. HINSTANCE g_hInstance = NULL;
  22. //////////////////////////////////////////////////////////////////////////////
  23. // DllMain
  24. // ***************************************************************************
  25. extern "C"
  26. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID)
  27. {
  28. BOOL fRet = TRUE;
  29. switch(dwReason)
  30. {
  31. case DLL_PROCESS_ATTACH:
  32. g_hInstance = hInstance;
  33. DisableThreadLibraryCalls(hInstance);
  34. __try { InitializeCriticalSection(&g_csReqs); }
  35. __except (EXCEPTION_EXECUTE_HANDLER) { fRet = FALSE;}
  36. InitializeSvcDataStructs();
  37. break;
  38. case DLL_PROCESS_DETACH:
  39. DeleteCriticalSection(&g_csReqs);
  40. break;
  41. }
  42. return fRet;
  43. }
  44. //////////////////////////////////////////////////////////////////////////////
  45. // Service functions
  46. // ***************************************************************************
  47. DWORD WINAPI HandlerEx(DWORD dwControl, DWORD dwEventType, LPVOID lpEventData,
  48. LPVOID lpContext)
  49. {
  50. HANDLE hevShutdown = (HANDLE)lpContext;
  51. switch(dwControl)
  52. {
  53. case SERVICE_CONTROL_STOP:
  54. if (g_hevSvcStop != NULL)
  55. SetEvent(g_hevSvcStop);
  56. break;
  57. case SERVICE_CONTROL_PAUSE:
  58. case SERVICE_CONTROL_CONTINUE:
  59. case SERVICE_CONTROL_INTERROGATE:
  60. break;
  61. case SERVICE_CONTROL_SHUTDOWN:
  62. if (g_hevSvcStop != NULL)
  63. SetEvent(g_hevSvcStop);
  64. break;
  65. default:
  66. return ERROR_CALL_NOT_IMPLEMENTED;
  67. }
  68. return NOERROR;
  69. }
  70. // ***************************************************************************
  71. void WINAPI ServiceMain(DWORD dwArgc, LPWSTR *lpszArgv)
  72. {
  73. SERVICE_STATUS_HANDLE hss;
  74. SERVICE_STATUS ss;
  75. CAutoUnlockCS aucs(&g_csReqs);
  76. SRequest *rgReqs = NULL;
  77. HANDLE hevShutdown = NULL;
  78. WCHAR wszMod[MAX_PATH];
  79. DWORD dw, cReqs;
  80. BOOL fRet;
  81. INIT_TRACING;
  82. // if lpszArgv is NULL or the ER service is not the one to be started
  83. // then bail...
  84. if (lpszArgv == NULL || _wcsicmp(lpszArgv[0], L"ersvc") != 0)
  85. return;
  86. g_hevSvcStop = CreateEventW(NULL, TRUE, FALSE, NULL);
  87. if (g_hevSvcStop == NULL)
  88. return;
  89. hss = RegisterServiceCtrlHandlerExW(c_wszERSvc, HandlerEx,
  90. (LPVOID)&g_hevSvcStop);
  91. // set up the status structure & set the initial status
  92. ss.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
  93. ss.dwCurrentState = SERVICE_START_PENDING;
  94. ss.dwServiceType = SERVICE_WIN32_SHARE_PROCESS;
  95. ss.dwServiceSpecificExitCode = 0;
  96. ss.dwWin32ExitCode = 0;
  97. ss.dwCheckPoint = 0;
  98. ss.dwWaitHint = 1000;
  99. SetServiceStatus(hss, &ss);
  100. // start up the waits
  101. fRet = StartERSvc(hss, ss, &rgReqs, &cReqs);
  102. if (fRet == FALSE)
  103. goto done;
  104. // yay! we're all happily running now...
  105. ss.dwCurrentState = SERVICE_RUNNING;
  106. ss.dwCheckPoint++;
  107. SetServiceStatus(hss, &ss);
  108. fRet = ProcessRequests(rgReqs, cReqs);
  109. // set ourselves as in the process of stopping
  110. ss.dwCurrentState = SERVICE_STOP_PENDING;
  111. ss.dwCheckPoint = 0;
  112. SetServiceStatus(hss, &ss);
  113. // stop all the waits
  114. __try { StopERSvc(hss, ss, rgReqs, cReqs); }
  115. __except(EXCEPTION_EXECUTE_HANDLER) { }
  116. SetLastError(0);
  117. done:
  118. if (g_hevSvcStop != NULL)
  119. CloseHandle(g_hevSvcStop);
  120. if (rgReqs != NULL)
  121. MyFree(rgReqs);
  122. ss.dwWin32ExitCode = GetLastError();
  123. ss.dwCurrentState = SERVICE_STOPPED;
  124. SetServiceStatus(hss, &ss);
  125. TERM_TRACING;
  126. return;
  127. }