9#pragma comment(lib, "advapi32.lib")
13#define PANO_STOP_EVENT_NAME L"PanoptesStop"
16#define PANO_STOP_EXIT_WAIT_MS 15000
18#define PANO_SHUTDOWN_WINDOW_CLASS L"PanoptesShutdownWindow"
20static HANDLE g_StopEvent = NULL;
21static HANDLE g_ShutdownComplete = NULL;
23static void RequestShutdown()
25 if (g_StopEvent != NULL)
27 SetEvent(g_StopEvent);
32static void RequestShutdownAndWait()
38#pragma region Console Handler
40static BOOL WINAPI ConsoleHandler(DWORD controlType)
45 case CTRL_BREAK_EVENT:
49 case CTRL_CLOSE_EVENT:
50 case CTRL_LOGOFF_EVENT:
51 case CTRL_SHUTDOWN_EVENT:
52 RequestShutdownAndWait();
61#pragma region Session End Window
63static LRESULT CALLBACK ShutdownWindowProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam)
67 case WM_QUERYENDSESSION:
73 RequestShutdownAndWait();
77 return DefWindowProcW(window, message, wParam, lParam);
84static void ShutdownWindowThread(HANDLE ready, HWND *created)
86 HINSTANCE instance = GetModuleHandleW(NULL);
88 WNDCLASSW windowClass{};
89 windowClass.lpfnWndProc = ShutdownWindowProc;
90 windowClass.hInstance = instance;
92 RegisterClassW(&windowClass);
96 0, 0, 0, 0, NULL, NULL, instance, NULL);
104 while (GetMessageW(&message, NULL, 0, 0) > 0)
106 TranslateMessage(&message);
107 DispatchMessageW(&message);
118 SECURITY_ATTRIBUTES security{
sizeof(security), NULL, FALSE };
119 PSECURITY_DESCRIPTOR descriptor = NULL;
122 if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(L
"D:P(A;;GA;;;SY)(A;;GA;;;BA)",
123 SDDL_REVISION_1, &descriptor, NULL))
127 security.lpSecurityDescriptor = descriptor;
131 DWORD error = GetLastError();
132 if (descriptor != NULL)
134 LocalFree(descriptor);
136 if (g_StopEvent == NULL)
138 std::cerr <<
"Failed to create the stop event. Error code: " << error << std::endl;
141 if (error == ERROR_ALREADY_EXISTS)
145 std::cerr <<
"The stop event already exists in another process" << std::endl;
146 CloseHandle(g_StopEvent);
151 g_ShutdownComplete = CreateEventW(NULL, TRUE, FALSE, NULL);
152 if (g_ShutdownComplete == NULL)
157 SetConsoleCtrlHandler(ConsoleHandler, TRUE);
159 HANDLE ready = CreateEventW(NULL, TRUE, FALSE, NULL);
165 std::thread(ShutdownWindowThread, ready, &window).detach();
166 WaitForSingleObject(ready, INFINITE);
171 std::cerr <<
"Failed to create the session end window" << std::endl;
180 WaitForSingleObject(g_StopEvent, INFINITE);
185 if (g_ShutdownComplete != NULL)
187 SetEvent(g_ShutdownComplete);
194 if (stopEvent == NULL)
196 DWORD error = GetLastError();
197 if (error == ERROR_ACCESS_DENIED)
201 std::cerr <<
"Panoptes is not running" << std::endl;
206 CloseHandle(stopEvent);
210 while (GetTickCount64() < deadline)
212 HANDLE mutex = OpenMutexA(SYNCHRONIZE, FALSE,
SERVICE_MUTEX);
ERRORCODE InitializeShutdown()
Creates the stop event and installs the console handler and session-end window. The stop event can on...
void WaitForShutdownRequest()
Blocks until a shutdown is requested.
ERRORCODE RequestServiceStop()
Implements –stop: sets a running service's stop event and waits for it to exit.
#define PANO_STOP_EXIT_WAIT_MS
void NotifyShutdownComplete()
Tells console handlers and the session-end window that teardown has finished, so they can return and ...
#define PANO_SHUTDOWN_WINDOW_CLASS
#define PANO_STOP_EVENT_NAME
#define PANO_SHUTDOWN_WAIT_MS