[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnurl] 170/264: tests/server: add hidden window to gracefully handle WM
From: |
gnunet |
Subject: |
[gnurl] 170/264: tests/server: add hidden window to gracefully handle WM_CLOSE |
Date: |
Thu, 30 Apr 2020 16:07:53 +0200 |
This is an automated email from the git hooks/post-receive script.
nikita pushed a commit to branch master
in repository gnurl.
commit ac1e206278b98fbe762f4b554803c64e8b562156
Author: Marc Hoersken <address@hidden>
AuthorDate: Sun Apr 12 18:38:12 2020 +0200
tests/server: add hidden window to gracefully handle WM_CLOSE
Forward Window events as signals to existing signal event handler.
---
tests/server/util.c | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 88 insertions(+)
diff --git a/tests/server/util.c b/tests/server/util.c
index c8bc32945..65d491c0b 100644
--- a/tests/server/util.c
+++ b/tests/server/util.c
@@ -544,6 +544,12 @@ static SIGHANDLER_T old_sigterm_handler = SIG_ERR;
static SIGHANDLER_T old_sigbreak_handler = SIG_ERR;
#endif
+#ifdef WIN32
+static DWORD thread_main_id = 0;
+static HANDLE thread_main_window = NULL;
+static HWND hidden_main_window = NULL;
+#endif
+
/* var which if set indicates that the program should finish execution */
volatile int got_exit_signal = 0;
@@ -606,6 +612,78 @@ static BOOL WINAPI ctrl_event_handler(DWORD dwCtrlType)
}
return TRUE;
}
+/* Window message handler for Windows applications to add support
+ * for graceful process termination via taskkill (without /f) which
+ * sends WM_CLOSE to all Windows of a process (even hidden ones).
+ *
+ * Therefore we create and run a hidden Window in a separate thread
+ * to receive and handle the WM_CLOSE message as SIGTERM signal.
+ */
+static LRESULT CALLBACK main_window_proc(HWND hwnd, UINT uMsg,
+ WPARAM wParam, LPARAM lParam)
+{
+ int signum = 0;
+ if(hwnd == hidden_main_window) {
+ switch(uMsg) {
+#ifdef SIGTERM
+ case WM_CLOSE: signum = SIGTERM; break;
+#endif
+ case WM_DESTROY: PostQuitMessage(0); break;
+ }
+ if(signum) {
+ logmsg("main_window_proc: %d -> %d", uMsg, signum);
+ exit_signal_handler(signum);
+ }
+ }
+ return DefWindowProc(hwnd, uMsg, wParam, lParam);
+}
+/* Window message queue loop for hidden main window, details see above.
+ */
+static DWORD WINAPI main_window_loop(LPVOID lpParameter)
+{
+ WNDCLASS wc;
+ BOOL ret;
+ MSG msg;
+
+ ZeroMemory(&wc, sizeof(wc));
+ wc.lpfnWndProc = (WNDPROC)main_window_proc;
+ wc.hInstance = (HINSTANCE)lpParameter;
+ wc.lpszClassName = "MainWClass";
+ if(!RegisterClass(&wc)) {
+ perror("RegisterClass failed");
+ return (DWORD)-1;
+ }
+
+ hidden_main_window = CreateWindowEx(0, "MainWClass", "Recv WM_CLOSE msg",
+ WS_OVERLAPPEDWINDOW,
+ CW_USEDEFAULT, CW_USEDEFAULT,
+ CW_USEDEFAULT, CW_USEDEFAULT,
+ (HWND)NULL, (HMENU)NULL,
+ wc.hInstance, (LPVOID)NULL);
+ if(!hidden_main_window) {
+ perror("CreateWindowEx failed");
+ return (DWORD)-1;
+ }
+
+ do {
+ ret = GetMessage(&msg, NULL, 0, 0);
+ if(ret == -1) {
+ perror("GetMessage failed");
+ return (DWORD)-1;
+ }
+ else if(ret) {
+ if(msg.message == WM_APP) {
+ DestroyWindow(hidden_main_window);
+ }
+ else if(msg.hwnd && !TranslateMessage(&msg)) {
+ DispatchMessage(&msg);
+ }
+ }
+ } while(ret);
+
+ hidden_main_window = NULL;
+ return (DWORD)msg.wParam;
+}
#endif
void install_signal_handlers(bool keep_sigalrm)
@@ -659,6 +737,12 @@ void install_signal_handlers(bool keep_sigalrm)
#ifdef WIN32
if(!SetConsoleCtrlHandler(ctrl_event_handler, TRUE))
logmsg("cannot install CTRL event handler");
+ thread_main_window = CreateThread(NULL, 0,
+ &main_window_loop,
+ (LPVOID)GetModuleHandle(NULL),
+ 0, &thread_main_id);
+ if(!thread_main_window || !thread_main_id)
+ logmsg("cannot start main window loop");
#endif
}
@@ -694,5 +778,9 @@ void restore_signal_handlers(bool keep_sigalrm)
#endif
#ifdef WIN32
(void)SetConsoleCtrlHandler(ctrl_event_handler, FALSE);
+ if(thread_main_window && thread_main_id) {
+ if(PostThreadMessage(thread_main_id, WM_APP, 0, 0))
+ (void)WaitForSingleObjectEx(thread_main_window, INFINITE, TRUE);
+ }
#endif
}
--
To stop receiving notification emails like this one, please contact
address@hidden.
- [gnurl] 189/264: scripts/release-notes: fix duplicate output header, (continued)
- [gnurl] 189/264: scripts/release-notes: fix duplicate output header, gnunet, 2020/04/30
- [gnurl] 121/264: curl: allow both --etag-compare and --etag-save with same file name, gnunet, 2020/04/30
- [gnurl] 146/264: tests: verify split initial HTTP requests with CURL_SMALLREQSEND, gnunet, 2020/04/30
- [gnurl] 212/264: test1245: make it work with dynamic FTP server port, gnunet, 2020/04/30
- [gnurl] 245/264: test1154: set a proper name, gnunet, 2020/04/30
- [gnurl] 147/264: schannel: support .P12 or .PFX client certificates, gnunet, 2020/04/30
- [gnurl] 163/264: tests: fix conflict between Cygwin/msys and Windows PIDs, gnunet, 2020/04/30
- [gnurl] 155/264: appveyor: ignore failing 'connect to non-listening proxy' tests, gnunet, 2020/04/30
- [gnurl] 160/264: Revert "file: on Windows, refuse paths that start with \\", gnunet, 2020/04/30
- [gnurl] 192/264: test2043: use revoked.badssl.com instead of revoked.grc.com, gnunet, 2020/04/30
- [gnurl] 170/264: tests/server: add hidden window to gracefully handle WM_CLOSE,
gnunet <=
- [gnurl] 22/264: TODO: TLS-PSK with OpenSSL, gnunet, 2020/04/30
- [gnurl] 40/264: multi: Improve parameter check for curl_multi_remove_handle, gnunet, 2020/04/30
- [gnurl] 56/264: test2100: fix static port instead of dynamic value being used, gnunet, 2020/04/30
- [gnurl] 68/264: cirrus: move the sanitizer build from freebsd 13 to freebsd 12, gnunet, 2020/04/30
- [gnurl] 72/264: nghttp2: 1.12.0 required, gnunet, 2020/04/30
- [gnurl] 89/264: TODO: Set custom client ip when using haproxy protocol, gnunet, 2020/04/30
- [gnurl] 91/264: KNOWN_BUGS: "FTPS needs session reuse", gnunet, 2020/04/30
- [gnurl] 178/264: version: increase buffer space for ssl version output, gnunet, 2020/04/30
- [gnurl] 228/264: lib: fix typos in comments and errormessages, gnunet, 2020/04/30
- [gnurl] 37/264: test 970: verify --write-out '%{json}', gnunet, 2020/04/30