From 9fb33d0beef38b9045c179f1087472c4e7a63085 Mon Sep 17 00:00:00 2001 From: Selva Nair Date: Wed, 11 May 2016 22:11:28 -0400 Subject: [PATCH] Fix exit handling while in modal loops PostThreadMessage used to trigger exit event gets lost while in modal dialog loops such as auth dialog. Replace it by PostMessage and handle it in the status window callback. Fixes openvpn processes left behind if exit is pressed while user-auth dialog is active. Changes after feedback: - Use PostMessage correctly in SuspendOpenVPN() (error pointed out by leobasilio@gmail.com). Signed-off-by: Selva Nair --- openvpn.c | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/openvpn.c b/openvpn.c index 3e8f186..b123069 100644 --- a/openvpn.c +++ b/openvpn.c @@ -848,6 +848,27 @@ StatusDialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) case WM_DESTROY: PostQuitMessage(0); break; + + case WM_OVPN_STOP: + c = (connection_t *) GetProp(hwndDlg, cfgProp); + c->state = disconnecting; + RunDisconnectScript(c, false); + EnableWindow(GetDlgItem(c->hwndStatus, ID_DISCONNECT), FALSE); + EnableWindow(GetDlgItem(c->hwndStatus, ID_RESTART), FALSE); + SetMenuStatus(c, disconnecting); + SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_WAIT_TERM)); + SetEvent(c->exit_event); + break; + + case WM_OVPN_SUSPEND: + c = (connection_t *) GetProp(hwndDlg, cfgProp); + c->state = suspending; + EnableWindow(GetDlgItem(c->hwndStatus, ID_DISCONNECT), FALSE); + EnableWindow(GetDlgItem(c->hwndStatus, ID_RESTART), FALSE); + SetMenuStatus(c, disconnecting); + SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_WAIT_TERM)); + SetEvent(c->exit_event); + break; } return FALSE; } @@ -913,31 +934,7 @@ ThreadOpenVPNStatus(void *p) continue; } - if (msg.hwnd == NULL) - { - switch (msg.message) - { - case WM_OVPN_STOP: - c->state = disconnecting; - RunDisconnectScript(c, false); - EnableWindow(GetDlgItem(c->hwndStatus, ID_DISCONNECT), FALSE); - EnableWindow(GetDlgItem(c->hwndStatus, ID_RESTART), FALSE); - SetMenuStatus(c, disconnecting); - SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_WAIT_TERM)); - SetEvent(c->exit_event); - break; - - case WM_OVPN_SUSPEND: - c->state = suspending; - EnableWindow(GetDlgItem(c->hwndStatus, ID_DISCONNECT), FALSE); - EnableWindow(GetDlgItem(c->hwndStatus, ID_RESTART), FALSE); - SetMenuStatus(c, disconnecting); - SetDlgItemText(c->hwndStatus, ID_TXT_STATUS, LoadLocalizedString(IDS_NFO_STATE_WAIT_TERM)); - SetEvent(c->exit_event); - break; - } - } - else if (IsDialogMessage(c->hwndStatus, &msg) == 0) + if (IsDialogMessage(c->hwndStatus, &msg) == 0) { TranslateMessage(&msg); DispatchMessage(&msg); @@ -1158,14 +1155,14 @@ out: void StopOpenVPN(connection_t *c) { - PostThreadMessage(c->threadId, WM_OVPN_STOP, 0, 0); + PostMessage(c->hwndStatus, WM_OVPN_STOP, 0, 0); } void SuspendOpenVPN(int config) { - PostThreadMessage(o.conn[config].threadId, WM_OVPN_SUSPEND, 0, 0); + PostMessage(o.conn[config].hwndStatus, WM_OVPN_SUSPEND, 0, 0); }