Source code of Windows XP (NT5)
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.

230 lines
6.2 KiB

  1. // faultsvc.cpp : Implementation of WinMain
  2. // Note: Proxy/Stub Information
  3. // To build a separate proxy/stub DLL,
  4. // run nmake -f faultsvcps.mk in the project directory.
  5. #include "windows.h"
  6. #include "shellapi.h"
  7. SERVICE_STATUS_HANDLE g_hSvcStatus = NULL;
  8. SERVICE_STATUS g_ssStatus;
  9. WCHAR g_wszSvcName[] = L"faultsvc";
  10. DWORD g_dwThreadId = 0;
  11. #define sizeofSTRW(wsz) (sizeof((wsz)) / sizeof(WCHAR))
  12. // ***************************************************************************
  13. DWORD WINAPI threadFault(LPVOID pv)
  14. {
  15. HANDLE hFile = INVALID_HANDLE_VALUE;
  16. for(;;)
  17. {
  18. Sleep(5000);
  19. hFile = CreateFileA("d:\\faultnow.flt", GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
  20. if (hFile != INVALID_HANDLE_VALUE)
  21. {
  22. SYSTEMTIME st;
  23. DWORD cbWrite;
  24. CHAR sz[1024];
  25. CloseHandle(hFile);
  26. GetSystemTime(&st);
  27. wsprintfA(sz, "%02d-%02d-%04d %02d:%02d:%02d\n", st.wDay, st.wMonth, st.wYear, st.wHour, st.wMinute, st.wSecond);
  28. hFile = CreateFileA("d:\\faultnow.log", GENERIC_WRITE, 0, NULL, OPEN_ALWAYS, 0, NULL);
  29. if (hFile != INVALID_HANDLE_VALUE)
  30. {
  31. SetFilePointer(hFile, 0, NULL, FILE_END);
  32. WriteFile(hFile, sz, lstrlenA(sz), &cbWrite, NULL);
  33. CloseHandle(hFile);
  34. DWORD *pdw = NULL;
  35. *pdw = 0;
  36. return 0;
  37. }
  38. }
  39. }
  40. return 0;
  41. }
  42. // ***************************************************************************
  43. BOOL InstallService(void)
  44. {
  45. SC_HANDLE hSCM = NULL;
  46. SC_HANDLE hSvc = NULL;
  47. BOOL fRet = FALSE;
  48. WCHAR wszFilePath[MAX_PATH];
  49. hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  50. if (hSCM == NULL)
  51. goto done;
  52. // does the service already exist?
  53. hSvc = OpenServiceW(hSCM, g_wszSvcName, SERVICE_QUERY_CONFIG);
  54. if (hSvc != NULL)
  55. fRet = TRUE;
  56. if (fRet)
  57. goto done;
  58. // Get the executable file path
  59. GetModuleFileNameW(NULL, wszFilePath, sizeofSTRW(wszFilePath));
  60. hSvc = CreateServiceW(hSCM, g_wszSvcName, g_wszSvcName, SERVICE_ALL_ACCESS,
  61. SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START,
  62. SERVICE_ERROR_NORMAL, wszFilePath, NULL, NULL,
  63. L"RPCSS\0", NULL, NULL);
  64. if (hSvc == NULL)
  65. goto done;
  66. fRet = TRUE;
  67. done:
  68. if (hSCM != NULL)
  69. CloseServiceHandle(hSCM);
  70. if (hSvc != NULL)
  71. CloseServiceHandle(hSvc);
  72. return fRet;
  73. }
  74. // ***************************************************************************
  75. BOOL UninstallService(void)
  76. {
  77. SERVICE_STATUS status;
  78. SC_HANDLE hSCM = NULL;
  79. SC_HANDLE hSvc = NULL;
  80. BOOL fRet = FALSE;
  81. WCHAR wszFilePath[MAX_PATH];
  82. hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  83. if (hSCM == NULL)
  84. goto done;
  85. // does the service already exist?
  86. hSvc = OpenServiceW(hSCM, g_wszSvcName, SERVICE_STOP | DELETE);
  87. if (hSvc == NULL)
  88. fRet = TRUE;
  89. if (fRet)
  90. goto done;
  91. ControlService(hSvc, SERVICE_CONTROL_STOP, &status);
  92. fRet = DeleteService(hSvc);
  93. done:
  94. if (hSCM != NULL)
  95. CloseServiceHandle(hSCM);
  96. if (hSvc != NULL)
  97. CloseServiceHandle(hSvc);
  98. return fRet;
  99. }
  100. // ***************************************************************************
  101. void WINAPI ServiceHandler(DWORD fdwControl)
  102. {
  103. switch (fdwControl)
  104. {
  105. case SERVICE_CONTROL_STOP:
  106. g_ssStatus.dwCurrentState = SERVICE_STOP_PENDING;
  107. SetServiceStatus(g_hSvcStatus, &g_ssStatus);
  108. PostThreadMessage(g_dwThreadId, WM_QUIT, 0, 0);
  109. break;
  110. case SERVICE_CONTROL_PAUSE:
  111. case SERVICE_CONTROL_CONTINUE:
  112. case SERVICE_CONTROL_INTERROGATE:
  113. case SERVICE_CONTROL_SHUTDOWN:
  114. default:
  115. break;
  116. }
  117. }
  118. // ***************************************************************************
  119. void WINAPI ServiceMain(DWORD dwArgc, LPWSTR *lpszArgv)
  120. {
  121. HANDLE hth = NULL;
  122. DWORD dw;
  123. MSG msg;
  124. ZeroMemory(&g_ssStatus, sizeof(g_ssStatus));
  125. g_hSvcStatus = RegisterServiceCtrlHandlerW(g_wszSvcName, ServiceHandler);
  126. if (g_hSvcStatus == NULL)
  127. goto done;
  128. // mark the service as about to start
  129. g_ssStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
  130. g_ssStatus.dwCurrentState = SERVICE_START_PENDING;
  131. g_ssStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  132. g_ssStatus.dwWin32ExitCode = 0;
  133. g_ssStatus.dwServiceSpecificExitCode = 0;
  134. g_ssStatus.dwCheckPoint = 1;
  135. g_ssStatus.dwWaitHint = 1000;
  136. SetServiceStatus(g_hSvcStatus, &g_ssStatus);
  137. g_dwThreadId = GetCurrentThreadId();
  138. // mark the service as started
  139. g_ssStatus.dwCurrentState = SERVICE_RUNNING;
  140. SetServiceStatus(g_hSvcStatus, &g_ssStatus);
  141. hth = CreateThread(NULL, 0, threadFault, NULL, 0, &dw);
  142. if (hth != NULL)
  143. {
  144. while (GetMessage(&msg, 0, 0, 0))
  145. DispatchMessage(&msg);
  146. CloseHandle(hth);
  147. }
  148. // mark the service as stopped
  149. g_ssStatus.dwCurrentState = SERVICE_STOPPED;
  150. SetServiceStatus(g_hSvcStatus, &g_ssStatus);
  151. done:
  152. return;
  153. }
  154. // ***************************************************************************
  155. BOOL StartService(void)
  156. {
  157. SERVICE_TABLE_ENTRYW st[] =
  158. {
  159. { g_wszSvcName, ServiceMain },
  160. { NULL, NULL }
  161. };
  162. return StartServiceCtrlDispatcherW(st);
  163. }
  164. // ***************************************************************************
  165. extern "C" int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  166. LPWSTR lpCmdLine, int nShowCmd)
  167. {
  168. LPWSTR wszCmdLine = NULL;
  169. WCHAR **argv;
  170. int argc;
  171. wszCmdLine = GetCommandLineW();
  172. argv = CommandLineToArgvW(wszCmdLine, &argc);
  173. if (argc > 1)
  174. {
  175. if (_wcsicmp(argv[1], L"Install") == 0)
  176. return InstallService();
  177. if (_wcsicmp(argv[1], L"Uninstall") == 0)
  178. return UninstallService();
  179. }
  180. StartService();
  181. return 0;
  182. }