MCPcopy Create free account
hub / github.com/dobin/RedEdr / ServiceMain

Function ServiceMain

RedEdrPplService/RedEdrPplService.cpp:66–130  ·  view source on GitHub ↗

Real service main()

Source from the content-addressed store, hash-verified

64
65// Real service main()
66VOID WINAPI ServiceMain(DWORD argc, LPTSTR* argv)
67{
68 DWORD retval = 0;
69 LOG_W(LOG_INFO, L"Service Start");
70
71 // Register our service control handler with the SCM
72 g_StatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler);
73 if (g_StatusHandle == NULL)
74 {
75 retval = GetLastError();
76 LOG_W(LOG_ERROR, L"ServiceMain: RegisterServiceCtrlHandler Error: %d", retval);
77 return;
78 }
79
80 // Set the service status to START_PENDING
81 g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
82 g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
83 g_ServiceStatus.dwControlsAccepted = 0;
84 g_ServiceStatus.dwWin32ExitCode = 0;
85 g_ServiceStatus.dwServiceSpecificExitCode = 0;
86 g_ServiceStatus.dwCheckPoint = 0;
87 g_ServiceStatus.dwWaitHint = 3000; // Wait hint of 3 seconds
88
89 if (!SetServiceStatus(g_StatusHandle, &g_ServiceStatus)) {
90 LOG_W(LOG_ERROR, L"ServiceMain: Failed to set service status");
91 return;
92 }
93
94 // Initialize object cache
95 g_ProcessResolver.PopulateAllProcesses();
96
97 // Start Control thread which will listen on a pipe for commands
98 StartControl();
99
100 // Set the service status to RUNNING after initialization is complete
101 g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
102 g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
103 g_ServiceStatus.dwCheckPoint = 0;
104 g_ServiceStatus.dwWaitHint = 0;
105
106 if (!SetServiceStatus(g_StatusHandle, &g_ServiceStatus)) {
107 LOG_W(LOG_ERROR, L"ServiceMain: Failed to set running status");
108 ShutdownService();
109 return;
110 }
111
112 LOG_W(LOG_INFO, L"Service is now running");
113
114 // Service main loop
115 while (g_ServiceStatus.dwCurrentState == SERVICE_RUNNING && !g_ServiceStopping) {
116 // Start collecting - this may block
117 StartEtwtiReader();
118
119 // If we get here, ETW reader has stopped, check if we should continue
120 if (!g_ServiceStopping) {
121 LOG_W(LOG_INFO, L"ServiceMain: ETWTI reader stopped, waiting before restart...");
122 Sleep(5000); // Wait before retry to avoid rapid restart loop
123 }

Callers

nothing calls this directly

Calls 5

StartControlFunction · 0.85
ShutdownServiceFunction · 0.85
StartEtwtiReaderFunction · 0.85
PopulateAllProcessesMethod · 0.80
LOG_WFunction · 0.70

Tested by

no test coverage detected